ricekuo / zhpy

Automatically exported from code.google.com/p/zhpy
0 stars 0 forks source link

Add zhpy interpreter #9

Closed GoogleCodeExporter closed 8 years ago

GoogleCodeExporter commented 8 years ago
draft zhpy interpreter implementation
http://groups.google.com/group/python-cn/browse_thread/thread/d85ad9a285998774/a
55caca591268753#a55caca591268753

{{{
zhpy.py

    os.chdir(os.getcwd())
    +# no args
    +if len(sys.argv) == 1:
    +    from zhipy import interpreter
    +    interpreter()
    +    sys.exit()
}}}

{{{
#zhipy by jiahua huang

# -*- coding: utf-8 -*-

def _indict(dic):
    '''indict({'a':'1'})  -> {'1':'a'}
    '''
    d = {}
    dick = dic.keys()
    dick.reverse()
    map(d.update, map(lambda i: {dic[i]:i}, dick))
    return d

from zhpy import *
en_cndict = _indict(cndict)
en_twdict = _indict(twdict)

"""def convertToChinese(s, l, t):
    tmp = t[0]
    if tmp in en_twdict:
        return en_twdict[tmp]
    elif re.match(r'^p_[_\d]*_v\d?$', tmp):
        tmp2 = ''
        for i in tmp.split('_'):
            if i.isdigit():
                tmp2 += unichr(int(i)).encode('utf8')
        return tmp2
    else:
        return tmp

englishChars = srange('[0-z]')
englishWord = Word(englishChars)
englishWord.setParseAction(convertToChinese)
englishWord = quotedString | englishWord"""

import code
def code4zh2en(code):
    """
    """
    for k, v in replacedict.items():
        code = code.replace(k,v)

    ucode = code.decode("utf8")
    result = chineseWord.transformString(ucode)
    result = result.encode("utf8")
    return result

class console(code.InteractiveConsole):
    """
    """
    def push(self, line):
        line = code4zh2en(line)
        self.buffer.append(line)
        source = "\n".join(self.buffer)
        more = self.runsource(source, self.filename)
        if not more:
            self.resetbuffer()
        return more

def interpreter():
    """
    zhpy interpreter
    """
    import readline, rlcompleter
    readline.parse_and_bind("tab: complete")
    con = console()
    banner = 'Python %s\nzhipy.py - An Chinese Interactive Python
shell'%(sys.version)
    con.interact(banner)
}}}

Original issue reported on code.google.com by gasolin on 24 Aug 2007 at 6:49

GoogleCodeExporter commented 8 years ago
fixed in [198]

Original comment by gasolin on 30 Aug 2007 at 4:14