SHARE
TWEET

print flag emoticons to console for copy and paste

DeaD_EyE Sep 21st, 2018 (edited) 96 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. import sys
  2. from string import ascii_uppercase as uppercase
  3. from unicodedata import lookup
  4. import argparse
  5.  
  6.  
  7. from iso3166 import countries_by_alpha2 as alpha2
  8. from iso3166 import countries_by_name as by_name
  9. # pip install iso3166
  10. # or use pycountry
  11.  
  12.  
  13. LETTER = 'REGIONAL INDICATOR SYMBOL LETTER {}'
  14. TRANSLATION = {c: lookup(LETTER.format(c)) for c in uppercase}
  15.  
  16.  
  17. def make_flag(code):
  18.     first, second = code.upper()
  19.     return TRANSLATION[first] + TRANSLATION[second]
  20.  
  21.  
  22. def make_countries_dict():
  23.     return {code : make_flag(code) for code in alpha2.keys()}
  24.    
  25.  
  26. def print_all():
  27.     flags = make_countries_dict()
  28.     print(' '.join(flags.values()))
  29.  
  30.  
  31. if __name__ == '__main__':
  32.     parser = argparse.ArgumentParser(description='This programm prints unicode flag emoticons to your console')
  33.     parser.add_argument('-a', action='store_true', help='print all flags')
  34.     parser.add_argument('-l', action='store_true', help='list all country codes')
  35.     parser.add_argument('--country', action='store', help='Print a flag by given country name')
  36.     parser.add_argument('--alpha2', action='store', help='Print a flag by given alpha2 code')
  37.     args = parser.parse_args()
  38.     if args.a:
  39.         print_all()
  40.     elif args.l:
  41.         for country in by_name.values():
  42.             print(country)
  43.     elif args.alpha2:
  44.         if args.alpha2.upper() not in alpha2:
  45.             print(f'{args.alpha2} does not exist', file=sys.stderr)
  46.             sys.exit(1)
  47.         print(make_flag(args.alpha2.upper()))
  48.     elif args.country:
  49.         country = by_name[args.country.upper()]
  50.         print(make_flag(country.alpha2))
  51.     else:
  52.         parser.print_help()
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
Not a member of Pastebin yet?
Sign Up, it unlocks many cool features!
 
Top