Skip to main content

A simple and easy-to-use C/C++/MATLAB-like structure type for Python

Project description

Yarpiz Structure (ypstruct)

A simple and easy-to-use C/C++/MATLAB-like structure type for Python

Installation

You can use pip to install ypstruct:

pip install ypstruct

If you want to install from source code, you can download from github or simply use:

git clone https://github.com/smkalami/ypstruct

and then, run this:

python setup.py install

Sample Usage

The structure is defined by struct class. Also an alias of this class is defined and available as structure.

A simple usage code of ypstruct.struct follows.

from ypstruct import *

p = struct()
p.x = 3
p.y = 4
p.A = p.x * p.y
print(p)

The output will be:

struct({'x': 3, 'y': 4, 'A': 12})

Here, after importing the struct (and its alias structure) from ypstruct, an instance of struct is created and then fields x and y are defined. Then, a new field A is added to the structure. Finally, the string representation of the struct is printed.

In the previous code, you can simply use the following method to create and initialize the structure.

p = struct(x=3, y=4)
p.A = p.x * p.y

Merging Structures

It is possible to merge two struct objects. For example let's define two structures as:

a = struct(x=1, y=2, z=3)
b = struct(y=5, t=20, u=30)

We can merge these structure using + operator:

c = a + b
print(c)

The output will be:

struct({'x': 1, 'y': 5, 'z': 3, 't': 20, 'u': 30})

Repeating a Structure

Sometimes we need to repeat/replicate a structure. For example, assume that we are going to implement an Evolutionary Algorithm and we defined the individuals as struct objects. First we need to create a template:

empty_individual = struct(pos=None, fval=None)

Then we can initialize the population array using following code:

pop_size = 10
pop = empty_individual.repeat(pop_size)

Or simply we can use * operator to perform replication:

pop = empty_individual * pop_size

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

ypstruct-0.0.2.tar.gz (3.0 kB view details)

Uploaded Source

File details

Details for the file ypstruct-0.0.2.tar.gz.

File metadata

  • Download URL: ypstruct-0.0.2.tar.gz
  • Upload date:
  • Size: 3.0 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.2.0 pkginfo/1.5.0.1 requests/2.24.0 setuptools/49.2.1 requests-toolbelt/0.9.1 tqdm/4.50.2 CPython/3.9.0

File hashes

Hashes for ypstruct-0.0.2.tar.gz
Algorithm Hash digest
SHA256 26143a45978083849bff2c373041c96ae9dbfa226164e82fb51ec966065f8361
MD5 ca2ac36539c8dd32ec4f4d5f2cbb5ba3
BLAKE2b-256 05917300f279cd63f1b5e3444c765ec26c4027f34ee5d7124a5cb74ed0b0304c

See more details on using hashes here.

Supported by

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