From 7a13fb0f5b968046795fa9d221a38c2a34503605 Mon Sep 17 00:00:00 2001 From: Wataru Ashihara Date: Sun, 22 Sep 2019 13:51:14 +0900 Subject: [PATCH] Fix SyntaxError with Python 3 in JSON tutorial unpacking a tuple argument is invalid in Python 3. >>> def foo(a, (b, c)): File "", line 1 def foo(a, (b, c)): ^ SyntaxError: invalid syntax Fixes #403 --- docs/json_tutorial.md | 15 ++++++++++----- 1 file changed, 10 insertions(+), 5 deletions(-) diff --git a/docs/json_tutorial.md b/docs/json_tutorial.md index ca1db73..9cc87e7 100644 --- a/docs/json_tutorial.md +++ b/docs/json_tutorial.md @@ -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