array
Hint:
considering unicode array is deprecated since Python3.3, and will be removed in Python3.16, it's not implemented
Hint:
since python3.13, 'w' (Py_UCS4) array is introduced. here we use Rune in std/unicode as its item type.
XXX: the extend is used by array(c, ls), And Py_UCS4-array's extend is not generic, so it must be placed before array proc
Procs
func `[]=`(self: var PyArray[Py_UCS4]; i: int): PyStr {.inline, ...raises: [], tags: [], forbids: [].}
- Source Edit
func buffer_info[T](arr: PyArray[T]): tuple[address: int, length: int]
- Source Edit
func byteswap[T](arr: var PyArray[T])
-
array.byteswap
Currently only compilable for c_* types, a.k.a. not for Nim's int*, e.g. int16
Hint: trial of using this method on PyArray[int*] may lead to compile-error of C compiler.Example:
when sizeof(cshort) == 2: var arr = array('h', [1, 2]) arr.byteswap() assert arr[0] == 256, $arr[0] # int from \x01\x00 assert arr[1] == 512, $arr[1]
Source Edit func newPyArray[T](): PyArray[T]
- Source Edit
Macros
macro array(typecode: static[char]): PyArray
-
Example:
var a = array('i') assert a.typecode == 'i' assert len(a) == 0 a.append(3) assert a.len == 1 and a[0] == 3
Source Edit macro array(typecode: static[char]; initializer: typed): PyArray
-
bytes or bytearray, a Unicode string, or iterable over elements of the appropriate type.
initializer can be a bracket stmt, no need to manually add type convert, see examples
Example:
assert array('i', [1, 2])[1] == c_int(2) assert array('b', bytes("123"))[2] == c_schar('3')
Source Edit