Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Book created from a dict discards ordering #219

Closed
craiga opened this issue Aug 18, 2020 · 1 comment · Fixed by #220
Closed

Book created from a dict discards ordering #219

craiga opened this issue Aug 18, 2020 · 1 comment · Fixed by #220

Comments

@craiga
Copy link
Contributor

craiga commented Aug 18, 2020

Since Python 3.7, dictionaries preserve order, so I'd expect the following code to pass:

book = pyexcel.Book(
    {
        "Stuff": [["some", "data", "I", "can't", "parse"]],
        "Records": [["record_id"], ["a"], ["b"], ["c"]],
        "Quarters": [
            ["id", "quarter"],
            ["one", "2021Q1"],
            ["two", "2021Q2"],
        ],
    }
)
assert list(book.sheet_names()) == ["Stuff", "Records", "Quarters"]

Unfortunately it does not.

E       AssertionError: assert ['Quarters', ...rds', 'Stuff'] == ['Stuff', 'Re...', 'Quarters']
E         At index 0 diff: 'Quarters' != 'Stuff'
E         Full diff:
E         - ['Stuff', 'Records', 'Quarters']
E         + ['Quarters', 'Records', 'Stuff']

I'd expect sheet_names to return the sheet names in the same order as the source dict.

@craiga
Copy link
Contributor Author

craiga commented Aug 18, 2020

Digging into this, it looks like load_from_sheet is explicitly checking for OrderedDict and re-ordering the keys if the dictionary isn't a subclass of OrderedDict.

I can work around this problem by casting my dictionary to an OrderedDict, but ideally I shouldn't have to, right?

craiga added a commit to craiga/pyexcel that referenced this issue Aug 18, 2020
craiga added a commit to craiga/pyexcel that referenced this issue Aug 18, 2020
craiga added a commit to craiga/pyexcel that referenced this issue Aug 18, 2020
@chfw chfw closed this as completed in #220 Aug 18, 2020
@chfw chfw mentioned this issue Aug 18, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

1 participant