glide.utils module¶
Common utilities
-
class
glide.utils.
DateTimeWindowAction
(option_strings, dest, nargs=None, const=None, default=None, type=None, choices=None, required=False, help=None, metavar=None)[source]¶ Bases:
argparse.Action
An argparse Action for handling datetime window CLI args
-
class
glide.utils.
DateWindowAction
(option_strings, dest, nargs=None, const=None, default=None, type=None, choices=None, required=False, help=None, metavar=None)[source]¶ Bases:
argparse.Action
An argparse Action for handling date window CLI args
-
glide.utils.
cancel_asyncio_tasks
(tasks, loop, cancel_timeout=None)[source]¶ Cancel a set of asyncio tasks
- Parameters
tasks (iterable) – An iterable of asyncio tasks to cancel
loop – asyncio Event Loop
cancel_timeout (int or float, optional) – A timeout to use when waiting for tasks to finish cancelling
-
glide.utils.
divide_data
(data, n)[source]¶ Divide data into n chunks, with special handling for pandas objects
-
glide.utils.
find_class_in_dict
(cls, d, include=None, exclude=None)[source]¶ Get a list of keys that are an instance of a class in a dict
-
glide.utils.
get_class_list_docstring
(heading, classes)[source]¶ Helper to generate a part of a module docstring from a list of classes
-
glide.utils.
get_datetime_windows
(start_date, end_date, window_size_hours=None, num_windows=None, reverse=False, add_second=True)[source]¶ Produce a list of start/end date tuples
- Parameters
start_date (date, datetime, or str) – The absolute start date of the range
end_date (date, datetime, or str) – The absolute end date of the range
window_size_hours (float, optional) – The size of the windows in hours. May be a float to represent partial hours.
num_windows (int, optional) – The number of windows to split the date range into. One of num_windows or window_size_hours must be specified.
reverse (bool, optional) – If true return the windows in reverse order
add_second (bool, optional) – If true, offset the start of each window to be one second past the end date of the previous window.
- Returns
dt_windows – A list of tuples of start / end datetime pairs
- Return type
list
-
glide.utils.
get_file_handle
(*args, **kwargs)[source]¶ Context manager pass through to open_filepath_or_buffer. This will automatically close the file if and only if it was opened here. If file handles are passed in it is assumed the caller will manage them.
-
glide.utils.
get_or_create_event_loop
()[source]¶ Get an existing asyncio Event Loop or create one if necessary
-
glide.utils.
iterize
(o)[source]¶ Automatically wrap certain objects that you would not normally process item by item.
TODO: this function should probaly be improved/generalized.
-
glide.utils.
join
(tables, on=None, how='left', rsuffixes=None)[source]¶ Join a list of iterables or DataFrames
-
glide.utils.
load_ini_config
(filename, key=None)[source]¶ Load a config from an ini file, optionally extracting a key
-
glide.utils.
load_json_config
(filename, key=None)[source]¶ Load a config from a json file, optionally extracting a key
-
glide.utils.
load_yaml_config
(filename, key=None)[source]¶ Load a config from a yaml file, optionally extracting a key
-
glide.utils.
open_filepath_or_buffer
(f, open_flags='r', compression=None, is_text=True)[source]¶ Use pandas IO functions to return a handle from a filepath or buffer.
- Parameters
f (str or buffer) – filepath or buffer to open
open_flags (str, optional) – mode to open file
compression (str, optional) – compression arg passed to pandas functions
is_text (bool) – Whether file/buffer is in text format, Passed through to pandas helpers.
- Returns
f (file-like) – A file-like object
handles (list of file-like) – A list of file-like objects opened. Seems mostly relevant for zipped archives.
close (bool) – A flag indicating whether the caller should close the file object when done
-
glide.utils.
read_excel
(f, **kwargs)[source]¶ Read data from an Excel file using pyexcel
- Parameters
f (str or buffer) – Excel file to read from
**kwargs – Keyword arguments passed to pyexcel
-
glide.utils.
save_excel
(f, data, **kwargs)[source]¶ Write data to an Excel file using pyexcel
Note
If f is a file that ends in .xls, pyexcel_xls will be used, otherwise it defaults to pyexcel_xlsx.
- Parameters
f (str or buffer) – Excel file to write to
data (dict) – Data to write to the file. This is expected to be a dict of {sheet_name: sheet_data} format.
**kwargs – Keyword arguments passed to pyexcel’s save_data
-
glide.utils.
size
(o, default=None)[source]¶ Helper to return the len() of an object if it is available