5.3. PikaStdData data structure

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

5.3.1. Install

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.10.0

Run pikaPackage.exe

5.3.2. import

Add in main.py

  1. #main.py
  2. import PikaStdData

5.3.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()

5.3.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 __getitem__(self, i: int) -> any:
  6. pass
  7. # set an arg by the index
  8. def __setitem__(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 __setitem__() method cannot exceed the length of the List. If you want to add members of the list, you need to use the append() method.

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

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

5.3.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)

5.3.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()

5.3.4.1. Dict class methods

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

5.3.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.__getitem__('x') .

5.3.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)

5.3.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.

Example:

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