Fix SyntaxError with Python 3 in JSON tutorial

unpacking a tuple argument is invalid in Python 3.

    >>> def foo(a, (b, c)):
      File "<stdin>", line 1
        def foo(a, (b, c)):
                   ^
    SyntaxError: invalid syntax

Fixes #403
This commit is contained in:
Wataru Ashihara 2019-09-22 13:51:14 +09:00
parent bb57629418
commit 7a13fb0f5b
No known key found for this signature in database
GPG Key ID: CE9EC2D5AE43DEFA
1 changed files with 10 additions and 5 deletions

View File

@ -230,7 +230,8 @@ from lark import Transformer
class MyTransformer(Transformer):
def list(self, items):
return list(items)
def pair(self, (k,v)):
def pair(self, key_value):
k, v = key_value
return k, v
def dict(self, items):
return dict(items)
@ -251,9 +252,11 @@ Also, our definitions of list and dict are a bit verbose. We can do better:
from lark import Transformer
class TreeToJson(Transformer):
def string(self, (s,)):
def string(self, s):
(s,) = s
return s[1:-1]
def number(self, (n,)):
def number(self, n):
(n,) = n
return float(n)
list = list
@ -315,9 +318,11 @@ json_grammar = r"""
"""
class TreeToJson(Transformer):
def string(self, (s,)):
def string(self, s):
(s,) = s
return s[1:-1]
def number(self, (n,)):
def number(self, n):
(n,) = n
return float(n)
list = list