Keywords: gildas-python

Next: The SicVar class Up: How SIC variables are Previous: How SIC variables are Contents Index

### Comments on the strategy

Concerning variables handling from both SIC and Python, the first priority was to manage the same data in memory from the two processes, e.g. a modification of a variable in one process should be instantaneously visible in the other with no particular resynchronization.

Another consideration is that Python does not natively handle multi-dimensionnal arrays. This feature can be easily added, but we need the `NumPy` package.

It appeared quite naturally that the solution was to use the `PyArray_SimpleNewFromData()` function of the `NumPy` C-API: it can construct a `numpy.ndarray` from the memory address of an array, its dimensions and its datatype.

With this feature the strategy can be summarized as follow:

- `NumPy` freezes its `array` definition (attributes, size in memory and so on) at compilation time. We can not add to it more interesting features, especially new attributes or write protection for read-only variables.
- an overlay had to be created with one purpose: defining a set of features like
  - the ability to define attributes to variables,
  - the protection of read-only variables,
  - a special treatment for character string variables (concatenation),
  - ...
  and all this must keep unmodified all the `numpy.ndarray` features (indexing, array handling, special functions and attributes, and so on)
- this overlay was created as a Python class named `SicVar`. It has three attributes: a string named `__sicname__`, which is the SIC name of the variable, a `numpy.ndarray` named `__sicdata__`, which shares its data with the SIC variable, and its SIC-level (global or local) stored as an integer named `__siclevel__`. All `numpy.ndarray`-like requests on a `SicVar` are redirected to the `__sicdata__` attribute: with this, `SicVar` instances behaves like any other `numpy.ndarray`.

Next: The SicVar class Up: How SIC variables are Previous: How SIC variables are Contents Index