pyowm.utils package

Submodules

pyowm.utils.temputils module

Module containing utility functions for temperature and wind units conversion

pyowm.utils.temputils.kelvin_dict_to(d, target_temperature_unit)[source]

Converts all the values in a dict from Kelvin temperatures to the specified temperature format.

Parameters:
  • d (dict) – the dictionary containing Kelvin temperature values
  • target_temperature_unit (str) – the target temperature unit, may be: ‘celsius’ or ‘fahrenheit’
Returns:

a dict with the same keys as the input dict and converted temperature values as values

Raises:

ValueError when unknown target temperature units are provided

pyowm.utils.temputils.kelvin_to_celsius(kelvintemp)[source]

Converts a numeric temperature from Kelvin degrees to Celsius degrees

Parameters:kelvintemp (int/long/float) – the Kelvin temperature
Returns:the float Celsius temperature
Raises:TypeError when bad argument types are provided
pyowm.utils.temputils.kelvin_to_fahrenheit(kelvintemp)[source]

Converts a numeric temperature from Kelvin degrees to Fahrenheit degrees

Parameters:kelvintemp (int/long/float) – the Kelvin temperature
Returns:the float Fahrenheit temperature
Raises:TypeError when bad argument types are provided
pyowm.utils.temputils.metric_wind_dict_to_imperial(d)[source]

Converts all the wind values in a dict from meters/sec (metric measurement system) to miles/hour (imperial measurement system) .

Parameters:d (dict) – the dictionary containing metric values
Returns:a dict with the same keys as the input dict and values converted to miles/hour

pyowm.utils.timeformatutils module

Module containing utility functions for time formats conversion

class pyowm.utils.timeformatutils.UTC[source]

Bases: datetime.tzinfo

dst(dt)[source]
tzname(dt)[source]
utcoffset(dt)[source]
pyowm.utils.timeformatutils.timeformat(timeobject, timeformat)[source]

Formats the specified time object to the target format type.

Parameters:
  • timeobject (int, datetime.datetime or ISO8601-formatted string with pattern YYYY-MM-DD HH:MM:SS+00) – the object conveying the time value
  • timeformat (str) – the target format for the time conversion. May be: ‘unix‘ (outputs an int UNIXtime), ‘date‘ (outputs a datetime.datetime object) or ‘iso‘ (outputs an ISO8601-formatted string with pattern YYYY-MM-DD HH:MM:SS+00)
Returns:

the formatted time

Raises:

ValueError when unknown timeformat switches are provided or when negative time values are provided

pyowm.utils.timeformatutils.to_ISO8601(timeobject)[source]

Returns the ISO8601-formatted string corresponding to the time value conveyed by the specified object, which can be either a UNIXtime, a datetime.datetime object or an ISO8601-formatted string in the format YYYY-MM-DD HH:MM:SS+00`.

Parameters:timeobject (int, datetime.datetime or ISO8601-formatted string) – the object conveying the time value
Returns:an ISO8601-formatted string with pattern YYYY-MM-DD HH:MM:SS+00`
Raises:TypeError when bad argument types are provided, ValueError when negative UNIXtimes are provided
pyowm.utils.timeformatutils.to_UNIXtime(timeobject)[source]

Returns the UNIXtime corresponding to the time value conveyed by the specified object, which can be either a UNIXtime, a datetime.datetime object or an ISO8601-formatted string in the format YYYY-MM-DD HH:MM:SS+00`.

Parameters:timeobject (int, datetime.datetime or ISO8601-formatted string) – the object conveying the time value
Returns:an int UNIXtime
Raises:TypeError when bad argument types are provided, ValueError when negative UNIXtimes are provided
pyowm.utils.timeformatutils.to_date(timeobject)[source]

Returns the datetime.datetime object corresponding to the time value conveyed by the specified object, which can be either a UNIXtime, a datetime.datetime object or an ISO8601-formatted string in the format YYYY-MM-DD HH:MM:SS+00`.

Parameters:timeobject (int, datetime.datetime or ISO8601-formatted string) – the object conveying the time value
Returns:a datetime.datetime object
Raises:TypeError when bad argument types are provided, ValueError when negative UNIXtimes are provided

pyowm.utils.timeutils module

Module containing utility functions for time values generation/management

pyowm.utils.timeutils.last_hour(date=None)[source]

Gives the datetime.datetime object corresponding to the last hour before now or before the specified datetime.datetime object.

Parameters:date (datetime.datetime object) – the date you want an hour to be subtracted from (if left None, the current date and time will be used)
Returns:a datetime.datetime object
pyowm.utils.timeutils.last_month(date=None)[source]

Gives the datetime.datetime object corresponding to the last month before now or before the specified datetime.datetime object. A month corresponds to 30 days.

Parameters:date (datetime.datetime object) – the date you want a month to be subtracted from (if left None, the current date and time will be used)
Returns:a datetime.datetime object
pyowm.utils.timeutils.last_three_hours(date=None)[source]

Gives the datetime.datetime object corresponding to last three hours before now or before the specified datetime.datetime object.

Parameters:date (datetime.datetime object) – the date you want three hours to be subtracted from (if left None, the current date and time will be used)
Returns:a datetime.datetime object
pyowm.utils.timeutils.last_week(date=None)[source]

Gives the datetime.datetime object corresponding to the last week before now or before the specified datetime.datetime object. A week corresponds to 7 days.

