classes
Smart, pythonic, ad-hoc, typed polymorphism for Python
Science Score: 36.0%
This score indicates how likely this project is to be science-related based on various indicators:
-
○CITATION.cff file
-
✓codemeta.json file
Found codemeta.json file -
✓.zenodo.json file
Found .zenodo.json file -
○DOI references
-
○Academic publication links
-
✓Committers with academic emails
1 of 16 committers (6.3%) from academic institutions -
○Institutional organization owner
-
○JOSS paper metadata
-
○Scientific vocabulary similarity
Low similarity (16.2%) to scientific vocabulary
Keywords
Keywords from Contributors
Repository
Smart, pythonic, ad-hoc, typed polymorphism for Python
Basic Info
- Host: GitHub
- Owner: dry-python
- License: bsd-2-clause
- Language: Python
- Default Branch: master
- Homepage: https://classes.rtfd.io
- Size: 881 KB
Statistics
- Stars: 716
- Watchers: 14
- Forks: 29
- Open Issues: 37
- Releases: 5
Topics
Metadata Files
README.md
classes
Smart, pythonic, ad-hoc, typed polymorphism for Python.
Features
- Provides a bunch of primitives to write declarative business logic
- Enforces better architecture
- Fully typed with annotations and checked with
mypy, PEP561 compatible - Allows to write a lot of simple code without inheritance or interfaces
- Pythonic and pleasant to write and to read (!)
- Easy to start: has lots of docs, tests, and tutorials
Installation
bash
pip install classes
You also need to configure
mypy correctly and install our plugin:
```ini
In setup.cfg or mypy.ini:
[mypy] plugins = classes.contrib.mypy.classes_plugin ```
Without this step, your project will report type-violations here and there.
We also recommend to use the same mypy settings we use.
Make sure you know how to get started, check out our docs!
Example
Imagine, that you want to bind implementation to some particular type. Like, strings behave like this, numbers behave like that, and so on.
The good realworld example is djangorestframework.
It is built around the idea that different
data types should be converted differently to and from json format.
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:
```python class IntField(Field): def from_json(self, value): return value
def to_json(self, value):
return value
```
It literally has a lot of problems:
- It is hard to type this code. How can I be sure that my
jsonis parseable by the given schema? - It produces a lot of boilerplate
- It has complex API: there are usually several methods to override, some fields to adjust. Moreover, we use a class, not a simple function
- It is hard to extend the default library for new custom types you will have in your own project
- It is hard to override
There should be a better way of solving this problem! And typeclasses are a better way!
How would new API look like with this concept?
```python
from typing import Union from classes import typeclass
@typeclass ... def to_json(instance) -> str: ... """This is a typeclass definition to convert things to json."""
@tojson.instance(int) ... @tojson.instance(float) ... def tojson_int(instance: Union[int, float]) -> str: ... return str(instance)
@tojson.instance(bool) ... def _tojson_bool(instance: bool) -> str: ... return 'true' if instance else 'false'
@tojson.instance(list) ... def _tojsonlist(instance: list) -> str: ... return '[{0}]'.format( ... ', '.join(tojson(listitem) for listitem in instance), ... )
```
See how easy it is to work with types and implementation?
Typeclass is represented as a regular function, so you can use it like one:
```python
tojson(True) 'true' tojson(1) '1' to_json([False, 1, 2.5]) '[false, 1, 2.5]'
```
And it is easy to extend this typeclass with your own classes as well:
```python
Pretending to import the existing library from somewhere:
from tojson import tojson
import datetime as dt
@tojson.instance(dt.datetime) ... def _tojson_datetime(instance: dt.datetime) -> str: ... return instance.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 to check out our full docs to 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 in our Telegram group
Owner
- Name: dry-python
- Login: dry-python
- Kind: organization
- Email: mail@sobolevn.me
- Website: https://dry-labs.github.io
- Repositories: 10
- Profile: https://github.com/dry-python
A set of libraries for pluggable business logic components.
GitHub Events
Total
- Issues event: 4
- Watch event: 54
- Issue comment event: 13
- Push event: 2
- Pull request review event: 7
- Pull request review comment event: 2
- Pull request event: 7
- Fork event: 3
Last Year
- Issues event: 4
- Watch event: 54
- Issue comment event: 13
- Push event: 2
- Pull request review event: 7
- Pull request review comment event: 2
- Pull request event: 7
- Fork event: 3
Committers
Last synced: 8 months ago
Top Committers
| Name | Commits | |
|---|---|---|
| sobolevn | m****l@s****e | 122 |
| dependabot[bot] | 4****] | 112 |
| dependabot-preview[bot] | 2****] | 106 |
| Pablo Aguilar | p****r@o****r | 8 |
| mikhail-akimov | d****o@g****m | 3 |
| Maxim Ivanov | 4****g | 2 |
| Anton Agestam | g****t@a****e | 1 |
| David Brochart | d****t@g****m | 1 |
| Farhad Taebi | f****i@u****e | 1 |
| Nicholas Mobbs | n****n | 1 |
| Sergey | 5****p | 1 |
| ShalokShalom | s****m@p****h | 1 |
| Vincent Poulailleau | v****u@g****m | 1 |
| github-actions[bot] | 4****] | 1 |
| q0w | 4****w | 1 |
| Dmytro Litvinov | l****t@g****m | 1 |
Committer Domains (Top 20 + Academic)
Issues and Pull Requests
Last synced: 6 months ago
All Time
- Total issues: 6
- Total pull requests: 107
- Average time to close issues: 3 months
- Average time to close pull requests: 13 days
- Total issue authors: 3
- Total pull request authors: 10
- Average comments per issue: 1.5
- Average comments per pull request: 0.76
- Merged pull requests: 57
- Bot issues: 0
- Bot pull requests: 95
Past Year
- Issues: 4
- Pull requests: 6
- Average time to close issues: 3 months
- Average time to close pull requests: about 4 hours
- Issue authors: 1
- Pull request authors: 2
- Average comments per issue: 1.0
- Average comments per pull request: 1.33
- Merged pull requests: 3
- Bot issues: 0
- Bot pull requests: 0
Top Authors
Issue Authors
- ivanovmg (4)
- zhukovgreen (1)
- sobolevn (1)
Pull Request Authors
- dependabot[bot] (92)
- ivanovmg (6)
- davidbrochart (2)
- dependabot-preview[bot] (2)
- antonagestam (1)
- sobolevn (1)
- q0w (1)
- thepabloaguilar (1)
- ShalokShalom (1)
- zhukovgreen (1)
Top Labels
Issue Labels
Pull Request Labels
Packages
- Total packages: 2
-
Total downloads:
- pypi 21,966 last-month
-
Total dependent packages: 5
(may contain duplicates) -
Total dependent repositories: 40
(may contain duplicates) - Total versions: 7
- Total maintainers: 2
pypi.org: classes
Smart, pythonic, ad-hoc, typed polymorphism for Python
- Homepage: https://classes.readthedocs.io
- Documentation: https://classes.readthedocs.io/
- License: BSD-2-Clause
-
Latest release: 0.4.1
published almost 4 years ago
Rankings
Maintainers (2)
pypi.org: effects
Smart, pythonic, ad-hoc, typed polymorphism for Python
- Homepage: https://classes.readthedocs.io
- Documentation: https://effects.readthedocs.io/
- License: BSD-2-Clause
-
Latest release: 0.0.1
published about 6 years ago
Rankings
Maintainers (1)
Dependencies
- actions/checkout v3 composite
- actions/setup-python v4 composite
- codecov/codecov-action v3 composite
- 123 dependencies
- codespell ^2.2 develop
- doc8 ^1.0 develop
- flake8-pytest-style ^1.6 develop
- m2r2 ^0.3 develop
- mypy ^0.942 develop
- nitpick ^0.32 develop
- phantom-types ^1.0 develop
- pytest ^7.2 develop
- pytest-cov ^4.0 develop
- pytest-mypy-plugins ^1.9 develop
- pytest-randomly ^3.12 develop
- safety ^2.3 develop
- sphinx ^5.2 develop
- sphinx-autodoc-typehints ^1.20 develop
- sphinx-typlog-theme ^0.8 develop
- sphinxcontrib-mermaid ^0.7 develop
- tomlkit ^0.11 develop
- wemake-python-styleguide ^0.17 develop
- python ^3.7
- typing_extensions >=3.10,<5.0
