General Utilities

Date Manipulation

gwrappy.utils.timestamp_to_datetime(input_timestamp, tz=None)[source]

Converts epoch timestamp into datetime object.

Parameters:
  • input_timestamp (long) – Epoch timestamp. Microsecond or millisecond inputs accepted.
  • tz – String representation of timezone accepted by pytz. eg. ‘Asia/Hong_Kong’. If param is unfilled, system timezone is used.
Returns:

timezone aware datetime object

gwrappy.utils.datetime_to_timestamp(input_datetime, date_format='%Y-%m-%d %H:%M:%S', tz=None)[source]

Converts datetime to epoch timestamp.

Note - If input_datetime is timestamp aware, it would first be localized according to the tz parameter if filled, or the system timezone if unfilled.

Parameters:
  • input_datetime (datetime object or string representation of datetime.) – Date to convert.
  • date_format – If input is string, denotes string datetime format to convert from.
  • tz – String representation of timezone accepted by pytz. eg. ‘Asia/Hong_Kong’. If param is unfilled, system timezone is used.
Returns:

timezone aware datetime object

gwrappy.utils.date_range(start, end, ascending=True, date_format='%Y-%m-%d')[source]

Simple datetime generator for dates between start and end (inclusive).

Parameters:
  • start (datetime object or string representation of datetime.) – Date to start at.
  • end (datetime object or string representation of datetime.) – Date to stop at.
  • ascending (boolean) – Toggle sorting of output.
  • date_format – If input is string, denotes string datetime format to convert from.
Returns:

generator object for naive datetime objects

gwrappy.utils.month_range(start, end, full_months=False, month_format='%Y-%m', ascending=True, date_format='%Y-%m-%d')[source]

Simple utility to chunk date range into months.

Parameters:
  • start – Date to start at.
  • end – Date to end at.
  • full_months – If true, only data up till the last complete month would be included.
  • month_format – Format for month key.
  • date_format – If start and end is string, denotes string datetime format to convert from.
  • ascending – Sort date list ascending
Returns:

dictionary keyed by month (in the format specified by month_format) with values being the list of dates within that month.

gwrappy.utils.simple_mail(send_to, subject, text, send_from=None, username=None, password=None, server='smtp.gmail.com', port=587)[source]

Simple utility mail function - only text messages without attachments.

Note - In Gmail you’d have to allow ‘less secure apps to access your account’. Not recommended for secure information/accounts.

Parameters:
  • send_to (list or string) – Email recipients
  • subject – Email Subject
  • text – Email Body
  • send_from – Name of sender
  • username – Login username
  • password – Login password
  • server – Mail server
  • port – Connection port
class gwrappy.utils.StringLogger(name=None, level=20, formatter=None, ignore_modules=None)[source]

Simple logging wrapper with a string buffer handler to easily write and retrieve logs as strings.

Parameters:
  • name – Name of logger
  • level – Logging level
  • formatter – logging.Formatter() object
  • ignore_modules – list of module names to ignore from logging process
get_logger()[source]

Return instantiated Logger.

Returns:logging.Logger object
get_log_string()[source]

Return logs as string.

Returns:logged data as string