Parameters:date (datetime.datetime object) – the date you want a week to be subtracted from (if left None, the current date and time will be used)
Returns:a datetime.datetime object
pyowm.utils.timeutils.last_year(date=None)[source]

Gives the datetime.datetime object corresponding to the last year before now or before the specified datetime.datetime object. A year corresponds to 365 days.

Parameters:date (datetime.datetime object) – the date you want a year to be subtracted from (if left None, the current date and time will be used)
Returns:a datetime.datetime object
pyowm.utils.timeutils.next_hour(date=None)[source]

Gives the datetime.datetime object corresponding to the next hour from now or from the specified datetime.datetime object.

Parameters:date (datetime.datetime object) – the date you want an hour to be added (if left None, the current date and time will be used)
Returns:a datetime.datetime object
pyowm.utils.timeutils.next_month(date=None)[source]

Gives the datetime.datetime object corresponding to the next month after now or after the specified datetime.datetime object. A month corresponds to 30 days.

Parameters:date (datetime.datetime object) – the date you want a month to be added to (if left None, the current date and time will be used)
Returns:a datetime.datetime object
pyowm.utils.timeutils.next_three_hours(date=None)[source]

Gives the datetime.datetime object corresponding to the next three hours from now or from the specified datetime.datetime object.

Parameters:date (datetime.datetime object) – the date you want three hours to be added (if left None, the current date and time will be used)
Returns:a datetime.datetime object
pyowm.utils.timeutils.next_week(date=None)[source]

Gives the datetime.datetime object corresponding to the next week from now or from the specified datetime.datetime object. A week corresponds to 7 days.

Parameters:date (datetime.datetime object) – the date you want a week to be added (if left None, the current date and time will be used)
Returns:a datetime.datetime object
pyowm.utils.timeutils.next_year(date=None)[source]

Gives the datetime.datetime object corresponding to the next year after now or after the specified datetime.datetime object. A month corresponds to 30 days.

Parameters:date (datetime.datetime object) – the date you want a year to be added to (if left None, the current date and time will be used)
Returns:a datetime.datetime object
pyowm.utils.timeutils.now(timeformat='date')[source]

Returns the current time in the specified timeformat.

Parameters:timeformat (str) – the target format for the time conversion. May be: ‘date‘ (default - outputs a datetime.datetime object), ‘unix‘ (outputs a long UNIXtime) or ‘iso‘ (outputs an ISO8601-formatted string with pattern YYYY-MM-DD HH:MM:SS+00)
Returns:the current time value
Raises:ValueError when unknown timeformat switches are provided or when negative time values are provided
pyowm.utils.timeutils.tomorrow(hour=None, minute=None)[source]

Gives the datetime.datetime object corresponding to tomorrow. The default value for optional parameters is the current value of hour and minute. I.e: when called without specifying values for parameters, the resulting object will refer to the time = now + 24 hours; when called with only hour specified, the resulting object will refer to tomorrow at the specified hour and at the current minute.

Parameters:
  • hour (int) – the hour for tomorrow, in the format 0-23 (defaults to None)
  • minute (int) – the minute for tomorrow, in the format 0-59 (defaults to None)
Returns:

a datetime.datetime object

Raises:

ValueError when hour or minute have bad values

pyowm.utils.timeutils.yesterday(hour=None, minute=None)[source]

Gives the datetime.datetime object corresponding to yesterday. The default value for optional parameters is the current value of hour and minute. I.e: when called without specifying values for parameters, the resulting object will refer to the time = now - 24 hours; when called with only hour specified, the resulting object will refer to yesterday at the specified hour and at the current minute.

Parameters:
  • hour (int) – the hour for yesterday, in the format 0-23 (defaults to None)
  • minute (int) – the minute for yesterday, in the format 0-59 (defaults to None)
Returns:

a datetime.datetime object

Raises:

ValueError when hour or minute have bad values

pyowm.utils.xmlutils module

Module containing utility functions for generating XML strings

pyowm.utils.xmlutils.DOM_node_to_XML(tree, xml_declaration=True)[source]

Prints a DOM tree to its Unicode representation.

Parameters:
  • tree (an xml.etree.ElementTree.Element object) – the input DOM tree
  • xml_declaration (bool) – if True (default) prints a leading XML declaration line
Returns:

Unicode object

pyowm.utils.xmlutils.annotate_with_XMLNS(tree, prefix, URI)[source]

Annotates the provided DOM tree with XMLNS attributes and adds XMLNS prefixes to the tags of the tree nodes.

Parameters:
  • tree (an xml.etree.ElementTree.ElementTree or xml.etree.ElementTree.Element object) – the input DOM tree
  • prefix (str) – XMLNS prefix for tree nodes’ tags
  • URI (str) – the URI for the XMLNS definition file
pyowm.utils.xmlutils.create_DOM_node_from_dict(d, name, parent_node)[source]

Dumps dict data to an xml.etree.ElementTree.SubElement DOM subtree object and attaches it to the specified DOM parent node. The created subtree object is named after the specified name. If the supplied dict is None no DOM node is created for it as well as no DOM subnodes are generated for eventual None values found inside the dict

Parameters:
  • d (dict) – the input dictionary
  • name (str) – the name for the DOM subtree to be created
  • parent_node (xml.etree.ElementTree.Element or derivative objects) – the parent DOM node the newly created subtree must be attached to
Returns:

xml.etree.ElementTree.SubElementTree object

Module contents