flowmachine.utils¶
Source: flowmachine/utils.py
Various simple utilities.
Enum Statistic¶
Statistic
Valid statistics for use with postgres. Convert a column name to a stat by doing f"{statistic:column_name}". For columns defined as a variable, do f"{statistic:'{column_name_variable}'}"
Members¶
-
COUNT:count -
SUM:sum -
AVG:avg -
MAX:max -
MIN:min -
MEDIAN:median -
STDDEV:stddev -
VARIANCE:variance -
MODE:mode
convert_dict_keys_to_strings¶
convert_dict_keys_to_strings(input_dict)
Return a copy of the input dictionary where all keys are converted to strings. This is necessary if the dictionary needs to be valid JSON.
Parameters¶
-
input_dict:dictThe input dictionary.
Returns¶
-
dictA copy of
input_dictwith all keys converted to strings.
get_dist_query_string¶
get_dist_query_string(*, lon1, lat1, lon2, lat2)
function for getting the distance query string between two lon-lat points.
get_name_and_alias¶
get_name_and_alias(column_name: str) -> Tuple[str]
Given a column name string, return the column name and alias (if there is one), or return the provided column name twice if there is no alias.
Returns¶
typing.Tuple[str]
Examples¶
get_name_and_alias("col AS alias")
('col', 'alias')
get_name_and_alias("col")
('col', 'col')
get_name_and_alias("table.col")
('table.col', 'col')
get_name_and_alias("table.col as alias")
('table.col', 'alias')
list_of_dates¶
list_of_dates(start, stop)
Parameters¶
-
start,stop:stryyyy-mm-dd format datestrings.
Returns¶
listofyyyy-mm-dd format datestrings between start and stop (inclusive)
make_where¶
make_where(condition) -> str
Create an sql where clause from a list of conditions.
Parameters¶
-
condition:listofstr,strWhere conditions to combine
Returns¶
-
strA where clause or empty string if all clauses are empty
Examples¶
make_where("")
""
make_where("NOT outgoing")
"WHERE NOT outgoing"
make_where(["NOT outgoing", "duration > 5")
"WHERE NOT outgoing AND duration > 5"
parse_datestring¶
parse_datestring(datestring: Union[str, datetime.datetime, datetime.date]) -> datetime.datetime
Parameters¶
-
datestring:typing.Union[str, datetime.datetime, datetime.date]ISO string date, or a datetime or date object
Returns¶
datetime.datetime
pretty_sql¶
pretty_sql(
sql,
compact_lists_margin=0,
split_string_literals_threshold=0,
special_functions=True,
comma_at_eoln=True,
)
Prettify and validate the syntax of an SQL query, using pglast
Parameters¶
-
sql:strSQL to prettyify and validate
-
compact_lists_margin:int, default0Use compact form for lists shorter than this
-
split_string_literals_threshold:int, default0Split strings (in the sql) longer than this threshold
-
special_functions:bool, defaultTrueTranslate some special functions to their more commonly used forms
-
comma_at_eoln
Returns¶
-
strThe prettified string.
proj4string¶
proj4string(conn, crs=None)
Provide a proj4 string for the input, or by default return the wsg84 proj4 string.
Parameters¶
-
conn:ConnectionFlowMachine db connection to use to get the proj4 string
-
crs:str,intAn integer EPSG code, or a proj4 string
Returns¶
-
strProj4 string for the input crs.
sort_recursively¶
sort_recursively(d)
Helper function to sort a dictionary recursively (including any dicts or sequences inside it).
Parameters¶
-
d:dictInput dictionary (can be nested).
Returns¶
-
dictThe sorted dictionary.
standardise_date¶
standardise_date(date: Union[str, datetime.date, datetime.datetime]) -> Union[str, NoneType]
Turn a date, datetime, or date string into a standardised date string (YYYY-MM-DD HH:MM:SS).
Parameters¶
-
date:typing.Union[str, datetime.date, datetime.datetime]Date-like-thing to standardise.
Returns¶
-
typing.Union[str, NoneType]YYYY-MM-DD HH:MM:SS formatted date string or None if input was None.
standardise_date_to_datetime¶
standardise_date_to_datetime(date: Union[str, datetime.date, datetime.datetime]) -> Union[datetime.datetime, NoneType]
As standardise_date but returns a datetime object or None if input was None Parameters ---------- date : str, date or datetime Date-like-thing to standardise.
Returns¶
typing.Union[datetime.datetime, NoneType]
time_period_add¶
time_period_add(date, n, unit="days")
Adds n days to the date (represented as a string). Or alternatively add hours or minutes to the date.
Parameters¶
-
date:strDate to add to in yyyy-mm-dd format
-
n:intNumber of units to add
-
unit:str, default'days'Type of unit to add to the date
Returns¶
-
strAltered date string
Examples¶
time_period_add('2016-01-01', 3)
'2016-01-04 00:00:00'
to_nested_list¶
to_nested_list(d)
Helper function to recursively convert an input dictionary to a nested list.
Parameters¶
-
d:dictInput dictionary (can be nested).
Returns¶
listoftuples