This repository has been archived by the owner on Apr 18, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 8
/
util.py
132 lines (108 loc) · 3.1 KB
/
util.py
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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
#!/usr/bin/env python3
import subprocess
import pickle
import hashlib
import os
from typing import List
from .object_model import *
def isLoc(loc):
try:
ext = loc.split('.')[1]
return True
except:
return False
def isJarvisClass(obj):
return type(obj) == Artifact or type(obj) == Action or type(obj) == Literal
def isLiteral(obj):
return type(obj) == Literal
def isPickle(loc):
try:
return loc.split('.')[1] == 'pkl'
except:
return False
def isCsv(loc):
try:
return loc.split('.')[1] == 'csv'
except:
return False
def isIpynb(loc):
return loc.split('.')[1] == 'ipynb'
def isIterable(obj):
return type(obj) == list or type(obj) == tuple
def runProc(bashCommand):
process = subprocess.Popen(bashCommand.split(), stdout=subprocess.PIPE)
output, error = process.communicate()
try:
return str(output, 'UTF-8')
except:
return output
def pickleTo(obj, loc):
with open(loc, 'wb') as f:
pickle.dump(obj, f)
def unpickle(loc):
with open(loc, 'rb') as f:
x = pickle.load(f)
return x
def isOrphan(obj):
return type(obj) == Literal or (type(obj) == Artifact and obj.parent is None)
def isNumber(s):
if type(s) == int or type(s) == float:
return True
try:
float(s)
return True
except:
return False
def loadArtifact(loc):
if isPickle(loc):
x = unpickle(loc)
elif isLoc(loc):
with open(loc, 'r') as f:
x = [i.strip() for i in f.readlines() if i.strip()]
if len(x) == 1:
x = x[0]
else:
raise NotImplementedError("Don't know how to load that file.")
return x
def master_pop(literals):
if not literals:
return True
subtreeMaxed = master_pop(literals[0:-1])
if subtreeMaxed:
popSuccess = literals[-1].__pop__()
if not popSuccess:
return True
[literal.__reset__() for literal in literals[0:-1]]
return False
def activate(pseudoArtifact):
if type(pseudoArtifact) == Literal:
pseudoArtifact.__enable__()
elif not isOrphan(pseudoArtifact) and pseudoArtifact.parent.in_artifacts:
for in_art in pseudoArtifact.parent.in_artifacts:
activate(in_art)
def md5(fname):
# Credit: https://stackoverflow.com/a/3431838
hash_md5 = hashlib.md5()
with open(fname, "rb") as f:
for chunk in iter(lambda: f.read(4096), b""):
hash_md5.update(chunk)
return hash_md5.hexdigest()
def plating(in_artifacts: List[Artifact]):
multiplier = []
for _in in in_artifacts:
if isLiteral(_in) and _in.__oneByOne__:
value = len(_in.v)
if value > 1:
multiplier.append(str(len(_in.v)))
plate_label = 'x'.join(multiplier)
if plate_label:
return plate_label
return None
class chinto(object):
def __init__(self, target):
self.original_dir = os.getcwd()
self.target = target
def __enter__(self):
os.chdir(self.target)
def __exit__(self, type, value, traceback):
os.chdir(self.original_dir)