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/GRef: Reference any Python object by its module path (e.g.,"mymodule:MyClass"). Useful for configuration files and lazy loading.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"
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
GRef = Annotated[GlobalRef, WithJsonSchema({'type': 'string'}, mode='serialization')]
module-attribute
¶
GlobalRef
¶
>>> 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'
Source code in src/lythonic/__init__.py
59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 | |
get_instance()
¶
Resolve the named attribute from the module. If the name ends
with __ and the attribute doesn't exist, strip the suffix and
call the resulting factory function instead.
The __ suffix convention supports lazy initialization: define
a factory function xyz() that returns an object, and reference
it as "module:xyz__". If the module defines xyz__ directly
(e.g., for caching an immutable result), that takes priority.
For mutable objects, omit the cached variable so the factory is
called fresh each time.
Source code in src/lythonic/__init__.py
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'