IPython Module Tutorial

This notebook introduces the utilipy.ipython.imports module

Functions:

The main function of this module is run_imports, which combines the functionality of both import_from_file as well as offers a variety of prepared import options, accessible by keyword, or from an external file by providing the file path. The details are provided below.

run_imports()

This function imports prepared files of imports using ipython magic. These files come in two varieties: custom files or files included in utilipy.

for custom files, see import_from_file. As just a simple reference guide, there are two arguments *files, which are the filepaths for the import files, and relative which specifies (by either a boolean or a boolean list of the same length as *files) whether the filepaths are relative or absolute paths.

Prepared Import Files

These can be imported by keyword=True

Keyword

Function

Description

base

base_imports()

a set of standard imports Base: os, sys, time, pdb, warnings, \(\qquad\) numpy -> np, scipy, \(\qquad\) tqdm_notebook -> tqdm Logging: .LogFileMisc: ObjDict IPython: display, Latex, Markdown, set_trace, \(\qquad\) printmd, printMD, printltx, printLaTeX, \(\qquad\) configure_matplotlib, \(\qquad\) set_autoreload, aimport, \(\qquad\) run_imports, import_from_file

extended

extended_imports()

some less used standard imports

matplotlib

matplotlib_imports()

matplotlib imports

astropy

astropy_imports()

astropy related imports

galpy

galpy_imports()

galpy related imports

if both astropy and matplotlib are imported here, then run_imports sets the matplotlib style to astropy_mpl_style, made by astropy.

import_from_file()

Custom Import Files

Making your own import file is trivial since it conforms exactly to the standard python import paradigm. Since the imports have been specified in a file separate from the main script, this is equivalent to doing from custom_imports import *, where custom_imports.py is the file of imports.

Since import * does not provide information about what is being imported, it is usefule to include this information in a print statement in the import file. An example custom_imports.py file, conforming to the standard file format adopted in utilipy, is shown below.

When importing from a custom file with run_imports or import_from_file, it is important to specify whether the file path is absolute or relative to the current jupyter notebook. This can be controlled using the relative keyword.

Example custom_imports.py file:

#!/usr/bin/env python
# -*- coding: utf-8 -*-

# ----------------------------------------------------------------------------
#
# TITLE   :
#
# ----------------------------------------------------------------------------

### Docstring and Metadata
"""
"""

__author__ = ""

##############################################################################
### IMPORTS

# +---------------------------------------------------------------------------+
# First Set of Imports
import this
from that import theother


# +---------------------------------------------------------------------------+
# Second Set of Imports
import lorem
from ipsum import dolor

##############################################################################
### Printing Information

print("""custom_imports:
    __information about custom imports here__
""")

##############################################################################
### END

Examples

First we import the imports module from utilipy.ipython.
For most cases this is unnecessary as both run_imports and import_from_file are accessible directly from utilipy.ipython.
[1]:
from utilipy.ipython import imports

run_imports()

[17]:
imports.run_imports(base=True)
/Users/nathanielstarkman/src/utilipy/ipython/../imports/base_imports.py
base_imports:
    Base: os, sys, time, pdb, warnings,
          numpy -> np, scipy,
          tqdm_notebook -> tqdm
    Logging: .LogFile
    Misc: ObjDict
    IPython: display, Latex, Markdown, set_trace,
             printmd, printMD, printltx, printLaTeX,
             configure_matplotlib,
             set_autoreload, aimport,
             run_imports, import_from_file,

testing numpy has actually been imported

[18]:
if 'np' in locals():
    print('success loading numpy')
success loading numpy

as an example of the extended imports

[23]:
imports.run_imports('../utilipy/imports/extend_imports.py',
                    relative='False')
../utilipy/imports/extend_imports.py
Imported:
numpy: linalg.norm
scipy stats.binned_statistic->binned_stats


END