Skip to content

Use list of numbers as a key or values  #228

Description

@vsraptor

With those two convert list of nums to string to use as a key or value and back :

      # list of numbers =to=> string
	def nums2str(nums, itype='H'): #B:int8,H:int16,I:int32,Q:int64
		return struct.pack(f">{len(nums)}{itype}", *nums)

	#string =2=> tuple of numbers, use list()
	def str2nums(b, itype='H'):
		size = 1 if itype == 'B' else ('HIQ'.index(itype) + 1) * 2
		return struct.unpack(f">{len(b)//size}{itype}", bytes(b,'utf-8'))	

       index.Set(nums2str([454,7889]), nums2str([1,2,3,4]))

       m = index.Get(nums2str([454,7889])
       res = str2nums(m.GetValue())

u can add this to the docs

bytes(b,'utf-8') : assumes u got back string if u got bytestring just use 'b' directly

Activity

  1. vsraptor commented on Jun 23, 2021

    @vsraptor
    Author

    i still have one problem though :

    always returns string even if I stored bytestring

    In [314]: kv.get('abc')
    Out[314]: '\x00\x01\x00\x02\x00\x03\x00\x04'

    it should return b'\x00\x01\x00\x02\x00\x03\x00\x04', but does not..

    I.e. there is no way to figure did u stored a list or scalar, so u can convert it back

  2. locked and limited conversation to collaborators on Jun 24, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions