pywind

pywind.export

Export functions for command line app.

pywind.export.export_to_file(args, obj)[source]

Export a pywind object to a file based on command line arguments. The object supplied can be exported as CSV, XLSX or XML depending on the args.format option supplied.

Parameters:
  • args – Command line args from argparse.parse_args()
  • obj – The pywind object to export.

pywind.log

PyWind now uses standard python logging :-)

pywind.log.setup_logging(debug=False, stdout=True, request_logging=False, filename=None)[source]

Setup the logging for pywind.

Parameters:
  • debug – Enable debug level messages (default False)
  • stdout – Enable logging to stdout (default True)
  • request_logging – Enable full logging of network requests (default False)
  • filename – Filename to use for log.

pywind.utils

Utility functions used by more than one module within pywind.

class pywind.utils.StdoutFormatter(*args, **kwargs)[source]

Bases: object

Small class to provide easier printing to stdout.

>>> from pywind.utils import StdoutFormatter
>>> sof = StdoutFormatter("5s", "6s", ">10s")
>>> sof.titles("Hello", "World", "right")
  Hello  World        right
  -----  ------  ----------
>>> sof.row("first", "row", "right")
  first  row          right

Note

The format detection doesn’t allow all options :-( “10,d” is not yet supported.

formatter(titles=False)[source]

Return the format string for the columns configured.

Parameters:titles – True if the format will be used for titles (all strings)
Returns:Format string
Return type:str
row(*args)[source]

Use the column format to generate a string. This tries to truncate long strings.

Parameters:args – The column values.
Returns:Formatted string using args
Return type:str
titles(*args)[source]

Generate the title string block.

Parameters:args – List of titles to use. Should be at least as long as the number of columns
Returns:Formatted title string
Return type:str
pywind.utils.args_get_datetime(args)[source]
pywind.utils.commandline_parser(help_text, epilog=None)[source]

Simple function to create a command line parser with some generic options.

Parameters:
  • help_text – The script description
  • epilog – Epilog text
Returns:

Argument parser

Return type:

argparse.ArgumentParser

pywind.utils.get_or_post_a_url(url, post=False, **kwargs)[source]

Use the requests library to either get or post to a specified URL. The return code is checked and exceptions raised if there has been a redirect or the status code is not 200.

Parameters:
  • url – The URL to be used.
  • post – True if the request should be a POST. Default is False which results in a GET request.
  • kwargs – Optional keyword arguments that are passed directly to the requests call.
Returns:

The requests object is returned if all checks pass.

Return type:

requests.Response

Raises:

Raises Exception for various errors.

Example

>>> from pywind.utils import get_or_post_a_url
>>> qry = {'q': 'rst document formatting'}
>>> response = get_or_post_a_url('http://www.google.com/search', params=qry)
>>> response.content
...
pywind.utils.map_xml_to_dict(xml_node, mapping=None)[source]

Given an XML node, create a dict using the mapping of attributes/elements supplied.

The format of each mapping item is a tuple of up to 3 components,
  • xml attribute
  • key for dict (optional)
  • type of data expected (optional)
  • default value for the mapping

If the key name is not supplied, the lower cased xml attribute will be used. If the type is not given it will be assumed to be a string.

Parameters:
  • xml_node – The XML node to parse
  • mapping – Iterable of xml element
Returns:

Dict of successfully extracted data

Return type:

dict

pywind.utils.multi_level_get(the_dict, key, default=None)[source]

Given the level of nested data contained in some of the results, this function performs an iterative get.

Parameters:
  • the_dict – The multi-level dict to get the key from.
  • key – The key to look for, with each level separated by ‘.’
  • default – The default to return if the key is not found. (None if not supplied)
Returns:

The value of the key or default if key does not exist in the dict.

pywind.utils.parse_response_as_xml(request)[source]

Given a the response object from requests, attempt to parse it’s contents as XML.

Parameters:request – The requests object
Returns:The root XML node or None if there is a parser error

Note

  • Nov 2014 Using parser with recover=True was the suggestion of energynumbers
pywind.utils.valid_date(dtstr)[source]

Parse a string into a date using the YYYY-MM-DD format. Used by the commandline_parser() function.

Parameters:dtstr – Date string to be parsed
Returns:Valid date
Return type:datetime.date
Raises:argparse.ArgumentTypeError if the date string is not formatted as YYYY-MM-DD
pywind.utils.valid_time(dtstr)[source]

Parse a string into a date using the YYYY-MM-DD format. Used by the commandline_parser() function.

Parameters:dtstr – Date string to be parsed
Returns:Valid date
Return type:datetime.date
Raises:argparse.ArgumentTypeError if the date string is not formatted as YYYY-MM-DD
pywind.utils.xml_attr_or_element(xml_node, name)[source]

Attempt to get the value of name from the xml_node. This could be an attribute or a child element.