lythonic¶
Core utilities: global references, Result type, and helpers.
Core utilities for the Lythonic library.
This module provides foundational types and utilities used throughout Lythonic:
GlobalRef: Reference any Python object by its module path (e.g.,"mymodule:MyClass"). Useful for configuration files and lazy loading.ModuleRef: Reference a Python module by its import path (e.g.,"json").Result[TOk, TErr]: A Rust-inspired Result type for explicit error handling without exceptions.utc_now(): Get the current UTC datetime.get_module(): Import a module by name.
GlobalRef Usage¶
from lythonic import GlobalRef
# Reference a class by string
ref = GlobalRef("json:dumps")
dumps_func = ref.get_instance()
# Reference from an object
ref = GlobalRef(MyClass)
print(ref) # "mymodule:MyClass"
ModuleRef Usage¶
from lythonic import ModuleRef
# Reference a module by string
ref = ModuleRef("json")
mod = ref.import_module()
# Reference from a module object
ref = ModuleRef(json)
print(ref) # "json"
Result Usage¶
from lythonic import Result
def divide(a: int, b: int) -> Result[float, str]:
if b == 0:
return Result.Err("division by zero")
return Result.Ok(a / b)
result = divide(10, 2)
if result.is_ok():
print(result.unwrap()) # 5.0
GlobalRef = GlobalRef
module-attribute
¶
A reference to a Python object identified by module path and name.
Accepts a string "module.path:name", a module, a class, a function,
or another GlobalRef instance. Module-only references use an empty name.
The string representation is always "module.path:name" (or "module.path:"
for module-only refs), matching NsRef format where scope is the module
path parts.
>>> ref = GlobalRef('lythonic:GlobalRef')
>>> ref
GlobalRef('lythonic:GlobalRef')
>>> ref.get_instance().__name__
'GlobalRef'
>>> ref.is_module()
False
>>> ref.get_module().__name__
'lythonic'
>>> grgr = GlobalRef(GlobalRef)
>>> grgr
GlobalRef('lythonic:GlobalRef')
>>> grgr.get_instance()
<class 'lythonic.GlobalRef'>
>>> grgr.is_class()
True
>>> grgr.is_function()
False
>>> grgr.is_module()
False
>>> uref = GlobalRef('lythonic:')
>>> uref.is_module()
True
>>> uref.get_module().__name__
'lythonic'
>>> uref = GlobalRef('lythonic')
>>> uref.is_module()
True
>>> uref = GlobalRef(uref)
>>> uref.is_module()
True
>>> uref.get_module().__name__
'lythonic'
>>> uref = GlobalRef(uref.get_module())
>>> uref.is_module()
True
>>> uref.get_module().__name__
'lythonic'
Result
¶
Bases: Generic[TOk, TErr]
A generic Result type inspired by Rust, representing either success (Ok) or failure (Err).
Source code in src/lythonic/__init__.py
get_module(name)
¶
>>> type(get_module('lythonic'))
<class 'module'>
>>> get_module('lythonic.c99')
Traceback (most recent call last):
...
ModuleNotFoundError: No module named 'lythonic.c99'