-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy path__init__.py
More file actions
72 lines (64 loc) · 2.55 KB
/
Copy path__init__.py
File metadata and controls
72 lines (64 loc) · 2.55 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
#!/usr/bin/python
# -*- coding: utf-8 -*-
__all__ = ["models", "simulators", "genome", "IOsimul"]
__author__ = "Florent Lassalle <f.lassalle@imperial.ac.uk>"
__date__ = "22 August 2016"
__credits__ = """Leonor Palmeira and Laurent Guéguen for initiating the tree2.Node module."""
import os
import inspect
nodelabelprefix = dict(livetip='S', deadtip='E', node='N')
def deleteAttr(obj, attrname):
"""delete object attributes which are generators in order to pickle the object"""
del obj.__dict__[attrname]
def checkDeleteAttr(obj, attrname=None, prompt=None):
"""check presence of object attributes to delete, e.g. generator objects in order to pickle the owner object"""
# cannot pickle generator objects, have to delete it
if attrname in obj.__dict__:
if prompt:
if prompt==True:
doit = raw_input("Delete generator attribute '%s' (y/n) "%repr(attrname))
else:
doit = raw_input(prompt)
while not (doit in ['y', 'n']):
print "answer 'y' (for yes) or 'n' (for no)"
doit = raw_input(prompt)
if doit=='y':
deleteGenneratorAttr(obj, attrname=attrname)
else:
return 1
else:
deleteAttr(obj, attrname=attrname)
return 0
def checkDeleteGenneratorAttr(obj, attrname=None, prompt=None):
# check that objects don't have annoying objects (e.g. generators) to delete before pickling
if isinstance(obj, list):
for subobj in obj:
checkDeleteGenneratorAttr(subobj)
elif isinstance(obj, dict):
for subobj in obj.values():
checkDeleteGenneratorAttr(subobj)
elif isinstance(obj, object):
for atname in obj.__dict__.keys():
if inspect.isgenerator(getattr(obj, atname)):
if checkDeleteAttr(obj, attrname=atname, prompt=prompt):
# deletion of attribute was not performed
return 1
else:
return 0
else:
return -1
# utilitary functions for JSON parsing
def _byteify(data, ignore_dicts = False):
"""parse multiple types of input and return byte (normal) strings instead of unicode strings"""
# if this is a unicode string, return its string representation
if isinstance(data, unicode):
return data.encode('utf-8')
# if this is a list of values, return list of byteified values
if isinstance(data, list):
return [ _byteify(item, ignore_dicts=True) for item in data ]
# if this is a dictionary, return dictionary of byteified keys and values
# but only if we haven't already byteified it
if isinstance(data, dict) and not ignore_dicts:
return { _byteify(key, ignore_dicts=True): _byteify(value, ignore_dicts=True) for key, value in data.iteritems() }
# if it's anything else, return it in its original form
return data