Package: tcheasy
Checkout - freepik -the photographer of Mr. Tcheasy & Sorpa!tcheasytcheasyis a python decorator that makes type checking (at runtime) easy!Just import the decorator and startto check your function inputsfrom the get-go.You want to defineinput restrictionsas well? Then your are in the right place!Python >= 3.8 needed. Tested ver. { 3.8.5, 3.9.9 }CONTENTwhy?how?install?where is Sorpa?examples?WHY?Back to topFor one of my projects I needed a way to check user inputs at runtime.There were already packages that do type-checking, but they only use the - relatively simplistic - python type hints.The problem, however, was that I needed not only type checking but also the option to restrict some parameter inputs.Entertcheasy!Out of the need to have a tool for other projects, this python decorator was born.This little kritter is able to use a predefineddictor thepython type hinting. Please refere to thehow?&example?section for more infos about the usage.tcheasyships also with his neat little friendsorpa:The functionsorpa()is able to extract and sort your passed function parameters at runtime.If you are sololy looking for a type checking tool (and some handy utility-tools) you should also check out:beartypetypeguardmypyElse feel free to use and modifytcheasyas you please.HOW?Back to topJust import the packagefromtcheasyimporttcheasyand add the decorator@tcheasy()to your to-be-checked function.If you are already usingpython type hintingtcheasyis ready out of the box.In the current version, the following types (hints) are supported:NoneType(s): Nonenumber types: int, float, complex, boolsequence types: str, list, tuplemapping types: dictset types: setscallable types: objecttyping-module types: typing.Any, typing.UnionBut the true might comes from usingdeclarative dicts.By passing a dict to@tcheasy()you can specify the parameter types, add rudimentary input restrictions & default values.The structure of these dictionaries (and their used keywords) is always the same (type,restriction(optional) &default(optional)):# define the dict!to_check={'positional':{'parameter-name':{'type':type,'restriction':"(optional) in the form: value condition",'default':"(optional) default value"},'next-parameter-name':{...},...},'args':[{'type':type,'restriction':str,'default':"value"},{...},...],'kwargs':{'parameter-name':{...},...}}As you can see, you have the complete control over potential*argsand**kwargs.
The definition structure of 'kwargs' is also the same as for thepositionals.Keep in mind that you do not have to definerestrictionand/ordefault. These two keywords are completely optional.After you have defined thedictyou can pass it to@tcheasy()and it will start to monitor the function at runtime (e.g. if yourdictis calledto_check, then you pass it like this:@tcheasy(to_check)).Note:The separation ofpositional,args&kwargsis derived of the python parameter types. There are 5 distinct types, which can be boiled down to basically 3 'blocks':positional:keyword parameters (your typicaldef test(a), while the call wastest(a='some-value'))(positional) parameters (again your typicaldef test(a)but this time calling justtest('some-value'))*args:arbitrary positional arguments (def test(a, *args), while callingtest('value-a', 'value-args'))**kwargs:arbitrary keyword arguments (def test(a, **kwargs), while callingtest('value-a', add_kwargs="some-value"))The structure of thedefaultkeyword is  - as mentioned - always the same. For example consider the following:...'parameter-name':{'type':int,'restriction':"value > 1"},...In this example, the passedintparameter gets evaluated and has to be be greater than one.
The wordvalueis the placeholder for your function parameter.To give you another example, you can use"value in ['yes', 'no', 'maybe']"to evaluate if the passed string parameter is one of the elements in the list.With this technique you are able to do some pretty awsome stuff!If you want further examples, please refere to theexamples?section.INSTALL?Back to topThere are two ways to installtcheasy.The easy way is to use pip:$ pip install tcheasyThe other option is togit clonethe project and then run:$ pip install 'path-to-your-clone'If you want to tinker with your code, run the pip install with-e.WHERE IS SORPA?Back to topMr. Tcheasycomes packed with his friendSorpa!sorpa()is a python function which shows you the passed parameters, names, declartions and hints of your targeted python function at runtime.The import is as easy as the import fortcheasy. Just:fromtcheasyimportsorpaThis function takes as input the function (func) you want to analyse and the locals (loc).As soon as you passed those two arguments, you get a handy dict which sorted the passed parameters, your declared parameter (during function definition) & python hints.For example, lets consider the following test function:# we define our test function (called 'test_function') with the parameters a, b & c# also we add some type hintingdeftest_function(a:int,b:str,c:float=.1)->dict:"""Our test function. Proper docstring skipped """# get our local variablesloc=locals()# lets consider we already imported 'sorpa'# we set decorated to 'False' to tell the algorithmn# that we are not within a decorator scope.sortedParameters=sorpa(func=test_function,loc=loc,decorated=False)returnsortedParametersNow we can call the function with some arguments (e.g.test_function(5, b="some-string")) and get as a return the following:>>{'positional':{'a':5,'b':'some-string','c':0.1},'args':[],'kwargs':{},'hinting':{'a':<class'int'>, 'b': <class 'str'>, 'c': <class 'float'>},'declared':['a','b','c'],'self':{'available':False,'value':None}}EXAMPLES?Back to topTo get some examples, please check out theexamplesdirectory (on github).There you will find examples forMr. Tcheasy's& and his friendSorpa'susecases.