Utils#
Utility module for Subgrounds
- subgrounds.utils.merge(data1: list[T], data2: list[T]) list[T]#
- subgrounds.utils.merge(data1: dict[str, T], data2: dict[str, T]) dict[str, T]
Merges
data1anddata2and returns the combined result.data1anddata2must be of the same type. Either both aredictorlist.>>> a = {'a': 1, 'b': {'c': 2, 'd': 3}} >>> b = {'b': {'e': 4}, 'f': 5} >>> merge(a, b) {'a': 1, 'b': {'c': 2, 'd': 3, 'e': 4}, 'f': 5}
- class subgrounds.utils.Sentinel#
This class purely used for 'Sentinel' type values such as default arguments to functions (where None is a significant value) or as a default return (such as str.find, where -1 is returned if a substring isn't found).
- subgrounds.utils.flatten_dict(data, keys=[])#
Takes a dictionary containing key-value pairs where all values are of type other than list and flattens it such that all key-value pairs in nested dictionaries are now at depth 1.
- subgrounds.utils.contains_list(data)#
Returns True if data contains a value of type list in its nested data and False otherwise
- subgrounds.utils.coroutine_generator(func)#
This defines a coroutine styled generator.
- All this does is start the generator via a next call allowing you to use .send
immediately instead of needing to use gen.send(None) or next first.
Inspired from: http://www.dabeaz.com/coroutines/Coroutines.pdf (p. 27)
Essentially, the logic is as follows: >>> def start(*args, **kwargs): ... gen = func(*args, **kwargs) ... next(gen) ... return gen
- subgrounds.utils.default_header(url)#
Contains the default header information for requests made by subgrounds
- Inserts the Playgrounds API Key iff:
a) targetting the Playgrounds API AND b) if the PLAYGROUNDS_API_KEY environment variable is set
- subgrounds.utils.user_agent()#
A basic user agent describing the following details:
"Subgrounds" (and version)
Platform/OS (and architecture)
Python Type (and version)
Examples: - Subgrounds/1.1.2 (Darwin; arm64) CPython/3.11.2 - Subgrounds/1.1.2 (Emscripten; wasm32) CPython/3.10.2
- ⚠️ To override this, construct your
Subgroundswith a headers parameter with a dictionary containing an empty "User-Agent" key-value pairing.
Pandas DataFrame utility module containing functions related to the formatting of GraphQL JSON data into DataFrames.
- class subgrounds.dataframe_utils.DataFrameColumns(key, fpaths)#
Helper class that holds data related to the shape of a DataFrame
- combine(other)#
Returns new DataFrameColumns containing the union of
selfandother's columns- Parameters:
other (Self) -- Columns to be combined to
self- Returns:
New
Selfcontaining the union ofselfandother- Return type:
Self
- mk_df(data, path_map)#
Formats the JSON data
datainto a DataFrame containing the columns defined inself.
- subgrounds.dataframe_utils.columns_of_selections(selections)#
Generates a list of DataFrame columns specifications based on a list of
Selectiontrees.- Parameters:
selections (list[subgrounds.query.Selection]) -- The selection trees
- Returns:
The list of DataFrame columns specifications
- Return type:
- subgrounds.dataframe_utils.df_of_json(json_data, fpaths, columns=None, concat=False)#
Formats the JSON data
json_datainto Pandas DataFrames, flattening the data in the process.Depending on the request's fieldpaths, either one or multiple dataframes will be returned based on how flattenable the response data is.
fpathsis a list ofFieldPathobjects corresponding to the set of fieldpaths that were used to get the response datajson_data.columnsis an optional argument used to rename the dataframes(s) columns. The length ofcolumnsmust be the same as the number of columns of all returned dataframes.concatindicates whether or not the resulting dataframes should be concatenated together. The dataframes must have the same number of columns, as well as the same column names (which can be set using thecolumnsargument).- Parameters:
fpaths (list[subgrounds.subgraph.fieldpath.FieldPath]) -- Fieldpaths that yielded the response data
columns (Optional[list[str]]) -- Column names. Defaults to None.
concat (bool) -- Flag indicating whether or not to concatenate the resulting dataframes, if there are more than one. Defaults to False.
- Returns:
The resulting dataframe(s)
- Return type:
pandas.core.frame.DataFrame | list[pandas.core.frame.DataFrame]