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.0.tar.gz (5.3 kB view details)

Uploaded Source

Built Distribution

proxied-1.0.0-py3-none-any.whl (5.0 kB view details)

Uploaded Python 3

File details

Details for the file proxied-1.0.0.tar.gz.

File metadata

  • Download URL: proxied-1.0.0.tar.gz
  • Upload date:
  • Size: 5.3 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.1.5 CPython/3.8.7 Linux/4.15.0-1077-gcp

File hashes

Hashes for proxied-1.0.0.tar.gz
Algorithm Hash digest
SHA256 c17adacf6401208dbb0d9024c90ec5147571ddebbe9ba45567b87da32c17f041
MD5 198c3ed1c9f7aee9b24a041b34181278
BLAKE2b-256 f0893e18e6fa186c0a58b655429408f0e53556e9bc48c822750dcfe709549e7f

See more details on using hashes here.

File details

Details for the file proxied-1.0.0-py3-none-any.whl.

File metadata

  • Download URL: proxied-1.0.0-py3-none-any.whl
  • Upload date:
  • Size: 5.0 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.1.5 CPython/3.8.7 Linux/4.15.0-1077-gcp

File hashes

Hashes for proxied-1.0.0-py3-none-any.whl
Algorithm Hash digest
SHA256 87340a115a26f1d2e1f6bfaac30efd32eb990beead2dc2196a015203985b6668
MD5 56793f931715089274ca9a402e3ee20c
BLAKE2b-256 63bcf88e058f95d809e989ed7629fe77de91cdd8cc1f4da642b5588199808e12

See more details on using hashes here.

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