Common Package¶
Submodules¶
Mapper¶
-
class
seed.common.mapper.
MapItem
(key, item)¶ Bases:
object
Wrapper around a mapped item.
An object will be created with the following attributes:
- source => The source field from which we mapped
- field => The field to which we mapped
- is_bedes => flag, whether this field is BEDES-compliant
- is_numeric => whether the data is numeric (or string)
-
as_json
()¶
-
class
seed.common.mapper.
Mapping
(fileobj, encoding=None, regex=False, spc_or_underscore=True, ignore_case=True, normalize_units=True)¶ Bases:
object
Mapping from one set of fields to another. The mapping can be many:1. The lookup may be by static string or regular expression.
-
META_BEDES
= 'bedes'¶
-
META_NUMERIC
= 'numeric'¶
-
META_TYPE
= 'type'¶
-
apply
(keys)¶ Get value for a list of keys.
Parameters: keys – List of keys (strings) Returns: A pair of values. The first is a mapping {key: value} of the keys that matched. The second is a list [key, key, ..] of those that didn’t.
-
get
(key, default=None)¶ Wrapper around __getitem__ that will return the default instead of raising KeyError if the item is not found.
-
keys
()¶ Get list of source keys.
- Return:
- (list) Source keys
-
-
class
seed.common.mapper.
MappingConfiguration
¶ Bases:
object
Factory for creating Mapping objects from configurations.
-
pm
(version)¶ Get Portfolio Manager mapping for given version.
- Args:
- version (tuple): A list of integers/strings (major, minor, ..)
- Raises:
- ValueError, if no mapping is found
-
-
class
seed.common.mapper.
Programs
¶ Bases:
object
Enumeration of program names.
-
PM
= 'PortfolioManager'¶
-
-
seed.common.mapper.
get_pm_mapping
(version, columns, include_none=False)¶ Create and return Portfolio Manager (PM) mapping for a given version of PM and the given list of column names.
- Args:
- version (str): Version in format ‘x.y[.z]’ columns (list): A list of [column_name, field, {metadata}] include_none (bool): If True, add {column:None} for unmatched columns.
- Return:
- (dict) of {column:MapItem}, where column is one of the values in the input list. If include_none was True, then all columns should be in the output.
Util¶
-
seed.common.util.
apply_map
(map_path, data_path, out_file)¶ Apply a JSON mapping to data, and write the output.
- Args:
- map_path (str): Path to mapping file data_path (str): Path to data file out_file (file): output stream
- Return:
- None
-
seed.common.util.
create_map
(path_in, path_out)¶ Create a JSON mapping file, suitable for map.Mapping(), from a CSV input file in our own custom style.
Input columns: CurrentSEED,NewSEED,PM1,PM2,Type (ignore rest)
Parameters: - path_in –
- path_out –
Returns: None
-
seed.common.util.
find_duplicates
(map_path, data_path, out_file)¶ Find duplicates created by a given mapping on a given input file.
- Args:
- map_path (str): Path to mapping file data_path (str): Path to data file out_file (file): output stream
- Return:
- None
Views¶
-
seed.common.views.
api_error
(reason)¶ JSON response for API error.
-
seed.common.views.
api_success
(**kwargs)¶ JSON response for API success.
-
seed.common.views.
missing_request_keys
(keys, body)¶ Check for keys in body.
- Args:
- keys (list): List of keys to check body (dict): body of request
- Returns:
- None if all present, JSON error response (using api_error()) if one or more is missing.
-
seed.common.views.
typeof_request_values
(types, body)¶ Check for type of request values.
All keys in types are present in body.
Parameters: - types (dict) – key to type map, where type is a function used to test the type conversion. It should take one argument, the value, and raise a ValueError if it is invalid.
- body (dict) – body of request
Returns: JSON
Example:
None if all OK, JSON error response (using `api_error()`) if one or more is of the wrong type.