Skip to main content

rb_utils

rb_utils is a collection of utilities reused across the backend.

Modules / Functions

1) rb_utils.parser

  • snake_to_camel(s: str) -> str: Converts a snake_case string to CamelCase
  • camel_to_snake(s: str) -> str: Converts a CamelCase string to snake_case
  • t_to_dict(obj: Any) -> Any: Recursively converts nested objects (FlatBuffer T, dict/list/tuple/set, bytes, numpy arrays) into Python built-in types
  • _normalize_filter(filter_: Any) -> dict: Normalizes None/dict/JSON string into a common dict
  • to_json(obj: Any) -> str: Serializes the result of t_to_dict to a JSON string
  • to_iso(val: str | datetime) -> str: Returns an ISO string for a datetime, or the string unchanged

Example:

from rb_utils.parser import t_to_dict, to_json

payload_dict = t_to_dict(obj_payload)
payload_json = to_json(obj_payload)

2) rb_utils.flow_manager

  • safe_eval_expr(expr, variables=None, get_global_variable=None) -> Any: Safely evaluates an expression under restricted AST rules
  • _pick_target_loop(func, provided) -> asyncio.AbstractEventLoop | None: Selects the loop on which to run the callable
  • call_with_matching_args(func, **provided) -> Any: Calls a function with only the arguments matching its signature (works for both sync and async)
  • eval_value(value, variables=None, get_global_variable=None) -> Any: Recursively evaluates strings/lists/tuples/dicts
  • make_builtins_allow_most() -> dict: Builds a builtins dict excluding dangerous builtins

Example:

from rb_utils.flow_manager import eval_value

value = eval_value("a + 10", variables={"local": {"a": 5}, "global": {}})
# value == 15

3) rb_utils.asyncio_helper

  • spawn(coro, *, name=None, log_ex=True): Creates a task on the current loop and logs its completion/exceptions
  • _ensure_private_loop() -> asyncio.AbstractEventLoop: Ensures a dedicated background loop exists
  • fire_and_log(coro, *, name=None, loop=None): Safely runs on the current loop, an external loop, or the dedicated loop
  • _attach_done_logging(fut_or_task, *, name=None): Attaches a callback that logs exceptions when a Future/Task completes

Example:

from rb_utils.asyncio_helper import fire_and_log

fire_and_log(socket_client.emit("muscat/program/log", {"msg": "hello"}), name="program-log")

4) rb_utils.service_exception

  • class ServiceException(Exception):
    • Fields: message: str, status_code: int | None
    • __str__(): Outputs in the form message (status=...)
    • to_dict(): Returns {message, status_code}

Example:

from rb_utils.service_exception import ServiceException

raise ServiceException("invalid request", status_code=400)

5) rb_utils.date

  • is_valid_date(d): Validates a date/str/int(timestamp)
  • timestamp_ms_to_seconds(timestamp, tz=UTC): Normalizes a ms/seconds timestamp to seconds
  • is_has_time(s: str): Determines whether a string contains time information
  • parse_date_with_time(mode, d, tz=UTC): Converts a date/string/timestamp to a timezone-aware datetime
  • ensure_datetime(dt, tz): Coerces a date | datetime into a datetime in the given tz
  • _tz(tz_name): Looks up a timezone from the cache
  • convert_dt(local, in_tz, out_tz): Converts a datetime/date from the input tz to the output tz
  • get_current_dt_yyyymmddhhmmss(tz): Returns the current time as a YYYYMMDDHHMMSS string

Example:

from rb_utils.date import parse_date_with_time
from datetime import UTC

dt = parse_date_with_time("start", "2026-02-25", tz=UTC)

6) rb_utils.file

  • content_disposition_header(filename) -> str: Builds a Content-Disposition header for downloads
  • sanitize_filename(name, default="export.csv", max_name_len=100) -> str: Removes OS-forbidden characters and limits length
  • get_env_path() -> Path | None: Searches for a .env file relative to the working location

Example:

from rb_utils.file import sanitize_filename

safe_name = sanitize_filename("../../report?.csv")

