Package: char
Table of ContentsOverview.ExampleDifferences from python type hintingInstallation via pip:Usage with default settingsDefault prefixesExampleUsage with user defined settingsDecorator argumentsDecorator argument:bool_is_to_skip_None_valueDecorator argument:dict_tuple_types_by_prefix_to_update_defaultDecorator argument:dict_tuple_types_by_prefixLinksProject local LinksContactsLicenseOverview.char stands for: check of arguments.This library gives to user ability to check types of function arguments via one decorator.If your team or you have some agreements how to name variables with defined typesOr if you are ready to use mine (which is derived from Hungarian notation (it will be described bellow))then type checking will besimpleandpleasant.P.S. I like to use variables notation because it really improves readability of the code.ExampleLet’s say that you’ve defined a functionfunc(int_x)and you want to checkif value that is given to variableint_xhas type intUsually you would have to check it by adding one more line at the start of the functionsomehow like this:isinstance(int_x, int)With this decorator this can be done for you automatically.fromcharimportchar# OR from char import check_types_of_arguments  # They are equivalent@chardeffunc(int_x):passIf you try to call function with wrong types of arguments like:func(“pewpew”)then you’ll get a nice exception with description:ArgumentTypeError:Incorrecttypeofvariablewasgiventofunction:func--->Forvariable:int_x--->Weregivenvalue:pewpew--->Withtype:<class'str'>--->Insteadof:<class'int'>Differences from python type hintingThe main difference from the internal python type hinting is that type hinting do static analysis of the codeand do not do any checks during the runtime.So actually it can’t protect a user from giving to some type hinted function arguments with the wrong typesso even with type hinting you still have to write isinstance type checks.Additional difference is that this package supports python >=2.7 when type hinting is available only since python 3.5In case if you don’t want to use any notation of variables and want to use the type hinting then you can try the librarytypeguard.Installation via pip:pipinstallcharUsage with default settingsDefault prefixesHere will be a list of name prefixes and which type the variable is expected to beIf variable name doesn’t start with ony of the given  prefixes then variable type won’t be checked.“any_” -  object“bool_” -  bool“b_” -  bool“is_” -  bool“has_” -  bool“str_” -  str“bytes_” -  bytes“int_” -  int“i_” -  int“float_” -  float“f_” -  float“list_” -  list“l_” -  list“dict_” -  dict“d_” -  dict“set_” -  set“tuple_” -  tuple“t_” -  tupleExamplefromcharimportchar@chardefoh_my_god(int_arg,float_arg,list_arg,undef_arg,d_kwarg=None,i_kwarg=0,is_kwarg=False):passoh_my_god(0,0.0,[],1)# Will PASSoh_my_god(0,0.0,None,"text")# Will PASSoh_my_god(0,0.0,{},"text")# Will FAIL and raise an ArgumentTypeErroroh_my_god(0,0.0,[],Exception,d_kwarg={0:1})# Will PASSoh_my_god(0,0.0,[],object,is_kwarg=0)# Will FAIL and raise an ArgumentTypeErrorUsage with user defined settingsDecorator argumentsbool_is_to_skip_None_value=True: Flag what to do with None values, by default None values won’t be checked.dict_tuple_types_by_prefix_to_update_default: dictionary, which prefixes to add to the default onesdict_tuple_types_by_prefix: dictionary, which prefixes to use instead of default onesYour can use any combination of given arguments for the decorator.For simplicity will be shown usage of every argument separately.Decorator argument:bool_is_to_skip_None_value@chardeffunc_with_default_decorator(dict_x):pass@char(bool_is_to_skip_None_value=False)deffunc_with_custom_decorator(dict_x):passfunc_with_default_decorator(None)# Will PASSfunc_with_custom_decorator(None)# Will FAIL and raise an ArgumentTypeErrorDecorator argument:dict_tuple_types_by_prefix_to_update_default@char(dict_tuple_types_by_prefix_to_update_default={"num_":(int,float,bool)})defvery_complex_function(num_x,str_y=""):passvery_complex_function(0,"hihi")# Will PASSvery_complex_function(0.5,"heyhey")# Will PASSvery_complex_function(True)# Will PASSvery_complex_function("True")# Will FAIL and raise an ArgumentTypeErrorDecorator argument:dict_tuple_types_by_prefix@char(dict_tuple_types_by_prefix={"exception":(BaseException)})deffunction_with_only_one_check(int_x,exception_y=None):passfunction_with_only_one_check(0,Exception)# Will PASSfunction_with_only_one_check(0.5,TypeError)# Will PASS because first variable won't be checkedfunction_with_only_one_check(0.5,"ERROR")# Will FAIL and raise an ArgumentTypeErrorLinksPYPIreadthedocsGitHubProject local LinksCHANGELOG.CONTRIBUTING.ContactsEmail:stas.prokopiev@gmail.comvk.comFacebookLicenseThis project is licensed under the MIT License.