-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathconversions.py
More file actions
executable file
·78 lines (68 loc) · 2.05 KB
/
Copy pathconversions.py
File metadata and controls
executable file
·78 lines (68 loc) · 2.05 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
73
74
75
76
77
78
__author__ = "Mike Rightmire"
__copyright__ = "BioCom Software"
__license__ = "BioCom_GPL_Copyright.py"
__version__ = "0.9.0.0"
__maintainer__ = "Mike Rightmire"
__email__ = "rightmirem@utr.net"
__status__ = "Test"
import ast
# Type checks that use functional checks instead of isinstance or type
def _isdict(data):
try:
data.keys()
return True
except AttributeError:
return False
def _isstr(data):
try:
data + "string test"
return True
except TypeError:
return False
def _islist(data):
try:
data.insert(0,"THIS_IS_FALSE_DATA_ADDED_AS_A_TEST")
data.pop(0)
return True
except AttributeError or TypeError:
return False
#To lists
def strToList(data, delim = " "):
if _islist(data): return data
if not _isstr(data): raise TypeError("str_to_list requires string input")
else: data = data.split(delim);
return data
# To Dictionaries
def _literal_str_to_dict(data):
if _isdict(data): return data
data = ast.literal_eval(data)
return data
def _assumptive_str_to_dict(data):
if _isdict(data): return data
data = str_to_list(data, ",")
data = list_to_dict(data)
return data
def listToDict(data):
_dict = {}
if _isdict(data): return data
if _islist(data) is False: raise TypeError("str_to_list requires list input")
loop = True
while loop:
try:
key = data.pop(0)
try:
value = data.pop(0)
except IndexError:
value = None
_dict[key] = value
except IndexError:
loop = False
return _dict
def strToDict(data):
if _isdict(data): return data
data = "".join(c for c in data if c not in "\"\'\]\[")
if ":" in data: return _literal_str_to_dict(data)
else: return _assumptive_str_to_dict(data)
if __name__ == "__main__":
d = str_to_dict("['a','b','c']")
print "d = ", d