Allow for long's as well as ints for request params #207

Merged
devdave merged 4 commits from patch-1 into master 2013-05-29 08:35:49 -07:00
devdave commented 2013-05-28 16:35:43 -07:00 (Migrated from github.com)

_params['max_id']
330122291755220993L

type(_params['max_id'])
<type 'long'>

isinstance(_params['max_id'], int)
False

isinstance(_params['max_id'], (long,int))
True

_params['max_id'] 330122291755220993L type(_params['max_id']) <type 'long'> isinstance(_params['max_id'], int) False isinstance(_params['max_id'], (long,int)) True
michaelhelmick commented 2013-05-28 17:11:34 -07:00 (Migrated from github.com)

Actually, you'd want to change the compat.py file and update it like this:

import sys

_ver = sys.version_info

#: Python 2.x?
is_py2 = (_ver[0] == 2)

#: Python 3.x?
is_py3 = (_ver[0] == 3)

try:
    import simplejson as json
except ImportError:
    import json

if is_py2:
    from urllib import urlencode, quote_plus
    try:
        from urlparse import parse_qsl
    except ImportError:
        from cgi import parse_qsl

    str = unicode
    basestring = basestring
    numeric_types = (int, long, float)


elif is_py3:
    from urllib.parse import urlencode, quote_plus, parse_qsl

    str = str
    basestring = (str, bytes)
    numeric_types = (int, float)

and in helpers.py

from .compat import basestring, numeric_types


def _transparent_params(_params):
    params = {}
    files = {}
    for k, v in _params.items():
        if hasattr(v, 'read') and callable(v.read):
            files[k] = v
        elif isinstance(v, bool):
            if v:
                params[k] = 'true'
            else:
                params[k] = 'false'
        elif isinstance(v, basestring) or isinstance(v, numeric_types):
            params[k] = v
        else:
            continue
    return params, files

This is to keep Python 3 compatibility. You can either make the changes I've stated or if you wish that I make them.. let me know!

Actually, you'd want to change the `compat.py` file and update it like this: ``` python import sys _ver = sys.version_info #: Python 2.x? is_py2 = (_ver[0] == 2) #: Python 3.x? is_py3 = (_ver[0] == 3) try: import simplejson as json except ImportError: import json if is_py2: from urllib import urlencode, quote_plus try: from urlparse import parse_qsl except ImportError: from cgi import parse_qsl str = unicode basestring = basestring numeric_types = (int, long, float) elif is_py3: from urllib.parse import urlencode, quote_plus, parse_qsl str = str basestring = (str, bytes) numeric_types = (int, float) ``` and in `helpers.py` ``` python from .compat import basestring, numeric_types def _transparent_params(_params): params = {} files = {} for k, v in _params.items(): if hasattr(v, 'read') and callable(v.read): files[k] = v elif isinstance(v, bool): if v: params[k] = 'true' else: params[k] = 'false' elif isinstance(v, basestring) or isinstance(v, numeric_types): params[k] = v else: continue return params, files ``` This is to keep Python 3 compatibility. You can either make the changes I've stated or if you wish that I make them.. let me know!
devdave commented 2013-05-28 18:11:20 -07:00 (Migrated from github.com)

May need to drop this pull request/fork, didn't realize you had Travis CI covering Twython and made the commits out of order.

May need to drop this pull request/fork, didn't realize you had Travis CI covering Twython and made the commits out of order.
michaelhelmick commented 2013-05-28 18:12:35 -07:00 (Migrated from github.com)

@devdave It seems to not work for PR anyways because secure variables aren't available in PR's ... -___-
That being said, don't be discouraged if Travis says your build fails

@devdave It seems to not work for PR anyways because secure variables aren't available in PR's ... -___- That being said, don't be discouraged if Travis says your build fails
devdave commented 2013-05-28 18:21:30 -07:00 (Migrated from github.com)

Alright, numeric_types was added to .compat and implemented into helpers._transparent_params. Currently ransacking twitter with this via my "test in prod" integration system.

Alright, numeric_types was added to .compat and implemented into helpers._transparent_params. Currently ransacking twitter with this via my "test in prod" integration system.
michaelhelmick commented 2013-05-28 18:53:16 -07:00 (Migrated from github.com)

Cool, cool! Be sure to add yourself to AUTHORS.rst and I'll merge this in tomorrow morning! :)

Cool, cool! Be sure to add yourself to AUTHORS.rst and I'll merge this in tomorrow morning! :)
Sign in to join this conversation.
No reviewers
No milestone
No project
No assignees
1 participant
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Dependencies

No dependencies set.

Reference: code/twython#207
No description provided.