diff --git a/news/inline_import.rst b/news/inline_import.rst new file mode 100644 index 000000000..89de8731c --- /dev/null +++ b/news/inline_import.rst @@ -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:** + +* + +**Deprecated:** + +* + +**Removed:** + +* + +**Fixed:** + +* + +**Security:** + +* diff --git a/xonsh/built_ins.py b/xonsh/built_ins.py index 4a9262a90..24c18b9d2 100644 --- a/xonsh/built_ins.py +++ b/xonsh/built_ins.py @@ -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