Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
def test_slots(self, session):
# no slots in an empty gnucash file but the default_currency
slots = session.query(Slot._name).all()
assert slots == []
with pytest.raises(KeyError):
b["a/b/c"]
del b["a"]["b"]
book.session.flush()
assert len(b["a"].slots) == 0
with pytest.raises(TypeError):
b["a"] = b
with pytest.raises(KeyError):
del b["a/n"]
del b[:]
book.session.flush()
assert {n for (n,) in book.session.query(Slot._name)} == set([])
for k, v in b["a/b/c/d"].iteritems():
assert k == "f" or k == "t"
print(b.slots)
assert b["a/b/c/d"].get("t", "hello") == "ok"
assert b["a/b/c/d"].get("not there", "hello") == "hello"
del b["a/b/c/d/t"]
assert repr(b["a"]) == ""
with pytest.raises(TypeError):
b["a/b/c/d/f"] = 4
with pytest.raises(TypeError):
b["a/b/c"] = True
book.flush()
assert {n for (n,) in book.session.query(Slot._name)} == {"a", "a/b", "a/b/c", "a/b/c/d", "a/b/c/d/f"}
# delete some elements
del b["a"]["b"][:]
book.flush()
assert {n for (n,) in book.session.query(Slot._name)} == {"a", "a/b"}
book.flush()
assert len(b["a"].slots) == 1
assert len(b["a/b"].slots) == 0
with pytest.raises(KeyError):
b["a/b/c"]
del b["a"]["b"]
book.session.flush()
assert len(b["a"].slots) == 0
__mapper_args__ = {
'polymorphic_on': slot_type,
}
def __init__(self, name, value=None, obj_guid=None):
self.name = name
if value is not None:
self.value = value
if obj_guid is not None:
self.obj_guid = obj_guid
def __str__(self):
return "<{} {}={!r}>".format(self.__class__.__name__, self.name, self.value)
class SlotSimple(Slot):
__mapper_args__ = {
'polymorphic_identity': -1,
}
_python_type = ()
@property
def value(self):
return getattr(self, self._field)
@value.setter
def value(self, value):
setattr(self, self._field, value)
def __eq__(self, other):
return (isinstance(other, self.__class__)
def slots(cls):
rel = relation('Slot',
primaryjoin=foreign(Slot.obj_guid) == cls.guid,
cascade='all, delete-orphan',
collection_class=CallableList,
)
return rel
field="timespec_val",
col_type=_DateTime(),
col_default=None,
)
class SlotFrame(DictWrapper, Slot):
__mapper_args__ = {
'polymorphic_identity': KVP_Type.KVP_TYPE_FRAME
}
_python_type = (dict,)
guid_val = Column('guid_val', VARCHAR(length=32))
slots = relation('Slot',
primaryjoin=foreign(Slot.obj_guid) == guid_val,
cascade='all, delete-orphan',
collection_class=CallableList,
single_parent=True,
backref=backref("parent", remote_side=guid_val),
)
@property
def value(self):
# convert to dict
return {sl.name: sl.value for sl in self.slots}
@value.setter
def value(self, value):
self.slots = [slot(parent=self, name=k, value=v) for k, v in value.items()]
def slot(parent, name, value):
if isinstance(parent, SlotFrame):
name = parent._name + "/" + name
guid_parent = parent.guid_val
else:
guid_parent = parent.guid
# handle datetime before others (as otherwise can be mixed with date)
if isinstance(value, datetime.datetime):
return SlotTime(name=name, value=value, obj_guid=guid_parent)
for cls in get_all_subclasses(Slot):
if isinstance(value, cls._python_type) and cls != SlotFrame and cls != SlotList:
return cls(name=name, value=value, obj_guid=guid_parent)
if isinstance(value, dict):
# transform a dict to Frame/Slots
sf = SlotFrame(name=name, obj_guid=guid_parent)
for k, v in value.items():
sl = slot(parent=sf, name=k, value=v)
sl.parent = sf
return sf
if isinstance(value, list):
# transform a list to List/Slots
sf = SlotList(name=name)
for i, v in enumerate(value):
sl = slot(parent=sf, name=str(i), value=v)
pytype=(float,),
KVPtype=KVP_Type.KVP_TYPE_DOUBLE,
field="double_val",
col_type=REAL(),
col_default=0,
)
SlotTime = define_simpleslot(postfix="Time",
pytype=(datetime.time,),
KVPtype=KVP_Type.KVP_TYPE_TIMESPEC,
field="timespec_val",
col_type=_DateTime(),
col_default=None,
)
class SlotFrame(DictWrapper, Slot):
__mapper_args__ = {
'polymorphic_identity': KVP_Type.KVP_TYPE_FRAME
}
_python_type = (dict,)
guid_val = Column('guid_val', VARCHAR(length=32))
slots = relation('Slot',
primaryjoin=foreign(Slot.obj_guid) == guid_val,
cascade='all, delete-orphan',
collection_class=CallableList,
single_parent=True,
backref=backref("parent", remote_side=guid_val),
)
sl = slot(parent=sf, name=k, value=v)
sl.parent = sf
return sf
if isinstance(value, list):
# transform a list to List/Slots
sf = SlotList(name=name)
for i, v in enumerate(value):
sl = slot(parent=sf, name=str(i), value=v)
sl.parent = sf
return sf
raise ValueError("Cannot handle type of '{}'".format(value))
class SlotNumeric(Slot):
__mapper_args__ = {
'polymorphic_identity': KVP_Type.KVP_TYPE_NUMERIC
}
_python_type = (tuple, decimal.Decimal)
_numeric_val_num = Column('numeric_val_num', BIGINT(), nullable=True, default=0)
_numeric_val_denom = Column('numeric_val_denom', BIGINT(), nullable=True, default=1)
value = hybrid_property_gncnumeric(_numeric_val_num, _numeric_val_denom)
SlotDate = define_simpleslot(postfix="Date",
pytype=(datetime.date,),
KVPtype=KVP_Type.KVP_TYPE_GDATE,
field="gdate_val",
col_type=_Date(),
col_default=None,