| Conditions | 8 |
| Total Lines | 72 |
| Lines | 0 |
| Ratio | 0 % |
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
| 1 | import logging |
||
| 62 | @register_input |
||
| 63 | def read_large_json(filename, json_prefix='item', **kwargs): |
||
| 64 | # TODO: add the script to automatically find the json_prefix based on a key |
||
| 65 | # Also should still have the option to manually specify a prefix for complex |
||
| 66 | # json structures. |
||
| 67 | """Iterate over all items and sub-items in a json object that match the specified prefix |
||
| 68 | |||
| 69 | |||
| 70 | Parameters |
||
| 71 | ---------- |
||
| 72 | filename : str |
||
| 73 | The filename of the large json file |
||
| 74 | |||
| 75 | json_prefix : str |
||
| 76 | The string representation of the hierarchical prefix where the items of |
||
| 77 | interest may be located within the larger json object. |
||
| 78 | |||
| 79 | Try the following script if you need help determining the desired prefix: |
||
| 80 | $ import ijson |
||
| 81 | $ with open('test_data_large_json_2.json', 'r') as f: |
||
| 82 | $ parser = ijson.parse(f) |
||
| 83 | $ for prefix, event, value in parser: |
||
| 84 | $ print("prefix = '%r' || event = '%r' || value = '%r'" % |
||
| 85 | $ (prefix, event, value)) |
||
| 86 | |||
| 87 | |||
| 88 | Examples |
||
| 89 | -------- |
||
| 90 | >>> documents = read_large_json( |
||
| 91 | ... '{}/test_data_large_json.json'.format(test_data_path), |
||
| 92 | ... json_prefix='item._source.isAuthorOf') |
||
| 93 | >>> next(documents) == { |
||
| 94 | ... u'a': u'ScholarlyArticle', |
||
| 95 | ... u'name': u'Path planning and formation control via potential function for UAV Quadrotor', |
||
| 96 | ... u'author': [ |
||
| 97 | ... u'http://dig.isi.edu/autonomy/data/author/a.a.a.rizqi', |
||
| 98 | ... u'http://dig.isi.edu/autonomy/data/author/t.b.adji', |
||
| 99 | ... u'http://dig.isi.edu/autonomy/data/author/a.i.cahyadi'], |
||
| 100 | ... u'text': u"Potential-function-based control strategy for path planning and formation " + |
||
| 101 | ... u"control of Quadrotors is proposed in this work. The potential function is " + |
||
| 102 | ... u"used to attract the Quadrotor to the goal location as well as avoiding the " + |
||
| 103 | ... u"obstacle. The algorithm to solve the so called local minima problem by utilizing " + |
||
| 104 | ... u"the wall-following behavior is also explained. The resulted path planning via " + |
||
| 105 | ... u"potential function strategy is then used to design formation control algorithm. " + |
||
| 106 | ... u"Using the hybrid virtual leader and behavioral approach schema, the formation " + |
||
| 107 | ... u"control strategy by means of potential function is proposed. The overall strategy " + |
||
| 108 | ... u"has been successfully applied to the Quadrotor's model of Parrot AR Drone 2.0 in " + |
||
| 109 | ... u"Gazebo simulator programmed using Robot Operating System.\\nAuthor(s) Rizqi, A.A.A. " + |
||
| 110 | ... u"Dept. of Electr. Eng. & Inf. Technol., Univ. Gadjah Mada, Yogyakarta, Indonesia " + |
||
| 111 | ... u"Cahyadi, A.I. ; Adji, T.B.\\nReferenced Items are not available for this document.\\n" + |
||
| 112 | ... u"No versions found for this document.\\nStandards Dictionary Terms are available to " + |
||
| 113 | ... u"subscribers only.", |
||
| 114 | ... u'uri': u'http://dig.isi.edu/autonomy/data/article/6871517', |
||
| 115 | ... u'datePublished': u'2014', |
||
| 116 | ... 'filename': '{}/test_data_large_json.json'.format(test_data_path)} |
||
| 117 | True |
||
| 118 | """ |
||
| 119 | |||
| 120 | with open(filename, 'r') as handle: |
||
| 121 | for item in ijson.items(handle, json_prefix): |
||
| 122 | if hasattr(item, 'keys'): # check if item is a dictionary |
||
| 123 | item['filename'] = filename |
||
| 124 | yield item |
||
| 125 | # check if item is both iterable and not a string |
||
| 126 | elif __is_iterable(item) and not isinstance(item, str): |
||
| 127 | for sub_item in item: |
||
| 128 | # check if sub_item is a dictionary |
||
| 129 | if hasattr(sub_item, 'keys'): |
||
| 130 | sub_item['filename'] = filename |
||
| 131 | yield sub_item |
||
| 132 | else: |
||
| 133 | raise ValueError("'item' in json source is not a dict, and is either a string or not iterable: %r" % item) |
||
| 134 | |||
| 135 |
The coding style of this project requires that you add a docstring to this code element. Below, you find an example for methods:
If you would like to know more about docstrings, we recommend to read PEP-257: Docstring Conventions.