Exemplo n.º 1
0
def test_import():
    """
    test import statement with from/import/as
    """
    assert convertor("導入 sys") == "import sys"
    assert convertor("導入 sys 作為 unix") == "import sys as unix"
    assert convertor("從 os 導入 path 作為 url") == "from os import path as url"
Exemplo n.º 2
0
def test_is():
    """
    test is, is not statement
    """
    assert convertor("4 為 4") == ("4 is 4")
    assert convertor("4 是 4") == ("4 is 4")
    assert convertor("4 不是 2") == ("4 is not 2")
Exemplo n.º 3
0
def test_try():
    """
    test try except
    """
    assert convertor("尝试: 导入 a; 异常 ImportError, e: 打印 e") == \
                "try: import a; except ImportError, e: print e"
    assert convertor("引发 例外") == "raise Exception"
Exemplo n.º 4
0
def test_import():
    """
    test import statement with from/import/as
    """
    assert convertor("载入 sys") == "import sys"
    assert convertor("载入 sys 取名 unix") == "import sys as unix"
    assert convertor("从 os 载入 path 取名 url") == "from os import path as url"
Exemplo n.º 5
0
def test_try():
    """
    test try except
    """
    assert convertor("尝试: 导入 a; 异常 ImportError, e: 打印 e") == \
                "try: import a; except ImportError, e: print e"
    assert convertor("引发 例外") == "raise Exception"
Exemplo n.º 6
0
def test_string():
    """
    same as print test
    """
    s = "hello.py"
    assert convertor("s.開始字串('he')") == "s.startswith('he')"
    assert convertor("s.結束字串('he')") == "s.endswith('he')"
Exemplo n.º 7
0
def test_print():
    """
    test output statement and string types
    """
    assert convertor("印出 'hello'") == "print 'hello'"
    assert convertor('印出 "hello"') == 'print "hello"'
    assert convertor("""印出 'hello'""") == "print 'hello'"
Exemplo n.º 8
0
def test_import():
    """
    test import statement with from/import/as
    """
    assert convertor("载入 sys") == "import sys"
    assert convertor("载入 sys 取名 unix") == "import sys as unix"
    assert convertor("从 os 载入 path 取名 url") == "from os import path as url"
Exemplo n.º 9
0
def test_try():
    """
    test try except
    """
    assert convertor("嘗試: 導入 a; 異常 ImportError, e: 印出 e") == \
                "try: import a; except ImportError, e: print e"
    assert convertor("引發 例外") == "raise Exception"
Exemplo n.º 10
0
def test_print():
    """
    test output statement and string types
    """
    assert convertor("印出 'hello'") == "print 'hello'"
    assert convertor('印出 "hello"') == 'print "hello"'
    assert convertor("""印出 'hello'""") == "print 'hello'"
Exemplo n.º 11
0
def test_import():
    """
    test import statement with from/import/as
    """
    assert convertor("导入 sys") == "import sys"
    assert convertor("导入 sys 作为 unix") == "import sys as unix"
    assert convertor("从 os 导入 path 作为 url") == "from os import path as url"
Exemplo n.º 12
0
def test_is():
    """
    test is, is not statement
    """
    assert convertor("4 為 4") == ("4 is 4")
    assert convertor("4 是 4") == ("4 is 4")
    assert convertor("4 不是 2") == ("4 is not 2")
Exemplo n.º 13
0
def test_import():
    """
    test import statement with from/import/as
    """
    assert convertor("导入 sys") == "import sys"
    assert convertor("导入 sys 作为 unix") == "import sys as unix"
    assert convertor("从 os 导入 path 作为 url") == "from os import path as url"
Exemplo n.º 14
0
def test_try():
    """
    test try except
    """
    assert convertor("嘗試: 導入 a; 異常 ImportError, e: 印出 e") == \
                "try: import a; except ImportError, e: print e"
    assert convertor("引發 例外") == "raise Exception"
