-
Notifications
You must be signed in to change notification settings - Fork 19
/
util.py
55 lines (46 loc) · 1.54 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
from org.openlca.core.database import Daos
import org.openlca.core.model as model
def insert(db, value):
Daos.createBaseDao(db, value.getClass()).insert(value)
def update(db, value):
return Daos.createBaseDao(db, value.getClass()).update(value)
def delete_all(db, clazz):
dao = Daos.createBaseDao(db, clazz)
dao.deleteAll()
def find(db, clazz, name):
""" Find something by name"""
dao = Daos.createBaseDao(db, clazz)
for item in dao.getAll():
if item.name == name:
return item
return None
def find_or_create(db, clazz, name, fn):
obj = find(db, clazz, name)
return obj if obj is not None else fn()
def create_flow(db, name, flow_property,
flow_type=model.FlowType.PRODUCT_FLOW):
flow = find(db, model.Flow, name)
if flow is not None:
return flow
flow = model.Flow()
flow.flowType = flow_type
flow.name = name
flow.referenceFlowProperty = flow_property
fp_factor = model.FlowPropertyFactor()
fp_factor.flowProperty = flow_property
fp_factor.conversionFactor = 1.0
flow.flowPropertyFactors.add(fp_factor)
insert(db, flow)
return flow
def create_exchange(flow, amount=1.0, is_input=False):
e = model.Exchange()
e.input = is_input
e.flow = flow
e.unit = flow.referenceFlowProperty.unitGroup.referenceUnit
e.amountValue = amount
e.flowPropertyFactor = flow.getReferenceFactor()
return e
def find_exchange(flow, process):
for e in process.exchanges:
if e.flow == flow:
return e