Skip to content

Commit

Permalink
Add python 3.7 support and fix deprecation warnings
Browse files Browse the repository at this point in the history
Close #11
Close #17
  • Loading branch information
Toilal committed Oct 12, 2018
1 parent 3590e3e commit 6ae3a65
Show file tree
Hide file tree
Showing 10 changed files with 77 additions and 31 deletions.
9 changes: 7 additions & 2 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,16 @@ python:
- 3.5
- 3.6
- pypy
matrix:
include:
- python: 3.7
dist: xenial
sudo: true
install:
- pip install pip --upgrade
- if [ $TRAVIS_PYTHON_VERSION != 2.6 ] && [ $TRAVIS_PYTHON_VERSION != 3.3 ]; then pip install pip --upgrade; fi
- pip install -e .[dev,test]
- pip install pytest --upgrade
- pip install coveralls
- if [ $TRAVIS_PYTHON_VERSION != 2.6 ] && [ $TRAVIS_PYTHON_VERSION != 3.3 ]; then pip install coveralls; fi
script:
- if [ $TRAVIS_PYTHON_VERSION != 2.6 ]; then pylint rebulk; fi
- coverage run --source=rebulk setup.py test
Expand Down
4 changes: 2 additions & 2 deletions README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ You can define several patterns with a single ``regex`` method call.

.. code-block:: python
>>> Rebulk().regex(r'Wint\wr', 'com\w{3}').matches("Winter is coming...")
>>> Rebulk().regex(r'Wint\wr', r'com\w{3}').matches("Winter is coming...")
[<Winter:(0, 6)>, <coming:(10, 16)>]
All keyword arguments from `re.compile`_ are supported.
Expand Down Expand Up @@ -136,7 +136,7 @@ If `regex module`_ is available, it automatically supports repeated captures.
Defined as a list of 2-tuple, each tuple is an abbreviation. It simply replace ``tuple[0]`` with ``tuple[1]`` in the
expression.

>>> Rebulk().regex(r'Custom-separators', abbreviations=[("-", "[\W_]+")])\
>>> Rebulk().regex(r'Custom-separators', abbreviations=[("-", r"[\W_]+")])\
... .matches("Custom_separators using-abbreviations")
[<Custom_separators:(0, 17)>]

Expand Down
3 changes: 2 additions & 1 deletion pylintrc
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,8 @@ disable=basestring-builtin,range-builtin-not-iterating,unichr-builtin,buffer-bui
map-builtin-not-iterating,old-ne-operator,long-builtin,intern-builtin,zip-builtin-not-iterating,reduce-builtin,
unicode-builtin,old-division,xrange-builtin,old-octal-literal,coerce-method,
too-few-public-methods,too-many-arguments,too-many-instance-attributes,bad-builtin,too-many-ancestors,
too-few-format-args,fixme,duplicate-code,deprecated-lambda,cyclic-import,
too-few-format-args,fixme,duplicate-code,deprecated-lambda,cyclic-import,useless-object-inheritance,
inconsistent-return-statements,
I

[REPORTS]
Expand Down
46 changes: 42 additions & 4 deletions rebulk/loose.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,18 @@
"""
Various utilities functions
"""
import inspect


import sys
import inspect

try:
from inspect import getfullargspec as getargspec
_fullargspec_supported = True
except ImportError:
_fullargspec_supported = False
from inspect import getargspec

from .utils import is_iterable

if sys.version_info < (3, 4, 0): # pragma: no cover
Expand Down Expand Up @@ -63,7 +73,7 @@ def function_args(callable_, *args, **kwargs):
:return: (args, kwargs) matching the function signature
:rtype: tuple
"""
argspec = inspect.getargspec(callable_) # pylint:disable=deprecated-method
argspec = getargspec(callable_) # pylint:disable=deprecated-method
return argspec_args(argspec, False, *args, **kwargs)


Expand All @@ -80,7 +90,7 @@ def constructor_args(class_, *args, **kwargs):
:return: (args, kwargs) matching the function signature
:rtype: tuple
"""
argspec = inspect.getargspec(_constructor(class_)) # pylint:disable=deprecated-method
argspec = getargspec(_constructor(class_)) # pylint:disable=deprecated-method
return argspec_args(argspec, True, *args, **kwargs)


