Skip to main content

Create data objects

Project description

Cobble is a Python library that allows easy creation of data objects, including implementations of common methods such as __eq__ and __repr__.

Examples

import cobble

@cobble.data
class Song(object):
    name = cobble.field()
    artist = cobble.field()
    album = cobble.field(default=None)


song = Song("MFEO", artist="Jack's Mannequin")

print(song) # Prints "Song(name='MFEO', artist="Jack's Mannequin", album=None)"
class Expression(object):
    pass

@cobble.data
class Literal(Expression):
    value = cobble.field()

@cobble.data
class Add(Expression):
    left = cobble.field()
    right = cobble.field()

class Evaluator(cobble.visitor(Expression)):
    def visit_literal(self, literal):
        return literal.value

    def visit_add(self, add):
        return self.visit(add.left) + self.visit(add.right)

Evaluator().visit(Add(Literal(2), Literal(4))) # 6
class Expression(object):
    pass

@cobble.visitable
class Literal(Expression):
    def __init__(self, value):
        self.value = value

@cobble.visitable
class Add(Expression):
    def __init__(self, left, right):
        self.left = left
        self.right = right

class Evaluator(cobble.visitor(Expression)):
    def visit_literal(self, literal):
        return literal.value

    def visit_add(self, add):
        return self.visit(add.left) + self.visit(add.right)

Evaluator().visit(Add(Literal(2), Literal(4))) # 6

License

2-Clause BSD

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

cobble-0.1.4.tar.gz (3.8 kB view details)

Uploaded Source

Built Distribution

cobble-0.1.4-py3-none-any.whl (4.0 kB view details)

Uploaded Python 3

File details

Details for the file cobble-0.1.4.tar.gz.

File metadata

  • Download URL: cobble-0.1.4.tar.gz
  • Upload date:
  • Size: 3.8 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/5.1.0 CPython/3.11.2

File hashes

Hashes for cobble-0.1.4.tar.gz
Algorithm Hash digest
SHA256 de38be1539992c8a06e569630717c485a5f91be2192c461ea2b220607dfa78aa
MD5 375872d0e07f30b38b72c0aa2cc4a741
BLAKE2b-256 547aa507c709be2c96e1bb6102eb7b7f4026c5e5e223ef7d745a17d239e9d844

See more details on using hashes here.

File details

Details for the file cobble-0.1.4-py3-none-any.whl.

File metadata

  • Download URL: cobble-0.1.4-py3-none-any.whl
  • Upload date:
  • Size: 4.0 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/5.1.0 CPython/3.11.2

File hashes

Hashes for cobble-0.1.4-py3-none-any.whl
Algorithm Hash digest
SHA256 36c91b1655e599fd428e2b95fdd5f0da1ca2e9f1abb0bc871dec21a0e78a2b44
MD5 a3a42ef8ac625f78c17cd1a2f493f50d
BLAKE2b-256 d5e13714a2f371985215c219c2a70953d38e3eed81ef165aed061d21de0e998b

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