Library for making automatic property for methods with type validation and access modificators.
Project description
AutoProperty library
What it used for?
This library is used for creating autogenerated properties with type validation.
I wrote this library for my own uses and points. 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, a bonus.
How does this work?
Basically it is just a common property or data descriptor (if you feel better to call it like that) but instead of storing the providing private (optionally) class attribute inside itself, it creating the attribute inside the autoproperty containing class instance.
You do not need to do anything - only smoke the cigarrete, throw legs on a table and rest. Let it do all hard and boring work. You are the king and you deserve no other treatment.
Jokes aside, it do all hard work, let me show you.
Usage
Syntax
Base syntax.
class Exmpl:
@AutoProperty[int] # <-- need for correct syntax highlighting
def SomeProp(self, v: int): ... # <-- no need to implement, it won't change anything
# ^ type annotation for checking object in setter
In runtime it turns construction above to a construction like below:
class Exmpl:
__someProp: int
@AutoProperty[int]
def SomeProp(self, v: int): ...
Annotations
You have to add annotation on any of three places
class Exmpl:
__someProp: int # <-- one
@AutoProperty[int](annotationType=int) # <-- two
def SomeProp(self, v: int): ...
# ^ three (will read only first parameter after self)
Access
By default autoproperty has private access:
class Exmpl:
@AutoProperty[int]
def SomeProp(self, v: int): ...
def __init__(self):
self.SomeProp = 42 # ok
example = Exmpl()
print(example.SomeProp) # Error: UnaccessiblePropertyMethod
CSharp analogy:
class Exmpl(){
private int SomeProp { get; set; }
Exmpl(){
this.SomeProp = 42; // ok
}
}
Exmpl example = new();
_ = example.SomeProp; // Error: CS0271
I dont know how to fix it but even python debugger in VSCode cant go against it, if you do know please open an issue and tell me. I'll appreciate that.
Full example
from autoproperty import AutoProperty
class Point:
def __init__(self, x: int, y: int):
self.X = x
self.Y = y
# by default it has private access
@AutoProperty[int](access_mod="public", s_access_mod="private")
def X(self, v: int): ...
@AutoProperty[int](access_mod="public", s_access_mod="private")
def Y(self, v: int): ...
def __repr__(self) -> str:
return f"[{self.X};{self.Y}]"
# ^ ^ is ok
myPointOne = Point(2, 6)
myPointOne.X = 5 # Error: UnaccessiblePropertyMethod
print(myPointOne.X) # ok
print(myPointOne) # [2; 6]
Known problems
- Not working correct while inheriting from class with private autoproperty using super().__init__() or any other method or object from parent class due not enough information.
- Not tested yet with classmethods or staticmethods, only bound methods
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.2.tar.gz.
File metadata
- Download URL: autoproperty-0.0.2.tar.gz
- Upload date:
- Size: 13.7 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.1.0 CPython/3.10.12
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
9cd91e52add0ed8b5751b1be39371682986412421668bf0bf2f30ba2897e6a86
|
|
| MD5 |
278a6677a6656dc0db8ef5a84f90f105
|
|
| BLAKE2b-256 |
653df74e6dd1691cf746f0f2c0400355c5f2ac3826e6e38ecebcdb2a9c40df02
|
File details
Details for the file autoproperty-0.0.2-py3-none-any.whl.
File metadata
- Download URL: autoproperty-0.0.2-py3-none-any.whl
- Upload date:
- Size: 11.8 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.1.0 CPython/3.10.12
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
ec4a23f67ef530831d411a34db195daeea8a1236feb6a377d71707a127134979
|
|
| MD5 |
867d4685c4418407c29140e96fb6fa8e
|
|
| BLAKE2b-256 |
add8a5dc7d9db83abf5080df3c46d0eb0bbf3eb519cdf53bbb6739a5762ccd84
|