33.Packaging – pyproject, build, publish | CLI, Concurrency, I/O, Data
Packaging with pyproject.toml
Modern Python packaging revolves around the use of the pyproject.toml file, which standardizes build configuration across tools. It replaces setup.py and setup.cfg, allowing tools like setuptools, poetry, and flit to define dependencies, build systems, and metadata.
Packaging with pyproject.toml
Example pyproject.toml:
[project]
name = “example_package”
version = “0.1.0”
description = “An example Python package”
authors = [{ name=”Your Name”, email=”your@email.com” }]
dependencies = [“requests”]
[build-system]
requires = [“setuptools>=42”, “wheel”]
build-backend = “setuptools.build_meta”
Building and Publishing
To build a package, use the build module:
Command: python -m build
To publish to PyPI, use twine:
Command: twine upload dist/*
Poetry simplifies this with: poetry build and poetry publish
Creating CLI Applications
Command-line interfaces (CLI) allow users to interact with Python programs via the terminal. The argparse and click libraries are commonly used.
Example using argparse:
import argparse
parser = argparse.ArgumentParser(description=”Greet the user.”)
parser.add_argument(“name”, help=”Your name”)
args = parser.parse_args()
print(f”Hello, {args.name}!”)
Concurrency with threading and asyncio
Concurrency allows programs to perform multiple tasks simultaneously. Python supports threading for I/O-bound tasks and asyncio for asynchronous programming.
Example using threading:
import threading
def task():
print(“Running in thread”)
thread = threading.Thread(target=task)
thread.start()
Example using asyncio:
import asyncio
async def task():
print(“Running asynchronously”)
asyncio.run(task())
File I/O Operations
Python provides built-in functions to read and write files. Use open(), read(), write(), and with statements for safe file handling.
Example:
with open(“example.txt”, “w”) as f:
f.write(“Hello, world!”)
with open(“example.txt”, “r”) as f:
content = f.read()
print(content)
Data Handling with JSON and pandas
JSON is a lightweight data format. Use the json module to serialize and deserialize data.
Example:
import json
data = {“name”: “Alice”, “age”: 30}
json_str = json.dumps(data)
parsed = json.loads(json_str)
Pandas is a powerful library for data analysis and manipulation.
Example:
import pandas as pd
df = pd.DataFrame({“name”: [“Alice”, “Bob”], “age”: [30, 25]})
print(df)
Common Pitfalls
– Mixing packaging tools can lead to conflicts.
– Not handling CLI errors gracefully.
– Blocking calls inside async functions.
– Forgetting to close files.
– Misusing pandas data types or ignoring missing values.