From b275b0c53086ad5fb896d2a3de5fb36e8ca1981a Mon Sep 17 00:00:00 2001 From: hantianfeng Date: Thu, 29 Aug 2024 17:01:56 +0800 Subject: [PATCH] =?UTF-8?q?=E6=94=AF=E6=8C=81=E5=87=BD=E6=95=B0=E9=BB=98?= =?UTF-8?q?=E8=AE=A4=E5=80=BC=EF=BC=8C=E6=97=A5=E6=9C=9F=E6=A0=BC=E5=BC=8F?= =?UTF-8?q?=E5=8C=96?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- cases/fstring.py | 5 +++++ cases/snippets/11.py | 10 ++++++++++ cases/snippets/19.py | 7 +++++++ cases/snippets/9.py | 6 ++++++ src/Python/Translator.php | 17 +++++++++++++---- 5 files changed, 41 insertions(+), 4 deletions(-) create mode 100644 cases/fstring.py create mode 100644 cases/snippets/11.py create mode 100644 cases/snippets/19.py create mode 100644 cases/snippets/9.py diff --git a/cases/fstring.py b/cases/fstring.py new file mode 100644 index 00000000..4c016938 --- /dev/null +++ b/cases/fstring.py @@ -0,0 +1,5 @@ +from datetime import datetime; + +date_val = datetime.utcnow() + +print(f'{date_val=:%Y-%m-%d}') # date_val=2021-07-09 diff --git a/cases/snippets/11.py b/cases/snippets/11.py new file mode 100644 index 00000000..2ae5f1da --- /dev/null +++ b/cases/snippets/11.py @@ -0,0 +1,10 @@ +from math import ceil + +def chunk_into_n(lst, n): + size = ceil(len(lst) / n) + return list( + map(lambda x: lst[x * size:x * size + size], + list(range(n))) + ) + +chunk_into_n([1, 2, 3, 4, 5, 6, 7], 4) # [[1, 2], [3, 4], [5, 6], [7]] diff --git a/cases/snippets/19.py b/cases/snippets/19.py new file mode 100644 index 00000000..aa2942b4 --- /dev/null +++ b/cases/snippets/19.py @@ -0,0 +1,7 @@ +a = {'max': 200} +b = {'min': 100, 'max': 250} +c = {'min': 50} + +print(a['min']) +print(a['min'] + b['min'] + c['min']) # throws KeyError +print(a.get('min', 0) + b.get('min', 0) + c.get('min', 0)) # 150 diff --git a/cases/snippets/9.py b/cases/snippets/9.py new file mode 100644 index 00000000..c2c65ba3 --- /dev/null +++ b/cases/snippets/9.py @@ -0,0 +1,6 @@ +def capitalize(s, lower_rest=False, val3=3423): + return ''.join([s[:1].upper(), (s[1:].lower() if lower_rest else s[1:])]) + + +capitalize('fooBar') # 'FooBar' +capitalize('fooBar', True) # 'Foobar' diff --git a/src/Python/Translator.php b/src/Python/Translator.php index f5f8a67c..61b7e9d1 100644 --- a/src/Python/Translator.php +++ b/src/Python/Translator.php @@ -258,7 +258,7 @@ class Translator extends \PhpAot\Core\Translator } elseif ($op == 'Del') { return $slice->id . '->__delitem__(' . $target . ')'; } else { - return $slice->id . '->__getitem__(' . $target . ')'; + return $slice->id . '[' . $target . ']'; } } @@ -275,7 +275,7 @@ class Translator extends \PhpAot\Core\Translator return implode(';' . PHP_EOL, $imports); } - function parseImport($node) + function parseImport($node): string { $out = ''; foreach ($node->names as $name) { @@ -359,9 +359,14 @@ class Translator extends \PhpAot\Core\Translator function parseArguments($args): string { + $defaults = $args->defaults ?? []; $names = []; - foreach ($args->args as $arg) { - $names[] = '$' . $arg->arg; + foreach ($args->args as $k => $arg) { + $tmp = '$' . $arg->arg; + if ($k >= count($args->args) - count($defaults)) { + $tmp .= ' = ' . $this->parseValue($defaults[$k - count($args->args) + count($defaults)]); + } + $names[] = $tmp; } if ($args->vararg) { $names[] = '...$' . $args->vararg->arg; @@ -914,6 +919,10 @@ class Translator extends \PhpAot\Core\Translator $format = $value->format_spec->values[0]->value; if ($format[0] == '.' and $format[-1] == 'f') { return 'round(' . $expr . ', ' . substr($format, 1, strlen($format) - 2) . ')'; + } elseif (str_contains($format, '%')) { + return $expr . '->strftime("' . $format . '")'; + } else { + throw new \RuntimeException("Unsupported format: $format"); } } else { return $expr;