Library for creating autogenerated property with type validation.
Project description
AutoProperty library
- What it used for
- Why you need it?
- Installation
- Requirements
- How does this work?
- Quick start
- Full example
- Documentation
- FAQ
- Known problems
- Perfomance
- Plans
- Feedback
What it used for?
This open source library is used for creating autogenerated properties with type validation.
I think it is very comfy to use and less code to write.
I just have enough of writting millions and trillions of "@property" decorators and setters for them. It also have type validation, as a bonus.
Why you need it?
This library provides more readability to the code, decreasing amount of lines and preventing from violation of the principles of object-oriented programming (DRY).
Installation
To install this lib run this line in your terminal/console:
pip install autoproperty
Requirements
The only it one dependency is pydantic library. If pip didnt install by itself then run this command in your env:
pip install pydantic
How does this work?
Basically it is just a common property but with autogenerated getters and setters (no deleters for now, will be added later) like in Csharp but without access modifiers. I tried to achieve the better speed perfomance as the basic solution.
It also has builtin field validation from pydantic but you can turn it off (turned on by default) and save some time on the runtime.
Quick start
Syntax
Base syntax.
class Exmpl:
@AutoProperty[int] # <- (Optional) generic for IDE syntax highlighting
def some_prop(self) -> int: ... # <- not have to implement!
# ^ type annotation for checking object in setter
# and type annotation
obj = Exmpl()
obj.some_prop = 42
Annotations
You have to add at least one type annotation to any of these three places (If you didnt turned off type validation). Be careful to not mix them up. They all have to be the same, otherwise will raise an error.
class Exmpl:
_some_prop: int # <- one
@AutoProperty(annotation_type=int) # <- two
def some_prop(self) -> int: ...
# ^ three
Full example
from autoproperty import AutoProperty
class Point:
def __init__(self, x: int, y: int):
self.X = x
self.Y = y
@AutoProperty[int]
def X(self) -> int: ...
@AutoProperty[int]
def Y(self) -> int: ...
def __repr__(self) -> str:
return f"[{self.X};{self.Y}]"
myPointOne = Point(2, 6)
print(myPointOne.X) # 2
print(myPointOne) # [2; 6]
Documentation
More info about syntax and options you can find in docs folder. Here is a base example.
FAQ
-
How to install this library?
Just run
pip install autoproperty. -
How much slower is the basic auto-property class compared to the basic solution?
It's not much slower, the basic autoproperty class is only a few times slower than the basic solution, but there's a nanosecond count going on. For more details check Perfomance.
-
Is lightweight version of autoproperty is available on all platforms?
Not yet. Now it is only available on linux and mac because I dont have windows machine to compile the windows version. But I will add it ASAP.
-
Do this lib have a lot of dependencies?
No. There is only one dependency: pydantic. Even this one i would make optional if user does not intend to use field validation.
-
Can I use this lib in production?
It is hard to give a solid answer. I would prefer anyone can use this in production. But I do have a low amount of experience in such things. It need some tests and professional opinion to be sure for 100%. So yes you can, but be careful.
-
How often updates will come out?
As more as I have powers for contributing. I do work alone, no sponsors, all on my own. If this project will like some people, even a couple, of course I would update it more often.
Known problems
- Not tested yet with classmethods or staticmethods, only bound methods, but it should work as well.
- No windows version of LightProperty
Performance
First of all, I want to note that the basic property solution in Python is 447% faster for the getter and 265% faster for the setter than the basic auto property from this library.
However, do not jump to conclusions. First of all, you should consider the convenience and readability of the code, and only then the performance. For high-load projects, where every nanosecond is important, a lightweight version of auto-action has been created. Its speed is impressive: by 414% for the getter and by 366% for the setter, which surpasses the basic auto performance.
Thus, lightweight autoproperty turns out to be faster than autoproperty and the lightweight autoproperty setter is even faster than in the basic Python implementation.
You can test it out on your own PC. Just launch test_time_comparing.py from /test/.
There is result of tests:
autoproperty getter time: 204.9903589429996
autoproperty setter time: 187.70526755399987
light autoproperty getter time: 49.50247659899833
light autoproperty setter time: 51.2770686570002
basic property getter time 45.86868843100092
basic property setter time 70.93056209800125
diff between getters of autoproperty and it light version 4.141012188209338
diff between setters of autoproperty and it light version 3.6606083863644354
diff between getters of autoproperty and basic solution 4.469069553871401
diff between setters of autoproperty and basic solution 2.6463242642100706
Lets find out who is faster. More percent - faster
getters
If AutoProperty result = 100%, then LightAutoProperty = 414%, and basic property = 447%
setters
If AutoProperty result = 100%, then LightAutoProperty = 366%, and basic property = 265%
Plans
Currently I'm planning to add these features to the lib:
- The opportunity to add your own validation.
- The opportunity to add handlers to "set" and "get" events.
- The lightweight autoproperty class for high load projects
- Modificators like "read-only"
- The opportunity to add your own getter and setter using protocols
Feedback
If you want to ask me something, you have solution one of the above problems, you have an offer to me or any other reason, please open an issue or message me via email alecsw86@gmail.com.
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
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file autoproperty-0.0.51.tar.gz.
File metadata
- Download URL: autoproperty-0.0.51.tar.gz
- Upload date:
- Size: 228.1 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.12.3
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
62cff4eea0a6ffead5a034d63e6744a65b25f4e342e967f72fac4a2950824f85
|
|
| MD5 |
f8bf5449d589148783aaea5784a88b34
|
|
| BLAKE2b-256 |
1041c24dbd7b4f5c95f6822a7dc13a02d2b7ab7122f5a90276f5b954c78392ef
|
File details
Details for the file autoproperty-0.0.51-py3-none-any.whl.
File metadata
- Download URL: autoproperty-0.0.51-py3-none-any.whl
- Upload date:
- Size: 228.7 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.12.3
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
bb02817738b0f96dd78722aeea6b0b0698f4360c8ec13a7f8b665fc7a685f7cb
|
|
| MD5 |
8f9af7eb9ea3472159fb8bd1862c5847
|
|
| BLAKE2b-256 |
3df6803b8d7b1ae9849dc0e0b21477e2bd0e1effdb029f6571fcdec7b1f1466b
|