Created
June 14, 2019 05:27
-
-
Save mdneuzerling/855f050abdea1ad5c60b7c2c9ed8e920 to your computer and use it in GitHub Desktop.
Quick bit of code for determining whether an Australian licence plate is standard or personalised
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Takes a pattern like xxxddd (for three letters followed by three numbers) | |
# and converts it to regex. Other characters are left as is in the regex | |
# pattern string. This is helpful for states like SA in which every modern | |
# licence plate begins with an "S". | |
def match_plate_pattern(plate, pattern): | |
pattern = pattern.replace('x','[A-Za-z]').replace('d','[0-9]') | |
return(re.fullmatch(pattern, plate)) | |
# Some of these rules are a bit more general than they need to be. | |
# These rules aren't complete (I tended to ignore anything before 1970). | |
# Rules come from: | |
# https://en.wikipedia.org/wiki/Vehicle_registration_plates_of_Australia | |
# https://www.vicroads.vic.gov.au/registration/number-plates/general-issue-number-plates | |
licence_plate_patterns = { | |
"VIC": ["dxxdxx", # current car | |
"xxxddd", # old car | |
"dxdxx", # current motorcycle | |
"xxddd"], # old motorcycle | |
"NSW": ["xxddxx", # current car | |
"xxxddx", # old car | |
"xxxdd"], # motorcycles | |
"QLD": ["dddxxx", # current car | |
"dddxxd", # future car | |
"dddxx"], # motorcycles | |
"SA": ["Sdddxxx", # current car | |
"dddxxx", # old car | |
"Sddxxx", # current motorcycles | |
"ddxxx"], # old motorcycles | |
"WA": ["1xxxddd", # current style plates | |
"xxxddd", # old style plates before 1978 | |
"dxxddd", # old style plates 1978--1997 | |
"1ddxxx"], # current motorcycles (1997 onwards)] | |
"TAS": ["xddxx", # current style plates | |
"xxdddd", # old style plates 1970--2008 | |
"xxxddd", # old style plates 1954--1970 | |
"xxddd", # motorcycles | |
"xdddx"], # motorcycles | |
"ACT": ["xxxddx", # current style plates | |
"xxxddd", # future style plates | |
"xdddd"], # motorcycles | |
"NT": ["xxddxx", # current style plates | |
"dddddd", # future style plates | |
"xdddd", # current motorcycles | |
"ddddd"] # old motorcycles 1979--2011 | |
} | |
def plate_type(plate, state): | |
plate = re.sub(r'[\W_]+', '', str(plate)) # remove anything that isn't a letter or number | |
plate_matches = [match_plate_pattern(plate, pattern) for pattern | |
in licence_plate_patterns[state]] | |
if plate == "": | |
return("no_plate") | |
elif any(plate_matches): | |
return("standard_plate") | |
else: | |
return("custom_plate") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Random, but i came across this trying to accomplish something similar in PHP; identifying the likely state a plate originates from based on its format.
Im sure you are aware, but just in Victoria alone taxis, buses, trailers, club permits, primary producer vehicles etc will return a custom plate with this code.
I love this approach though. Its a clean way of accomplishing the task. What i have so far is way more convoluted 🤣