19. PikaStdData data structure

PikaStdData data structure library provides List (list), Dict (dictionary) data structure.

19.1. Install

  1. Add the dependency of PikaStdLib to requestment.txt. The version number of PikaStdLib should be the same as the version number of the kernel.
  1. PikaStdLib==v1.6.1
  1. Run pikaPackage.exe

19.2. import

Add in main.py

  1. #main.py
  2. import PikaStdData

19.3. class List():

The List class provides the List list function. By creating an object of the List class, a list can be created. Such as:

  1. import PikaStdData
  2. list = PikaStdData.List()

19.3.1. Methods of the List class

  1. # add an arg after the end of list
  2. def append(self, arg: any):
  3. pass
  4. # get an arg by the index
  5. def get(self, i: int) -> any:
  6. pass
  7. # set an arg by the index
  8. def set(self, i: int, arg: any):
  9. pass
  10. # get the length of list
  11. def len(self) -> int:
  12. pass

Note that the index of the set() method cannot exceed the length of the List. If you want to add members of the list, you need to use the append() method.

19.3.2. Use ‘[]’ brackets to index the list

List objects can be indexed using ‘[]’. list[1] = a is equivalent to list.set(1, a), and a = list[1] is equivalent to a = list.get(1).

19.3.3. Use for loop to iterate over List

List objects support for loop traversal example:

  1. import PikaStdData
  2. list = PikaStdData.List()
  3. list.append(1)
  4. list.append('eee')
  5. list.append(23.44)
  6. for item in list:
  7. print(item)

19.4. class Dict():

The Dict class provides the Dict dictionary function, and a dictionary can be created by creating an object of the Dict class. Such as:

  1. import PikaStdData
  2. dict = PikaStdData.Dict()

19.4.1. Dict class methods

  1. # get an arg by the key
  2. def get(self, key: str) -> any:
  3. pass
  4. # set an arg by the key
  5. def set(self, key: str, arg: any):
  6. pass
  7. # remove an arg by the key
  8. def remove(self, key: str):
  9. pass

19.4.2. Index dictionary using ‘[]’ brackets

Dict objects can be indexed using ‘[]’. dict['x'] = a is equivalent to dict.set('x', a) and a = dict['x'] is equivalent to a = dict.get('x').

19.4.3. Using a for loop to iterate over a Dict

Dict objects support for loop traversal example:

  1. import PikaStdData
  2. dict = PikaStdData.Dict()
  3. dict['a'] = 1
  4. dict['b'] = 'eee'
  5. dict['c'] = 23.44
  6. for item in dict:
  7. print(item)

19.5. class ByteArray(List)

[Note]: The version of PikaStdData requires at least v1.5.3

The ByteArray class provides the ByteArray byte array function. By creating an object of the ByteArray class, a byte array can be created.

Such as:

  1. import PikaStdData
  2. bytes = PikaStdData.ByteArray()

The ByteArray class inherits from the List class and can use the methods of the List class.

19.5.1. Methods of the ByteArray class

  1. # convert a string to ByteArray
  2. def fromString(self, s:str):
  3. pass

Example:

  1. >>> bytes = PikaStdData.ByteArray()
  2. >>> bytes.fromString('test')
  3. >>> for byte in bytes:
  4. ... print(byte)
  5. ...
  6. 116
  7. 101
  8. 115
  9. 116
  10. >>> bytes.append(0xff)
  11. >>> bytes.append(0x0f)
  12. >>> print(bytes[4])
  13. 255
  14. >>> print(bytes[5])
  15. 15