Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

lint & format io/iohandlers{p-s}.py #641

Merged
merged 2 commits into from
Nov 11, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
60 changes: 24 additions & 36 deletions libpysal/io/iohandlers/pyDbfIO.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
from .. import tables
from ...common import MISSINGVALUE
# ruff: noqa: N802, N806, N816, N999, SIM115

import datetime
import struct
import os
import struct
import time

from typing import Union
from ...common import MISSINGVALUE
from .. import tables

__author__ = "Charles R Schmidt <[email protected]>"
__all__ = ["DBF"]
Expand Down Expand Up @@ -71,7 +72,7 @@
self._col_index = {}
idx = 0

for fieldno in range(numfields):
for _ in range(numfields):
# again, check struct for fmt def.
name, typ, size, deci = struct.unpack("<11sc4xBB14x", f.read(32))
# forces to unicode in 2, to str in 3
Expand All @@ -98,7 +99,7 @@
self.header = [fInfo[0] for fInfo in self.field_info[1:]]
field_spec = []

for fname, ftype, flen, fpre in self.field_info[1:]:
for _, ftype, flen, fpre in self.field_info[1:]:
field_spec.append((ftype, flen, fpre))

self.field_spec = field_spec
Expand All @@ -124,7 +125,7 @@

if self.mode != "r":
msg = "Invalid operation, cannot read from a file opened in 'w' mode."
raise IOError(msg)
raise OSError(msg)

Check warning on line 128 in libpysal/io/iohandlers/pyDbfIO.py

View check run for this annotation

Codecov / codecov/patch

libpysal/io/iohandlers/pyDbfIO.py#L128

Added line #L128 was not covered by tests

return self.n_records

Expand Down Expand Up @@ -182,11 +183,8 @@
value = (value in "YyTt" and "T") or (value in "NnFf" and "F") or "?"
elif typ == "F":
value = value.replace("\0", "").lstrip()
if value == "":
value = MISSINGVALUE
else:
value = float(value)
if isinstance(value, str) or isinstance(value, str):
value = MISSINGVALUE if value == "" else float(value)

Check warning on line 186 in libpysal/io/iohandlers/pyDbfIO.py

View check run for this annotation

Codecov / codecov/patch

libpysal/io/iohandlers/pyDbfIO.py#L186

Added line #L186 was not covered by tests
if isinstance(value, str | str):
value = value.rstrip()
col[i] = value

Expand All @@ -204,7 +202,7 @@
return self.read_record(i + 1)
result = []

for (name, typ, size, deci), value in zip(self.field_info, rec):
for (name, typ, _, deci), value in zip(self.field_info, rec, strict=True):
if name == "DeletionFlag":
continue
if typ == "N":
Expand Down Expand Up @@ -232,17 +230,14 @@
value = (value in "YyTt" and "T") or (value in "NnFf" and "F") or "?"
elif typ == "F":
value = value.replace("\0", "").lstrip()
if value == "":
value = MISSINGVALUE
else:
value = float(value)
if isinstance(value, str) or isinstance(value, str):
value = MISSINGVALUE if value == "" else float(value)
if isinstance(value, str | str):
value = value.rstrip()
result.append(value)

return result

