Skip to main content

Library for creating autogenerated property with type validation.

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.

Requirements

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.

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.5.tar.gz (226.6 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.5-py3-none-any.whl (227.2 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: autoproperty-0.0.5.tar.gz
  • Upload date:
  • Size: 226.6 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.5.tar.gz
Algorithm Hash digest
SHA256 f9067c6e0ef5deaa90a5f542b535dfe3a9364a6f1aad39bcbb85349c8e313f53
MD5 1c1247d17fb5a6b8e4400c0ee4d8b42e
BLAKE2b-256 fb5ddee5de1edc21568e339c55d6f42f36b1ece251adf7c998332d4c9403b0cd

See more details on using hashes here.

File details

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

File metadata

  • Download URL: autoproperty-0.0.5-py3-none-any.whl
  • Upload date:
  • Size: 227.2 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.5-py3-none-any.whl
Algorithm Hash digest
SHA256 f916022f691f2331c974ea7c96f39a33262311ead6e994920fc4f4031bf6445a
MD5 59c70ef2e1954a95b7e27d90e5f7f907
BLAKE2b-256 70a5bdd97432df17fb4139184258c33f157fb0dd57f31f6bf6af3086f46c906c

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