Exemplo n.º 15
0
def test_import():
    """
    test import statement with from/import/as
    """
    assert convertor("導入 sys") == "import sys"
    assert convertor("導入 sys 作為 unix") == "import sys as unix"
    assert convertor("從 os 導入 path 作為 url") == "from os import path as url"
Exemplo n.º 16
0
def test_string():
    """
    same as print test
    """
    s = "hello.py"
    assert convertor("s.開始字串('he')") == "s.startswith('he')"
    assert convertor("s.結束字串('he')") == "s.endswith('he')"
Exemplo n.º 17
0
def test_boolean():
    """
    test boolean type
    """
    assert convertor("n = 真") == "n = True"
    assert convertor("p = 假") == "p = False"
    assert convertor("q = 实") == "q = True"
    assert convertor("r = 虛") == "r = False"
Exemplo n.º 18
0
def test_if():
    """
    test if, elif, else
    """
    assert convertor("如果 a: 略過") == "if a: pass"
    assert convertor("如果 a: 略過; 否則: 略過") == "if a: pass; else: pass"
    assert convertor("如果 a: 略過; 假使 b: 略過; 否則: 略過") == \
                    "if a: pass; elif b: pass; else: pass"
Exemplo n.º 19
0
def test_boolean():
    """
    test boolean type
    """
    assert convertor("n = 真") == "n = True"
    assert convertor("p = 假") == "p = False"
    assert convertor("q = 实") == "q = True"
    assert convertor("r = 虛") == "r = False"
Exemplo n.º 20
0
def test_list():
    """
    test list type
    """
    assert convertor("列表((1,2,3,4)) == [1,2,3,4]") == "list((1,2,3,4)) == [1,2,3,4]"
    assert convertor("a = []; a.加入(2); 宣告 a == [2]") == "a = []; a.append(2); assert a == [2]"
    p = "h,e,l,l,o"
    assert convertor('p.分离(",")') == 'p.split(",")'
Exemplo n.º 21
0
def test_if():
    """
    test if, elif, else
    """
    assert convertor("如果 a: 略過") == "if a: pass"
    assert convertor("如果 a: 略過; 否則: 略過") == "if a: pass; else: pass"
    assert convertor("如果 a: 略過; 假使 b: 略過; 否則: 略過") == \
                    "if a: pass; elif b: pass; else: pass"
Exemplo n.º 22
0
def test_string():
    """
    same as print test
    """
    assert convertor("s.开头为('he')") == "s.startswith('he')"
    assert convertor("s.结尾为('he')") == "s.endswith('he')"
    assert convertor("items = 'zero one two three'.分离()") == "items = 'zero one two three'.split()"
    assert convertor("''.连接(s)") == "''.join(s)"
Exemplo n.º 23
0
def test_if():
    """
    test if, elif, else
    """
    assert convertor("如果 a: 略过") == "if a: pass"
    assert convertor("如果 a: 略过; 否则: 略过") == "if a: pass; else: pass"
    assert convertor("如果 a: 略过; 否则如果 b: 略过; 否则: 略过") == \
                    "if a: pass; elif b: pass; else: pass"
Exemplo n.º 24
0
def test_if():
    """
    test if, elif, else
    """
    assert convertor("如果 a: 略过") == "if a: pass"
    assert convertor("如果 a: 略过; 否则: 略过") == "if a: pass; else: pass"
    assert convertor("如果 a: 略过; 否则如果 b: 略过; 否则: 略过") == \
                    "if a: pass; elif b: pass; else: pass"
Exemplo n.º 25
0
def test_bool():
    """
    test boolean type
    """
    assert convertor("布尔(1)") == "bool(1)"
    assert convertor("n 是 真") == "n is True"
    assert convertor("p 为 假") == "p is False"
    assert convertor("q 不是 实") == "q is not True"
    assert convertor("r 不为 虛") == "r is not False"