7) rb_utils.env

  • env_get(key, default=None, start=None): Looks up a value from os.environ first, then the nearest .env.development → .env, in that order
  • load_env(start=None, override=False): Loads the nearest env files into os.environ
  • iter_env_paths(start=None): Returns the env files that will actually be read, in priority order
  • parse_env_file(path): Parses an env file into a dict

Example:

In new code, you don't need load_env() to read a single value directly.

from rb_utils.env import env_get

username = env_get("MONGO_USERNAME")
password = env_get("MONGO_PASSWORD")
host = env_get("MONGO_HOST", "127.0.0.1")

If existing code uses os.getenv(...) heavily, load once at process startup.

import os

from rb_utils.env import load_env

load_env()

username = os.getenv("MONGO_USERNAME")
password = os.getenv("MONGO_PASSWORD")

Default priority:

  1. The .env.development closest to the current location
  2. .env in the same directory
  3. .env.development in the parent directory
  4. .env in the parent directory

In other words, in a development environment .env.development overrides values in .env. Production builds do not copy .env.development, so only .env applies.

If you need a different suffix, specify it explicitly:

env_get("KEY", env_files=(".env.local", ".env.development", ".env"))

8) rb_utils.pagination

  • class PageInfo(TypedDict): Page response metadata type (mode/page/pages/limit/total/sort/order)
  • class LogsResponse(TypedDict): Log list response type (items, pageInfo)

9) rb_utils.helper

  • ManipulateZenohResHelper(obj): Function intended as a utility for fixing up Zenoh response objects

Caution:

  • The current implementation uses .hasAttr/.set access instead of dict handling and raises ValueError immediately, so verify its behavior before use.

Common Usage Patterns

  • Flattening FlatBuffer/SDK responses: t_to_dict
  • Evaluating dynamic Flow step arguments: eval_value, safe_eval_expr
  • Async fire-and-forget + exception logging: fire_and_log
  • Common service-layer exception: ServiceException
  • File downloads / filename sanitization: content_disposition_header, sanitize_filename

General Notes

  • The flow_manager evaluator only executes allowed operations/functions.
  • Timezone conversion functions require an explicit IANA tz string such as Asia/Seoul.

Metadata

Release files for rainbow-rb-utils 0.10.6

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for rainbow-rb-utils 0.10.6
File Size Uploaded Provenance Actions
rainbow_rb_utils-0.10.6.tar.gz 28.7 kB Without provenance Details for rainbow_rb_utils-0.10.6.tar.gz

Built distribution (wheel)

Table of built distributions (wheels) for rainbow-rb-utils 0.10.6
File Interpreter ABI Platform Actions
rainbow_rb_utils-0.10.6-py3-none-any.whl Python 3 none any Details for rainbow_rb_utils-0.10.6-py3-none-any.whl

Total release size: 59.1 kB

Release files / rainbow_rb_utils-0.10.6.tar.gz

Download URL rainbow_rb_utils-0.10.6.tar.gz
Size 28.7 kB
Tags Source
SHA-256 checksum
How to use checksums
de39c3623ee4e29c4d99b96395f1abac6943cd87cc4ba231789b657ce8081e18
BLAKE2b-256 checksum
How to use checksums
01d62722f0d0f066d7b0ff3aa21de718d6a600be6ac6cd0afcfff02e92a7a66d
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/7.0.0 CPython/3.12.13

Release files / rainbow_rb_utils-0.10.6-py3-none-any.whl

Download URL rainbow_rb_utils-0.10.6-py3-none-any.whl
Size 30.4 kB
Tags Python 3
SHA-256 checksum
How to use checksums
2147d21035265ddb4531f46e55cf7b7caf454e5dc06107e8cfc6fcd05e7bef0a
BLAKE2b-256 checksum
How to use checksums
82784ed4d53c5a699b4a4914a9118d71d5dc4d20fb7ae2df6bd0ce03ac910681
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/7.0.0 CPython/3.12.13

Security for rainbow-rb-utils 0.10.6

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

No provenance found

None of the files in this release were uploaded with provenance metadata.

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

Release history Release notifications | RSS feed

This release

0.10.6 This release

2 release files No provenance

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page