flashdb.py 788 B

123456789101112131415161718192021222324252627282930
  1. import _flashdb
  2. import struct
  3. KVDB_CTRL = _flashdb.KVDB_CTRL()
  4. class KVDB(_flashdb.KVDB):
  5. def get_blob(self, key, size):
  6. res = super().get_blob(key, size)
  7. if type(res) == list:
  8. return bytes(res)
  9. return None
  10. def set_by_fmt(self, key, v, fmt):
  11. if type(v) == list or type(v) == tuple:
  12. blob = struct.pack(fmt, *v)
  13. return super().set_blob(key, blob)
  14. if type(v) == type(0):
  15. blob = struct.pack(fmt, v)
  16. return super().set_blob(key, blob)
  17. def get_by_fmt(self, key, size, fmt):
  18. res = super().get_blob(key, size)
  19. if res is None:
  20. return None
  21. vs = struct.unpack(fmt, bytes(res))
  22. if len(vs) == 1:
  23. return vs[0]
  24. return vs