Skip to content

Folders and files

NameName
Last commit message
Last commit date

Latest commit

History

69 Commits

Repository files navigation

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

classTestAcceptsClass(object):
# This instance method will raise an exception# if an argument other than boolean is passed.@accepts.booleandef_boolean(self,_boolean_):
print('boolean => '+str(_boolean_))
# This instance method will raise an exception# if an argument other than integer is passed.@accepts.integerdef_integer(self,_integer_):
print('integer => '+str(_integer_))
# This instance method will raise an exception# if an argument other than string is passed.@accepts.stringdef_string(self,_string_):
print('string => '+str(_string_))
# This instance method will raise an exception# if an argument other than dict is passed.@accepts.dictionarydef_dictionary(self,_dictionary_):
print('dictionary => '+str(_dictionary_))
# This instance method will raise an exception# if an argument other than list is passed.@accepts.listdef_list(self,_list_):
print('list => '+str(_list_))
# This instance method will raise an exception# if an argument other than tuple is passed.@accepts.tupledef_tuple(self,_tuple_):
print('tuple => '+str(_tuple_))
if__name__=='__main__':
# Our test classtest=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.integerdef_integer(_integer_):
print('integer: '+str(_integer_))
_integer(1)

encryption decorators

classTestStringClass(object):
@string.encryptdefencrypt(self,string):
returnstring @string.decryptdefdecrypt(self,string):
returnstringif__name__=='__main__':
# Thiswillworktest=TestStringClass()encrypted_text=test.encrypt('This is an encrypted string')printtest.decrypt(encrypted_text)
# Thiswillnotwork
@string.encryptdeftest_string_method(string):
stringtest_string_method('This is a test')

^^ Remove newlines in between method declarations if using the python shell/interpretter and are having problems!

About

Custom Python decorators I wrote to restrict method arguments to a specific type. I also added an encrypt/decrypt decorator as well.

Topics

Resources

Stars

0 stars

Watchers

1 watching

Forks

Releases

Packages

Used by

Contributors

Languages