Skip to content

Use list of numbers as a key or values  #228

@vsraptor

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions