Skip to content

Utils

utils

Additional utilities that fit nowhere else.

GT = TypeVar('GT', bound=GenericObject) module-attribute

KT = TypeVar('KT') module-attribute

ObjRef: TypeAlias = UUID | str module-attribute

Self = TypeVar('Self', bound='Wrapper[Any]') module-attribute

T = TypeVar('T') module-attribute

VT = TypeVar('VT') module-attribute

ObjRefWrapper

Wrapper for objects that have an obj_ref attribute.

Note: This allows us to define Mixin classes that require the obj_ref attribute.

obj_ref: GT instance-attribute

SList

A list that holds often only a single element.

item() -> T

Wrapper(*args: Any, **kwargs: Any)

Convert objects from the obj-based API to the high-level API and vice versa.

obj_ref: GT = obj_api_type.build(*args, **kwargs) instance-attribute

__init_subclass__(wraps: type[GT], **kwargs: Any)

__new__(*args, **kwargs) -> Wrapper

wrap_obj_ref(obj_ref: GT) -> Self classmethod

Wraps obj_ref into a high-level object for the API of Ultimate Notion.

convert_md_to_py(path: Path | str)

Converts a Markdown file to a py file by extracting all python codeblocks

deepcopy_with_sharing(obj: Any, shared_attributes: list[str], memo: dict[int, Any] | None = None)

Like deepcopy but specified attributes are shared.

Deepcopy an object, except for a given list of attributes, which should be shared between the original object and its copy.

Parameters:

Name Type Description Default
obj Any

some object to copy

required
shared_attributes list[str]

A list of strings identifying the attributes that should be shared instead of copied.

required
memo dict[int, Any] | None

dictionary passed into deepcopy. Ignore this argument if not calling from within deepcopy.

None

Example

class A(object):
    def __init__(self):
        self.copy_me = []
        self.share_me = []

    def __deepcopy__(self, memo):
        return deepcopy_with_sharing(
            self, shared_attribute_names=["share_me"], memo=memo
        )


a = A()
b = deepcopy(a)
assert a.copy_me is not b.copy_me
assert a.share_me is b.share_me

c = deepcopy(b)
assert c.copy_me is not b.copy_me
assert c.share_me is b.share_me

Original from https://stackoverflow.com/a/24621200

dict_diff(dct1: dict[KT, VT], dct2: dict[KT, VT]) -> tuple[list[KT], list[KT], dict[KT, tuple[VT, VT]]]

Returns the added keys, removed keys and keys of changed values of both dictionaries.

dict_diff_str(dct1: dict[KT, VT], dct2: dict[KT, VT]) -> tuple[str, str, str]

Returns the added keys, removed keys and keys of changed values of both dictionaries as strings for printing.

find_index(elem: Any, lst: list[Any]) -> int | None

Find the index of the element in the list or return None.

find_indices(elements: np.ndarray | list[Any], total_set: np.ndarray | list[Any]) -> np.ndarray

Finds the indices of the elements in the total set.

get_active_session() -> Session

Return the current active session or raise an exception.

Avoids cyclic imports when used within the package itself. For internal use mostly.

get_repr(obj: Any, /, *, name: Any = None, desc: Any = None) -> str

Default representation, i.e. repr(...), used by us for consistency.

get_url(object_id: UUID | str) -> str

Return the URL for the object with the given id.

get_uuid(obj: str | UUID | objs.ParentRef | objs.NotionObject | objs.BlockRef) -> UUID

Retrieves a UUID from an object reference.

Only meant for internal use.

is_notebook() -> bool

Determine if we are running within a Jupyter notebook.

local_time_zone() -> tzinfo

Returns the local time zone.

rank(arr: np.ndarray) -> np.ndarray

Returns the rank of the elements in the array and gives the same rank to equal elements.

store_retvals(func)

Decorator storing the return values as function attribute for later cleanups.

This can be used for instance in a generator like this:

@pytest.fixture
def create_blank_db(notion, test_area):
    @store_retvals
    def nested_func(db_name):
        db = notion.databases.create(
            parent=test_area,
            title=db_name,
            schema={
                "Name": schema.Title(),
            },
        )
        return db

    yield nested_func

    # clean up by deleting the db of each prior call
    for db in nested_func.retvals:
        notion.databases.delete(db)

str_hash(*args: str, n_chars: int = 16) -> str

Hashes string arguments to a n-character string.