Package: classes
classesSmart, pythonic, ad-hoc, typed polymorphism for Python.FeaturesProvides a bunch of primitives to write declarative business logicEnforces better architectureFully typed with annotations and checked withmypy,PEP561 compatibleAllows to write a lot of simple code without inheritance or interfacesPythonic and pleasant to write and to read (!)Easy to start: has lots of docs, tests, and tutorialsInstallationpipinstallclassesYou also need toconfiguremypycorrectly and install our plugin:# In setup.cfg or mypy.ini:[mypy]plugins=classes.contrib.mypy.classes_pluginWithout this step, your project will report type-violations here and there.We also recommend to use the samemypysettingswe use.Make sure you know how to get started,check out our docs!ExampleImagine, that you want to bound implementation to some particular type.
Like, strings behave like this, numbers behave like that, and so on.The good realworld example isdjangorestframework.
It is build around the idea that different
data types should be converted differently to and fromjsonformat.What is the "traditional" (or outdated if you will!) approach?
To create tons of classes for different data types and use them.That's how we end up with classes like so:classIntField(Field):deffrom_json(self,value):returnvaluedefto_json(self,value):returnvalueIt literally has a lot of problems:It is hard to type this code. How can I be sure that myjsonis parseable by the given schema?It produces a lot of boilerplateIt has complex API: there are usually several methods to override, some fields to adjust. Moreover, we use a class, not a simple functionIt is hard to extend the default library for new custom types you will have in your own projectIt is hard to overrideThere should be a better way of solving this problem!
And typeclasses are a better way!How would new API look like with this concept?>>>fromtypingimportUnion>>>fromclassesimporttypeclass>>>@typeclass...defto_json(instance)->str:..."""This is a typeclass definition to convert things to json.""">>>@to_json.instance(int)...@to_json.instance(float)...def_to_json_int(instance:Union[int,float])->str:...returnstr(instance)>>>@to_json.instance(bool)...def_to_json_bool(instance:bool)->str:...return'true'ifinstanceelse'false'>>>@to_json.instance(list)...def_to_json_list(instance:list)->str:...return'[{0}]'.format(...', '.join(to_json(list_item)forlist_itemininstance),...)See how easy it is to works with types and implementation?Typeclass is represented as a regular function, so you can use it like one:>>>to_json(True)'true'>>>to_json(1)'1'>>>to_json([False,1,2.5])'[false, 1, 2.5]'And it easy to extend this typeclass with your own classes as well:# Pretending to import the existing library from somewhere:# from to_json import to_json>>>importdatetimeasdt>>>@to_json.instance(dt.datetime)...def_to_json_datetime(instance:dt.datetime)->str:...returninstance.isoformat()>>>to_json(dt.datetime(2019,10,31,12,28,00))'2019-10-31T12:28:00'That's how simple, safe, and powerful typeclasses are!
Make sure tocheck out our full docsto learn more.More!Want more?Go to the docs!Or read these articles:Typeclasses in Python— ⭐️ —Drylabs maintains dry-python and helps those who want to use it inside their organizations.Read more atdrylabs.io