Skip to content

Instantly share code, notes, and snippets.

@xxiz
Last active April 25, 2024 19:02
Show Gist options
  • Save xxiz/79e437c462e9e7dd0ae77d66e8d6c1d6 to your computer and use it in GitHub Desktop.
Save xxiz/79e437c462e9e7dd0ae77d66e8d6c1d6 to your computer and use it in GitHub Desktop.
Create a canadian postal code based on a province
def generate_postal_suffix():
n = str(random.randint(0, 9))
l = random.choice(string.ascii_letters)
n2 = str(random.randint(0, 9))
return n + l + n2
def generate_postal_code():
import json
postal_codes = json.load(open("postal_codes.json", "r")) # https://gist.github.com/xxiz/e276ad0e9d0b786e492c8e4085f76a91
PROVINCE = "BC"
postal_codes = [
item["name"] for item in postal_codes["data"] if PROVINCE in item["detail"]
]
postal_code = random.choice(postal_codes) + generate_postal_suffix()
return postal_code
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment