acaos / python-pep505 Goto Github PK
View Code? Open in Web Editor NEWPython Polyfill for PEP 505 (None-aware / null-coalescing operators)
License: MIT License
Python Polyfill for PEP 505 (None-aware / null-coalescing operators)
License: MIT License
Hi Doug,
I've been reading today about PEP 505 and I wondered if the following syntax could take away the complaint about new Python users finding the .? syntax unintuitive:
x = foo -> .help -> .get_user("doug") -> ["name"]
This would be the none-aware way to access foo.help.get_user("doug")["name"]
.
It comes down to using -> .
instead of ?.
and -> [
instead of ?[
. I think that new users will intuitively understand that the ->
arrows form a chain that might be broken at some point (and then None is returned).
Best,
Maarten
ps I just came to the conclusion that I personally also find this syntax easier on the eyes than .?
. It seems to work well on the code examples that you give in PEP505
Hello.
In a Japanese Windows environment, the following parsing error occurs when attempting to execute source code containing non-ASCII (multibyte) characters.
> python -m main
Traceback (most recent call last):
File "C:\Python310\lib\runpy.py", line 187, in _run_module_as_main
mod_name, mod_spec, code = _get_module_details(mod_name, _Error)
File "C:\Python310\lib\runpy.py", line 157, in _get_module_details
code = loader.get_code(mod_name)
File "C:\Sandbox\sdvx_score_tool\venv\lib\site-packages\parsinghook\hook.py", line 98, in get_code
return FULLNAME_TO_LOADER[fullname]._parse_module()
File "C:\Sandbox\sdvx_score_tool\venv\lib\site-packages\parsinghook\hook.py", line 84, in _parse_module
tree = self._parser.parse_module(f, self._filename)
File "C:\Sandbox\sdvx_score_tool\venv\lib\site-packages\pep505\hook.py", line 52, in parse_module
tree = parser.start()
File "C:\Sandbox\sdvx_score_tool\venv\lib\site-packages\pep505\pegen\parser.py", line 60, in memoize_wrapper
tree = method(self, *args)
File "C:\Sandbox\sdvx_score_tool\venv\lib\site-packages\pep505\parser.py", line 405, in start
(file := self.file())
File "C:\Sandbox\sdvx_score_tool\venv\lib\site-packages\pep505\pegen\parser.py", line 60, in memoize_wrapper
tree = method(self, *args)
File "C:\Sandbox\sdvx_score_tool\venv\lib\site-packages\pep505\parser.py", line 416, in file
(a := self.statements(),)
File "C:\Sandbox\sdvx_score_tool\venv\lib\site-packages\pep505\pegen\parser.py", line 60, in memoize_wrapper
tree = method(self, *args)
File "C:\Sandbox\sdvx_score_tool\venv\lib\site-packages\pep505\parser.py", line 489, in statements
(a := self._loop1_3())
File "C:\Sandbox\sdvx_score_tool\venv\lib\site-packages\pep505\pegen\parser.py", line 60, in memoize_wrapper
tree = method(self, *args)
File "C:\Sandbox\sdvx_score_tool\venv\lib\site-packages\pep505\parser.py", line 6463, in _loop1_3
(statement := self.statement())
File "C:\Sandbox\sdvx_score_tool\venv\lib\site-packages\pep505\pegen\parser.py", line 60, in memoize_wrapper
tree = method(self, *args)
File "C:\Sandbox\sdvx_score_tool\venv\lib\site-packages\pep505\parser.py", line 500, in statement
(a := self.compound_stmt())
File "C:\Sandbox\sdvx_score_tool\venv\lib\site-packages\pep505\pegen\parser.py", line 60, in memoize_wrapper
tree = method(self, *args)
File "C:\Sandbox\sdvx_score_tool\venv\lib\site-packages\pep505\parser.py", line 669, in compound_stmt
self.positive_lookahead(self._tmp_7, )
File "C:\Sandbox\sdvx_score_tool\venv\lib\site-packages\pep505\pegen\parser.py", line 250, in positive_lookahead
ok = func(*args)
File "C:\Sandbox\sdvx_score_tool\venv\lib\site-packages\pep505\pegen\parser.py", line 60, in memoize_wrapper
tree = method(self, *args)
File "C:\Sandbox\sdvx_score_tool\venv\lib\site-packages\pep505\parser.py", line 6521, in _tmp_7
(literal := self.expect('def'))
File "C:\Sandbox\sdvx_score_tool\venv\lib\site-packages\pep505\pegen\parser.py", line 60, in memoize_wrapper
tree = method(self, *args)
File "C:\Sandbox\sdvx_score_tool\venv\lib\site-packages\pep505\pegen\parser.py", line 230, in expect
tok = self._tokenizer.peek()
File "C:\Sandbox\sdvx_score_tool\venv\lib\site-packages\pep505\pegen\tokenizer.py", line 46, in peek
tok = next(self._tokengen)
File "<string>", line 452, in _tokenize
UnicodeDecodeError: 'cp932' codec can't decode byte 0x82 in position 998: illegal multibyte sequence
The source code file itself is written in UTF-8, but we think the reason is that Python on Japanese Windows tries to open the file in CP932 (SHIFT-JIS) by default.
I don't know much about the inner workings, but if you can solve this by explicitly specifying the encoding when opening the file, please do so.
Thanks.
I will break Pycharm (or other IDE) syntax check, is there any solution?
Surprised to see we did something similar 😀 (Just a greeting, hope it doesn't bother you.)
https://mingshe.aber.sh/en/syntax/nullish-coalescing/
https://mingshe.aber.sh/en/syntax/optional-chaining/
A declarative, efficient, and flexible JavaScript library for building user interfaces.
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google ❤️ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.