Skip to content

Instantly share code, notes, and snippets.

@inky
Last active August 29, 2015 14:23
Show Gist options
  • Select an option

  • Save inky/10a14ceecb1c41d7c7c8 to your computer and use it in GitHub Desktop.

Select an option

Save inky/10a14ceecb1c41d7c7c8 to your computer and use it in GitHub Desktop.

Revisions

  1. @ljcooke ljcooke revised this gist Jun 14, 2015. 1 changed file with 9 additions and 3 deletions.
    12 changes: 9 additions & 3 deletions flag.py
    Original file line number Diff line number Diff line change
    @@ -9,9 +9,15 @@
    OFFSET = ord('A')

    def flag(code):
    """Return the Unicode flag emoji for a two-letter country code."""
    assert len(code) == 2
    assert all(c in string.ascii_letters for c in code)
    """
    Return the Unicode flag emoji for a two-letter country code.
    """
    if len(code) != 2:
    raise ValueError('country code must be two letters long')
    if not all(c in string.ascii_letters for c in code):
    raise ValueError('country code must only contain letters A-Z')

    return ''.join(CHARS[ord(c) - OFFSET] for c in code.upper())

    def main():
  2. @ljcooke ljcooke created this gist Jun 14, 2015.
    34 changes: 34 additions & 0 deletions flag.py
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,34 @@
    #!/usr/bin/env python3
    import argparse
    import string
    import sys

    CHARS = ("๐Ÿ‡ฆ", "๐Ÿ‡ง", "๐Ÿ‡จ", "๐Ÿ‡ฉ", "๐Ÿ‡ช", "๐Ÿ‡ซ", "๐Ÿ‡ฌ", "๐Ÿ‡ญ", "๐Ÿ‡ฎ", "๐Ÿ‡ฏ", "๐Ÿ‡ฐ", "๐Ÿ‡ฑ", "๐Ÿ‡ฒ",
    "๐Ÿ‡ณ", "๐Ÿ‡ด", "๐Ÿ‡ต", "๐Ÿ‡ถ", "๐Ÿ‡ท", "๐Ÿ‡ธ", "๐Ÿ‡น", "๐Ÿ‡บ", "๐Ÿ‡ป", "๐Ÿ‡ผ", "๐Ÿ‡ฝ", "๐Ÿ‡พ", "๐Ÿ‡ฟ")

    OFFSET = ord('A')

    def flag(code):
    """Return the Unicode flag emoji for a two-letter country code."""
    assert len(code) == 2
    assert all(c in string.ascii_letters for c in code)
    return ''.join(CHARS[ord(c) - OFFSET] for c in code.upper())

    def main():
    parser = argparse.ArgumentParser()
    parser.add_argument('country_code', nargs='*',
    help='a two-letter country code (ISO 3166-1)')
    parser.add_argument('-s', '--sep', help='separator')
    args = parser.parse_args()

    if args.country_code:
    flags = (flag(code) for code in args.country_code)
    else:
    parser.print_help()
    return 1

    sep = ' ' if args.sep is None else args.sep
    print(sep.join(flags))

    if __name__ == '__main__':
    sys.exit(main())