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 details)

Uploaded Source

Built Distribution

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

Uploaded Python 3

File details

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

File metadata

  • Download URL: proxied-1.0.1.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.1.tar.gz
Algorithm Hash digest
SHA256 b9f7c7c721345a495de1292e2f3502e2c7dc1bed62e27b8e737657c1fe19a94b
MD5 ec7de828186bb325113db3d2955ef80e
BLAKE2b-256 d367e01fc5a83142534960046064d1815023b836ae2f4f7d7849ee7ab28423f5

See more details on using hashes here.

File details

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

File metadata

  • Download URL: proxied-1.0.1-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.1-py3-none-any.whl
Algorithm Hash digest
SHA256 a55bbcb1a7ff91f7619f1bbe3844c97252ab69bd5c037144151217fd895f6370
MD5 370bad83064b99e4c44923ddfb6c96be
BLAKE2b-256 798973c5a75d3c33247a892dd729e8b4204721940e31409aff45cda15ea3b0c4

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