Skip to main content

Library for making automatic property for methods with type validation and access modificators.

Project description

AutoProperty library

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.

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.

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. (Joke)

Jokes aside, it do all hard work, let me show you.

Usage

Syntax

Base syntax.

class Exmpl:
    @AutoProperty[int] # <- generic need for correct IDE syntax highlighting
    def some_prop(self) -> int: ... # <- no need to implement, it won't change anything
                          # ^ type annotation for checking object in setter

obj = Exmpl()
obj.some_prop = 42

In runtime it turns construction above to a construction like below:

class Exmpl:
    _some_prop = 42

    @AutoProperty[int]
    def some_prop(self) -> int: ...

obj = Exmpl()
obj.some_prop = 42

Annotations

You have to add at least one 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[int](annotation_type=int) # <- two
    def some_prop(self) -> int: ...
                          # ^ three

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.

Known problems

  • Not tested yet with classmethods or staticmethods, only bound methods, but it may work.

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

autoproperty-0.0.3.tar.gz (12.5 kB view details)

Uploaded Source

Built Distribution

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

autoproperty-0.0.3-py3-none-any.whl (11.1 kB view details)

Uploaded Python 3

File details

Details for the file autoproperty-0.0.3.tar.gz.

File metadata

  • Download URL: autoproperty-0.0.3.tar.gz
  • Upload date:
  • Size: 12.5 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.12.3

File hashes

Hashes for autoproperty-0.0.3.tar.gz
Algorithm Hash digest
SHA256 47f44b144b8f017aa2edd7e4914b96b6a1e11ab88dee112236fc4bcd126f0b45
MD5 6936ae5a985e1c0e46fbd69128961ec9
BLAKE2b-256 2613cac773453a6680c601bbd21885a2829f346845ec366d513c150535416e88

See more details on using hashes here.

File details

Details for the file autoproperty-0.0.3-py3-none-any.whl.

File metadata

  • Download URL: autoproperty-0.0.3-py3-none-any.whl
  • Upload date:
  • Size: 11.1 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.12.3

File hashes

Hashes for autoproperty-0.0.3-py3-none-any.whl
Algorithm Hash digest
SHA256 fd7c429a5e768c45dec7a0a7a9f301cfaaabc3fb5ec9714e44422c3ddb11a108
MD5 dccc80c0f487b5a353e460e98d9e008f
BLAKE2b-256 4313a14c5d1c77f822ebcfded05e6a9e4307ab2fce66fcc31c1c8aae97d73dce

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