Skip to content

Commit

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

* martin suggestion – keep commented

* lint&format io/iohandlers{t-w}.py

* martin suggest – comment out
  • Loading branch information
jGaboardi authored Nov 11, 2023
1 parent dcc9403 commit 776e001
Show file tree
Hide file tree
Showing 3 changed files with 73 additions and 75 deletions.
102 changes: 48 additions & 54 deletions libpysal/io/iohandlers/template.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
"""

# ruff: noqa: N812, SIM115

from .. import fileio as FileIO

__author__ = "Charles R Schmidt <schmidtc@gmail.com>"
Expand All @@ -12,7 +14,6 @@

# Always subclass FileIO
class TemplateWriter(FileIO.FileIO):

# REQUIRED, List the formats this class supports.
FORMATS = ["foo"]

Expand All @@ -35,18 +36,18 @@ def __init__(self, *args, **kwargs):

# Writers must subclass ``.write()``
def write(self, obj):
""" ``.write`` method of the 'foobar' template
"""``.write`` method of the 'foobar' template
Parameters
----------
obj : str
Some string.
Raises
------
TypeError
Raised when a ``str`` is expected, but got another type.
"""

# GOOD TO HAVE, this will prevent invalid operations on closed files.
Expand All @@ -57,13 +58,9 @@ def write(self, obj):
# we will support writing string objects in this example,
# all string are derived from basestring...
if issubclass(type(obj), str):

# Non-essential...
def foobar(c):
if c in "foobar":
return True
else:
return False
return c in "foobar"

# e.g. 'foobara' == filter(foobar,'my little foobar example')
result = list(filter(foobar, obj))
Expand Down Expand Up @@ -101,27 +98,24 @@ def __init__(self, *args, **kwargs):

def _filter(self, st):
def foobar(c):
if c in "foobar":
return True
else:
return False
return c in "foobar"

# e.g. 'foobara' == filter(foobar,'my little foobar example')
return list(filter(foobar, st))

def _read(self):
"""The ``_read`` method should return only ONE object.
Returns
-------
obj_plus_break : str
only ONE object.
Raises
------
StopIteration
Raised at the EOF.
"""

line = self.fileObj.readline()
Expand All @@ -138,17 +132,17 @@ def _read(self):

def write(self, obj):
"""The ``.write`` method of the 'foobar' template, receives an ``obj``.
Paramters
---------
obj : str
Some string.
Raises
------
TypeError
Raised when a ``str`` is expected, but got another type.
"""
self._complain_ifclosed(self.closed)
if issubclass(type(obj), str):
Expand All @@ -167,37 +161,37 @@ def close(self):
FileIO.close(self)


if __name__ == "__main__":
"NOTE, by running OR importing this module "
"it's automatically added to the pysal fileIO registry."

pysal.open.check()

lines = [
"This is an example of template FileIO classes",
"Each call to write expects a string object",
"that string is filtered and only letters 'f','o','b','a','r' are kept",
"these kept letters are written to the file",
"and a new line char is appends to each line",
"likewise the reader filters each line from a file.",
]

f = pysal.open("test.foo", "w")
for line in lines:
f.write(line)
f.close()

f = pysal.open("test.bar", "w")
for line in lines:
f.write(line)
f.close()

f = pysal.open("test.bar", "r")
s = "".join(f.read())
f.close()
print(s)

f = open("test.foo", "r")
s2 = f.read()
f.close()
print(s == s2)
# if __name__ == "__main__":
# "NOTE, by running OR importing this module"
# "it's automatically added to the pysal fileIO registry."
#
# pysal.open.check() # noqa
#
# lines = [
# "This is an example of template FileIO classes",
# "Each call to write expects a string object",
# "that string is filtered and only letters 'f','o','b','a','r' are kept",
# "these kept letters are written to the file",
# "and a new line char is appends to each line",
# "likewise the reader filters each line from a file.",
# ]
#
# f = pysal.open("test.foo", "w") # noqa
# for line in lines:
# f.write(line)
# f.close()
#
# f = pysal.open("test.bar", "w") # noqa
# for line in lines:
# f.write(line)
# f.close()
#
# f = pysal.open("test.bar", "r") # noqa
# s = "".join(f.read())
# f.close()
# print(s)
#
# f = open("test.foo")
# s2 = f.read()
# f.close()
# print(s == s2)
40 changes: 21 additions & 19 deletions libpysal/io/iohandlers/wk1.py
Original file line number Diff line number Diff line change
@@ -1,14 +1,17 @@
# ruff: noqa: ARG002, N802, SIM115

import struct
from .. import fileio

from ...weights import W
from .. import fileio

__author__ = "Myunghwa Hwang <mhwang4@gmail.com>"
__all__ = ["Wk1IO"]