Expand All @@ -99,7 +109,7 @@ def argspec_args(argspec, constructor, *args, **kwargs):
:return: (args, kwargs) matching the function signature
:rtype: tuple
"""
if argspec.keywords:
if argspec.varkw:
call_kwarg = kwargs
else:
call_kwarg = dict((k, kwargs[k]) for k in kwargs if k in argspec.args) # Python 2.6 dict comprehension
Expand All @@ -110,6 +120,34 @@ def argspec_args(argspec, constructor, *args, **kwargs):
return call_args, call_kwarg


if not _fullargspec_supported:
def argspec_args_legacy(argspec, constructor, *args, **kwargs):
"""
Return (args, kwargs) matching the argspec object
:param argspec: argspec to use
:type argspec: argspec
:param constructor: is it a constructor ?
:type constructor: bool
:param args:
:type args:
:param kwargs:
:type kwargs:
:return: (args, kwargs) matching the function signature
:rtype: tuple
"""
if argspec.keywords:
call_kwarg = kwargs
else:
call_kwarg = dict((k, kwargs[k]) for k in kwargs if k in argspec.args) # Python 2.6 dict comprehension
if argspec.varargs:
call_args = args
else:
call_args = args[:len(argspec.args) - (1 if constructor else 0)]
return call_args, call_kwarg
argspec_args = argspec_args_legacy


def ensure_list(param):
"""
Retrieves a list from given parameter.
Expand Down
10 changes: 7 additions & 3 deletions rebulk/match.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,11 @@
"""
import copy
import itertools
from collections import defaultdict, MutableSequence
from collections import defaultdict
try:
from collections.abc import MutableSequence
except ImportError:
from collections import MutableSequence

try:
from collections import OrderedDict # pylint:disable=ungrouped-imports
Expand Down Expand Up @@ -778,9 +782,9 @@ def crop(self, crops, predicate=None, index=None):
right.start = end
if right:
ret.append(right)
elif end <= current.end and end > current.start:
elif current.end >= end > current.start:
current.start = end
elif start >= current.start and start < current.end:
elif current.start <= start < current.end:
current.end = start
return filter_index(ret, predicate, index)

Expand Down
2 changes: 1 addition & 1 deletion rebulk/processors.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ def _default_conflict_solver(match, conflicting_match):
"""
if len(conflicting_match.initiator) < len(match.initiator):
return conflicting_match
elif len(match.initiator) < len(conflicting_match.initiator):
if len(match.initiator) < len(conflicting_match.initiator):
return match
return None

Expand Down
18 changes: 8 additions & 10 deletions rebulk/rules.py
Original file line number Diff line number Diff line change
Expand Up @@ -140,10 +140,9 @@ def then(self, matches, when_response, context):
matches.remove(match)
ret.append(match)
return ret
else:
if when_response in matches:
matches.remove(when_response)
return when_response
if when_response in matches:
matches.remove(when_response)
return when_response


class AppendMatch(Consequence): # pylint: disable=abstract-method
Expand All @@ -164,12 +163,11 @@ def then(self, matches, when_response, context):
matches.append(match)
ret.append(match)
return ret
else:
if self.match_name:
when_response.name = self.match_name
if when_response not in matches:
matches.append(when_response)
return when_response
if self.match_name:
when_response.name = self.match_name
if when_response not in matches:
matches.append(when_response)
return when_response


class RenameMatch(Consequence): # pylint: disable=abstract-method
Expand Down
5 changes: 4 additions & 1 deletion rebulk/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,10 @@
"""
Various utilities functions
"""
from collections import MutableSet
try:
from collections.abc import MutableSet
except ImportError:
from collections import MutableSet

from types import GeneratorType

Expand Down
4 changes: 3 additions & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@

setup_requires = ['pytest-runner']

dev_require = ['pytest>=2.7.3', 'pytest-capturelog', 'zest.releaser[recommended]', 'pylint', 'tox']
dev_require = ['pytest>=2.7.3', 'zest.releaser[recommended]', 'pylint', 'tox']

tests_require = ['pytest']

Expand All @@ -40,6 +40,8 @@
'Programming Language :: Python :: 3.3',
'Programming Language :: Python :: 3.4',
'Programming Language :: Python :: 3.5',
'Programming Language :: Python :: 3.6',
'Programming Language :: Python :: 3.7',
'Topic :: Software Development :: Libraries :: Python Modules'
],
keywords='re regexp regular expression search pattern string match',
Expand Down
7 changes: 1 addition & 6 deletions tox.ini
Original file line number Diff line number Diff line change
@@ -1,16 +1,11 @@
[tox]
envlist = py26,py27,py33,py34,py35,pypy
envlist = py26,py27,py33,py34,py35,py36,py37,pypy

[testenv:py26]
commands =
{envbindir}/pip install -e .[dev]
{envpython} setup.py test

[testenv:py35]
commands =
{envbindir}/pip install -e .[dev]
{envpython} setup.py test

[testenv]
commands =
{envbindir}/pip install -e .[dev]
Expand Down

0 comments on commit 6ae3a65

Please sign in to comment.