Implementation of generic python builder
Project description
Python Builder
Simple implementation of a builder pattern for Python. This library is done to be as small and simple as possible. I wanted an easy wrapper around any class.
In my tests, I explicitly check if my library works with:
- base python
class - python
dataclass - python "slotted" class (using
__slots__) - pydantic
BaseModel
But because of how simple the code is, I expect it to work with all classes.
Usage
To use the Python Builder, decorate your classes with @add_builder and utilize the generated builder() method to construct instances fluently.
Example with a Regular Python Class
from python_builder import add_builder
@add_builder
class RegularClass:
def __init__(self, a: int, b: str, c: bool):
self.a = a
self.b = b
self.c = c
# Building an instance
builder = RegularClass.builder()
instance = (
builder
.set("a", 10)
.set("b", "test")
.set("c", True)
.build()
)
print(instance.a) # Output: 10
print(instance.b) # Output: test
print(instance.c) # Output: True
Example with a Dataclass
from dataclasses import dataclass
from python_builder import add_builder
@add_builder
@dataclass
class DataClass:
x: float = None
y: str = None
z: int = None
# Building an instance
instance = (
DataClass.builder()
.set("x", 3.14)
.set("y", "pi")
.set("z", 42)
.build()
)
print(instance.x) # Output: 3.14
print(instance.y) # Output: pi
print(instance.z) # Output: 42
Example with a Pydantic Model
from pydantic import BaseModel
from python_builder import add_builder
@add_builder
class PydanticModel(BaseModel):
foo: str
bar: int
baz: bool
# Building an instance
instance = (
PydanticModel.builder()
.set("foo", "hello")
.set("bar", 123)
.set("baz", False)
.build()
)
print(instance.foo) # Output: hello
print(instance.bar) # Output: 123
print(instance.baz) # Output: False
Example with a Slotted Class
from python_builder import add_builder
@add_builder
class SlotClass:
__slots__ = ["x", "y", "z"]
def __init__(self, x: int, y: str, z: bool):
self.x = x
self.y = y
self.z = z
# Building an instance
instance = (
SlotClass.builder()
.set("x", 100)
.set("y", "slot test")
.set("z", True)
.build()
)
print(instance.x) # Output: 100
print(instance.y) # Output: slot test
print(instance.z) # Output: True
Merging Builders
You can merge multiple builder instances using the | operator. In case of conflicting properties, the last set value will take precedence.
builder1 = RegularClass.builder().set("a", 1)
builder2 = RegularClass.builder().set("b", "abc")
merged_builder = builder1 | builder2
instance = merged_builder.set("c", True).build()
print(instance.a) # Output: 1
print(instance.b) # Output: abc
print(instance.c) # Output: True
Handling Errors
If you set an invalid property or omit required properties, the builder will raise appropriate errors.
# Setting an invalid property
builder = RegularClass.builder().set("d", "invalid")
instance = builder.build() # Raises TypeError
# Building with missing required properties
builder = RegularClass.builder().set("a", 100)
instance = builder.build() # Raises TypeError
These examples demonstrate how to utilize the Python Builder with different types of classes, handle merging of builders, and manage potential errors during the building process.
Developement
I use uv so I expect you have it installed too. After cloning the repo, run:
uv sync
That should install all dev dependencies. After that, activate venv and run:
pre-commit install
That will auto format your code on each commit.
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
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file python_builder-0.3.0.tar.gz.
File metadata
- Download URL: python_builder-0.3.0.tar.gz
- Upload date:
- Size: 4.7 kB
- Tags: Source
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.12.9
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
8153485d0c37598fb792786b0662fc5bc0e87092028a92aaf7de8c7b7b0fc851
|
|
| MD5 |
1523899675bba6979932450f4bc8b309
|
|
| BLAKE2b-256 |
68c9ad9b5741657b2df1c7cf93db7f0803b7c1ab491919e6c1d1c31e7b101d12
|
Provenance
The following attestation bundles were made for python_builder-0.3.0.tar.gz:
Publisher:
python-publish.yml on ChrisW-priv/python-builder
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
python_builder-0.3.0.tar.gz -
Subject digest:
8153485d0c37598fb792786b0662fc5bc0e87092028a92aaf7de8c7b7b0fc851 - Sigstore transparency entry: 178471735
- Sigstore integration time:
-
Permalink:
ChrisW-priv/python-builder@c00d85e0f33df217a30183bb0dabb0da88d6932d -
Branch / Tag:
refs/tags/v0.3.0 - Owner: https://github.com/ChrisW-priv
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
python-publish.yml@c00d85e0f33df217a30183bb0dabb0da88d6932d -
Trigger Event:
release
-
Statement type:
File details
Details for the file python_builder-0.3.0-py3-none-any.whl.
File metadata
- Download URL: python_builder-0.3.0-py3-none-any.whl
- Upload date:
- Size: 4.1 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.12.9
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
d73412f1d71e82e983624f3137cc213048db342390f3654ad0b17267a951dee9
|
|
| MD5 |
7ea6673fe45c4ac3d9262674f6dbd2c7
|
|
| BLAKE2b-256 |
e3338e3e73ce79e759a28e850025197f11f494476f1f4d8bfeed8e690d4d0ff7
|
Provenance
The following attestation bundles were made for python_builder-0.3.0-py3-none-any.whl:
Publisher:
python-publish.yml on ChrisW-priv/python-builder
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
python_builder-0.3.0-py3-none-any.whl -
Subject digest:
d73412f1d71e82e983624f3137cc213048db342390f3654ad0b17267a951dee9 - Sigstore transparency entry: 178471736
- Sigstore integration time:
-
Permalink:
ChrisW-priv/python-builder@c00d85e0f33df217a30183bb0dabb0da88d6932d -
Branch / Tag:
refs/tags/v0.3.0 - Owner: https://github.com/ChrisW-priv
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
python-publish.yml@c00d85e0f33df217a30183bb0dabb0da88d6932d -
Trigger Event:
release
-
Statement type: