Skip to content

Commit

Permalink
lint & format io/iohandlers{p-s}.py (#641)
Browse files Browse the repository at this point in the history
* lint&format io/iohandlers{p-s}.py

* martin suggestion – keep commented
  • Loading branch information
jGaboardi authored Nov 11, 2023
1 parent f1d8227 commit dcc9403
Show file tree
Hide file tree
Showing 3 changed files with 87 additions and 82 deletions.
64 changes: 28 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 <schmidtc@gmail.com>"
__all__ = ["DBF"]
Expand Down Expand Up @@ -71,7 +72,7 @@ def __init__(self, *args, **kwargs):
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 @@ def __init__(self, *args, **kwargs):
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 @@ def __len__(self) -> int:

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

return self.n_records

Expand Down Expand Up @@ -182,11 +183,8 @@ def _get_col(self, key: str) -> list:
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()
col[i] = value

Expand All @@ -204,7 +202,7 @@ def read_record(self, i: int) -> list:
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 @@ def read_record(self, i: int) -> list:
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 @@ def _read(self) -> Union[list, None]:

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

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

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

if self.FIRST_WRITE:
self._firstWrite()
Expand All @@ -290,19 +285,16 @@ def write(self, obj: list):
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
elif typ == "N" or typ == "F":
v = str(value).rjust(size, " ")
# if len(v) == size:
# value = v
# else:
# v = str(value).rjust(size, " ")
# # if len(v) == size:
# # value = v
# # else:
value = (("%" + "%d.%d" % (size, deci) + "f") % (value))[:size]
elif typ == "D":
value = value.strftime("%Y%m%d")
Expand All @@ -327,7 +319,7 @@ def close(self):
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 +337,9 @@ def _firstWrite(self):
"""

if not self.header:
raise IOError("No header, DBF files require a header.")
raise OSError("No header, DBF files require a header.")
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.")

self._writeHeader()

Expand Down Expand Up @@ -377,15 +369,15 @@ def _writeHeader(self):
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 <schmidtc@gmail.com>"
__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 @@ class PurePyShpWrapper(fileio.FileIO):
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 @@ class PurePyShpWrapper(fileio.FileIO):
>>> 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 @@ def __init__(self, *args, **kwargs):
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 @@ def __open(self):
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

def __create(self):
self.write = self.__firstWrite
Expand Down Expand Up @@ -176,11 +179,11 @@ def __writer(self, shape):
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_)
rec["Parts Index"] = partsIndex
verts = sum(all_parts, [])
verts = [(x, y) for x, y in verts]
verts = list(verts)
rec["NumPoints"] = len(verts)
rec["Vertices"] = verts
self.dataObj.add_shape(rec)
Expand Down Expand Up @@ -218,8 +221,12 @@ def _read(self):
]
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 @@ def _read(self):
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)
print(msg)

shp = self.type(vertices)
else:
warn("Polygon %d has zero parts." % self.pos, RuntimeWarning)
warn(
"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

0 comments on commit dcc9403

Please sign in to comment.