{"id":16444898,"url":"https://github.com/jaymon/decorators","last_synced_at":"2025-03-23T08:32:10.276Z","repository":{"id":11823802,"uuid":"14376724","full_name":"Jaymon/decorators","owner":"Jaymon","description":"Quickly create flexible Python decorators","archived":false,"fork":false,"pushed_at":"2023-01-20T07:54:31.000Z","size":70,"stargazers_count":7,"open_issues_count":1,"forks_count":1,"subscribers_count":8,"default_branch":"master","last_synced_at":"2025-03-18T19:29:06.205Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":null,"language":"Python","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/Jaymon.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE.txt","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2013-11-13T21:18:53.000Z","updated_at":"2024-04-02T17:40:42.000Z","dependencies_parsed_at":"2023-02-12T00:30:38.967Z","dependency_job_id":null,"html_url":"https://github.com/Jaymon/decorators","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Jaymon%2Fdecorators","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Jaymon%2Fdecorators/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Jaymon%2Fdecorators/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Jaymon%2Fdecorators/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/Jaymon","download_url":"https://codeload.github.com/Jaymon/decorators/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":245078067,"owners_count":20557274,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":[],"created_at":"2024-10-11T09:42:31.905Z","updated_at":"2025-03-23T08:32:10.000Z","avatar_url":"https://github.com/Jaymon.png","language":"Python","funding_links":[],"categories":[],"sub_categories":[],"readme":"# Decorators\n\nAs of January 19, 2023 I've moved this code into [Datatypes](https://github.com/Jaymon/datatypes), this will continue to work, but further development and features will continue in the Datatypes submodule.\n\n-----\n\n## The problem\n\nPython has a few forms for decorators, you can have a plain simple decorator, with no arguments:\n\n```python\n@mydecorator\ndef foo(): pass\n```\n\nOr a decorator with some arguments:\n\n```python\n@mydecorator(1, 2)\ndef foo(): pass\n```\n\nYou can even decorate a class:\n\n```python\n@mydecorator\nclass Foo(object): pass\n```\n\nand each form is a little different to implement. This was frustrating if you wanted to create easy to use decorators where the developer didn't need to worry about `@mydecorator()` working differently than `@mydecorator`.\n\n\n## decorators module\n\nThe `decorators.Decorator` class allows you to easily create broad decorators that encompass all forms and all types (functions, methods, classes) using the same interface:\n\n```python\nimport decorators\n\nclass mydecorator(decorators.Decorator):\n    def decorate_func(self, func, *dec_args, **dec_kwargs):\n        def decorator(*args, *kwargs):\n            print(\"You passed into the decorator these arguments\", dec_args, dec_kwargs)\n            print(\"You passed into your function these arguments\", args, kwargs)\n            print(\"Your function is\", func)\n            return func(*args, **kwargs)\n\n        return decorator\n\n    def decorate_class(self, klass, *dec_args, **dec_kwargs):\n        print(\"You passed into the decorator these arguments\", dec_args, dec_kwargs)\n        print(\"Your class is\", klass)\n        return klass\n```\n\nYou can then use this decorator:\n\n```python\n@mydecorator\ndef foo(): print \"foo()\"\n\n@mydecorator(1, 2, boom=\"blam\")\ndef bar(*args, **kwargs): print \"bar()\"\n\n@mydecorator\nclass Baz(object): pass\n\n@mydecorator(1, 2, boom=\"blam\")\nclass Che(object): pass\n```\n\nNow, your decorator can decorate functions or classes, pass in arguments, or not, and you never have to worry about the subtle differences between the decorators, and best of all, you don't have to duplicate code.\n\n\n## Other decorators\n\n### FuncDecorator, ClassDecorator, and InstanceDecorator\n\nThe `Decorator` class is good if you want to create a decorator that is totally flexible, if you want to enforce your decorator only being used for a function/method, you can use `FuncDecorator`. If you want to only decorate a class, use `ClassDecorator`, and if you want to decorate every instance of a class, use `InstanceDecorator`.\n\nWhatever child class you use, you override the `decorate` method to return your decorator function:\n\n```python\nimport decorators\n\nclass only_func(FuncDecorator):\n    def decorate(self, func, *dec_a, **dec_kw):\n        def decorator(*args, **kwargs):\n            return func(*args, **kwargs)\n        return decorator\n\n# this will work\n@only_func\ndef foo(): pass\n\n# this will fail\n@only_func\nclass Foo(object): pass\n```\n\n\n### Property Decorator\n\nThe `property` decorator is a drop-in replacement for Python's built-in `property` decorator, with additional functionality:\n\n```python\nfrom decorators import property\n\nclass Foo(object):\n    @property\n    def bar(self):\n        \"\"\"This will act just like python's built-in @property decorator\"\"\"\n        return 1\n        \n    @property(cached=\"_che\")\n    def che(self):\n        \"\"\"This will cache the return value into _che and add a setter/deleter\"\"\"\n        return 1\n```\n\n\n### Classproperty Decorator\n\nAllows you to create a property on the class:\n\n```python\nfrom decorators import classproperty\n\nclass Foo(object):\n    @classproperty\n    def bar(cls):\n        \"\"\"Available as Foo.bar\"\"\"\n        return 1\n        \nprint(Foo.bar) # 1\n```\n\n\n## Installation\n\nUse pip:\n\n    pip install decorators\n\nOr, to get the latest and greatest from source:\n\n    pip install -U \"git+https://github.com/Jaymon/decorators#egg=decorators\"\n\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fjaymon%2Fdecorators","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fjaymon%2Fdecorators","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fjaymon%2Fdecorators/lists"}