Skip to main content

A package that contains some useful decorators for any case.

Project description

ExtraDecorators

This module introduces some handy decorators like @validatetyping.

@validatetyping

# Example 1: Applying to a function
@validatetyping
def add(x: int, y: int) -> int:
    return x + y

result = add(5, 10)  # Valid call
result = add(5, '10') # Invalid call, will raise an ValueError

@read_only and @restoreAttributeTypeIntegrity

ment to be used on and in the setattr methode in a class

from ExtraDecorators.validatetyping import validatetyping
from ExtraDecorators.read_only import read_only
from ExtraDecorators.restoreAttributeTypeIntegrity import restoreAttributeTypeIntegrity

class Example:
    viewercount:int 
    channelDisplayName:str
    profileImage:any
    @validatetyping
    def __init__(self,channelobject:dict):
        self.viewercount = channelobject.get('viewercount')
        self.channelDisplayName = channelobject.get('channelDisplayName')
        self.profileImage = channelobject.get("image")
    
    @read_only
    def __setattr__(self, name, value) -> None:

        @restoreAttributeTypeIntegrity
        def prepvalid(self,name, value):
            result = (name, value)

            return result

        nam, val = prepvalid(self, name, value)
        print(type(val))
        super().__setattr__(nam, val)

    @validatetyping #from above to ensure there is only a string entered
    def setChannelDisplayname(self, name:str):
        #do some fancy api stuff
        # and get, for some reason an int as channelDisplayname back
        self.channelDisplayName = 125684576623366 #simulated by this hardcoded int

        # this anomaly gets caught by the @restoreAttributeTypeIntegrity decorator and tries to convert the value to a string
        # because self.channelDisplayName was annotated to be a str

# see as i set viewercount as a string, instead of the expected int
channelobject = {'viewercount':'5','channelDisplayName':'test', 'image':'hallo.png'}
# however as you may observe the print statement in the __setattr__ method prints the type of the viewercount value to be an int

cla = Example(channelobject=channelobject)
#cla.viewercount = 5 # failes due to read_only
#print(cla.viewercount) # still prints the value as expected

In summery @read_only ensures that you cant just so modify the attributes from outside the class (decorators are an exeption, they can [at least in some cases] still modify the attributes with very little restriction), but still be able to read and compare their values.

And @restoreAtributeTypeIntegrity ensures that, either the incomming value does get changed to the annotated type or it raising an AttributeError if it was unable to restore the type. If type is any or unset it skips the validation process for this atribute.

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

extradecorators-0.4.tar.gz (4.6 kB view details)

Uploaded Source

Built Distribution

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

extradecorators-0.4-py3-none-any.whl (5.9 kB view details)

Uploaded Python 3

File details

Details for the file extradecorators-0.4.tar.gz.

File metadata

  • Download URL: extradecorators-0.4.tar.gz
  • Upload date:
  • Size: 4.6 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/5.0.0 CPython/3.9.13

File hashes

Hashes for extradecorators-0.4.tar.gz
Algorithm Hash digest
SHA256 e1a8f639673dd382bf99905e4842401dc6287f9e77d61ded94a80d7b46012ec2
MD5 3ebf34131ded200acb2251d2a89ff5f2
BLAKE2b-256 b99954a5da129b37a07ef1896021466164af78c6a1c3db35dbad600aad54d1c3

See more details on using hashes here.

File details

Details for the file extradecorators-0.4-py3-none-any.whl.

File metadata

  • Download URL: extradecorators-0.4-py3-none-any.whl
  • Upload date:
  • Size: 5.9 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/5.0.0 CPython/3.9.13

File hashes

Hashes for extradecorators-0.4-py3-none-any.whl
Algorithm Hash digest
SHA256 6b4d660d0ddc89539cea621e1b5d80135f440fe67cd3578c29538ca64f8d9e41
MD5 ef56536c39ed6b3e3b9ea992c2d93151
BLAKE2b-256 7c92c84d34bac9b6068bbd6036693c5bdf4da707224f8e1e9c04375897916dde

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