Skip to main content

A set of useful python classes to inherit from

Project description

Overview

This set of libraries is intended to provide useful classes to inherit from, in a way that is cross compatible between multiple languages. A version of this library is currently available in:

  • Python (2 or 3)

  • Javascript

The libraries currently provide:

  • singleton objects

  • serializable objects

Singleton

The singleton class provides a way to ensure you only have one instance of a class. For instance:

from predecessor import Singleton


class Example(Singleton):
    def __init__(self, foo, bar):
        self.foo = foo
        self.bar = bar


a = Example(3, 8)
b = Example(2, 9)
a is b  # returns True
a.foo == b.foo == 3  # returns True

Or equivalently in Javascript:

const Singleton = require('predecessor').Singleton;

class Example extends Singleton {
    constructor(foo, bar)   {
        this.foo = foo;
        this.bar = bar;
    }
}

let a = new Example(3, 8);
let b = new Example(2, 9);
a === b;  // returns true
a.foo === 3;  // returns true
b.foo === 3;  // returns true

Serializable

The singleton class provides a way to serialize a class without needing to care about what form the resulting blob takes. If a compatible class definition is available in all supported languages, it should be deserializable in all supported languages.

The Basic Case

from predecessor import Serializable


class Example(Serializable):
    def __init__(self, foo, bar):  # Note that keyword args are not supported
        self.foo = foo
        self.bar = bar

    def serialized(self):
        return super(Example, self).serialized(self.foo, self.bar)


a = Example(3, 8)
b = Example.deserialize(a.serialized())
a.foo == b.foo == 3  # returns True
a.bar == b.bar == 8  # returns True

Or equivalently in Javascript:

const Serializable = require('predecessor').Serializable;

class Example extends Serializable {
    constructor(foo, bar)   {
        this.foo = foo;
        this.bar = bar;
    }

    serialized()    {
        return super.serialized(this.foo, this.bar);
    }
}

let a = new Example(3, 8);
let b = Example.deserialize(a.serialized());
a.foo === 3;  // returns true
b.foo === 3;  // returns true
a.foo === 8;  // returns true
b.foo === 8;  // returns true

Implied Serialization

In both languages, you can also use implied serialization. This looks like:

class Example(Serializable):
    __slots__ = ('a', 'b', 'c')

    def __init__(self, a, b, c):
        self.a = a
        self.b = b
        self.c = c
class Example extends Serializable  {
    constructor(a, b, c)    {
        super();
        this._slots = ['a', 'b', 'c'];
        this.a = a;
        this.b = b;
        this.c = c;
    }
}

Advanced Recombination

In both languages you can do data processing before feeding things into your constructor.

class Example(Serializable):
    def __init__(self, a, b, c):
        self.a = a
        self.b = b
        self.c = c

    def serialized(self):
        return super(Example, self).serialized(self.a, self.b)

    @classmethod
    def recombine(cls, a, b):
        return cls(a, b, a+b)
class Example extends Serializable  {
    constructor(a, b, c)    {
        super();
        this.a = a;
        this.b = b;
        this.c = c;
    }

    serialized()    {
        return super.serialized(this.a, this.b);
    }

    static recombine(a, b)  {
        return new this(a, b, a+b);
    }
}

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

predecessor-0.0.3.tar.gz (2.8 kB view details)

Uploaded Source

Built Distribution

predecessor-0.0.3-py2.py3-none-any.whl (4.4 kB view details)

Uploaded Python 2 Python 3

File details

Details for the file predecessor-0.0.3.tar.gz.

File metadata

  • Download URL: predecessor-0.0.3.tar.gz
  • Upload date:
  • Size: 2.8 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No

File hashes

Hashes for predecessor-0.0.3.tar.gz
Algorithm Hash digest
SHA256 64d5681481d56765145c0c6afde07302405dca2d34acdd71650928890a8f73b1
MD5 f91a9352ad866c6a655d1d599b67c91f
BLAKE2b-256 583879b3aa3eaa4caa654d0da932613825121a6266ba21d1ea32784a0d58e318

See more details on using hashes here.

File details

Details for the file predecessor-0.0.3-py2.py3-none-any.whl.

File metadata

File hashes

Hashes for predecessor-0.0.3-py2.py3-none-any.whl
Algorithm Hash digest
SHA256 7c8def70350fa26bb5f3ceb049942d6eecc902f1c0fc7b057720c2efb02e8022
MD5 3d9879c61268ec46246b04e949720c8e
BLAKE2b-256 218f7c45e1f57cd9e0d806f2df31b3d0104a890ab5430d9a6bea9d37c4554d80

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