From 776e0011bd8d94316832e0d7e6d50b2a0c9086b6 Mon Sep 17 00:00:00 2001 From: James Gaboardi Date: Sat, 11 Nov 2023 14:19:19 -0500 Subject: [PATCH] lint & format `io/iohandlers{t-w}`.py (#642) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * lint&format io/iohandlers{p-s}.py * martin suggestion – keep commented * lint&format io/iohandlers{t-w}.py * martin suggest – comment out --- libpysal/io/iohandlers/template.py | 102 ++++++++++++++--------------- libpysal/io/iohandlers/wk1.py | 40 +++++------ libpysal/io/iohandlers/wkt.py | 6 +- 3 files changed, 73 insertions(+), 75 deletions(-) diff --git a/libpysal/io/iohandlers/template.py b/libpysal/io/iohandlers/template.py index 0ae9df384..b06e7eff7 100644 --- a/libpysal/io/iohandlers/template.py +++ b/libpysal/io/iohandlers/template.py @@ -4,6 +4,8 @@ """ +# ruff: noqa: N812, SIM115 + from .. import fileio as FileIO __author__ = "Charles R Schmidt " @@ -12,7 +14,6 @@ # Always subclass FileIO class TemplateWriter(FileIO.FileIO): - # REQUIRED, List the formats this class supports. FORMATS = ["foo"] @@ -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. @@ -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)) @@ -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() @@ -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): @@ -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) diff --git a/libpysal/io/iohandlers/wk1.py b/libpysal/io/iohandlers/wk1.py index 25600a8cb..6a5da4e6f 100644 --- a/libpysal/io/iohandlers/wk1.py +++ b/libpysal/io/iohandlers/wk1.py @@ -1,6 +1,9 @@ +# ruff: noqa: ARG002, N802, SIM115 + import struct -from .. import fileio + from ...weights import W +from .. import fileio __author__ = "Myunghwa Hwang " __all__ = ["Wk1IO"] @@ -8,7 +11,7 @@ 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. @@ -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 @@ -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"] @@ -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) @@ -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 -------- @@ -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 = " 0: ngh = neighbors.setdefault(row, []) @@ -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 -------- @@ -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 @@ -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)) diff --git a/libpysal/io/iohandlers/wkt.py b/libpysal/io/iohandlers/wkt.py index 6eb05a921..8ff33befe 100644 --- a/libpysal/io/iohandlers/wkt.py +++ b/libpysal/io/iohandlers/wkt.py @@ -1,3 +1,5 @@ +# ruff: noqa: SIM115 + from .. import fileio from ..util import WKTParser @@ -16,7 +18,7 @@ class WKTReader(fileio.FileIO): Examples -------- - + Read in WKT-formatted file. >>> import libpysal @@ -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):