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.

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.4.tar.gz (225.8 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.4-py3-none-any.whl (226.4 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: autoproperty-0.0.4.tar.gz
  • Upload date:
  • Size: 225.8 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.4.tar.gz
Algorithm Hash digest
SHA256 53ff3bcd38bfe8cdb99d9ef1d5b59164f223ff0edbd91e035ea0e188f214f483
MD5 f01dc1b3a18f499140821f5995ad6eaa
BLAKE2b-256 93cb86bc2025060d07cbbac6aaa3859c705425bc15bed40179b59b844603d3b0

See more details on using hashes here.

File details

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

File metadata

  • Download URL: autoproperty-0.0.4-py3-none-any.whl
  • Upload date:
  • Size: 226.4 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.4-py3-none-any.whl
Algorithm Hash digest
SHA256 137229476e13eb0cf0c42f763e0c8e906e1416dd0f872d0a7792f5d495e93a21
MD5 0b49612fa2a90ec8d0421b0566f99119
BLAKE2b-256 7dc9776a2b77eedc7c248f458ea59f0b9336cf8fedb7a04567b7b3ec7f7b02e7

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