Results 1 to 5 of 5

Thread: [Python] Writing upside-down characters

  1. #1
    Join Date
    Mar 2008
    Beans
    4,714
    Distro
    Ubuntu 9.10 Karmic Koala

    [Python] Writing upside-down characters

    I would like to make a command-line python program to translate [a-zA-Z] strings into their "upside-down" equivalent:

    ˙ʇuǝlɐʌınbǝ "uʍop-ǝpısdn" ɹıǝɥʇ oʇuı sƃuıɹʇs [z-ɐz-ɐ] ǝʇɐlsuɐɹʇ oʇ ɯɐɹƃoɹd uoɥʇʎd ǝuıl-puɐɯɯoɔ ɐ ǝʞɐɯ oʇ ǝʞıl plnoʍ ı

    I used this to list all the utf8 characters:
    PHP Code:
    #!/usr/bin/env python
    codec='utf8'
    for num in range(32,65536):
        
    achr=unichr(num)
        print(
    "'':%s, # %s"%(num,achr.encode(codec))) 
    Using that list, I was able to come up with this:
    PHP Code:
    #!/usr/bin/env python
    # -*- encoding: utf-8 -*-

    import sys
    rot180
    ={
        
    'a':592# ɐ
        
    'b':5227# ᑫ
        
    'd':1088# р
        
    'e':600# ɘ
        
    'h':1063# Ч
        
    'l':645# ʅ    
        
    'p':599# ɗ
        
    'r':633# ɹ
        
    't':647# ʇ
        
    'v':8743# ∧
        
    'w':653# ʍ
        
    'y':654# ʎ
        
    'A':5578# ᗊ
        
    'B':5626# ᗺ
        
    'C':390# Ɔ
        
    'D':5613# ᗭ
        
    'E':8707# ∃
        
    'O':2848# ଠ
        
    'R':5512# ᖈ
        
    'T':639# ɿ
        
    'U':1055# П
        
    'V':923# Λ
        
    '3':5620# ᗴ
        
    }

    input_str=sys.argv[1]
    print(
    ''.join([unichr(rot180.get(achr,ord(achr))).encode('utf8') for achr in input_str])) 
    In so far as it goes, it works okay:
    Code:
    % upsidedown.py Howdy
    Hoʍрʎ
    The problem is, I don't have a complete set of upside-down characters.

    According to http://www.sevenwires.com/play/UpsideDownLetters.html, the characters come from the "Latin Extended" and "International Phonetic Alphabet" character sets.

    I've looked these character sets here: file:///usr/share/doc/python2.5/html/lib/standard-encodings.html but couldn't find them.

    Do you know what encoding I should be using to access upside-down characters?
    Last edited by unutbu; June 23rd, 2009 at 02:44 PM.

  2. #2
    Join Date
    Feb 2009
    Beans
    789
    Distro
    Ubuntu 10.04 Lucid Lynx

    Re: [Python] Writing upside-down characters

    I don't think that every arbitrary character has an upside-down equivalent in some character set so I think the best you can do is to manually create a mapping between characters and their upside-down equivalent.

    For example, in the source of the webpage that you mentioned, it also uses a table to lookup the upside-down character.

  3. #3
    Join Date
    Mar 2008
    Beans
    4,714
    Distro
    Ubuntu 9.10 Karmic Koala

    Re: [Python] Writing upside-down characters

    Thanks simeon87. Indeed, I don't mind making a dictionary by hand, even though it feels icky

    I've only made a cursory run through the utf-8 character set to build the rot180 dictionary as it stands so far.

    The problem is, I'm not sure I'm going to find a complete set of upside down characters even for [a-z] in utf8.

    http://www.sevenwires.com/play/UpsideDownLetters.html demonstrates that javascript can do it. I'd like to be able to do it in Python.

    Should I just keep looking through my list of utf8, or am I looking in the wrong place?
    Last edited by unutbu; June 23rd, 2009 at 02:58 PM.

  4. #4
    Join Date
    Mar 2008
    Beans
    4,714
    Distro
    Ubuntu 9.10 Karmic Koala

    Re: [Python] Writing upside-down characters

    Oh, I'm so silly. The javascript code is visible if you click View>Page Source... LOL

  5. #5
    Join Date
    Mar 2008
    Beans
    4,714
    Distro
    Ubuntu 9.10 Karmic Koala

    Re: [Python] Writing upside-down characters

    Here is the finished script. It can do everything http://www.sevenwires.com/play/UpsideDownLetters.html can do, but from the command-line.
    (I also added a few capital letters.)

    Many thanks to simeon87 for his help.

    PHP Code:
    #!/usr/bin/env python
    # -*- encoding: utf-8 -*-
    import sys

    __usage__
    ='''
    upsidedown.py "The quick brown fox jumps over the lazy dog."
    ˙ƃop ʎzɐʅ ɘɥʇ ɹɘʌo sdɯuɾ xoɟ uʍoɹq ʞɔıuq ɘɥﻠ
    '''

    rot180={
        
    'a' u'\u0250'# ɐ
        
    'b' u'q',
        
    'c' u'\u0254'# ɔ
        
    'd' u'p',
        
    'e' u'\u0258'# ǝ
        
    'f' u'\u025F'# ɟ
        
    'g' u'\u0183'# ƃ
        
    'h' u'\u0265'# ɥ
        
    'i' u'\u0131'# ı
        
    'j' u'\u027E'# ɾ
        
    'k' u'\u029E'# ʞ
        
    'l' u'\u0285'# ʅ    
        
    'm' u'\u026F'# ɯ
        
    'n' u'u'# u
        
    'p' u'd',
        
    'r' u'\u0279'# ɹ
        
    't' u'\u0287'# ʇ
        
    'u' u'n',
        
    'v' u'\u028C'# ʌ
        
    'w' u'\u028D'# ʍ
        
    'y' u'\u028E'# ʎ
        
    '.' u'\u02D9'# ˙
        
    '[' u']',
        
    '(' u')',
        
    '{' u'}',
        
    '?' u'\u00BF'# ¿
        
    '!' u'\u00A1'# ¡
        
    "\'" u','
        
    '<' u'>',
        
    '_' u'\u203E'# ‾
        
    '"' u'\u201E'# „
        
    '\\' u'\\',
        
    ';' u'\u061B'# ؛
        
    '\u203F' u'\u2040'# ‿ --> ⁀
        
    '\u2045' u'\u2046'# ⁅ --> ⁆
        
    '\u2234' u'\u2235'# ∴ --> ∵
    #     'A':u'\u15CA', # ᗊ (viewable in emacs, but not in terminal)
    #     'B':u'\u15FA', # ᗺ (viewable in emacs, but not in terminal)
        
    'C':u'\u0186'# Ɔ (viewable in emacs, but not in terminal)
    #     'D':u'\u15ED', # ᗭ (viewable in emacs, but not in terminal)
        
    'E':u'\u2203'# ∃ (viewable in emacs, but not in terminal)
    #     'N':u'\u2d4d', # ⵍ (viewable in terminal, but not in emacs)
    #     'O':u'\u0B20', # ଠ (viewable in emacs, but not in terminal)
    #     'R':u'\u1588', # ᖈ (viewable in emacs, but not in terminal)
        
    'T':u'\uFEE0'# ﻠ
        
    'U':u'\u041F'# П
        
    'V':u'\u039B'# Λ
    #     'V':u'\u2d37', # ⴷ (viewable in terminal, but not in emacs)
    #     'Y':u'\u2d43', # ⵃ (viewable in terminal, but not in emacs)
    #     '2':u'\u2D52', # ⵒ (viewable in terminal, but not in emacs)
    #     '3':u'\u15F4', # ᗴ (viewable in emacs, but not in terminal)
        
    }

    input_str=sys.argv[1]
    print(
    ''.join(reversed([rot180.get(achr,achr) for achr in input_str]))) 
    If anyone can explain why certain unicode characters are viewable in emacs, but not from a gnome-terminal, and vice versa, I'd really like to know. (See the commented-out source code for examples.)
    Last edited by unutbu; July 19th, 2009 at 10:09 PM.

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •