How to use the pfun.effect.get_environment function in pfun

To help you get started, we’ve selected a few pfun examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github suned / pfun / tests / test_effect.py View on Github external
def test_get_environment(self):
        assert effect.get_environment().run('env') == 'env'
github suned / pfun / pfun / console.py View on Github external
Get an `Effect` that prints to the console and succeeds with `None`

    Example:
        >>> class Env:
        ...     console = Console()
        >>> print_line('Hello pfun!').run(Env())
        Hello pfun!

    Args:
        msg: Message to print

    Return:
        `Effect` that prints to the console using the \
        `HasConsole` provided to `run`
    """
    return get_environment(HasConsole
                           ).and_then(lambda env: env.console.print(msg))
github suned / pfun / pfun / files.py View on Github external
def read(path: str) -> Effect[HasFiles, OSError, str]:
    """
    get an `Effect` that reads the content of a file as a str

    Example:
        >>> class Env:
        ...     files = Files()
        >>> read('foo.txt').run(Env())
        'contents of foo.txt'

    Args:
        path: path to file
    Return:
        `Effect` that reads file located at `path`
    """
    return get_environment(HasFiles).and_then(lambda env: env.files.read(path))
github suned / pfun / pfun / files.py View on Github external
Example:
        >>> class Env:
        ...     files = Files()
        >>> write('foo.txt')('contents')\\
        ...     .discard_and_then(read('foo.txt'))\\
        ...     .run(Env())
        'content of foo.txt'

    Args:
        path: the path of the file to be written
        content the content to write
    Return:
        `Effect` that that writes `content` to file at `path`
    """
    return get_environment(HasFiles).and_then(
        lambda env: env.files.write(path, content)
    )
github suned / pfun / pfun / sql.py View on Github external
"""
    Get an `Effect` that produces a
    `asyncpg.Connection`. Used to work with `asyncpg` directly.

    Example:
        >>> class Env:
        ...     sql = SQL('postgres://user@host/database')
        >>> get_connection()(Env())
        

    Return:
        `Effect` that produces `asyncpg.Connection`
    Return:
        `Effect` that produces `asyncpg.Connection`
    """
    return get_environment(HasSQL
                           ).and_then(lambda env: env.sql.get_connection())
github suned / pfun / pfun / http.py View on Github external
Make a HTTP `patch` request to URL `url`. All keyword
    arguments are passed to `aiohttp.ClientSession.request`. Refer
    to the original documentation for their meaning.

    Example:
        >>> class Env:
        ...     http = HTTP()
        >>> patch('http://foo.com')(Env())
        Response(...)

    Args:
        url: target URL for the request
    Return:
        `Effect` that produces a `Response`
    """
    return get_environment(HasHTTP).and_then(
        lambda env: env.http.make_request(
            'patch',
            url,
            params=params,
            data=data,
            json=json,
            cookies=cookies,
            headers=headers,
            skip_auto_headers=skip_auto_headers,
            auth=auth,
            allow_redirects=allow_redirects,
            max_redirects=max_redirects,
            compress=compress,
            chunked=chunked,
            expect100=expect100,
            raise_for_status=raise_for_status,
github suned / pfun / pfun / files.py View on Github external
Example:
        >>> class Env:
        ...     files = Files()
        >>> append('foo.txt')('content of foo.txt')\\
        ...     .discard_and_then(read('foo.txt'))\\
        ...     .run(Env())
        'content of foo.txt'

    Args:
        path: the path of the file to be written
        content the content to append
    Return:
        `Effect` that that appends `content` to file at `path`
    """
    return get_environment(HasFiles).and_then(
        lambda env: env.files.append(path, content)
    )
github suned / pfun / pfun / files.py View on Github external
Example:
        >>> class Env:
        ...     files = Files()
        >>> write_bytes('foo.txt')(b'content of foo.txt')\\
        ...     .discard_and_then(read('foo.txt'))\\
        ...     .run(Env())
        'content of foo.txt'

    Args:
        path: the path of the file to be written
        content the content to write
    Return:
        `Effect` that that writes `content` to file at `path`
    """
    return get_environment(HasFiles).and_then(
        lambda env: env.files.write_bytes(path, content)
    )
github suned / pfun / pfun / logging.py View on Github external
Example:
        >>> class Env:
        ...     logging = Logging()
        >>> critical('hello!').run(Env())
        CRITICAL:root:hello!

    Args:
        msg: the log message
        stack_info: whether to include stack information in the log message
        exc_info: whether to include exception info in the log message

    Return:
        `Effect` that calls `logging.critical` with `msg`
    """
    return get_environment().and_then(
        lambda env: env.logging.
        critical(msg, stack_info=stack_info, exc_info=exc_info)
    )
github suned / pfun / pfun / sql.py View on Github external
...     sql = SQL('postgres://user@host/database')
        >>> execute_many(
        ...     'INSERT INTO users(name, age) VALUES($1, $2)',
        ...     [('bob', 32), ('alice', 20)]
        ... )(Env())
        'INSERT 2'

    Args:
        query: query to execute
        args: arguments for query
        timeout: query timeout
    Return:
        `Effect` that executes `query` with all args in `args` and \
        produces a database response for each query
    """
    return get_environment(HasSQL).and_then(
        lambda env: env.sql.execute_many(query, args, timeout=timeout)
    )