class Wk1IO(fileio.FileIO):
"""MATLAB ``wk1read.m`` and ``wk1write.m`` that were written by
Brian M. Bourgault in 10/22/93. Opens, reads, and writes weights
Brian M. Bourgault in 10/22/93. Opens, reads, and writes weights
ile objects in Lotus Wk1 format. Lotus Wk1 files are used in Dr.
LeSage's MATLAB Econometrics library.
Expand All @@ -20,12 +23,12 @@ class Wk1IO(fileio.FileIO):
is a blank or has a number.
The internal structure of a `Wk1` file written by PySAL is as follows:
```
[BOF][DIM][CPI][CAL][CMODE][CORD][SPLIT][SYNC][CURS][WIN]
[HCOL][MRG][LBL][CELL_1]...[CELL_m][EOF]
```
where ``[CELL_k]`` equals to ``[DTYPE][DLEN][DFORMAT][CINDEX][CVALUE]``.
The parts between ``[BOF]`` and ``[CELL_1]`` are variable according to
the software program used to write a ``.wk1`` file. While reading a
Expand Down Expand Up @@ -132,7 +135,7 @@ class Wk1IO(fileio.FileIO):
| [EOF] |End of file |unsigned short |4 |1,0,0,0 |
+-------------+---------------------+-------------------------+-------+-----------------------------+
"""
""" # noqa E501

FORMATS = ["wk1"]
MODES = ["r", "w"]
Expand All @@ -149,21 +152,21 @@ def _set_varName(self, val):
def _get_varName(self) -> str:
return self._varName

varName = property(fget=_get_varName, fset=_set_varName)
varName = property(fget=_get_varName, fset=_set_varName) # noqa N815

def read(self, n=-1):
"""
Parameters
----------
n : int
Read at most ``n`` objects. Default is ``-1``.
Returns
-------
w : libpysal.weights.W
A PySAL `W` object.
"""

self._complain_ifclosed(self.closed)
Expand All @@ -181,14 +184,14 @@ def _read(self):
-------
w : libpysal.weights.W
A PySAL `W` object.
Raises
------
StopIteration
Raised at the EOF.
ValueError
Raised when the header of the file is invalid.
Examples
--------
Expand Down Expand Up @@ -224,15 +227,14 @@ def _read(self):
dtype, dlen = struct.unpack("<2H", self.file.read(4))

while dtype != 1:

if dtype in [13, 14, 16]:
self.file.read(1)
row, column = struct.unpack("<2H", self.file.read(4))
format, length = "<d", 8
format_, length = "<d", 8

if dtype == 13:
format, length = "<h", 2
value = float(struct.unpack(format, self.file.read(length))[0])
format_, length = "<h", 2
value = float(struct.unpack(format_, self.file.read(length))[0])

if value > 0:
ngh = neighbors.setdefault(row, [])
Expand Down Expand Up @@ -261,14 +263,14 @@ def write(self, obj):
----------
obj : libpysal.weights.W
A PySAL `W` object.
Raises
------
ValueError
Raised when the `WK1` file has more than 256 observations.
TypeError
Raised when the input ``obj`` is not a PySAL `W`.
Examples
--------
Expand Down Expand Up @@ -360,7 +362,7 @@ def write(self, obj):
hidcol = tuple(["<2H32b", 100, 32] + [0] * 32)
f.write(pack(*hidcol))
f.write(pack("<7H", 40, 10, 4, 76, 66, 2, 2))
f.write(pack("<2H1c", 41, 1, "'".encode()))
f.write(pack("<2H1c", 41, 1, b"'"))

id2i = obj.id2i

Expand All @@ -369,7 +371,7 @@ def write(self, obj):
for k in w_i[1]:
row[id2i[k]] = w_i[1][k]
for c, v in enumerate(row):
cell = tuple(["<2H1b2H1d", 14, 13, 113, i, c, v])
cell = ("<2H1b2H1d", 14, 13, 113, i, c, v)
f.write(pack(*cell))
f.write(pack("<4B", 1, 0, 0, 0))

Expand Down
6 changes: 4 additions & 2 deletions libpysal/io/iohandlers/wkt.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
# ruff: noqa: SIM115

from .. import fileio
from ..util import WKTParser

Expand All @@ -16,7 +18,7 @@ class WKTReader(fileio.FileIO):
Examples
--------
Read in WKT-formatted file.
>>> import libpysal
Expand Down Expand Up @@ -49,7 +51,7 @@ def __init__(self, *args, **kwargs):
self.__pos = 0
self.__open()

def open(self):
def open(self): # noqa A003
self.__open()

def __open(self):
Expand Down

0 comments on commit 776e001

Please sign in to comment.