Skip to content

Commit

Permalink
Removed OrderedDict usage
Browse files Browse the repository at this point in the history
Plain dicts are ordered since Python 3.6.
  • Loading branch information
claudep committed Feb 24, 2024
1 parent 9402ffe commit 2b88338
Show file tree
Hide file tree
Showing 4 changed files with 11 additions and 24 deletions.
25 changes: 7 additions & 18 deletions src/tablib/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@
:license: MIT, see LICENSE for more details.
"""

from collections import OrderedDict
from copy import copy
from operator import itemgetter

Expand Down Expand Up @@ -265,17 +264,12 @@ def _validate(self, row=None, col=None, safety=False):
raise InvalidDimensions
return False

def _package(self, dicts=True, ordered=True):
def _package(self, dicts=True):
"""Packages Dataset into lists of dictionaries for transmission."""
# TODO: Dicts default to false?

_data = list(self._data)

if ordered:
dict_pack = OrderedDict
else:
dict_pack = dict

# Execute formatters
if self._formatters:
for row_i, row in enumerate(_data):
Expand All @@ -291,7 +285,7 @@ def _package(self, dicts=True, ordered=True):

if self.headers:
if dicts:
data = [dict_pack(list(zip(self.headers, data_row))) for data_row in _data]
data = [dict(list(zip(self.headers, data_row))) for data_row in _data]
else:
data = [list(self.headers)] + list(_data)
else:
Expand Down Expand Up @@ -883,20 +877,15 @@ def add_sheet(self, dataset):
else:
raise InvalidDatasetType

def _package(self, ordered=True):
def _package(self):
"""Packages :class:`Databook` for delivery."""
collector = []

if ordered:
dict_pack = OrderedDict
else:
dict_pack = dict

for dset in self._datasets:
collector.append(dict_pack(
title=dset.title,
data=dset._package(ordered=ordered)
))
collector.append({
'title': dset.title,
'data': dset._package()
})
return collector

@property
Expand Down
3 changes: 1 addition & 2 deletions src/tablib/formats/__init__.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
""" Tablib - formats
"""
from collections import OrderedDict
from functools import partialmethod
from importlib import import_module
from importlib.util import find_spec
Expand Down Expand Up @@ -65,7 +64,7 @@ def __set__(self, obj, val):


class Registry:
_formats = OrderedDict()
_formats = {}

def register(self, key, format_or_path):
from ..core import Databook, Dataset
Expand Down
4 changes: 2 additions & 2 deletions src/tablib/formats/_yaml.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,14 +14,14 @@ class YAMLFormat:
def export_set(cls, dataset):
"""Returns YAML representation of Dataset."""
return yaml.safe_dump(
dataset._package(ordered=False), default_flow_style=None, allow_unicode=True
dataset._package(), default_flow_style=None, allow_unicode=True
)

@classmethod
def export_book(cls, databook):
"""Returns YAML representation of Databook."""
return yaml.safe_dump(
databook._package(ordered=False), default_flow_style=None, allow_unicode=True
databook._package(), default_flow_style=None, allow_unicode=True
)

@classmethod
Expand Down
3 changes: 1 addition & 2 deletions tests/test_tablib.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
import pickle
import tempfile
import unittest
from collections import OrderedDict
from decimal import Decimal
from io import BytesIO, StringIO
from pathlib import Path
Expand Down Expand Up @@ -1211,7 +1210,7 @@ def test_xls_import_with_errors(self):
data = tablib.Dataset().load(fh.read())
self.assertEqual(
data.dict[0],
OrderedDict([
dict([
('div by 0', '#DIV/0!'),
('name unknown', '#NAME?'),
('not available (formula)', '#N/A'),
Expand Down

0 comments on commit 2b88338

Please sign in to comment.