flashdb.py 831 B

1234567891011121314151617181920212223242526272829
  1. import _flashdb
  2. import struct
  3. class FDB(_flashdb.FlashDB):
  4. def kv_get_blob(self, kvdb, key, size):
  5. res = super().kv_get_blob(kvdb, key, size)
  6. if type(res) == type([]):
  7. return bytes(res)
  8. return None
  9. def kv_set_by_fmt(self, kvdb, key, v, fmt):
  10. if type(v) == type([]) or type(v) == type(()):
  11. blob = struct.pack(fmt, *v)
  12. return super().kv_set_blob(kvdb, key, blob)
  13. if type(v) == type(0):
  14. blob = struct.pack(fmt, v)
  15. return super().kv_set_blob(kvdb, key, blob)
  16. def kv_get_by_fmt(self, kvdb, key, size, fmt):
  17. res = super().kv_get_blob(kvdb, key, size)
  18. if res is None:
  19. return None
  20. vs = struct.unpack(fmt, bytes(res))
  21. if len(vs) == 1:
  22. return vs[0]
  23. return vs