GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Categories
  • Newsletter
  • Submit
  • Login
  • Awesome Python
  • Functional Programming

This view

  • Original page
  • New address

GeezerVison is showing python.libhunt.com — classes Alternatives - Python Functional Programming | LibHunt

  • Subscribe
  • Submit
  • Categories
  • About
  • Login

classes

Smart, pythonic, ad-hoc, typed polymorphism for Python

dry-python logo
classes.rtfd.io    Source Code    Changelog
Suggest Changes
Popularity
3.9
Stable
Activity
4.5
-
Stars 730
Watchers 12
Forks 30
Last Commit almost 2 years ago

Programming language: Python
License: BSD 2-clause "Simplified" License
Tags: Functional Programming     Python     Type Hints     Python3     Mypy    
Latest version: v0.4.1

classes alternatives and similar packages

Based on the "Functional Programming" category.
Alternatively, view classes alternatives based on common mentions on social networks and blogs.

  • Toolz

    7.7 4.7 L4 classes VS Toolz
    A functional standard library for Python.
    pytoolz logo
  • Coconut

    7.2 6.1 classes VS Coconut
    Simple, elegant, Pythonic functional programming.
    evhub logo
  • SaaSHub - Software Alternatives and Reviews

    SaaSHub helps you find the best software and product alternatives
    Promo www.saashub.com
    SaaSHub Logo
  • returns

    7.2 9.2 classes VS returns
    Make your functions return something meaningful, typed, and safe!
    dry-python logo
  • fn.py

    6.9 0.0 L4 classes VS fn.py
    Functional programming in Python: implementation of missing features to enjoy FP
    kachayev logo
  • funcy

    6.9 8.6 L5 classes VS funcy
    A fancy and practical functional tools lib
    Suor logo
  • Pyrsistent

    6.1 6.9 L5 classes VS Pyrsistent
    Persistent/Immutable/Functional data structures for Python
    tobgu logo
  • CyToolz

    4.7 3.5 L4 classes VS CyToolz
    Cython implementation of Toolz: High performance functional utilities
    pytoolz logo
  • Deal

    4.4 6.7 classes VS Deal
    🤝 Design by contract for Python. Write bug-free code. Add a few decorators, get static analysis and tests for free.
    life4 logo
  • PyContracts

    3.5 1.3 classes VS PyContracts
    PyContracts is a Python package that allows to declare constraints on function parameters and return values. Contracts can be specified using Python3 annotations, or inside a docstring. PyContracts supports a basic type system, variables binding, arithmetic constraints, and has several specialized contracts and an extension API.
    AndreaCensi logo
  • icontract

    3.3 5.8 classes VS icontract
    Design-by-contract in Python3 with informative violation messages and inheritance
    Parquery logo
  • effect

    3.2 2.6 classes VS effect
    effect isolation in Python, to facilitate more purely functional code
    python-effect logo
  • dpcontracts

    3.1 10.0 classes VS dpcontracts
    An implementation of contracts for Python.
    deadpixi logo

* Code Quality Rankings and insights are calculated and provided by Lumnify.
They vary from L1 to L5 with "L5" being the highest.

Do you think we are missing an alternative of classes or a related project?

Add another 'Functional Programming' Package

Popular Comparisons

  • classes
    vs
    funcy
  • classes
    vs
    PyContracts
  • classes
    vs
    effect
  • classes
    vs
    Pyrsistent
  • classes
    vs
    fn.py

README

classes

classes logo


Build Status codecov Documentation Status Python Version wemake-python-styleguide Telegram chat


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

pip install classes

You also need to configure mypy correctly and install our plugin:

# 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 bound 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 build 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:

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 json is 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?

>>> from typing import Union
>>> from classes import typeclass

>>> @typeclass
... def to_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:
...     return str(instance)

>>> @to_json.instance(bool)
... def _to_json_bool(instance: bool) -> str:
...     return 'true' if instance else 'false'

>>> @to_json.instance(list)
... def _to_json_list(instance: list) -> str:
...     return '[{0}]'.format(
...         ', '.join(to_json(list_item) for list_item in instance),
...     )

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

>>> import datetime as dt

>>> @to_json.instance(dt.datetime)
... def _to_json_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 at drylabs.io

Do not miss the trending, packages, news and articles with our weekly report.

Awesome Python is part of the LibHunt network. Terms. Privacy Policy.

(CC)
BY-SA
Spin The Wheel Of Names for a cryptographically secure random name picker.
Schedule a when to meet poll.
You are visitor 01402503 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.