Skip to main content

Documentation Index

Fetch the complete documentation index at: https://mintlify.com/alex-ber/AlexBerUtils/llms.txt

Use this file to discover all available pages before exploring further.

The props module provides two independent utilities: Properties, a Python port of java.util.Properties for reading and writing .properties files, and lazyproperty, a descriptor that evaluates a method only on first access and caches the result.

Properties

A Python replacement for java.util.Properties. Reads files that use key=value, key:value, or whitespace-separated key value syntax. Supports # comments, backslash line continuation, and ${} placeholder substitution.
from alexber.utils.props import Properties

props = Properties()

with open('app.properties', 'r') as f:
    props.load(f)

db_host = props.getProperty('db.host')
db_host = props['db.host']  # equivalent dict-style access

__init__

props
dict
Optional initial property dictionary. Currently unused — pass None or omit.

load(stream)

Parses a .properties file from an open readable stream and populates the internal property dictionary.
with open('config.properties', 'r') as f:
    props.load(f)
stream
IO
required
Any readable stream (file object, io.StringIO, etc.).
Raises: IOError — propagated from the underlying stream read.

getProperty(key)

Returns the value for the given key, or an empty string if the key is not found.
key
str
required
Property key to look up.
Returns: str — the property value, or '' if absent.

setProperty(key, value)

Sets a property. Both key and value must be strings.
key
str
required
Property key.
value
str
required
Property value.
Raises: TypeError — if either argument is not a string.

propertyNames()

Returns an iterator over all property keys. Returns: KeysView[str]

list(out=sys.stdout)

Prints all properties to a stream in key=value format.
out
IO
Output stream. Defaults to sys.stdout.

store(out, header="")

Writes all properties to a writable file stream with a timestamp header.
out
IO
required
Writable file stream. Must be opened in write mode ('w').
header
str
Optional comment header written as the first line. Defaults to "".
Raises: ValueError — if the stream is not opened in write mode. IOError — propagated from the stream.

as_dict()

Returns the internal properties as a plain OrderedDict. Returns: OrderedDict[str, str]

Dict-style access

Properties supports direct dictionary-style get and set via __getitem__ and __setitem__:
props['server.port'] = '8080'
print(props['server.port'])  # '8080'

lazyproperty

Decorator like @property, but the decorated method is evaluated only on the first access. The result is cached in the instance’s __dict__ and returned directly on every subsequent access without re-evaluation.
from alexber.utils.props import lazyproperty

class MyModel:
    @lazyproperty
    def expensive_data(self):
        print('Computing...')
        return [i ** 2 for i in range(1_000_000)]

obj = MyModel()
obj.expensive_data  # prints 'Computing...' and caches result
obj.expensive_data  # returns cached result immediately
lazyproperty is a data descriptor — it lives in the class __dict__ and its __get__ is invoked on every attribute access, ensuring the cache in the instance __dict__ is used after the first evaluation.

__init__(fget)

fget
Callable[[self], any]
required
The method to decorate. Must accept only self as a parameter (same constraint as @property). The method’s __name__, __doc__, and other attributes are copied to the descriptor via functools.update_wrapper.

Immutability

lazyproperty is read-only. Attempting to assign to a lazyproperty attribute unconditionally raises AttributeError.
obj.expensive_data = 42  # raises AttributeError: can't set attribute
lazyproperty is not suitable for wrapping plain functions (as opposed to instance methods) because it is not directly callable.

IllegalArgumentException

Exception raised internally by Properties when a .properties file contains a malformed line.
from alexber.utils.props import IllegalArgumentException

try:
    props.load(stream)
except IllegalArgumentException as e:
    print(f'Parse error at line {e.lineno}: {e.msg}')
AttributeTypeDescription
linenointLine number in the source file where the error occurred.
msgstrHuman-readable error description.