Core utilities to complement Python's standard library
Project description
Frequenz Core Library
Introduction
Core utilities to complement Python's standard library. This library provides essential building blocks for Python applications, including mathematical utilities, datetime constants, typing helpers, strongly-typed identifiers, and module introspection tools.
The frequenz-core library is designed to be lightweight, type-safe, and
follow modern Python best practices. It fills common gaps in the standard
library with utilities that are frequently needed across different projects.
Supported Platforms
The following platforms are officially supported (tested):
- Python: 3.11
- Operating System: Ubuntu Linux 20.04
- Architectures: amd64, arm64
Installation
You can install the library from PyPI using pip:
python -m pip install frequenz-core
Or add it to your project's dependencies in pyproject.toml:
[project]
dependencies = [
"frequenz-core >= 1.0.2, < 2",
]
[!NOTE] We recommend pinning the dependency to the latest version for programs, like
"frequenz-core == 1.0.2", and specifying a version range spanning one major version for libraries, like"frequenz-core >= 1.0.2, < 2". We follow semver.
Quick Start
Here's a quick overview of the main functionality:
from frequenz.core.math import is_close_to_zero, Interval
from frequenz.core.datetime import UNIX_EPOCH
from frequenz.core.module import get_public_module_name
# Math utilities
print(is_close_to_zero(1e-10)) # True - check if float is close to zero
interval = Interval(1, 10)
print(5 in interval) # True - check if value is in range
# Datetime utilities
print(UNIX_EPOCH) # 1970-01-01 00:00:00+00:00
# Module utilities
public_name = get_public_module_name("my.package._private.module")
print(public_name) # "my.package"
Code Examples
Math Utilities
The math module provides utilities for floating-point comparisons and interval checking:
from frequenz.core.math import is_close_to_zero, Interval
# Robust floating-point zero comparison
assert is_close_to_zero(1e-10) # True
assert not is_close_to_zero(0.1) # False
# Interval checking with inclusive bounds
numbers = Interval(0, 100)
assert 50 in numbers # True
assert not (150 in numbers) # False - 150 is outside the interval
# Unbounded intervals
positive = Interval(0, None) # [0, ∞]
assert 1000 in positive # True
Enum with deprecated members
Define enums with deprecated members that raise deprecation warnings when accessed:
from frequenz.core.enum import Enum, deprecated_member, unique
@unique
class TaskStatus(Enum):
OPEN = 1
IN_PROGRESS = 2
# Duplicate values are fine with `@unique` as long as they are deprecated
PENDING = deprecated_member(1, "PENDING is deprecated, use OPEN instead")
DONE = deprecated_member(3, "DONE is deprecated, use FINISHED instead")
FINISHED = 4
status1 = TaskStatus.PENDING # Warns: "PENDING is deprecated, use OPEN instead"
assert status1 is TaskStatus.OPEN
Typing Utilities
Disable class constructors to enforce factory pattern usage:
from frequenz.core.typing import disable_init
@disable_init
class ApiClient:
@classmethod
def create(cls, api_key: str) -> "ApiClient":
# Factory method with validation
instance = cls.__new__(cls)
# Custom initialization logic here
return instance
# This will raise TypeError:
# client = ApiClient() # ❌ TypeError
# Use factory method instead:
client = ApiClient.create("my-api-key") # ✅ Works
Strongly-Typed IDs
Create type-safe identifiers for different entities:
from frequenz.core.id import BaseId
class UserId(BaseId, str_prefix="USR"):
pass
class OrderId(BaseId, str_prefix="ORD"):
pass
user_id = UserId(123)
order_id = OrderId(456)
print(f"User: {user_id}") # User: USR123
print(f"Order: {order_id}") # Order: ORD456
# Type safety prevents mixing different ID types
def process_user(user_id: UserId) -> None:
print(f"Processing user: {user_id}")
process_user(user_id) # ✅ Works
# process_user(order_id) # ❌ Type error
Documentation
For information on how to use this library, please refer to the documentation.
Contributing
If you want to know how to build this project and contribute to it, please check out the Contributing Guide.
Project details
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
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file frequenz_core-1.3.0.tar.gz.
File metadata
- Download URL: frequenz_core-1.3.0.tar.gz
- Upload date:
- Size: 19.0 kB
- Tags: Source
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.13.7
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
f614c01ea962943c1cc3ad0797bc4f1bdfebde948fec02dbe06dcb29b5adefe4
|
|
| MD5 |
4b1220ed3992ea5b712b1804ce68fa92
|
|
| BLAKE2b-256 |
73a7877c2f2b10c9532294e3c053ebcdc0515bed8f8647f889f8b710e917bc61
|
Provenance
The following attestation bundles were made for frequenz_core-1.3.0.tar.gz:
Publisher:
ci.yaml on frequenz-floss/frequenz-core-python
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
frequenz_core-1.3.0.tar.gz -
Subject digest:
f614c01ea962943c1cc3ad0797bc4f1bdfebde948fec02dbe06dcb29b5adefe4 - Sigstore transparency entry: 563469683
- Sigstore integration time:
-
Permalink:
frequenz-floss/frequenz-core-python@3bc144e0c69ad05f3ceb1bd5b540c27fe9379d3a -
Branch / Tag:
refs/tags/v1.3.0 - Owner: https://github.com/frequenz-floss
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
ci.yaml@3bc144e0c69ad05f3ceb1bd5b540c27fe9379d3a -
Trigger Event:
push
-
Statement type:
File details
Details for the file frequenz_core-1.3.0-py3-none-any.whl.
File metadata
- Download URL: frequenz_core-1.3.0-py3-none-any.whl
- Upload date:
- Size: 17.2 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.13.7
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
1cdf98361a1103f328ab3200542997de12ee47d22cc19456696697640a948c16
|
|
| MD5 |
062c440aa2aefc4072d9e9691f8e9598
|
|
| BLAKE2b-256 |
96c1805f9fa42b4ab93864423a1fc5441d21aa1b6d9994a21f8621fcec9ad6c8
|
Provenance
The following attestation bundles were made for frequenz_core-1.3.0-py3-none-any.whl:
Publisher:
ci.yaml on frequenz-floss/frequenz-core-python
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
frequenz_core-1.3.0-py3-none-any.whl -
Subject digest:
1cdf98361a1103f328ab3200542997de12ee47d22cc19456696697640a948c16 - Sigstore transparency entry: 563469698
- Sigstore integration time:
-
Permalink:
frequenz-floss/frequenz-core-python@3bc144e0c69ad05f3ceb1bd5b540c27fe9379d3a -
Branch / Tag:
refs/tags/v1.3.0 - Owner: https://github.com/frequenz-floss
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
ci.yaml@3bc144e0c69ad05f3ceb1bd5b540c27fe9379d3a -
Trigger Event:
push
-
Statement type: