Skip to main content

A simple to use proxy for python objects

Project description

proxied

proxied package can be use to defer initialization of object.

Suppose we have:

__init__.py

from config import Config, load_config

config: Config = None

def init_app():
    config = load_config()

If we try to import config in another file, i.e.

cool_staff.py

from . import config

...

init_staff(config.db_url)

We will end up importing None. So, we will be forced to make import not at the top of the file.

Here comes the proxied package

__init__.py

from typing import Union
from proxy import Proxy
from config import Config, load_config

config: Union[Config, Proxy] = Proxy()

def init_app():
    config.set_inner(load_config())

Now we can easily import config at the top of the file work with proxy object as it was the object of type Config.

Installation

pip install proxied

Proxy

Proxy class can be initialized with inner or inner_constructor. If inner_constructor is supplied, then it will be called once, and the result will be cached.

If Proxy class is initialized without inner and inner_constructor, the inner should be set later with a help of proxy.set_inner method.

from proxy import Proxy
proxy = Proxy()
proxy.set_inner({})
proxy["test_key"] = 10

It’s possible to set values for multiple proxies.

from proxy import Proxy
proxies = [Proxy(), Proxy(), Proxy(), Proxy()]
values = [10, 11, list(), dict()]
Proxy.set_proxies(proxies, values)

There is a check, if proxy is initialized with proxied value

from proxy import Proxy
proxy = Proxy()

if not proxy.initialized:
    data = get_needed_data(...)
    proxy.set_inner(data)

Example

from proxy import Proxy
class NotAvailableDuringImport:
    @property
    def data(self):
        return "Not Available during import"


proxy: Union[NotAvailableDuringImport, Proxy] = Proxy()
proxy.set_inner(NotAvailableDuringImport)
assert proxy.data == "Not Available during import"

License

Copyright Oleksii Petrenko, 2020.

Distributed under the terms of the MIT license, json_modify is free and open source software.

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

proxied-1.0.1.tar.gz (5.3 kB view hashes)

Uploaded Source

Built Distribution

proxied-1.0.1-py3-none-any.whl (5.0 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