API orientation

Assignment and CodeBlock

Structured top-level source items.

generate_python_source(obj, header="", clean_mode=False, *, context=None)

Generate complete executable source and required imports. A caller-supplied FormatContext is retained across both render passes and owns clean mode.

to_source(value, context=None)

Format one value into a SourceFragment.

SourceFormatter

Nominal extension point. Subclasses implement can_format and format; application-specific formatters register without changing pycodify core.

resolve_imports

Resolve import requirements and aliases for colliding names.

FormatContext and SourceFragment

Immutable typed formatting state and output records. FormatContext may carry exact-type extensions and exposes them through extension(type).

The canonical import surface is pycodify.__all__.

Public API

class pycodify.Assignment(name: 'str', value: 'Any')[source]
name: str
value: Any
__init__(name: str, value: Any) → None
class pycodify.BlankLine[source]
__init__() → None
class pycodify.CodeBlock(items: 'Tuple[Any, ...]')[source]
items: Tuple[Any, ...]
classmethod from_items(items: Iterable[Any]) → CodeBlock[source]
__init__(items: Tuple[Any, ...]) → None
class pycodify.Comment(text: 'str')[source]
text: str
__init__(text: str) → None
class pycodify.FormatContext(indent: int = 0, clean_mode: bool = False, name_mappings: ~collections.abc.Mapping[~typing.Tuple[str, str], str] = <factory>, extensions: ~collections.abc.Mapping[type[object], object] = <factory>)[source]

Context passed through serialization.

indent: int = 0
clean_mode: bool = False
name_mappings: Mapping[Tuple[str, str], str]
extensions: Mapping[type[object], object]
indented() → FormatContext[source]
property indent_str: str
extension(extension_type: type[Any]) → Any | None[source]

Return one caller-owned rendering extension by exact nominal type.

__init__(indent: int = 0, clean_mode: bool = False, name_mappings: ~collections.abc.Mapping[~typing.Tuple[str, str], str] = <factory>, extensions: ~collections.abc.Mapping[type[object], object] = <factory>) → None
class pycodify.SourceFormatter[source]

Formats a value to Python source code.

priority: int = 0
abstractmethod can_format(value: Any) → bool[source]

Return True if this formatter handles this value type.

abstractmethod format(value: Any, context: FormatContext) → SourceFragment[source]

Format value to Python source, returning code and required imports.

classmethod register(formatter: SourceFormatter) → None[source]

Register a virtual subclass of an ABC.

Returns the subclass, to allow usage as a class decorator.

classmethod get_formatter(value: Any) → SourceFormatter[source]
class pycodify.SourceFragment(code: str, imports: FrozenSet[Tuple[str, str]] = frozenset({}))[source]

Result of serializing a value to Python source.

code: str
imports: FrozenSet[Tuple[str, str]] = frozenset({})
__init__(code: str, imports: FrozenSet[Tuple[str, str]] = frozenset({})) → None
pycodify.generate_python_source(obj: Any, header: str = '', clean_mode: bool = False, *, context: FormatContext | None = None) → str[source]

Generate complete Python source with imports.

pycodify.resolve_imports(imports: FrozenSet[Tuple[str, str]] | Iterable[Tuple[str, str]]) → Tuple[List[str], Dict[Tuple[str, str], str]][source]

Resolve import collisions and generate import lines.

pycodify.to_source(value: Any, ctx: FormatContext | None = None) → SourceFragment[source]

Convert any value to Python source code.