Skip to content

Commit

Permalink
uses re.match instead of re.search
Browse files Browse the repository at this point in the history
  • Loading branch information
davidpeckham committed Feb 12, 2024
1 parent 6820a60 commit a3699c6
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 10 deletions.
9 changes: 5 additions & 4 deletions src/vin/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -129,15 +129,16 @@ def _decode_vin(self) -> None:
Raises:
DecodingError: Unable to decode VIN using NHTSA vPIC.
"""
vehicle: DecodedVehicle = None
db_path = files("vin").joinpath("vehicle.db")
with VehicleDatabase(path=db_path) as db:
model_year = self._decode_model_year()
if model_year > 0:
vehicle = db.lookup_vehicle(self.wmi, self.vds, model_year)
vehicle = db.lookup_vehicle(self.wmi, self.descriptor, model_year)
else:
vehicle = db.lookup_vehicle(self.wmi, self.vds, abs(model_year))
vehicle = db.lookup_vehicle(self.wmi, self.descriptor, abs(model_year))
if not vehicle:
vehicle = db.lookup_vehicle(self.wmi, self.vds, abs(model_year) - 30)
vehicle = db.lookup_vehicle(self.wmi, self.descriptor, abs(model_year) - 30)
if vehicle is None:
raise DecodingError()

Expand Down Expand Up @@ -381,7 +382,7 @@ def descriptor(self) -> str:
Returns:
str: the 11- or 14-character descriptor for this VIN
"""
return f"{self._vin[3:9]}|{self._vin[9:]}"
return f"{self._vin[3:8]}|{self._vin[9:]}"
# if self._vin[2] == "9":
# return descriptor[:14]
# else:
Expand Down
10 changes: 4 additions & 6 deletions src/vin/database.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,8 @@ class DecodedVehicle:


def regex(value, pattern):
"""REGEXP shim for SQLite versions that lack it"""
rex = re.compile("^" + pattern)
found = rex.search(value) is not None
"""REGEXP shim for SQLite versions bundled with Python 3.11 and earlier"""
found = re.match(pattern, value) is not None
print(f"{value=} {pattern=} {'found' if found else '---'}")
return found

Expand All @@ -40,10 +39,9 @@ def __enter__(self) -> "VehicleDatabase":
Build the database and schema if requested.
"""
log.debug(f"Opening database {self._path.absolute()}")
connection = sqlite3.connect(
self._path, isolation_level="DEFERRED", detect_types=sqlite3.PARSE_DECLTYPES
)
connection = sqlite3.connect(self._path, detect_types=sqlite3.PARSE_DECLTYPES)
connection.row_factory = sqlite3.Row
# version = sqlite3.sqlite_version_info
connection.create_function("REGEXP", 2, regex)
self._connection = connection
return self
Expand Down
5 changes: 5 additions & 0 deletions tests/cars/test_decode.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,3 +13,8 @@ def test_inconclusive_model_year() -> None:
identify the model year, but we can decode it using vPIC data.
"""
assert VIN("2GCEC19Z0S1245490").model_year == 1995


def test_chevy_silverado() -> None:
v = VIN("1GCHK29U21E231713")
assert v.description == "2001 Chevrolet Silverado 2500 3/4 Ton"

0 comments on commit a3699c6

Please sign in to comment.