-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsample.py
executable file
·297 lines (220 loc) · 8.05 KB
/
sample.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
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
#!/bin/env python3
# Generate a safe sample number.
def getSafeNumber(currentMax):
import numbering as n
return 100
# Function to compute the next readable number for the sample.
def getNextReadable(currentMax, computation = "running"):
if currentMax is None:
print(
"Warning: No highest readable number could be determined \n" +
"This warning should only appear once per order."
)
currentMax = 0
# "running" means a generic running number (n = n + 1).
if computation == "running":
return currentMax + 1
# Do not allow repeating digits as in 311 or 10222.
elif computation == "no_repeating":
# Start with the assumption that the next bigger integer
# is a suitable candidate.
candidate = currentMax + 1
# As long as there are repeating digits (turning the stringified form of
# candidate into a set removes doubles) add one.
while len(set(str(candidate))) < len(str(candidate)):
candidate = candidate + 1
return candidate
# Do not allow permutations of the new max to be smaller numbers.
# E.g. if you have 124 you can't have 421 or 241 or 214 ...
elif computation == "no_swaps":
import itertools # For creating permutations.
# Start with the assumption that the next bigger integer
# is a suitable candidate.
candidate = currentMax + 1
# Create permutation for current string.
sPermutations = [''.join(p) for p in itertools.permutations(str(candidate))]
# Check wether a permutation of candidate is a smaller number.
while any(
item in str(list(range(1, currentMax))) for item in sPermutations
):
# If so try the next.
candidate = candidate + 1
# Also recreate permutation.
sPermutations = [
''.join(p) for p in itertools.permutations(str(candidate))
]
return candidate
# Allow neither repeating nor swappable digits.
elif computation == "save":
import itertools # For creating permutations.
# Start with the assumption that the next bigger integer
# is a suitable candidate.
candidate = currentMax + 1
# Create permutation for current string.
sPermutations = [''.join(p) for p in itertools.permutations(str(candidate))]
# Check wether a permutation of candidate is a smaller number.
while any(
item in str(list(range(1, currentMax))) for item in sPermutations
) or len(set(str(candidate))) < len(str(candidate)):
# If so try the next.
candidate = candidate + 1
# Also recreate permutation.
sPermutations = [
''.join(p) for p in itertools.permutations(str(candidate))
]
return candidate
# Anything else was probably a typo.
else:
raise ValueError("Computation type \"" + computation + "\" is not defined.")
# Function to register a sample from a given yaml file.
def registerSamplesFromYAML(sqliteConnection, yamlFile, verbose = False):
# Overall imports.
import yaml_interop as yi
import sqlite_interop as si
import import_helpers as ih
import helpers
# Get new samples from YAML file.
try:
newSamples = yi.loadDictianories(yamlFile)
except Exception as e:
print("While trying to load YAML sample info into lucent this happend:")
print(e)
exit(1) # This totally has to work.
for s in newSamples:
try:
if verbose: print("Processing sample…")
if verbose: print(s)
except Exception as e:
print(e)
exit(1)
# Get the spot id. Can be supplied as id or as name. getForeignKey handles
# both.
spotId = ih.getForeignKey(s, "spot", sqliteConnection = sqliteConnection)
# Same for matrix.
if verbose: print("Getting matrix id…")
matrixId = ih.getForeignKey(
s
, "matrix"
, "type"
, sqliteConnection = sqliteConnection
)
if verbose: print("Got: " + str(matrixId))
# And for type.
if verbose: print("Getting type id…")
typeId = ih.getForeignKey(s, "type", sqliteConnection = sqliteConnection)
if verbose: print("Got: " + str(typeId))
# Anf for campaign.
if verbose: print("Getting campaign id…")
campaignId = ih.getForeignKey(s, "campaign", sqliteConnection = sqliteConnection)
if verbose: print("Got: " + str(campaignId))
# Retrieve sample id.
if verbose: print("Looking up biggest sample id so far …")
maxSampleId = si.fetchData(
sqliteConnection
, "SELECT MAX(ID_SAMPLE) FROM SAMPLE"
)
if verbose: print("Found: " + str(maxSampleId[0]["MAX(ID_SAMPLE)"]))
# Get next bigger integer.
try:
newMaxSampleId = int(maxSampleId[0]["MAX(ID_SAMPLE)"]) + 1
except TypeError:
if verbose: print(
"Query for highest event id returned none." +
"\nAssuming 0. This warning should only appear once!"
)
maxSampleId = 0
newMaxSampleId = maxSampleId + 1
# Execute insertion.
if verbose: print("Inserting " + str(newMaxSampleId) + " …")
si.executeStatement(
sqliteConnection
, si.buildQueryString(
"./sql/INSERT_SAMPLE.SQL"
, {
"ID_SAMPLE": newMaxSampleId
, "ID_SPOT": spotId
, "ID_CAMPAIGN": campaignId
}
)
)
# Insert into conenction table (n-m-sample-order).
if verbose: print("Attaching orders…")
for o in s["order"]:
if verbose: print("Attaching: ")
if verbose: print(o)
# Fetch the id if it wasn't supplied.
try:
orderId = o["id"]
except KeyError:
orderId = si.fetchData(
sqliteConnection
, "SELECT ID_ORDER FROM `ORDER` WHERE NAME = \'" + o["name"] + "\'"
)[0]["id_order"]
except Exception as e:
print(
"While trying to connect sample "
+ str(newMaxSampleId) + " to an order this exception was raised."
)
print(e)
# Get the highest order specific sample number.
biggestOrderSampleNumber = si.fetchData(
sqliteConnection
, "SELECT MAX(NO_SAMPLE_IN_ORDER) FROM SAMPLE_ORDER X " +
"WHERE X.ID_ORDER = " + str(orderId)
)[0]["MAX(NO_SAMPLE_IN_ORDER)"]
# Calculate the next biggest one.
newOrderSampleNumber = getNextReadable(biggestOrderSampleNumber, "save")
# Create the connection.
try:
si.executeStatement(
sqliteConnection
, si.buildQueryString(
"./sql/INSERT_SAMPLE_ORDER_JOIN.SQL"
, {
"ID_SAMPLE": newMaxSampleId
, "ID_ORDER": orderId
, "PRIORITY": o["priority"]
, "NO_SAMPLE_IN_ORDER": newOrderSampleNumber
}
)
)
except KeyError:
print("Connecting sample to order failed. Was a priority supplied?")
if verbose: print("Attaching events…")
# Attach events to registered samples.
for e in s["event"]:
if verbose: print("Attaching:")
if verbose: print(e)
# First get the biggest id from the events table.
currentMaxId = si.fetchData(
sqliteConnection
, "SELECT MAX(ID_EVENT) FROM EVENT"
)[0]["MAX(ID_EVENT)"]
try:
newMaxId = currentMaxId + 1
except TypeError:
if verbose: print(
"Query for highest event id returned none." +
"\nAssuming 0. This warning should only appear once!"
)
currentMaxId = 0
newMaxId = currentMaxId + 1
# Invoke the insert query.
si.executeStatement(
sqliteConnection
, si.buildQueryString(
"./sql/INSERT_EVENT.SQL"
, {
"ID_EVENT": newMaxId
, "ID_FOREIGN": newMaxSampleId
, "CONCERNED_TABLE": "\'SAMPLE\'"
, "NAME": "\'" + e["name"] + "\'"
, "TYPE": "\'user\'"
, "DESCRIPTION": "\'" + e.get("description", "") + "\'"
, "START_TIME": "\'" + e["start_time"] + "\'"
, "STOP_TIME": "\'" + e.get("stop_time", "") + "\'"
}
)
)
def addOneAndOne(a, b):
return(a + b)