Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/amboxer21/custompythondecorators
Custom Python decorators I wrote to restrict method arguments to a specific type. I also added an encrypt/decrypt decorator as well.
https://github.com/amboxer21/custompythondecorators
advanced aes aes-encryption curried-functions currying custom custom-python-decorators decorator decorators encrypt encrypted encryption encryption-decryption functional functional-programming functional-python higher-order-functions metaprogramming python
Last synced: about 2 months ago
JSON representation
Custom Python decorators I wrote to restrict method arguments to a specific type. I also added an encrypt/decrypt decorator as well.
- Host: GitHub
- URL: https://github.com/amboxer21/custompythondecorators
- Owner: amboxer21
- Created: 2018-09-18T13:56:02.000Z (over 6 years ago)
- Default Branch: master
- Last Pushed: 2019-04-05T18:52:31.000Z (almost 6 years ago)
- Last Synced: 2024-10-25T09:20:09.715Z (3 months ago)
- Topics: advanced, aes, aes-encryption, curried-functions, currying, custom, custom-python-decorators, decorator, decorators, encrypt, encrypted, encryption, encryption-decryption, functional, functional-programming, functional-python, higher-order-functions, metaprogramming, python
- Language: Python
- Homepage:
- Size: 57.6 KB
- Stars: 0
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
# CustomPythonDecorators
These are custom Python decorators I wrote to restrict method arguments to a specific type. If you pass an argument other than what the decorator allows, it will raise a TypeError exception. These decorators also ensure that you are calling the decorator on an instance method of a class. I also wrote other decorators to encrypt/decrypt strings passed to instance methods.**Examples:**
>**accepts decorators**
```python
class TestAcceptsClass(object):# This instance method will raise an exception
# if an argument other than boolean is passed.
@accepts.boolean
def _boolean(self,_boolean_):
print('boolean => '+str(_boolean_))# This instance method will raise an exception
# if an argument other than integer is passed.
@accepts.integer
def _integer(self,_integer_):
print('integer => '+str(_integer_))# This instance method will raise an exception
# if an argument other than string is passed.
@accepts.string
def _string(self,_string_):
print('string => '+str(_string_))# This instance method will raise an exception
# if an argument other than dict is passed.
@accepts.dictionary
def _dictionary(self,_dictionary_):
print('dictionary => '+str(_dictionary_))# This instance method will raise an exception
# if an argument other than list is passed.
@accepts.list
def _list(self,_list_):
print('list => '+str(_list_))# This instance method will raise an exception
# if an argument other than tuple is passed.
@accepts.tuple
def _tuple(self,_tuple_):
print('tuple => '+str(_tuple_))if __name__ == '__main__':
# Our test class
test = TestAcceptsClass()# The following examples are ways that the custom
# decorators can be used.
test._integer(1)
test._integer(1,2)test._boolean(True)
test._boolean(True,False)test._list(['1','2','3'])
test._list(['1','2','3'],['4','5','6'])test._tuple(('1','2','3'),)
test._tuple(('1','2','3'),('4','5','6'))test._string('test string')
test._string('test string1','test string2')test._dictionary(
{'one': '1','two': '2','three': '3'},
{'four': '4','five': '5','six': '6'}
)
test._dictionary({'one': '1','two': '2','three': '3'})
# The following test cases will fail and will raise a type exception!
test._integer('1')
test._integer('1','2')
test._boolean(1)
test._boolean('True','False',1)
test._list(('1','2','3'))
test._list(('1','2','3'),('4','5','6'))
test._tuple(['1','2','3'],)
test._tuple(['1','2','3'],['4','5','6'])
test._string(True)
test._string(True,False)# The following examples will fail and will raise a SyntaxError
# and complain about the method not being an instance of a class.
@accepts.integer
def _integer(_integer_):
print('integer: '+str(_integer_))
_integer(1)
```>**encryption decorators**
```javascript
class TestStringClass(object):@string.encrypt
def encrypt(self,string):
return string@string.decrypt
def decrypt(self,string):
return stringif __name__ == '__main__':
# This will work
test = TestStringClass()
encrypted_text = test.encrypt('This is an encrypted string')
print test.decrypt(encrypted_text)# This will not work
@string.encrypt
def test_string_method(string):
string
test_string_method('This is a test')
```> ^^ Remove newlines in between method declarations if using the python shell/interpretter and are having problems!