Skip to main content

a simple way to proxy imports in python

Project description

import-proxy

a simple way to proxy imports in python

usage

proxy any object's attributes as a module:

from importproxy import register, object_resolver

class ApiClient:
    def get_user(self, id): return f"user {id}"
    def get_posts(self): return ["post1", "post2"]

client = ApiClient()
register('api', object_resolver(client))

from api import get_user, get_posts
print(get_user(123))  # user 123

create synthetic modules from dictionaries:

from importproxy import register, synthetic_module

math_funcs = {
    'double': lambda x: x * 2,
    'square': lambda x: x ** 2,
    'PI': 3.14159
}

register('mymath', synthetic_module(math_funcs))

from mymath import double, PI
print(double(5))  # 10

proxy real modules with custom names:

from importproxy import register, proxy_module
import pathlib

register('paths', proxy_module(pathlib))

from paths import Path
p = Path('/tmp/test')

chain multiple sources together:

from importproxy import register, chain_resolvers, dict_resolver, proxy_module
import math

custom = {'triple': lambda x: x * 3}

register('math_2', chain_resolvers(
    dict_resolver(custom),    # try custom first
    proxy_module(math)        # fall back to real math
))

from math_2 import triple, sin
print(triple(4))  # 12 (custom)
print(sin(0))     # 0.0 (from math)

why does this exist?

i was tired of having to Thing = something.Thing when working with rpyc remote objects. now i can just from something import Thing and have my scripts work unmodified

yes, it's a massive hack, but it's funny, it amuses me

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

import_proxy-0.1.1.tar.gz (9.6 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

import_proxy-0.1.1-py3-none-any.whl (3.8 kB view details)

Uploaded Python 3

File details

Details for the file import_proxy-0.1.1.tar.gz.

File metadata

  • Download URL: import_proxy-0.1.1.tar.gz
  • Upload date:
  • Size: 9.6 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/2.1.4 CPython/3.13.5 Darwin/24.6.0

File hashes

Hashes for import_proxy-0.1.1.tar.gz
Algorithm Hash digest
SHA256 c44a2d1bd0f613cf4d2f930e4f9caf58769cc696ba621e5666d0521f13f7f8c2
MD5 2bbfac7f5723a15cff91f3f42dd018c0
BLAKE2b-256 275d8c4f4fb801197d70b0b6b7a9eba6676bf8d5cb43454cb4ab9a8aac766aa9

See more details on using hashes here.

File details

Details for the file import_proxy-0.1.1-py3-none-any.whl.

File metadata

  • Download URL: import_proxy-0.1.1-py3-none-any.whl
  • Upload date:
  • Size: 3.8 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/2.1.4 CPython/3.13.5 Darwin/24.6.0

File hashes

Hashes for import_proxy-0.1.1-py3-none-any.whl
Algorithm Hash digest
SHA256 2504942a549a99b103182af3eea7f2e06f945ccd8c7205a8b58e96ebcde99d81
MD5 048addd269be755634bab9cca2be0fa4
BLAKE2b-256 05df4f6b0b91f2c234ef41ebe9d37260cd5e02cdd15c94ceaceb251b15f268c5

See more details on using hashes here.

Supported by

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