Skip to main content

Analogue to dataclass that uses a numpy-backed array to store values.

Project description

arrayclass

A small @dataclass-like decorator for python classes. The class will store its values in a single contiguous numpy array. It can also be converted to and from plain numpy arrays.

Installation

poetry add dataclasses or pip install dataclasses

Usage

def simulate(steps: int, fs: float) -> np.ndarray:
    # Define the state class.
    # Imagine we had 20 variables here...
    @arrayclasses.arrayclass
    class State:
        x: float
        y: float
    
    def step(t, xy):
        # normally, this would be `x, y, ... = xy`
        s = arrayclasses.from_array(State, xy)
        a = 1 - np.sqrt(s.x ** 2 + s.y ** 2)
        w = 2 * np.pi / (1 * fs)
        
        # normally, this would be `return (..., ...)`
        return State(
            x=a * s.x - w * s.y,
            y=a * s.y + w * s.x,
        )
    
    solved = integrate.solve_ivp(
        fun=step,
        y0=State(-1, 0),
        t_span=(0, steps),
        method="RK45"
    )
    return solved.y

Features

@arrayclasses.arrayclass(dtype=object)  # You can coerce the array dtype manually
class Object:
    x: int  # A single value.
    y: tuple[int, int]  # Will yield np.ndarray windows, not tuples. Should be np.ndarray[float, ...] but requires PEP 646 to work.

a = Object(x=5, y=(2, 3))
print(len(a))  # 3
print(tuple(a))  # (5, 2, 3)

Why would I need this?

You may be forced, or inclined, to use numpy arrays in some situations where classes would be more appropriate.

An example might be scipy.integrate - You are working with an array of numbers that really wants to be a class.

Packing and unpacking tuples is a common workaround. However, when you approach 10 or 20 variables, this gets quite messy fast. Now, you might prefer to use an @arrayclass to get nicer code that plays well with your IDE.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

arrayclasses-0.1.4.tar.gz (4.5 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

arrayclasses-0.1.4-py3-none-any.whl (5.3 kB view details)

Uploaded Python 3

File details

Details for the file arrayclasses-0.1.4.tar.gz.

File metadata

  • Download URL: arrayclasses-0.1.4.tar.gz
  • Upload date:
  • Size: 4.5 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.1.8 CPython/3.8.10 Darwin/22.5.0

File hashes

Hashes for arrayclasses-0.1.4.tar.gz
Algorithm Hash digest
SHA256 4bcca4c3259a2e16ef33c623a8704b8c3eef534b82997deec927d537270b1221
MD5 7673e54b1c639ce9c959498d93cb0ccf
BLAKE2b-256 cba70df31a5dec340e0bce370f781db4d46cae2002eff0faef2e5266a29c7c53

See more details on using hashes here.

File details

Details for the file arrayclasses-0.1.4-py3-none-any.whl.

File metadata

  • Download URL: arrayclasses-0.1.4-py3-none-any.whl
  • Upload date:
  • Size: 5.3 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.1.8 CPython/3.8.10 Darwin/22.5.0

File hashes

Hashes for arrayclasses-0.1.4-py3-none-any.whl
Algorithm Hash digest
SHA256 0ba2e4d3b6af2f87042e700540aef3d0f1f8f38433be70b0de4983bc1295627a
MD5 164a035609a4250f1aa82dc67672b023
BLAKE2b-256 ce37a0864b26aa78dc8336252753a10d3f4f1ebf9be9232fa7b8958677607df6

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page