Skip to main content

Pybok

The Python library to remove all the boilerplate from your code — inspired in https://projectlombok.org/ 🔥

Installation

pip install pybok

Usage

There are multiple useful decorators.

ArgsConstructor

@ArgsConstructor
class Student:
    name: str
    age: int

student = Student("John", 21)

print(student._name) # John
print(student._age) # 21

Getter

@Getter
@ArgsConstructor
class Student:
    name: str
    age: int

student = Student("John", 21)

print(student.name) # John
print(student.age) # 21

Setter

@Setter
@ArgsConstructor
class Student:
    name: str
    age: int

student = Student("John", 21)

student.name = "Jane"
student.age = 19

print(student._name("Jane")) # Jane
print(student._age_(19)) # 19

Data

@Data
class Person:
    name: str
    age: int

p1 = Person("John", 21)

print(p1) # 'Person(name=John,age=21)'

print(p1.name) # John
print(p1.age) # 21

print((hash(p1), hash((p1._name, p1._age))) # True

p1.name = "Mike"
p1.age = 28

print(p1._name) # Mike
print(p1.get_age()) # 28

Builder

@Builder
@ArgsConstructor
class Person:
    name: str
    age: int

person = Person.builder().name("John").age(23).build()

print(person._name) # John
print(person._age # 23

EqualsAndHashCode

@EqualsAndHashCode
@ArgsConstructor
class Person:
    name: str
    age: int

p1 = Person("John", 21)

print(hash(p1) == hash((p1._name, p1._age))) # True

p2 = Person("Jane", 19)

print(p1 == p2) # False
print(p1 == p1) # True

ToString

@ToString
@ArgsConstructor
class Person:
    name: str
    age: int

person = Person("John", 21)

print(person) # Person(name=John,age=21)

ToJSON

@ToJSON
@ArgsConstructor
class Person:
    name: str
    age: int

person = Person("John", 21)

print(person.json()) # {"name": "John", "age": 21} but pretty

Singleton

@Singleton
@ArgsConstructor
class Person:
    name: str
    age: int

john = Person("John", 21)
jane = Person("Jane", 19)
empty = Person()

print(john == jane) # True
print(john == empty) # True

UtilityClass

@UtilityClass
class MyClass:
    def utility_method():
        # whatever you need

MyClass() # raises exception (cannot instantiate)

With

@With
@ArgsConstructor
class Person:
    name: str
    age: int

person = Person("Jane", 19)
same = person.with_name("Jane")
another = person.with_age(21)
        
print(person == same) # True
print(person == another) # False  

Copy

@ArgsConstructor
class Eyes:
    color: str

@Copy
@ArgsConstructor
class Person:
    name: str
    age: int
    eyes: Eyes  

person = Person("Jane", 19, Eyes("blue"))

shallow_copy = person.copy()
deep_copy = person.deepcopy()

print(person._name == shallow_copy._name) # True
print(person._age == shallow_copy._age) # True
print(person._eyes == shallow_copy._eyes) # True

print(person._name == deep_copy._name) # True
print(person._age == deep_copy._age) # True
print(person._eyes == deep_copy._eyes) # False

Log

@Log
@ArgsConstructor
class Person:
    name: str
    age: int

    def greet(self):
        self.logger.info("{name} greets")

@Log
@ArgsConstructor
class Cat:
    name: str
    age: int

    def meow(self):
        self.logger.warning(f"{name}" meows!)

person = Person("Jane", 19)

person.greet() # 2005-03-23 23:47:11,663 - pybok_app - INFO - Jane greets

cat = Cat("Tom", 8)

cat.meow() # 2005-03-23 23:47:11,663 - pybok_app - WARN - Tom meows

print(person.logger == cat.logger) # True

ConfigurationProperties

Reads any environment variable that you require. Supports default values as well.

Also, you can make it a singleton to inject it wherever you want by using the @Singleton.

os.environ[self.DAY] = "1"
os.environ[self.MONTH] = "10"

@Singleton
@ConfigurationProperties
class MyProps:
    day: str
    month: str
    year: str = '1991'

myprops = MyProps()

print(myprops._day) # 1
print(myprops._month) # 10
print(myprops._year) # 1991

my_other_props = MyProps()

print(myprops == my_other_props) # True

Contribution

Please read CONTRIBUTION.md.

Code of Conduct

Please read the Contributor Covenant Code of Conduct.

License

This project is licensed under the MIT License.

Metadata

Release files for pybok 0.0.1

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

Source distribution (sdist)

Source distribution for pybok 0.0.1
File Size Uploaded
pybok-0.0.1.tar.gz 10.9 kB Details

Built distribution (wheel)

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

Total release size: 23.2 kB

Release files / pybok-0.0.1.tar.gz

Download URL pybok-0.0.1.tar.gz
Size 10.9 kB
Tags Source
SHA-256 checksum
How to use checksums
6890ca9feb60ba85717f3a31e1f564d247bd02b1676cff1115573995ddd5159b
BLAKE2b-256 checksum
How to use checksums
efe5b62104367716dfeb125eb70bca23e2f9b8fbc2034366c73e4570be53e3c4
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via poetry/1.3.1 CPython/3.9.15 Darwin/22.2.0

Release files / pybok-0.0.1-py3-none-any.whl

Download URL pybok-0.0.1-py3-none-any.whl
Size 12.3 kB
Tags Python 3
SHA-256 checksum
How to use checksums
388e9da076cf95bebf055a4972e8255f729b43725d0600cbe0eea7b742a134d1
BLAKE2b-256 checksum
How to use checksums
ddc128c29c9643ab7c54a7a8bfd2d3f04ccdf270aff8101d23ff740473411cb3
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via poetry/1.3.1 CPython/3.9.15 Darwin/22.2.0

Release history Release notifications | RSS feed

This release

0.0.1 This release

2 release files

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