Skip to content

Tools

Global collection of auxiliary methods.

get_file_size(path) async

Get file size in bytes.

Source code in src\ramifice\utils\tools.py
async def get_file_size(path: str) -> int:
    """Get file size in bytes."""
    size: int = await to_thread(getsize, path)
    return size

hash_to_obj_id(hash)

Get ObjectId from hash string.

Source code in src\ramifice\utils\tools.py
def hash_to_obj_id(hash: str | None) -> ObjectId | None:
    """Get ObjectId from hash string."""
    return ObjectId(hash) if bool(hash) else None

is_color(color_code)

Validate Color code.

Source code in src\ramifice\utils\tools.py
def is_color(color_code: str | None) -> bool:
    """Validate Color code."""
    return REGEX["color_code"].match(str(color_code)) is not None

is_email(email) async

Validate Email address.

Source code in src\ramifice\utils\tools.py
async def is_email(email: str | None) -> bool:
    """Validate Email address."""
    try:
        await to_thread(
            validate_email,
            str(email),
            check_deliverability=True,
        )
    except EmailNotValidError:
        return False
    return True

is_ip(address)

Validate IP address.

Source code in src\ramifice\utils\tools.py
def is_ip(address: str | int | None) -> bool:
    """Validate IP address."""
    try:
        ipaddress.ip_address(str(address))
    except ValueError:
        return False
    return True

is_mongo_id(oid)

Validation of the Mongodb identifier.

Source code in src\ramifice\utils\tools.py
def is_mongo_id(oid: Any) -> bool:
    """Validation of the Mongodb identifier."""
    return ObjectId.is_valid(oid)

is_password(password)

Validate Password.

Source code in src\ramifice\utils\tools.py
def is_password(password: str | None) -> bool:
    """Validate Password."""
    return REGEX["password"].match(str(password)) is not None

is_phone(number)

Validate Phone number.

Source code in src\ramifice\utils\tools.py
def is_phone(number: str | None) -> bool:
    """Validate Phone number."""
    try:
        phone = phonenumbers.parse(str(number))
        if not phonenumbers.is_valid_number(phone):
            return False
    except phonenumbers.phonenumberutil.NumberParseException:
        return False
    return True

is_url(url)

Validate URL address.

Source code in src\ramifice\utils\tools.py
def is_url(url: str | None) -> bool:
    """Validate URL address."""
    result = urlparse(str(url))
    return not (not result.scheme or not result.netloc)

normal_email(email)

Normalizing email address.

Use this before requeste to a database. For example, on the login page.

Source code in src\ramifice\utils\tools.py
def normal_email(email: str | None) -> str | None:
    """Normalizing email address.

    Use this before requeste to a database.
    For example, on the login page.
    """
    normal: str | None = None
    try:
        emailinfo = validate_email(
            str(email),
            check_deliverability=False,
        )
        normal = emailinfo.normalized
    except EmailNotValidError:
        pass
    return normal