Exemplo n.º 26
0
def test_string():
    """
    same as print test
    """
    assert convertor("s.开头为('he')") == "s.startswith('he')"
    assert convertor("s.结尾为('he')") == "s.endswith('he')"
    assert convertor("items = 'zero one two three'.分离()"
                     ) == "items = 'zero one two three'.split()"
    assert convertor("''.连接(s)") == "''.join(s)"
Exemplo n.º 27
0
def test_bool():
    """
    test boolean type
    """
    assert convertor("布尔(1)") == "bool(1)"
    assert convertor("n 是 真") == "n is True"
    assert convertor("p 为 假") == "p is False"
    assert convertor("q 不是 实") == "q is not True"
    assert convertor("r 不为 虛") == "r is not False"
Exemplo n.º 28
0
def test_operators():
    """
    test operators
    
    >>> 1 == 1
    True
    """
    assert convertor("a 等于 b") == "a == b"
    assert convertor("a 不等于 b") == "a != b"
Exemplo n.º 29
0
def test_operators():
    """
    test operators
    
    >>> 1 == 1
    True
    """
    assert convertor("a 等於 b") == "a == b"
    assert convertor("a 不等於 b") == "a != b"
Exemplo n.º 30
0
def test_print():
    """
    test output statement and string types
    
    >>> print "hello"
    hello
    """
    assert convertor("打印 'hello'") == "print 'hello'"
    assert convertor('打印 "hello"') == 'print "hello"'
    assert convertor("""打印 'hello'""") == "print 'hello'"
Exemplo n.º 31
0
def test_list():
    """
    test list type
    """
    assert convertor("列表((1,2,3,4)) == [1,2,3,4]") == \
                    "list((1,2,3,4)) == [1,2,3,4]"
    assert convertor("a = []; a.加入(2); 宣告 a == [2]") == \
                    "a = []; a.append(2); assert a == [2]"
    p = "h,e,l,l,o"
    assert convertor('p.分离(",")') == 'p.split(",")'
Exemplo n.º 32
0
def test_print():
    """
    test output statement and string types
    
    >>> print "hello"
    hello
    """
    assert convertor("打印 'hello'") == "print 'hello'"
    assert convertor('打印 "hello"') == 'print "hello"'
    assert convertor("""打印 'hello'""") == "print 'hello'"
Exemplo n.º 33
0
def test_convertor():
    """
    test convertor
    """
    rev_annotator()
    assert convertor("印出 'hello'") == "print 'hello'" 
    assert python_convertor("print 'hello'") == ("印出 'hello'")
    assert python_convertor(convertor("印出 'hello'")) == ("印出 'hello'")
    # check if variable convert correctly, not a python valid syntax
    assert python_convertor(convertor("打出 '''哈囉'''")) == ("打出 '''哈囉'''")
Exemplo n.º 34
0
def test_list():
    """
    test list type
    
    >>> list((1,2,3,4)) == [1,2,3,4]
    True
    """
    assert convertor("列表((1,2,3,4)) == [1,2,3,4]") == \
                    "list((1,2,3,4)) == [1,2,3,4]"
    assert convertor("a = []; a.加入(2); 申明 a == [2]") == \
                    "a = []; a.append(2); assert a == [2]"
    p = "h,e,l,l,o"
    assert convertor('p.分離(",")') == 'p.split(",")'
def test_enumerate():
    """
    test enumerate
    
    """
    assert convertor("取 (index, item) 在 列举(items): 打印 index, item") == \
                    "for (index, item) in enumerate(items): print index, item"
Exemplo n.º 36
0
 def push(self, line):
     self.buffer.append(line)
     source = "\n".join(self.buffer)
     more = self.runsource(convertor(source), self.filename)
     if not more:
         self.resetbuffer()
     return more
Exemplo n.º 37
0
 def push(self, line):
     self.buffer.append(line)
     source = "\n".join(self.buffer)
     more = self.runsource(convertor(source), self.filename)
     if not more:
         self.resetbuffer()
     return more
