extradecorators


Nameextradecorators JSON
Version 0.4 PyPI version JSON
download
home_pageNone
SummaryA package that contains some useful decorators for any case.
upload_time2024-05-06 16:36:07
maintainerNone
docs_urlNone
authorRandomTimeTV
requires_pythonNone
licenseMIT with required credit to the author.
keywords decorators
VCS
bugtrack_url
requirements No requirements were recorded.
Travis-CI No Travis.
coveralls test coverage No coveralls.
            # ExtraDecorators
This module introduces some handy decorators like @validatetyping. 


### @validatetyping
```py
# 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
```py
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.


            

Raw data

            {
    "_id": null,
    "home_page": null,
    "name": "extradecorators",
    "maintainer": null,
    "docs_url": null,
    "requires_python": null,
    "maintainer_email": null,
    "keywords": "Decorators",
    "author": "RandomTimeTV",
    "author_email": "dergepanzerte1@gmail.com",
    "download_url": "https://files.pythonhosted.org/packages/b9/99/54a5da129b37a07ef1896021466164af78c6a1c3db35dbad600aad54d1c3/extradecorators-0.4.tar.gz",
    "platform": null,
    "description": "# ExtraDecorators\r\nThis module introduces some handy decorators like @validatetyping. \r\n\r\n\r\n### @validatetyping\r\n```py\r\n# Example 1: Applying to a function\r\n@validatetyping\r\ndef add(x: int, y: int) -> int:\r\n    return x + y\r\n\r\nresult = add(5, 10)  # Valid call\r\nresult = add(5, '10') # Invalid call, will raise an ValueError\r\n\r\n```\r\n\r\n\r\n\r\n### @read_only and @restoreAttributeTypeIntegrity\r\nment to be used on and in the __setattr__ methode in a class\r\n```py\r\nfrom ExtraDecorators.validatetyping import validatetyping\r\nfrom ExtraDecorators.read_only import read_only\r\nfrom ExtraDecorators.restoreAttributeTypeIntegrity import restoreAttributeTypeIntegrity\r\n\r\nclass Example:\r\n    viewercount:int \r\n    channelDisplayName:str\r\n    profileImage:any\r\n    @validatetyping\r\n    def __init__(self,channelobject:dict):\r\n        self.viewercount = channelobject.get('viewercount')\r\n        self.channelDisplayName = channelobject.get('channelDisplayName')\r\n        self.profileImage = channelobject.get(\"image\")\r\n    \r\n    @read_only\r\n    def __setattr__(self, name, value) -> None:\r\n\r\n        @restoreAttributeTypeIntegrity\r\n        def prepvalid(self,name, value):\r\n            result = (name, value)\r\n\r\n            return result\r\n\r\n        nam, val = prepvalid(self, name, value)\r\n        print(type(val))\r\n        super().__setattr__(nam, val)\r\n\r\n    @validatetyping #from above to ensure there is only a string entered\r\n    def setChannelDisplayname(self, name:str):\r\n        #do some fancy api stuff\r\n        # and get, for some reason an int as channelDisplayname back\r\n        self.channelDisplayName = 125684576623366 #simulated by this hardcoded int\r\n\r\n        # this anomaly gets caught by the @restoreAttributeTypeIntegrity decorator and tries to convert the value to a string\r\n        # because self.channelDisplayName was annotated to be a str\r\n\r\n# see as i set viewercount as a string, instead of the expected int\r\nchannelobject = {'viewercount':'5','channelDisplayName':'test', 'image':'hallo.png'}\r\n# however as you may observe the print statement in the __setattr__ method prints the type of the viewercount value to be an int\r\n\r\ncla = Example(channelobject=channelobject)\r\n#cla.viewercount = 5 # failes due to read_only\r\n#print(cla.viewercount) # still prints the value as expected\r\n```\r\nIn 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.\r\n\r\nAnd @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.\r\n\r\n",
    "bugtrack_url": null,
    "license": "MIT with required credit to the author.",
    "summary": "A package that contains some useful decorators for any case.",
    "version": "0.4",
    "project_urls": null,
    "split_keywords": [
        "decorators"
    ],
    "urls": [
        {
            "comment_text": "",
            "digests": {
                "blake2b_256": "7c92c84d34bac9b6068bbd6036693c5bdf4da707224f8e1e9c04375897916dde",
                "md5": "ef56536c39ed6b3e3b9ea992c2d93151",
                "sha256": "6b4d660d0ddc89539cea621e1b5d80135f440fe67cd3578c29538ca64f8d9e41"
            },
            "downloads": -1,
            "filename": "extradecorators-0.4-py3-none-any.whl",
            "has_sig": false,
            "md5_digest": "ef56536c39ed6b3e3b9ea992c2d93151",
            "packagetype": "bdist_wheel",
            "python_version": "py3",
            "requires_python": null,
            "size": 5948,
            "upload_time": "2024-05-06T16:36:05",
            "upload_time_iso_8601": "2024-05-06T16:36:05.984606Z",
            "url": "https://files.pythonhosted.org/packages/7c/92/c84d34bac9b6068bbd6036693c5bdf4da707224f8e1e9c04375897916dde/extradecorators-0.4-py3-none-any.whl",
            "yanked": false,
            "yanked_reason": null
        },
        {
            "comment_text": "",
            "digests": {
                "blake2b_256": "b99954a5da129b37a07ef1896021466164af78c6a1c3db35dbad600aad54d1c3",
                "md5": "3ebf34131ded200acb2251d2a89ff5f2",
                "sha256": "e1a8f639673dd382bf99905e4842401dc6287f9e77d61ded94a80d7b46012ec2"
            },
            "downloads": -1,
            "filename": "extradecorators-0.4.tar.gz",
            "has_sig": false,
            "md5_digest": "3ebf34131ded200acb2251d2a89ff5f2",
            "packagetype": "sdist",
            "python_version": "source",
            "requires_python": null,
            "size": 4648,
            "upload_time": "2024-05-06T16:36:07",
            "upload_time_iso_8601": "2024-05-06T16:36:07.582875Z",
            "url": "https://files.pythonhosted.org/packages/b9/99/54a5da129b37a07ef1896021466164af78c6a1c3db35dbad600aad54d1c3/extradecorators-0.4.tar.gz",
            "yanked": false,
            "yanked_reason": null
        }
    ],
    "upload_time": "2024-05-06 16:36:07",
    "github": false,
    "gitlab": false,
    "bitbucket": false,
    "codeberg": false,
    "lcname": "extradecorators"
}
        
Elapsed time: 4.35162s