def _read(self) -> Union[list, None]:
def _read(self) -> list | None:
"""

Raises
Expand All @@ -254,7 +249,7 @@

if self.mode != "r":
msg = "Invalid operation, cannot read from a file opened in 'w' mode."
raise IOError(msg)
raise OSError(msg)

Check warning on line 252 in libpysal/io/iohandlers/pyDbfIO.py

View check run for this annotation

Codecov / codecov/patch

libpysal/io/iohandlers/pyDbfIO.py#L252

Added line #L252 was not covered by tests

if self.pos < len(self):
rec = self.read_record(self.pos)
Expand All @@ -279,7 +274,7 @@

if self.mode != "w":
msg = "Invalid operation, cannot read from a file opened in 'r' mode."
raise IOError(msg)
raise OSError(msg)

Check warning on line 277 in libpysal/io/iohandlers/pyDbfIO.py

View check run for this annotation

Codecov / codecov/patch

libpysal/io/iohandlers/pyDbfIO.py#L277

Added line #L277 was not covered by tests

if self.FIRST_WRITE:
self._firstWrite()
Expand All @@ -290,19 +285,12 @@
self.numrec += 1

# deletion flag
self.f.write(" ".encode())
self.f.write(b" ")

for (typ, size, deci), value in zip(self.field_spec, obj):
for (typ, size, deci), value in zip(self.field_spec, obj, strict=True):
if value is None:
if typ == "C":
value = " " * size
else:
value = "\0" * size
value = " " * size if typ == "C" else "\x00" * size
jGaboardi marked this conversation as resolved.
Show resolved Hide resolved
elif typ == "N" or typ == "F":
v = str(value).rjust(size, " ")
# if len(v) == size:
# value = v
# else:
jGaboardi marked this conversation as resolved.
Show resolved Hide resolved
value = (("%" + "%d.%d" % (size, deci) + "f") % (value))[:size]
elif typ == "D":
value = value.strftime("%Y%m%d")
Expand All @@ -327,7 +315,7 @@
if self.mode == "w":
self.flush()
# End of file
self.f.write("\x1A".encode())
self.f.write(b"\x1A")
self.f.close()

tables.DataTable.close(self)
Expand All @@ -345,9 +333,9 @@
"""

if not self.header:
raise IOError("No header, DBF files require a header.")
raise OSError("No header, DBF files require a header.")

Check warning on line 336 in libpysal/io/iohandlers/pyDbfIO.py

View check run for this annotation

Codecov / codecov/patch

libpysal/io/iohandlers/pyDbfIO.py#L336

Added line #L336 was not covered by tests
if not self.field_spec:
raise IOError("No field_spec, DBF files require a specification.")
raise OSError("No field_spec, DBF files require a specification.")

Check warning on line 338 in libpysal/io/iohandlers/pyDbfIO.py

View check run for this annotation

Codecov / codecov/patch

libpysal/io/iohandlers/pyDbfIO.py#L338

Added line #L338 was not covered by tests

self._writeHeader()

Expand Down Expand Up @@ -377,15 +365,15 @@
self.f.write(hdr)

# field specs
for name, (typ, size, deci) in zip(self.header, self.field_spec):
for name, (typ, size, deci) in zip(self.header, self.field_spec, strict=True):
typ = typ.encode()
name = name.ljust(11, "\x00")
name = name.encode()
fld = struct.pack("<11sc4xBB14x", name, typ, size, deci)
self.f.write(fld)

# terminator
term = "\r".encode()
term = b"\r"
self.f.write(term)
if self.f.tell() != POS and not self.FIRST_WRITE:
self.f.seek(POS)
Expand Down
49 changes: 30 additions & 19 deletions libpysal/io/iohandlers/pyShpIO.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,17 @@

