Skip to content

Commit

Permalink
Merge pull request #60 from jaraco/debt/setup.py
Browse files Browse the repository at this point in the history
Update for Python 3.8 and later
  • Loading branch information
jaraco authored Feb 19, 2024
2 parents fd2000c + 220f7fd commit 42f3ee0
Show file tree
Hide file tree
Showing 19 changed files with 52 additions and 81 deletions.
1 change: 0 additions & 1 deletion examples/usage.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
# -*- coding: utf-8 -*-
# vim: sw=4:ts=4:expandtab

"""
Expand Down
1 change: 0 additions & 1 deletion manage.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
#!/usr/bin/env python
# -*- coding: utf-8 -*-
# vim: sw=4:ts=4:expandtab

""" A script to manage development tasks """
Expand Down
1 change: 0 additions & 1 deletion meza/__init__.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
#!/usr/bin/env python
# -*- coding: utf-8 -*-
# vim: sw=4:ts=4:expandtab

"""
Expand Down
1 change: 0 additions & 1 deletion meza/compat.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
#!/usr/bin/env python
# -*- coding: utf-8 -*-
# vim: sw=4:ts=4:expandtab

"""
Expand Down
17 changes: 8 additions & 9 deletions meza/convert.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
#!/usr/bin/env python
# -*- coding: utf-8 -*-
# vim: sw=4:ts=4:expandtab

"""
Expand Down Expand Up @@ -151,7 +150,7 @@ def to_bool(content, trues=None, falses=None, warn=False, **kwargs):
except (TypeError, AttributeError):
value = bool(content)
elif warn:
raise ValueError("Invalid bool value: `{}`.".format(content))
raise ValueError(f"Invalid bool value: `{content}`.")
else:
value = False

Expand Down Expand Up @@ -196,13 +195,13 @@ def to_int(content, thousand_sep=",", decimal_sep=".", warn=False, **kwargs):
int
"""
if warn and not ft.is_int(content):
raise ValueError("Invalid int value: `{}`.".format(content))
raise ValueError(f"Invalid int value: `{content}`.")

try:
value = int(float(ft.strip(content, thousand_sep, decimal_sep)))
except ValueError:
if warn:
raise ValueError("Invalid int value: `{}`.".format(content))
raise ValueError(f"Invalid int value: `{content}`.")
else:
value = 0

Expand Down Expand Up @@ -246,7 +245,7 @@ def to_float(content, thousand_sep=",", decimal_sep=".", warn=False, **kwargs):
if ft.is_numeric(content):
value = float(ft.strip(content, thousand_sep, decimal_sep))
elif warn:
raise ValueError("Invalid float value: `{}`.".format(content))
raise ValueError(f"Invalid float value: `{content}`.")
else:
value = 0.0

Expand Down Expand Up @@ -303,7 +302,7 @@ def to_decimal(content, thousand_sep=",", decimal_sep=".", **kwargs):
if ft.is_numeric(content):
decimalized = Decimal(ft.strip(content, thousand_sep, decimal_sep))
elif kwargs.get("warn"):
raise ValueError("Invalid numeric value: `{}`.".format(content))
raise ValueError(f"Invalid numeric value: `{content}`.")
else:
decimalized = Decimal(0)

Expand Down Expand Up @@ -430,7 +429,7 @@ def to_datetime(content, dt_format=None, warn=False, **kwargs):
value = NULL_DATETIME

if warn and value == NULL_DATETIME:
raise ValueError("Invalid datetime value: `{}`.".format(content))
raise ValueError(f"Invalid datetime value: `{content}`.")
else:
datetime = value.strftime(dt_format) if dt_format else value

Expand Down Expand Up @@ -552,7 +551,7 @@ def to_filepath(filepath, **kwargs):
filename = "{}.{}".format(resource_id, filename.split("=")[1])
elif isdir and "." not in filename:
ctype = headers.get("content-type")
filename = "{}.{}".format(filename, ctype2ext(ctype))
filename = f"{filename}.{ctype2ext(ctype)}"

return p.join(filepath, filename) if isdir else filepath

Expand Down Expand Up @@ -993,7 +992,7 @@ def gen_subresults(records, kw):
polygon = [[(r[kw.lon], r[kw.lat]) for r in g[1]] for g in groups]
yield (polygon, first_row)
else:
raise TypeError("Invalid type: {}".format(_type))
raise TypeError(f"Invalid type: {_type}")


def records2geojson(records, **kwargs):
Expand Down
5 changes: 2 additions & 3 deletions meza/dbf.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
#!/usr/bin/env python
# -*- coding: utf-8 -*-
# vim: sw=4:ts=4:expandtab

"""
Expand Down Expand Up @@ -57,7 +56,7 @@ def __init__(self, filepath, **kwargs):
"""
try:
kwargs["recfactory"] = dict
return super(DBF2, self).__init__(filepath, **kwargs)
return super().__init__(filepath, **kwargs)
except (AttributeError, TypeError):
filename = filepath.name

Expand All @@ -69,7 +68,7 @@ def __init__(self, filepath, **kwargs):
self.filename = ifind(filename) if self.ignorecase else filename

if not self.filename:
raise DBFNotFound("could not find file {!r}".format(filename))
raise DBFNotFound(f"could not find file {filename!r}")

self.fields = []
self.field_names = []
Expand Down
36 changes: 16 additions & 20 deletions meza/fntools.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
#!/usr/bin/env python
# -*- coding: utf-8 -*-
# vim: sw=4:ts=4:expandtab

"""
Expand Down Expand Up @@ -120,7 +119,7 @@
logger = gogo.Gogo(__name__, monolog=True).logger


