Skip to content

Commit

Permalink
Improved lazy load functions
Browse files Browse the repository at this point in the history
  • Loading branch information
dhondta committed Nov 22, 2023
1 parent 2c4c6a4 commit 65acb17
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 15 deletions.
2 changes: 1 addition & 1 deletion docs/pages/enhancements.md
Expand Up @@ -6,7 +6,7 @@ Formerly a set of [helper functions](helpers.md), the following have been attach

Code can be monkey-patched at runtime using multiple functions, depending on what should be patched and how. Note that some of the functions rely on the [`patchy`](https://github.com/adamchainz/patchy) module.

- `add_line`, `add_lines`, `insert_line`, `insert_line`: it allows to add line(s) at specific indices (starting from 0), before or after (using `after=True`).
- `add_line`, `add_lines`, `insert_line`, `insert_lines`: it allows to add line(s) at specific indices (starting from 0), before or after (using `after=True`).
- `delete_line`, `delete_lines`, `remove_line`, `remove_lines`: it allows to delete line(s) by index (starting from 0).
- `patch`: alias for `patchy.patch`, taking a function and a patch file's text as arguments.
- `replace`: wrapper for `patchy.replace`, handling multiple replacements at a time, either replacing whole function (like in original `replace`) or only parts of the code.
Expand Down
2 changes: 1 addition & 1 deletion src/tinyscript/VERSION.txt
@@ -1 +1 @@
1.29.3
1.29.4
34 changes: 22 additions & 12 deletions src/tinyscript/__conf__.py
Expand Up @@ -2,7 +2,7 @@
"""Deprecation warnings, for use when no backward-compatibility provided.
"""
import builtins
import builtins as bi
import warnings
from importlib import import_module
from inspect import currentframe
Expand Down Expand Up @@ -32,39 +32,49 @@ def _wrapper(*a, **kw):
_warn(old, new)


builtins.deprecate = deprecate
builtins.warn = warnings.warn
bi.deprecate = deprecate
bi.warn = warnings.warn

builtins.lazy_object = Proxy
bi.lazy_object = Proxy


def lazy_load_module(module, relative=None, alias=None, postload=None):
def lazy_load_module(module, relative=None, alias=None, preload=None, postload=None):
""" Lazily load a module. """
glob = currentframe().f_back.f_globals
def _load():
if callable(preload):
preload()
glob[alias or module] = m = import_module(*((module, ) if relative is None else ("." + module, relative)))
m.__name__ = alias or module
if postload is not None:
postload(m)
if callable(postload):
try:
postload()
except TypeError:
postload(m)
return m
glob[alias or module] = m = Proxy(_load)
return m
builtins.lazy_load_module = lazy_load_module
bi.lazy_load_module = lazy_load_module


def lazy_load_object(name, load_func, postload=None):
def lazy_load_object(name, load_func, preload=None, postload=None):
""" Lazily load an object. """
glob = currentframe().f_back.f_globals
def _load():
if callable(preload):
preload()
glob[name] = o = load_func()
try:
o._instance = o
except (AttributeError, TypeError):
pass
if postload is not None:
postload(o)
if callable(postload):
try:
postload()
except TypeError:
postload(o)
return o
glob[name] = o = Proxy(_load)
return o
builtins.lazy_load_object = lazy_load_object
bi.lazy_load_object = lazy_load_object

2 changes: 1 addition & 1 deletion src/tinyscript/__main__.py
Expand Up @@ -155,7 +155,7 @@ def main():
__add_src(search)
with commands.add_parser("update", help="update the list of publicly available scripts") as update:
update.add_argument("-s", "--source", nargs="*", type=url, help="set a source URL for a list of scripts")
initialize(noargs_action="wizard")
initialize(add_version=True, noargs_action="wizard")
if args.command =="add-source":
s, sources = args.url, _get_sources_list()
if not args.fetch or args.fetch and _fetch_source(s):
Expand Down

0 comments on commit 65acb17

Please sign in to comment.