"""

# ruff: noqa: N802, N806, N999

__author__ = "Charles R Schmidt <[email protected]>"
__credits__ = "Copyright (c) 2009 Charles R. Schmidt"
__all__ = ["PurePyShpWrapper"]

from warnings import warn

from ... import cg
from .. import fileio
from ..util import shp_file
from ... import cg
from warnings import warn

STRING_TO_TYPE = {
"POLYGON": cg.Polygon,
Expand All @@ -37,7 +40,7 @@

Notes
-----

This class wraps ``_pyShpIO``'s ``shp_file`` class with the PySAL `FileIO` API.
shp_file can be used without PySAL.

Expand All @@ -48,25 +51,25 @@
>>> f = tempfile.NamedTemporaryFile(suffix='.shp')
>>> fname = f.name
>>> f.close()

>>> import libpysal
>>> i = libpysal.io.open(libpysal.examples.get_path('10740.shp'),'r')
>>> o = libpysal.io.open(fname,'w')

>>> for shp in i:
... o.write(shp)
>>> o.close()

>>> one = libpysal.io.open(libpysal.examples.get_path('10740.shp'),'rb').read()
>>> two = libpysal.io.open(fname,'rb').read()
>>> one[0].centroid == two[0].centroid
True

>>> one = libpysal.io.open(libpysal.examples.get_path('10740.shx'),'rb').read()
>>> two = libpysal.io.open(fname[:-1]+'x','rb').read()
>>> one[0].centroid == two[0].centroid
True

>>> import os
>>> os.remove(fname); os.remove(fname.replace('.shp','.shx'))

Expand All @@ -84,7 +87,7 @@
self.__create()

def __len__(self) -> int:
if self.dataObj != None:
if self.dataObj is not None:
return len(self.dataObj)
else:
return 0
Expand All @@ -108,7 +111,7 @@
except KeyError:
msg = "%s does not support shapes of type: %s."
msg = msg % (self.__class__.__name__, self.dataObj.type())
raise TypeError(msg)
raise TypeError(msg) from None

Check warning on line 114 in libpysal/io/iohandlers/pyShpIO.py

View check run for this annotation

Codecov / codecov/patch

libpysal/io/iohandlers/pyShpIO.py#L114

Added line #L114 was not covered by tests

def __create(self):
self.write = self.__firstWrite
Expand Down Expand Up @@ -176,11 +179,11 @@
rec["NumParts"] = len(shape.parts)
all_parts = shape.parts
partsIndex = [0]
for l in [len(part) for part in all_parts][:-1]:
partsIndex.append(partsIndex[-1] + l)
for l_ in [len(part) for part in all_parts][:-1]:
partsIndex.append(partsIndex[-1] + l_)

Check warning on line 183 in libpysal/io/iohandlers/pyShpIO.py

View check run for this annotation

Codecov / codecov/patch

libpysal/io/iohandlers/pyShpIO.py#L183

Added line #L183 was not covered by tests
rec["Parts Index"] = partsIndex
verts = sum(all_parts, [])
verts = [(x, y) for x, y in verts]
verts = list(verts)
jGaboardi marked this conversation as resolved.
Show resolved Hide resolved
rec["NumPoints"] = len(verts)
rec["Vertices"] = verts
self.dataObj.add_shape(rec)
Expand Down Expand Up @@ -218,8 +221,12 @@
]
if self.dataObj.type() == "POLYGON":
is_cw = [cg.is_clockwise(part) for part in parts]
vertices = [part for part, cw in zip(parts, is_cw) if cw]
holes = [part for part, cw in zip(parts, is_cw) if not cw]
vertices = [
part for part, cw in zip(parts, is_cw, strict=True) if cw
]
holes = [
part for part, cw in zip(parts, is_cw, strict=True) if not cw
]
if not holes:
holes = None
shp = self.type(vertices, holes)
Expand All @@ -229,17 +236,21 @@
elif rec["NumParts"] == 1:
vertices = rec["Vertices"]
if self.dataObj.type() == "POLYGON" and not cg.is_clockwise(vertices):

### SHAPEFILE WARNING: Polygon %d topology has been fixed. (ccw -> cw)
# SHAPEFILE WARNING:
# Polygon %d topology has been fixed. (ccw -> cw)
msg = "SHAPEFILE WARNING: Polygon %d "
msg += "topology has been fixed. (ccw -> cw)."
msg = msg % self.pos
warn(msg, RuntimeWarning)
warn(msg, RuntimeWarning, stacklevel=2)

Check warning on line 244 in libpysal/io/iohandlers/pyShpIO.py

View check run for this annotation

Codecov / codecov/patch

libpysal/io/iohandlers/pyShpIO.py#L244

Added line #L244 was not covered by tests
print(msg)

shp = self.type(vertices)
else:
warn("Polygon %d has zero parts." % self.pos, RuntimeWarning)
warn(

Check warning on line 249 in libpysal/io/iohandlers/pyShpIO.py

View check run for this annotation

Codecov / codecov/patch

libpysal/io/iohandlers/pyShpIO.py#L249

Added line #L249 was not covered by tests
"Polygon %d has zero parts." % self.pos,
RuntimeWarning,
stacklevel=2,
)
shp = self.type([[]])
# raise ValueError, "Polygon %d has zero parts"%self.pos

Expand Down
Loading