What’s New in utilipy v1.0.X?¶
Bug fixes (in X releases) are annotated with @X
Overview¶
Adopted the Astropy-affiliate template.
In particular, this release includes:
Framework¶
Many of these do not have a lot of functions implemented, but the framework is in place as utilities are added.
configfor configurationconstantsfor astropy-style constants with frozen valuesdata_utilsfor data utility functions, like converting scipy.optimize functions to lmfit.decoratorsa repository of useful decoratorsimportfor *-style importsipythonfor IPython conveniences, like Markdown printing.mathfor math functionplotfor plot-related functionsscriptsfor scripts
Config¶
Configuration functions: check_config, write_config, get_import_verbosity, set_import_verbosity, get_warnings_verbosity, set_warnings_verbosity, get_frozen_constants, set_frozen_constants.
All the configurations can be get / set during run-time.
There is a with version of all the configurations, for running code with a temporarily changed configurations.
Constants¶
Astropy constants, with a frozen version for reproducibility.
float versions of the constants accessible through values module this includes frozen version for reproducibility to access frozen version, set frozen-constants=True in utilipy config.
FrozenConstantsfor frozen constantsConstantsValuesfor the values of constants.
data_utils¶
Data utilities.
idxDecoratorto control whether a fnction returns boolean arrays or indices.inRange: multidimensional box selection.outRange: multidimensional box exclusion.ioRange: multidimensional box selection and exclusion.ellipse: elliptical selection of data in many dimensions.circle: circular selection of data in many dimensions.
fitting¶
scipy_residual_to_lmfitdecorator to make scipy residual functions compatible with lmfit.
Decorators¶
Decorators are a powerful way to augment functions, and even classes. With a decorator we can alter the input or output of a function, and even edit the properties of a function. This discussion presupposes some familiarity with the use and construction of decorators.
The ultimate goal is a decorator that can edit the input and output of a function, will inherit the signature, annotations, and docstring of the decorated function, and can modify said docstring and signature.
These decorator can do:
anything to the function input and output
make a function that looks exactly like the input function
for quality introspection.
work when created with parenthesis
accept (kw)arguments on application
add any extra (kw)arguments to control the wrapper
also make the defaults be dynamically set on function creation.
document what the wrapper is doing.
In a way that is introspectable, by modifying both the signature and docstring.
1>>> from utilipy.utils import functools
2>>> def template_decorator(function=None, *, kw1=None, kw2=None):
3... '''Docstring for decorator.
4...
5... Description of this decorator
6...
7... Parameters
8... ----------
9... function : types.FunctionType or None, optional
10... the function to be decoratored
11... if None, then returns decorator to apply.
12... kw1, kw2 : any, optional
13... key-word only arguments
14... sets the wrappeer's default values.
15...
16... Returns
17... -------
18... wrapper : types.FunctionType
19... wrapper for function
20... does a few things
21... includes the original function in a method `.__wrapped__`
22...
23... '''
24... if function is None: # allowing for optional arguments
25... return functools.partial(template_decorator, kw1=k1, kw2=kw2)
26...
27... @functools.wraps(function)
28... def wrapper(*args, kw1=kw1, kw2=kw2, kw3='not in decorator factory', **kw):
29... """wrapper docstring.
30...
31... Decorator
32... ---------
33... prints information about function
34... kw1, kw2: defaults {kw1}, {kw2}
35...
36... """
37... # do stuff here
38... return_ = function(*args, **kw)
39... # and here
40... return return_
41... # /def
42...
43... return wrapper
44... # /def
45
46>>> @template_decorator
47... def function(x: '1st arg', y: '2nd arg', # arguments
48... a: '1st defarg'=10, b=11, # defaulted arguments
49... *args: 'args', # variable arguments
50... k: '1st kwonly'='one', l: '2nd kwonly'='two', # keyword-only arguments
51... **kw: 'kwargs' # variable keyword arguments
52... ) -> tuple:
53... '''Function for testing decoration.
54...
55... This function has all 5 different types of arguments:
56... 1) arguments, 2) defaulted arguments, 3) variable arguments,
57... 4) keyword-only arguments, 5) variable keyword arguments
58...
59... '''
60... return x, y, a, b, args, k, l, kw
61... # /def
62
63>>> help(function)
64
65Help on function function in module __main__:
66
67function(x: '1st arg', y: '2nd arg', a: '1st defarg' = 10, b=11, *args: 'args', k: '1st kwonly' = 'one', l: '2nd kwonly' = 'two', kw1=None, kw2=None, kw3='not in decorator factory', **kw: 'kwargs') -> tuple
68 function for testing decoration
69 This function has all 5 different types of arguments:
70 1) arguments, 2) defaulted arguments, 3) variable arguments,
71 4) keyword-only arguments, 5) variable keyword arguments
72
73 Decorator
74 ---------
75 prints information about function
76 kw1, kw2: defaults None, None
utilipy.decorators.baseclass¶
A set of baseclasses to make improved decorators. This module requires further testing.
utilipy.decorators.docstrings¶
astropy’s “format_doc”
utilipy.decorators.func_io¶
Function input / output.
function
store_function_inputto store all the input to a function as a BoundArgumentfunction
add_folder_backslashto add a backslash to directory path inputs.dtypeDecoratorMakerfunction to make a dtype decorator.dtypeDecoratorfunction to ensure arguments are type dtype.boolDecorator,intDecorator,floatDecorator,strDecorator,ndarrayDecorator,ndfloat64Decorator, which enforce their respective dtypes.
corrected import inspect to utilipy’s inspect (@v1.0.1)
Imports¶
This module provides a variety of files that can be *-imported to provide basic set of imports.
The quick imports are base_imports, extended_imports, astropy_imports, matplotlib_imports, galpy_imports and amuse_imports.
utilipy.imports.base¶
helper function base_imports_help
Base imports
os, sys, time, pdb, warnings,
numpy -> np, scipy,
tqdm -> TQDM, tqdm, .tqdm_notebook -> tqdmn
IPython imports
display, Latex, Markdown, set_trace,
printmd, printMD, printltx, printLaTeX,
set_autoreload, aimport,
run_imports, import_from_file,
add_raw_code_toggle
utilipy imports
LogFile
ObjDict
utilipy.imports.extended¶
helper function extended_imports_help
Numpy imports
linalg.norm
Scipy imports
stats.binned_statistic->binned_stats
utilipy.imports.matplotlib¶
helper function matplotlib_imports_help
Matplotlib imports
pyplot->plt
matplotlib->mpl, .cm, .colors
mpl_toolkits.mplot3d.Axes3D
utilipy imports
ipython.plot.configure_matplotlib
utilipy.imports.plotly¶
helper function plotly_imports_help
plotly imports
plotly
express -> px
graph_objs -> go
io -> pio
subplots -> make_subplots
utilipy.imports.astropy¶
helper function astropy_imports_help
Astropy imports
units->u,
coordinates->coords, SkyCoord,
table.Table, QTable
visualization.quantity_support, astropy_mpl_style
utilipy.imports.galpy¶
helper function galpy_imports_help
Galpy imports
potential, .MWPotential2014
galpy.orbit.Orbit
galpy.util: bovy_conversion, bovy_coords
utilipy.imports.amuse¶
helper function amuse_imports_help
imports
amuse,amuse.lab,amuse.units.units,amuse.units.constants,amuse.couple.bridgeprovides a help function,
amuse_imports_help
IPython¶
Functions for interacting with the IPython environment. If in the IPython, sets the ast_node_interactivity to “all” and configures matplotlib, via configure_matplotlib, to an inline backend and retina resolution.
loads into the top-level namespace:
help function
modules:
autoreload,imports,notebook,plot,printingfunctions:
set_autoreload,aimport,run_imports,import_from_file,add_raw_code_toggle,configure_matplotlib,printMD,printLTX
utilipy.ipython.autoreload¶
If in an IPython environment, sets the autoreload state to 1 (autoreload anything imported by aimport).
set_autoreloadfunction to change the global imports setting.aimportfor autoreloading individual modules
utilipy.ipython.imports¶
Module for running utilipy.imports in an IPython environment.
import_from_filefunction to run any import file, fromutilipyor a custom file.run_importsfunction to import a file using IPython magic. Usesimport_from_fileon custom files. Has built-in options for a set of basic imports (by keywordbase), extended imports (by keywordextended), astropy, matplotlib, plotly, galpy, and amuse import sets by the respective keywords.
utilipy.ipython.notebook¶
Functions for Jupyter notebook / lab / hub.
add_raw_code_togglefunction to show/hide code cells when Notebook is exported to HTML
utilipy.ipython.plot¶
configure_matplotlibfunction to control plotting in an IPython environment.
utilipy.ipython.printing¶
printMDfunction to print in Markdown.printLTXfunction to print in Latex.
Math¶
A place for math functions. Currently only quadrature.
Plot¶
nothing implemented yet. See What’s Planned for utilipy?.
Scripts¶
nothing implemented yet. See What’s Planned for utilipy?.
Utils¶
Utilities. The following are imported on instantiation.
1from .logging import LogPrint, LogFile
2from .collections import ObjDict
3
4from . import functools, pickle
5
6# import top level packages
7from . import (
8 collections,
9 doc_parse_tools,
10 logging,
11 metaclasses,
12)
utilipy.utils.exceptions¶
utilipyWarningutilipyWarningVerbose
utilipy.utils.functools¶
makeFunction: make a function from an existing code object.copy_function: Copy a function.update_wrapper: this overrides the defaultfunctoolsupdate_wrapperand adds signature and docstring overridingwraps: overrides the defaultfunctoolsupdate_wrapperand adds signature and docstring overriding
utilipy.utils.inspect¶
added FullerArgSpec which better separates parts of a signature, like arguments with and without defaults. Also a FullerSignature object which has much finer control over signatures and itself appears to have the signature of the function to which it is a signature.
POSITIONAL_ONLYPOSITIONAL_OR_KEYWORDVAR_POSITIONALKEYWORD_ONLYVAR_KEYWORD_void_empty_placehold_is_empty_is_void_is_placehold_is_placeholderFullerArgSpecgetfullerargspecget_annotations_from_signatureget_defaults_from_signatureget_kwdefaults_from_signatureget_kwonlydefaults_from_signatureget_kinds_from_signaturemodify_parameterreplace_with_parameterinsert_parameterprepend_parameterappend_parameterdrop_parameterFullerSignaturefuller_signature
utilipy.utils.pickle¶
dump and load many objects
utilipy.utils.string¶
FormatTemplatewith string supportingformat, syntax.
utilipy.utils.typing¶
array_like: typing.Sequence
utilipy.utils.logging¶
Basic loggers that can both print and/or record to a file.
LogPrint: print logger
LogFile: This class uses
open
utilipy.utils.doc_parse_tools¶
Docstring inheritance-style implementations. Supports numpy and google docstrings.
To implement your own inheritance file, simply write a function that fits the template
def your_style(prnt_doc, child_doc):
''' Merges parent and child docstrings
Parameters
----------
prnt_cls_doc: Optional[str]
child_doc: Optional[str]
Returns
------
Optional[str]
The merged docstring that will be utilized.'''
return final_docstring
and log this using custom_inherit.add_style(your_style).
To permanently save your function
define your function within
custom_inherit/_style_store.pylog it in
custom_inherit.style_store.__all__.
utilipy.utils.collections¶
ObjDict: Dictionary-like object intended to store information. Instantiated with a name (str)