Skip to content

Instantly share code, notes, and snippets.

@glasnt glasnt/regional.py
Last active Aug 25, 2016

Embed
What would you like to do?
๐Ÿ‡ทโ€‹๐Ÿ‡ชโ€‹๐Ÿ‡ฌโ€‹๐Ÿ‡ฎโ€‹๐Ÿ‡ดโ€‹๐Ÿ‡ณโ€‹๐Ÿ‡ฆโ€‹๐Ÿ‡ฑโ€‹ โ€‹๐Ÿ‡ฎโ€‹๐Ÿ‡ณโ€‹๐Ÿ‡ฉโ€‹๐Ÿ‡ฎโ€‹๐Ÿ‡จโ€‹๐Ÿ‡ฆโ€‹๐Ÿ‡นโ€‹๐Ÿ‡ดโ€‹๐Ÿ‡ทโ€‹ โ€‹๐Ÿ‡จโ€‹๐Ÿ‡ญโ€‹๐Ÿ‡ฆโ€‹๐Ÿ‡ทโ€‹๐Ÿ‡ฆโ€‹๐Ÿ‡จโ€‹๐Ÿ‡นโ€‹๐Ÿ‡ชโ€‹๐Ÿ‡ทโ€‹๐Ÿ‡ธโ€‹ โ€‹๐Ÿ‡ฆโ€‹๐Ÿ‡ธโ€‹ โ€‹๐Ÿ‡ฆโ€‹ โ€‹๐Ÿ‡ธโ€‹๐Ÿ‡ชโ€‹๐Ÿ‡ทโ€‹๐Ÿ‡ปโ€‹๐Ÿ‡ฎโ€‹๐Ÿ‡จโ€‹๐Ÿ‡ช
"""
Usage: python3 regional.py list of words
Prints a representation of the words as Regional Identifiers, and copies the output directly to keyboard for you
This is because some terminals think that zero width spaces are silly.
Currently supports: A-Z
Requires: Python 3
Now less complex, thanks @bmispelon!
"""
import sys
import xerox
import unicodedata
def regional_indicator(letter):
# Try and see if there's a REGIONAL INDICATOR SYMBOL for our letter.
try:
return unicodedata.lookup('REGIONAL INDICATOR SYMBOL LETTER %s' % letter.upper());
except KeyError:
return letter
output = "\u200B".join([regional_indicator(x) for x in " ".join(sys.argv[1:]).upper()])
xerox.copy(output)
print("Copied to Clipboard: %s" % output)
@bmispelon

This comment has been minimized.

Copy link

bmispelon commented Aug 23, 2016

You can use unicodedata.lookup('REGIONAL INDICATOR SYMBOL LETTER %s' % letter.upper()) and make the code much shorter.

@glasnt

This comment has been minimized.

Copy link
Owner Author

glasnt commented Aug 24, 2016

@bmispelon Thank you! ๐ŸŽ‰ I thought there'd be a better way than what I had. I've updated it accordingly

@salty-horse

This comment has been minimized.

Copy link

salty-horse commented Aug 24, 2016

You could even lookup 'REGIONAL INDICATOR SYMBOL LETTER A' and just add an offset from there (they are in sequence), instead of looking up each letter in the Unicode database.

@salty-horse

This comment has been minimized.

Copy link

salty-horse commented Aug 24, 2016

A few more silly comments:

The list comprehension's brackets '[ ]' inside the join() can be removed to turn it into a generator expression, which is faster and does the same thing.

The code currently sandwiches Zero-Width Spaces between regional indicators and spaces between words, which isn't necessary. Can be fixed by joining with U+200B before joining with " ".

@AnthonyBriggs

This comment has been minimized.

Copy link

AnthonyBriggs commented Aug 24, 2016

You can even one-line it if you're feeling particularly evil. Something like

`output = "\u200B".join([unicodedata.lookup('REGIONAL INDICATOR SYMBOL LETTER %s' % x.upper()) if 65 <= ord(x.upper()) <= 90 else ' ' for x in ' '.join(sys.argv[1:])])`

should work ๐Ÿ˜‡

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You canโ€™t perform that action at this time.