class Objectify(object):
class Objectify:
"""Creates an object with dynamically set attributes. Useful
for accessing the kwargs of a function as attributes.
"""
Expand Down Expand Up @@ -177,7 +176,7 @@ def __setattr__(self, key, value):
if key not in {"data", "func", "keys", "values", "items", "get"}:
self.data.__setitem__(key, value)

return super(Objectify, self).__setattr__(key, value)
return super().__setattr__(key, value)

def __getattr__(self, name):
return self.__getitem__(name)
Expand All @@ -195,7 +194,7 @@ def iteritems(self):
return iter(self.items())


class Andand(object):
class Andand:
"""A Ruby inspired null soaking object
Examples:
Expand Down Expand Up @@ -235,14 +234,14 @@ def default(self, obj):
encoded = float(obj)
elif hasattr(obj, "to_dict"):
encoded = obj.to_dict()
elif set(["quantize", "year", "hour"]).intersection(dir(obj)):
elif {"quantize", "year", "hour"}.intersection(dir(obj)):
encoded = str(obj)
elif hasattr(obj, "union"):
encoded = tuple(obj)
elif set(["next", "append"]).intersection(dir(obj)):
elif {"next", "append"}.intersection(dir(obj)):
encoded = list(obj)
else:
encoded = super(CustomEncoder, self).default(obj)
encoded = super().default(obj)

return encoded

Expand All @@ -254,15 +253,15 @@ class SleepyDict(dict):

def __init__(self, *args, **kwargs):
self.delay = kwargs.pop("delay", 0)
super(SleepyDict, self).__init__(*args, **kwargs)
super().__init__(*args, **kwargs)

def __len__(self):
time.sleep(self.delay)
return super(SleepyDict, self).__len__()
return super().__len__()

def get(self, key, default=None):
time.sleep(self.delay)
return super(SleepyDict, self).get(key, default)
return super().get(key, default)


def underscorify(content):
Expand Down Expand Up @@ -716,12 +715,10 @@ def get_values(narray):
try:
yield narray.tounicode()
except ValueError:
for y in narray.tolist():
yield y
yield from narray.tolist()
except AttributeError:
for n in narray:
for x in get_values(n):
yield x
yield from get_values(n)


def xmlize(content):
Expand Down Expand Up @@ -783,7 +780,7 @@ def afterish(content, separator=","):
elif numeric:
after = -1
else:
raise ValueError("Not able to coerce {} to a number".format(content))
raise ValueError(f"Not able to coerce {content} to a number")

return after

Expand Down Expand Up @@ -834,7 +831,7 @@ def get_separators(content):
else:
logger.debug("after_comma: %s", after_comma)
logger.debug("after_decimal: %s", after_decimal)
raise ValueError("Invalid number format for `{}`.".format(content))
raise ValueError(f"Invalid number format for `{content}`.")

return {"thousand_sep": thousand_sep, "decimal_sep": decimal_sep}

Expand Down Expand Up @@ -867,7 +864,7 @@ def _fuzzy_match(needle, haystack, **kwargs):


def _exact_match(*args, **kwargs):
sets = (set(i.lower() for i in arg) for arg in args)
sets = ({i.lower() for i in arg} for arg in args)
return iter(reduce(lambda x, y: x.intersection(y), sets))


Expand Down Expand Up @@ -1101,10 +1098,9 @@ def flatten(record, prefix=None):
"""
try:
for key, value in record.items():
newkey = "{}_{}".format(prefix, key) if prefix else key
newkey = f"{prefix}_{key}" if prefix else key

for flattened in flatten(value, newkey):
yield flattened
yield from flatten(value, newkey)
except AttributeError:
yield (prefix, record)

Expand Down
16 changes: 7 additions & 9 deletions meza/io.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
#!/usr/bin/env python
# -*- coding: utf-8 -*-
# vim: sw=4:ts=4:expandtab

