From f4f609e1dd00918897d90e6863456e7074c1d8aa Mon Sep 17 00:00:00 2001 From: Hugo van Kemenade Date: Sat, 28 Aug 2021 15:24:06 +0300 Subject: [PATCH 1/7] Drop support for EOL Python 2.7 --- .coveragerc | 1 - .github/workflows/test-changes.yml | 2 +- .travis.yml | 1 - appveyor.yml | 4 ---- docs/contributing.rst | 6 +++--- docs/intro.rst | 2 +- requirements-formats.txt | 3 +-- requirements-remote.txt | 10 +++++----- setup.py | 4 +--- tox.ini | 8 +++----- 10 files changed, 15 insertions(+), 26 deletions(-) diff --git a/.coveragerc b/.coveragerc index a7b8a6b7..23e8aa05 100644 --- a/.coveragerc +++ b/.coveragerc @@ -1,4 +1,3 @@ [report] exclude_lines = pragma: no cover - pragma: ${PY_MAJOR_VERSION} no cover diff --git a/.github/workflows/test-changes.yml b/.github/workflows/test-changes.yml index 74e58832..fec23c0a 100644 --- a/.github/workflows/test-changes.yml +++ b/.github/workflows/test-changes.yml @@ -27,7 +27,7 @@ jobs: fail-fast: false matrix: os: [ "ubuntu-latest", "windows-latest", "macos-latest" ] - python: [2.7, 3.6, 3.7, 3.8, 3.9] + python: [3.6, 3.7, 3.8, 3.9] runs-on: "${{ matrix.os }}" diff --git a/.travis.yml b/.travis.yml index 20d0fba7..22250c2b 100644 --- a/.travis.yml +++ b/.travis.yml @@ -9,7 +9,6 @@ jobs: - python: 3.7 - python: 3.8 - python: 3.9 - - python: 2.7 # - python: pypy3 before_install: - sudo apt-get -qq update diff --git a/appveyor.yml b/appveyor.yml index f24e3ebb..75253291 100644 --- a/appveyor.yml +++ b/appveyor.yml @@ -12,10 +12,6 @@ environment: matrix: - - PYTHON: "C:\\Python27-x64" - PYTHON_VERSION: "2.7" - DISTUTILS_USE_SDK: "1" - - PYTHON: "C:\\Python36-x64" PYTHON_VERSION: "3.6" diff --git a/docs/contributing.rst b/docs/contributing.rst index 0d891f8d..17234908 100644 --- a/docs/contributing.rst +++ b/docs/contributing.rst @@ -29,7 +29,7 @@ suite with:: $ pip install nose $ nosetests -v -Currently :mod:`petl` supports Python 2.7, 3.4, 3.5 and 3.6 +Currently :mod:`petl` supports Python 3.6+ so the tests should pass under all these Python versions. Dependencies @@ -85,12 +85,12 @@ Automatically running all tests All of the above tests can be run automatically using `tox `_. You will need binaries for Python -2.7 and 3.6 available on your system. +3.6 available on your system. To run all tests **without** installing any of the optional dependencies, do:: - $ tox -e py27,py36,docs + $ tox -e py36,docs To run the entire test suite, including installation of **all** optional dependencies, do:: diff --git a/docs/intro.rst b/docs/intro.rst index 7abeb03a..863571b8 100644 --- a/docs/intro.rst +++ b/docs/intro.rst @@ -21,7 +21,7 @@ To verify the installation, the test suite can be run with `nose $ pip install nose $ nosetests -v petl -:mod:`petl` has been tested with Python versions 2.7 and 3.4-3.6 +:mod:`petl` has been tested with Python versions 3.6+ under Linux and Windows operating systems. .. _intro_dependencies: diff --git a/requirements-formats.txt b/requirements-formats.txt index 68a5ff5e..809324b9 100644 --- a/requirements-formats.txt +++ b/requirements-formats.txt @@ -9,5 +9,4 @@ tables Whoosh==2.7.4 xlrd==1.2.0 xlwt==1.3.0 -fastavro>=0.24.2 ; python_version >= '3.4' -fastavro==0.24.2 ; python_version < '3.0' +fastavro>=0.24.2 diff --git a/requirements-remote.txt b/requirements-remote.txt index adbd7fca..3de42b6d 100644 --- a/requirements-remote.txt +++ b/requirements-remote.txt @@ -1,9 +1,9 @@ # packages for testing remote sources -fastavro>=0.24.2 ; python_version >= '3.4' +fastavro>=0.24.2 smbprotocol>=1.0.1 paramiko>=2.7.1 -requests; python_version >= '3.4' -fsspec>=0.7.4 ; python_version >= '3.4' -aiohttp>=3.6.2 ; python_version >= '3.5.3' -s3fs>=0.2.2 ; python_version >= '3.4' +requests +fsspec>=0.7.4 +aiohttp>=3.6.2 +s3fs>=0.2.2 diff --git a/setup.py b/setup.py index db1bb3cb..3ee2f078 100644 --- a/setup.py +++ b/setup.py @@ -14,7 +14,7 @@ description='A Python package for extracting, transforming and loading ' 'tables of data.', long_description=open('README.txt').read(), - python_requires='>=2.7, !=3.0.*, !=3.1.*, !=3.2.*, !=3.3.*', + python_requires='>=3.6', setup_requires=["setuptools>18.0", "setuptools-scm>1.5.4"], extras_require={ 'avro': ['fastavro>=0.24.0'], @@ -40,8 +40,6 @@ }, classifiers=['Intended Audience :: Developers', 'License :: OSI Approved :: MIT License', - 'Programming Language :: Python :: 2', - 'Programming Language :: Python :: 2.7', 'Programming Language :: Python :: 3', 'Programming Language :: Python :: 3.6', 'Programming Language :: Python :: 3.7', diff --git a/tox.ini b/tox.ini index fb9915af..1b35b696 100644 --- a/tox.ini +++ b/tox.ini @@ -4,7 +4,7 @@ # and then run "tox" from this directory. [tox] -envlist = py27, py36, py37, py38, py39, {py36,py37,py38}-docs +envlist = py36, py37, py38, py39, {py36,py37,py38}-docs # trick to enable pre-installation of numpy and numexpr indexserver = preinstall1 = https://pypi.org/simple @@ -14,10 +14,8 @@ indexserver = # get stable output for unordered types setenv = PYTHONHASHSEED = 42 - py27: PY_MAJOR_VERSION = py2 - py36,py37,py38,py39: PY_MAJOR_VERSION = py3 commands = - py27,py36,py38,py39: nosetests -v petl --with-coverage --cover-package=petl + py36,py38,py39: nosetests -v petl --with-coverage --cover-package=petl py37: nosetests -v --with-coverage --cover-package=petl --with-doctest --doctest-options=+NORMALIZE_WHITESPACE petl -I"csv_py2\.py" -I"db\.py" coverage report -m deps = @@ -73,7 +71,7 @@ deps = -rrequirements-database.txt [testenv:mysqldb] -basepython = python2.7 +basepython = python3.9 setenv = PYTHONHASHSEED = 42 deps = From 41bf001f14db35ebdca32053609f41099d68cdae Mon Sep 17 00:00:00 2001 From: Hugo van Kemenade Date: Sat, 28 Aug 2021 15:27:44 +0300 Subject: [PATCH 2/7] Upgrade Python syntax with pyupgrade --py36-plus --- docs/conf.py | 13 +- examples/comparison.py | 3 - examples/intro.py | 2 - examples/io/csv.py | 3 - examples/io/html.py | 3 - examples/io/json.py | 3 - examples/io/numpy.py | 3 - examples/io/pandas.py | 3 - examples/io/pickle.py | 3 - examples/io/pytables.py | 4 - examples/io/sqlite3.py | 1 - examples/io/text.py | 3 - examples/io/whoosh.py | 22 ++-- examples/io/xml.py | 3 - examples/transform/basics.py | 5 +- examples/transform/conversions.py | 3 - examples/transform/dedup.py | 5 +- examples/transform/fills.py | 3 - examples/transform/headers.py | 3 - examples/transform/intervals.py | 3 - examples/transform/joins.py | 3 - examples/transform/maps.py | 3 - examples/transform/reductions.py | 3 - examples/transform/regex.py | 3 - examples/transform/reshape.py | 3 - examples/transform/selects.py | 3 - examples/transform/setops.py | 3 - examples/transform/sorts.py | 3 - examples/transform/unpacks.py | 3 - examples/transform/validation.py | 4 - examples/util/base.py | 4 - examples/util/counting.py | 25 ++-- examples/util/lookups.py | 3 - examples/util/materialise.py | 3 - examples/util/misc.py | 9 +- examples/util/parsers.py | 3 - examples/util/random.py | 3 - examples/util/statistics.py | 5 +- examples/util/timing.py | 3 - examples/util/vis.py | 3 - notes/20140424_example.py | 3 +- notes/issue_219.py | 1 - notes/issue_256.py | 1 - petl/__init__.py | 3 - petl/comparison.py | 5 +- petl/compat.py | 3 - petl/config.py | 3 - petl/errors.py | 3 - petl/io/__init__.py | 2 - petl/io/avro.py | 6 +- petl/io/base.py | 7 +- petl/io/bcolz.py | 5 +- petl/io/csv.py | 4 - petl/io/csv_py2.py | 4 - petl/io/csv_py3.py | 1 - petl/io/db.py | 19 +-- petl/io/db_create.py | 5 +- petl/io/db_utils.py | 4 - petl/io/html.py | 8 +- petl/io/json.py | 11 +- petl/io/numpy.py | 4 - petl/io/pandas.py | 4 - petl/io/pickle.py | 4 - petl/io/pytables.py | 4 - petl/io/remotes.py | 9 +- petl/io/sources.py | 28 ++--- petl/io/text.py | 4 - petl/io/whoosh.py | 4 - petl/io/xls.py | 4 - petl/io/xlsx.py | 3 - petl/io/xlutils_view.py | 7 +- petl/io/xml.py | 16 +-- petl/test/helpers.py | 2 - petl/test/io/__init__.py | 1 - petl/test/io/test_avro.py | 117 +++++++++--------- petl/test/io/test_avro_schemas.py | 2 - petl/test/io/test_bcolz.py | 2 - petl/test/io/test_csv.py | 4 - petl/test/io/test_csv_unicode.py | 150 ++++++++++++------------ petl/test/io/test_db.py | 4 - petl/test/io/test_db_create.py | 4 - petl/test/io/test_db_server.py | 12 +- petl/test/io/test_html.py | 90 +++++++------- petl/test/io/test_html_unicode.py | 68 +++++------ petl/test/io/test_json.py | 4 - petl/test/io/test_json_unicode.py | 14 +-- petl/test/io/test_jsonl.py | 3 - petl/test/io/test_numpy.py | 4 - petl/test/io/test_pandas.py | 4 - petl/test/io/test_pickle.py | 4 - petl/test/io/test_pytables.py | 4 - petl/test/io/test_remotes.py | 25 ++-- petl/test/io/test_sources.py | 8 +- petl/test/io/test_sqlite3.py | 4 - petl/test/io/test_tees.py | 58 +++++---- petl/test/io/test_text.py | 6 +- petl/test/io/test_text_unicode.py | 90 +++++++------- petl/test/io/test_whoosh.py | 104 ++++++++-------- petl/test/io/test_xls.py | 14 +-- petl/test/io/test_xlsx.py | 32 +++-- petl/test/io/test_xml.py | 5 +- petl/test/test_comparison.py | 27 ++--- petl/test/test_fluent.py | 3 - petl/test/test_interactive.py | 3 - petl/test/transform/__init__.py | 1 - petl/test/transform/test_basics.py | 35 +++--- petl/test/transform/test_conversions.py | 33 +++--- petl/test/transform/test_dedup.py | 13 +- petl/test/transform/test_fills.py | 3 - petl/test/transform/test_headers.py | 3 - petl/test/transform/test_intervals.py | 3 - petl/test/transform/test_joins.py | 3 - petl/test/transform/test_maps.py | 2 - petl/test/transform/test_reductions.py | 9 +- petl/test/transform/test_regex.py | 46 ++++---- petl/test/transform/test_reshape.py | 3 - petl/test/transform/test_selects.py | 3 - petl/test/transform/test_setops.py | 3 - petl/test/transform/test_sorts.py | 3 - petl/test/transform/test_unpacks.py | 3 - petl/test/transform/test_validation.py | 4 - petl/test/util/__init__.py | 1 - petl/test/util/test_base.py | 7 +- petl/test/util/test_counting.py | 9 +- petl/test/util/test_lookups.py | 3 - petl/test/util/test_materialise.py | 3 - petl/test/util/test_misc.py | 11 +- petl/test/util/test_parsers.py | 3 - petl/test/util/test_statistics.py | 3 - petl/test/util/test_timing.py | 3 - petl/test/util/test_vis.py | 3 - petl/transform/__init__.py | 2 - petl/transform/basics.py | 3 - petl/transform/conversions.py | 7 +- petl/transform/dedup.py | 3 - petl/transform/fills.py | 3 - petl/transform/hashjoins.py | 6 +- petl/transform/headers.py | 9 +- petl/transform/intervals.py | 11 +- petl/transform/joins.py | 3 - petl/transform/maps.py | 8 +- petl/transform/reductions.py | 11 +- petl/transform/regex.py | 3 - petl/transform/reshape.py | 6 +- petl/transform/selects.py | 3 - petl/transform/setops.py | 5 +- petl/transform/sorts.py | 10 +- petl/transform/unpacks.py | 3 - petl/transform/validation.py | 4 - petl/util/__init__.py | 3 - petl/util/base.py | 13 +- petl/util/counting.py | 3 - petl/util/lookups.py | 3 - petl/util/materialise.py | 3 - petl/util/misc.py | 3 - petl/util/parsers.py | 3 - petl/util/random.py | 3 - petl/util/statistics.py | 3 - petl/util/timing.py | 7 +- petl/util/vis.py | 17 ++- setup.py | 1 - 161 files changed, 557 insertions(+), 1065 deletions(-) diff --git a/docs/conf.py b/docs/conf.py index 18c26ef8..a94a09a5 100644 --- a/docs/conf.py +++ b/docs/conf.py @@ -1,4 +1,3 @@ -# -*- coding: utf-8 -*- # # petl documentation build configuration file, created by # sphinx-quickstart on Fri Aug 19 11:16:43 2011. @@ -47,8 +46,8 @@ master_doc = 'index' # General information about the project. -project = u'petl' -copyright = u'2014, Alistair Miles' +project = 'petl' +copyright = '2014, Alistair Miles' # The version info for the project you're documenting, acts as replacement for # |version| and |release|, also used in various other places throughout the @@ -185,8 +184,8 @@ # Grouping the document tree into LaTeX files. List of tuples # (source start file, target name, title, author, documentclass [howto/manual]). latex_documents = [ - ('index', 'petl.tex', u'petl Documentation', - u'Alistair Miles', 'manual'), + ('index', 'petl.tex', 'petl Documentation', + 'Alistair Miles', 'manual'), ] # The name of an image file (relative to this directory) to place at the top of @@ -218,8 +217,8 @@ # One entry per manual page. List of tuples # (source start file, name, description, authors, manual section). man_pages = [ - ('index', 'petl', u'petl Documentation', - [u'Alistair Miles'], 1) + ('index', 'petl', 'petl Documentation', + ['Alistair Miles'], 1) ] diff --git a/examples/comparison.py b/examples/comparison.py index 4bfd2219..cc377a1e 100644 --- a/examples/comparison.py +++ b/examples/comparison.py @@ -1,6 +1,3 @@ -from __future__ import absolute_import, print_function, division - - import petl as etl table = [['foo', 'bar'], ['a', 1], diff --git a/examples/intro.py b/examples/intro.py index f5bea054..ab7c508e 100644 --- a/examples/intro.py +++ b/examples/intro.py @@ -1,5 +1,3 @@ -from __future__ import division, print_function, absolute_import - example_data = """foo,bar,baz a,1,3.4 b,2,7.4 diff --git a/examples/io/csv.py b/examples/io/csv.py index 992b7ab3..4f196526 100644 --- a/examples/io/csv.py +++ b/examples/io/csv.py @@ -1,6 +1,3 @@ -from __future__ import division, print_function, absolute_import - - # fromcsv() ########### diff --git a/examples/io/html.py b/examples/io/html.py index 2a043565..2ac726c0 100644 --- a/examples/io/html.py +++ b/examples/io/html.py @@ -1,6 +1,3 @@ -from __future__ import division, print_function, absolute_import - - # tohtml() ########## diff --git a/examples/io/json.py b/examples/io/json.py index 256def62..57cbb5e6 100644 --- a/examples/io/json.py +++ b/examples/io/json.py @@ -1,6 +1,3 @@ -from __future__ import division, print_function, absolute_import - - # fromjson() ############ diff --git a/examples/io/numpy.py b/examples/io/numpy.py index bb8f3c17..536167bf 100644 --- a/examples/io/numpy.py +++ b/examples/io/numpy.py @@ -1,6 +1,3 @@ -from __future__ import division, print_function, absolute_import - - # toarray() ########### diff --git a/examples/io/pandas.py b/examples/io/pandas.py index 3278aab4..ad73375f 100644 --- a/examples/io/pandas.py +++ b/examples/io/pandas.py @@ -1,6 +1,3 @@ -from __future__ import division, print_function, absolute_import - - # todataframe() ############### diff --git a/examples/io/pickle.py b/examples/io/pickle.py index 5e2701cc..664f82c8 100644 --- a/examples/io/pickle.py +++ b/examples/io/pickle.py @@ -1,6 +1,3 @@ -from __future__ import division, print_function, absolute_import - - # frompickle() ############## diff --git a/examples/io/pytables.py b/examples/io/pytables.py index 72e08594..6ff66ef4 100644 --- a/examples/io/pytables.py +++ b/examples/io/pytables.py @@ -1,7 +1,3 @@ -# -*- coding: utf-8 -*- -from __future__ import absolute_import, print_function, division - - # fromhdf5() ############ diff --git a/examples/io/sqlite3.py b/examples/io/sqlite3.py index 4835ced8..0f6b0709 100644 --- a/examples/io/sqlite3.py +++ b/examples/io/sqlite3.py @@ -1,4 +1,3 @@ -from __future__ import division, print_function, absolute_import import os diff --git a/examples/io/text.py b/examples/io/text.py index 2ef84605..25897c92 100644 --- a/examples/io/text.py +++ b/examples/io/text.py @@ -1,6 +1,3 @@ -from __future__ import division, print_function, absolute_import - - # fromtext() ############ diff --git a/examples/io/whoosh.py b/examples/io/whoosh.py index eca98757..5f4b9513 100644 --- a/examples/io/whoosh.py +++ b/examples/io/whoosh.py @@ -1,7 +1,3 @@ -# -*- coding: utf-8 -*- -from __future__ import absolute_import, print_function, division - - # fromtextindex() ################# @@ -17,10 +13,10 @@ index = create_in(dirname, schema) writer = index.writer() -writer.add_document(title=u"First document", path=u"/a", - content=u"This is the first document we've added!") -writer.add_document(title=u"Second document", path=u"/b", - content=u"The second one is even more interesting!") +writer.add_document(title="First document", path="/a", + content="This is the first document we've added!") +writer.add_document(title="Second document", path="/b", + content="The second one is even more interesting!") writer.commit() # extract documents as a table table = etl.fromtextindex(dirname) @@ -68,11 +64,11 @@ index = create_in('example.whoosh', schema) writer = index.writer() -writer.add_document(title=u"Oranges", path=u"/a", - content=u"This is the first document we've added!") -writer.add_document(title=u"Apples", path=u"/b", - content=u"The second document is even more " - u"interesting!") +writer.add_document(title="Oranges", path="/a", + content="This is the first document we've added!") +writer.add_document(title="Apples", path="/b", + content="The second document is even more " + "interesting!") writer.commit() # demonstrate the use of searchtextindex() table1 = etl.searchtextindex('example.whoosh', 'oranges') diff --git a/examples/io/xml.py b/examples/io/xml.py index 3a44e224..1c4d8831 100644 --- a/examples/io/xml.py +++ b/examples/io/xml.py @@ -1,6 +1,3 @@ -from __future__ import division, print_function, absolute_import - - import petl as etl # setup a file to demonstrate with d = ''' diff --git a/examples/transform/basics.py b/examples/transform/basics.py index e7cc95b9..f9bd1ce0 100644 --- a/examples/transform/basics.py +++ b/examples/transform/basics.py @@ -1,6 +1,3 @@ -from __future__ import division, print_function, absolute_import - - # cut() ####### @@ -54,7 +51,7 @@ table4 = [['foo', 'bar', 'baz'], ['A', 1, 2], ['B', '2', '3.4'], - [u'B', u'3', u'7.8', True], + ['B', '3', '7.8', True], ['D', 'xyz', 9.0], ['E', None]] table5 = etl.cat(table4) diff --git a/examples/transform/conversions.py b/examples/transform/conversions.py index ae5ed0ad..2dfc72b7 100644 --- a/examples/transform/conversions.py +++ b/examples/transform/conversions.py @@ -1,6 +1,3 @@ -from __future__ import division, print_function, absolute_import - - # convert() ########### diff --git a/examples/transform/dedup.py b/examples/transform/dedup.py index a307fd55..8d9c2e83 100644 --- a/examples/transform/dedup.py +++ b/examples/transform/dedup.py @@ -1,6 +1,3 @@ -from __future__ import division, print_function, absolute_import - - # duplicates() ############## @@ -28,7 +25,7 @@ ['A', 1, 2], ['B', '2', '3.4'], ['D', 'xyz', 9.0], - ['B', u'3', u'7.8'], + ['B', '3', '7.8'], ['B', '2', 42], ['E', None, None], ['D', 4, 12.3], diff --git a/examples/transform/fills.py b/examples/transform/fills.py index 40a9610c..767ee79b 100644 --- a/examples/transform/fills.py +++ b/examples/transform/fills.py @@ -1,6 +1,3 @@ -from __future__ import division, print_function, absolute_import - - # filldown() ############ diff --git a/examples/transform/headers.py b/examples/transform/headers.py index 9ba000f3..263a4660 100644 --- a/examples/transform/headers.py +++ b/examples/transform/headers.py @@ -1,6 +1,3 @@ -from __future__ import division, print_function, absolute_import - - # rename() ########## diff --git a/examples/transform/intervals.py b/examples/transform/intervals.py index 95288eeb..85d37f8f 100644 --- a/examples/transform/intervals.py +++ b/examples/transform/intervals.py @@ -1,6 +1,3 @@ -from __future__ import division, print_function, absolute_import - - # intervallookup() ################## diff --git a/examples/transform/joins.py b/examples/transform/joins.py index 51082d0c..4f7faab6 100644 --- a/examples/transform/joins.py +++ b/examples/transform/joins.py @@ -1,6 +1,3 @@ -from __future__ import absolute_import, print_function, division - - # join() ######## diff --git a/examples/transform/maps.py b/examples/transform/maps.py index 0ad1cedb..3c2b5b08 100644 --- a/examples/transform/maps.py +++ b/examples/transform/maps.py @@ -1,6 +1,3 @@ -from __future__ import absolute_import, print_function, division - - # fieldmap() ############ diff --git a/examples/transform/reductions.py b/examples/transform/reductions.py index 6418dcf5..5a2b51b7 100644 --- a/examples/transform/reductions.py +++ b/examples/transform/reductions.py @@ -1,6 +1,3 @@ -from __future__ import absolute_import, print_function, division - - # rowreduce() ############# diff --git a/examples/transform/regex.py b/examples/transform/regex.py index 317183c9..e2de6703 100644 --- a/examples/transform/regex.py +++ b/examples/transform/regex.py @@ -1,6 +1,3 @@ -from __future__ import absolute_import, print_function, division - - # capture() ############ diff --git a/examples/transform/reshape.py b/examples/transform/reshape.py index 387f7dea..937ab35f 100644 --- a/examples/transform/reshape.py +++ b/examples/transform/reshape.py @@ -1,6 +1,3 @@ -from __future__ import absolute_import, print_function, division - - # melt() ######## diff --git a/examples/transform/selects.py b/examples/transform/selects.py index a05b6b45..9604ee4a 100644 --- a/examples/transform/selects.py +++ b/examples/transform/selects.py @@ -1,6 +1,3 @@ -from __future__ import absolute_import, print_function, division - - # select() ########## diff --git a/examples/transform/setops.py b/examples/transform/setops.py index 3870ae1f..02587d6d 100644 --- a/examples/transform/setops.py +++ b/examples/transform/setops.py @@ -1,6 +1,3 @@ -from __future__ import absolute_import, print_function, division - - # complement() ############## diff --git a/examples/transform/sorts.py b/examples/transform/sorts.py index f45e62aa..d40ac257 100644 --- a/examples/transform/sorts.py +++ b/examples/transform/sorts.py @@ -1,6 +1,3 @@ -from __future__ import absolute_import, print_function, division - - # sort() ######## diff --git a/examples/transform/unpacks.py b/examples/transform/unpacks.py index 2cec7e2f..26e0476c 100644 --- a/examples/transform/unpacks.py +++ b/examples/transform/unpacks.py @@ -1,6 +1,3 @@ -from __future__ import absolute_import, print_function, division - - # unpack() ########## diff --git a/examples/transform/validation.py b/examples/transform/validation.py index 995bdd4c..6ee0c0f2 100644 --- a/examples/transform/validation.py +++ b/examples/transform/validation.py @@ -1,7 +1,3 @@ -# -*- coding: utf-8 -*- -from __future__ import absolute_import, print_function, division - - # validate() ############ diff --git a/examples/util/base.py b/examples/util/base.py index 140fdbe0..9867a0ee 100644 --- a/examples/util/base.py +++ b/examples/util/base.py @@ -1,7 +1,3 @@ -from __future__ import division, print_function, absolute_import, \ - unicode_literals - - # values() ########## diff --git a/examples/util/counting.py b/examples/util/counting.py index d3790d80..dbfe80f6 100644 --- a/examples/util/counting.py +++ b/examples/util/counting.py @@ -1,6 +1,3 @@ -from __future__ import division, print_function, absolute_import - - # nrows() ######### @@ -52,8 +49,8 @@ import petl as etl table = [['foo', 'bar', 'baz'], ['A', 'aaa', 2], - ['B', u'2', '3.4'], - [u'B', u'3', u'7.8', True], + ['B', '2', '3.4'], + ['B', '3', '7.8', True], ['D', '3.7', 9.0], ['E', 42]] counter, errors = etl.parsecounter(table, 'bar') @@ -67,8 +64,8 @@ import petl as etl table = [['foo', 'bar', 'baz'], ['A', 'aaa', 2], - ['B', u'2', '3.4'], - [u'B', u'3', u'7.8', True], + ['B', '2', '3.4'], + ['B', '3', '7.8', True], ['D', '3.7', 9.0], ['E', 42]] etl.parsecounts(table, 'bar') @@ -80,9 +77,9 @@ import petl as etl table = [['foo', 'bar', 'baz'], ['A', 1, 2], - ['B', u'2', '3.4'], - [u'B', u'3', u'7.8', True], - ['D', u'xyz', 9.0], + ['B', '2', '3.4'], + ['B', '3', '7.8', True], + ['D', 'xyz', 9.0], ['E', 42]] etl.typecounter(table, 'foo').most_common() etl.typecounter(table, 'bar').most_common() @@ -97,7 +94,7 @@ [b'A', 1, 2], [b'B', '2', b'3.4'], ['B', '3', '7.8', True], - ['D', u'xyz', 9.0], + ['D', 'xyz', 9.0], ['E', 42]] etl.typecounts(table, 'foo') etl.typecounts(table, 'bar') @@ -112,8 +109,8 @@ ['Mr. Foo', '123-1254'], ['Mrs. Bar', '234-1123'], ['Mr. Spo', '123-1254'], - [u'Mr. Baz', u'321 1434'], - [u'Mrs. Baz', u'321 1434'], + ['Mr. Baz', '321 1434'], + ['Mrs. Baz', '321 1434'], ['Mr. Quux', '123-1254-XX']] etl.stringpatterns(table, 'foo') etl.stringpatterns(table, 'bar') @@ -126,7 +123,7 @@ table = [['foo', 'bar', 'baz'], ['A', 1, 2], ['B', '2', '3.4'], - [u'B', u'3', u'7.8', True], + ['B', '3', '7.8', True], ['D', 'xyz', 9.0], ['E', None], ['F', 9]] diff --git a/examples/util/lookups.py b/examples/util/lookups.py index 9b302423..1373c8c3 100644 --- a/examples/util/lookups.py +++ b/examples/util/lookups.py @@ -1,6 +1,3 @@ -from __future__ import division, print_function, absolute_import - - # lookup() ########## diff --git a/examples/util/materialise.py b/examples/util/materialise.py index 29f9f3ae..c8bd398e 100644 --- a/examples/util/materialise.py +++ b/examples/util/materialise.py @@ -1,6 +1,3 @@ -from __future__ import division, print_function, absolute_import - - # columns() ########### diff --git a/examples/util/misc.py b/examples/util/misc.py index c2bb2efd..ab669c19 100644 --- a/examples/util/misc.py +++ b/examples/util/misc.py @@ -1,15 +1,12 @@ -from __future__ import division, print_function, absolute_import - - # typeset() ########### import petl as etl table = [['foo', 'bar', 'baz'], ['A', 1, '2'], - ['B', u'2', '3.4'], - [u'B', u'3', '7.8', True], - ['D', u'xyz', 9.0], + ['B', '2', '3.4'], + ['B', '3', '7.8', True], + ['D', 'xyz', 9.0], ['E', 42]] sorted(etl.typeset(table, 'foo')) sorted(etl.typeset(table, 'bar')) diff --git a/examples/util/parsers.py b/examples/util/parsers.py index 30141f58..8f205e04 100644 --- a/examples/util/parsers.py +++ b/examples/util/parsers.py @@ -1,6 +1,3 @@ -from __future__ import division, print_function, absolute_import - - # datetimeparser() ################## diff --git a/examples/util/random.py b/examples/util/random.py index 0a065636..1f017054 100644 --- a/examples/util/random.py +++ b/examples/util/random.py @@ -1,6 +1,3 @@ -from __future__ import division, print_function, absolute_import - - # randomtable() ############### diff --git a/examples/util/statistics.py b/examples/util/statistics.py index a1bf0019..ab7cd47b 100644 --- a/examples/util/statistics.py +++ b/examples/util/statistics.py @@ -1,6 +1,3 @@ -from __future__ import division, print_function, absolute_import - - # limits() ########## @@ -18,7 +15,7 @@ table = [['foo', 'bar', 'baz'], ['A', 1, 2], ['B', '2', '3.4'], - [u'B', u'3', u'7.8', True], + ['B', '3', '7.8', True], ['D', 'xyz', 9.0], ['E', None]] etl.stats(table, 'bar') diff --git a/examples/util/timing.py b/examples/util/timing.py index 47a673ef..34a7cf0c 100644 --- a/examples/util/timing.py +++ b/examples/util/timing.py @@ -1,6 +1,3 @@ -from __future__ import division, print_function, absolute_import - - # progress() ############ diff --git a/examples/util/vis.py b/examples/util/vis.py index cda051a4..a47b2257 100644 --- a/examples/util/vis.py +++ b/examples/util/vis.py @@ -1,6 +1,3 @@ -from __future__ import division, print_function, absolute_import - - # look() ######## diff --git a/notes/20140424_example.py b/notes/20140424_example.py index 43b1e612..238d609d 100644 --- a/notes/20140424_example.py +++ b/notes/20140424_example.py @@ -1,4 +1,3 @@ -# -*- coding: utf-8 -*- # 3.0 # @@ -54,7 +53,7 @@ def make_room_for_category(row): # -class CustomTransformer(object): +class CustomTransformer: def __init__(self, source): self.source = source diff --git a/notes/issue_219.py b/notes/issue_219.py index 25a91f63..278d484b 100644 --- a/notes/issue_219.py +++ b/notes/issue_219.py @@ -1,4 +1,3 @@ -# -*- coding: utf-8 -*- # 3.0 # diff --git a/notes/issue_256.py b/notes/issue_256.py index ed368135..3aee1d5e 100644 --- a/notes/issue_256.py +++ b/notes/issue_256.py @@ -1,4 +1,3 @@ -# -*- coding: utf-8 -*- # 3.0 # diff --git a/petl/__init__.py b/petl/__init__.py index 36ac6344..13dac2c6 100644 --- a/petl/__init__.py +++ b/petl/__init__.py @@ -1,6 +1,3 @@ -from __future__ import absolute_import, print_function, division - - from petl.version import version as __version__ from petl import comparison from petl.comparison import Comparable diff --git a/petl/comparison.py b/petl/comparison.py index fc4d991d..d5a6b92a 100644 --- a/petl/comparison.py +++ b/petl/comparison.py @@ -1,13 +1,10 @@ -from __future__ import absolute_import, print_function, division - - import operator from petl.compat import text_type, binary_type, numeric_types -class Comparable(object): +class Comparable: """Wrapper class to allow for flexible comparison of objects of different types, preserving the relaxed sorting behaviour of Python 2 with additional flexibility to allow for comparison of arbitrary objects with diff --git a/petl/compat.py b/petl/compat.py index 71c29018..3bfc47a1 100644 --- a/petl/compat.py +++ b/petl/compat.py @@ -1,6 +1,3 @@ -from __future__ import absolute_import, print_function, division - - import sys diff --git a/petl/config.py b/petl/config.py index 4815a15b..c99cdea9 100644 --- a/petl/config.py +++ b/petl/config.py @@ -1,6 +1,3 @@ -from __future__ import division, print_function, absolute_import - - from petl.compat import text_type diff --git a/petl/errors.py b/petl/errors.py index 39f1e4f2..d3d1b9ca 100644 --- a/petl/errors.py +++ b/petl/errors.py @@ -1,6 +1,3 @@ -from __future__ import absolute_import, print_function, division - - class DuplicateKeyError(Exception): def __init__(self, key): diff --git a/petl/io/__init__.py b/petl/io/__init__.py index 630735eb..b2b993f6 100644 --- a/petl/io/__init__.py +++ b/petl/io/__init__.py @@ -1,5 +1,3 @@ -from __future__ import absolute_import, print_function, division - from petl.io.base import fromcolumns from petl.io.sources import FileSource, GzipSource, BZ2Source, ZipSource, \ diff --git a/petl/io/avro.py b/petl/io/avro.py index b2d3f023..ebdd17f0 100644 --- a/petl/io/avro.py +++ b/petl/io/avro.py @@ -1,6 +1,3 @@ -# -*- coding: utf-8 -*- -from __future__ import absolute_import, division, print_function - import sys import math from collections import OrderedDict @@ -258,8 +255,7 @@ def __iter__(self): avro_reader = self._open_reader(source_file) header = self._decode_schema(avro_reader) yield header - for row in self._read_rows_from(avro_reader, header): - yield row + yield from self._read_rows_from(avro_reader, header) def _open_reader(self, source_file): '''This could raise a error when the file is corrupt or is not avro''' diff --git a/petl/io/base.py b/petl/io/base.py index 19c7f64d..9ca82d49 100644 --- a/petl/io/base.py +++ b/petl/io/base.py @@ -1,7 +1,3 @@ -# -*- coding: utf-8 -*- -from __future__ import division, print_function, absolute_import - - import locale import codecs from petl.compat import izip_longest @@ -73,5 +69,4 @@ def itercolumns(cols, header, missing): if header is None: header = ['f%s' % i for i in range(len(cols))] yield tuple(header) - for row in izip_longest(*cols, **dict(fillvalue=missing)): - yield row + yield from izip_longest(*cols, **dict(fillvalue=missing)) diff --git a/petl/io/bcolz.py b/petl/io/bcolz.py index 34fbd403..cd758e57 100644 --- a/petl/io/bcolz.py +++ b/petl/io/bcolz.py @@ -1,5 +1,3 @@ -# -*- coding: utf-8 -*- -from __future__ import absolute_import, print_function, division import itertools @@ -92,8 +90,7 @@ def __iter__(self): it = ctbl.where(self.expression, outcols=self.outcols, skip=self.skip, limit=self.limit) - for row in it: - yield row + yield from it def tobcolz(table, dtype=None, sample=1000, **kwargs): diff --git a/petl/io/csv.py b/petl/io/csv.py index 6c092c53..aa350b6e 100644 --- a/petl/io/csv.py +++ b/petl/io/csv.py @@ -1,7 +1,3 @@ -# -*- coding: utf-8 -*- -from __future__ import absolute_import, print_function, division - - # standard library dependencies from petl.compat import PY2 diff --git a/petl/io/csv_py2.py b/petl/io/csv_py2.py index a7057ad3..2bfa3016 100644 --- a/petl/io/csv_py2.py +++ b/petl/io/csv_py2.py @@ -1,7 +1,3 @@ -# -*- coding: utf-8 -*- -from __future__ import absolute_import, print_function, division - - # standard library dependencies import csv import cStringIO diff --git a/petl/io/csv_py3.py b/petl/io/csv_py3.py index 9b800baa..5d241e81 100644 --- a/petl/io/csv_py3.py +++ b/petl/io/csv_py3.py @@ -1,4 +1,3 @@ -# -*- coding: utf-8 -*- import io import csv import logging diff --git a/petl/io/db.py b/petl/io/db.py index 294fe8ab..d12bd68e 100644 --- a/petl/io/db.py +++ b/petl/io/db.py @@ -1,7 +1,3 @@ -# -*- coding: utf-8 -*- -from __future__ import absolute_import, print_function, division - - # standard library dependencies import logging from petl.compat import next, text_type, string_types @@ -144,8 +140,7 @@ def __iter__(self): def _iter_dbapi_mkcurs(mkcurs, query, *args, **kwargs): cursor = mkcurs() try: - for row in _iter_dbapi_cursor(cursor, query, *args, **kwargs): - yield row + yield from _iter_dbapi_cursor(cursor, query, *args, **kwargs) finally: cursor.close() @@ -153,8 +148,7 @@ def _iter_dbapi_mkcurs(mkcurs, query, *args, **kwargs): def _iter_dbapi_connection(connection, query, *args, **kwargs): cursor = connection.cursor() try: - for row in _iter_dbapi_cursor(cursor, query, *args, **kwargs): - yield row + yield from _iter_dbapi_cursor(cursor, query, *args, **kwargs) finally: cursor.close() @@ -178,8 +172,7 @@ def _iter_dbapi_cursor(cursor, query, *args, **kwargs): if first_row is None: return yield first_row - for row in it: - yield row # don't wrap, return whatever the database engine returns + yield from it def _iter_sqlalchemy_engine(engine, query, *args, **kwargs): @@ -192,16 +185,14 @@ def _iter_sqlalchemy_connection(connection, query, *args, **kwargs): results = connection.execute(query, *args, **kwargs) hdr = results.keys() yield tuple(hdr) - for row in results: - yield row + yield from results def _iter_sqlalchemy_session(session, query, *args, **kwargs): results = session.execute(query, *args, **kwargs) hdr = results.keys() yield tuple(hdr) - for row in results: - yield row + yield from results def todb(table, dbo, tablename, schema=None, commit=True, diff --git a/petl/io/db_create.py b/petl/io/db_create.py index 0dd9f824..5faff842 100644 --- a/petl/io/db_create.py +++ b/petl/io/db_create.py @@ -1,4 +1,3 @@ -# -*- coding: utf-8 -*- """ Module providing some convenience functions for working with SQL databases. SQLAlchemy is required, try ``apt-get install python-sqlalchemy`` @@ -103,7 +102,7 @@ def make_sqlalchemy_column(col, colname, constraints=True): else: sql_column_type = sqlalchemy.String if constraints: - sql_type_kwargs['length'] = max([len(text_type(v)) for v in col]) + sql_type_kwargs['length'] = max(len(text_type(v)) for v in col) if constraints: sql_column_kwargs['nullable'] = len(col_not_none) < len(col) @@ -249,7 +248,7 @@ def drop_table(dbo, tablename, schema=None, commit=True): if schema is not None: tablename = _quote(schema) + '.' + tablename - sql = u'DROP TABLE %s' % tablename + sql = 'DROP TABLE %s' % tablename _execute(sql, dbo, commit) diff --git a/petl/io/db_utils.py b/petl/io/db_utils.py index 87d1e3de..4a3eb78c 100644 --- a/petl/io/db_utils.py +++ b/petl/io/db_utils.py @@ -1,7 +1,3 @@ -# -*- coding: utf-8 -*- -from __future__ import absolute_import, print_function, division - - import logging diff --git a/petl/io/html.py b/petl/io/html.py index fe6ef07c..e4de892d 100644 --- a/petl/io/html.py +++ b/petl/io/html.py @@ -1,7 +1,3 @@ -# -*- coding: utf-8 -*- -from __future__ import absolute_import, print_function, division - - # standard library dependencies import io from petl.compat import text_type, numeric_types, next, PY2, izip_longest, \ @@ -197,7 +193,7 @@ def _write_begin(f, flds, lineterminator, caption, index_header, truncate): f.write('' + lineterminator) for i, h in enumerate(flds): if index_header: - h = '%s|%s' % (i, h) + h = f'{i}|{h}' if truncate: h = h[:truncate] f.write(('' % h) + lineterminator) @@ -219,7 +215,7 @@ def _write_row(f, flds, row, lineterminator, vrepr, tr_style, td_styles, r = r[:truncate] td_css = _get_td_css(h, v, td_styles) if td_css: - f.write(("" % (td_css, r)) + lineterminator) + f.write((f"") + lineterminator) else: f.write(("" % r) + lineterminator) f.write('' + lineterminator) diff --git a/petl/io/json.py b/petl/io/json.py index e00a3bea..d321f9ec 100644 --- a/petl/io/json.py +++ b/petl/io/json.py @@ -1,6 +1,3 @@ -# -*- coding: utf-8 -*- -from __future__ import absolute_import, print_function, division - # standard library dependencies import io import json @@ -106,13 +103,11 @@ def __iter__(self): write_through=True) try: if self.lines: - for row in iterjlines(f, self.header, self.missing): - yield row + yield from iterjlines(f, self.header, self.missing) else: dicts = json.load(f, *self.args, **self.kwargs) - for row in iterdicts(dicts, self.header, self.sample, - self.missing): - yield row + yield from iterdicts(dicts, self.header, self.sample, + self.missing) finally: if not PY2: f.detach() diff --git a/petl/io/numpy.py b/petl/io/numpy.py index d2a14ca4..f7483744 100644 --- a/petl/io/numpy.py +++ b/petl/io/numpy.py @@ -1,7 +1,3 @@ -# -*- coding: utf-8 -*- -from __future__ import division, print_function, absolute_import - - from petl.compat import next, string_types from petl.util.base import iterpeek, ValuesView, Table from petl.util.materialise import columns diff --git a/petl/io/pandas.py b/petl/io/pandas.py index f5cb5b8d..63e72b57 100644 --- a/petl/io/pandas.py +++ b/petl/io/pandas.py @@ -1,7 +1,3 @@ -# -*- coding: utf-8 -*- -from __future__ import division, print_function, absolute_import - - import inspect diff --git a/petl/io/pickle.py b/petl/io/pickle.py index 0bf77802..cf871591 100644 --- a/petl/io/pickle.py +++ b/petl/io/pickle.py @@ -1,7 +1,3 @@ -# -*- coding: utf-8 -*- -from __future__ import absolute_import, print_function, division - - # standard library dependencies from petl.compat import pickle, next diff --git a/petl/io/pytables.py b/petl/io/pytables.py index 1b4909f5..156ec1ba 100644 --- a/petl/io/pytables.py +++ b/petl/io/pytables.py @@ -1,7 +1,3 @@ -# -*- coding: utf-8 -*- -from __future__ import absolute_import, print_function, division - - from contextlib import contextmanager from petl.compat import string_types diff --git a/petl/io/remotes.py b/petl/io/remotes.py index 81b70689..fb38dc13 100644 --- a/petl/io/remotes.py +++ b/petl/io/remotes.py @@ -1,6 +1,3 @@ -# -*- coding: utf-8 -*- -from __future__ import absolute_import, print_function, division - import logging import sys from contextlib import contextmanager @@ -13,7 +10,7 @@ # region RemoteSource -class RemoteSource(object): +class RemoteSource: """Read or write directly from files in remote filesystems. This source handles many filesystems that are selected based on the @@ -144,7 +141,7 @@ def _try_register_filesystems(): # region SMBSource -class SMBSource(object): +class SMBSource: """Downloads or uploads to Windows and Samba network drives. E.g.:: >>> def example_smb(): @@ -237,7 +234,7 @@ def _parse_smb_url(url): raise ValueError(e + url) unc_path = parsed.path.replace("/", "\\") - server_path = "\\\\{}{}".format(parsed.hostname, unc_path) + server_path = f"\\\\{parsed.hostname}{unc_path}" if not parsed.username: domain = None diff --git a/petl/io/sources.py b/petl/io/sources.py index 108cdbdc..21dcdde7 100644 --- a/petl/io/sources.py +++ b/petl/io/sources.py @@ -1,7 +1,3 @@ -# -*- coding: utf-8 -*- -from __future__ import absolute_import, print_function, division - - import os import io import gzip @@ -23,17 +19,17 @@ debug = logger.debug -class FileSource(object): +class FileSource: def __init__(self, filename, **kwargs): self.filename = filename self.kwargs = kwargs def open(self, mode='r'): - return io.open(self.filename, mode, **self.kwargs) + return open(self.filename, mode, **self.kwargs) -class GzipSource(object): +class GzipSource: def __init__(self, filename, remote=False, **kwargs): self.filename = filename @@ -55,7 +51,7 @@ def open(self, mode='r'): source.close() -class BZ2Source(object): +class BZ2Source: def __init__(self, filename, remote=False, **kwargs): self.filename = filename @@ -77,7 +73,7 @@ def open(self, mode='r'): source.close() -class ZipSource(object): +class ZipSource: def __init__(self, filename, membername, pwd=None, **kwargs): self.filename = filename @@ -101,7 +97,7 @@ def open(self, mode): zf.close() -class Uncloseable(object): +class Uncloseable: def __init__(self, inner): object.__setattr__(self, '_inner', inner) @@ -169,7 +165,7 @@ def _get_stdin_binary(): stdin_binary = _get_stdin_binary() -class StdoutSource(object): +class StdoutSource: @contextmanager def open(self, mode): @@ -181,7 +177,7 @@ def open(self, mode): yield Uncloseable(sys.stdout) -class StdinSource(object): +class StdinSource: @contextmanager def open(self, mode='r'): @@ -193,7 +189,7 @@ def open(self, mode='r'): yield Uncloseable(sys.stdin) -class URLSource(object): +class URLSource: def __init__(self, *args, **kwargs): self.args = args @@ -210,7 +206,7 @@ def open(self, mode='r'): f.close() -class MemorySource(object): +class MemorySource: """Memory data source. E.g.:: >>> import petl as etl @@ -280,7 +276,7 @@ def getvalue(self): StringSource = MemorySource -class PopenSource(object): +class PopenSource: def __init__(self, *args, **kwargs): self.args = args @@ -298,7 +294,7 @@ def open(self, mode='r'): pass -class CompressedSource(object): +class CompressedSource: '''Handle IO from a file-like object and (de)compress with a codec The `source` argument (source class) is the source class that will handle diff --git a/petl/io/text.py b/petl/io/text.py index 570d2efe..e2da6374 100644 --- a/petl/io/text.py +++ b/petl/io/text.py @@ -1,7 +1,3 @@ -# -*- coding: utf-8 -*- -from __future__ import absolute_import, print_function, division - - # standard library dependencies import io from petl.compat import next, PY2, text_type diff --git a/petl/io/whoosh.py b/petl/io/whoosh.py index e20b4f96..667a5feb 100644 --- a/petl/io/whoosh.py +++ b/petl/io/whoosh.py @@ -1,7 +1,3 @@ -# -*- coding: utf-8 -*- -from __future__ import absolute_import, print_function, division - - import operator from petl.compat import string_types, izip diff --git a/petl/io/xls.py b/petl/io/xls.py index b9896d8d..3b062fa0 100644 --- a/petl/io/xls.py +++ b/petl/io/xls.py @@ -1,7 +1,3 @@ -# -*- coding: utf-8 -*- -from __future__ import division, print_function, absolute_import - - import locale diff --git a/petl/io/xlsx.py b/petl/io/xlsx.py index 84b14520..5905c0f2 100644 --- a/petl/io/xlsx.py +++ b/petl/io/xlsx.py @@ -1,6 +1,3 @@ -# -*- coding: utf-8 -*- -from __future__ import absolute_import, print_function, division - from petl.compat import PY3 from petl.util.base import Table, data from petl.io.sources import read_source_from_arg, write_source_from_arg diff --git a/petl/io/xlutils_view.py b/petl/io/xlutils_view.py index 62136cbc..e20f0d25 100644 --- a/petl/io/xlutils_view.py +++ b/petl/io/xlutils_view.py @@ -1,4 +1,3 @@ -# -*- coding: utf-8 -*- # Copyright (c) 2013 Simplistix Ltd # # This Software is released under the MIT License: @@ -10,7 +9,7 @@ from petl.compat import xrange -class Index(object): +class Index: def __init__(self, name): self.name = name @@ -34,7 +33,7 @@ def __index__(self): return col_by_name(self.name) -class SheetView(object): +class SheetView: """ A view on a sheet in a workbook. Should be created by indexing a :class:`View`. @@ -97,7 +96,7 @@ def __getitem__(self, slices): return self.__class__(self.book, self.sheet, *slices) -class View(object): +class View: """ A view wrapper around a :class:`~xlrd.Book` that allows for easy iteration over the data in a group of cells. diff --git a/petl/io/xml.py b/petl/io/xml.py index 23dbf1b9..1d0377ba 100644 --- a/petl/io/xml.py +++ b/petl/io/xml.py @@ -1,7 +1,3 @@ -# -*- coding: utf-8 -*- -from __future__ import absolute_import, print_function, division - - # standard library dependencies try: # prefer lxml as it supports XPath @@ -194,8 +190,8 @@ def __iter__(self): velms = rowelm.findall(vmatch) else: # match multiple paths - velms = itertools.chain(*[rowelm.findall(enm) - for enm in vmatch]) + velms = itertools.chain(*(rowelm.findall(enm) + for enm in vmatch)) yield tuple(getv(velm) for velm in velms) @@ -380,18 +376,18 @@ def _build_xml_header(style, props, root, head, rows, prologue, encoding): th1 = _build_nesting(head, False, nested) col = _build_cols(style, props, head, False) th2 = _build_nesting(head, True, nested) - thd = '{0}\n{1}{2}'.format(th1, col, th2) + thd = f'{th1}\n{col}{th2}' else: thd = '' tbd = _build_nesting(rows, False, nested) if prologue and prologue.startswith('' % enc pre = prologue + '\n' if prologue and not root else '' pos = '\n' + prologue if prologue and root else '' - res = '{0}\n{1}{2}{3}{4}{5}\n'.format(xml, pre, tab, thd, tbd, pos) + res = f'{xml}\n{pre}{tab}{thd}{tbd}{pos}\n' return res @@ -453,5 +449,5 @@ def _build_cols_attribs(props, path): fmt = '{0}="{{{0}}}"' cols = [fmt.format(e) for e in props] atts = ' '.join(cols) - res = ' <{0} {1} />\n'.format(row, atts) + res = f' <{row} {atts} />\n' return res diff --git a/petl/test/helpers.py b/petl/test/helpers.py index 7bcecb46..de7fd839 100644 --- a/petl/test/helpers.py +++ b/petl/test/helpers.py @@ -1,5 +1,3 @@ -from __future__ import absolute_import, print_function, division - import sys from nose.tools import eq_, assert_almost_equal diff --git a/petl/test/io/__init__.py b/petl/test/io/__init__.py index f298a611..e69de29b 100644 --- a/petl/test/io/__init__.py +++ b/petl/test/io/__init__.py @@ -1 +0,0 @@ -from __future__ import absolute_import, print_function, division diff --git a/petl/test/io/test_avro.py b/petl/test/io/test_avro.py index 8b320f4e..9621451e 100644 --- a/petl/test/io/test_avro.py +++ b/petl/test/io/test_avro.py @@ -1,6 +1,3 @@ -# -*- coding: utf-8 -*- -from __future__ import absolute_import, print_function, division - import sys import math @@ -210,12 +207,12 @@ def _create_avro_example(test_schema, test_table): # region Mockup data - header1 = [u'name', u'friends', u'age'] + header1 = ['name', 'friends', 'age'] - rows1 = [[u'Bob', 42, 33], - [u'Jim', 13, 69], - [u'Joe', 86, 17], - [u'Ted', 23, 51]] + rows1 = [['Bob', 42, 33], + ['Jim', 13, 69], + ['Joe', 86, 17], + ['Ted', 23, 51]] table1 = [header1] + rows1 @@ -225,60 +222,60 @@ def _create_avro_example(test_schema, test_table): table01 = [header1[0:2]] + [item[0:2] for item in rows1] table10 = [header1] + [item[0:2] + [None] for item in rows1] - table2 = [[u'name', u'age', u'birthday', u'death', u'insurance', u'deny'], - [u'pete', 17, date(2012, 10, 11), + table2 = [['name', 'age', 'birthday', 'death', 'insurance', 'deny'], + ['pete', 17, date(2012, 10, 11), _utc(2018, 10, 14, 15, 16, 17, 18000), Decimal('1.100'), False], - [u'mike', 27, date(2002, 11, 12), + ['mike', 27, date(2002, 11, 12), _utc(2015, 12, 13, 14, 15, 16, 17000), Decimal('1.010'), False], - [u'zack', 37, date(1992, 12, 13), + ['zack', 37, date(1992, 12, 13), _utc(2010, 11, 12, 13, 14, 15, 16000), Decimal('123.456'), True], - [u'gene', 47, date(1982, 12, 25), + ['gene', 47, date(1982, 12, 25), _utc(2009, 10, 11, 12, 13, 14, 15000), Decimal('-1.010'), False]] table21 = table2[0:3] table22 = [table2[0]] + table2[3:] - table3 = [[u'name', u'age', u'birthday', u'death'], - [u'pete', 17, date(2012, 10, 11), + table3 = [['name', 'age', 'birthday', 'death'], + ['pete', 17, date(2012, 10, 11), _utc(2018, 10, 14, 15, 16, 17, 18000)], - [u'mike', 27, date(2002, 11, 12), + ['mike', 27, date(2002, 11, 12), _utc(2015, 12, 13, 14, 15, 16, 17000)], - [u'zack', 37, date(1992, 12, 13), + ['zack', 37, date(1992, 12, 13), _utc(2010, 11, 12, 13, 14, 15, 16000)], - [u'gene', 47, date(1982, 12, 25), + ['gene', 47, date(1982, 12, 25), _utc(2009, 10, 11, 12, 13, 14, 15000)]] - table4 = [[u'name', u'friends', u'age', u'birthday'], - [u'Bob', 42, 33, date(2012, 10, 11)], - [u'Jim', 13, 69, None], + table4 = [['name', 'friends', 'age', 'birthday'], + ['Bob', 42, 33, date(2012, 10, 11)], + ['Jim', 13, 69, None], [None, 86, 17, date(1992, 12, 13)], - [u'Ted', 23, None, date(1982, 12, 25)]] - - table5 = [[u'palette', u'colors'], - [u'red', [u'red', u'salmon', u'crimson', u'firebrick', u'coral']], - [u'pink', [u'pink', u'rose']], - [u'purple', [u'purple', u'violet', u'fuchsia', - u'magenta', u'indigo', u'orchid', u'lavender']], - [u'green', [u'green', u'lime', u'seagreen', - u'grass', u'olive', u'forest', u'teal']], - [u'blue', [u'blue', u'cyan', u'aqua', u'aquamarine', - u'turquoise', u'royal', u'sky', u'navy']], - [u'gold', [u'gold', u'yellow', u'khaki', - u'mocassin', u'papayawhip', u'lemonchiffon']], - [u'black', None]] - - header6 = [u'array_string', u'array_record', u'nulable_date', - u'multi_union_time', u'array_bytes_decimal', u'array_fixed_decimal'] - - rows61 = [[u'a', u'b', u'c'], - [{u'f1': u'1', u'f2': Decimal('654.321')}], + ['Ted', 23, None, date(1982, 12, 25)]] + + table5 = [['palette', 'colors'], + ['red', ['red', 'salmon', 'crimson', 'firebrick', 'coral']], + ['pink', ['pink', 'rose']], + ['purple', ['purple', 'violet', 'fuchsia', + 'magenta', 'indigo', 'orchid', 'lavender']], + ['green', ['green', 'lime', 'seagreen', + 'grass', 'olive', 'forest', 'teal']], + ['blue', ['blue', 'cyan', 'aqua', 'aquamarine', + 'turquoise', 'royal', 'sky', 'navy']], + ['gold', ['gold', 'yellow', 'khaki', + 'mocassin', 'papayawhip', 'lemonchiffon']], + ['black', None]] + + header6 = ['array_string', 'array_record', 'nulable_date', + 'multi_union_time', 'array_bytes_decimal', 'array_fixed_decimal'] + + rows61 = [['a', 'b', 'c'], + [{'f1': '1', 'f2': Decimal('654.321')}], date(2020, 1, 10), _utc(2020, 12, 19, 18, 17, 16, 15000), [Decimal('123.456')], [Decimal('987.654')], ] - rows62 = [[u'a', u'b', u'c'], - [{u'f1': u'1', u'f2': Decimal('654.321')}], + rows62 = [['a', 'b', 'c'], + [{'f1': '1', 'f2': Decimal('654.321')}], date(2020, 1, 10), _utc(2020, 12, 19, 18, 17, 16, 15000), [Decimal('123.456'), Decimal('456.789')], @@ -292,7 +289,7 @@ def _create_avro_example(test_schema, test_table): table60 = [header6, [rows61[0], rows61[1], ]] - header7 = [u'col', u'sqrt_pow_ij'] + header7 = ['col', 'sqrt_pow_ij'] rows70 = [[j, [round(math.sqrt(math.pow(i*j, i+j)), 9) for i in range(1, j+1)]] for j in range(1, 7)] @@ -303,28 +300,28 @@ def _create_avro_example(test_schema, test_table): table70 = [header7] + rows70 table71 = [header7] + rows71 - header8 = [u'number', u'properties'] + header8 = ['number', 'properties'] rows8 = [[_decs(x), { - u'atan': _decs(math.atan(x)), - u'sin': math.sin(x), - u'cos': math.cos(x), - u'tan': math.tan(x), - u'square': x*x, - u'sqrt': math.sqrt(x), - u'log': math.log(x), - u'log10': math.log10(x), - u'exp': math.log10(x), - u'power_x': x**x, - u'power_minus_x': x**-x, + 'atan': _decs(math.atan(x)), + 'sin': math.sin(x), + 'cos': math.cos(x), + 'tan': math.tan(x), + 'square': x*x, + 'sqrt': math.sqrt(x), + 'log': math.log(x), + 'log10': math.log10(x), + 'exp': math.log10(x), + 'power_x': x**x, + 'power_minus_x': x**-x, }] for x in range(1, 12)] table8 = [header8] + rows8 - rows9 = [[1, { u'name': u'Bob', u'age': 20 }], - [2, { u'name': u'Ted', u'budget': _decs(54321.25) }], - [2, { u'name': u'Jim', u'color': u'blue' }], - [2, { u'name': u'Joe', u'alias': u'terminator' }]] + rows9 = [[1, { 'name': 'Bob', 'age': 20 }], + [2, { 'name': 'Ted', 'budget': _decs(54321.25) }], + [2, { 'name': 'Jim', 'color': 'blue' }], + [2, { 'name': 'Joe', 'alias': 'terminator' }]] table9 = [header8] + rows9 diff --git a/petl/test/io/test_avro_schemas.py b/petl/test/io/test_avro_schemas.py index ca8b8b9a..296d8861 100644 --- a/petl/test/io/test_avro_schemas.py +++ b/petl/test/io/test_avro_schemas.py @@ -1,5 +1,3 @@ -# -*- coding: utf-8 -*- - # begin_nullable_schema schema0 = { diff --git a/petl/test/io/test_bcolz.py b/petl/test/io/test_bcolz.py index 069369a5..207bcbff 100644 --- a/petl/test/io/test_bcolz.py +++ b/petl/test/io/test_bcolz.py @@ -1,5 +1,3 @@ -# -*- coding: utf-8 -*- -from __future__ import absolute_import, print_function, division import sys import tempfile diff --git a/petl/test/io/test_csv.py b/petl/test/io/test_csv.py index 440f9003..17328196 100644 --- a/petl/test/io/test_csv.py +++ b/petl/test/io/test_csv.py @@ -1,7 +1,3 @@ -# -*- coding: utf-8 -*- -from __future__ import absolute_import, print_function, division - - from tempfile import NamedTemporaryFile import gzip import os diff --git a/petl/test/io/test_csv_unicode.py b/petl/test/io/test_csv_unicode.py index e4b4ef88..c89838cf 100644 --- a/petl/test/io/test_csv_unicode.py +++ b/petl/test/io/test_csv_unicode.py @@ -1,7 +1,3 @@ -# -*- coding: utf-8 -*- -from __future__ import absolute_import, print_function, division - - import io from tempfile import NamedTemporaryFile @@ -13,42 +9,42 @@ def test_fromcsv(): data = ( - u"name,id\n" - u"Արամ Խաչատրյան,1\n" - u"Johann Strauß,2\n" - u"Вагиф Сәмәдоғлу,3\n" - u"章子怡,4\n" + "name,id\n" + "Արամ Խաչատրյան,1\n" + "Johann Strauß,2\n" + "Вагиф Сәмәдоғлу,3\n" + "章子怡,4\n" ) fn = NamedTemporaryFile().name - uf = io.open(fn, encoding='utf-8', mode='wt') + uf = open(fn, encoding='utf-8', mode='wt') uf.write(data) uf.close() actual = fromcsv(fn, encoding='utf-8') - expect = ((u'name', u'id'), - (u'Արամ Խաչատրյան', u'1'), - (u'Johann Strauß', u'2'), - (u'Вагиф Сәмәдоғлу', u'3'), - (u'章子怡', u'4')) + expect = (('name', 'id'), + ('Արամ Խաչատրյան', '1'), + ('Johann Strauß', '2'), + ('Вагиф Сәмәдоғлу', '3'), + ('章子怡', '4')) ieq(expect, actual) ieq(expect, actual) # verify can iterate twice def test_fromcsv_lineterminators(): - data = (u'name,id', - u'Արամ Խաչատրյան,1', - u'Johann Strauß,2', - u'Вагиф Сәмәдоғлу,3', - u'章子怡,4') - expect = ((u'name', u'id'), - (u'Արամ Խաչատրյան', u'1'), - (u'Johann Strauß', u'2'), - (u'Вагиф Сәмәдоғлу', u'3'), - (u'章子怡', u'4')) - - for lt in u'\r', u'\n', u'\r\n': + data = ('name,id', + 'Արամ Խաչատրյան,1', + 'Johann Strauß,2', + 'Вагиф Сәмәдоғлу,3', + '章子怡,4') + expect = (('name', 'id'), + ('Արամ Խաչատրյան', '1'), + ('Johann Strauß', '2'), + ('Вагиф Сәмәдоғлу', '3'), + ('章子怡', '4')) + + for lt in '\r', '\n', '\r\n': fn = NamedTemporaryFile().name - uf = io.open(fn, encoding='utf-8', mode='wt', newline='') + uf = open(fn, encoding='utf-8', mode='wt', newline='') uf.write(lt.join(data)) uf.close() actual = fromcsv(fn, encoding='utf-8') @@ -57,40 +53,40 @@ def test_fromcsv_lineterminators(): def test_tocsv(): - tbl = ((u'name', u'id'), - (u'Արամ Խաչատրյան', 1), - (u'Johann Strauß', 2), - (u'Вагиф Сәмәдоғлу', 3), - (u'章子怡', 4)) + tbl = (('name', 'id'), + ('Արամ Խաչատրյան', 1), + ('Johann Strauß', 2), + ('Вагиф Сәмәдоғлу', 3), + ('章子怡', 4)) fn = NamedTemporaryFile().name tocsv(tbl, fn, encoding='utf-8', lineterminator='\n') expect = ( - u"name,id\n" - u"Արամ Խաչատրյան,1\n" - u"Johann Strauß,2\n" - u"Вагиф Сәмәдоғлу,3\n" - u"章子怡,4\n" + "name,id\n" + "Արամ Խաչատրյան,1\n" + "Johann Strauß,2\n" + "Вагиф Сәмәдоғлу,3\n" + "章子怡,4\n" ) - uf = io.open(fn, encoding='utf-8', mode='rt', newline='') + uf = open(fn, encoding='utf-8', mode='rt', newline='') actual = uf.read() eq_(expect, actual) # Test with write_header=False - tbl = ((u'name', u'id'), - (u'Արամ Խաչատրյան', 1), - (u'Johann Strauß', 2), - (u'Вагиф Сәмәдоғлу', 3), - (u'章子怡', 4)) + tbl = (('name', 'id'), + ('Արամ Խաչատրյան', 1), + ('Johann Strauß', 2), + ('Вагиф Сәмәдоғлу', 3), + ('章子怡', 4)) tocsv(tbl, fn, encoding='utf-8', lineterminator='\n', write_header=False) expect = ( - u"Արամ Խաչատրյան,1\n" - u"Johann Strauß,2\n" - u"Вагиф Сәмәдоғлу,3\n" - u"章子怡,4\n" + "Արամ Խաչատրյան,1\n" + "Johann Strauß,2\n" + "Вагиф Сәмәдоғлу,3\n" + "章子怡,4\n" ) - uf = io.open(fn, encoding='utf-8', mode='rt', newline='') + uf = open(fn, encoding='utf-8', mode='rt', newline='') actual = uf.read() eq_(expect, actual) @@ -98,54 +94,54 @@ def test_tocsv(): def test_appendcsv(): data = ( - u"name,id\n" - u"Արամ Խաչատրյան,1\n" - u"Johann Strauß,2\n" - u"Вагиф Сәмәдоғлу,3\n" - u"章子怡,4\n" + "name,id\n" + "Արամ Խաչատրյան,1\n" + "Johann Strauß,2\n" + "Вагиф Сәмәдоғлу,3\n" + "章子怡,4\n" ) fn = NamedTemporaryFile().name - uf = io.open(fn, encoding='utf-8', mode='wt') + uf = open(fn, encoding='utf-8', mode='wt') uf.write(data) uf.close() - tbl = ((u'name', u'id'), - (u'ኃይሌ ገብረሥላሴ', 5), - (u'ედუარდ შევარდნაძე', 6)) + tbl = (('name', 'id'), + ('ኃይሌ ገብረሥላሴ', 5), + ('ედუარდ შევარდნაძე', 6)) appendcsv(tbl, fn, encoding='utf-8', lineterminator='\n') expect = ( - u"name,id\n" - u"Արամ Խաչատրյան,1\n" - u"Johann Strauß,2\n" - u"Вагиф Сәмәдоғлу,3\n" - u"章子怡,4\n" - u"ኃይሌ ገብረሥላሴ,5\n" - u"ედუარდ შევარდნაძე,6\n" + "name,id\n" + "Արամ Խաչատրյան,1\n" + "Johann Strauß,2\n" + "Вагиф Сәмәдоғлу,3\n" + "章子怡,4\n" + "ኃይሌ ገብረሥላሴ,5\n" + "ედუარდ შევარდნაძე,6\n" ) - uf = io.open(fn, encoding='utf-8', mode='rt') + uf = open(fn, encoding='utf-8', mode='rt') actual = uf.read() eq_(expect, actual) def test_tocsv_none(): - tbl = ((u'col1', u'colNone'), - (u'a', 1), - (u'b', None), - (u'c', None), - (u'd', 4)) + tbl = (('col1', 'colNone'), + ('a', 1), + ('b', None), + ('c', None), + ('d', 4)) fn = NamedTemporaryFile().name tocsv(tbl, fn, encoding='utf-8', lineterminator='\n') expect = ( - u'col1,colNone\n' - u'a,1\n' - u'b,\n' - u'c,\n' - u'd,4\n' + 'col1,colNone\n' + 'a,1\n' + 'b,\n' + 'c,\n' + 'd,4\n' ) - uf = io.open(fn, encoding='utf-8', mode='rt', newline='') + uf = open(fn, encoding='utf-8', mode='rt', newline='') actual = uf.read() eq_(expect, actual) diff --git a/petl/test/io/test_db.py b/petl/test/io/test_db.py index 216f1dc1..0f2f3ed4 100644 --- a/petl/test/io/test_db.py +++ b/petl/test/io/test_db.py @@ -1,7 +1,3 @@ -# -*- coding: utf-8 -*- -from __future__ import absolute_import, print_function, division - - import sqlite3 from tempfile import NamedTemporaryFile from petl.compat import next diff --git a/petl/test/io/test_db_create.py b/petl/test/io/test_db_create.py index c5c9befd..b84dba1a 100644 --- a/petl/test/io/test_db_create.py +++ b/petl/test/io/test_db_create.py @@ -1,7 +1,3 @@ -# -*- coding: utf-8 -*- -from __future__ import absolute_import, print_function, division - - import sys import logging from datetime import datetime, date diff --git a/petl/test/io/test_db_server.py b/petl/test/io/test_db_server.py index 8323ce00..e0f4bd72 100644 --- a/petl/test/io/test_db_server.py +++ b/petl/test/io/test_db_server.py @@ -1,5 +1,3 @@ -# -*- coding: utf-8 -*- -from __future__ import absolute_import, print_function, division import sys import logging @@ -86,11 +84,11 @@ def _test_with_schema(dbo, schema): def _test_unicode(dbo): - expect = ((u'name', u'id'), - (u'Արամ Խաչատրյան', 1), - (u'Johann Strauß', 2), - (u'Вагиф Сәмәдоғлу', 3), - (u'章子怡', 4), + expect = (('name', 'id'), + ('Արամ Խաչատրյան', 1), + ('Johann Strauß', 2), + ('Вагиф Сәмәдоғлу', 3), + ('章子怡', 4), ) actual = etl.fromdb(dbo, 'SELECT * FROM test_unicode') diff --git a/petl/test/io/test_html.py b/petl/test/io/test_html.py index 56ae016d..ec7a6075 100644 --- a/petl/test/io/test_html.py +++ b/petl/test/io/test_html.py @@ -1,7 +1,3 @@ -# -*- coding: utf-8 -*- -from __future__ import absolute_import, print_function, division - - from tempfile import NamedTemporaryFile import io from petl.test.helpers import eq_ @@ -22,31 +18,31 @@ def test_tohtml(): tohtml(table, f.name, encoding='ascii', lineterminator='\n') # check what it did - with io.open(f.name, mode='rt', encoding='ascii', newline='') as o: + with open(f.name, mode='rt', encoding='ascii', newline='') as o: actual = o.read() expect = ( - u"
%s%s{r}%s
\n" - u"\n" - u"\n" - u"\n" - u"\n" - u"\n" - u"\n" - u"\n" - u"\n" - u"\n" - u"\n" - u"\n" - u"\n" - u"\n" - u"\n" - u"\n" - u"\n" - u"\n" - u"\n" - u"\n" - u"\n" - u"
foobar
a1
b(1, 2)
cFalse
\n" + "\n" + "\n" + "\n" + "\n" + "\n" + "\n" + "\n" + "\n" + "\n" + "\n" + "\n" + "\n" + "\n" + "\n" + "\n" + "\n" + "\n" + "\n" + "\n" + "\n" + "\n" + "
foobar
a1
b(1, 2)
cFalse
\n" ) eq_(expect, actual) @@ -62,27 +58,27 @@ def test_tohtml_caption(): lineterminator='\n') # check what it did - with io.open(f.name, mode='rt', encoding='ascii', newline='') as o: + with open(f.name, mode='rt', encoding='ascii', newline='') as o: actual = o.read() expect = ( - u"\n" - u"\n" - u"\n" - u"\n" - u"\n" - u"\n" - u"\n" - u"\n" - u"\n" - u"\n" - u"\n" - u"\n" - u"\n" - u"\n" - u"\n" - u"\n" - u"\n" - u"\n" - u"
my table
foobar
a1
b(1, 2)
\n" + "\n" + "\n" + "\n" + "\n" + "\n" + "\n" + "\n" + "\n" + "\n" + "\n" + "\n" + "\n" + "\n" + "\n" + "\n" + "\n" + "\n" + "\n" + "
my table
foobar
a1
b(1, 2)
\n" ) eq_(expect, actual) diff --git a/petl/test/io/test_html_unicode.py b/petl/test/io/test_html_unicode.py index fcebc112..6c9c523e 100644 --- a/petl/test/io/test_html_unicode.py +++ b/petl/test/io/test_html_unicode.py @@ -1,7 +1,3 @@ -# -*- coding: utf-8 -*- -from __future__ import absolute_import, print_function, division - - import io from tempfile import NamedTemporaryFile from petl.test.helpers import eq_ @@ -13,43 +9,43 @@ def test_tohtml(): # exercise function - tbl = ((u'name', u'id'), - (u'Արամ Խաչատրյան', 1), - (u'Johann Strauß', 2), - (u'Вагиф Сәмәдоғлу', 3), - (u'章子怡', 4)) + tbl = (('name', 'id'), + ('Արամ Խաչատրյան', 1), + ('Johann Strauß', 2), + ('Вагиф Сәмәдоғлу', 3), + ('章子怡', 4)) fn = NamedTemporaryFile().name tohtml(tbl, fn, encoding='utf-8', lineterminator='\n') # check what it did - f = io.open(fn, mode='rt', encoding='utf-8', newline='') + f = open(fn, mode='rt', encoding='utf-8', newline='') actual = f.read() expect = ( - u"\n" - u"\n" - u"\n" - u"\n" - u"\n" - u"\n" - u"\n" - u"\n" - u"\n" - u"\n" - u"\n" - u"\n" - u"\n" - u"\n" - u"\n" - u"\n" - u"\n" - u"\n" - u"\n" - u"\n" - u"\n" - u"\n" - u"\n" - u"\n" - u"\n" - u"
nameid
Արամ Խաչատրյան1
Johann Strauß2
Вагиф Сәмәдоғлу3
章子怡4
\n" + "\n" + "\n" + "\n" + "\n" + "\n" + "\n" + "\n" + "\n" + "\n" + "\n" + "\n" + "\n" + "\n" + "\n" + "\n" + "\n" + "\n" + "\n" + "\n" + "\n" + "\n" + "\n" + "\n" + "\n" + "\n" + "
nameid
Արամ Խաչատրյան1
Johann Strauß2
Вагиф Сәмәдоғлу3
章子怡4
\n" ) eq_(expect, actual) diff --git a/petl/test/io/test_json.py b/petl/test/io/test_json.py index 5fa0b347..6555842c 100644 --- a/petl/test/io/test_json.py +++ b/petl/test/io/test_json.py @@ -1,7 +1,3 @@ -# -*- coding: utf-8 -*- -from __future__ import absolute_import, print_function, division - - from tempfile import NamedTemporaryFile import json diff --git a/petl/test/io/test_json_unicode.py b/petl/test/io/test_json_unicode.py index 5a02c1f8..af475a02 100644 --- a/petl/test/io/test_json_unicode.py +++ b/petl/test/io/test_json_unicode.py @@ -1,7 +1,3 @@ -# -*- coding: utf-8 -*- -from __future__ import absolute_import, print_function, division - - import json from tempfile import NamedTemporaryFile @@ -12,11 +8,11 @@ def test_json_unicode(): - tbl = ((u'id', u'name'), - (1, u'Արամ Խաչատրյան'), - (2, u'Johann Strauß'), - (3, u'Вагиф Сәмәдоғлу'), - (4, u'章子怡'), + tbl = (('id', 'name'), + (1, 'Արամ Խաչատրյան'), + (2, 'Johann Strauß'), + (3, 'Вагиф Сәмәдоғлу'), + (4, '章子怡'), ) fn = NamedTemporaryFile().name tojson(tbl, fn) diff --git a/petl/test/io/test_jsonl.py b/petl/test/io/test_jsonl.py index fc72a108..51da7d82 100644 --- a/petl/test/io/test_jsonl.py +++ b/petl/test/io/test_jsonl.py @@ -1,6 +1,3 @@ -# -*- coding: utf-8 -*- -from __future__ import absolute_import, print_function, division - from tempfile import NamedTemporaryFile from petl import fromjson diff --git a/petl/test/io/test_numpy.py b/petl/test/io/test_numpy.py index 32589076..1a939499 100644 --- a/petl/test/io/test_numpy.py +++ b/petl/test/io/test_numpy.py @@ -1,7 +1,3 @@ -# -*- coding: utf-8 -*- -from __future__ import absolute_import, print_function, division - - import sys diff --git a/petl/test/io/test_pandas.py b/petl/test/io/test_pandas.py index 5e54b48a..80f60136 100644 --- a/petl/test/io/test_pandas.py +++ b/petl/test/io/test_pandas.py @@ -1,7 +1,3 @@ -# -*- coding: utf-8 -*- -from __future__ import division, print_function, absolute_import - - import sys diff --git a/petl/test/io/test_pickle.py b/petl/test/io/test_pickle.py index 7f8f160f..bc1ca3c8 100644 --- a/petl/test/io/test_pickle.py +++ b/petl/test/io/test_pickle.py @@ -1,7 +1,3 @@ -# -*- coding: utf-8 -*- -from __future__ import absolute_import, print_function, division - - from tempfile import NamedTemporaryFile from petl.compat import pickle diff --git a/petl/test/io/test_pytables.py b/petl/test/io/test_pytables.py index a3fd13d7..77610757 100644 --- a/petl/test/io/test_pytables.py +++ b/petl/test/io/test_pytables.py @@ -1,7 +1,3 @@ -# -*- coding: utf-8 -*- -from __future__ import division, print_function, absolute_import - - import sys from itertools import chain from tempfile import NamedTemporaryFile diff --git a/petl/test/io/test_remotes.py b/petl/test/io/test_remotes.py index 34e1f428..e10d8bc9 100644 --- a/petl/test/io/test_remotes.py +++ b/petl/test/io/test_remotes.py @@ -1,6 +1,3 @@ -# -*- coding: utf-8 -*- -from __future__ import absolute_import, print_function, division - import sys import os from importlib import import_module @@ -71,7 +68,7 @@ def _write_read_from_env_matching(prefix): q = 0 for variable, base_url in os.environ.items(): if variable.upper().startswith(prefix.upper()): - fmsg = "\n {}: {} -> ".format(variable, base_url) + fmsg = f"\n {variable}: {base_url} -> " print(fmsg, file=sys.stderr, end="") _write_read_into_url(base_url) print("DONE ", file=sys.stderr, end="") @@ -183,19 +180,19 @@ def _is_installed(package_name, message=None): # region Mockup data _table = ( - (u"name", u"friends", u"age"), - (u"Bob", "42", "33"), - (u"Jim", "13", "69"), - (u"Joe", "86", "17"), - (u"Ted", "23", "51"), + ("name", "friends", "age"), + ("Bob", "42", "33"), + ("Jim", "13", "69"), + ("Joe", "86", "17"), + ("Ted", "23", "51"), ) _table2 = ( - (u"name", u"friends", u"age"), - (u"Giannis", "31", "12"), - (u"James", "38", "8"), - (u"Stephen", "28", "4"), - (u"Jason", "23", "12"), + ("name", "friends", "age"), + ("Giannis", "31", "12"), + ("James", "38", "8"), + ("Stephen", "28", "4"), + ("Jason", "23", "12"), ) # endregion diff --git a/petl/test/io/test_sources.py b/petl/test/io/test_sources.py index f1e1bafb..774fb373 100644 --- a/petl/test/io/test_sources.py +++ b/petl/test/io/test_sources.py @@ -1,7 +1,3 @@ -# -*- coding: utf-8 -*- -from __future__ import absolute_import, print_function, division - - import gzip import bz2 import zipfile @@ -95,8 +91,8 @@ def test_stdoutsource(): def test_stdoutsource_unicode(): tbl = [('foo', 'bar'), - (u'Արամ Խաչատրյան', 1), - (u'Johann Strauß', 2)] + ('Արամ Խաչատրյան', 1), + ('Johann Strauß', 2)] etl.tocsv(tbl, StdoutSource(), encoding='utf-8') etl.tohtml(tbl, StdoutSource(), encoding='utf-8') etl.topickle(tbl, StdoutSource()) diff --git a/petl/test/io/test_sqlite3.py b/petl/test/io/test_sqlite3.py index 5e9e72b4..4dc563a8 100644 --- a/petl/test/io/test_sqlite3.py +++ b/petl/test/io/test_sqlite3.py @@ -1,7 +1,3 @@ -# -*- coding: utf-8 -*- -from __future__ import absolute_import, print_function, division - - from tempfile import NamedTemporaryFile import sqlite3 diff --git a/petl/test/io/test_tees.py b/petl/test/io/test_tees.py index 388349f0..50f7924f 100644 --- a/petl/test/io/test_tees.py +++ b/petl/test/io/test_tees.py @@ -1,7 +1,3 @@ -# -*- coding: utf-8 -*- -from __future__ import absolute_import, print_function, division - - from tempfile import NamedTemporaryFile @@ -93,11 +89,11 @@ def test_teecsv_write_header(): def test_teecsv_unicode(): - t1 = ((u'name', u'id'), - (u'Արամ Խաչատրյան', 1), - (u'Johann Strauß', 2), - (u'Вагиф Сәмәдоғлу', 3), - (u'章子怡', 4)) + t1 = (('name', 'id'), + ('Արամ Խաչատրյան', 1), + ('Johann Strauß', 2), + ('Вагиф Сәмәдоғлу', 3), + ('章子怡', 4)) f1 = NamedTemporaryFile(delete=False) f2 = NamedTemporaryFile(delete=False) @@ -116,11 +112,11 @@ def test_teecsv_unicode(): def test_teecsv_unicode_write_header(): - t1 = ((u'name', u'id'), - (u'Արամ Խաչատրյան', u'1'), - (u'Johann Strauß', u'2'), - (u'Вагиф Сәмәдоғлу', u'3'), - (u'章子怡', u'4')) + t1 = (('name', 'id'), + ('Արամ Խաչատրյան', '1'), + ('Johann Strauß', '2'), + ('Вагиф Сәмәдоғлу', '3'), + ('章子怡', '4')) f1 = NamedTemporaryFile(delete=False) f2 = NamedTemporaryFile(delete=False) @@ -140,11 +136,11 @@ def test_teecsv_unicode_write_header(): def test_teetsv_unicode(): - t1 = ((u'name', u'id'), - (u'Արամ Խաչատրյան', 1), - (u'Johann Strauß', 2), - (u'Вагиф Сәмәдоғлу', 3), - (u'章子怡', 4),) + t1 = (('name', 'id'), + ('Արամ Խաչատրյան', 1), + ('Johann Strauß', 2), + ('Вагиф Сәмәдоғлу', 3), + ('章子怡', 4),) f1 = NamedTemporaryFile(delete=False) f2 = NamedTemporaryFile(delete=False) @@ -191,17 +187,17 @@ def test_teetext(): def test_teetext_unicode(): - t1 = ((u'foo', u'bar'), - (u'Արամ Խաչատրյան', 2), - (u'Johann Strauß', 1), - (u'Вагиф Сәмәдоғлу', 3)) + t1 = (('foo', 'bar'), + ('Արամ Խաչատրյան', 2), + ('Johann Strauß', 1), + ('Вагиф Сәмәдоғлу', 3)) f1 = NamedTemporaryFile(delete=False) f2 = NamedTemporaryFile(delete=False) - prologue = u'foo,bar\n' - template = u'{foo},{bar}\n' - epilogue = u'章子怡,4' + prologue = 'foo,bar\n' + template = '{foo},{bar}\n' + epilogue = '章子怡,4' (etl .wrap(t1) .teetext(f1.name, @@ -212,7 +208,7 @@ def test_teetext_unicode(): .selectgt('bar', 1) .topickle(f2.name)) - ieq(t1 + ((u'章子怡', 4),), + ieq(t1 + (('章子怡', 4),), etl.fromcsv(f1.name, encoding='utf-8').convertnumbers()) ieq(etl.wrap(t1).selectgt('bar', 1), etl.frompickle(f2.name)) @@ -235,10 +231,10 @@ def test_teehtml(): def test_teehtml_unicode(): - t1 = ((u'foo', u'bar'), - (u'Արամ Խաչատրյան', 2), - (u'Johann Strauß', 1), - (u'Вагиф Сәмәдоғлу', 3)) + t1 = (('foo', 'bar'), + ('Արամ Խաչատրյան', 2), + ('Johann Strauß', 1), + ('Вагиф Сәмәдоғлу', 3)) f1 = NamedTemporaryFile(delete=False) f2 = NamedTemporaryFile(delete=False) diff --git a/petl/test/io/test_text.py b/petl/test/io/test_text.py index 4ffdf19d..a0f93bb5 100644 --- a/petl/test/io/test_text.py +++ b/petl/test/io/test_text.py @@ -1,7 +1,3 @@ -# -*- coding: utf-8 -*- -from __future__ import absolute_import, print_function, division - - from tempfile import NamedTemporaryFile import gzip import os @@ -78,7 +74,7 @@ def test_totext(): prologue=prologue, epilogue=epilogue) # check what it did - with io.open(f.name, mode='rt', encoding='ascii', newline='') as o: + with open(f.name, mode='rt', encoding='ascii', newline='') as o: actual = o.read() expect = ( "{| class='wikitable'\n" diff --git a/petl/test/io/test_text_unicode.py b/petl/test/io/test_text_unicode.py index 6721a854..be65ef86 100644 --- a/petl/test/io/test_text_unicode.py +++ b/petl/test/io/test_text_unicode.py @@ -1,7 +1,3 @@ -# -*- coding: utf-8 -*- -from __future__ import absolute_import, print_function, division - - import io from tempfile import NamedTemporaryFile from petl.test.helpers import ieq, eq_ @@ -12,24 +8,24 @@ def test_fromtext(): data = ( - u"name,id\n" - u"Արամ Խաչատրյան,1\n" - u"Johann Strauß,2\n" - u"Вагиф Сәмәдоғлу,3\n" - u"章子怡,4\n" + "name,id\n" + "Արամ Խաչատրյան,1\n" + "Johann Strauß,2\n" + "Вагиф Сәмәдоғлу,3\n" + "章子怡,4\n" ) fn = NamedTemporaryFile().name - f = io.open(fn, encoding='utf-8', mode='wt') + f = open(fn, encoding='utf-8', mode='wt') f.write(data) f.close() actual = fromtext(fn, encoding='utf-8') - expect = ((u'lines',), - (u'name,id',), - (u'Արամ Խաչատրյան,1',), - (u'Johann Strauß,2',), - (u'Вагиф Сәмәдоғлу,3',), - (u'章子怡,4',), + expect = (('lines',), + ('name,id',), + ('Արամ Խաչատրյան,1',), + ('Johann Strauß,2',), + ('Вагиф Сәмәдоғлу,3',), + ('章子怡,4',), ) ieq(expect, actual) ieq(expect, actual) # verify can iterate twice @@ -38,48 +34,48 @@ def test_fromtext(): def test_totext(): # exercise function - tbl = ((u'name', u'id'), - (u'Արամ Խաչատրյան', 1), - (u'Johann Strauß', 2), - (u'Вагиф Сәмәдоғлу', 3), - (u'章子怡', 4), + tbl = (('name', 'id'), + ('Արամ Խաչատրյան', 1), + ('Johann Strauß', 2), + ('Вагиф Сәмәдоғлу', 3), + ('章子怡', 4), ) prologue = ( - u"{| class='wikitable'\n" - u"|-\n" - u"! name\n" - u"! id\n" + "{| class='wikitable'\n" + "|-\n" + "! name\n" + "! id\n" ) template = ( - u"|-\n" - u"| {name}\n" - u"| {id}\n" + "|-\n" + "| {name}\n" + "| {id}\n" ) - epilogue = u"|}\n" + epilogue = "|}\n" fn = NamedTemporaryFile().name totext(tbl, fn, template=template, prologue=prologue, epilogue=epilogue, encoding='utf-8') # check what it did - f = io.open(fn, encoding='utf-8', mode='rt') + f = open(fn, encoding='utf-8', mode='rt') actual = f.read() expect = ( - u"{| class='wikitable'\n" - u"|-\n" - u"! name\n" - u"! id\n" - u"|-\n" - u"| Արամ Խաչատրյան\n" - u"| 1\n" - u"|-\n" - u"| Johann Strauß\n" - u"| 2\n" - u"|-\n" - u"| Вагиф Сәмәдоғлу\n" - u"| 3\n" - u"|-\n" - u"| 章子怡\n" - u"| 4\n" - u"|}\n" + "{| class='wikitable'\n" + "|-\n" + "! name\n" + "! id\n" + "|-\n" + "| Արամ Խաչատրյան\n" + "| 1\n" + "|-\n" + "| Johann Strauß\n" + "| 2\n" + "|-\n" + "| Вагиф Сәмәдоғлу\n" + "| 3\n" + "|-\n" + "| 章子怡\n" + "| 4\n" + "|}\n" ) eq_(expect, actual) diff --git a/petl/test/io/test_whoosh.py b/petl/test/io/test_whoosh.py index 8b6b9108..74a0e3ad 100644 --- a/petl/test/io/test_whoosh.py +++ b/petl/test/io/test_whoosh.py @@ -1,7 +1,3 @@ -# -*- coding: utf-8 -*- -from __future__ import absolute_import, print_function, division - - import sys import os import tempfile @@ -33,16 +29,16 @@ def test_fromindex_dirname(): ix = create_in(dirname, schema) writer = ix.writer() - writer.add_document(title=u"First document", path=u"/a", - content=u"This is the first document we've added!") - writer.add_document(title=u"Second document", path=u"/b", - content=u"The second one is even more interesting!") + writer.add_document(title="First document", path="/a", + content="This is the first document we've added!") + writer.add_document(title="Second document", path="/b", + content="The second one is even more interesting!") writer.commit() # N.B., fields get sorted - expect = ((u'path', u'title'), - (u'/a', u'First document'), - (u'/b', u'Second document')) + expect = (('path', 'title'), + ('/a', 'First document'), + ('/b', 'Second document')) actual = fromtextindex(dirname) ieq(expect, actual) @@ -55,16 +51,16 @@ def test_fromindex_index(): ix = create_in(dirname, schema) writer = ix.writer() - writer.add_document(title=u"First document", path=u"/a", - content=u"This is the first document we've added!") - writer.add_document(title=u"Second document", path=u"/b", - content=u"The second one is even more interesting!") + writer.add_document(title="First document", path="/a", + content="This is the first document we've added!") + writer.add_document(title="Second document", path="/b", + content="The second one is even more interesting!") writer.commit() # N.B., fields get sorted - expect = ((u'path', u'title'), - (u'/a', u'First document'), - (u'/b', u'Second document')) + expect = (('path', 'title'), + ('/a', 'First document'), + ('/b', 'Second document')) actual = fromtextindex(ix) ieq(expect, actual) @@ -76,16 +72,16 @@ def test_fromindex_docnum_field(): ix = create_in(dirname, schema) writer = ix.writer() - writer.add_document(title=u"First document", path=u"/a", - content=u"This is the first document we've added!") - writer.add_document(title=u"Second document", path=u"/b", - content=u"The second one is even more interesting!") + writer.add_document(title="First document", path="/a", + content="This is the first document we've added!") + writer.add_document(title="Second document", path="/b", + content="The second one is even more interesting!") writer.commit() # N.B., fields get sorted - expect = ((u'docnum', u'path', u'title'), - (0, u'/a', u'First document'), - (1, u'/b', u'Second document')) + expect = (('docnum', 'path', 'title'), + (0, '/a', 'First document'), + (1, '/b', 'Second document')) actual = fromtextindex(dirname, docnum_field='docnum') ieq(expect, actual) @@ -95,9 +91,9 @@ def test_toindex_dirname(): # name fields in ascending order as whoosh sorts fields on the way out tbl = (('f0', 'f1', 'f2', 'f3', 'f4'), - (u'AAA', 12, 4.3, True, datetime.datetime.now()), - (u'BBB', 6, 3.4, False, datetime.datetime(1900, 1, 31)), - (u'CCC', 42, 7.8, True, datetime.datetime(2100, 12, 25))) + ('AAA', 12, 4.3, True, datetime.datetime.now()), + ('BBB', 6, 3.4, False, datetime.datetime(1900, 1, 31)), + ('CCC', 42, 7.8, True, datetime.datetime(2100, 12, 25))) schema = Schema(f0=TEXT(stored=True), f1=NUMERIC(int, stored=True), @@ -116,9 +112,9 @@ def test_toindex_index(): # name fields in ascending order as whoosh sorts fields on the way out tbl = (('f0', 'f1', 'f2', 'f3', 'f4'), - (u'AAA', 12, 4.3, True, datetime.datetime.now()), - (u'BBB', 6, 3.4, False, datetime.datetime(1900, 1, 31)), - (u'CCC', 42, 7.8, True, datetime.datetime(2100, 12, 25))) + ('AAA', 12, 4.3, True, datetime.datetime.now()), + ('BBB', 6, 3.4, False, datetime.datetime(1900, 1, 31)), + ('CCC', 42, 7.8, True, datetime.datetime(2100, 12, 25))) schema = Schema(f0=TEXT(stored=True), f1=NUMERIC(int, stored=True), @@ -138,9 +134,9 @@ def test_appendindex_dirname(): # name fields in ascending order as whoosh sorts fields on the way out tbl = (('f0', 'f1', 'f2', 'f3', 'f4'), - (u'AAA', 12, 4.3, True, datetime.datetime.now()), - (u'BBB', 6, 3.4, False, datetime.datetime(1900, 1, 31)), - (u'CCC', 42, 7.8, True, datetime.datetime(2100, 12, 25))) + ('AAA', 12, 4.3, True, datetime.datetime.now()), + ('BBB', 6, 3.4, False, datetime.datetime(1900, 1, 31)), + ('CCC', 42, 7.8, True, datetime.datetime(2100, 12, 25))) schema = Schema(f0=TEXT(stored=True), f1=NUMERIC(int, stored=True), @@ -161,9 +157,9 @@ def test_appendindex_index(): # name fields in ascending order as whoosh sorts fields on the way out tbl = (('f0', 'f1', 'f2', 'f3', 'f4'), - (u'AAA', 12, 4.3, True, datetime.datetime.now()), - (u'BBB', 6, 3.4, False, datetime.datetime(1900, 1, 31)), - (u'CCC', 42, 7.8, True, datetime.datetime(2100, 12, 25))) + ('AAA', 12, 4.3, True, datetime.datetime.now()), + ('BBB', 6, 3.4, False, datetime.datetime(1900, 1, 31)), + ('CCC', 42, 7.8, True, datetime.datetime(2100, 12, 25))) schema = Schema(f0=TEXT(stored=True), f1=NUMERIC(int, stored=True), @@ -187,25 +183,25 @@ def test_searchindex(): ix = create_in(dirname, schema) writer = ix.writer() - writer.add_document(title=u"Oranges", path=u"/a", - content=u"This is the first document we've added!") - writer.add_document(title=u"Apples", path=u"/b", - content=u"The second document is even more " - u"interesting!") + writer.add_document(title="Oranges", path="/a", + content="This is the first document we've added!") + writer.add_document(title="Apples", path="/b", + content="The second document is even more " + "interesting!") writer.commit() # N.B., fields get sorted - expect = ((u'path', u'title'), - (u'/a', u'Oranges')) + expect = (('path', 'title'), + ('/a', 'Oranges')) # N.B., by default whoosh does not do stemming actual = searchtextindex(dirname, 'oranges') ieq(expect, actual) actual = searchtextindex(dirname, 'add*') ieq(expect, actual) - expect = ((u'path', u'title'), - (u'/a', u'Oranges'), - (u'/b', u'Apples')) + expect = (('path', 'title'), + ('/a', 'Oranges'), + ('/b', 'Apples')) actual = searchtextindex(dirname, 'doc*') ieq(expect, actual) @@ -217,16 +213,16 @@ def test_integration(): ix = create_in(dirname, schema) writer = ix.writer() - writer.add_document(title=u"First document", path=u"/a", - content=u"This is the first document we've added!") - writer.add_document(title=u"Second document", path=u"/b", - content=u"The second one is even more interesting!") + writer.add_document(title="First document", path="/a", + content="This is the first document we've added!") + writer.add_document(title="Second document", path="/b", + content="The second one is even more interesting!") writer.commit() # N.B., fields get sorted - expect = ((u'path', u'title'), - (u'/a', u'first document'), - (u'/b', u'second document')) + expect = (('path', 'title'), + ('/a', 'first document'), + ('/b', 'second document')) actual = etl.fromtextindex(dirname).convert('title', 'lower') ieq(expect, actual) diff --git a/petl/test/io/test_xls.py b/petl/test/io/test_xls.py index 4fee539d..7f407043 100644 --- a/petl/test/io/test_xls.py +++ b/petl/test/io/test_xls.py @@ -1,7 +1,3 @@ -# -*- coding: utf-8 -*- -from __future__ import division, print_function, absolute_import - - import sys from datetime import datetime from tempfile import NamedTemporaryFile @@ -37,7 +33,7 @@ def test_fromxls(): ('A', 1), ('B', 2), ('C', 2), - (u'é', datetime(2012, 1, 1))) + ('é', datetime(2012, 1, 1))) ieq(expect, tbl) ieq(expect, tbl) @@ -50,7 +46,7 @@ def test_fromxls_nosheet(): ('A', 1), ('B', 2), ('C', 2), - (u'é', datetime(2012, 1, 1))) + ('é', datetime(2012, 1, 1))) ieq(expect, tbl) ieq(expect, tbl) @@ -63,7 +59,7 @@ def test_fromxls_use_view(): ('A', 1), ('B', 2), ('C', 2), - (u'é', 40909.0)) + ('é', 40909.0)) ieq(expect, tbl) ieq(expect, tbl) @@ -81,8 +77,8 @@ def test_toxls(): def test_toxls_date(): expect = (('foo', 'bar'), - (u'é', datetime(2012, 1, 1)), - (u'éé', datetime(2013, 2, 22))) + ('é', datetime(2012, 1, 1)), + ('éé', datetime(2013, 2, 22))) f = NamedTemporaryFile(delete=False) f.close() toxls(expect, f.name, 'Sheet1', diff --git a/petl/test/io/test_xlsx.py b/petl/test/io/test_xlsx.py index 1de14d95..fb5ea831 100644 --- a/petl/test/io/test_xlsx.py +++ b/petl/test/io/test_xlsx.py @@ -1,7 +1,3 @@ -# -*- coding: utf-8 -*- -from __future__ import absolute_import, print_function, division - - import sys from datetime import datetime from tempfile import NamedTemporaryFile @@ -35,7 +31,7 @@ def test_fromxlsx(): ('A', 1), ('B', 2), ('C', 2), - (u'é', datetime(2012, 1, 1))) + ('é', datetime(2012, 1, 1))) ieq(expect, tbl) ieq(expect, tbl) @@ -48,7 +44,7 @@ def test_fromxlsx_read_only(): ('A', 1), ('B', 2), ('C', 2), - (u'é', datetime(2012, 1, 1))) + ('é', datetime(2012, 1, 1))) ieq(expect, tbl) ieq(expect, tbl) @@ -61,7 +57,7 @@ def test_fromxlsx_nosheet(): ('A', 1), ('B', 2), ('C', 2), - (u'é', datetime(2012, 1, 1))) + ('é', datetime(2012, 1, 1))) ieq(expect, tbl) ieq(expect, tbl) @@ -74,7 +70,7 @@ def test_fromxlsx_range(): ('A', 1), ('B', 2), ('C', 2), - (u'é', datetime(2012, 1, 1))) + ('é', datetime(2012, 1, 1))) ieq(expect, tbl) ieq(expect, tbl) @@ -97,7 +93,7 @@ def test_toxlsx_appendxlsx(): ('A', 1), ('B', 2), ('C', 2), - (u'é', datetime(2012, 1, 1))) + ('é', datetime(2012, 1, 1))) f = NamedTemporaryFile(delete=True, suffix='.xlsx') f.close() @@ -116,7 +112,7 @@ def test_toxlsx_nosheet(): ('A', 1), ('B', 2), ('C', 2), - (u'é', datetime(2012, 1, 1))) + ('é', datetime(2012, 1, 1))) f = NamedTemporaryFile(delete=True, suffix='.xlsx') f.close() toxlsx(tbl, f.name) @@ -128,7 +124,7 @@ def test_integration(): ('A', 1), ('B', 2), ('C', 2), - (u'é', datetime(2012, 1, 1))) + ('é', datetime(2012, 1, 1))) f = NamedTemporaryFile(delete=True, suffix='.xlsx') f.close() tbl = etl.wrap(tbl) @@ -144,7 +140,7 @@ def test_toxlsx_overwrite(): ('A', 1), ('B', 2), ('C', 2), - (u'é', datetime(2012, 1, 1))) + ('é', datetime(2012, 1, 1))) f = NamedTemporaryFile(delete=False, suffix='.xlsx') f.close() @@ -157,7 +153,7 @@ def test_toxlsx_replace_file(): ('A', 1), ('B', 2), ('C', 2), - (u'é', datetime(2012, 1, 1))) + ('é', datetime(2012, 1, 1))) f = NamedTemporaryFile(delete=True, suffix='.xlsx') f.close() @@ -171,7 +167,7 @@ def test_toxlsx_replace_sheet(): ('A', 1), ('B', 2), ('C', 2), - (u'é', datetime(2012, 1, 1))) + ('é', datetime(2012, 1, 1))) f = NamedTemporaryFile(delete=True, suffix='.xlsx') f.close() @@ -185,7 +181,7 @@ def test_toxlsx_replace_sheet_nofile(): ('A', 1), ('B', 2), ('C', 2), - (u'é', datetime(2012, 1, 1))) + ('é', datetime(2012, 1, 1))) f = NamedTemporaryFile(delete=True, suffix='.xlsx') f.close() @@ -198,7 +194,7 @@ def test_toxlsx_add_nosheet(): ('A', 1), ('B', 2), ('C', 2), - (u'é', datetime(2012, 1, 1))) + ('é', datetime(2012, 1, 1))) f = NamedTemporaryFile(delete=True, suffix='.xlsx') f.close() @@ -212,7 +208,7 @@ def test_toxlsx_add_sheet_nomatch(): ('A', 1), ('B', 2), ('C', 2), - (u'é', datetime(2012, 1, 1))) + ('é', datetime(2012, 1, 1))) f = NamedTemporaryFile(delete=True, suffix='.xlsx') f.close() @@ -226,7 +222,7 @@ def test_toxlsx_add_sheet_match(): ('A', 1), ('B', 2), ('C', 2), - (u'é', datetime(2012, 1, 1))) + ('é', datetime(2012, 1, 1))) f = NamedTemporaryFile(delete=True, suffix='.xlsx') f.close() diff --git a/petl/test/io/test_xml.py b/petl/test/io/test_xml.py index b569e01e..50953c42 100644 --- a/petl/test/io/test_xml.py +++ b/petl/test/io/test_xml.py @@ -1,6 +1,3 @@ -# -*- coding: utf-8 -*- -from __future__ import absolute_import, print_function, division - import sys from collections import OrderedDict from tempfile import NamedTemporaryFile @@ -216,7 +213,7 @@ def _write_temp_file(content, out=None): f.close() if out is not None: outf = sys.stderr if out else sys.stdout - print('TEST %s:\n%s' % (res, content), file=outf) + print(f'TEST {res}:\n{content}', file=outf) return res diff --git a/petl/test/test_comparison.py b/petl/test/test_comparison.py index 94bf3637..caecb1de 100644 --- a/petl/test/test_comparison.py +++ b/petl/test/test_comparison.py @@ -1,6 +1,3 @@ -from __future__ import print_function, division, absolute_import - - from datetime import datetime @@ -53,16 +50,16 @@ def test_comparable(): eq_(e, a) # text - d = [u'b', u'ccc', u'aa'] + d = ['b', 'ccc', 'aa'] a = sorted(d, key=Comparable) - e = [u'aa', u'b', u'ccc'] + e = ['aa', 'b', 'ccc'] eq_(e, a) # mixed bytes and text - d = [u'b', b'ccc', b'aa'] + d = ['b', b'ccc', b'aa'] a = sorted(d, key=Comparable) # N.B., expect always bytes < unicode - e = [b'aa', b'ccc', u'b'] + e = [b'aa', b'ccc', 'b'] eq_(e, a) # mixed bytes and None @@ -72,22 +69,22 @@ def test_comparable(): eq_(e, a) # mixed text and None - d = [u'b', u'ccc', None, u'aa'] + d = ['b', 'ccc', None, 'aa'] a = sorted(d, key=Comparable) - e = [None, u'aa', u'b', u'ccc'] + e = [None, 'aa', 'b', 'ccc'] eq_(e, a) # mixed bytes, text and None - d = [u'b', b'ccc', None, b'aa'] + d = ['b', b'ccc', None, b'aa'] a = sorted(d, key=Comparable) # N.B., expect always bytes < unicode - e = [None, b'aa', b'ccc', u'b'] + e = [None, b'aa', b'ccc', 'b'] eq_(e, a) # mixed bytes, text, numbers and None - d = [u'b', True, b'ccc', False, None, b'aa', -1, 3.4] + d = ['b', True, b'ccc', False, None, b'aa', -1, 3.4] a = sorted(d, key=Comparable) - e = [None, -1, False, True, 3.4, b'aa', b'ccc', u'b'] + e = [None, -1, False, True, 3.4, b'aa', b'ccc', 'b'] eq_(e, a) @@ -108,7 +105,7 @@ def test_comparable_datetime(): eq_(e, a) # mixed datetime, numbers, bytes, text and None - d = [dt(hour=12), None, dt(hour=3), u'b', True, b'ccc', False, b'aa', -1, + d = [dt(hour=12), None, dt(hour=3), 'b', True, b'ccc', False, b'aa', -1, 3.4] a = sorted(d, key=Comparable) # N.B., because bytes and unicode type names have changed in PY3, @@ -116,7 +113,7 @@ def test_comparable_datetime(): # versions, i.e., 'datetime' < 'str' < 'unicode' rather than 'bytes' < # 'datetime' < 'str' e = [None, -1, False, True, 3.4, dt(hour=3), dt(hour=12), b'aa', b'ccc', - u'b'] + 'b'] eq_(e, a) diff --git a/petl/test/test_fluent.py b/petl/test/test_fluent.py index 9fac4796..24b904a8 100644 --- a/petl/test/test_fluent.py +++ b/petl/test/test_fluent.py @@ -1,6 +1,3 @@ -from __future__ import absolute_import, print_function, division - - from tempfile import NamedTemporaryFile import csv from petl.compat import PY2 diff --git a/petl/test/test_interactive.py b/petl/test/test_interactive.py index 25f0ba97..316d8c86 100644 --- a/petl/test/test_interactive.py +++ b/petl/test/test_interactive.py @@ -1,6 +1,3 @@ -from __future__ import absolute_import, print_function, division - - import petl as etl from petl.test.helpers import eq_ diff --git a/petl/test/transform/__init__.py b/petl/test/transform/__init__.py index f298a611..e69de29b 100644 --- a/petl/test/transform/__init__.py +++ b/petl/test/transform/__init__.py @@ -1 +0,0 @@ -from __future__ import absolute_import, print_function, division diff --git a/petl/test/transform/test_basics.py b/petl/test/transform/test_basics.py index 17412eff..d2c4cf36 100644 --- a/petl/test/transform/test_basics.py +++ b/petl/test/transform/test_basics.py @@ -1,6 +1,3 @@ -from __future__ import absolute_import, print_function, division - - from petl.test.helpers import ieq from petl.util import expr, empty, coalesce from petl.transform.basics import cut, cat, addfield, rowslice, head, tail, \ @@ -13,7 +10,7 @@ def test_cut(): table = (('foo', 'bar', 'baz'), ('A', 1, 2), ('B', '2', '3.4'), - (u'B', u'3', u'7.8', True), + ('B', '3', '7.8', True), ('D', 'xyz', 9.0), ('E', None)) @@ -21,7 +18,7 @@ def test_cut(): expectation = (('foo',), ('A',), ('B',), - (u'B',), + ('B',), ('D',), ('E',)) ieq(expectation, cut1) @@ -30,7 +27,7 @@ def test_cut(): expectation = (('foo', 'baz'), ('A', 2), ('B', '3.4'), - (u'B', u'7.8'), + ('B', '7.8'), ('D', 9.0), ('E', None)) ieq(expectation, cut2) @@ -39,7 +36,7 @@ def test_cut(): expectation = (('foo', 'baz'), ('A', 2), ('B', '3.4'), - (u'B', u'7.8'), + ('B', '7.8'), ('D', 9.0), ('E', None)) ieq(expectation, cut3) @@ -48,7 +45,7 @@ def test_cut(): expectation = (('bar', 'foo'), (1, 'A'), ('2', 'B'), - (u'3', u'B'), + ('3', 'B'), ('xyz', 'D'), (None, 'E')) ieq(expectation, cut4) @@ -57,7 +54,7 @@ def test_cut(): expectation = (('foo', 'baz'), ('A', 2), ('B', '3.4'), - (u'B', u'7.8'), + ('B', '7.8'), ('D', 9.0), ('E', None)) ieq(expectation, cut5) @@ -75,7 +72,7 @@ def test_cutout(): table = (('foo', 'bar', 'baz'), ('A', 1, 2), ('B', '2', '3.4'), - (u'B', u'3', u'7.8', True), + ('B', '3', '7.8', True), ('D', 'xyz', 9.0), ('E', None)) @@ -83,7 +80,7 @@ def test_cutout(): expectation = (('foo',), ('A',), ('B',), - (u'B',), + ('B',), ('D',), ('E',)) ieq(expectation, cut1) @@ -92,7 +89,7 @@ def test_cutout(): expectation = (('foo', 'baz'), ('A', 2), ('B', '3.4'), - (u'B', u'7.8'), + ('B', '7.8'), ('D', 9.0), ('E', None)) ieq(expectation, cut2) @@ -101,7 +98,7 @@ def test_cutout(): expectation = (('foo', 'baz'), ('A', 2), ('B', '3.4'), - (u'B', u'7.8'), + ('B', '7.8'), ('D', 9.0), ('E', None)) ieq(expectation, cut3) @@ -130,7 +127,7 @@ def test_cat(): table3 = (('foo', 'bar', 'baz'), ('A', 1, 2), ('B', '2', '3.4'), - (u'B', u'3', u'7.8', True), + ('B', '3', '7.8', True), ('D', 'xyz', 9.0), ('E', None)) @@ -138,7 +135,7 @@ def test_cat(): expectation = (('foo', 'bar', 'baz'), ('A', 1, 2), ('B', '2', '3.4'), - (u'B', u'3', u'7.8'), + ('B', '3', '7.8'), ('D', 'xyz', 9.0), ('E', None, None)) ieq(expectation, cat3) @@ -152,7 +149,7 @@ def test_cat(): (None, 'D', False), ('A', 1, 2), ('B', '2', '3.4'), - (u'B', u'3', u'7.8'), + ('B', '3', '7.8'), ('D', 'xyz', 9.0), ('E', None, None)) ieq(expectation, cat4) @@ -266,7 +263,7 @@ def test_addfield(): ieq(expectation, result) ieq(expectation, result) - result = addfield(table, 'baz', lambda row: '%s,%s' % (row.foo, row.bar)) + result = addfield(table, 'baz', lambda row: f'{row.foo},{row.bar}') expectation = (('foo', 'bar', 'baz'), ('M', 12, 'M,12'), ('F', 34, 'F,34'), @@ -366,7 +363,7 @@ def test_addfields(): ('-', 56)) result = addfields(table, [('baz', 42), - ('qux', lambda row: '%s,%s' % (row.foo, row.bar)), + ('qux', lambda row: f'{row.foo},{row.bar}'), ('fiz', lambda rec: rec['bar'] * 2, 0)]) expectation = (('fiz', 'foo', 'bar', 'baz', 'qux'), (24, 'M', 12, 42, 'M,12'), @@ -408,7 +405,7 @@ def test_rowslice(): table = (('foo', 'bar', 'baz'), ('A', 1, 2), ('B', '2', '3.4'), - (u'B', u'3', u'7.8', True), + ('B', '3', '7.8', True), ('D', 'xyz', 9.0), ('E', None)) diff --git a/petl/test/transform/test_conversions.py b/petl/test/transform/test_conversions.py index 928f31b4..b0b12c43 100644 --- a/petl/test/transform/test_conversions.py +++ b/petl/test/transform/test_conversions.py @@ -1,6 +1,3 @@ -from __future__ import absolute_import, print_function, division - - from petl.test.failonerror import test_failonerror from petl.test.helpers import ieq from petl.transform.conversions import convert, convertall, convertnumbers, \ @@ -14,7 +11,7 @@ def test_convert(): table1 = (('foo', 'bar', 'baz'), ('A', 1, 2), ('B', '2', '3.4'), - (u'B', u'3', u'7.8', True), + ('B', '3', '7.8', True), ('D', 'xyz', 9.0), ('E', None)) @@ -23,7 +20,7 @@ def test_convert(): expect2 = (('foo', 'bar', 'baz'), ('a', 1, 2), ('b', '2', '3.4'), - (u'b', u'3', u'7.8', True), + ('b', '3', '7.8', True), ('d', 'xyz', 9.0), ('e', None)) ieq(expect2, table2) @@ -39,7 +36,7 @@ def test_convert(): expect4 = (('foo', 'bar', 'baz'), ('A', 1, 2), ('BB', '2', '3.4'), - (u'BB', u'3', u'7.8', True), + ('BB', '3', '7.8', True), ('D', 'xyz', 9.0), ('E', None)) ieq(expect4, table4) @@ -49,7 +46,7 @@ def test_convert(): expect5 = (('foo', 'bar', 'baz'), ('A', '1', '2'), ('B', '2', '3.4'), - (u'B', u'3', u'7.8', True), + ('B', '3', '7.8', True), ('D', 'xyz', '9.0'), ('E', 'None')) ieq(expect5, table5) @@ -59,7 +56,7 @@ def test_convert(): expect6 = (('foo', 'bar', 'baz'), ('Z', 1, 2), ('Y', '2', '3.4'), - (u'Y', u'3', u'7.8', True), + ('Y', '3', '7.8', True), ('D', 'xyz', 9.0), ('E', None)) ieq(expect6, table6) @@ -77,7 +74,7 @@ def test_convert_indexes(): table1 = (('foo', 'bar', 'baz'), ('A', 1, 2), ('B', '2', '3.4'), - (u'B', u'3', u'7.8', True), + ('B', '3', '7.8', True), ('D', 'xyz', 9.0), ('E', None)) @@ -86,7 +83,7 @@ def test_convert_indexes(): expect2 = (('foo', 'bar', 'baz'), ('a', 1, 2), ('b', '2', '3.4'), - (u'b', u'3', u'7.8', True), + ('b', '3', '7.8', True), ('d', 'xyz', 9.0), ('e', None)) ieq(expect2, table2) @@ -102,7 +99,7 @@ def test_convert_indexes(): expect4 = (('foo', 'bar', 'baz'), ('A', 1, 2), ('BB', '2', '3.4'), - (u'BB', u'3', u'7.8', True), + ('BB', '3', '7.8', True), ('D', 'xyz', 9.0), ('E', None)) ieq(expect4, table4) @@ -115,7 +112,7 @@ def test_convert_indexes(): expect5 = (('foo', 'bar', 'baz'), ('A', '1', '2'), ('B', '2', '3.4'), - (u'B', u'3', u'7.8', True), + ('B', '3', '7.8', True), ('D', 'xyz', '9.0'), ('E', 'None')) ieq(expect5, table5a) @@ -128,7 +125,7 @@ def test_convert_indexes(): expect6 = (('foo', 'bar', 'baz'), ('Z', 1, 2), ('Y', '2', '3.4'), - (u'Y', u'3', u'7.8', True), + ('Y', '3', '7.8', True), ('D', 'xyz', 9.0), ('E', None)) ieq(expect6, table6) @@ -139,7 +136,7 @@ def test_fieldconvert(): table1 = (('foo', 'bar', 'baz'), ('A', 1, 2), ('B', '2', '3.4'), - (u'B', u'3', u'7.8', True), + ('B', '3', '7.8', True), ('D', 'xyz', 9.0), ('E', None)) @@ -174,7 +171,7 @@ def test_fieldconvert(): expect7 = (('foo', 'bar', 'baz'), ('A', 1, 2), ('BB', '2', '3.4'), - (u'BB', u'3', u'7.8', True), + ('BB', '3', '7.8', True), ('D', 'xyz', 9.0), ('E', None)) ieq(expect7, table7) @@ -196,7 +193,7 @@ def test_fieldconvert(): expect9 = (('foo', 'bar', 'baz'), ('A', 1, 2.0), ('B', '2', 3.4), - ('B', u'3', 7.8, True), # N.B., long rows are preserved + ('B', '3', 7.8, True), # N.B., long rows are preserved ('D', 'xyz', 9.0), ('E', None)) # N.B., short rows are preserved ieq(expect9, table9) @@ -325,7 +322,7 @@ def test_update(): table1 = (('foo', 'bar', 'baz'), ('A', 1, 2), ('B', '2', '3.4'), - (u'B', u'3', u'7.8', True), + ('B', '3', '7.8', True), ('D', 'xyz', 9.0), ('E', None)) @@ -333,7 +330,7 @@ def test_update(): expect2 = (('foo', 'bar', 'baz'), ('X', 1, 2), ('X', '2', '3.4'), - ('X', u'3', u'7.8', True), + ('X', '3', '7.8', True), ('X', 'xyz', 9.0), ('X', None)) ieq(expect2, table2) diff --git a/petl/test/transform/test_dedup.py b/petl/test/transform/test_dedup.py index 519e9211..cb52c7ae 100644 --- a/petl/test/transform/test_dedup.py +++ b/petl/test/transform/test_dedup.py @@ -1,6 +1,3 @@ -from __future__ import absolute_import, print_function, division - - from petl.test.helpers import ieq from petl.transform.dedup import duplicates, unique, conflicts, distinct, \ isunique @@ -12,7 +9,7 @@ def test_duplicates(): ('A', 1, 2), ('B', '2', '3.4'), ('D', 'xyz', 9.0), - ('B', u'3', u'7.8', True), + ('B', '3', '7.8', True), ('B', '2', 42), ('E', None), ('D', 4, 12.3)) @@ -20,7 +17,7 @@ def test_duplicates(): result = duplicates(table, 'foo') expectation = (('foo', 'bar', 'baz'), ('B', '2', '3.4'), - ('B', u'3', u'7.8', True), + ('B', '3', '7.8', True), ('B', '2', 42), ('D', 'xyz', 9.0), ('D', 4, 12.3)) @@ -62,7 +59,7 @@ def test_unique(): ('A', 1, 2), ('B', '2', '3.4'), ('D', 'xyz', 9.0), - ('B', u'3', u'7.8', True), + ('B', '3', '7.8', True), ('B', '2', 42), ('E', None), ('D', 4, 12.3), @@ -80,7 +77,7 @@ def test_unique(): result = unique(table, key=('foo', 'bar')) expectation = (('foo', 'bar', 'baz'), ('A', 1, 2), - ('B', u'3', u'7.8', True), + ('B', '3', '7.8', True), ('D', 4, 12.3), ('D', 'xyz', 9.0), ('E', None), @@ -117,7 +114,7 @@ def test_conflicts(): ('A', 1, 2), ('B', '2', None), ('D', 'xyz', 9.4), - ('B', None, u'7.8', True), + ('B', None, '7.8', True), ('E', None), ('D', 'xyz', 12.3), ('A', 2, None)) diff --git a/petl/test/transform/test_fills.py b/petl/test/transform/test_fills.py index c6c8625e..9dc9272d 100644 --- a/petl/test/transform/test_fills.py +++ b/petl/test/transform/test_fills.py @@ -1,6 +1,3 @@ -from __future__ import absolute_import, print_function, division - - from petl.test.helpers import ieq from petl.transform.fills import filldown, fillleft, fillright diff --git a/petl/test/transform/test_headers.py b/petl/test/transform/test_headers.py index c151e528..fc2c4ad1 100644 --- a/petl/test/transform/test_headers.py +++ b/petl/test/transform/test_headers.py @@ -1,6 +1,3 @@ -from __future__ import absolute_import, print_function, division - - from petl.test.helpers import ieq from petl.errors import FieldSelectionError from petl.util import fieldnames diff --git a/petl/test/transform/test_intervals.py b/petl/test/transform/test_intervals.py index eababb26..bae3cc0a 100644 --- a/petl/test/transform/test_intervals.py +++ b/petl/test/transform/test_intervals.py @@ -1,6 +1,3 @@ -from __future__ import absolute_import, print_function, division - - import logging import sys diff --git a/petl/test/transform/test_joins.py b/petl/test/transform/test_joins.py index 2bcc748d..92bfb76a 100644 --- a/petl/test/transform/test_joins.py +++ b/petl/test/transform/test_joins.py @@ -1,6 +1,3 @@ -from __future__ import absolute_import, print_function, division - - from petl.test.helpers import ieq from petl import join, leftjoin, rightjoin, outerjoin, crossjoin, antijoin, \ lookupjoin, hashjoin, hashleftjoin, hashrightjoin, hashantijoin, \ diff --git a/petl/test/transform/test_maps.py b/petl/test/transform/test_maps.py index 6e26efc9..f1aeffbe 100644 --- a/petl/test/transform/test_maps.py +++ b/petl/test/transform/test_maps.py @@ -1,5 +1,3 @@ -from __future__ import absolute_import, print_function, division - from collections import OrderedDict from petl.test.failonerror import test_failonerror diff --git a/petl/test/transform/test_reductions.py b/petl/test/transform/test_reductions.py index 4ef2aab0..dace953d 100644 --- a/petl/test/transform/test_reductions.py +++ b/petl/test/transform/test_reductions.py @@ -1,6 +1,3 @@ -from __future__ import absolute_import, print_function, division - - import operator @@ -44,7 +41,7 @@ def test_rowreduce_fieldnameaccess(): ('c', 4)) def sumbar(key, records): - return [key, sum([rec['bar'] for rec in records])] + return [key, sum(rec['bar'] for rec in records)] table2 = rowreduce(table1, key='foo', reducer=sumbar, header=['foo', 'barsum']) @@ -436,7 +433,7 @@ def test_mergeduplicates(): ('A', 1, 2), ('B', '2', None), ('D', 'xyz', 9.4), - ('B', None, u'7.8', True), + ('B', None, '7.8', True), ('E', None, 42.), ('D', 'xyz', 12.3), ('A', 2, None)) @@ -445,7 +442,7 @@ def test_mergeduplicates(): result = mergeduplicates(table, 'foo', missing=None) expectation = (('foo', 'bar', 'baz'), ('A', Conflict([1, 2]), 2), - ('B', '2', u'7.8'), + ('B', '2', '7.8'), ('D', 'xyz', Conflict([9.4, 12.3])), ('E', None, 42.)) ieq(expectation, result) diff --git a/petl/test/transform/test_regex.py b/petl/test/transform/test_regex.py index 95305f5f..28b58222 100644 --- a/petl/test/transform/test_regex.py +++ b/petl/test/transform/test_regex.py @@ -1,7 +1,3 @@ -# -*- coding: utf-8 -*- -from __future__ import absolute_import, print_function, division - - from petl.compat import next @@ -233,35 +229,35 @@ def test_searchcomplement(): def test_search_unicode(): - tbl = ((u'name', u'id'), - (u'Արամ Խաչատրյան', 1), - (u'Johann Strauß', 2), - (u'Вагиф Сәмәдоғлу', 3), - (u'章子怡', 4)) - actual = search(tbl, u'.Խա.') - expect = ((u'name', u'id'), - (u'Արամ Խաչատրյան', 1)) + tbl = (('name', 'id'), + ('Արամ Խաչատրյան', 1), + ('Johann Strauß', 2), + ('Вагиф Сәмәдоғлу', 3), + ('章子怡', 4)) + actual = search(tbl, '.Խա.') + expect = (('name', 'id'), + ('Արամ Խաչատրյան', 1)) ieq(expect, actual) ieq(expect, actual) def test_splitdown(): - tbl = ((u'name', u'roles'), - (u'Jane Doe', u'president,engineer,tailor,lawyer'), - (u'John Doe', u'rocket scientist,optometrist,chef,knight,sailor')) + tbl = (('name', 'roles'), + ('Jane Doe', 'president,engineer,tailor,lawyer'), + ('John Doe', 'rocket scientist,optometrist,chef,knight,sailor')) actual = splitdown(tbl, 'roles', ',') - expect = ((u'name', u'roles'), - (u'Jane Doe', u'president'), - (u'Jane Doe', u'engineer'), - (u'Jane Doe', u'tailor'), - (u'Jane Doe', u'lawyer'), - (u'John Doe', u'rocket scientist'), - (u'John Doe', u'optometrist'), - (u'John Doe', u'chef'), - (u'John Doe', u'knight'), - (u'John Doe', u'sailor')) + expect = (('name', 'roles'), + ('Jane Doe', 'president'), + ('Jane Doe', 'engineer'), + ('Jane Doe', 'tailor'), + ('Jane Doe', 'lawyer'), + ('John Doe', 'rocket scientist'), + ('John Doe', 'optometrist'), + ('John Doe', 'chef'), + ('John Doe', 'knight'), + ('John Doe', 'sailor')) ieq(expect, actual) ieq(expect, actual) diff --git a/petl/test/transform/test_reshape.py b/petl/test/transform/test_reshape.py index 35502237..d004a0dc 100644 --- a/petl/test/transform/test_reshape.py +++ b/petl/test/transform/test_reshape.py @@ -1,6 +1,3 @@ -from __future__ import absolute_import, print_function, division - - from datetime import datetime diff --git a/petl/test/transform/test_selects.py b/petl/test/transform/test_selects.py index ec9ac913..942ff4b2 100644 --- a/petl/test/transform/test_selects.py +++ b/petl/test/transform/test_selects.py @@ -1,6 +1,3 @@ -from __future__ import absolute_import, print_function, division - - from petl.test.helpers import ieq, eq_ from petl.comparison import Comparable from petl.transform.selects import select, selectin, selectcontains, \ diff --git a/petl/test/transform/test_setops.py b/petl/test/transform/test_setops.py index f4c5ef2c..0ee64b92 100644 --- a/petl/test/transform/test_setops.py +++ b/petl/test/transform/test_setops.py @@ -1,6 +1,3 @@ -from __future__ import absolute_import, print_function, division - - from datetime import datetime diff --git a/petl/test/transform/test_sorts.py b/petl/test/transform/test_sorts.py index 3b6bfc9e..c234124a 100644 --- a/petl/test/transform/test_sorts.py +++ b/petl/test/transform/test_sorts.py @@ -1,6 +1,3 @@ -from __future__ import absolute_import, print_function, division - - import os import gc import logging diff --git a/petl/test/transform/test_unpacks.py b/petl/test/transform/test_unpacks.py index bb83e910..af6e33a7 100644 --- a/petl/test/transform/test_unpacks.py +++ b/petl/test/transform/test_unpacks.py @@ -1,6 +1,3 @@ -from __future__ import absolute_import, print_function, division - - from petl.test.helpers import ieq from petl.transform.unpacks import unpack, unpackdict diff --git a/petl/test/transform/test_validation.py b/petl/test/transform/test_validation.py index 6d4ecdcd..55276f64 100644 --- a/petl/test/transform/test_validation.py +++ b/petl/test/transform/test_validation.py @@ -1,7 +1,3 @@ -# -*- coding: utf-8 -*- -from __future__ import absolute_import, print_function, division - - import logging diff --git a/petl/test/util/__init__.py b/petl/test/util/__init__.py index 880b8d0f..e69de29b 100644 --- a/petl/test/util/__init__.py +++ b/petl/test/util/__init__.py @@ -1 +0,0 @@ -from __future__ import absolute_import, print_function, division \ No newline at end of file diff --git a/petl/test/util/test_base.py b/petl/test/util/test_base.py index 65431e2d..5a820edb 100644 --- a/petl/test/util/test_base.py +++ b/petl/test/util/test_base.py @@ -1,6 +1,3 @@ -from __future__ import absolute_import, print_function, division - - from petl.errors import ArgumentError from petl.test.helpers import ieq, eq_ from petl.compat import next @@ -24,7 +21,7 @@ def test_fieldnames(): expect = ('foo', 'bar') eq_(expect, actual) - class CustomField(object): + class CustomField: def __init__(self, key, description): self.key = key @@ -34,7 +31,7 @@ def __str__(self): return self.key def __repr__(self): - return 'CustomField(%r, %r)' % (self.key, self.description) + return f'CustomField({self.key!r}, {self.description!r})' table = ((CustomField('foo', 'Get some foo.'), CustomField('bar', 'A lot of bar.')), diff --git a/petl/test/util/test_counting.py b/petl/test/util/test_counting.py index 30c8e807..dda8b74e 100644 --- a/petl/test/util/test_counting.py +++ b/petl/test/util/test_counting.py @@ -1,6 +1,3 @@ -from __future__ import absolute_import, print_function, division - - from petl.compat import PY2 from petl.test.helpers import ieq, eq_ from petl.util.counting import valuecount, valuecounter, valuecounts, \ @@ -107,9 +104,9 @@ def test_typecounts(): table = (('foo', 'bar', 'baz'), (b'A', 1, 2.), - (b'B', u'2', 3.4), - (u'B', u'3', 7.8, True), - (b'D', u'xyz', 9.0), + (b'B', '2', 3.4), + ('B', '3', 7.8, True), + (b'D', 'xyz', 9.0), (b'E', 42)) actual = typecounts(table, 'foo') diff --git a/petl/test/util/test_lookups.py b/petl/test/util/test_lookups.py index 2a8c54bf..32fbbec8 100644 --- a/petl/test/util/test_lookups.py +++ b/petl/test/util/test_lookups.py @@ -1,6 +1,3 @@ -from __future__ import absolute_import, print_function, division - - from petl.errors import DuplicateKeyError from petl.test.helpers import eq_ from petl import cut, lookup, lookupone, dictlookup, dictlookupone, \ diff --git a/petl/test/util/test_materialise.py b/petl/test/util/test_materialise.py index ae723720..9eac5afc 100644 --- a/petl/test/util/test_materialise.py +++ b/petl/test/util/test_materialise.py @@ -1,6 +1,3 @@ -from __future__ import absolute_import, print_function, division - - from petl.test.helpers import eq_ from petl.util.materialise import columns, facetcolumns diff --git a/petl/test/util/test_misc.py b/petl/test/util/test_misc.py index 251245fc..0019d1b4 100644 --- a/petl/test/util/test_misc.py +++ b/petl/test/util/test_misc.py @@ -1,6 +1,3 @@ -from __future__ import absolute_import, print_function, division - - from petl.test.helpers import eq_ from petl.compat import PY2 from petl.util.misc import typeset, diffvalues, diffheaders @@ -9,10 +6,10 @@ def test_typeset(): table = (('foo', 'bar', 'baz'), - (b'A', 1, u'2'), - (b'B', '2', u'3.4'), - (b'B', '3', u'7.8', True), - (u'D', u'xyz', 9.0), + (b'A', 1, '2'), + (b'B', '2', '3.4'), + (b'B', '3', '7.8', True), + ('D', 'xyz', 9.0), (b'E', 42)) actual = typeset(table, 'foo') diff --git a/petl/test/util/test_parsers.py b/petl/test/util/test_parsers.py index 4f01ad18..d15945d1 100644 --- a/petl/test/util/test_parsers.py +++ b/petl/test/util/test_parsers.py @@ -1,6 +1,3 @@ -from __future__ import absolute_import, print_function, division - - from petl.compat import maxint from petl.test.helpers import eq_ from petl.util.parsers import numparser, datetimeparser diff --git a/petl/test/util/test_statistics.py b/petl/test/util/test_statistics.py index 384ec7c7..9294af61 100644 --- a/petl/test/util/test_statistics.py +++ b/petl/test/util/test_statistics.py @@ -1,6 +1,3 @@ -from __future__ import absolute_import, print_function, division - - from petl.test.helpers import eq_ from petl.util.statistics import stats diff --git a/petl/test/util/test_timing.py b/petl/test/util/test_timing.py index 418cc889..2293e9ae 100644 --- a/petl/test/util/test_timing.py +++ b/petl/test/util/test_timing.py @@ -1,6 +1,3 @@ -from __future__ import absolute_import, print_function, division - - from petl.util.counting import nrows from petl.util.timing import progress, log_progress diff --git a/petl/test/util/test_vis.py b/petl/test/util/test_vis.py index b34b089e..03fbe2cb 100644 --- a/petl/test/util/test_vis.py +++ b/petl/test/util/test_vis.py @@ -1,6 +1,3 @@ -from __future__ import absolute_import, print_function, division - - import logging diff --git a/petl/transform/__init__.py b/petl/transform/__init__.py index 7c43275d..a9044995 100644 --- a/petl/transform/__init__.py +++ b/petl/transform/__init__.py @@ -1,5 +1,3 @@ -from __future__ import absolute_import, print_function, division - from petl.transform.basics import cut, cutout, movefield, cat, annex, \ addfield, addfieldusingcontext, addrownumbers, addcolumn, rowslice, head, \ tail, skipcomments, stack, addfields diff --git a/petl/transform/basics.py b/petl/transform/basics.py index 5142d735..32eb3404 100644 --- a/petl/transform/basics.py +++ b/petl/transform/basics.py @@ -1,6 +1,3 @@ -from __future__ import absolute_import, print_function, division - - # standard library dependencies from itertools import islice, chain from collections import deque diff --git a/petl/transform/conversions.py b/petl/transform/conversions.py index 5328e2d3..5b7c17a8 100644 --- a/petl/transform/conversions.py +++ b/petl/transform/conversions.py @@ -1,6 +1,3 @@ -from __future__ import absolute_import, print_function, division - - from petl.compat import next, integer_types, string_types, text_type @@ -332,7 +329,7 @@ def __init__(self, source, converters=None, failonerror=None, elif isinstance(converters, dict): self.converters = converters elif isinstance(converters, (tuple, list)): - self.converters = dict([(i, v) for i, v in enumerate(converters)]) + self.converters = {i: v for i, v in enumerate(converters)} else: raise ArgumentError('unexpected converters: %r' % converters) self.failonerror = (config.failonerror if failonerror is None @@ -393,7 +390,7 @@ def iterfieldconvert(source, converters, failonerror, errorvalue, where, pass # ignore else: raise ArgumentError( - 'unexpected converter specification on field %r: %r' % (k, c) + f'unexpected converter specification on field {k!r}: {c!r}' ) # define a function to transform a value diff --git a/petl/transform/dedup.py b/petl/transform/dedup.py index 5c64e5ca..e4e3e6fe 100644 --- a/petl/transform/dedup.py +++ b/petl/transform/dedup.py @@ -1,6 +1,3 @@ -from __future__ import absolute_import, print_function, division - - import operator from petl.compat import text_type diff --git a/petl/transform/fills.py b/petl/transform/fills.py index a3c8f8e4..c212b57c 100644 --- a/petl/transform/fills.py +++ b/petl/transform/fills.py @@ -1,6 +1,3 @@ -from __future__ import absolute_import, print_function, division - - from petl.compat import next diff --git a/petl/transform/hashjoins.py b/petl/transform/hashjoins.py index 9d1a6613..19bff0c1 100644 --- a/petl/transform/hashjoins.py +++ b/petl/transform/hashjoins.py @@ -1,6 +1,3 @@ -from __future__ import absolute_import, print_function, division - - import operator from petl.compat import next, text_type @@ -100,8 +97,7 @@ def joinrows(_lrow, _rrows): k = lgetk(lrow) if k in rlookup: rrows = rlookup[k] - for outrow in joinrows(lrow, rrows): - yield outrow + yield from joinrows(lrow, rrows) def hashleftjoin(left, right, key=None, lkey=None, rkey=None, missing=None, diff --git a/petl/transform/headers.py b/petl/transform/headers.py index 641acffe..4545d23a 100644 --- a/petl/transform/headers.py +++ b/petl/transform/headers.py @@ -1,6 +1,3 @@ -from __future__ import absolute_import, print_function, division - - import itertools from petl.compat import next, text_type from petl.errors import FieldSelectionError @@ -311,8 +308,7 @@ def __iter__(self): hdr = next(it) outhdr = tuple((text_type(self.prefix) + text_type(f)) for f in hdr) yield outhdr - for row in it: - yield row + yield from it def suffixheader(table, suffix): @@ -335,8 +331,7 @@ def __iter__(self): hdr = next(it) outhdr = tuple((text_type(f) + text_type(self.suffix)) for f in hdr) yield outhdr - for row in it: - yield row + yield from it def sortheader(table, reverse=False, missing=None): diff --git a/petl/transform/intervals.py b/petl/transform/intervals.py index 07cba9bf..abdbff5a 100644 --- a/petl/transform/intervals.py +++ b/petl/transform/intervals.py @@ -1,6 +1,3 @@ -from __future__ import absolute_import, print_function, division - - from operator import itemgetter, attrgetter from petl.compat import text_type @@ -207,7 +204,7 @@ def _search_tree(tree, start, stop, include_stop): return results -class IntervalTreeLookup(object): +class IntervalTreeLookup: def __init__(self, tree, include_stop=False): self.tree = tree @@ -268,7 +265,7 @@ def intervallookupone(table, start='start', stop='stop', value=None, Table.intervallookupone = intervallookupone -class IntervalTreeLookupOne(object): +class IntervalTreeLookupOne: def __init__(self, tree, strict=True, include_stop=False): self.tree = tree @@ -936,7 +933,7 @@ def iterintervalsubtract(left, right, lstart, lstop, rstart, rstop, lkey, rkey, if not rrows: yield tuple(lrow) else: - rivs = sorted([getrcoords(rrow) for rrow in rrows], + rivs = sorted((getrcoords(rrow) for rrow in rrows), key=itemgetter(0)) # sort by start for x, y in _subtract(start, stop, rivs): out = list(lrow) @@ -963,7 +960,7 @@ def iterintervalsubtract(left, right, lstart, lstop, rstart, rstop, lkey, rkey, if not rrows: yield tuple(lrow) else: - rivs = sorted([getrcoords(rrow) for rrow in rrows], + rivs = sorted((getrcoords(rrow) for rrow in rrows), key=itemgetter(0)) # sort by start for x, y in _subtract(start, stop, rivs): out = list(lrow) diff --git a/petl/transform/joins.py b/petl/transform/joins.py index 027012d6..9c7b0730 100644 --- a/petl/transform/joins.py +++ b/petl/transform/joins.py @@ -1,6 +1,3 @@ -from __future__ import absolute_import, print_function, division - - import itertools import operator from petl.compat import next, text_type diff --git a/petl/transform/maps.py b/petl/transform/maps.py index ff64afe0..fa388565 100644 --- a/petl/transform/maps.py +++ b/petl/transform/maps.py @@ -1,6 +1,3 @@ -from __future__ import absolute_import, print_function, division - - import operator from collections import OrderedDict from petl.compat import next, string_types, text_type @@ -113,7 +110,7 @@ def iterfieldmap(source, mappings, failonerror, errorvalue): else: raise ArgumentError('expected callable or dict') else: - raise ArgumentError('invalid mapping %r: %r' % (outfld, m)) + raise ArgumentError(f'invalid mapping {outfld!r}: {m!r}') # wrap rows as records it = (Record(row, flds) for row in it) @@ -361,5 +358,4 @@ def __iter__(self): def iterrowgroupmap(source, key, mapper, header): yield tuple(header) for key, rows in rowgroupby(source, key): - for row in mapper(key, rows): - yield row + yield from mapper(key, rows) diff --git a/petl/transform/reductions.py b/petl/transform/reductions.py index 6edc7b6b..bb8768dd 100644 --- a/petl/transform/reductions.py +++ b/petl/transform/reductions.py @@ -1,6 +1,3 @@ -from __future__ import absolute_import, print_function, division - - import itertools import operator from collections import OrderedDict @@ -338,7 +335,7 @@ def itermultiaggregate(source, key, aggregation): elif len(agg) == 2: pass # no need to normalise else: - raise ArgumentError('invalid aggregation: %r, %r' % (outfld, agg)) + raise ArgumentError(f'invalid aggregation: {outfld!r}, {agg!r}') # determine output header if isinstance(key, (list, tuple)): @@ -599,8 +596,8 @@ def itermergeduplicates(table, key, missing): outrow = [k] else: outrow = list(k) - mergedvals = [set(row[i] for row in grp - if len(row) > i and row[i] != missing) + mergedvals = [{row[i] for row in grp + if len(row) > i and row[i] != missing} for i in valfldidxs] normedvals = [vals.pop() if len(vals) == 1 else missing if len(vals) == 0 @@ -656,7 +653,7 @@ def merge(*tables, **kwargs): class Conflict(frozenset): def __new__(cls, items): - s = super(Conflict, cls).__new__(cls, items) + s = super().__new__(cls, items) return s diff --git a/petl/transform/regex.py b/petl/transform/regex.py index 0b71329d..8e42a1ad 100644 --- a/petl/transform/regex.py +++ b/petl/transform/regex.py @@ -1,6 +1,3 @@ -from __future__ import absolute_import, print_function, division - - import re import operator from petl.compat import next, text_type diff --git a/petl/transform/reshape.py b/petl/transform/reshape.py index 055b1abc..038e45b4 100644 --- a/petl/transform/reshape.py +++ b/petl/transform/reshape.py @@ -1,6 +1,3 @@ -from __future__ import absolute_import, print_function, division - - import itertools import collections import operator @@ -582,8 +579,7 @@ def __init__(self, table): def __iter__(self): for row in data(self.table): - for value in row: - yield value + yield from row def unflatten(*args, **kwargs): diff --git a/petl/transform/selects.py b/petl/transform/selects.py index 87456aef..7cb03043 100644 --- a/petl/transform/selects.py +++ b/petl/transform/selects.py @@ -1,6 +1,3 @@ -from __future__ import absolute_import, print_function, division - - import operator from petl.compat import next, string_types, callable, text_type from petl.comparison import Comparable diff --git a/petl/transform/setops.py b/petl/transform/setops.py index 96b9b866..421f709a 100644 --- a/petl/transform/setops.py +++ b/petl/transform/setops.py @@ -1,5 +1,3 @@ -from __future__ import absolute_import, print_function, division - from collections import Counter from petl.compat import next from petl.comparison import Comparable @@ -137,8 +135,7 @@ def itercomplement(ta, tb, strict): b = next(itb) except StopIteration: yield a - for row in ita: - yield row + yield from ita else: # we want the elements in a that are not in b while True: diff --git a/petl/transform/sorts.py b/petl/transform/sorts.py index 521d0cec..1ac4deb4 100755 --- a/petl/transform/sorts.py +++ b/petl/transform/sorts.py @@ -1,6 +1,3 @@ -from __future__ import absolute_import, print_function, division - - import os import heapq from tempfile import NamedTemporaryFile @@ -355,7 +352,7 @@ def _iternocache(self, source, key, reverse): yield tuple(row) -class _NamedTempFileDeleteOnGC(object): +class _NamedTempFileDeleteOnGC: def __init__(self, name): self.name = name @@ -366,7 +363,7 @@ def delete(self, unlink=os.unlink, log=logger.debug): log('deleting %s' % name) unlink(name) except Exception as e: - log('exception deleting %s: %s' % (name, e)) + log(f'exception deleting {name}: {e}') raise else: log('deleted %s' % name) @@ -528,8 +525,7 @@ def _standardisedata(it, hdr, ofs): getkey = comparable_itemgetter(*indices) # OK, do the merge sort - for row in _shortlistmergesorted(getkey, reverse, *sits): - yield row + yield from _shortlistmergesorted(getkey, reverse, *sits) def issorted(table, key=None, reverse=False, strict=False): diff --git a/petl/transform/unpacks.py b/petl/transform/unpacks.py index 00d39b2e..294e227d 100644 --- a/petl/transform/unpacks.py +++ b/petl/transform/unpacks.py @@ -1,6 +1,3 @@ -from __future__ import absolute_import, print_function, division - - import itertools from petl.compat import next, text_type diff --git a/petl/transform/validation.py b/petl/transform/validation.py index 76cde4d3..03cbedfc 100644 --- a/petl/transform/validation.py +++ b/petl/transform/validation.py @@ -1,7 +1,3 @@ -# -*- coding: utf-8 -*- -from __future__ import absolute_import, print_function, division - - import operator from petl.compat import text_type diff --git a/petl/util/__init__.py b/petl/util/__init__.py index 5eb62fd3..38828ea8 100644 --- a/petl/util/__init__.py +++ b/petl/util/__init__.py @@ -1,6 +1,3 @@ -from __future__ import absolute_import, print_function, division - - from petl.util.base import Table, Record, values, header, data, \ fieldnames, records, dicts, namedtuples, expr, rowgroupby, empty, wrap diff --git a/petl/util/base.py b/petl/util/base.py index 22d7acf7..466706cc 100644 --- a/petl/util/base.py +++ b/petl/util/base.py @@ -1,6 +1,3 @@ -from __future__ import absolute_import, print_function, division - - import re from itertools import islice, chain, cycle, product,\ permutations, combinations, takewhile, dropwhile, \ @@ -16,7 +13,7 @@ from petl.comparison import comparable_itemgetter -class IterContainer(object): +class IterContainer: def __contains__(self, item): for o in self: @@ -169,7 +166,7 @@ def __getitem__(self, item): if isinstance(item, string_types): return ValuesView(self, item) else: - return super(Table, self).__getitem__(item) + return super().__getitem__(item) def values(table, *field, **kwargs): @@ -547,7 +544,7 @@ def asnamedtuple(nt, row, missing=None): class Record(tuple): def __new__(cls, row, flds, missing=None): - t = super(Record, cls).__new__(cls, row) + t = super().__new__(cls, row) return t def __init__(self, row, flds, missing=None): @@ -563,14 +560,14 @@ def __getitem__(self, f): raise KeyError('item ' + repr(f) + ' not in fields ' + repr(self.flds)) try: - return super(Record, self).__getitem__(idx) + return super().__getitem__(idx) except IndexError: # handle short rows return self.missing def __getattr__(self, f): if f in self.flds: try: - return super(Record, self).__getitem__(self.flds.index(f)) + return super().__getitem__(self.flds.index(f)) except IndexError: # handle short rows return self.missing else: diff --git a/petl/util/counting.py b/petl/util/counting.py index 467af1e8..b4a116af 100644 --- a/petl/util/counting.py +++ b/petl/util/counting.py @@ -1,6 +1,3 @@ -from __future__ import absolute_import, print_function, division - - from collections import Counter from petl.compat import string_types, maketrans diff --git a/petl/util/lookups.py b/petl/util/lookups.py index 921b45e4..94b45914 100644 --- a/petl/util/lookups.py +++ b/petl/util/lookups.py @@ -1,6 +1,3 @@ -from __future__ import absolute_import, print_function, division - - import operator from petl.compat import text_type diff --git a/petl/util/materialise.py b/petl/util/materialise.py index 0728d77c..6cfe3397 100644 --- a/petl/util/materialise.py +++ b/petl/util/materialise.py @@ -1,6 +1,3 @@ -from __future__ import absolute_import, print_function, division - - import operator from collections import OrderedDict from itertools import islice diff --git a/petl/util/misc.py b/petl/util/misc.py index 6cc58ba5..1edd917b 100644 --- a/petl/util/misc.py +++ b/petl/util/misc.py @@ -1,6 +1,3 @@ -from __future__ import absolute_import, print_function, division - - from petl.util.base import values, header, Table diff --git a/petl/util/parsers.py b/petl/util/parsers.py index b9227e45..16b21b1e 100644 --- a/petl/util/parsers.py +++ b/petl/util/parsers.py @@ -1,6 +1,3 @@ -from __future__ import absolute_import, print_function, division - - import datetime from petl.compat import long diff --git a/petl/util/random.py b/petl/util/random.py index 94fa7588..b88cfae6 100644 --- a/petl/util/random.py +++ b/petl/util/random.py @@ -1,6 +1,3 @@ -from __future__ import absolute_import, print_function, division - - import datetime import random import time diff --git a/petl/util/statistics.py b/petl/util/statistics.py index 105dea8d..d0a4244e 100644 --- a/petl/util/statistics.py +++ b/petl/util/statistics.py @@ -1,6 +1,3 @@ -from __future__ import absolute_import, print_function, division - - from collections import namedtuple diff --git a/petl/util/timing.py b/petl/util/timing.py index 41e8ff6b..fae8832e 100644 --- a/petl/util/timing.py +++ b/petl/util/timing.py @@ -1,6 +1,3 @@ -from __future__ import absolute_import, print_function, division - - import abc import logging import sys @@ -169,7 +166,7 @@ def __init__(self, inner, batchsize, prefix, out): self.file_object = sys.stderr else: self.file_object = out - super(ProgressView, self).__init__(inner, batchsize, prefix) + super().__init__(inner, batchsize, prefix) def print_message(self, message): print(message, file=self.file_object) @@ -189,7 +186,7 @@ def __init__(self, inner, batchsize, prefix, logger, level=logging.INFO): else: self.logger = logger self.level = level - super(LoggingProgressView, self).__init__(inner, batchsize, prefix) + super().__init__(inner, batchsize, prefix) def print_message(self, message): self.logger.log(self.level, message) diff --git a/petl/util/vis.py b/petl/util/vis.py index e1d830a2..0a97ce84 100644 --- a/petl/util/vis.py +++ b/petl/util/vis.py @@ -1,6 +1,3 @@ -from __future__ import absolute_import, print_function, division - - import locale from itertools import islice from collections import defaultdict @@ -86,7 +83,7 @@ def look(table, limit=0, vrepr=None, index_header=None, style=None, Table.look = look -class Look(object): +class Look: def __init__(self, table, limit, vrepr, index_header, style, truncate, width): @@ -197,7 +194,7 @@ def _look_grid(table, vrepr, index_header, truncate, width): hdr = next(it) flds = list(map(text_type, hdr)) if index_header: - fldsrepr = ['%s|%s' % (i, r) for (i, r) in enumerate(flds)] + fldsrepr = [f'{i}|{r}' for (i, r) in enumerate(flds)] else: fldsrepr = flds @@ -297,7 +294,7 @@ def _look_simple(table, vrepr, index_header, truncate, width): hdr = next(it) flds = list(map(text_type, hdr)) if index_header: - fldsrepr = ['%s|%s' % (i, r) for (i, r) in enumerate(flds)] + fldsrepr = [f'{i}|{r}' for (i, r) in enumerate(flds)] else: fldsrepr = flds @@ -380,7 +377,7 @@ def _look_minimal(table, vrepr, index_header, truncate, width): hdr = next(it) flds = list(map(text_type, hdr)) if index_header: - fldsrepr = ['%s|%s' % (i, r) for (i, r) in enumerate(flds)] + fldsrepr = [f'{i}|{r}' for (i, r) in enumerate(flds)] else: fldsrepr = flds @@ -476,7 +473,7 @@ def see(table, limit=0, vrepr=None, index_header=None): return See(table, limit=limit, vrepr=vrepr, index_header=index_header) -class See(object): +class See: def __init__(self, table, limit, vrepr, index_header): self.table = table @@ -505,8 +502,8 @@ def __repr__(self): cols[str(f)].append('') for i, f in enumerate(flds): if index_header: - f = '%s|%s' % (i, f) - output += '%s: %s' % (f, ', '.join(cols[str(i)])) + f = f'{i}|{f}' + output += '{}: {}'.format(f, ', '.join(cols[str(i)])) if overflow: output += '...\n' else: diff --git a/setup.py b/setup.py index 3ee2f078..1822ee5e 100644 --- a/setup.py +++ b/setup.py @@ -1,4 +1,3 @@ -from __future__ import print_function, absolute_import, division from setuptools import setup, find_packages From 17dc4d675c84db1edc2b909b1c94f90720a5cbb1 Mon Sep 17 00:00:00 2001 From: Hugo van Kemenade Date: Sat, 28 Aug 2021 15:37:25 +0300 Subject: [PATCH 3/7] Remove compat.py --- .settings/org.eclipse.core.resources.prefs | 1 - petl/comparison.py | 21 +++---- petl/compat.py | 67 ---------------------- petl/config.py | 5 +- petl/io/avro.py | 22 +++---- petl/io/base.py | 4 +- petl/io/bcolz.py | 9 ++- petl/io/csv.py | 11 +--- petl/io/db.py | 15 +++-- petl/io/db_create.py | 14 ++--- petl/io/db_utils.py | 3 - petl/io/html.py | 50 ++++++---------- petl/io/json.py | 27 ++++----- petl/io/numpy.py | 3 +- petl/io/pickle.py | 2 +- petl/io/pytables.py | 5 +- petl/io/remotes.py | 9 +-- petl/io/sources.py | 14 ++--- petl/io/text.py | 48 ++++++---------- petl/io/whoosh.py | 13 ++--- petl/io/xls.py | 8 +-- petl/io/xlsx.py | 8 +-- petl/io/xlutils_view.py | 3 +- petl/io/xml.py | 9 ++- petl/test/helpers.py | 8 +-- petl/test/io/test_avro.py | 8 +-- petl/test/io/test_csv.py | 8 +-- petl/test/io/test_db.py | 1 - petl/test/io/test_pickle.py | 2 +- petl/test/io/test_remotes.py | 6 +- petl/test/io/test_sources.py | 13 +---- petl/test/io/test_xml.py | 2 +- petl/test/test_fluent.py | 2 - petl/test/transform/test_regex.py | 3 - petl/test/transform/test_sorts.py | 1 - petl/test/util/test_base.py | 2 - petl/test/util/test_counting.py | 23 ++------ petl/test/util/test_misc.py | 6 +- petl/test/util/test_parsers.py | 6 +- petl/transform/basics.py | 18 +++--- petl/transform/conversions.py | 13 ++--- petl/transform/dedup.py | 3 +- petl/transform/fills.py | 3 - petl/transform/hashjoins.py | 17 +++--- petl/transform/headers.py | 7 +-- petl/transform/intervals.py | 11 ++-- petl/transform/joins.py | 11 ++-- petl/transform/maps.py | 9 ++- petl/transform/reductions.py | 12 ++-- petl/transform/regex.py | 15 +++-- petl/transform/reshape.py | 5 +- petl/transform/selects.py | 7 +-- petl/transform/setops.py | 1 - petl/transform/sorts.py | 8 +-- petl/transform/unpacks.py | 7 +-- petl/transform/validation.py | 7 +-- petl/util/base.py | 31 +++++----- petl/util/counting.py | 5 +- petl/util/lookups.py | 9 ++- petl/util/materialise.py | 11 ++-- petl/util/parsers.py | 7 +-- petl/util/random.py | 9 ++- petl/util/timing.py | 5 +- petl/util/vis.py | 32 ++++++----- 64 files changed, 250 insertions(+), 475 deletions(-) delete mode 100644 petl/compat.py diff --git a/.settings/org.eclipse.core.resources.prefs b/.settings/org.eclipse.core.resources.prefs index 733f4dbf..6cb7e217 100644 --- a/.settings/org.eclipse.core.resources.prefs +++ b/.settings/org.eclipse.core.resources.prefs @@ -1,4 +1,3 @@ #Fri Jun 22 10:55:09 BST 2012 eclipse.preferences.version=1 encoding//docs/conf.py=utf-8 -encoding//src/petl/compat.py=utf-8 diff --git a/petl/comparison.py b/petl/comparison.py index d5a6b92a..8f7e4d5c 100644 --- a/petl/comparison.py +++ b/petl/comparison.py @@ -1,9 +1,6 @@ import operator -from petl.compat import text_type, binary_type, numeric_types - - class Comparable: """Wrapper class to allow for flexible comparison of objects of different types, preserving the relaxed sorting behaviour of Python 2 with @@ -37,17 +34,17 @@ def __lt__(self, other): return True # numbers < everything else (except None) - if isinstance(obj, numeric_types) \ - and not isinstance(other, numeric_types): + if isinstance(obj, (bool, int, float)) \ + and not isinstance(other, (bool, int, float)): return True - if not isinstance(obj, numeric_types) \ - and isinstance(other, numeric_types): + if not isinstance(obj, (bool, int, float)) \ + and isinstance(other, (bool, int, float)): return False # binary < unicode - if isinstance(obj, text_type) and isinstance(other, binary_type): + if isinstance(obj, str) and isinstance(other, bytes): return False - if isinstance(obj, binary_type) and isinstance(other, text_type): + if isinstance(obj, bytes) and isinstance(other, str): return True try: @@ -76,7 +73,7 @@ def __str__(self): return str(self.obj) def __unicode__(self): - return text_type(self.obj) + return str(self.obj) def __repr__(self): return 'Comparable(' + repr(self.obj) + ')' @@ -93,9 +90,9 @@ def __getitem__(self, item): def _typestr(x): # attempt to preserve Python 2 name orderings - if isinstance(x, binary_type): + if isinstance(x, bytes): return 'str' - if isinstance(x, text_type): + if isinstance(x, str): return 'unicode' return type(x).__name__ diff --git a/petl/compat.py b/petl/compat.py deleted file mode 100644 index 3bfc47a1..00000000 --- a/petl/compat.py +++ /dev/null @@ -1,67 +0,0 @@ -import sys - - - -########################## -# Python 3 compatibility # -########################## - -PY2 = sys.version_info.major == 2 -PY3 = sys.version_info.major == 3 - -if PY2: - from itertools import ifilter, ifilterfalse, imap, izip, izip_longest - from string import maketrans - string_types = basestring, - integer_types = int, long - numeric_types = bool, int, long, float - text_type = unicode - binary_type = str - from urllib2 import urlopen - try: - from cStringIO import StringIO - except ImportError: - from StringIO import StringIO - BytesIO = StringIO - try: - import cPickle as pickle - except ImportError: - import pickle - maxint = sys.maxint - long = long - xrange = xrange - reduce = reduce - -else: - ifilter = filter - imap = map - izip = zip - xrange = range - from itertools import filterfalse as ifilterfalse - from itertools import zip_longest as izip_longest - from functools import reduce - maketrans = str.maketrans - string_types = str, - integer_types = int, - numeric_types = bool, int, float - class_types = type, - text_type = str - binary_type = bytes - long = int - from urllib.request import urlopen - from io import StringIO, BytesIO - import pickle - maxint = sys.maxsize - -try: - advance_iterator = next -except NameError: - def advance_iterator(it): - return it.next() -next = advance_iterator - -try: - callable = callable -except NameError: - def callable(obj): - return any("__call__" in klass.__dict__ for klass in type(obj).__mro__) diff --git a/petl/config.py b/petl/config.py index c99cdea9..05f2dedb 100644 --- a/petl/config.py +++ b/petl/config.py @@ -1,6 +1,3 @@ -from petl.compat import text_type - - look_style = 'grid' # alternatives: 'simple', 'minimal' look_limit = 5 look_index_header = False @@ -11,7 +8,7 @@ see_vrepr = repr display_limit = 5 display_index_header = False -display_vrepr = text_type +display_vrepr = str sort_buffersize = 100000 failonerror=False # False, True, 'inline' """ diff --git a/petl/io/avro.py b/petl/io/avro.py index ebdd17f0..6d0aed50 100644 --- a/petl/io/avro.py +++ b/petl/io/avro.py @@ -3,8 +3,8 @@ from collections import OrderedDict from datetime import datetime, date, time from decimal import Decimal +from itertools import zip_longest -from petl.compat import izip_longest, text_type, string_types, PY3 from petl.io.sources import read_source_from_arg, write_source_from_arg from petl.transform.headers import skip, setheader from petl.util.base import Table, dicts, fieldnames, iterpeek, wrap @@ -293,12 +293,7 @@ def _map_row_from(self, header, record): - avro type: int logicalType: date -> python date - avro type: bytes logicalType: decimal -> python Decimal ''' - if header is None or PY3: - r = tuple(record.values()) - else: - # fastavro on python2 does not respect dict order - r = tuple(record.get(col) for col in header) - return r + return tuple(record.values()) def _write_toavro(table, target, mode, schema, sample, @@ -311,7 +306,7 @@ def _write_toavro(table, target, mode, schema, sample, else: table2 = _fix_missing_headers(table, schema) # fastavro expects a iterator of dicts - rows = dicts(table2) if PY3 else _ordered_dict_iterator(table2) + rows = dicts(table2) target2 = write_source_from_arg(target, mode=mode) with target2.open(mode) as target_file: @@ -381,7 +376,7 @@ def _build_schema_fields_from_values(peek, props): def _update_field_defs_from(props, row, fields, previous, fill_missing): - for prop, val in izip_longest(props, row): + for prop, val in zip_longest(props, row): if prop is None: break dprev = previous.get(prop + '_prec') @@ -526,7 +521,7 @@ def _get_error_details(target, num, err, record, schema): else: table = [headers, record] example = wrap(table).look() - dest = " output: %s" % target if isinstance(target, string_types) else '' + dest = " output: %s" % target if isinstance(target, str) else '' printed = "failed writing on row #%d: %s\n%s\n schema: %s\n%s" details = printed % (num, err, dest, schema, example) return details @@ -545,10 +540,7 @@ def _raise_error(ErrorType, new_message): exinf = sys.exc_info() tracebk = exinf[2] try: - if PY3: - raise ErrorType(new_message).with_traceback(tracebk) - # Python2 compatibility workaround - exec('raise ErrorType, new_message, tracebk') + raise ErrorType(new_message).with_traceback(tracebk) finally: exinf = None tracebk = None # noqa: F841 @@ -557,7 +549,7 @@ def _raise_error(ErrorType, new_message): def _ordered_dict_iterator(table): it = iter(table) hdr = next(it) - flds = [text_type(f) for f in hdr] + flds = [str(f) for f in hdr] for row in it: items = list() for i, f in enumerate(flds): diff --git a/petl/io/base.py b/petl/io/base.py index 9ca82d49..bca08c49 100644 --- a/petl/io/base.py +++ b/petl/io/base.py @@ -1,6 +1,6 @@ import locale import codecs -from petl.compat import izip_longest +from itertools import zip_longest from petl.util.base import Table @@ -69,4 +69,4 @@ def itercolumns(cols, header, missing): if header is None: header = ['f%s' % i for i in range(len(cols))] yield tuple(header) - yield from izip_longest(*cols, **dict(fillvalue=missing)) + yield from zip_longest(*cols, **dict(fillvalue=missing)) diff --git a/petl/io/bcolz.py b/petl/io/bcolz.py index cd758e57..abe9d7f3 100644 --- a/petl/io/bcolz.py +++ b/petl/io/bcolz.py @@ -1,7 +1,6 @@ import itertools -from petl.compat import string_types, text_type from petl.util.base import Table, iterpeek from petl.io.numpy import construct_dtype @@ -67,7 +66,7 @@ def __init__(self, source, expression=None, outcols=None, limit=None, def __iter__(self): # obtain ctable - if isinstance(self.source, string_types): + if isinstance(self.source, str): import bcolz ctbl = bcolz.open(self.source, mode='r') else: @@ -134,7 +133,7 @@ def tobcolz(table, dtype=None, sample=1000, **kwargs): hdr = next(it) # numpy is fussy about having tuples, need to make sure it = (tuple(row) for row in it) - flds = list(map(text_type, hdr)) + flds = list(map(str, hdr)) dtype = construct_dtype(flds, peek, dtype) # create ctable @@ -168,7 +167,7 @@ def appendbcolz(table, obj, check_names=True): import bcolz import numpy as np - if isinstance(obj, string_types): + if isinstance(obj, str): ctbl = bcolz.open(obj, mode='a') else: assert hasattr(obj, 'append') and hasattr(obj, 'names'), \ @@ -179,7 +178,7 @@ def appendbcolz(table, obj, check_names=True): dtype = ctbl.dtype it = iter(table) hdr = next(it) - flds = list(map(text_type, hdr)) + flds = list(map(str, hdr)) # check names match if check_names: diff --git a/petl/io/csv.py b/petl/io/csv.py index aa350b6e..5caad2e2 100644 --- a/petl/io/csv.py +++ b/petl/io/csv.py @@ -1,16 +1,7 @@ -# standard library dependencies -from petl.compat import PY2 - - # internal dependencies from petl.util.base import Table from petl.io.sources import read_source_from_arg, write_source_from_arg -if PY2: - from petl.io.csv_py2 import fromcsv_impl, tocsv_impl, appendcsv_impl, \ - teecsv_impl -else: - from petl.io.csv_py3 import fromcsv_impl, tocsv_impl, appendcsv_impl, \ - teecsv_impl +from petl.io.csv_py3 import fromcsv_impl, tocsv_impl, appendcsv_impl, teecsv_impl def fromcsv(source=None, encoding=None, errors='strict', header=None, diff --git a/petl/io/db.py b/petl/io/db.py index d12bd68e..7d5a4ed1 100644 --- a/petl/io/db.py +++ b/petl/io/db.py @@ -1,6 +1,5 @@ # standard library dependencies import logging -from petl.compat import next, text_type, string_types # internal dependencies @@ -78,7 +77,7 @@ def fromdb(dbo, query, *args, **kwargs): """ # convenience for working with sqlite3 - if isinstance(dbo, string_types): + if isinstance(dbo, str): import sqlite3 dbo = sqlite3.connect(dbo) @@ -316,7 +315,7 @@ def todb(table, dbo, tablename, schema=None, commit=True, needs_closing = False # convenience for working with sqlite3 - if isinstance(dbo, string_types): + if isinstance(dbo, str): import sqlite3 dbo = sqlite3.connect(dbo) needs_closing = True @@ -401,7 +400,7 @@ def _todb_dbapi_connection(table, connection, tablename, schema=None, # sanitise field names it = iter(table) hdr = next(it) - flds = list(map(text_type, hdr)) + flds = list(map(str, hdr)) colnames = [_quote(n) for n in flds] debug('column names: %r', colnames) @@ -448,7 +447,7 @@ def _todb_dbapi_mkcurs(table, mkcurs, tablename, schema=None, commit=True, # sanitise field names it = iter(table) hdr = next(it) - flds = list(map(text_type, hdr)) + flds = list(map(str, hdr)) colnames = [_quote(n) for n in flds] debug('column names: %r', colnames) @@ -496,7 +495,7 @@ def _todb_dbapi_cursor(table, cursor, tablename, schema=None, commit=True, # sanitise field names it = iter(table) hdr = next(it) - flds = list(map(text_type, hdr)) + flds = list(map(str, hdr)) colnames = [_quote(n) for n in flds] debug('column names: %r', colnames) @@ -550,7 +549,7 @@ def _todb_sqlalchemy_connection(table, connection, tablename, schema=None, # sanitise field names it = iter(table) hdr = next(it) - flds = list(map(text_type, hdr)) + flds = list(map(str, hdr)) colnames = [_quote(n) for n in flds] debug('column names: %r', colnames) @@ -609,7 +608,7 @@ def appenddb(table, dbo, tablename, schema=None, commit=True): needs_closing = False # convenience for working with sqlite3 - if isinstance(dbo, string_types): + if isinstance(dbo, str): import sqlite3 dbo = sqlite3.connect(dbo) needs_closing = True diff --git a/petl/io/db_create.py b/petl/io/db_create.py index 5faff842..62ee1a60 100644 --- a/petl/io/db_create.py +++ b/petl/io/db_create.py @@ -11,7 +11,6 @@ import datetime import logging -from petl.compat import long, text_type from petl.errors import ArgumentError @@ -81,13 +80,10 @@ def make_sqlalchemy_column(col, colname, constraints=True): else: sql_column_type = sqlalchemy.Integer - elif all(isinstance(v, long) for v in col_not_none): - sql_column_type = sqlalchemy.BigInteger - - elif all(isinstance(v, (int, long)) for v in col_not_none): + elif all(isinstance(v, int) for v in col_not_none): sql_column_type = sqlalchemy.BigInteger - elif all(isinstance(v, (int, long, float)) for v in col_not_none): + elif all(isinstance(v, (int, float)) for v in col_not_none): sql_column_type = sqlalchemy.Float elif all(isinstance(v, datetime.datetime) for v in col_not_none): @@ -102,7 +98,7 @@ def make_sqlalchemy_column(col, colname, constraints=True): else: sql_column_type = sqlalchemy.String if constraints: - sql_type_kwargs['length'] = max(len(text_type(v)) for v in col) + sql_type_kwargs['length'] = max(len(str(v)) for v in col) if constraints: sql_column_kwargs['nullable'] = len(col_not_none) < len(col) @@ -182,8 +178,8 @@ def make_create_table_statement(table, tablename, schema=None, else: sql_dialect = None - return text_type(sqlalchemy.schema.CreateTable(sql_table) - .compile(dialect=sql_dialect)).strip() + return str(sqlalchemy.schema.CreateTable(sql_table) + .compile(dialect=sql_dialect)).strip() def create_table(table, dbo, tablename, schema=None, commit=True, diff --git a/petl/io/db_utils.py b/petl/io/db_utils.py index 4a3eb78c..679f66f9 100644 --- a/petl/io/db_utils.py +++ b/petl/io/db_utils.py @@ -1,9 +1,6 @@ import logging -from petl.compat import callable - - logger = logging.getLogger(__name__) debug = logger.debug diff --git a/petl/io/html.py b/petl/io/html.py index e4de892d..f15b197d 100644 --- a/petl/io/html.py +++ b/petl/io/html.py @@ -1,18 +1,16 @@ # standard library dependencies import io -from petl.compat import text_type, numeric_types, next, PY2, izip_longest, \ - string_types, callable +from itertools import zip_longest # internal dependencies from petl.errors import ArgumentError from petl.util.base import Table, Record -from petl.io.base import getcodec from petl.io.sources import write_source_from_arg def tohtml(table, source=None, encoding=None, errors='strict', caption=None, - vrepr=text_type, lineterminator='\n', index_header=False, + vrepr=str, lineterminator='\n', index_header=False, tr_style=None, td_styles=None, truncate=None): """ Write the table as HTML to a file. E.g.:: @@ -57,14 +55,10 @@ def tohtml(table, source=None, encoding=None, errors='strict', caption=None, with source.open('wb') as buf: # deal with text encoding - if PY2: - codec = getcodec(encoding) - f = codec.streamwriter(buf, errors=errors) - else: - f = io.TextIOWrapper(buf, - encoding=encoding, - errors=errors, - newline='') + f = io.TextIOWrapper(buf, + encoding=encoding, + errors=errors, + newline='') # write the table try: @@ -88,15 +82,14 @@ def tohtml(table, source=None, encoding=None, errors='strict', caption=None, f.flush() finally: - if not PY2: - f.detach() + f.detach() Table.tohtml = tohtml def teehtml(table, source=None, encoding=None, errors='strict', caption=None, - vrepr=text_type, lineterminator='\n', index_header=False, + vrepr=str, lineterminator='\n', index_header=False, tr_style=None, td_styles=None, truncate=None): """ Return a table that writes rows to a Unicode HTML file as they are @@ -117,7 +110,7 @@ def teehtml(table, source=None, encoding=None, errors='strict', caption=None, class TeeHTMLView(Table): def __init__(self, table, source=None, encoding=None, errors='strict', - caption=None, vrepr=text_type, lineterminator='\n', + caption=None, vrepr=str, lineterminator='\n', index_header=False, tr_style=None, td_styles=None, truncate=None): self.table = table @@ -148,14 +141,10 @@ def __iter__(self): with source.open('wb') as buf: # deal with text encoding - if PY2: - codec = getcodec(encoding) - f = codec.streamwriter(buf, errors=errors) - else: - f = io.TextIOWrapper(buf, - encoding=encoding, - errors=errors, - newline='') + f = io.TextIOWrapper(buf, + encoding=encoding, + errors=errors, + newline='') # write the table try: @@ -181,8 +170,7 @@ def __iter__(self): f.flush() finally: - if not PY2: - f.detach() + f.detach() def _write_begin(f, flds, lineterminator, caption, index_header, truncate): @@ -209,7 +197,7 @@ def _write_row(f, flds, row, lineterminator, vrepr, tr_style, td_styles, f.write(("" % tr_css) + lineterminator) else: f.write("" + lineterminator) - for h, v in izip_longest(flds, row, fillvalue=None): + for h, v in zip_longest(flds, row, fillvalue=None): r = vrepr(v) if truncate: r = r[:truncate] @@ -224,7 +212,7 @@ def _write_row(f, flds, row, lineterminator, vrepr, tr_style, td_styles, def _get_tr_css(row, tr_style): # check for user-provided style if tr_style: - if isinstance(tr_style, string_types): + if isinstance(tr_style, str): return tr_style elif callable(tr_style): return tr_style(row) @@ -238,14 +226,14 @@ def _get_tr_css(row, tr_style): def _get_td_css(h, v, td_styles): # check for user-provided style if td_styles: - if isinstance(td_styles, string_types): + if isinstance(td_styles, str): return td_styles elif callable(td_styles): return td_styles(v) elif isinstance(td_styles, dict): if h in td_styles: s = td_styles[h] - if isinstance(s, string_types): + if isinstance(s, str): return s elif callable(s): return s(v) @@ -256,7 +244,7 @@ def _get_td_css(h, v, td_styles): raise ArgumentError('expected string, callable or dict, got %r' % td_styles) # fall back to default style - if isinstance(v, numeric_types) and not isinstance(v, bool): + if isinstance(v, (bool, int, float)) and not isinstance(v, bool): return 'text-align: right' else: return '' diff --git a/petl/io/json.py b/petl/io/json.py index d321f9ec..410f6dfb 100644 --- a/petl/io/json.py +++ b/petl/io/json.py @@ -3,7 +3,6 @@ import json from json.encoder import JSONEncoder -from petl.compat import PY2 from petl.io.sources import read_source_from_arg, write_source_from_arg # internal dependencies from petl.util.base import data, Table, dicts as _dicts, iterpeek @@ -97,10 +96,9 @@ def __init__(self, source, *args, **kwargs): def __iter__(self): with self.source.open('rb') as f: - if not PY2: - # wrap buffer for text IO - f = io.TextIOWrapper(f, encoding='utf-8', newline='', - write_through=True) + # wrap buffer for text IO + f = io.TextIOWrapper(f, encoding='utf-8', newline='', + write_through=True) try: if self.lines: yield from iterjlines(f, self.header, self.missing) @@ -109,8 +107,7 @@ def __iter__(self): yield from iterdicts(dicts, self.header, self.sample, self.missing) finally: - if not PY2: - f.detach() + f.detach() def fromdicts(dicts, header=None, sample=1000, missing=None): @@ -260,17 +257,13 @@ def _writejson(source, obj, prefix, suffix, *args, **kwargs): encoder = JSONEncoder(*args, **kwargs) source = write_source_from_arg(source) with source.open('wb') as f: - if PY2: - # write directly to buffer + # wrap buffer for text IO + f = io.TextIOWrapper(f, encoding='utf-8', newline='', + write_through=True) + try: _writeobj(encoder, obj, f, prefix, suffix) - else: - # wrap buffer for text IO - f = io.TextIOWrapper(f, encoding='utf-8', newline='', - write_through=True) - try: - _writeobj(encoder, obj, f, prefix, suffix) - finally: - f.detach() + finally: + f.detach() def _writeobj(encoder, obj, f, prefix, suffix): diff --git a/petl/io/numpy.py b/petl/io/numpy.py index f7483744..971795e2 100644 --- a/petl/io/numpy.py +++ b/petl/io/numpy.py @@ -1,4 +1,3 @@ -from petl.compat import next, string_types from petl.util.base import iterpeek, ValuesView, Table from petl.util.materialise import columns @@ -21,7 +20,7 @@ def construct_dtype(flds, peek, dtype): if dtype is None: dtype = infer_dtype(peek) - elif isinstance(dtype, string_types): + elif isinstance(dtype, str): # insert field names from source table typestrings = [s.strip() for s in dtype.split(',')] dtype = [(f, t) for f, t in zip(flds, typestrings)] diff --git a/petl/io/pickle.py b/petl/io/pickle.py index cf871591..c7b3e9c3 100644 --- a/petl/io/pickle.py +++ b/petl/io/pickle.py @@ -1,5 +1,5 @@ # standard library dependencies -from petl.compat import pickle, next +import pickle # internal dependencies diff --git a/petl/io/pytables.py b/petl/io/pytables.py index 156ec1ba..9782b762 100644 --- a/petl/io/pytables.py +++ b/petl/io/pytables.py @@ -1,5 +1,4 @@ from contextlib import contextmanager -from petl.compat import string_types from petl.errors import ArgumentError @@ -112,7 +111,7 @@ def _get_hdf5_table(source, where, name, mode='r'): # source is a table h5tbl = source - elif isinstance(source, string_types): + elif isinstance(source, str): # assume source is the name of an HDF5 file, try to open it h5file = tables.open_file(source, mode=mode) @@ -147,7 +146,7 @@ def _get_hdf5_file(source, mode='r'): needs_closing = False # allow for polymorphic args - if isinstance(source, string_types): + if isinstance(source, str): # assume source is the name of an HDF5 file, try to open it h5file = tables.open_file(source, mode=mode) diff --git a/petl/io/remotes.py b/petl/io/remotes.py index fb38dc13..d5d30bec 100644 --- a/petl/io/remotes.py +++ b/petl/io/remotes.py @@ -2,7 +2,6 @@ import sys from contextlib import contextmanager -from petl.compat import PY3 from petl.io.sources import register_reader, register_writer, get_reader, get_writer logger = logging.getLogger(__name__) @@ -133,8 +132,7 @@ def _try_register_filesystems(): raise ImportError("# ERROR: failed to register fsspec filesystems", ex) -if PY3: - _try_register_filesystems() +_try_register_filesystems() # endregion @@ -225,10 +223,7 @@ def _parse_smb_url(url): if not url.startswith("smb://"): raise ValueError(e + url) - if PY3: - from urllib.parse import urlparse - else: - from urlparse import urlparse + from urllib.parse import urlparse parsed = urlparse(url) if not parsed.path: raise ValueError(e + url) diff --git a/petl/io/sources.py b/petl/io/sources.py index 21dcdde7..a96e73f5 100644 --- a/petl/io/sources.py +++ b/petl/io/sources.py @@ -7,10 +7,11 @@ from contextlib import contextmanager import subprocess import logging +from urllib.request import urlopen +from io import StringIO, BytesIO from petl.errors import ArgumentError -from petl.compat import urlopen, StringIO, BytesIO, string_types, PY2 logger = logging.getLogger(__name__) @@ -83,10 +84,7 @@ def __init__(self, filename, membername, pwd=None, **kwargs): @contextmanager def open(self, mode): - if PY2: - mode = mode.translate(None, 'bU') - else: - mode = mode.translate({ord('b'): None, ord('U'): None}) + mode = mode.translate({ord('b'): None, ord('U'): None}) zf = zipfile.ZipFile(self.filename, mode, **self.kwargs) try: if self.pwd is not None: @@ -334,7 +332,7 @@ def _assert_source_has_open(source_class): def _register_handler(handler_type, handler_class, handler_list): - assert isinstance(handler_type, string_types), _invalid_source_msg % handler_type + assert isinstance(handler_type, str), _invalid_source_msg % handler_type assert isinstance(handler_class, type), _invalid_source_msg % handler_type _assert_source_has_open(handler_class) handler_list[handler_type] = handler_class @@ -342,7 +340,7 @@ def _register_handler(handler_type, handler_class, handler_list): def _get_handler(handler_type, handler_list): - if isinstance(handler_type, string_types): + if isinstance(handler_type, str): if handler_type in handler_list: return handler_list[handler_type] return None @@ -434,7 +432,7 @@ def _get_handler_from(source, handlers): def _resolve_source_from_arg(source, handlers): if source is None: return StdinSource() - elif isinstance(source, string_types): + elif isinstance(source, str): handler = _get_handler_from(source, handlers) codec = _get_codec_for(source) if handler is None: diff --git a/petl/io/text.py b/petl/io/text.py index e2da6374..daa1a3c1 100644 --- a/petl/io/text.py +++ b/petl/io/text.py @@ -1,6 +1,5 @@ # standard library dependencies import io -from petl.compat import next, PY2, text_type # internal dependencies @@ -73,14 +72,10 @@ def __iter__(self): with self.source.open('rb') as buf: # deal with text encoding - if PY2: - codec = getcodec(self.encoding) - f = codec.streamreader(buf, errors=self.errors) - else: - f = io.TextIOWrapper(buf, - encoding=self.encoding, - errors=self.errors, - newline='') + f = io.TextIOWrapper(buf, + encoding=self.encoding, + errors=self.errors, + newline='') # generate the table try: @@ -93,8 +88,7 @@ def __iter__(self): for line in f: yield (line.strip(self.strip),) finally: - if not PY2: - f.detach() + f.detach() def totext(table, source=None, encoding=None, errors='strict', template=None, @@ -176,14 +170,10 @@ def _writetext(table, source, mode, encoding, errors, template, prologue, with source.open(mode) as buf: # deal with text encoding - if PY2: - codec = getcodec(encoding) - f = codec.streamwriter(buf, errors=errors) - else: - f = io.TextIOWrapper(buf, - encoding=encoding, - errors=errors, - newline='') + f = io.TextIOWrapper(buf, + encoding=encoding, + errors=errors, + newline='') # write the table try: @@ -191,7 +181,7 @@ def _writetext(table, source, mode, encoding, errors, template, prologue, f.write(prologue) it = iter(table) hdr = next(it) - flds = list(map(text_type, hdr)) + flds = list(map(str, hdr)) for row in it: rec = asdict(flds, row) s = template.format(**rec) @@ -201,8 +191,7 @@ def _writetext(table, source, mode, encoding, errors, template, prologue, f.flush() finally: - if not PY2: - f.detach() + f.detach() def teetext(table, source=None, encoding=None, errors='strict', template=None, @@ -249,13 +238,9 @@ def _iterteetext(table, source, encoding, errors, template, prologue, epilogue): with source.open('wb') as buf: # deal with text encoding - if PY2: - codec = getcodec(encoding) - f = codec.streamwriter(buf, errors=errors) - else: - f = io.TextIOWrapper(buf, - encoding=encoding, - errors=errors) + f = io.TextIOWrapper(buf, + encoding=encoding, + errors=errors) # write the data try: @@ -264,7 +249,7 @@ def _iterteetext(table, source, encoding, errors, template, prologue, epilogue): it = iter(table) hdr = next(it) yield tuple(hdr) - flds = list(map(text_type, hdr)) + flds = list(map(str, hdr)) for row in it: rec = asdict(flds, row) s = template.format(**rec) @@ -275,5 +260,4 @@ def _iterteetext(table, source, encoding, errors, template, prologue, epilogue): f.flush() finally: - if not PY2: - f.detach() + f.detach() diff --git a/petl/io/whoosh.py b/petl/io/whoosh.py index 667a5feb..525b5107 100644 --- a/petl/io/whoosh.py +++ b/petl/io/whoosh.py @@ -1,5 +1,4 @@ import operator -from petl.compat import string_types, izip from petl.errors import ArgumentError @@ -73,7 +72,7 @@ def __iter__(self): def itertextindex(index_or_dirname, indexname, docnum_field): import whoosh.index - if isinstance(index_or_dirname, string_types): + if isinstance(index_or_dirname, str): dirname = index_or_dirname index = whoosh.index.open_dir(dirname, indexname=indexname, readonly=True) @@ -168,7 +167,7 @@ def totextindex(table, index_or_dirname, schema=None, indexname=None, import whoosh.writing # deal with polymorphic argument - if isinstance(index_or_dirname, string_types): + if isinstance(index_or_dirname, str): dirname = index_or_dirname index = whoosh.index.create_in(dirname, schema, indexname=indexname) @@ -222,7 +221,7 @@ def appendtextindex(table, index_or_dirname, indexname=None, merge=True, import whoosh.index # deal with polymorphic argument - if isinstance(index_or_dirname, string_types): + if isinstance(index_or_dirname, str): dirname = index_or_dirname index = whoosh.index.open_dir(dirname, indexname=indexname, readonly=False) @@ -408,7 +407,7 @@ def itersearchindex(index_or_dirname, query, limit, pagenum, pagelen, indexname, if not search_kwargs: search_kwargs = dict() - if isinstance(index_or_dirname, string_types): + if isinstance(index_or_dirname, str): dirname = index_or_dirname index = whoosh.index.open_dir(dirname, indexname=indexname, @@ -434,7 +433,7 @@ def itersearchindex(index_or_dirname, query, limit, pagenum, pagelen, indexname, yield hdr # parse the query - if isinstance(query, string_types): + if isinstance(query, str): # search all fields by default parser = whoosh.qparser.MultifieldParser( index.schema.names(), @@ -468,7 +467,7 @@ def itersearchindex(index_or_dirname, query, limit, pagenum, pagelen, indexname, else: - for (docnum, score), doc in izip(results.items(), results): + for (docnum, score), doc in zip(results.items(), results): row = tuple() if docnum_field is not None: row += (docnum,) diff --git a/petl/io/xls.py b/petl/io/xls.py index 3b062fa0..212d2c69 100644 --- a/petl/io/xls.py +++ b/petl/io/xls.py @@ -1,7 +1,7 @@ import locale +from itertools import zip_longest -from petl.compat import izip_longest, next, xrange, BytesIO from petl.util.base import Table from petl.io.sources import read_source_from_arg, write_source_from_arg @@ -56,7 +56,7 @@ def __iter__(self): ws = wb.sheet_by_index(self.sheet) else: ws = wb.sheet_by_name(str(self.sheet)) - for rownum in xrange(ws.nrows): + for rownum in range(ws.nrows): yield tuple(ws.row_values(rownum)) @@ -90,8 +90,8 @@ def toxls(tbl, filename, sheet, encoding=None, style_compression=0, # convert to list for easy zipping styles = [styles[f] for f in flds] for r, row in enumerate(it): - for c, (v, style) in enumerate(izip_longest(row, styles, - fillvalue=None)): + for c, (v, style) in enumerate(zip_longest(row, styles, + fillvalue=None)): ws.write(r+1, c, label=v, style=style) target = write_source_from_arg(filename) diff --git a/petl/io/xlsx.py b/petl/io/xlsx.py index 5905c0f2..c64857b8 100644 --- a/petl/io/xlsx.py +++ b/petl/io/xlsx.py @@ -1,4 +1,3 @@ -from petl.compat import PY3 from petl.util.base import Table, data from petl.io.sources import read_source_from_arg, write_source_from_arg @@ -118,11 +117,6 @@ def toxlsx(tbl, filename, sheet=None, write_header=True, mode="replace"): def _load_or_create_workbook(filename, mode, sheet): - if PY3: - FileNotFound = FileNotFoundError - else: - FileNotFound = IOError - import openpyxl wb = None if not (mode == "overwrite" or (mode == "replace" and sheet is None)): @@ -130,7 +124,7 @@ def _load_or_create_workbook(filename, mode, sheet): source = read_source_from_arg(filename) with source.open('rb') as source2: wb = openpyxl.load_workbook(filename=source2, read_only=False) - except FileNotFound: + except FileNotFoundError: wb = None if wb is None: wb = openpyxl.Workbook(write_only=True) diff --git a/petl/io/xlutils_view.py b/petl/io/xlutils_view.py index e20f0d25..aaf60ddb 100644 --- a/petl/io/xlutils_view.py +++ b/petl/io/xlutils_view.py @@ -6,7 +6,6 @@ from datetime import datetime, time -from petl.compat import xrange class Index: @@ -71,7 +70,7 @@ def __init__(self, book, sheet, row_slice=None, col_slice=None): stop = max(0, max_n + stop_val) elif stop_val > 0: stop = min(max_n, stop_val) - setattr(self, name, xrange(start, stop)) + setattr(self, name, range(start, stop)) def __row(self, rowx): from xlrd import XL_CELL_DATE, xldate_as_tuple diff --git a/petl/io/xml.py b/petl/io/xml.py index 1d0377ba..a7633ef6 100644 --- a/petl/io/xml.py +++ b/petl/io/xml.py @@ -7,7 +7,6 @@ from operator import attrgetter import itertools -from petl.compat import string_types, text_type # internal dependencies @@ -147,7 +146,7 @@ class XmlView(Table): def __init__(self, source, *args, **kwargs): self.source = source self.args = args - if len(args) == 2 and isinstance(args[1], (string_types, tuple, list)): + if len(args) == 2 and isinstance(args[1], (str, tuple, list)): self.rmatch = args[0] self.vmatch = args[1] self.vdict = None @@ -185,7 +184,7 @@ def __iter__(self): getv = attrgetter('text') else: getv = lambda e: e.get(self.attr) - if isinstance(vmatch, string_types): + if isinstance(vmatch, str): # match only one path velms = rowelm.findall(vmatch) else: @@ -199,7 +198,7 @@ def __iter__(self): # difficult case, deal with different paths for each field # determine output header - flds = tuple(sorted(map(text_type, vdict.keys()))) + flds = tuple(sorted(map(str, vdict.keys()))) yield flds # setup value getters @@ -207,7 +206,7 @@ def __iter__(self): vgetters = dict() for f in flds: vmatch = self.vdict[f] - if isinstance(vmatch, string_types): + if isinstance(vmatch, str): # match element path vmatches[f] = vmatch vgetters[f] = element_text_getter(self.missing) diff --git a/petl/test/helpers.py b/petl/test/helpers.py index de7fd839..39c7b045 100644 --- a/petl/test/helpers.py +++ b/petl/test/helpers.py @@ -1,15 +1,15 @@ import sys from nose.tools import eq_, assert_almost_equal +from itertools import zip_longest -from petl.compat import izip_longest def ieq(expect, actual, cast=None): '''test when values are equals for eacfh row and column''' ie = iter(expect) ia = iter(actual) - for re, ra in izip_longest(ie, ia, fillvalue=None): + for re, ra in zip_longest(ie, ia, fillvalue=None): if cast: ra = cast(ra) if re is None and ra is None: @@ -17,9 +17,9 @@ def ieq(expect, actual, cast=None): if type(re) in (int, float, bool, str): eq_(re, ra) continue - for ve, va in izip_longest(re, ra, fillvalue=None): + for ve, va in zip_longest(re, ra, fillvalue=None): if isinstance(ve, list): - for je, ja in izip_longest(ve, va, fillvalue=None): + for je, ja in zip_longest(ve, va, fillvalue=None): _eq_print(je, ja, re, ra) elif not isinstance(ve, dict): _eq_print(ve, va, re, ra) diff --git a/petl/test/io/test_avro.py b/petl/test/io/test_avro.py index 9621451e..ca037ae7 100644 --- a/petl/test/io/test_avro.py +++ b/petl/test/io/test_avro.py @@ -5,7 +5,6 @@ from decimal import Decimal from tempfile import NamedTemporaryFile -from petl.compat import PY3 from petl.transform.basics import cat from petl.util.base import dicts from petl.util.vis import look @@ -17,8 +16,7 @@ from petl.test.io.test_avro_schemas import schema0, schema1, schema2, \ schema3, schema4, schema5, schema6 -if PY3: - from datetime import timezone +from datetime import timezone try: import fastavro @@ -186,9 +184,7 @@ def _decs(float_value, rounding=12): def _utc(year, month, day, hour=0, minute=0, second=0, microsecond=0): u = datetime(year, month, day, hour, minute, second, microsecond) - if PY3: - return u.replace(tzinfo=timezone.utc) - return u.replace(tzinfo=pytz.utc) + return u.replace(tzinfo=timezone.utc) def _get_tempfile_path(delete_on_close=False): f = NamedTemporaryFile(delete=delete_on_close, mode='r') diff --git a/petl/test/io/test_csv.py b/petl/test/io/test_csv.py index 17328196..2b781d65 100644 --- a/petl/test/io/test_csv.py +++ b/petl/test/io/test_csv.py @@ -2,7 +2,6 @@ import gzip import os import logging -from petl.compat import PY2 from petl.test.helpers import ieq, eq_ @@ -108,12 +107,7 @@ def test_fromcsv_gz(): ('b', '2'), ('c', '2')) - # '\r' not supported in PY2 because universal newline mode is - # not supported by gzip module - if PY2: - lts = b'\n', b'\r\n' - else: - lts = b'\r', b'\n', b'\r\n' + lts = b'\r', b'\n', b'\r\n' for lt in lts: f = NamedTemporaryFile(delete=False) f.close() diff --git a/petl/test/io/test_db.py b/petl/test/io/test_db.py index 0f2f3ed4..d0dd640e 100644 --- a/petl/test/io/test_db.py +++ b/petl/test/io/test_db.py @@ -1,6 +1,5 @@ import sqlite3 from tempfile import NamedTemporaryFile -from petl.compat import next from petl.test.helpers import ieq, eq_ diff --git a/petl/test/io/test_pickle.py b/petl/test/io/test_pickle.py index bc1ca3c8..5c0f2206 100644 --- a/petl/test/io/test_pickle.py +++ b/petl/test/io/test_pickle.py @@ -1,5 +1,5 @@ from tempfile import NamedTemporaryFile -from petl.compat import pickle +import pickle from petl.test.helpers import ieq diff --git a/petl/test/io/test_remotes.py b/petl/test/io/test_remotes.py index e10d8bc9..79ed5e4f 100644 --- a/petl/test/io/test_remotes.py +++ b/petl/test/io/test_remotes.py @@ -2,7 +2,6 @@ import os from importlib import import_module -from petl.compat import PY3 from petl.test.helpers import ieq, eq_ from petl.io.avro import fromavro, toavro from petl.io.csv import fromcsv, tocsv @@ -15,9 +14,8 @@ def test_helper_local(): - if PY3: - _ensure_dir("./tmp") - _write_read_into_url("./tmp/example.") + _ensure_dir("./tmp") + _write_read_into_url("./tmp/example.") def test_helper_fsspec(): diff --git a/petl/test/io/test_sources.py b/petl/test/io/test_sources.py index 774fb373..db10c80a 100644 --- a/petl/test/io/test_sources.py +++ b/petl/test/io/test_sources.py @@ -2,7 +2,6 @@ import bz2 import zipfile from tempfile import NamedTemporaryFile -from petl.compat import PY2 from petl.test.helpers import ieq, eq_ @@ -20,9 +19,7 @@ def test_memorysource(): # test writing to a string buffer ss = MemorySource() etl.tocsv(tbl1, ss) - expect = "foo,bar\r\na,1\r\nb,2\r\nc,2\r\n" - if not PY2: - expect = expect.encode('ascii') + expect = "foo,bar\r\na,1\r\nb,2\r\nc,2\r\n".encode('ascii') actual = ss.getvalue() eq_(expect, actual) @@ -34,17 +31,13 @@ def test_memorysource(): # test appending etl.appendcsv(tbl1, ss) actual = ss.getvalue() - expect = "foo,bar\r\na,1\r\nb,2\r\nc,2\r\na,1\r\nb,2\r\nc,2\r\n" - if not PY2: - expect = expect.encode('ascii') + expect = "foo,bar\r\na,1\r\nb,2\r\nc,2\r\na,1\r\nb,2\r\nc,2\r\n".encode('ascii') eq_(expect, actual) def test_memorysource_2(): - data = 'foo,bar\r\na,1\r\nb,2\r\nc,2\r\n' - if not PY2: - data = data.encode('ascii') + data = 'foo,bar\r\na,1\r\nb,2\r\nc,2\r\n'.encode('ascii') actual = etl.fromcsv(MemorySource(data)) expect = (('foo', 'bar'), ('a', '1'), diff --git a/petl/test/io/test_xml.py b/petl/test/io/test_xml.py index 50953c42..153b621a 100644 --- a/petl/test/io/test_xml.py +++ b/petl/test/io/test_xml.py @@ -1,11 +1,11 @@ import sys from collections import OrderedDict from tempfile import NamedTemporaryFile +from urllib.request import urlopen from petl.test.helpers import ieq from petl.util import nrows, look from petl.io.xml import fromxml, toxml -from petl.compat import urlopen def test_fromxml(): diff --git a/petl/test/test_fluent.py b/petl/test/test_fluent.py index 24b904a8..9d2360d2 100644 --- a/petl/test/test_fluent.py +++ b/petl/test/test_fluent.py @@ -1,6 +1,4 @@ from tempfile import NamedTemporaryFile -import csv -from petl.compat import PY2 import petl as etl diff --git a/petl/test/transform/test_regex.py b/petl/test/transform/test_regex.py index 28b58222..f0137fa7 100644 --- a/petl/test/transform/test_regex.py +++ b/petl/test/transform/test_regex.py @@ -1,6 +1,3 @@ -from petl.compat import next - - from petl.test.helpers import ieq, eq_ from petl.transform.regex import capture, split, search, searchcomplement, splitdown from petl.transform.basics import TransformError diff --git a/petl/test/transform/test_sorts.py b/petl/test/transform/test_sorts.py index c234124a..a389af87 100644 --- a/petl/test/transform/test_sorts.py +++ b/petl/test/transform/test_sorts.py @@ -3,7 +3,6 @@ import logging from datetime import datetime import sys -from petl.compat import next from petl.test.helpers import ieq, eq_ diff --git a/petl/test/util/test_base.py b/petl/test/util/test_base.py index 5a820edb..00e6e6ab 100644 --- a/petl/test/util/test_base.py +++ b/petl/test/util/test_base.py @@ -1,6 +1,4 @@ -from petl.errors import ArgumentError from petl.test.helpers import ieq, eq_ -from petl.compat import next from petl.util.base import header, fieldnames, data, dicts, records, \ namedtuples, itervalues, values, rowgroupby diff --git a/petl/test/util/test_counting.py b/petl/test/util/test_counting.py index dda8b74e..1f36e45d 100644 --- a/petl/test/util/test_counting.py +++ b/petl/test/util/test_counting.py @@ -1,4 +1,3 @@ -from petl.compat import PY2 from petl.test.helpers import ieq, eq_ from petl.util.counting import valuecount, valuecounter, valuecounts, \ rowlengths, typecounts, parsecounts, stringpatterns, nrows @@ -110,25 +109,15 @@ def test_typecounts(): (b'E', 42)) actual = typecounts(table, 'foo') - if PY2: - expect = (('type', 'count', 'frequency'), - ('str', 4, 4./5), - ('unicode', 1, 1./5)) - else: - expect = (('type', 'count', 'frequency'), - ('bytes', 4, 4./5), - ('str', 1, 1./5)) + expect = (('type', 'count', 'frequency'), + ('bytes', 4, 4./5), + ('str', 1, 1./5)) ieq(expect, actual) actual = typecounts(table, 'bar') - if PY2: - expect = (('type', 'count', 'frequency'), - ('unicode', 3, 3./5), - ('int', 2, 2./5)) - else: - expect = (('type', 'count', 'frequency'), - ('str', 3, 3./5), - ('int', 2, 2./5)) + expect = (('type', 'count', 'frequency'), + ('str', 3, 3./5), + ('int', 2, 2./5)) ieq(expect, actual) actual = typecounts(table, 'baz') diff --git a/petl/test/util/test_misc.py b/petl/test/util/test_misc.py index 0019d1b4..3e1041eb 100644 --- a/petl/test/util/test_misc.py +++ b/petl/test/util/test_misc.py @@ -1,5 +1,4 @@ from petl.test.helpers import eq_ -from petl.compat import PY2 from petl.util.misc import typeset, diffvalues, diffheaders @@ -13,10 +12,7 @@ def test_typeset(): (b'E', 42)) actual = typeset(table, 'foo') - if PY2: - expect = {'str', 'unicode'} - else: - expect = {'bytes', 'str'} + expect = {'bytes', 'str'} eq_(expect, actual) diff --git a/petl/test/util/test_parsers.py b/petl/test/util/test_parsers.py index d15945d1..1670e156 100644 --- a/petl/test/util/test_parsers.py +++ b/petl/test/util/test_parsers.py @@ -1,4 +1,4 @@ -from petl.compat import maxint +import sys from petl.test.helpers import eq_ from petl.util.parsers import numparser, datetimeparser @@ -8,7 +8,7 @@ def test_numparser(): parsenumber = numparser() assert parsenumber('1') == 1 assert parsenumber('1.0') == 1.0 - assert parsenumber(str(maxint + 1)) == maxint + 1 + assert parsenumber(str(sys.maxsize + 1)) == sys.maxsize + 1 assert parsenumber('3+4j') == 3 + 4j assert parsenumber('aaa') == 'aaa' assert parsenumber(None) is None @@ -19,7 +19,7 @@ def test_numparser_strict(): parsenumber = numparser(strict=True) assert parsenumber('1') == 1 assert parsenumber('1.0') == 1.0 - assert parsenumber(str(maxint + 1)) == maxint + 1 + assert parsenumber(str(sys.maxsize + 1)) == sys.maxsize + 1 assert parsenumber('3+4j') == 3 + 4j try: parsenumber('aaa') diff --git a/petl/transform/basics.py b/petl/transform/basics.py index 32eb3404..d2b58156 100644 --- a/petl/transform/basics.py +++ b/petl/transform/basics.py @@ -1,8 +1,6 @@ # standard library dependencies -from itertools import islice, chain +from itertools import count, chain, islice, zip_longest from collections import deque -from itertools import count -from petl.compat import izip, izip_longest, next, string_types, text_type # internal dependencies @@ -524,7 +522,7 @@ def __iter__(self): def iteraddfield(source, field, value, index): it = iter(source) hdr = next(it) - flds = list(map(text_type, hdr)) + flds = list(map(str, hdr)) # determine index of new field if index is None: @@ -613,7 +611,7 @@ def __iter__(self): def iteraddfields(source, field_defs): it = iter(source) hdr = next(it) - flds = list(map(text_type, hdr)) + flds = list(map(str, hdr)) # initialize output fields and indices outhdr = list(hdr) @@ -876,7 +874,7 @@ def __iter__(self): def iterskipcomments(source, prefix): return (row for row in source if (len(row) > 0 - and not(isinstance(row[0], string_types) + and not(isinstance(row[0], str) and row[0].startswith(prefix)))) @@ -974,7 +972,7 @@ def iterannex(tables, missing): hdrs = [next(it) for it in its] outhdr = tuple(chain(*hdrs)) yield outhdr - for rows in izip_longest(*its): + for rows in zip_longest(*its): outrow = list() for i, row in enumerate(rows): lh = len(hdrs[i]) @@ -1040,7 +1038,7 @@ def iteraddrownumbers(table, start, step, field): outhdr = [field] outhdr.extend(hdr) yield tuple(outhdr) - for row, n in izip(it, count(start, step)): + for row, n in zip(it, count(start, step)): outrow = [n] outrow.extend(row) yield tuple(outrow) @@ -1103,7 +1101,7 @@ def iteraddcolumn(table, field, col, index, missing): yield tuple(outhdr) # construct output data - for row, val in izip_longest(it, col, fillvalue=missing): + for row, val in zip_longest(it, col, fillvalue=missing): # run out of rows? if row == missing: row = [missing] * len(hdr) @@ -1181,7 +1179,7 @@ def __iter__(self): def iteraddfieldusingcontext(table, field, query): it = iter(table) hdr = tuple(next(it)) - flds = list(map(text_type, hdr)) + flds = list(map(str, hdr)) yield hdr + (field,) flds.append(field) it = (Record(row, flds) for row in it) diff --git a/petl/transform/conversions.py b/petl/transform/conversions.py index 5b7c17a8..35e99120 100644 --- a/petl/transform/conversions.py +++ b/petl/transform/conversions.py @@ -1,6 +1,3 @@ -from petl.compat import next, integer_types, string_types, text_type - - import petl.config as config from petl.errors import ArgumentError, FieldSelectionError from petl.util.base import Table, expr, header, Record @@ -352,7 +349,7 @@ def iterfieldconvert(source, converters, failonerror, errorvalue, where, # grab the fields in the source table it = iter(source) hdr = next(it) - flds = list(map(text_type, hdr)) + flds = list(map(str, hdr)) yield tuple(hdr) # these are not modified # build converter functions @@ -360,7 +357,7 @@ def iterfieldconvert(source, converters, failonerror, errorvalue, where, for k, c in converters.items(): # turn field names into row indices - if not isinstance(k, integer_types): + if not isinstance(k, int): try: k = flds.index(k) except ValueError: # not in list @@ -372,11 +369,11 @@ def iterfieldconvert(source, converters, failonerror, errorvalue, where, converter_functions[k] = c # is converter a method name? - elif isinstance(c, string_types): + elif isinstance(c, str): converter_functions[k] = methodcaller(c) # is converter a method name with arguments? - elif isinstance(c, (tuple, list)) and isinstance(c[0], string_types): + elif isinstance(c, (tuple, list)) and isinstance(c[0], str): methnm = c[0] methargs = c[1:] converter_functions[k] = methodcaller(methnm, *methargs) @@ -420,7 +417,7 @@ def transform_row(_row): for i, v in enumerate(_row)) # prepare where function - if isinstance(where, string_types): + if isinstance(where, str): where = expr(where) elif where is not None: assert callable(where), 'expected callable for "where" argument, ' \ diff --git a/petl/transform/dedup.py b/petl/transform/dedup.py index e4e3e6fe..9a210724 100644 --- a/petl/transform/dedup.py +++ b/petl/transform/dedup.py @@ -1,5 +1,4 @@ import operator -from petl.compat import text_type from petl.util.base import Table, asindices, itervalues @@ -324,7 +323,7 @@ def iterconflicts(source, key, missing, exclude, include): it = iter(source) hdr = next(it) - flds = list(map(text_type, hdr)) + flds = list(map(str, hdr)) yield tuple(hdr) # convert field selection into field indices diff --git a/petl/transform/fills.py b/petl/transform/fills.py index c212b57c..dbb65a8a 100644 --- a/petl/transform/fills.py +++ b/petl/transform/fills.py @@ -1,6 +1,3 @@ -from petl.compat import next - - from petl.util.base import Table, asindices diff --git a/petl/transform/hashjoins.py b/petl/transform/hashjoins.py index 19bff0c1..cfcde369 100644 --- a/petl/transform/hashjoins.py +++ b/petl/transform/hashjoins.py @@ -1,5 +1,4 @@ import operator -from petl.compat import next, text_type from petl.util.base import Table, asindices, rowgetter, iterpeek @@ -76,12 +75,12 @@ def iterhashjoin(left, right, lkey, rkey, rlookup, lprefix, rprefix): if lprefix is None: outhdr = list(lhdr) else: - outhdr = [(text_type(lprefix) + text_type(f)) + outhdr = [(str(lprefix) + str(f)) for f in lhdr] if rprefix is None: outhdr.extend(rgetv(rhdr)) else: - outhdr.extend([(text_type(rprefix) + text_type(f)) for f in rgetv(rhdr)]) + outhdr.extend([(str(rprefix) + str(f)) for f in rgetv(rhdr)]) yield tuple(outhdr) # define a function to join rows @@ -172,12 +171,12 @@ def iterhashleftjoin(left, right, lkey, rkey, missing, rlookup, lprefix, if lprefix is None: outhdr = list(lhdr) else: - outhdr = [(text_type(lprefix) + text_type(f)) + outhdr = [(str(lprefix) + str(f)) for f in lhdr] if rprefix is None: outhdr.extend(rgetv(rhdr)) else: - outhdr.extend([(text_type(rprefix) + text_type(f)) for f in rgetv(rhdr)]) + outhdr.extend([(str(rprefix) + str(f)) for f in rgetv(rhdr)]) yield tuple(outhdr) # define a function to join rows @@ -274,12 +273,12 @@ def iterhashrightjoin(left, right, lkey, rkey, missing, llookup, lprefix, if lprefix is None: outhdr = list(lhdr) else: - outhdr = [(text_type(lprefix) + text_type(f)) + outhdr = [(str(lprefix) + str(f)) for f in lhdr] if rprefix is None: outhdr.extend(rgetv(rhdr)) else: - outhdr.extend([(text_type(rprefix) + text_type(f)) + outhdr.extend([(str(rprefix) + str(f)) for f in rgetv(rhdr)]) yield tuple(outhdr) @@ -432,12 +431,12 @@ def iterhashlookupjoin(left, right, lkey, rkey, missing, lprefix, rprefix): if lprefix is None: outhdr = list(lhdr) else: - outhdr = [(text_type(lprefix) + text_type(f)) + outhdr = [(str(lprefix) + str(f)) for f in lhdr] if rprefix is None: outhdr.extend(rgetv(rhdr)) else: - outhdr.extend([(text_type(rprefix) + text_type(f)) + outhdr.extend([(str(rprefix) + str(f)) for f in rgetv(rhdr)]) yield tuple(outhdr) diff --git a/petl/transform/headers.py b/petl/transform/headers.py index 4545d23a..70d11099 100644 --- a/petl/transform/headers.py +++ b/petl/transform/headers.py @@ -1,5 +1,4 @@ import itertools -from petl.compat import next, text_type from petl.errors import FieldSelectionError @@ -77,7 +76,7 @@ def __setitem__(self, key, value): def iterrename(source, spec, strict): it = iter(source) hdr = next(it) - flds = list(map(text_type, hdr)) + flds = list(map(str, hdr)) if strict: for x in spec: if isinstance(x, int): @@ -306,7 +305,7 @@ def __init__(self, table, prefix): def __iter__(self): it = iter(self.table) hdr = next(it) - outhdr = tuple((text_type(self.prefix) + text_type(f)) for f in hdr) + outhdr = tuple((str(self.prefix) + str(f)) for f in hdr) yield outhdr yield from it @@ -329,7 +328,7 @@ def __init__(self, table, suffix): def __iter__(self): it = iter(self.table) hdr = next(it) - outhdr = tuple((text_type(f) + text_type(self.suffix)) for f in hdr) + outhdr = tuple((str(f) + str(self.suffix)) for f in hdr) yield outhdr yield from it diff --git a/petl/transform/intervals.py b/petl/transform/intervals.py index abdbff5a..226e914b 100644 --- a/petl/transform/intervals.py +++ b/petl/transform/intervals.py @@ -1,5 +1,4 @@ from operator import itemgetter, attrgetter -from petl.compat import text_type from petl.util.base import asindices, records, Table, values, rowgroupby @@ -19,7 +18,7 @@ def tupletree(table, start='start', stop='stop', value=None): tree = intervaltree.IntervalTree() it = iter(table) hdr = next(it) - flds = list(map(text_type, hdr)) + flds = list(map(str, hdr)) assert start in flds, 'start field not recognised' assert stop in flds, 'stop field not recognised' getstart = itemgetter(flds.index(start)) @@ -45,7 +44,7 @@ def facettupletrees(table, key, start='start', stop='stop', value=None): import intervaltree it = iter(table) hdr = next(it) - flds = list(map(text_type, hdr)) + flds = list(map(str, hdr)) assert start in flds, 'start field not recognised' assert stop in flds, 'stop field not recognised' getstart = itemgetter(flds.index(start)) @@ -737,10 +736,10 @@ def iterintervaljoin(left, right, lstart, lstop, rstart, rstop, lkey, # create iterators and obtain fields lit = iter(left) lhdr = next(lit) - lflds = list(map(text_type, lhdr)) + lflds = list(map(str, lhdr)) rit = iter(right) rhdr = next(rit) - rflds = list(map(text_type, rhdr)) + rflds = list(map(str, rhdr)) # check fields via petl.util.asindices (raises FieldSelectionError if spec # is not valid) @@ -898,7 +897,7 @@ def iterintervalsubtract(left, right, lstart, lstop, rstart, rstop, lkey, rkey, # create iterators and obtain fields lit = iter(left) lhdr = next(lit) - lflds = list(map(text_type, lhdr)) + lflds = list(map(str, lhdr)) rit = iter(right) rhdr = next(rit) diff --git a/petl/transform/joins.py b/petl/transform/joins.py index 9c7b0730..79d7a677 100644 --- a/petl/transform/joins.py +++ b/petl/transform/joins.py @@ -1,6 +1,5 @@ import itertools import operator -from petl.compat import next, text_type from petl.errors import ArgumentError @@ -349,11 +348,11 @@ def iterjoin(left, right, lkey, rkey, leftouter=False, rightouter=False, if lprefix is None: outhdr = list(lhdr) else: - outhdr = [(text_type(lprefix) + text_type(f)) for f in lhdr] + outhdr = [(str(lprefix) + str(f)) for f in lhdr] if rprefix is None: outhdr.extend(rgetv(rhdr)) else: - outhdr.extend([(text_type(rprefix) + text_type(f)) for f in rgetv(rhdr)]) + outhdr.extend([(str(rprefix) + str(f)) for f in rgetv(rhdr)]) yield tuple(outhdr) # define a function to join two groups of rows @@ -498,7 +497,7 @@ def itercrossjoin(sources, prefix): for i, s in enumerate(sources): if prefix: # use one-based numbering - outhdr.extend([text_type(i+1) + '_' + text_type(f) for f in header(s)]) + outhdr.extend([str(i+1) + '_' + str(f) for f in header(s)]) else: outhdr.extend(header(s)) yield tuple(outhdr) @@ -733,11 +732,11 @@ def iterlookupjoin(left, right, lkey, rkey, missing=None, lprefix=None, if lprefix is None: outhdr = list(lhdr) else: - outhdr = [(text_type(lprefix) + text_type(f)) for f in lhdr] + outhdr = [(str(lprefix) + str(f)) for f in lhdr] if rprefix is None: outhdr.extend(rgetv(rhdr)) else: - outhdr.extend([(text_type(rprefix) + text_type(f)) for f in rgetv(rhdr)]) + outhdr.extend([(str(rprefix) + str(f)) for f in rgetv(rhdr)]) yield tuple(outhdr) # define a function to join two groups of rows diff --git a/petl/transform/maps.py b/petl/transform/maps.py index fa388565..8a0ff8ff 100644 --- a/petl/transform/maps.py +++ b/petl/transform/maps.py @@ -1,6 +1,5 @@ import operator from collections import OrderedDict -from petl.compat import next, string_types, text_type import petl.config as config @@ -86,7 +85,7 @@ def __iter__(self): def iterfieldmap(source, mappings, failonerror, errorvalue): it = iter(source) hdr = next(it) - flds = list(map(text_type, hdr)) + flds = list(map(str, hdr)) outhdr = mappings.keys() yield tuple(outhdr) @@ -96,7 +95,7 @@ def iterfieldmap(source, mappings, failonerror, errorvalue): mapfuns[outfld] = operator.itemgetter(m) elif isinstance(m, int) and m < len(hdr): mapfuns[outfld] = operator.itemgetter(m) - elif isinstance(m, string_types): + elif isinstance(m, str): mapfuns[outfld] = expr(m) elif callable(m): mapfuns[outfld] = m @@ -212,7 +211,7 @@ def __iter__(self): def iterrowmap(source, rowmapper, header, failonerror): it = iter(source) hdr = next(it) - flds = list(map(text_type, hdr)) + flds = list(map(str, hdr)) yield tuple(header) it = (Record(row, flds) for row in it) for row in it: @@ -306,7 +305,7 @@ def __iter__(self): def iterrowmapmany(source, rowgenerator, header, failonerror): it = iter(source) hdr = next(it) - flds = list(map(text_type, hdr)) + flds = list(map(str, hdr)) yield tuple(header) it = (Record(row, flds) for row in it) for row in it: diff --git a/petl/transform/reductions.py b/petl/transform/reductions.py index bb8768dd..b9eb3b79 100644 --- a/petl/transform/reductions.py +++ b/petl/transform/reductions.py @@ -1,7 +1,7 @@ import itertools import operator from collections import OrderedDict -from petl.compat import next, string_types, reduce, text_type +from functools import reduce from petl.errors import ArgumentError @@ -326,9 +326,9 @@ def itermultiaggregate(source, key, aggregation): agg = aggregation[outfld] if callable(agg): aggregation[outfld] = None, agg - elif isinstance(agg, string_types): + elif isinstance(agg, str): aggregation[outfld] = agg, list # list is default - elif len(agg) == 1 and isinstance(agg[0], string_types): + elif len(agg) == 1 and isinstance(agg[0], str): aggregation[outfld] = agg[0], list # list is default elif len(agg) == 1 and callable(agg[0]): aggregation[outfld] = None, agg[0] # aggregate whole rows @@ -575,10 +575,10 @@ def __iter__(self): def itermergeduplicates(table, key, missing): it = iter(table) hdr, it = iterpeek(it) - flds = list(map(text_type, hdr)) + flds = list(map(str, hdr)) # determine output fields - if isinstance(key, string_types): + if isinstance(key, str): outhdr = [key] keyflds = {key} else: @@ -592,7 +592,7 @@ def itermergeduplicates(table, key, missing): # do the work for k, grp in rowgroupby(it, key): grp = list(grp) - if isinstance(key, string_types): + if isinstance(key, str): outrow = [k] else: outrow = list(k) diff --git a/petl/transform/regex.py b/petl/transform/regex.py index 8e42a1ad..1af6aadf 100644 --- a/petl/transform/regex.py +++ b/petl/transform/regex.py @@ -1,6 +1,5 @@ import re import operator -from petl.compat import next, text_type from petl.errors import ArgumentError @@ -99,7 +98,7 @@ def itercapture(source, field, pattern, newfields, include_original, flags, prog = re.compile(pattern, flags) hdr = next(it) - flds = list(map(text_type, hdr)) + flds = list(map(str, hdr)) if isinstance(field, int) and field < len(hdr): field_index = field elif field in flds: @@ -196,7 +195,7 @@ def itersplit(source, field, pattern, newfields, include_original, maxsplit, prog = re.compile(pattern, flags) hdr = next(it) - flds = list(map(text_type, hdr)) + flds = list(map(str, hdr)) if isinstance(field, int) and field < len(hdr): field_index = field field = hdr[field_index] @@ -311,20 +310,20 @@ def itersearch(table, pattern, field, flags, complement): prog = re.compile(pattern, flags) it = iter(table) hdr = next(it) - flds = list(map(text_type, hdr)) + flds = list(map(str, hdr)) yield tuple(hdr) if field is None: # search whole row - test = lambda r: any(prog.search(text_type(v)) for v in r) + test = lambda r: any(prog.search(str(v)) for v in r) else: indices = asindices(hdr, field) if len(indices) == 1: index = indices[0] - test = lambda r: prog.search(text_type(r[index])) + test = lambda r: prog.search(str(r[index])) else: getvals = operator.itemgetter(*indices) - test = lambda r: any(prog.search(text_type(v)) for v in getvals(r)) + test = lambda r: any(prog.search(str(v)) for v in getvals(r)) # complement==False, return rows that match if not complement: for row in it: @@ -438,7 +437,7 @@ def itersplitdown(table, field, pattern, maxsplit, flags): prog = re.compile(pattern, flags) it = iter(table) hdr = next(it) - flds = list(map(text_type, hdr)) + flds = list(map(str, hdr)) if isinstance(field, int) and field < len(hdr): field_index = field diff --git a/petl/transform/reshape.py b/petl/transform/reshape.py index 038e45b4..4f59b490 100644 --- a/petl/transform/reshape.py +++ b/petl/transform/reshape.py @@ -1,7 +1,6 @@ import itertools import collections import operator -from petl.compat import next, text_type from petl.comparison import comparable_itemgetter @@ -296,7 +295,7 @@ def iterrecast(source, key, variablefield, valuefield, it = iter(source) hdr = next(it) - flds = list(map(text_type, hdr)) + flds = list(map(str, hdr)) # normalise some stuff keyfields = key @@ -536,7 +535,7 @@ def iterpivot(source, f1, f2, f3, aggfun, missing): # second pass - generate output it = iter(source) hdr = next(it) - flds = list(map(text_type, hdr)) + flds = list(map(str, hdr)) f1i = flds.index(f1) f2i = flds.index(f2) f3i = flds.index(f3) diff --git a/petl/transform/selects.py b/petl/transform/selects.py index 7cb03043..f8631d67 100644 --- a/petl/transform/selects.py +++ b/petl/transform/selects.py @@ -1,5 +1,4 @@ import operator -from petl.compat import next, string_types, callable, text_type from petl.comparison import Comparable @@ -63,7 +62,7 @@ def select(table, *args, **kwargs): raise ArgumentError('missing positional argument') elif len(args) == 1: where = args[0] - if isinstance(where, string_types): + if isinstance(where, str): where = expr(where) else: assert callable(where), 'second argument must be string or callable' @@ -125,7 +124,7 @@ def iterfieldselect(source, field, where, complement, missing): def iterrowselect(source, where, missing, complement): it = iter(source) hdr = next(it) - flds = list(map(text_type, hdr)) + flds = list(map(str, hdr)) yield tuple(hdr) it = (Record(row, flds, missing=missing) for row in it) for row in it: @@ -419,7 +418,7 @@ def __iter__(self): def iterselectusingcontext(table, query): it = iter(table) hdr = tuple(next(it)) - flds = list(map(text_type, hdr)) + flds = list(map(str, hdr)) yield hdr it = (Record(row, flds) for row in it) prv = None diff --git a/petl/transform/setops.py b/petl/transform/setops.py index 421f709a..951b866f 100644 --- a/petl/transform/setops.py +++ b/petl/transform/setops.py @@ -1,5 +1,4 @@ from collections import Counter -from petl.compat import next from petl.comparison import Comparable from petl.util.base import header, Table from petl.transform.sorts import sort diff --git a/petl/transform/sorts.py b/petl/transform/sorts.py index 1ac4deb4..bb9852b8 100755 --- a/petl/transform/sorts.py +++ b/petl/transform/sorts.py @@ -5,7 +5,7 @@ import logging from collections import namedtuple import operator -from petl.compat import pickle, next, text_type +import pickle import petl.config as config @@ -484,7 +484,7 @@ def itermergesort(sources, key, header, missing, reverse): # determine output fields by gathering all fields found in the sources outhdr = list() for hdr in src_hdrs: - for f in list(map(text_type, hdr)): + for f in list(map(str, hdr)): if f not in outhdr: # add any new fields as we find them outhdr.append(f) @@ -494,7 +494,7 @@ def itermergesort(sources, key, header, missing, reverse): yield tuple(outhdr) def _standardisedata(it, hdr, ofs): - flds = list(map(text_type, hdr)) + flds = list(map(str, hdr)) # now construct and yield the data rows for _row in it: try: @@ -559,7 +559,7 @@ def issorted(table, key=None, reverse=False, strict=False): op = operator.ge it = iter(table) - flds = [text_type(f) for f in next(it)] + flds = [str(f) for f in next(it)] if key is None: prev = next(it) for curr in it: diff --git a/petl/transform/unpacks.py b/petl/transform/unpacks.py index 294e227d..36f33eae 100644 --- a/petl/transform/unpacks.py +++ b/petl/transform/unpacks.py @@ -1,5 +1,4 @@ import itertools -from petl.compat import next, text_type from petl.errors import ArgumentError @@ -62,7 +61,7 @@ def iterunpack(source, field, newfields, include_original, missing): it = iter(source) hdr = next(it) - flds = list(map(text_type, hdr)) + flds = list(map(str, hdr)) if field in flds: field_index = flds.index(field) elif isinstance(field, int) and field < len(flds): @@ -80,7 +79,7 @@ def iterunpack(source, field, newfields, include_original, missing): nunpack = len(newfields) elif isinstance(newfields, int): nunpack = newfields - newfields = [text_type(field) + text_type(i+1) for i in range(newfields)] + newfields = [str(field) + str(i+1) for i in range(newfields)] outhdr.extend(newfields) elif newfields is None: nunpack = 0 @@ -162,7 +161,7 @@ def iterunpackdict(table, field, keys, includeoriginal, samplesize, missing): # set up it = iter(table) hdr = next(it) - flds = list(map(text_type, hdr)) + flds = list(map(str, hdr)) fidx = flds.index(field) outhdr = list(flds) if not includeoriginal: diff --git a/petl/transform/validation.py b/petl/transform/validation.py index 03cbedfc..b275d513 100644 --- a/petl/transform/validation.py +++ b/petl/transform/validation.py @@ -1,5 +1,4 @@ import operator -from petl.compat import text_type from petl.util.base import Table, asindices, Record @@ -111,10 +110,10 @@ def iterproblems(table, constraints, expected_header): actual_header = next(it) if expected_header is None: - flds = list(map(text_type, actual_header)) + flds = list(map(str, actual_header)) else: - expected_flds = list(map(text_type, expected_header)) - actual_flds = list(map(text_type, actual_header)) + expected_flds = list(map(str, expected_header)) + actual_flds = list(map(str, actual_header)) try: assert expected_flds == actual_flds except Exception as e: diff --git a/petl/util/base.py b/petl/util/base.py index 466706cc..a51c9f9d 100644 --- a/petl/util/base.py +++ b/petl/util/base.py @@ -4,9 +4,8 @@ starmap, groupby, tee import operator from collections import Counter, namedtuple, OrderedDict -from itertools import compress, combinations_with_replacement -from petl.compat import imap, izip, izip_longest, ifilter, ifilterfalse, \ - reduce, next, string_types, text_type +from functools import reduce +from itertools import compress, combinations_with_replacement, filterfalse, zip_longest from petl.errors import FieldSelectionError @@ -112,13 +111,13 @@ def takewhile(self, predicate): return takewhile(predicate, self) def ifilter(self, predicate): - return ifilter(predicate, self) + return filter(predicate, self) def ifilterfalse(self, predicate): - return ifilterfalse(predicate, self) + return filterfalse(predicate, self) def imap(self, function): - return imap(function, self) + return map(function, self) def starmap(self, function): return starmap(function, self) @@ -145,10 +144,10 @@ def combinations_with_replacement(self, *args, **kwargs): return combinations_with_replacement(self, *args, **kwargs) def izip(self, *args, **kwargs): - return izip(self, *args, **kwargs) + return zip(self, *args, **kwargs) def izip_longest(self, *args, **kwargs): - return izip_longest(self, *args, **kwargs) + return zip_longest(self, *args, **kwargs) def product(self, *args, **kwargs): return product(self, *args, **kwargs) @@ -163,7 +162,7 @@ def __iadd__(self, other): class Table(IterContainer): def __getitem__(self, item): - if isinstance(item, string_types): + if isinstance(item, str): return ValuesView(self, item) else: return super().__getitem__(item) @@ -230,7 +229,7 @@ def __iter__(self): def __repr__(self): vreprs = list(map(repr, islice(self, 6))) - r = text_type(self.field) + ': ' + r = str(self.field) + ': ' r += ', '.join(vreprs[:5]) if len(vreprs) > 5: r += ', ...' @@ -279,7 +278,7 @@ def __iter__(self): def asindices(hdr, spec): """Convert the given field `spec` into a list of field indices.""" - flds = list(map(text_type, hdr)) + flds = list(map(str, hdr)) indices = list() if not isinstance(spec, (list, tuple)): spec = (spec,) @@ -350,7 +349,7 @@ def fieldnames(table): """ - return tuple(text_type(f) for f in header(table)) + return tuple(str(f) for f in header(table)) Table.fieldnames = fieldnames @@ -448,7 +447,7 @@ def iterdicts(table, *sliceargs, **kwargs): def asdict(hdr, row, missing=None): - flds = [text_type(f) for f in hdr] + flds = [str(f) for f in hdr] try: # list comprehension should be faster items = [(flds[i], row[i]) for i in range(len(flds))] @@ -513,7 +512,7 @@ def iternamedtuples(table, *sliceargs, **kwargs): name = kwargs.get('name', 'row') it = iter(table) hdr = next(it) - flds = list(map(text_type, hdr)) + flds = list(map(str, hdr)) nt = namedtuple(name, tuple(flds)) if sliceargs: it = islice(it, *sliceargs) @@ -635,7 +634,7 @@ def iterrecords(table, *sliceargs, **kwargs): missing = kwargs.get('missing', None) it = iter(table) hdr = next(it) - flds = list(map(text_type, hdr)) + flds = list(map(str, hdr)) if sliceargs: it = islice(it, *sliceargs) for row in it: @@ -691,7 +690,7 @@ def rowgroupby(table, key, value=None): it = iter(table) hdr = next(it) - flds = list(map(text_type, hdr)) + flds = list(map(str, hdr)) # wrap rows as records it = (Record(row, flds) for row in it) diff --git a/petl/util/counting.py b/petl/util/counting.py index b4a116af..76183d1f 100644 --- a/petl/util/counting.py +++ b/petl/util/counting.py @@ -1,5 +1,4 @@ from collections import Counter -from petl.compat import string_types, maketrans from petl.util.base import values, Table, data, wrap @@ -192,7 +191,7 @@ def parsecounter(table, field, parsers=(('int', int), ('float', float))): counter[n] = 0 errors[n] = 0 for v in values(table, field): - if isinstance(v, string_types): + if isinstance(v, str): for name, parser in parsers.items(): try: parser(v) @@ -365,7 +364,7 @@ def stringpatterncounter(table, field): """ - trans = maketrans( + trans = str.maketrans( 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789', 'AAAAAAAAAAAAAAAAAAAAAAAAAAaaaaaaaaaaaaaaaaaaaaaaaaaa9999999999' ) diff --git a/petl/util/lookups.py b/petl/util/lookups.py index 94b45914..d8340c32 100644 --- a/petl/util/lookups.py +++ b/petl/util/lookups.py @@ -1,5 +1,4 @@ import operator -from petl.compat import text_type from petl.errors import DuplicateKeyError @@ -223,7 +222,7 @@ def dictlookup(table, key, dictionary=None): it = iter(table) hdr = next(it) - flds = list(map(text_type, hdr)) + flds = list(map(str, hdr)) keyindices = asindices(hdr, key) assert len(keyindices) > 0, 'no key selected' getkey = operator.itemgetter(*keyindices) @@ -301,7 +300,7 @@ def dictlookupone(table, key, dictionary=None, strict=False): it = iter(table) hdr = next(it) - flds = list(map(text_type, hdr)) + flds = list(map(str, hdr)) keyindices = asindices(hdr, key) assert len(keyindices) > 0, 'no key selected' getkey = operator.itemgetter(*keyindices) @@ -329,7 +328,7 @@ def recordlookup(table, key, dictionary=None): it = iter(table) hdr = next(it) - flds = list(map(text_type, hdr)) + flds = list(map(str, hdr)) keyindices = asindices(hdr, key) assert len(keyindices) > 0, 'no key selected' getkey = operator.itemgetter(*keyindices) @@ -361,7 +360,7 @@ def recordlookupone(table, key, dictionary=None, strict=False): it = iter(table) hdr = next(it) - flds = list(map(text_type, hdr)) + flds = list(map(str, hdr)) keyindices = asindices(hdr, key) assert len(keyindices) > 0, 'no key selected' getkey = operator.itemgetter(*keyindices) diff --git a/petl/util/materialise.py b/petl/util/materialise.py index 6cfe3397..0d3a738b 100644 --- a/petl/util/materialise.py +++ b/petl/util/materialise.py @@ -1,7 +1,6 @@ import operator from collections import OrderedDict -from itertools import islice -from petl.compat import izip_longest, text_type, next +from itertools import islice, zip_longest from petl.util.base import asindices, Table @@ -58,11 +57,11 @@ def columns(table, missing=None): cols = OrderedDict() it = iter(table) hdr = next(it) - flds = list(map(text_type, hdr)) + flds = list(map(str, hdr)) for f in flds: cols[f] = list() for row in it: - for f, v in izip_longest(flds, row, fillvalue=missing): + for f, v in zip_longest(flds, row, fillvalue=missing): if f in cols: cols[f].append(v) return cols @@ -92,7 +91,7 @@ def facetcolumns(table, key, missing=None): fct = dict() it = iter(table) hdr = next(it) - flds = list(map(text_type, hdr)) + flds = list(map(str, hdr)) indices = asindices(hdr, key) assert len(indices) > 0, 'no key field selected' getkey = operator.itemgetter(*indices) @@ -106,7 +105,7 @@ def facetcolumns(table, key, missing=None): fct[kv] = cols else: cols = fct[kv] - for f, v in izip_longest(flds, row, fillvalue=missing): + for f, v in zip_longest(flds, row, fillvalue=missing): if f in cols: cols[f].append(v) diff --git a/petl/util/parsers.py b/petl/util/parsers.py index 16b21b1e..4ed1c743 100644 --- a/petl/util/parsers.py +++ b/petl/util/parsers.py @@ -1,5 +1,4 @@ import datetime -from petl.compat import long def datetimeparser(fmt, strict=True): @@ -161,7 +160,7 @@ def parser(value): def numparser(strict=False): """Return a function that will attempt to parse the value as a number, - trying :func:`int`, :func:`long`, :func:`float` and :func:`complex` in + trying :func:`int`, :func:`float` and :func:`complex` in that order. If all fail, return the value as-is, unless ``strict=True``, in which case raise the underlying exception. @@ -172,10 +171,6 @@ def f(v): return int(v) except (ValueError, TypeError): pass - try: - return long(v) - except (ValueError, TypeError): - pass try: return float(v) except (ValueError, TypeError): diff --git a/petl/util/random.py b/petl/util/random.py index b88cfae6..de542975 100644 --- a/petl/util/random.py +++ b/petl/util/random.py @@ -3,7 +3,6 @@ import time from collections import OrderedDict from functools import partial -from petl.compat import xrange, text_type from petl.util.base import Table @@ -67,7 +66,7 @@ def __iter__(self): yield tuple(flds) # construct data rows - for _ in xrange(nr): + for _ in range(nr): # artificial delay if self.wait: time.sleep(self.wait) @@ -156,7 +155,7 @@ def __init__(self, numrows=100, fields=None, wait=0, seed=None): self.seed = seed def __setitem__(self, item, value): - self.fields[text_type(item)] = value + self.fields[str(item)] = value def __iter__(self): nr = self.numrows @@ -167,11 +166,11 @@ def __iter__(self): random.seed(seed) # construct header row - hdr = tuple(text_type(f) for f in fields.keys()) + hdr = tuple(str(f) for f in fields.keys()) yield hdr # construct data rows - for _ in xrange(nr): + for _ in range(nr): # artificial delay if self.wait: time.sleep(self.wait) diff --git a/petl/util/timing.py b/petl/util/timing.py index fae8832e..f444f8d1 100644 --- a/petl/util/timing.py +++ b/petl/util/timing.py @@ -4,7 +4,6 @@ import time -from petl.compat import PY3 from petl.util.base import Table from petl.util.statistics import onlinestats @@ -244,11 +243,11 @@ def __iter__(self): self.time = 0 it = iter(self.wrapped) while True: - before = time.perf_counter() if PY3 else time.clock() + before = time.perf_counter() try: row = next(it) except StopIteration: return - after = time.perf_counter() if PY3 else time.clock() + after = time.perf_counter() self.time += (after - before) yield row diff --git a/petl/util/vis.py b/petl/util/vis.py index 0a97ce84..b3fd6511 100644 --- a/petl/util/vis.py +++ b/petl/util/vis.py @@ -1,7 +1,9 @@ +from __future__ import absolute_import, print_function, division + + import locale from itertools import islice from collections import defaultdict -from petl.compat import numeric_types, text_type from petl import config @@ -83,7 +85,7 @@ def look(table, limit=0, vrepr=None, index_header=None, style=None, Table.look = look -class Look: +class Look(object): def __init__(self, table, limit, vrepr, index_header, style, truncate, width): @@ -192,9 +194,9 @@ def _look_grid(table, vrepr, index_header, truncate, width): # fields representation hdr = next(it) - flds = list(map(text_type, hdr)) + flds = list(map(str, hdr)) if index_header: - fldsrepr = [f'{i}|{r}' for (i, r) in enumerate(flds)] + fldsrepr = ['%s|%s' % (i, r) for (i, r) in enumerate(flds)] else: fldsrepr = flds @@ -264,7 +266,7 @@ def _look_grid(table, vrepr, index_header, truncate, width): rowline = '|' for i, w in enumerate(colwidths): vr = valsrepr[i] - if i < len(vals) and isinstance(vals[i], numeric_types) \ + if i < len(vals) and isinstance(vals[i], (int, float)) \ and not isinstance(vals[i], bool): # left pad numbers rowline += ' ' * (w + 1 - len(vr)) # padding @@ -292,9 +294,9 @@ def _look_simple(table, vrepr, index_header, truncate, width): # fields representation hdr = next(it) - flds = list(map(text_type, hdr)) + flds = list(map(str, hdr)) if index_header: - fldsrepr = [f'{i}|{r}' for (i, r) in enumerate(flds)] + fldsrepr = ['%s|%s' % (i, r) for (i, r) in enumerate(flds)] else: fldsrepr = flds @@ -347,7 +349,7 @@ def _look_simple(table, vrepr, index_header, truncate, width): rowline = '' for i, w in enumerate(colwidths): vr = valsrepr[i] - if i < len(vals) and isinstance(vals[i], numeric_types) \ + if i < len(vals) and isinstance(vals[i], (int, float)) \ and not isinstance(vals[i], bool): # left pad numbers rowline += vr.rjust(w) @@ -375,9 +377,9 @@ def _look_minimal(table, vrepr, index_header, truncate, width): # fields representation hdr = next(it) - flds = list(map(text_type, hdr)) + flds = list(map(str, hdr)) if index_header: - fldsrepr = [f'{i}|{r}' for (i, r) in enumerate(flds)] + fldsrepr = ['%s|%s' % (i, r) for (i, r) in enumerate(flds)] else: fldsrepr = flds @@ -424,7 +426,7 @@ def _look_minimal(table, vrepr, index_header, truncate, width): rowline = '' for i, w in enumerate(colwidths): vr = valsrepr[i] - if i < len(vals) and isinstance(vals[i], numeric_types) \ + if i < len(vals) and isinstance(vals[i], (int, float)) \ and not isinstance(vals[i], bool): # left pad numbers rowline += vr.rjust(w) @@ -473,7 +475,7 @@ def see(table, limit=0, vrepr=None, index_header=None): return See(table, limit=limit, vrepr=vrepr, index_header=index_header) -class See: +class See(object): def __init__(self, table, limit, vrepr, index_header): self.table = table @@ -502,8 +504,8 @@ def __repr__(self): cols[str(f)].append('') for i, f in enumerate(flds): if index_header: - f = f'{i}|{f}' - output += '{}: {}'.format(f, ', '.join(cols[str(i)])) + f = '%s|%s' % (i, f) + output += '%s: %s' % (f, ', '.join(cols[str(i)])) if overflow: output += '...\n' else: @@ -548,7 +550,7 @@ def _display_html(table, limit=0, vrepr=None, index_header=None, caption=None, tohtml(table, buf, encoding=encoding, index_header=index_header, vrepr=vrepr, caption=caption, tr_style=tr_style, td_styles=td_styles, truncate=truncate) - output = text_type(buf.getvalue(), encoding) + output = str(buf.getvalue(), encoding) if epilogue: output += '

%s

' % epilogue From 92f6bd8483ebcb73ada9ea02b24674386d271cb5 Mon Sep 17 00:00:00 2001 From: Hugo van Kemenade Date: Sat, 28 Aug 2021 17:19:59 +0300 Subject: [PATCH 4/7] Drop support for EOL Python 2.6 --- petl/io/xml.py | 3 --- 1 file changed, 3 deletions(-) diff --git a/petl/io/xml.py b/petl/io/xml.py index a7633ef6..abee5187 100644 --- a/petl/io/xml.py +++ b/petl/io/xml.py @@ -173,9 +173,6 @@ def __iter__(self): with self.source.open('rb') as xmlf: parser2 = _create_xml_parser(self.user_parser) tree = etree.parse(xmlf, parser=parser2) - if not hasattr(tree, 'iterfind'): - # Python 2.6 compatibility - tree.iterfind = tree.findall if vmatch is not None: # simple case, all value paths are the same From 2f9c11170adba4be2fddff10cd0be768084fef02 Mon Sep 17 00:00:00 2001 From: Hugo van Kemenade Date: Sat, 28 Aug 2021 17:23:01 +0300 Subject: [PATCH 5/7] Upgrade Python syntax with pyupgrade --py36-plus --- petl/test/io/test_csv_unicode.py | 8 ++++---- petl/test/io/test_html.py | 4 ++-- petl/test/io/test_html_unicode.py | 2 +- petl/test/io/test_sources.py | 6 +++--- petl/test/io/test_text.py | 2 +- petl/test/io/test_text_unicode.py | 2 +- petl/util/vis.py | 17 +++++++---------- 7 files changed, 19 insertions(+), 22 deletions(-) diff --git a/petl/test/io/test_csv_unicode.py b/petl/test/io/test_csv_unicode.py index c89838cf..ad8bac33 100644 --- a/petl/test/io/test_csv_unicode.py +++ b/petl/test/io/test_csv_unicode.py @@ -68,7 +68,7 @@ def test_tocsv(): "Вагиф Сәмәдоғлу,3\n" "章子怡,4\n" ) - uf = open(fn, encoding='utf-8', mode='rt', newline='') + uf = open(fn, encoding='utf-8', newline='') actual = uf.read() eq_(expect, actual) @@ -86,7 +86,7 @@ def test_tocsv(): "Вагиф Сәмәдоғлу,3\n" "章子怡,4\n" ) - uf = open(fn, encoding='utf-8', mode='rt', newline='') + uf = open(fn, encoding='utf-8', newline='') actual = uf.read() eq_(expect, actual) @@ -119,7 +119,7 @@ def test_appendcsv(): "ኃይሌ ገብረሥላሴ,5\n" "ედუარდ შევარდნაძე,6\n" ) - uf = open(fn, encoding='utf-8', mode='rt') + uf = open(fn, encoding='utf-8') actual = uf.read() eq_(expect, actual) @@ -142,6 +142,6 @@ def test_tocsv_none(): 'd,4\n' ) - uf = open(fn, encoding='utf-8', mode='rt', newline='') + uf = open(fn, encoding='utf-8', newline='') actual = uf.read() eq_(expect, actual) diff --git a/petl/test/io/test_html.py b/petl/test/io/test_html.py index ec7a6075..8b8f52a8 100644 --- a/petl/test/io/test_html.py +++ b/petl/test/io/test_html.py @@ -18,7 +18,7 @@ def test_tohtml(): tohtml(table, f.name, encoding='ascii', lineterminator='\n') # check what it did - with open(f.name, mode='rt', encoding='ascii', newline='') as o: + with open(f.name, encoding='ascii', newline='') as o: actual = o.read() expect = ( "\n" @@ -58,7 +58,7 @@ def test_tohtml_caption(): lineterminator='\n') # check what it did - with open(f.name, mode='rt', encoding='ascii', newline='') as o: + with open(f.name, encoding='ascii', newline='') as o: actual = o.read() expect = ( "
\n" diff --git a/petl/test/io/test_html_unicode.py b/petl/test/io/test_html_unicode.py index 6c9c523e..5ea75254 100644 --- a/petl/test/io/test_html_unicode.py +++ b/petl/test/io/test_html_unicode.py @@ -18,7 +18,7 @@ def test_tohtml(): tohtml(tbl, fn, encoding='utf-8', lineterminator='\n') # check what it did - f = open(fn, mode='rt', encoding='utf-8', newline='') + f = open(fn, encoding='utf-8', newline='') actual = f.read() expect = ( "
\n" diff --git a/petl/test/io/test_sources.py b/petl/test/io/test_sources.py index db10c80a..d055a128 100644 --- a/petl/test/io/test_sources.py +++ b/petl/test/io/test_sources.py @@ -19,7 +19,7 @@ def test_memorysource(): # test writing to a string buffer ss = MemorySource() etl.tocsv(tbl1, ss) - expect = "foo,bar\r\na,1\r\nb,2\r\nc,2\r\n".encode('ascii') + expect = b"foo,bar\r\na,1\r\nb,2\r\nc,2\r\n" actual = ss.getvalue() eq_(expect, actual) @@ -31,13 +31,13 @@ def test_memorysource(): # test appending etl.appendcsv(tbl1, ss) actual = ss.getvalue() - expect = "foo,bar\r\na,1\r\nb,2\r\nc,2\r\na,1\r\nb,2\r\nc,2\r\n".encode('ascii') + expect = b"foo,bar\r\na,1\r\nb,2\r\nc,2\r\na,1\r\nb,2\r\nc,2\r\n" eq_(expect, actual) def test_memorysource_2(): - data = 'foo,bar\r\na,1\r\nb,2\r\nc,2\r\n'.encode('ascii') + data = b'foo,bar\r\na,1\r\nb,2\r\nc,2\r\n' actual = etl.fromcsv(MemorySource(data)) expect = (('foo', 'bar'), ('a', '1'), diff --git a/petl/test/io/test_text.py b/petl/test/io/test_text.py index a0f93bb5..be42c05c 100644 --- a/petl/test/io/test_text.py +++ b/petl/test/io/test_text.py @@ -74,7 +74,7 @@ def test_totext(): prologue=prologue, epilogue=epilogue) # check what it did - with open(f.name, mode='rt', encoding='ascii', newline='') as o: + with open(f.name, encoding='ascii', newline='') as o: actual = o.read() expect = ( "{| class='wikitable'\n" diff --git a/petl/test/io/test_text_unicode.py b/petl/test/io/test_text_unicode.py index be65ef86..d3d3f5bb 100644 --- a/petl/test/io/test_text_unicode.py +++ b/petl/test/io/test_text_unicode.py @@ -57,7 +57,7 @@ def test_totext(): epilogue=epilogue, encoding='utf-8') # check what it did - f = open(fn, encoding='utf-8', mode='rt') + f = open(fn, encoding='utf-8') actual = f.read() expect = ( "{| class='wikitable'\n" diff --git a/petl/util/vis.py b/petl/util/vis.py index b3fd6511..43bbf3a6 100644 --- a/petl/util/vis.py +++ b/petl/util/vis.py @@ -1,6 +1,3 @@ -from __future__ import absolute_import, print_function, division - - import locale from itertools import islice from collections import defaultdict @@ -85,7 +82,7 @@ def look(table, limit=0, vrepr=None, index_header=None, style=None, Table.look = look -class Look(object): +class Look: def __init__(self, table, limit, vrepr, index_header, style, truncate, width): @@ -196,7 +193,7 @@ def _look_grid(table, vrepr, index_header, truncate, width): hdr = next(it) flds = list(map(str, hdr)) if index_header: - fldsrepr = ['%s|%s' % (i, r) for (i, r) in enumerate(flds)] + fldsrepr = [f'{i}|{r}' for (i, r) in enumerate(flds)] else: fldsrepr = flds @@ -296,7 +293,7 @@ def _look_simple(table, vrepr, index_header, truncate, width): hdr = next(it) flds = list(map(str, hdr)) if index_header: - fldsrepr = ['%s|%s' % (i, r) for (i, r) in enumerate(flds)] + fldsrepr = [f'{i}|{r}' for (i, r) in enumerate(flds)] else: fldsrepr = flds @@ -379,7 +376,7 @@ def _look_minimal(table, vrepr, index_header, truncate, width): hdr = next(it) flds = list(map(str, hdr)) if index_header: - fldsrepr = ['%s|%s' % (i, r) for (i, r) in enumerate(flds)] + fldsrepr = [f'{i}|{r}' for (i, r) in enumerate(flds)] else: fldsrepr = flds @@ -475,7 +472,7 @@ def see(table, limit=0, vrepr=None, index_header=None): return See(table, limit=limit, vrepr=vrepr, index_header=index_header) -class See(object): +class See: def __init__(self, table, limit, vrepr, index_header): self.table = table @@ -504,8 +501,8 @@ def __repr__(self): cols[str(f)].append('') for i, f in enumerate(flds): if index_header: - f = '%s|%s' % (i, f) - output += '%s: %s' % (f, ', '.join(cols[str(i)])) + f = f'{i}|{f}' + output += '{}: {}'.format(f, ', '.join(cols[str(i)])) if overflow: output += '...\n' else: From 404909a5415065a8a116a782ac19928d80298af5 Mon Sep 17 00:00:00 2001 From: Hugo van Kemenade Date: Sat, 28 Aug 2021 17:37:10 +0300 Subject: [PATCH 6/7] Remove csv_py2.py --- petl/io/csv_py2.py | 183 --------------------------------------------- 1 file changed, 183 deletions(-) delete mode 100644 petl/io/csv_py2.py diff --git a/petl/io/csv_py2.py b/petl/io/csv_py2.py deleted file mode 100644 index 2bfa3016..00000000 --- a/petl/io/csv_py2.py +++ /dev/null @@ -1,183 +0,0 @@ -# standard library dependencies -import csv -import cStringIO - - -# internal dependencies -from petl.util.base import Table, data -from petl.io.base import getcodec - - -def fromcsv_impl(source, **kwargs): - return CSVView(source, **kwargs) - - -class CSVView(Table): - - def __init__(self, source=None, encoding=None, errors='strict', header=None, **csvargs): - self.source = source - self.encoding = encoding - self.errors = errors - self.csvargs = csvargs - self.header = header - - def __iter__(self): - if self.header is not None: - yield tuple(self.header) - - # determine encoding - codec = getcodec(self.encoding) - - # ascii - if codec.name == 'ascii': - # bypass encoding - with self.source.open('rU') as csvfile: - reader = csv.reader(csvfile, **self.csvargs) - for row in reader: - yield tuple(row) - - # non-ascii - else: - with self.source.open('rb') as buf: - reader = UnicodeReader(buf, encoding=self.encoding, - errors=self.errors, **self.csvargs) - for row in reader: - yield tuple(row) - - -def tocsv_impl(table, source, **kwargs): - _writecsv(table, source=source, mode='wb', **kwargs) - - -def appendcsv_impl(table, source, **kwargs): - _writecsv(table, source=source, mode='ab', **kwargs) - - -def _writecsv(table, source, mode, write_header, encoding, errors, **csvargs): - rows = table if write_header else data(table) - with source.open(mode) as buf: - - # determine encoding - codec = getcodec(encoding) - - # ascii - if codec.name == 'ascii': - # bypass encoding - writer = csv.writer(buf, **csvargs) - - # non-ascii - else: - writer = UnicodeWriter(buf, encoding=encoding, errors=errors, - **csvargs) - - for row in rows: - writer.writerow(row) - - -def teecsv_impl(table, source, **kwargs): - return TeeCSVView(table, source=source, **kwargs) - - -class TeeCSVView(Table): - def __init__(self, table, source=None, encoding=None, - errors='strict', write_header=True, **csvargs): - self.table = table - self.source = source - self.encoding = encoding - self.errors = errors - self.write_header = write_header - self.csvargs = csvargs - - def __iter__(self): - with self.source.open('wb') as buf: - - # determine encoding - codec = getcodec(self.encoding) - - # ascii - if codec.name == 'ascii': - # bypass encoding - writer = csv.writer(buf, **self.csvargs) - - # non-ascii - else: - writer = UnicodeWriter(buf, encoding=self.encoding, - errors=self.errors, - **self.csvargs) - - it = iter(self.table) - - # deal with header row - hdr = next(it) - if self.write_header: - writer.writerow(hdr) - # N.B., always yield header, even if we don't write it - yield tuple(hdr) - - # data rows - for row in it: - writer.writerow(row) - yield tuple(row) - - -# Additional classes for Unicode CSV support in PY2 -# taken from the original csv module docs -# http://docs.python.org/2/library/csv.html#examples - - -class UTF8Recoder: - - def __init__(self, buf, encoding, errors): - codec = getcodec(encoding) - self.reader = codec.streamreader(buf, errors=errors) - - def __iter__(self): - return self - - def next(self): - return self.reader.next().encode('utf-8') - - -class UnicodeReader: - - def __init__(self, f, encoding=None, errors='strict', **csvargs): - f = UTF8Recoder(f, encoding=encoding, errors=errors) - self.reader = csv.reader(f, **csvargs) - - def next(self): - row = self.reader.next() - return [unicode(s, 'utf-8') if isinstance(s, basestring) else s - for s in row] - - def __iter__(self): - return self - - -class UnicodeWriter: - - def __init__(self, buf, encoding=None, errors='strict', **csvargs): - # Redirect output to a queue - self.queue = cStringIO.StringIO() - self.writer = csv.writer(self.queue, **csvargs) - self.stream = buf - codec = getcodec(encoding) - self.encoder = codec.incrementalencoder(errors) - - def writerow(self, row): - self.writer.writerow( - [unicode(s).encode('utf-8') if s is not None else None - for s in row] - ) - # Fetch UTF-8 output from the queue ... - data = self.queue.getvalue() - data = data.decode('utf-8') - # ... and reencode it into the target encoding - data = self.encoder.encode(data) - # write to the target stream - self.stream.write(data) - # empty queue - self.queue.truncate(0) - - def writerows(self, rows): - for row in rows: - self.writerow(row) From 735e8099a7211ff463e3069233cff7a992c2f097 Mon Sep 17 00:00:00 2001 From: Hugo van Kemenade Date: Sat, 28 Aug 2021 17:43:34 +0300 Subject: [PATCH 7/7] Update docs and classifiers --- docs/changes.rst | 6 ++++++ setup.py | 2 ++ 2 files changed, 8 insertions(+) diff --git a/docs/changes.rst b/docs/changes.rst index 12052dc5..27d91a09 100644 --- a/docs/changes.rst +++ b/docs/changes.rst @@ -1,6 +1,12 @@ Changes ======= +Unreleaesed +------------- + +* Drop support for EOL Python 2.7. + By :user:`hugovk`, :issue:`478`. + Version 1.7.4 ------------- diff --git a/setup.py b/setup.py index 1822ee5e..28dcbb20 100644 --- a/setup.py +++ b/setup.py @@ -43,6 +43,8 @@ 'Programming Language :: Python :: 3.6', 'Programming Language :: Python :: 3.7', 'Programming Language :: Python :: 3.8', + 'Programming Language :: Python :: 3.9', + 'Programming Language :: Python :: 3 :: Only', 'Topic :: Software Development :: Libraries :: Python Modules' ] )