Skip to content

Instantly share code, notes, and snippets.

@dbernheisel
Created February 21, 2015 04:47
Show Gist options
  • Select an option

  • Save dbernheisel/7be362b6495907f87376 to your computer and use it in GitHub Desktop.

Select an option

Save dbernheisel/7be362b6495907f87376 to your computer and use it in GitHub Desktop.
This is a Python script to do some country code math when I was working with book metadata. It seemed that Amazon didn't recognize the newer country codes so they have an exception case. This was helpful in case someone expressed their sales rights as "Nowhere but US", but I needed to express it as "US only" (invert the expression).
#!/bin/python
import argparse
# Usage: country-math.py -a "US CA SS EY"
# Usage: country-math.py "US CA SS EY" -o "US CA SS EY GB"
parser = argparse.ArgumentParser(description="Intersect two ISO country code lists. Remove countries found in REMOVE_COUNTRIES from ORIGINAL_COUNTRIES or the world.")
parser.add_argument("countries", help="List of undelimited country codes surrounded by doublequotes that need to be removed", metavar="REMOVE_COUNTRIES")
parser.add_argument("-a", "--amazon", help="Remove country codes that are not supported by Amazon.", action='store_true')
parser.add_argument("-o", "--originallist", dest="originalcountries", help="List of undelimited country codes that represent original list. This list needs to be surrounded by doublequotes. If this flag is not provided, then program will default to all country codes", metavar="ORIGINAL_COUNTRIES")
parser.set_defaults(amazon=False)
AllCountries = [ "AD", "AE", "AF", "AG", "AI", "AL", "AM", "AO", "AQ", "AR", "AS", "AT", "AU", "AW", "AX", "AZ", "BA", "BB", "BD", "BE", "BF", "BG", "BH", "BI", "BJ", "BL", "BM", "BN", "BO", "BQ", "BR", "BS", "BT", "BV", "BW", "BY", "BZ", "CA", "CC", "CD", "CF", "CG", "CH", "CI", "CK", "CL", "CM", "CN", "CO", "CR", "CS", "CU", "CV", "CW", "CX", "CY", "CZ", "DE", "DJ", "DK", "DM", "DO", "DZ", "EC", "EE", "EG", "EH", "ER", "ES", "ET", "FI", "FJ", "FK", "FM", "FO", "FR", "GA", "GB", "GD", "GE", "GF", "GG", "GH", "GI", "GL", "GM", "GN", "GP", "GQ", "GR", "GS", "GT", "GU", "GW", "GY", "HK", "HM", "HN", "HR", "HT", "HU", "ID", "IE", "IL", "IM", "IN", "IO", "IQ", "IR", "IS", "IT", "JE", "JM", "JO", "JP", "KE", "KG", "KH", "KI", "KM", "KN", "KP", "KR", "KW", "KY", "KZ", "LA", "LB", "LC", "LI", "LK", "LR", "LS", "LT", "LU", "LV", "LY", "MA", "MC", "MD", "ME", "MF", "MG", "MH", "MK", "ML", "MM", "MN", "MO", "MP", "MQ", "MR", "MS", "MT", "MU", "MV", "MW", "MX", "MY", "MZ", "NA", "NC", "NE", "NF", "NG", "NI", "NL", "NO", "NP", "NR", "NU", "NZ", "OM", "PA", "PE", "PF", "PG", "PH", "PK", "PL", "PM", "PN", "PR", "PS", "PT", "PW", "PY", "QA", "RE", "RO", "RS", "RU", "RW", "SA", "SB", "SC", "SD", "SE", "SG", "SH", "SI", "SJ", "SK", "SL", "SM", "SN", "SO", "SR", "SS", "ST", "SV", "SX", "SY", "SZ", "TC", "TD", "TF", "TG", "TH", "TJ", "TK", "TL", "TM", "TN", "TO", "TR", "TT", "TV", "TW", "TZ", "UA", "UG", "UM", "US", "UY", "UZ", "VA", "VC", "VE", "VG", "VI", "VN", "VU", "WF", "WS", "YE", "YT", "YU", "ZA", "ZM", "ZW" ]
UnsupportedAMZ = [ "BQ", "CS", "CW", "SS", "SX", "YU" ]
def main():
args = parser.parse_args()
removecountries = args.countries.replace(",","").replace(";","").split(" ")
if args.originalcountries:
countries = args.originalcountries.replace(",","").replace(";","").split(" ")
else:
countries = AllCountries
# Validate given country codes
for x in removecountries:
if x not in AllCountries:
print "%s is not a valid Country Code. Removed from list." % x
removecountries.remove(x)
for x in countries:
if x not in AllCountries:
print "%s is not a valid Country Code. Removed from list." % x
countries.remove(x)
# Return intersection
if args.amazon:
removecountries = removecountries + UnsupportedAMZ
finalcountries = [x for x in countries if x not in removecountries]
removedcountries = [x for x in countries if x in removecountries]
print " "
print "Final List:"
print " ".join(finalcountries)
print " "
print "Countries identified that were removed from final list:"
print " ".join(removedcountries)
if __name__ == "__main__":
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment