Skip to content

Commit

Permalink
Replace OrderedDict with plain dict #295
Browse files Browse the repository at this point in the history
dict is ordered by default in Python 3.6+

Signed-off-by: Philippe Ombredanne <[email protected]>
  • Loading branch information
pombredanne committed Dec 4, 2020
1 parent 186641b commit 2517e26
Show file tree
Hide file tree
Showing 70 changed files with 234 additions and 304 deletions.
3 changes: 1 addition & 2 deletions etc/scripts/genurlrules.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@
# Visit https://github.com/nexB/scancode-toolkit/ for support and download.


from collections import OrderedDict
import os

import click
Expand Down Expand Up @@ -113,7 +112,7 @@ def combine_expressions(expressions, relation='AND', licensing=Licensing()):
type(expressions)))

# Remove duplicate element in the expressions list
expressions = OrderedDict((x, True) for x in expressions).keys()
expressions = dict((x, True) for x in expressions).keys()

if len(expressions) == 1:
return expressions[0]
Expand Down
3 changes: 1 addition & 2 deletions etc/scripts/scancli.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@
# specific language governing permissions and limitations under the License.


from collections import OrderedDict
import json
from os.path import abspath
from os.path import dirname
Expand Down Expand Up @@ -59,7 +58,7 @@ def scan(locations, deserialize=False, scancode_root_dir=None):
channel.send(scan_kwargs) # execute func-call remotely
results = channel.receive()
if deserialize:
results = json.loads(results, object_pairs_hook=OrderedDict)
results = json.loads(results)
yield results


