mirror of
https://github.com/xonsh/xonsh.git
synced 2025-03-04 00:14:41 +01:00
builtin: added `__xonsh__.imp
` to have an ability to import and use modules immediately in one line (#5595)
* imp * [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci * imp * [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci * imp * [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --------- Co-authored-by: a <1@1.1> Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
This commit is contained in:
parent
87032f6d30
commit
d08248f6aa
2 changed files with 35 additions and 0 deletions
25
news/inline_import.rst
Normal file
25
news/inline_import.rst
Normal file
|
@ -0,0 +1,25 @@
|
|||
**Added:**
|
||||
|
||||
* builtin: added ``__xonsh__.imp`` to have an ability to import and use modules immediately in one line
|
||||
e.g. ``__xonsh__.json.loads('{}')``.
|
||||
In the future ``__xonsh__`` will have short notation ``@`` and the line will looks more elegant ``@.imp.json.loads('{}')``.
|
||||
|
||||
**Changed:**
|
||||
|
||||
* <news item>
|
||||
|
||||
**Deprecated:**
|
||||
|
||||
* <news item>
|
||||
|
||||
**Removed:**
|
||||
|
||||
* <news item>
|
||||
|
||||
**Fixed:**
|
||||
|
||||
* <news item>
|
||||
|
||||
**Security:**
|
||||
|
||||
* <news item>
|
|
@ -520,6 +520,15 @@ def xonsh_builtins(execer=None):
|
|||
XSH.unload()
|
||||
|
||||
|
||||
class InlineImporter:
|
||||
"""Inline importer allows to import and use module attribute or function in one line."""
|
||||
|
||||
def __getattr__(self, name):
|
||||
if name.startswith("__"):
|
||||
return getattr(super(), name)
|
||||
return __import__(name)
|
||||
|
||||
|
||||
class XonshSession:
|
||||
"""All components defining a xonsh session."""
|
||||
|
||||
|
@ -530,6 +539,7 @@ class XonshSession:
|
|||
self.history = None
|
||||
self.shell = None
|
||||
self.env = None
|
||||
self.imp = InlineImporter()
|
||||
self.rc_files = None
|
||||
|
||||
# AST-invoked functions
|
||||
|
|
Loading…
Add table
Reference in a new issue