Skip to main content

Scala-like CaseClasses for Python

Project description

Build Status

Zero-dependency scala-like case classes for Python 2 + 3.

Features

  • Simple usage: Just inherit from case_class.CaseClass

  • Simple Installation: Zero dependencies

  • plays well with inheritance
    • CaseClass and AbstractCaseClass` usable as a Mix-In

    • Case-to-case inheritance forbidden by default
      • use AbstractCaseClass to allow only subclasses to be instantiated

      • use InheritableCaseClass to override allow both super and subclasses to be instantiated.`

    • also plays well with Multiple Inheritance * super() calls still work as expected

  • equality based on arguments
    • calls constructor only once per combination of arguments

    • works with == operator and is (referential equality) operator.

  • automatic repr() function

  • works in both Python 2 and Python 3!

Install

This package is published on the Python Package Index Installation can be done simply via pip:

pip install case_class

Alternatively, clone this repository and run setup.py:

git clone https://github.com/tkw1536/PythonCaseClass
python setup.py install

Examples

# Import the CaseClass module
from case_class import CaseClass

# Create a symbol case class
class Symbol(CaseClass):
    def __init__(self, name):
        self.name = name

# Create an instance
x = Symbol("x")
print(x)  # Symbol('x')

# And create another one
also_x = Symbol('x')
print(x == also_x)  # equality via operator
print(x is also_x)  # referential equality

Another example can be found in example.py.

License + Acknowledgements

This module and associated documentation is Copyright (c) Tom Wiesing 2016 and licensed under the MIT license, see license for details. Small parts of the code are adapted from the six module, which is Copyright (c) 2010-2015 Benjamin Peterson.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

case_class-0.0.8.tar.gz (12.2 kB view details)

Uploaded Source

Built Distributions

case_class-0.0.8-py3-none-any.whl (15.0 kB view details)

Uploaded Python 3

case_class-0.0.8-py2-none-any.whl (15.0 kB view details)

Uploaded Python 2

File details

Details for the file case_class-0.0.8.tar.gz.

File metadata

  • Download URL: case_class-0.0.8.tar.gz
  • Upload date:
  • Size: 12.2 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No

File hashes

Hashes for case_class-0.0.8.tar.gz
Algorithm Hash digest
SHA256 e30dac2da923c6afa296b5342fbd38c76c03803c4b3ccc8d3d2236671794931a
MD5 ec0b0c4bacf39d7662ce96290a017428
BLAKE2b-256 2aac76c5b462cf10c1c09b6c50f65d9d3de99fa7497c38ca5ca75ec77ae2015e

See more details on using hashes here.

File details

Details for the file case_class-0.0.8-py3-none-any.whl.

File metadata

File hashes

Hashes for case_class-0.0.8-py3-none-any.whl
Algorithm Hash digest
SHA256 aba2402856294f066fe8bcdceb9103e3b51acfdf009ba36a967b82a87404684b
MD5 abedd8fd1cd5e83a87b5e20a526d569e
BLAKE2b-256 aea8769a7c470e5f982796883cc92b0a83bb018b7814d8a8bb1353c0e5cfc937

See more details on using hashes here.

File details

Details for the file case_class-0.0.8-py2-none-any.whl.

File metadata

File hashes

Hashes for case_class-0.0.8-py2-none-any.whl
Algorithm Hash digest
SHA256 613645edd7a7472d064e5c22a0f15eaab3fb41355560a6dd2e648eb59ecd7ede
MD5 48193d151c59995bbe9f57c385b844a4
BLAKE2b-256 38b1132bb9e4846d77f75b6c7a22fbe022cdc734b439be58d7eb6f55e9d992a0

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page