Basic types
The basic or main types provided by the package are mainly those used in
numerical computation, such as the types covered by the
iso_fortran_env intrinsic module introduced by the 2003 Fortran
standard. Other C types provided by the ctypes module still work with the
rest of the API, so they can be imported from there and used if necessary.
See
here.
Available types
- Integer:
Signed
16-bit-32-bit-64-bit/ Unsigned16-bit-32-bit-64-bit- Real/Float:
- Complex:
64-bit-128-bit(as defined incomplex.hby the C99 standard)- Logical/Boolean:
- Character:
The aliases for these types are listed below, in the type classes section. All aliases are imported into the namespace of the module.
In addition to these, pointer and array types can be created from basic
types using the pointer() function or multipliying them by the
desired array length, respectively. C-style array declaration
can be mimicked by indexing type classes, which looks way prettier.
- pointer(typ)[source]
Return a pointer class. These pointer classes are used to declare pointer arguments in procedure interfaces. For pointers to actual
ctypesvariable instances usecpointer()instead.- Parameters:
typ – Type to get a pointer class from
- Returns:
Pointer class
import fancytypes as ft my_pointer = ft.pointer(ft.real64) print(my_pointer) # Prints "fancytypes.ptr_real64" # These can be chained my_pointer_to_a_pointer = ft.pointer(my_pointer) print(my_pointer_to_a_pointer) # Prints "fancytypes.ptr_ptr_real64"
Array types
To create an array class, simply do:
import fancytypes as ft my_array = 5 * ft.real64 print(my_array) # Prints "fancytypes.real64_array_5" # C-style array declaration is also supported my_char_array = ft.character[8] print(my_char_array) # Prints "fancytypes.character_array_8"It is advised to use NumPy arrays whenever posible instead ctypes arrays, but the package provides them nonetheless.
The package provides some functions to make interoperability easier:
strarray()- Create NumPy character arrays from Python strings
ptrarray()- Create an array of pointers to NumPy arrays on a list
nparray()- Explicitly declare procedure arguments as NumPy arrays
- strarray(items, *, strlen=None)[source]
Return a NumPy character array from a Python string or a list of strings. For the later, the longest string sets the length of the array “rows” that store the individual strings, padding the shorter strings.
Character encoding
Unicode outputs are explicitly disabled to ensure 8-bit elements. This function was originally meant for paths, so it will not fit all usecases. ASCII outside of the minimal 8-bit is not supported.
- Parameters:
items (
str,listortuple) – String or list/tuple of stringsstrlen (
int, optional) – Specify a string length, default is the longest required
- Returns:
NumPy character array of 8-bit characters
- Return type:
numpy.ndarray
import fancytypes as ft my_strings = ['data.dat', 'some_text.txt', 'user.cfg'] my_array = ft.strarray(my_strings) print(my_array) # Prints "[b'data.dat', b'some_text.txt', b'user.cfg']"
- ptrarray(arrays, typ)[source]
Return an array of pointers to NumPy arrays on a list. This can be used to pass an arbitrary number of arrays to a procedure. These arrays do not have to be contiguous in memory, which forces us to pass them like this.
- Parameters:
arrays (
listortuple) – List/tuple of arraystyp – Type to make the pointers to
- Returns:
ctypesarray instance
import numpy as np import fancytypes as ft my_array_1 = np.array([1, 2, 3, 4, 5], dtype=np.int32) my_array_2 = np.array([6, 7, 8, 9, 10], dtype=np.int32) my_array_3 = np.array([11, 12, 13, 14, 15], dtype=np.int32) my_arrays = [my_array_1, my_array_2, my_array_3] my_pointer_array = ft.ptrarray(my_arrays, ft.int32) print(my_pointer_array[0][:5]) # Prints [1, 2, 3, 4, 5] print(my_pointer_array[1][:5]) # Prints [6, 7, 8, 9, 10] print(my_pointer_array[2][:5]) # Prints [11, 12, 13, 14, 15]
- nparray(typ, *, ndim=None, shape=None, flags=None)[source]
Return a
ndpointerobject fromnumpy.ctypeslib. These can be used in procedure interfaces to explicitly declare NumPy arrays as arguments. This function is a wrapper around ndpointer, and its optional keyword arguments are described there.- Parameters:
typ – Type of the array
- Returns:
NumPy ndpointer object
- Return type:
ndpointer
import fancytypes as ft my_ndpointer = ft.nparray(ft.real64) print(my_ndpointer) # Prints "numpy.ctypeslib.ndpointer_<f8"
Placeholder text for references to documentation that I will write eventually
Type classes
- class FancyRealSingle[source]
Class for 32-bit real numbers, aka floats or singles (like me).
- Aliases:
real32
float32
single
sp
- class FancyRealDouble[source]
Class for 64-bit real numbers, aka floats-64 or doubles.
- Aliases:
real64
float64
double
dp
- class FancyLogical[source]
Class for 8-bit logical values, aka booleans or bools.
- Aliases:
logical
boolean
bool