"""
Expand Down Expand Up @@ -54,7 +53,7 @@
)

from xlrd.xldate import xldate_as_datetime as xl2dt
from io import StringIO, TextIOBase, BytesIO, open
from io import StringIO, TextIOBase, BytesIO

from . import fntools as ft, process as pr, unicsv as csv, dbf, ENCODING, BOM, DATA_DIR

Expand Down Expand Up @@ -388,7 +387,7 @@ def get_file_encoding(f, encoding=None, bytes_error=False):

if not bytes_error:
# Set the encoding to None so that we can detect the correct one.
extra = (" ({})".format(encoding)) if encoding else ""
extra = (f" ({encoding})") if encoding else ""
logger.warning("%s was opened with the wrong encoding%s", f, extra)
encoding = None

Expand Down Expand Up @@ -651,7 +650,7 @@ def read_mdb(filepath, table=None, **kwargs):
yield
return
except CalledProcessError:
raise TypeError("{} is not readable by mdbtools".format(filepath))
raise TypeError(f"{filepath} is not readable by mdbtools")

sanitize = kwargs.pop("sanitize", None)
dedupe = kwargs.pop("dedupe", False)
Expand Down Expand Up @@ -757,7 +756,7 @@ def read_sqlite(filepath, table=None):
if not table or table not in set(cursor.fetchall()):
table = cursor.fetchone()[0]

cursor.execute("SELECT * FROM {}".format(table))
cursor.execute(f"SELECT * FROM {table}")
return map(dict, cursor)


Expand Down Expand Up @@ -1186,7 +1185,7 @@ def gen_records(_type, record, coords, properties, **kwargs):
record["pos"] = pos
yield pr.merge([record, properties])
else:
raise TypeError("Invalid geometry type {}.".format(_type))
raise TypeError(f"Invalid geometry type {_type}.")


def read_geojson(filepath, key="id", mode="r", **kwargs):
Expand Down Expand Up @@ -1247,8 +1246,7 @@ def reader(f, **kwargs):

args = (record, coords, properties)

for rec in gen_records(_type, *args, **kwargs):
yield rec
yield from gen_records(_type, *args, **kwargs)

return read_any(filepath, reader, mode, **kwargs)

Expand Down Expand Up @@ -1420,7 +1418,7 @@ def reader(f, **kwargs):
names = map(get_text, ths)
else:
col_nums = range(len(a_row))
names = ["column_{}".format(i) for i in col_nums]
names = [f"column_{i}" for i in col_nums]

uscored = ft.underscorify(names) if sanitize else names
header = list(ft.dedupe(uscored) if dedupe else uscored)
Expand Down
9 changes: 4 additions & 5 deletions meza/process.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
#!/usr/bin/env python
# -*- coding: utf-8 -*-
# vim: sw=4:ts=4:expandtab

"""
Expand Down Expand Up @@ -941,14 +940,14 @@ def get_suffix(cpos, pos, k=None, count=None, chunksize=None):

if subchunks and k is None:
args = (cpos + 1, pos + 1)
suffix = "{0:02d}_{1:03d}".format(*args)
suffix = "{:02d}_{:03d}".format(*args)
elif subchunks:
args = (k, cpos + 1, pos + 1)
suffix = "{0}_{1:02d}_{2:03d}".format(*args)
suffix = "{}_{:02d}_{:03d}".format(*args)
elif chunksize and k is None:
suffix = "{0:03d}".format(cpos + 1)
suffix = f"{cpos + 1:03d}"
elif chunksize:
suffix = "{0}_{1:03d}".format(k, cpos + 1)
suffix = f"{k}_{cpos + 1:03d}"
else:
suffix = "" if k is None else k

Expand Down
1 change: 0 additions & 1 deletion meza/stats.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
#!/usr/bin/env python
# -*- coding: utf-8 -*-
# vim: sw=4:ts=4:expandtab

"""
Expand Down
3 changes: 1 addition & 2 deletions meza/typetools.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
#!/usr/bin/env python
# -*- coding: utf-8 -*-
# vim: sw=4:ts=4:expandtab

"""
Expand Down Expand Up @@ -169,7 +168,7 @@ def guess_type_by_value(record, blanks_as_nulls=True, strip_zeros=False):
yield result
break
else:
raise TypeError("Couldn't guess type of '{}'".format(value))
raise TypeError(f"Couldn't guess type of '{value}'")


def is_date(content):
Expand Down
3 changes: 1 addition & 2 deletions meza/unicsv.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
#!/usr/bin/env python
# -*- coding: utf-8 -*-
# vim: sw=4:ts=4:expandtab

"""
Expand Down Expand Up @@ -45,7 +44,7 @@ def encode_all(f=None, **kwargs):
return res


class UnicodeWriter(object):
class UnicodeWriter:
"""
>>> from io import StringIO
>>>
Expand Down
3 changes: 3 additions & 0 deletions setup.cfg
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
[options]
python_requires = >=3.8

[wheel]
universal=1

Expand Down
Loading

0 comments on commit 42f3ee0

Please sign in to comment.