Cobble: Create Python Data Objects

0.1.4 · active · verified Thu Apr 09

Cobble is a Python library designed for the easy creation of data objects. It automatically implements common methods such as `__eq__` and `__repr__`, simplifying the boilerplate typically associated with simple data-holding classes. The current version is 0.1.4, released on June 1, 2024, and it is actively maintained with a stable release cadence.

Warnings

Install

Imports

Quickstart

This example demonstrates how to define a simple data object `Song` using the `@cobble.data` decorator and `cobble.field()`. It also shows how to create a more complex structure like an expression tree with `Literal` and `Add` and then process it using a visitor pattern with `cobble.visitor`.

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", album="Everything in Transit")
print(song)

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

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

class Evaluator(cobble.visitor(object)):
    def visit_literal(self, literal):
        return literal.value
    def visit_add(self, add):
        return self.visit(add.left) + self.visit(add.right)

expression = Add(Literal(2), Literal(4))
result = Evaluator().visit(expression)
print(f"Evaluation of {expression} is: {result}")

view raw JSON →