Forwarded from Machine Learning with Python
Do you see yourself as a programmer, researcher, or engineer?
Anonymous Poll
44%
Programmer
22%
Researcher
34%
Engineer
Do you hate writing monotonous
__init__, __repr__ and __eq__ for each class? Dataclasses do it for you.class Point:
def __init__(self, x, y):
self.x = x
self.y = y
def __repr__(self):
return f"Point(x={self.x}, y={self.y})"
def __eq__(self, other):
return self.x == other.x and self.y == other.y
class User:
def __init__(self, name, age):
self.name = name
self.age = age
def __repr__(self):
return f"User(name={self.name}, age={self.age})"
def __eq__(self, other):
return self.name == other.name and self.age == other.age
Problem:
This is crap. Tons of boilerplate code that's easy to break or forget to update.
from dataclasses import dataclass
@dataclass
class Point:
x: int
y: int
@dataclass
class User:
name: str
age: int
p1 = Point(10, 20)
p2 = Point(10, 20)
u = User("Ivan", 30)
print(p1) # Point(x=10, y=20)
print(p1 == p2) # True
print(u) # User(name='Ivan', age=30)
How it works:
The decorator @dataclass automatically generates methods based on type annotations.
Customizing a dataclass:
from dataclasses import dataclass, fieldinits generated by default?:
@dataclass(order=True, frozen=True)
class Product:
name: str
price: float = 0.0
tags: list[str] = field(default_factory=list, compare=False)
def expensive(self):
return self.price > 1000
p1 = Product("Laptop", 1500.0)
p2 = Product("Mouse", 50.0)
print(p1 > p2) # True (price comparison due to order=True)
p1.tags.append("tech")
# p1.name = "PC" # Error! frozen=True makes the object immutable
are not a replacement for regular classes. Use them for data structures where standard methods are needed.π΅ __initreprtializer with parametersπ΅ __repr_eqetty string representationπ΅ __eq__ - comparison acltllles
gth geTrue: __lt__, __le__, __gt__, __ge__
Important:
Dataclasses
Please open Telegram to view this post
VIEW IN TELEGRAM
β€4
Creating Barcodes in Python!
Barcode generation can be easily automated using the
First, install the library itself and the dependency for image generation:
The
Import the main module and
Let's create an
Save it to disk:
As a result, the
Generating several barcodes: a common scenario is generating a series of barcodes, for example for a list of products or database records:
Here, it's also important to comply with the format requirement: 12 digits for
If you need to encode arbitrary strings (order numbers, documents), it's more convenient to use
This format is not limited to numbers and is widely used in logistics and document management.
In practice, generation is often outsourced to a function - for reuse in services or
π₯ This approach is convenient to scale and supplement with logic (validation, writer settings, saving paths in the database, etc.).
π @DataScience4
Barcode generation can be easily automated using the
python-barcode library.First, install the library itself and the dependency for image generation:
pip install python-barcode pillow
The
Pillow library is used to save in PNG format via ImageWriter.Import the main module and
writer for working with images:import barcode
from barcode.writer import ImageWriter
Let's create an
EAN-13 barcode. Important: exactly 12 digits are passed, and the check digit is automatically calculated and added.ean = barcode.get(
"ean13",
"590123412345",
writer=ImageWriter()
)
Save it to disk:
filename = ean.save("product_barcode")
print(filename)As a result, the
product_barcode.png file will be created in the current directory.Generating several barcodes: a common scenario is generating a series of barcodes, for example for a list of products or database records:
codes = [
"123456789012",
"987654321098",
"111222333444"
]
for value in codes:
code_obj = barcode.get(
"ean13",
value,
writer=ImageWriter()
)
code_obj.save(f"barcode_{value}")
Here, it's also important to comply with the format requirement: 12 digits for
EAN-13, otherwise the library will throw an exception.If you need to encode arbitrary strings (order numbers, documents), it's more convenient to use
Code128:code128 = barcode.get(
"code128",
"ORDER-2025-001",
writer=ImageWriter()
)
code128.save("order_barcode")
This format is not limited to numbers and is widely used in logistics and document management.
In practice, generation is often outsourced to a function - for reuse in services or
APIs:def generate_barcode(value, filename):
code = barcode.get(
"code128",
value,
writer=ImageWriter()
)
return code.save(filename)
generate_barcode("INVOICE-4587", "invoice_4587")
Please open Telegram to view this post
VIEW IN TELEGRAM
β€2
β¨ ast | Python Standard Library β¨
π Provides functionality to work with Abstract Syntax Trees (ASTs).
π·οΈ #Python
π Provides functionality to work with Abstract Syntax Trees (ASTs).
π·οΈ #Python
βοΈLISA HELPS EVERYONE EARN MONEY!$29,000 HE'S GIVING AWAY TODAY!
Everyone can join his channel and make money! He gives away from $200 to $5.000 every day in his channel
https://t.iss.one/+HDFF3Mo_t68zNWQy
β‘οΈFREE ONLY FOR THE FIRST 500 SUBSCRIBERS! FURTHER ENTRY IS PAID! ππ
https://t.iss.one/+HDFF3Mo_t68zNWQy
Everyone can join his channel and make money! He gives away from $200 to $5.000 every day in his channel
https://t.iss.one/+HDFF3Mo_t68zNWQy
β‘οΈFREE ONLY FOR THE FIRST 500 SUBSCRIBERS! FURTHER ENTRY IS PAID! ππ
https://t.iss.one/+HDFF3Mo_t68zNWQy
β¨ How Long Does It Take to Learn Python? β¨
π This guide breaks down how long it takes to learn Python with realistic timelines, weekly study plans, and strategies to speed up your progress.
π·οΈ #basics #career #community
π This guide breaks down how long it takes to learn Python with realistic timelines, weekly study plans, and strategies to speed up your progress.
π·οΈ #basics #career #community
β€1
Forwarded from ADMINOTEKA
Please open Telegram to view this post
VIEW IN TELEGRAM
β¨ Python + AI Content Specialist Wanted β¨
π We're looking for Python + AI Content Specialists to join our team. Keep reading to find out what's involved and how you can apply.
π·οΈ #Python
π We're looking for Python + AI Content Specialists to join our team. Keep reading to find out what's involved and how you can apply.
π·οΈ #Python
β¨ Join the Real Python Team β¨
π Explore open positions at Real Python. Join our team of Python educators and help millions of developers level up their skills.
π·οΈ #Python
π Explore open positions at Real Python. Join our team of Python educators and help millions of developers level up their skills.
π·οΈ #Python