The Python library to remove all the boilerplate from your code — inspired in https://projectlombok.org/ 🔥
Project description
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.
Project details
Release history Release notifications | RSS feed
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
File details
Details for the file pybok-0.0.1.tar.gz
.
File metadata
- Download URL: pybok-0.0.1.tar.gz
- Upload date:
- Size: 10.9 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: poetry/1.3.1 CPython/3.9.15 Darwin/22.2.0
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 6890ca9feb60ba85717f3a31e1f564d247bd02b1676cff1115573995ddd5159b |
|
MD5 | ee3f599be21e4b706bccdc14c66be40a |
|
BLAKE2b-256 | efe5b62104367716dfeb125eb70bca23e2f9b8fbc2034366c73e4570be53e3c4 |
File details
Details for the file pybok-0.0.1-py3-none-any.whl
.
File metadata
- Download URL: pybok-0.0.1-py3-none-any.whl
- Upload date:
- Size: 12.3 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: poetry/1.3.1 CPython/3.9.15 Darwin/22.2.0
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 388e9da076cf95bebf055a4972e8255f729b43725d0600cbe0eea7b742a134d1 |
|
MD5 | a81a8a2c46e8716f345d582001c84aea |
|
BLAKE2b-256 | ddc128c29c9643ab7c54a7a8bfd2d3f04ccdf270aff8101d23ff740473411cb3 |