def test_enumerate():
    """
    test enumerate
    
    """
    assert convertor("取 (index, item) 在 列举(items): 打印 index, item") == \
                    "for (index, item) in enumerate(items): print index, item"
Exemplo n.º 39
0
def test_string():
    """
    same as print test
    
    >>> s = "hello.py"
    >>> s.startswith('he')
    True
    >>> s.endswith('py')
    True
    >>> s = ['one', 'two']
    >>> ''.join(s)
    'onetwo'
    """
    assert convertor("s.开头为('he')") == "s.startswith('he')"
    assert convertor("s.结尾为('py')") == "s.endswith('py')"
    assert convertor("items = 'zero one two three'.分离()") == "items = 'zero one two three'.split()"
    assert convertor("''.连接(s)") == "''.join(s)"
Exemplo n.º 40
0
def test_set():
    """
    test set type
    
    >>> set([1,2,3,4]) == set([1, 2, 3, 4])
    True
    """
    assert convertor("集合([1,2,3,4]) == set([1, 2, 3, 4])") == \
                    "set([1,2,3,4]) == set([1, 2, 3, 4])"
Exemplo n.º 41
0
def test_tuple():
    """
    test tuple type
    
    >>> tuple([1,2,3,4]) == (1,2,3,4)
    True
    """
    assert convertor("組合([1,2,3,4]) == (1,2,3,4)") == \
                    "tuple([1,2,3,4]) == (1,2,3,4)"
Exemplo n.º 42
0
def test_dict():
    """
    test dict type
    
    >>> dict(a=1, b=2) == {'a':1, 'b':2}
    True
    """
    assert convertor("字典(a=1, b=2) == {'a':1, 'b':2}") == \
                    "dict(a=1, b=2) == {'a':1, 'b':2}"
Exemplo n.º 43
0
def test_tuple():
    """
    test tuple type
    
    >>> tuple([1,2,3,4]) == (1,2,3,4)
    True
    """
    assert convertor("数组([1,2,3,4]) == (1,2,3,4)") == \
                    "tuple([1,2,3,4]) == (1,2,3,4)"
Exemplo n.º 44
0
def test_string():
    """
    same as print test
    
    >>> s = "hello.py"
    >>> s.startswith('he')
    True
    >>> s.endswith('py')
    True
    >>> s = ['one', 'two']
    >>> ''.join(s)
    'onetwo'
    """
    assert convertor("s.开头为('he')") == "s.startswith('he')"
    assert convertor("s.结尾为('py')") == "s.endswith('py')"
    assert convertor("items = 'zero one two three'.分离()"
                     ) == "items = 'zero one two three'.split()"
    assert convertor("''.连接(s)") == "''.join(s)"
Exemplo n.º 45
0
def test_set():
    """
    test set type
    
    >>> set([1,2,3,4]) == set([1, 2, 3, 4])
    True
    """
    assert convertor("集合([1,2,3,4]) == set([1, 2, 3, 4])") == \
                    "set([1,2,3,4]) == set([1, 2, 3, 4])"
Exemplo n.º 46
0
def test_dict():
    """
    test dict type
    
    >>> dict(a=1, b=2) == {'a':1, 'b':2}
    True
    """
    assert convertor("字典(a=1, b=2) == {'a':1, 'b':2}") == \
                    "dict(a=1, b=2) == {'a':1, 'b':2}"
def test_enumerate():
    """
    test enumerate
    
    >>> items = ['zero', 'one', 'two', 'three']
    >>> print list(enumerate(items))
    [(0, 'zero'), (1, 'one'), (2, 'two'), (3, 'three')]
    """
    assert convertor("取 (index, item) 在 列舉(items): 印出 index, item") == \
                    "for (index, item) in enumerate(items): print index, item"
