Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP

Loading…

Remove isinstance() check for namedtuple_as_object #22

Merged
merged 2 commits into from

2 participants

@singingwolfboy

Using isinstance() is not Pythonic and should be avoided when possible.
By using duck-typing instead, we can easily support other objects
that do not inherit from tuple but still support the same API as
namedtuple, such as objects created using the recordtype library:
http://pypi.python.org/pypi/recordtype

singingwolfboy added some commits
@singingwolfboy singingwolfboy Removed isinstance() check for namedtuple_as_object
Using isinstance() is not Pythonic and should be avoided when possible.
By using duck-typing instead, we can easily support other objects
that do not inherit from tuple but still support the same API as
namedtuple, such as objects created using the `recordtype` library:
http://pypi.python.org/pypi/recordtype
698e6c1
@singingwolfboy singingwolfboy Removed PyTuple_Check from _is_namedtuple() in _speedups.c edfa765
@etrepum
Owner

This seems like a reasonable thing to do, but I will need some tests and updates to docs (index.rst) to reflect the change. Do you want to do this? If not, I'll get around to it in the next few weeks.

@etrepum
Owner

Take a look at this branch, includes your changes and I added some docs and tests. Had a bit more time than I expected today. master...namedtuple_duck-gh22

@singingwolfboy

Looks good to me! Sorry it took me so long to get back to this. Do you need me to do anything to get this merged into master?

@etrepum etrepum merged commit edfa765 into simplejson:master
@jperkin jperkin referenced this pull request from a commit in joyent/pkgsrc
wiz Update to 2.5.2:
Version 2.5.2 released 2012-05-10

* Fix for regression introduced in 2.5.1
  simplejson/simplejson#35

Version 2.5.1 released 2012-05-10

* Support for use_decimal=True in environments that use Python
  sub-interpreters such as uWSGI
  simplejson/simplejson#34

Version 2.5.0 released 2012-03-29

* New item_sort_key option for encoder to allow fine grained control of sorted
  output

Version 2.4.0 released 2012-03-06

* New bigint_as_string option for encoder to trade JavaScript number precision
  issues for type issues.
  simplejson/simplejson#31

Version 2.3.3 released 2012-02-27

* Allow unknown numerical types for indent parameter
  simplejson/simplejson#29

Version 2.3.2 released 2011-12-30

* Fix crashing regression in speedups introduced in 2.3.1

Version 2.3.1 released 2011-12-29

* namedtuple_as_object now checks _asdict to ensure that it
  is callable.
  simplejson/simplejson#26

Version 2.3.0 released 2011-12-05

* Any objects with _asdict() methods are now considered for
  namedtuple_as_object.
  simplejson/simplejson#22
6bc94ea
@jsonn jsonn referenced this pull request from a commit in jsonn/pkgsrc
wiz Update to 2.5.2:
Version 2.5.2 released 2012-05-10

* Fix for regression introduced in 2.5.1
  simplejson/simplejson#35

Version 2.5.1 released 2012-05-10

* Support for use_decimal=True in environments that use Python
  sub-interpreters such as uWSGI
  simplejson/simplejson#34

Version 2.5.0 released 2012-03-29

* New item_sort_key option for encoder to allow fine grained control of sorted
  output

Version 2.4.0 released 2012-03-06

* New bigint_as_string option for encoder to trade JavaScript number precision
  issues for type issues.
  simplejson/simplejson#31

Version 2.3.3 released 2012-02-27

* Allow unknown numerical types for indent parameter
  simplejson/simplejson#29

Version 2.3.2 released 2011-12-30

* Fix crashing regression in speedups introduced in 2.3.1

Version 2.3.1 released 2011-12-29

* namedtuple_as_object now checks _asdict to ensure that it
  is callable.
  simplejson/simplejson#26

Version 2.3.0 released 2011-12-05

* Any objects with _asdict() methods are now considered for
  namedtuple_as_object.
  simplejson/simplejson#22
b0726e7
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Commits on Nov 11, 2011
  1. @singingwolfboy

    Removed isinstance() check for namedtuple_as_object

    singingwolfboy authored
    Using isinstance() is not Pythonic and should be avoided when possible.
    By using duck-typing instead, we can easily support other objects
    that do not inherit from tuple but still support the same API as
    namedtuple, such as objects created using the `recordtype` library:
    http://pypi.python.org/pypi/recordtype
  2. @singingwolfboy
This page is out of date. Refresh to see the latest.
Showing with 5 additions and 8 deletions.
  1. +1 −1  simplejson/_speedups.c
  2. +4 −7 simplejson/encoder.py
View
2  simplejson/_speedups.c
@@ -169,7 +169,7 @@ _is_namedtuple(PyObject *obj);
static int
_is_namedtuple(PyObject *obj)
{
- return PyTuple_Check(obj) && PyObject_HasAttrString(obj, "_asdict");
+ return PyObject_HasAttrString(obj, "_asdict");
}
static int
View
11 simplejson/encoder.py
@@ -155,7 +155,7 @@ def __init__(self, skipkeys=False, ensure_ascii=True,
be supported directly by the encoder. For the inverse, decode JSON
with ``parse_float=decimal.Decimal``.
- If namedtuple_as_object is true (the default), tuple subclasses with
+ If namedtuple_as_object is true (the default), objects with
``_asdict()`` methods will be encoded as JSON objects.
If tuple_as_array is true (the default), tuple (and subclasses) will
@@ -387,8 +387,7 @@ def _iterencode_list(lst, _current_indent_level):
yield buf
if isinstance(value, list):
chunks = _iterencode_list(value, _current_indent_level)
- elif (_namedtuple_as_object and isinstance(value, tuple) and
- hasattr(value, '_asdict')):
+ elif (_namedtuple_as_object and hasattr(value, '_asdict')):
chunks = _iterencode_dict(value._asdict(),
_current_indent_level)
elif _tuple_as_array and isinstance(value, tuple):
@@ -472,8 +471,7 @@ def _iterencode_dict(dct, _current_indent_level):
else:
if isinstance(value, list):
chunks = _iterencode_list(value, _current_indent_level)
- elif (_namedtuple_as_object and isinstance(value, tuple) and
- hasattr(value, '_asdict')):
+ elif (_namedtuple_as_object and hasattr(value, '_asdict')):
chunks = _iterencode_dict(value._asdict(),
_current_indent_level)
elif _tuple_as_array and isinstance(value, tuple):
@@ -507,8 +505,7 @@ def _iterencode(o, _current_indent_level):
elif isinstance(o, list):
for chunk in _iterencode_list(o, _current_indent_level):
yield chunk
- elif (_namedtuple_as_object and isinstance(o, tuple) and
- hasattr(o, '_asdict')):
+ elif (_namedtuple_as_object and hasattr(o, '_asdict')):
for chunk in _iterencode_dict(o._asdict(), _current_indent_level):
yield chunk
elif (_tuple_as_array and isinstance(o, tuple)):
Something went wrong with that request. Please try again.