pyiv_common
Ubiquitous-library integrations for pyiv.
pyiv-common is the #include <stdlib.h> extra for pyiv: PyYAML and
requests only. Core pyiv stays stdlib-only.
What Problem Does This Solve?
Libraries such as PyYAML and requests are everywhere in Python, but they
are not the standard library. Putting them in core would break pyiv’s
zero-runtime-dependency guarantee. A dedicated package per library would
proliferate wheels. pyiv-common is that middle tier.
Real-World Use Cases:
- YAML config files via YAMLSerDe
- HTTP with the requests stack via RequestsClient
- Injecting those types through a pyiv Config
Usage:
>>> from pyiv_common.serde import YAMLSerDe
>>> YAMLSerDe().handler_type
'yaml'
>>> from pyiv_common.network import RequestsClient
>>> RequestsClient().handler_type
'http'
- class pyiv_common.RequestsClient[source]
Bases:
NetworkClientHTTP(S) client using the requests library.
handler_typeis"http"so it can replace urllibHTTPClienton the NETWORK_CLIENT chain. Bothhttp://andhttps://URLs are accepted.Example
>>> from pyiv import ChainType, Config, get_injector >>> from pyiv_common.network import RequestsClient >>> class MyConfig(Config): ... def configure(self): ... self.register_chain_handler( ... ChainType.NETWORK_CLIENT, "http", RequestsClient ... ) >>> injector = get_injector(MyConfig) >>> client = injector.inject_chain_handler(ChainType.NETWORK_CLIENT, "http") >>> isinstance(client, RequestsClient) True
- request(method: str, url: str, headers: Dict[str, str] | None = None, data: str | bytes | None = None, timeout: float | None = None) Dict[str, Any][source]
Make an HTTP request with requests.
- Parameters:
- Returns:
status: HTTP status code
headers: Response headers dictionary
body: Response body (bytes)
url: Final URL after redirects
- Return type:
Dictionary containing
- Raises:
ValueError – If URL is not http:// or https://
requests.RequestException – If the request fails
- class pyiv_common.YAMLSerDe[source]
Bases:
SerDeYAML encoding SerDe using PyYAML.
Install with
pip install pyiv-common. Register on the ENCODING chain like any otherSerDe.Example
>>> from pyiv import ChainType, Config, get_injector >>> from pyiv_common.serde import YAMLSerDe >>> class MyConfig(Config): ... def configure(self): ... self.register_chain_handler(ChainType.ENCODING, "yaml", YAMLSerDe) >>> injector = get_injector(MyConfig) >>> serde = injector.inject_chain_handler(ChainType.ENCODING, "yaml") >>> serde.deserialize(serde.serialize({"a": 1})) {'a': 1}
Modules
YAML SerDe for pyiv, backed by PyYAML. |
|
requests-backed NetworkClient for pyiv. |