Exemplo n.º 48
0
def test_int():
    """
    test int type
    
    >>> int(2.0)
    2
    >>> int(3.1415926)
    3
    """
    assert convertor("整数(2.0)") == "int(2.0)"
Exemplo n.º 49
0
def test_int():
    """
    test int type
    
    >>> int(2.0)
    2
    >>> int(3.1415926)
    3
    """
    assert convertor("整數(2.0)") == "int(2.0)"
Exemplo n.º 50
0
def test_enumerate():
    """
    test enumerate
    
    >>> items = ['zero', 'one', 'two', 'three']
    >>> print list(enumerate(items))
    [(0, 'zero'), (1, 'one'), (2, 'two'), (3, 'three')]
    """
    assert convertor("取 (index, item) 在 列舉(items): 印出 index, item") == \
                    "for (index, item) in enumerate(items): print index, item"
Exemplo n.º 51
0
    def push(self, line):
        self.buffer.append(line)
        source = "\n".join(self.buffer)
        #windows patch
        encoding = sys.stdout.encoding
        if encoding == 'cp950':
            encoding = ''

        more = self.runsource(convertor(source, encoding=encoding),
                              self.filename)
        if not more:
            self.resetbuffer()
        return more
Exemplo n.º 52
0
    def push(self, line):
        self.buffer.append(line)
        source = "\n".join(self.buffer)
        #windows patch
        encoding = sys.stdout.encoding
        if encoding == 'cp950':
            encoding = ''

        more = self.runsource(convertor(source, encoding=encoding),
                            self.filename)
        if not more:
            self.resetbuffer()
        return more
Exemplo n.º 53
0
def test_file():
    """
    test file type
    """
    assert convertor('fd = 打开("ReadMe_test.txt", "r")') == 'fd = open("ReadMe_test.txt", "r")'
    assert convertor("temp = fd.读一行()") == "temp = fd.readline()"
    assert convertor("temp = fd.读多行()") == "temp = fd.readlines()"
    assert convertor("temp = fd.读取()") == "temp = fd.read()"
    assert convertor("fd.写入(temp)") == "fd.write(temp)"
    assert convertor("fd.关闭()") == "fd.close()"
Exemplo n.º 54
0
def test_file():
    """
    test file type
    """
    assert convertor('fd = 打开("ReadMe_test.txt", "r")') == \
                    'fd = open("ReadMe_test.txt", "r")'
    assert convertor('temp = fd.读一行()') == 'temp = fd.readline()'
    assert convertor('temp = fd.读多行()') == 'temp = fd.readlines()'
    assert convertor('temp = fd.读取()') == 'temp = fd.read()'
    assert convertor('fd.写入(temp)') == 'fd.write(temp)'
    assert convertor('fd.关闭()') == 'fd.close()'
Exemplo n.º 55
0
def test_file():
    """
    test file type
    """
    assert convertor('fd = 打開("ReadMe_test.txt", "r")') == \
                    'fd = open("ReadMe_test.txt", "r")'
    assert convertor('temp = fd.讀一行()') == 'temp = fd.readline()'
    assert convertor('temp = fd.讀多行()') == 'temp = fd.readlines()'
    assert convertor('temp = fd.讀取()') == 'temp = fd.read()'
    assert convertor('fd.寫入(temp)') == 'fd.write(temp)'
    assert convertor('fd.關閉()') == 'fd.close()'
Exemplo n.º 56
0
def test_bool():
    """
    test boolean type
    
    """
    assert convertor("布林(1)") == "bool(1)"
    assert convertor("n is 真") == "n is True"
    assert convertor("n 是 真") == "n is True"
    assert convertor("p 為 假") == "p is False"
    assert convertor("q 不是 實") == "q is not True"
    assert convertor("r 不為 虛") == "r is not False"
