src/pylib/Lib/array

Search:
Group by:
Source   Edit  

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

Types

Py_UCS4 = Rune
inner Source   Edit  
PyArray[T] = distinct PyList[T]
Source   Edit  
SomeChar = char | cschar | cuchar
In C, char, unsigned char, signed char are three distinct types, that's, char is either signed or unsigned, which is implementation-dependent, unlike other integer types, e.g. int is alias of signed int Source   Edit  

Consts

typecodes = "bBuwhHiIlLqQfd"
Source   Edit  

Procs

func `$`(arr: PyArray[Py_UCS4]): string {.inline, ...raises: [], tags: [],
    forbids: [].}
Source   Edit  
func `$`[T](arr: PyArray[T]): string {.inline.}
Source   Edit  
func `<`[A, B](arr: PyArray[A]; other: PyArray[B]): bool {.inline.}
Source   Edit  
func `<=`[A, B](arr: PyArray[A]; other: PyArray[B]): bool {.inline.}
Source   Edit  
func `==`[A, B](arr: PyArray[A]; other: PyArray[B]): bool {.inline.}
Source   Edit  
func `@`[T](arr: PyArray[T]): seq[T] {.inline.}
Source   Edit  
func `[]=`(self: var PyArray[Py_UCS4]; i: int): PyStr {.inline, ...raises: [],
    tags: [], forbids: [].}
Source   Edit  
func `[]=`(self: var PyArray[Py_UCS4]; i: int; v: char) {.inline, ...raises: [],
    tags: [], forbids: [].}

Example:

from std/unicode import Rune
var a = array('w', "123")
a[0] = Rune(65)
a[1] = '3'
Source   Edit  
func `[]=`(self: var PyArray[Py_UCS4]; i: int; v: string) {.inline,
    ...raises: [TypeError], tags: [], forbids: [].}
Source   Edit  
func append(self: var PyArray[Py_UCS4]; s: PyStr) {.inline, ...raises: [TypeError],
    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 extend(self: var PyArray[Py_UCS4]; s: PyStr) {....raises: [], tags: [],
    forbids: [].}
Source   Edit  
func frombytes[C: SomeChar](arr: var PyArray[C]; buffer: BytesLike)
append byte from buffer to arr Source   Edit  
func frombytes[T: not SomeChar](arr: var PyArray[T]; buffer: BytesLike)
append byte from buffer to arr

Example:

when sizeof(cshort) == 2:
  var h = array('h', [1, 2])
  h.frombytes(bytes("\x05\x00"))
  when cpuEndian == littleEndian:
    assert h[2] == 5
  else:
    assert h[2] == 1280
Source   Edit  
func fromfile[T](arr: var PyArray[T]; f: File; n: int)
Currrently only for Nim's File Source   Edit  
func fromlist[T](arr: var PyArray[T]; ls: PyList[T])
Source   Edit  
func fromunicode(self: var PyArray[Py_UCS4]; s: PyStr) {.inline, ...raises: [],
    tags: [], forbids: [].}
Note: as PyArray here is static-typed, unlike CPython's, no ValueError will be raised
Source   Edit  
func getPtr[T](arr: var PyArray[T]; i: Natural | Natural): ptr T
EXT. unstable. Source   Edit  
func len[T](arr: PyArray[T]): int
Source   Edit  
func newPyArray[T](): PyArray[T]
Source   Edit  
func repr[T](arr: PyArray[T]): string
alias for $arr Source   Edit  
func setLen[T](arr: var PyArray[T]; n: int)
Source   Edit  
func tobytes(arr: PyArray[SomeChar]): PyBytes
Source   Edit  
func tobytes[T: not SomeChar](arr: PyArray[T]): PyBytes
Source   Edit  
func tofile[T](arr: var PyArray[T]; f: File)
Currrently only for Nim's File Source   Edit  
func tolist[T](arr: PyArray[T]): PyList[T]
Source   Edit  
func tolist[T](arr: var PyArray[T]): var PyList[T]
Source   Edit  
func tounicode(arr: PyArray[Py_UCS4]): PyStr {.inline, ...raises: [], tags: [],
    forbids: [].}
Source   Edit  

Iterators

iterator mitems[T](arr: var PyArray[T]): var T
EXT.

Example:

var arr = array('i', [1, 2])
for i in arr.mitems:
  i *= 2
assert arr == array('i', [2, 4])
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  

Templates

template `*=`[T](arr: PyArray[T]; n: int)
Source   Edit  
template `+=`[T](arr: PyArray[T]; x: untyped)
Source   Edit  
template `[]`[T](arr: PyArray[T]; i: int): T
Source   Edit  
template `[]=`[T](arr: PyArray[T]; i: int; val: T)
Source   Edit  
template append[T](arr: PyArray[T]; x: T)
Source   Edit  
template clear[T](arr: PyArray[T])
Source   Edit  
template contains[T](arr: PyArray[T]; x: T): bool
Source   Edit  
template delitem[T](arr: PyArray[T]; n: int)
Source   Edit  
template extend[T](arr: PyArray[T]; x: untyped)
Source   Edit  
template insert[T](arr: PyArray[T]; x: T)
Source   Edit  
template items[T](arr: PyArray[T]): untyped
Source   Edit  
template itemsize[T](arr: PyArray[T]): int
Source   Edit  
template newPyArray[T](x): PyArray[T]
unlike array, when x is a literal, type conversion is always needed.

Example:

discard newPyArray[cint]([1.cint, 2])
# or write: array('i', [1, 2])
Source   Edit  
template pop[T](arr: PyArray[T])
Source   Edit  
template pop[T](arr: PyArray[T]; i: int)
Source   Edit  
template remove[T](arr: PyArray[T]; x: T)
Source   Edit  
template reverse[T](arr: PyArray[T])
Source   Edit  
template typecode(_: PyArray[cdouble]): char
Source   Edit  
template typecode(_: PyArray[cfloat]): char
Source   Edit  
template typecode(_: PyArray[cint]): char
Source   Edit  
template typecode(_: PyArray[clong]): char
Source   Edit  
template typecode(_: PyArray[clonglong]): char
Source   Edit  
template typecode(_: PyArray[cschar]): char
Source   Edit  
template typecode(_: PyArray[cshort]): char
Source   Edit  
template typecode(_: PyArray[cuchar]): char
Source   Edit  
template typecode(_: PyArray[cuint]): char
Source   Edit  
template typecode(_: PyArray[culong]): char
Source   Edit  
template typecode(_: PyArray[culonglong]): char
Source   Edit  
template typecode(_: PyArray[cushort]): char
Source   Edit  
template typecode(_: PyArray[Py_UCS4]): char
Source   Edit