Expand Down
27 changes: 13 additions & 14 deletions etc/scripts/sch2js/sch2js.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,8 +39,7 @@
"""

if __name__ == '__main__':
from collections import OrderedDict


from six import iteritems

from schematics.types.base import BaseType
Expand Down Expand Up @@ -76,7 +75,7 @@ def jsonschema_for_single_field(field_instance):
"""
Return a mapping for the schema of a single field.
"""
field_schema = OrderedDict()
field_schema = {}

field_schema['type'] = SCHEMATIC_TYPE_TO_JSON_TYPE.get(
field_instance.__class__.__name__, 'string')
Expand All @@ -97,7 +96,7 @@ def jsonschema_for_fields(model):
"""
Return a mapping for the schema of a collection of fields.
"""
properties = OrderedDict()
properties = {}
required = []
for field_name, field_instance in iteritems(model.fields):
serialized_name = getattr(field_instance, 'serialized_name', None) or field_name
Expand All @@ -109,15 +108,15 @@ def jsonschema_for_fields(model):
try:
node = jsonschema_for_model(field_instance.model_class, 'array')
if hasattr(field_instance, 'metadata'):
_node = OrderedDict()
_node = {}
_node['type'] = node.pop('type')
_node['title'] = field_instance.metadata.get('label', '')
_node['description'] = field_instance.metadata.get('description', '')
_node.update(node)
node = _node
except AttributeError:
field_schema = jsonschema_for_single_field(field_instance.field)
node = OrderedDict()
node = {}
node['type'] = 'array'
if hasattr(field_instance, 'metadata'):
node['title'] = field_instance.metadata.get('label', '')
Expand Down Expand Up @@ -156,23 +155,23 @@ def jsonschema_for_model(model, _type='object'):
schema_title = ''
schema_description = ''

schema = OrderedDict([
('type', 'object'),
('title', schema_title),
('description', schema_description),
])
schema = {
'type': 'object',
'title': schema_title,
'description':schema_description,
}

if required:
schema['required'] = required

if hasattr(model, '_schema_order'):
ordered_properties = [(i, properties.pop(i)) for i in model._schema_order]
schema['properties'] = OrderedDict(ordered_properties)
schema['properties'] = dict(ordered_properties)
else:
schema['properties'] = properties

if _type == 'array':
schema = OrderedDict([
schema = dict([
('type', 'array'),
('items', schema),
])
Expand All @@ -185,7 +184,7 @@ def to_jsonschema(model, **kwargs):
Return a a JSON schema mapping for the `model` class.
"""
schema_id = kwargs.pop('schema_id', '')
jsonschema = OrderedDict([
jsonschema = dict([
('$schema', 'http://json-schema.org/draft-04/schema#'),
('id', schema_id)
])
Expand Down
9 changes: 4 additions & 5 deletions etc/scripts/synclic.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@
# Visit https://github.com/nexB/scancode-toolkit/ for support and download.


from collections import OrderedDict
import io
import json
import os
Expand Down Expand Up @@ -316,7 +315,7 @@ def get_response(url, headers, params):
status = response.status_code
if status != requests.codes.ok: # NOQA
raise Exception('Failed HTTP request for %(url)r: %(status)r' % locals())
return response.json(object_pairs_hook=OrderedDict)
return response.json(object_pairs_hook=dict)


def clean_text(text):
Expand Down Expand Up @@ -381,7 +380,7 @@ def fetch_licenses(self, scancode_licenses, from_repo=SPDX_DEFAULT_REPO):
# Skip the old plus licenses. We use them in
# ScanCode, but they are deprecated in SPDX.
continue
details = json.loads(archive.read(path), object_pairs_hook=OrderedDict)
details = json.loads(archive.read(path))
lic = self.build_license(details, scancode_licenses)
if lic:
yield lic
Expand Down Expand Up @@ -613,7 +612,7 @@ def call_deja_api(api_url, api_key, paginate=0, headers=None, params=None):
params = params or {}

def _get_results(response):
return response.json(object_pairs_hook=OrderedDict)
return response.json(object_pairs_hook=dict)

if paginate:
assert isinstance(paginate, int)
Expand Down Expand Up @@ -760,7 +759,7 @@ def get_owner(api_url, api_key, name):

def license_to_dict(lico):
"""
Return an OrderedDict of license data with texts for API calls.
Return an dict of license data with texts for API calls.
Fields with empty values are not included.
"""
licm = dict(
Expand Down
3 changes: 1 addition & 2 deletions src/cluecode/plugin_copyright.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@
# Visit https://github.com/nexB/scancode-toolkit/ for support and download.


from collections import OrderedDict

import attr

Expand All @@ -39,7 +38,7 @@ class CopyrightScanner(ScanPlugin):
Scan a Resource for copyrights.
"""

resource_attributes = OrderedDict([
resource_attributes = dict([
('copyrights',attr.ib(default=attr.Factory(list))),
('holders',attr.ib(default=attr.Factory(list))),
('authors',attr.ib(default=attr.Factory(list))),
Expand Down
23 changes: 11 additions & 12 deletions src/formattedcode/output_csv.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@
# Visit https://github.com/nexB/scancode-toolkit/ for support and download.


from collections import OrderedDict

import saneyaml
from six import string_types
Expand Down Expand Up @@ -81,7 +80,7 @@ def write_csv(results, output_file):
# FIXMe: this is reading all in memory
results = list(results)

headers = OrderedDict([
headers = dict([
('info', []),
('license_expression', []),
('license', []),
Expand Down Expand Up @@ -132,7 +131,7 @@ def collect_keys(mapping, key_group):

errors = scanned_file.pop('scan_errors', [])

file_info = OrderedDict(Resource=path)
file_info = dict(Resource=path)
file_info.update(((k, v) for k, v in scanned_file.items()
# FIXME: info are NOT lists: lists are the actual scans
if not isinstance(v, (list, dict))))
Expand All @@ -143,12 +142,12 @@ def collect_keys(mapping, key_group):
yield file_info

for lic_exp in scanned_file.get('license_expressions', []):
inf = OrderedDict(Resource=path, license_expression=lic_exp)
inf = dict(Resource=path, license_expression=lic_exp)
collect_keys(inf, 'license_expression')
yield inf

for licensing in scanned_file.get('licenses', []):
lic = OrderedDict(Resource=path)
lic = dict(Resource=path)
for k, val in licensing.items():
# do not include matched text for now.
if k == 'matched_text':
Expand Down Expand Up @@ -178,37 +177,37 @@ def collect_keys(mapping, key_group):
yield lic

for copyr in scanned_file.get('copyrights', []):
inf = OrderedDict(Resource=path)
inf = dict(Resource=path)
inf['copyright'] = copyr['value']
inf['start_line'] = copyr['start_line']
inf['end_line'] = copyr['start_line']
collect_keys(inf, 'copyright')
yield inf

for copyr in scanned_file.get('holders', []):
inf = OrderedDict(Resource=path)
inf = dict(Resource=path)
inf['copyright_holder'] = copyr['value']
inf['start_line'] = copyr['start_line']
inf['end_line'] = copyr['start_line']
collect_keys(inf, 'copyright')
yield inf

for copyr in scanned_file.get('authors', []):
inf = OrderedDict(Resource=path)
inf = dict(Resource=path)
inf['author'] = copyr['value']
inf['start_line'] = copyr['start_line']
inf['end_line'] = copyr['start_line']
collect_keys(inf, 'copyright')
yield inf

for email in scanned_file.get('emails', []):
email_info = OrderedDict(Resource=path)
email_info = dict(Resource=path)
email_info.update(email)
collect_keys(email_info, 'email')
yield email_info

for url in scanned_file.get('urls', []):
url_info = OrderedDict(Resource=path)
url_info = dict(Resource=path)
url_info.update(url)
collect_keys(url_info, 'url')
yield url_info
Expand All @@ -227,7 +226,7 @@ def pretty(data):
if not data:
return None
seqtypes = list, tuple
maptypes = OrderedDict, dict
maptypes = dict, dict
coltypes = seqtypes + maptypes
if isinstance(data, seqtypes):
if len(data) == 1 and isinstance(data[0], string_types):
Expand Down Expand Up @@ -281,7 +280,7 @@ def flatten_package(_package, path, prefix='package__'):

package_columns = get_package_columns()

pack = OrderedDict(Resource=path)
pack = dict(Resource=path)
for k, val in _package.items():
if k not in package_columns:
continue
Expand Down
13 changes: 6 additions & 7 deletions src/formattedcode/output_html.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@
# Visit https://github.com/nexB/scancode-toolkit/ for support and download.


from collections import OrderedDict
import io
from operator import itemgetter
from os.path import abspath
Expand Down Expand Up @@ -162,9 +161,9 @@ def generate_output(results, version, template):

from licensedcode.cache import get_licenses_db

converted = OrderedDict()
converted_infos = OrderedDict()
converted_packages = OrderedDict()
converted = {}
converted_infos = {}
converted_packages = {}
licenses = {}

LICENSES = 'licenses'
Expand All @@ -175,7 +174,7 @@ def generate_output(results, version, template):

# Create a flattened data dict keyed by path
for scanned_file in results:
scanned_file = OrderedDict(scanned_file)
scanned_file = dict(scanned_file)
path = scanned_file['path']
results = []
if COPYRIGHTS in scanned_file:
Expand Down Expand Up @@ -210,7 +209,7 @@ def generate_output(results, version, template):
# should rather just pass a the list of files from the scan
# results and let the template handle this rather than
# denormalizing the list here??
converted_infos[path] = OrderedDict()
converted_infos[path] = {}
for name, value in scanned_file.items():
if name in (LICENSES, PACKAGES, COPYRIGHTS, EMAILS, URLS):
continue
Expand All @@ -219,7 +218,7 @@ def generate_output(results, version, template):
if PACKAGES in scanned_file:
converted_packages[path] = scanned_file[PACKAGES]

licenses = OrderedDict(sorted(licenses.items()))
licenses = dict(sorted(licenses.items()))

files = {
'license_copyright': converted,
Expand Down
3 changes: 1 addition & 2 deletions src/formattedcode/output_json.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@
# Visit https://github.com/nexB/scancode-toolkit/ for support and download.


from collections import OrderedDict

import jsonstreams
from six import string_types
Expand Down Expand Up @@ -144,7 +143,7 @@ def get_results(codebase, as_list=False, **kwargs):
"""

codebase.add_files_count_to_current_header()
results = OrderedDict([('headers', codebase.get_headers()), ])
results = dict([('headers', codebase.get_headers()), ])

# add codebase toplevel attributes such as summaries
if codebase.attributes:
Expand Down
3 changes: 1 addition & 2 deletions src/formattedcode/output_jsonlines.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@
# Visit https://github.com/nexB/scancode-toolkit/ for support and download.


from collections import OrderedDict

import simplejson

Expand Down Expand Up @@ -60,7 +59,7 @@ def process_codebase(self, codebase, output_json_lines, **kwargs):

codebase.add_files_count_to_current_header()

headers = OrderedDict(headers=codebase.get_headers())
headers = dict(headers=codebase.get_headers())

simplejson_kwargs = dict(
iterable_as_array=True,
Expand Down
7 changes: 3 additions & 4 deletions src/licensedcode/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@

from collections import Counter
from collections import defaultdict
from collections import OrderedDict
from functools import partial
from itertools import chain
import io
Expand Down Expand Up @@ -218,7 +217,7 @@ def text(self):

def to_dict(self):
"""
Return an OrderedDict of license data (excluding texts).
Return an dict of license data (excluding texts).
Fields with empty values are not included.
"""

Expand All @@ -238,7 +237,7 @@ def dict_fields(attr, value):
return False
return True

data = attr.asdict(self, filter=dict_fields, dict_factory=OrderedDict)
data = attr.asdict(self, filter=dict_fields, dict_factory=dict)
cv = data.get('minimum_coverage')
if cv and isinstance(cv, float) and int(cv) == cv:
cv = int(cv)
Expand Down Expand Up @@ -934,7 +933,7 @@ def to_dict(self):
Return an ordered mapping of self, excluding texts. Used for
serialization. Empty values are not included.
"""
data = OrderedDict()
data = {}
if self.license_expression:
data['license_expression'] = self.license_expression

Expand Down
Loading

0 comments on commit 2517e26

Please sign in to comment.