Exemplo n.º 57
0
def test_bool():
    """
    test boolean type
    
    """
    assert convertor("布林(1)") == "bool(1)"
    assert convertor("n is 真") == "n is True"
    assert convertor("n 是 真") == "n is True"
    assert convertor("p 為 假") == "p is False"
    assert convertor("q 不是 實") == "q is not True"
    assert convertor("r 不為 虛") == "r is not False"
Exemplo n.º 58
0
def test_file():
    """
    test file type
    """
    assert convertor('fd = 打開("ReadMe_test.txt", "r")') == \
                    'fd = open("ReadMe_test.txt", "r")'
    assert convertor('temp = fd.讀一行()') == 'temp = fd.readline()'
    assert convertor('temp = fd.讀多行()') == 'temp = fd.readlines()'
    assert convertor('temp = fd.讀取()') == 'temp = fd.read()'
    assert convertor('fd.寫入(temp)') == 'fd.write(temp)'
    assert convertor('fd.關閉()') == 'fd.close()'
Exemplo n.º 59
0
def test_convertor():
    """
    test convertor
    """
    rev_annotator()
    assert convertor("印出 'hello'") == "print 'hello'"
    assert python_convertor("print 'hello'") == ("印出 'hello'")
    assert python_convertor(convertor("印出 'hello'")) == ("印出 'hello'")
    # check if variable convert correctly, not a python valid syntax
    assert python_convertor(convertor("打出 '''哈囉'''")) == ("打出 '''哈囉'''")
    assert python_convertor(
        convertor("打出 '''p_哈囉_v'''")) == ("打出 '''p_哈囉_v'''")
    assert python_convertor(convertor("打出 '''哈\
    囉'''")) == ("打出 '''哈\
    囉'''")
    assert python_convertor(convertor('打出 """哈\
    _囉"""')) == ('打出 """哈\
    _囉"""')
Exemplo n.º 60
0
def commandtool():
    """command line tool method
    
    input:
        speficy the input source
    output:
        speficy the output source
    python:
        compile to python and run
    cmp:
        input raw zhpy source and run
    encoding:
        specify the encoding
    """
    parser = OptionParser(
        usage="usage: %prog [-i|-p] input [-o] [output] [--e] [encoding]",
        version="zhpy %s" % version)
    parser.add_option("-i",
                      "--input",
                      help="speficy the input source",
                      dest="input",
                      default=None)
    parser.add_option("-o",
                      "--output",
                      help="speficy the output source",
                      dest="output",
                      default=None)
    parser.add_option("-p",
                      "--python",
                      help="compile to python and run",
                      dest="python",
                      default=None)
    parser.add_option("-c",
                      "--cmd",
                      help="input raw zhpy source and run",
                      dest="cmp",
                      default=None)
    parser.add_option("-e",
                      "--encoding",
                      help="specify the encoding",
                      dest="encoding",
                      default="")
    (options, args) = parser.parse_args()

    os.chdir(os.getcwd())
    #run as script
    if options.cmp:
        test = options.cmp
        annotator()
        if options.encoding:
            result = convertor(test, options.encoding)
        else:
            result = convertor(test)
        try_run(result)
        return
    #run as command
    #TODO: accept args
    argv = sys.argv[1:]
    if len(argv) >= 1:
        if (options.input is None) and argv[0].endswith("py"):
            options.input = argv[0]
        if options.python:
            options.input = options.python
        #if options.input:

        test = file(options.input, "r").read()
        annotator()
        if options.encoding:
            result = convertor(test, options.encoding)
        else:
            result = convertor(test)

        if len(argv) == 2:
            if argv[0].endswith("py") and argv[1].endswith("py"):
                options.output = argv[1]
            if options.python:
                filename = os.path.splitext(options.python)[0]
                file("n_" + filename + ".py", "w").write(result)
                print "compile to python and run: %s" % ("n_" + filename +
                                                         ".py")
        if options.output:
            file(options.output, "w").write(result)
        else:
            try_run(result)
    else:
        from zhpy_interpreter import interpreter
        interpreter()