Structer is a structurer written in Python based on C language structs.
Project description
Create similar C structs in Python intuitively!
📌 Summary
🛠️ Installation
Installation from PyPI:
pip3 install structer
Installation from GitHub:
pip3 install git+https://github.com/d3cryptofc/structer.git
🏃♀️ Getting Started
I assure you it's easier than it looks.
1. Creating your first struct model
Create your struct model using structer.structfy(name, fields)
:
from structer import structfy, Char, Str, Field
Person = structfy('Person', [
Field('name', Str(15)),
Field('gender', Char())
])
Notes:
structer.Str
is a short nickname forstructer.String
.structer.Char
is likestructer.String(1)
, but specialized for this.
2. Instance generation and data storage
You can create an instance by passing the values as an argument:
>>> p = Person(name='John', gender='M')
>>> p
Person(name(15)='John', gender(1)='M') -> 16
Or, perhaps you want to make the modifications individually with the already created instance:
>>> p = Person()
>>> p
Person(name(15)='', gender(1)='') -> 16
>>> p.name = 'John'
>>> p.gender = 'M'
>>> p
Person(name(15)='John', gender(1)='M') -> 16
3. Representation and size
You may have noticed that the object representation shows the size of each field and the total size of all fields.
To find out the total size of your instance, use the len
function:
>>> len(p)
16
Maybe you want to know the total size of the struct model without having to create an instance, access the __struct_size__
attribute (size given in bytes):
>>> Person.__struct_size__
16
4. Getting the serialized data
Just access the __struct_binary__
attribute:
>>> p.__struct_binary__
b'John\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00M'
Ready! Now you do whatever you want with it.
💬 Frequently Asked Questions (FAQ)
1. What are structs?
If you've never programmed in C, you might initially think that a struct is similar to a dataclass, but unlike a dataclass, structs map fields in memory, so that you have all the data glued together but delimited by their sizes.
You can imagine that internally the mapping is done like:
# Your field sizes.
f_first_name = 10
f_gender = 1
f_age = 2
# Memory containing the data.
memory = 'John M23'
# Accessing data delimited by its field sizes.
memory[0:f_first_name] # 'John '
memory[f_first_name:f_first_name + f_gender] # 'M'
memory[f_first_name + f_gender:f_first_name + f_gender + f_age] # '23'
But a struct abstracts this, so that usage is stupidly simple:
person.first_name = 'John'
person.gender = 'M'
person.age = 23
It's important to say that the first example is very crude, structs use bytes instead of strings, allowing you to save an absurd amount of space.
For example, in age
of the example above, '23'
was inserted as a string, which consumes 2 bytes in memory, but we could represent numbers from 0 to 255 (00 to FF) using a single byte.
Or better yet, imagine that you want to store the number 18,000,000,000,000,000,000
(18 quintillion) in memory, however storing it in a text file as a string would consume 20 bytes, whereas 8 bytes would be enough to represent the number.
The waste of these 12 bytes would represent twice the number itself, so much so that on a large scale this would throw a huge amount of storage space into the trash, around 60% of the space could be saved, that would be going from 1 TB to just 400G.
2. Why use structs in Python?
Structs are like models for mapping memory space and organizing data, and, unlike C (because it is compiled), in Python each instance that is created will consume space in RAM, just like any other Python class instance.
The point is not to use structs thinking that it will be a lighter alternative to a dataclass as much as a real struct (I don't perform miracles), the point is precisely in the memory mapping made by the struct, it will organize all the data in binary, and from there how you defined it to be organized, so that you can access it whenever you want, whether for:
- Saving file space.
- Bandwidth savings in data transmission.
- Deserialize data from real structs of a network protocol.
- Creation of binary layouts in general, even from a PNG file.
Or for any other case where it is also useful.
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
Built Distribution
File details
Details for the file structer-0.1.0.tar.gz
.
File metadata
- Download URL: structer-0.1.0.tar.gz
- Upload date:
- Size: 6.7 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: poetry/1.8.3 CPython/3.12.7 Linux/6.6.50-2-lts
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 94f5c7ba84b28aa4617b858f8b3feadee8fdabc2bd17a5131f4a2d93e65ed226 |
|
MD5 | 3bb2629ea038867f171087c0f82b98e4 |
|
BLAKE2b-256 | 717ef3e1881fa3b8e9795b4304eb80e0f85c927fb528a1002daabc8c446d549c |
File details
Details for the file structer-0.1.0-py3-none-any.whl
.
File metadata
- Download URL: structer-0.1.0-py3-none-any.whl
- Upload date:
- Size: 7.8 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: poetry/1.8.3 CPython/3.12.7 Linux/6.6.50-2-lts
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 6beef14b82b67b32c7a8a290f00360a0dff33acccf5e7a12289abea84e2ade21 |
|
MD5 | 7a3ba12c7aa993dcfbc7a3c350113c98 |
|
BLAKE2b-256 | 407dd0b61ef830963a785106ef32d899a3993d9878f8551e1b5df6a84f76f27b |