pycounter Internal APIs

pycounter.sushi5 module

COUNTER 5 SUSHI support.

pycounter.sushi5.get_sushi_stats_raw(wsdl_url=None, start_date=None, end_date=None, requestor_id=None, customer_reference=None, report='TR_J1', release=5, sushi_dump=False, verify=True, url=None, api_key=None, **kwargs)[source]

Get SUSHI stats for a given site in dict (decoded from JSON) format.

Parameters:
  • wsdl_url – (Deprecated; for backward compatibility with COUNTER 4 SUSHI code. Use url instead.) URL to API endpoint for this provider
  • start_date – start date for report (must be first day of a month)
  • end_date – end date for report (must be last day of a month)
  • requestor_id – requestor ID as defined by SUSHI protocol
  • customer_reference – customer reference number as defined by SUSHI protocol
  • report – report type, values defined by SUSHI protocol
  • release – COUNTER release (only 5 is supported in this module)
  • sushi_dump – produces dump of JSON to DEBUG logger
  • verify – bool: whether to verify SSL certificates
  • url – str: URL to endpoint for this provider
  • api_key – str: API key for SUSHI provider (not used by all vendors; see vendor instructions to determine if this is needed)
pycounter.sushi5.raw_to_full(raw_report)[source]

Convert a raw report to CounterReport.

Parameters:raw_report – raw report as dict decoded from JSON
Returns:a pycounter.report.CounterReport

pycounter.constants module

Constants used by pycounter.

pycounter.csvhelper module

Read CSV as unicode from both python 2 and 3 transparently.

class pycounter.csvhelper.UnicodeReader(filename, dialect=<class 'csv.excel'>, encoding='utf-8', fallback_encoding='latin-1', **kwargs)[source]

CSV reader that can handle unicode.

Must be used as a context manager:

with UnicodeReader(‘myfile.csv’) as reader:
pass # do things with reader
Parameters:
  • filename – path to file to open
  • dialect – a csv.Dialect instance or dialect name
  • encoding – text encoding of file
  • fallback_encoding – encoding to fall back to if default encoding fails; gives warning if it’s used.

All other parameters will be passed through to csv.reader()

class pycounter.csvhelper.UnicodeWriter(filename, dialect=<class 'csv.excel'>, encoding='utf-8', lineterminator='n', **kwargs)[source]

CSV writer that can handle unicode.

Must be used as a context manager:

with UnicodeWriter(‘myfile.csv’) as writer:
pass # do things with writer
Parameters:
  • filename – path to file to open
  • dialect – a csv.Dialect instance or dialect name
  • encoding – text encoding of file

All other parameters will be passed through to csv.writer()

writerow(row)[source]

Write a row to the output.

Parameters:row – list of cells to write to the file
writerows(rows)[source]

Write many rows to the output.

Parameters:rows – list of lists of cells to write

pycounter.helpers module

Helper functions used by pycounter.

pycounter.helpers.convert_covered(datestring)[source]

Convert coverage period string to datetimes.

Parameters:datestring – the string to convert to a date. Format as ‘YYYY-MM-DD to YYYY-MM-DD’
Returns:tuple of datetime.date instances

(Will also accept MM/DD/YYYY format, ISO 8601 timestamps, or existing datetime objects; these shouldn’t be in COUNTER reports, but they do show up in real world data…)

Also accepts strings of the form ‘Begin_Date=2019-01-01; End_Date=2019-12-31’ for better compatibility with some (broken) COUNTER 5 implementations.

pycounter.helpers.convert_date_column(datestring)[source]

Convert human-readable month to date of first day of month.

Parameters:datestring – the string to convert to a date. Format like “Jan-2014”.
Returns:datetime.date
pycounter.helpers.convert_date_run(datestring)[source]

Convert a date of the format ‘YYYY-MM-DD’ to a datetime.date object.

(Will also accept MM/DD/YYYY format, ISO 8601 timestamps, or existing datetime objects; these shouldn’t be in COUNTER reports, but they do show up in real world data…)

Parameters:datestring – the string to convert to a date.
Returns:datetime.date object
pycounter.helpers.format_stat(stat)[source]

Turn numbers possibly with embedded commas into integers.

Also accepts existing ints, which may be pre-converted from Excel.

Parameters:stat – numeric value, possibly with commas
Returns:int
pycounter.helpers.guess_type_from_content(file_obj)[source]

Guess type of a spreadsheet-like file.

Defaults to assuming it’s CSV, if it doesn’t appear to be XLSX or TSV.

Parameters:file_obj – file-like object of which to determine type.
Returns:string, one of “xlsx”, “tsv”, “csv”
pycounter.helpers.is_first_last(period)[source]
Args:
period: a tuple of datetime.date objects
Returns: bool, whether the period starts on the 1st of a month and ends on
the last of a month
pycounter.helpers.last_day(orig_date)[source]

Find last day of a month from any day in the month.

Parameters:orig_date – the date within the month for which we want the last day as datetime.date
Returns:datetime.date of last day of the month
pycounter.helpers.next_month(dateobj)[source]

Find the first day of the next month after the given date.

Parameters:dateobj – the date within the month for which we want the next month’s first day as datetime.date
Returns:datetime.date of the first day of the next month
pycounter.helpers.prev_month(dateobj)[source]

Find the first day of the previous month before the given date.

Parameters:dateobj – the date within the month for which we want the previous month’s first day as datetime.date.
Returns:datetime.date of first day of the previous month.