From f7018ddf8eeca527c9df3ed0e3bf4855a9a0f6c5 Mon Sep 17 00:00:00 2001 From: tianfenghan Date: Thu, 28 Dec 2023 19:20:52 +0800 Subject: [PATCH] =?UTF-8?q?=E6=B5=AE=E7=82=B9=E5=9E=8B=E7=B2=BE=E5=BA=A6?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- cases/float.py | 3 +++ conv.php | 18 +++++++++++++++++- web/public/index.php | 11 ++++++----- web/public/input.php | 2 ++ web/public/output.php | 2 ++ web/public/static/css/style.css | 3 +-- 6 files changed, 31 insertions(+), 8 deletions(-) create mode 100644 cases/float.py diff --git a/cases/float.py b/cases/float.py new file mode 100644 index 00000000..a7fc8021 --- /dev/null +++ b/cases/float.py @@ -0,0 +1,3 @@ +price_val = 6.12658 +world = "swoole" +print(f'{price_val:.2f}, hello={world}') # 6.13 diff --git a/conv.php b/conv.php index 5cc303c9..f730d7bb 100644 --- a/conv.php +++ b/conv.php @@ -263,7 +263,7 @@ class Translator case 'Subscript': return $this->parseSubscript($value); case 'FormattedValue': - return $this->parseValue($value->value); + return $this->parseFormattedValue($value); case 'UnaryOp': return $this->parseTarget($value); case 'BoolOp': @@ -921,6 +921,22 @@ class Translator $code = 'return ' . $lines[0] . PHP_EOL; return $code . PHP_EOL . $this->getIndent() . '}'; } + + private function parseFormattedValue($value) + { + $expr = $this->parseValue($value->value); + if ($value->format_spec and $value->format_spec->_type == 'JoinedStr') { + if (count($value->format_spec->values) != 1) { + debug($value); + } + $format = $value->format_spec->values[0]->value; + if ($format[0] == '.' and $format[-1] == 'f') { + return 'round(' . $expr . ', ' . substr($format, 1, strlen($format) - 2) . ')'; + } + } else { + return $expr; + } + } } error_reporting(E_ERROR); diff --git a/web/public/index.php b/web/public/index.php index 82063abc..a9247cc3 100644 --- a/web/public/index.php +++ b/web/public/index.php @@ -20,11 +20,12 @@ diff --git a/web/public/input.php b/web/public/input.php index b4486f81..86802ccb 100644 --- a/web/public/input.php +++ b/web/public/input.php @@ -83,6 +83,8 @@ define('ROOT_PATH', dirname(__DIR__)); window.parent.frames[1].updateCode() } + $('#divCodeWrapper').css('height', (window.screen.height - 30) + 'px') + $('#btn-convert').click(function () { const code = textarea1.value diff --git a/web/public/output.php b/web/public/output.php index 6ad1a9ec..3aca59b0 100644 --- a/web/public/output.php +++ b/web/public/output.php @@ -65,6 +65,8 @@ define('ROOT_PATH', dirname(__DIR__)); $('#btn-copy').attr('disabled', 'disabled').text('已复制代码') }) + $('#divCodeWrapper').css('height', (window.screen.height - 30) + 'px') + function updateLineNumbers() { let lineCount = textarea1.value.split('\n').length; let lines = ''; diff --git a/web/public/static/css/style.css b/web/public/static/css/style.css index c01c0a7a..9c1129a6 100644 --- a/web/public/static/css/style.css +++ b/web/public/static/css/style.css @@ -3,7 +3,6 @@ select { } #divCodeWrapper { - height: calc(100vh - 200px); max-height: 800px; width: 1200px; overflow: hidden; @@ -41,7 +40,7 @@ textarea { padding: 15px; z-index: 2; overflow-x: auto; - overflow-y: scroll; + overflow-y: auto; white-space: nowrap; background-color: rgba(0, 0, 0, 0); color: rgba(0, 0, 0, 0);