Skip to main content

Standard Rust core types implementations for Python.

Project description

sain

A pure Python package that implements standard Rust core types for Python.

Install

You'll need Python 3.8 or higher.

Still not on PyPI

$ pip install git+https://github.com/nxtlo/sain

Example

import sain

#[cfg_attr(target_os = unix)]
@sain.cfg_attr(target_os="unix")
def run_when_unix() -> None:
    import uvloop
    uvloop.install()

# If this returns True, get_token will run, otherwise returns.
if sain.cfg(requires_modules=("dotenv", ...), python_version=(3, 9, 6)):
    # Stright up replace typing.Optional[str]
    def get_token() -> sain.Option[str]:
        import dotenv
        return sain.Some(dotenv.get_key(".env", "SECRET_TOKEN"))

# Raises RuntimeError("No token found.") if T is None.
token: int = get_token().expect("No token found.")

# Unwrap the value, Returning DEFAULT_TOKEN if it was None.
env_or_default: str = get_token().unwrap_or("DEFAULT_TOKEN")

# type hint is fine.
as_none: sain.Option[str] = sain.Some(None)
assert as_none.is_none()

Defaults

A protocol that types can implement which have a default value.

import sain

class DefaultCache(sain.Default[dict[str, int]]):
    # One staticmethod must be implemented and must return the same type.
    @staticmethod
    def default() -> dict[str, int]:
        return {}

Iter

Turns normal iterables into Iter type.

import sain

f = sain.Iter([1,2,3])
# or f = sain.into_iter([1,2,3])
assert 1 in f

for item in f.take_while(lambda i: i > 1):
    print(item)

Why

i like Rust coding style :p

Notes

Since Rust is a compiled language, Whatever predict returns False will not compile.

But there's no such thing as this in Python, So RuntimeError will be raised and whatever was predicated will not run.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

sain-0.0.2.tar.gz (13.2 kB view hashes)

Uploaded Source

Built Distribution

sain-0.0.2-py3-none-any.whl (18.2 kB view hashes)

Uploaded Python 3

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page