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
pybok-0.0.1.tar.gz
(10.9 kB
view hashes)
Built Distribution
pybok-0.0.1-py3-none-any.whl
(12.3 kB
view hashes)