Skip to content

Commit ad7669c

Browse files
committed
Add some docstrings and improve some others
1 parent 2af8a00 commit ad7669c

File tree

6 files changed

+65
-7
lines changed

6 files changed

+65
-7
lines changed

flattentool/__init__.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ def create_template(schema, output_name='releases', output_format='all', main_sh
1414
Creates template file(s) from given inputs
1515
This function is built to deal with commandline input and arguments
1616
but to also be called from elswhere in future
17+
1718
"""
1819

1920
parser = SchemaParser(schema_filename=schema, main_sheet_name=main_sheet_name, rollup=rollup, root_id=root_id, use_titles=use_titles)
@@ -38,6 +39,11 @@ def spreadsheet_output(spreadsheet_output_class, name):
3839

3940

4041
def flatten(input_name, schema=None, output_name='releases', output_format='all', main_sheet_name='main', root_list_path='releases', rollup=False, root_id='ocid', use_titles=False, **_):
42+
"""
43+
Flatten a nested structure (JSON) to a flat structure (spreadsheet - csv or xlsx).
44+
45+
"""
46+
4147
if schema:
4248
schema_parser = SchemaParser(
4349
schema_filename=schema,
@@ -99,6 +105,10 @@ def decimal_default(o):
99105
def unflatten(input_name, base_json=None, input_format=None, output_name='releases.json',
100106
main_sheet_name='releases', encoding='utf8', timezone_name='UTC',
101107
root_id='ocid', schema='', convert_titles=False, **_):
108+
"""
109+
Unflatten a flat structure (spreadsheet - csv or xlsx) into a nested structure (JSON).
110+
111+
"""
102112
if input_format is None:
103113
raise Exception('You must specify an input format (may autodetect in future')
104114
elif input_format not in INPUT_FORMATS:

flattentool/cli.py

Lines changed: 29 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,28 @@
33
from flattentool import create_template, unflatten, flatten
44
from six import text_type
55

6+
"""
7+
This file does most of the work of the flatten-tool commandline command.
8+
9+
It takes any commandline arguments, and passes them to a function in
10+
``__init__.py``.
11+
12+
It is callable via the ``flatten-tool`` executable in the directory below, or
13+
using ``python -m flattentool.cli``.
14+
15+
"""
16+
617

718
def create_parser():
19+
"""
20+
Create an argparse ArgumentParser for our commandline arguments
21+
22+
Defaults are not set here, but rather given in the appropriate function.
23+
24+
(This is split out as it's own function primarily so it can be tested.)
25+
26+
"""
27+
828
parser = argparse.ArgumentParser()
929
subparsers = parser.add_subparsers(dest='subparser_name')
1030

@@ -109,15 +129,20 @@ def create_parser():
109129

110130

111131
def kwargs_from_parsed_args(args):
132+
"""
133+
Transforms argparse's parsed args object into a dictionary to be passed as
134+
kwargs.
135+
136+
"""
112137
return {k: v for k, v in vars(args).items() if v is not None}
113138

114139

115140
def main():
116141
"""
117-
Takes any command line arguments and then passes them onto
118-
create_template
119-
Defaults are not set here, but rather given in the create_template
120-
function incase that function is called from elsewhere in future.
142+
Use ``create_parser`` to get the commandline arguments, and pass them to
143+
the appropriate function in __init__.py (create_template, flatten or
144+
unflatten).
145+
121146
"""
122147
parser = create_parser()
123148
# Store the supplied arguments in args

flattentool/input.py

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,8 @@
1+
"""
2+
This file has classes describing input from spreadsheets.
3+
4+
"""
5+
16
from __future__ import print_function
27
from __future__ import unicode_literals
38
import sys
@@ -25,7 +30,17 @@
2530

2631

2732
class SpreadsheetInput(object):
33+
"""
34+
Base class describing a spreadsheet input. Has stubs which are
35+
implemented via inheritance for particular types of spreadsheet (e.g. xlsx
36+
or csv).
37+
38+
"""
2839
def convert_dict_titles(self, dicts, titles):
40+
"""
41+
Replace titles with field names in the given list of dictionaries (``dicts``) using the mapping in ``titles``.
42+
43+
"""
2944
titles = titles or {}
3045
titles_map = {title.replace(' ', '').lower(): title for title in titles}
3146
for d in dicts:

flattentool/json_input.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
"""
22
3-
This file contains code that takes an OCDS JSON release ifle as input (not a
3+
This file contains code that takes an OCDS JSON release file as input (not a
44
JSON schema, for that see schema.py).
55
66
"""

flattentool/output.py

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
1-
"""Code to output a parsed flattened JSON schema in various spreadsheet
2-
formats."""
1+
"""
2+
Code to output a parsed flattened JSON schema in various spreadsheet
3+
formats.
4+
5+
"""
36

47
import openpyxl
58
import csv

flattentool/sheet.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,9 @@
11
class Sheet(object):
2+
"""
3+
An abstract representation of a single sheet of a spreadsheet.
4+
5+
"""
6+
27
def __init__(self, columns=None, root_id='', name=None):
38
self.id_columns = []
49
self.columns = columns if columns else []

0 commit comments

Comments
 (0)