Skip to main content

PySynthetic is a set of tools that aims to make writing Python classes shorter and “cleaner”.

For instance, one can add properties and accessors (getters/setters) to a class with only one line of code (using respectively synthesize_property and synthesize_member decorators), thus making the code more than 5 times shorter (see examples ). One can even avoid the laborious task of members initialization by using the synthesize_constructor decorator that takes care of writing the __init__ method.

PySynthetic is also useful for applying strict type checking with no pain just by using the decorators’ contract argument (see PyContracts ).

Help and ideas are appreciated! Thank you!

https://api.flattr.com/button/flattr-badge-large.png https://www.paypalobjects.com/en_US/i/btn/btn_donate_SM.gif

Resources

Installation

pip install pysynthetic

Or simply from the tarball or source code if you are not using pip.

python setup.py install

Examples

Synthetic properties

With PySynthetic, the following code (8 lines)…

from synthetic import synthesize_constructor, synthesize_property

@synthesize_property('a', contract = int)
@synthesize_property('b', contract = list)
@synthesize_property('c', default = "", contract = str, read_only = True)
@synthesize_constructor()
class ShortAndClean(object):
    pass

… replaces this (43 lines):

from contracts import contract

class ThisHurtsMyKeyboard(object):

    @contract
    def __init__(self, a, b, c = ""):
        """
    :type a: int
    :type b: list
    :type c: str
"""
        self._a = a
        self._b = b
        self._c = c

    @property
    def a(self):
        return self._a

    @a.setter
    @contract
    def a(self, value):
        """
    :type value: int
"""
        self._a = value

    @property
    def b(self):
        return self._b

    @b.setter
    @contract
    def b(self, value):
        """
    :type value: list
"""
        self._b = value

    @property
    def c(self):
        return self._c

Synthetic accessors

But, if you are more into accessors than properties, you can use synthesize_member decorator instead.

This way, the following code (8 lines)…

from synthetic import synthesize_constructor, synthesize_member

@synthesize_member('a', contract = int)
@synthesize_member('b', contract = list)
@synthesize_member('c', default = "", contract = str, read_only = True)
@synthesize_constructor()
class ShortAndClean(object):
    pass

…will replace this (37 lines):

from contracts import contract

class ThisHurtsMyKeyboard(object):

    @contract
    def __init__(self, a, b, c = ""):
        """
    :type a: int
    :type b: list
    :type c: str
"""
        self._a = a
        self._b = b
        self._c = c

    def a(self):
        return self._a

    @contract
    def set_a(self, value):
        """
    :type value: int
"""
        self._a = value

    def b(self):
        return self._b

    @contract
    def set_b(self, value):
        """
    :type value: list
"""
        self._b = value

    def c(self):
        return self._c

Metadata

Release files for pysynthetic 0.5.3

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for pysynthetic 0.5.3
File Size Uploaded
pysynthetic-0.5.3.tar.gz 154.3 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for pysynthetic 0.5.3
File Interpreter ABI Platform
pysynthetic-0.5.3-py3-none-any.whl Python 3 none any Details

Total release size: 171.7 kB

Release files / pysynthetic-0.5.3.tar.gz

Download URL pysynthetic-0.5.3.tar.gz
Size 154.3 kB
Tags Source
SHA-256 checksum
How to use checksums
afaae45454bfeb1a932ff50fecc4aa3d076dae16a9f7d1b173c1b8542a0089c8
BLAKE2b-256 checksum
How to use checksums
fd168662fdba97091453f4ea6fbf22912ac4b99ee10ba998b05bdf5e9365d6d7
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/4.0.0 CPython/3.9.1

Release files / pysynthetic-0.5.3-py3-none-any.whl

Download URL pysynthetic-0.5.3-py3-none-any.whl
Size 17.4 kB
Tags Python 3
SHA-256 checksum
How to use checksums
cf59ba29fdb2bc17beb068ea01a1208ce6e9ed13d254911037d2e2bd920ade41
BLAKE2b-256 checksum
How to use checksums
6b2dc3010b8715d9d598915939eea8311c07cfb32a97995d6ce8829facd1eed4
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/4.0.0 CPython/3.9.1

Release history Release notifications | RSS feed

This release

0.5.3 This release

2 release files

0.5.2

1 release file

0.5.1

1 release file

0.5.0

2 release files

0.4.14

2 release files

0.4.13

2 release files

0.4.12

2 release files

0.4.11

2 release files

0.4.10

1 release file

0.4.9

1 release file

0.4.8

1 release file

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page