suggestions for VIN parsing

Ben Finney ben+python at benfinney.id.au
Thu Dec 25 21:18:13 EST 2014


Vincent Davis <vincent at vincentdavis.net> writes:

> I don't want to parse the page, I what a function that given a VIN
> (frame or engine number) returns the year the bike was made.

So the page has a collection of tables which the reader can use to
manually look up the VIN, or elements of the VIN, to determine the range
of dates of manufacture.

Your problem is to come up with a suitable data structure to map VIN to
date range.

The core of this, in Python, is going to be a ‘dict’ instance. You need
to represent “part of a VIN” as the key, and “range of dates” as the
resulting value.


For the value, a “date range” is expressed simply by a 2-item tuple of
dates::

    import datetime
    date_range = (datetime.date(1979, 8, 1), datetime.date(1980, 7, 31))

If you want something a little more expressive, make a namedtuple to
name the items in the tuple::

    import datetime
    import collections

    DateRange = collections.namedtuple('DateRange', ['begin', 'end'])

    date_range = DateRange(
            begin=datetime.date(1979, 8, 1),
            end=datetime.date(1980, 7, 31))


Given that a VIN is (despite the number) not a number, but instead a
string of characters, I would recommend using “string prefix” as the
key.

To match a VIN, iterate through the keys and attempt a match against the
prefix; if a match is found, the date range is obtained simply by
getting the corresponding value from the dictionary.

However, you have some entries in those tables with “prefix ranges”. You
can extrapolate from what I wrote here to come up with a method for
matching within a range of prefixes.


I *strongly* recommend keeping the data set small until you come up with
a working means to store and look up the information. While you do so,
feel free to post (small!) code examples here to show your working.

Good hunting.

-- 
 \     “The Vatican is not a state.… a state must have territory. This |
  `\         is a palace with gardens, about as big as an average golf |
_o__)                         course.” —Geoffrey Robertson, 2010-09-18 |
Ben Finney




More information about the Python-list mailing list