Skip to content

Commit

Permalink
Merge pull request #129 from alexandrevicenzi/dict_keys
Browse files Browse the repository at this point in the history
Python 3 compatible force_list and force_tuple
  • Loading branch information
alexandrevicenzi authored Oct 8, 2019
2 parents f48f281 + 9263bba commit 9339743
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 4 deletions.
17 changes: 13 additions & 4 deletions kobo/shortcuts.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,11 @@
from six.moves import range
from six.moves import shlex_quote

if six.PY3:
from collections.abc import KeysView
else:
from collections import KeysView

__all__ = (
"force_list",
"force_tuple",
Expand Down Expand Up @@ -50,10 +55,12 @@ def force_list(value):
@rtype: list
"""
if type(value) is list:
if isinstance(value, list):
return value
if type(value) in (tuple, set):

if isinstance(value, (tuple, set, KeysView)):
return list(value)

return [value]


Expand All @@ -62,10 +69,12 @@ def force_tuple(value):
@rtype: tuple
"""
if type(value) is tuple:
if isinstance(value, tuple):
return value
if type(value) in (list, set):

if isinstance(value, (list, set, KeysView)):
return tuple(value)

return (value, )


Expand Down
2 changes: 2 additions & 0 deletions tests/test_shortcuts.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,14 @@ def test_force_list(self):
self.assertEqual(force_list("a"), ["a"])
self.assertEqual(force_list(["a"]), ["a"])
self.assertEqual(force_list(["a", "b"]), ["a", "b"])
self.assertItemsEqual(force_list({'a': True, 'b': True}.keys()), ["a", "b"])
self.assertItemsEqual(force_list(set(["a", "b"])), ["a", "b"])

def test_force_tuple(self):
self.assertItemsEqual(force_tuple("a"), ("a",))
self.assertItemsEqual(force_tuple(("a",)), ("a",))
self.assertItemsEqual(force_tuple(("a", "b")), ("a", "b"))
self.assertItemsEqual(force_tuple({'a': True, 'b': True}.keys()), ("a", "b"))
self.assertItemsEqual(force_tuple(set(["a", "b"])), ("a", "b"))

def test_allof(self):
Expand Down

0 comments on commit 9339743

Please sign in to comment.