#python 3.12 final version is expected to arrive in October 2023. Along other changes we are to embrace the "Syntactic formalization of f-strings", which includes:
1) Quotes reuse inside f-strings, which makes it possible to have nested f-strings further than 4 levels deep.
In my humble opinion f-strings nesting presents unnecessary cognitive complexity and should be avoided in production. Though developers decided to ignore such concerns to focus on the parser technically and wish for linters to handle the situation.
2) Multi-line expressions and comments will be allowed.
Which IMO could make complex f-strings easier to read. Though preparing it's parts elsewhere is more likely to be easier to read.
3) Backslashes inside f-strings will be allowed.
Which will make it possible to use escaping and all related mechanisms like working with the newlines or emoji (Unicode escaped sequences). Though escaped braces are still disallowed intentionally, at least "for the time being" (c).
More details could be red in PEP-701.
Reasoning behind the f-strings formalization includes:
- lowering the maintenance cost of the CPython parser;
- allowing the new PEG parser to apply improved error messages;
- making f-strings a part of the "official Python grammar" so that other Python implementations can be sure they implemented f-strings correctly;
The mechanism behind the changes introduces 3 new tokens for the parser to work with: FSTRING_START, FSTRING_MIDDLE, FSTRING_END. So, the tokenizer won't be able to produce a single token for f-strings anymore, will need more.
1) Quotes reuse inside f-strings, which makes it possible to have nested f-strings further than 4 levels deep.
In my humble opinion f-strings nesting presents unnecessary cognitive complexity and should be avoided in production. Though developers decided to ignore such concerns to focus on the parser technically and wish for linters to handle the situation.
2) Multi-line expressions and comments will be allowed.
Which IMO could make complex f-strings easier to read. Though preparing it's parts elsewhere is more likely to be easier to read.
3) Backslashes inside f-strings will be allowed.
Which will make it possible to use escaping and all related mechanisms like working with the newlines or emoji (Unicode escaped sequences). Though escaped braces are still disallowed intentionally, at least "for the time being" (c).
More details could be red in PEP-701.
Reasoning behind the f-strings formalization includes:
- lowering the maintenance cost of the CPython parser;
- allowing the new PEG parser to apply improved error messages;
- making f-strings a part of the "official Python grammar" so that other Python implementations can be sure they implemented f-strings correctly;
The mechanism behind the changes introduces 3 new tokens for the parser to work with: FSTRING_START, FSTRING_MIDDLE, FSTRING_END. So, the tokenizer won't be able to produce a single token for f-strings anymore, will need more.
β€2
One of the key concepts of #python is the concept of callable.
Callable is an object that can be called. Basically, all of those are callables: user-defined functions, instance methods, generator functions, coroutine functions, async generator functions, built-in functions, built-in methods, classes and class instances.
In order to be a classic callable - you need to have a dunder method "call" to be defined. As it is in my short demo code on the attached image.
I make a custom class inheriting all there is from the float class. And I add the only method of the topic - the call dunder. For fun sake upon using our object as function - we will return a float times the specified number. In order to return our Callable_Number other than the float - we need to also modify the multiplication dunder. Which is now by default returns the float type.
On the line 5 we initiate our class. By the way, we are calling the class to get it's instance. That's how classes themselves are callable. And we get our Callable_Number object as the result. The following print statement clearly shows the float value and the existence of the call dunder, thanks to the callable built-in function.
On the line 7 we CALL the "a" object, an INSTANCE of our class. That's where the call dunder is activated. Thus the multiplication as a result. As you see - our newly created "b" object lacks the call dunder. Since it is a usual float due to the mechanism I described previously.
Useful links:
1 - callable built-in function https://docs.python.org/3/library/functions.html#callable
2 - datamodel with callable types described https://docs.python.org/3/reference/datamodel.html
3 - expressions reference on calls https://docs.python.org/3/reference/expressions.html#calls
4 - C-API call protocol https://docs.python.org/3/c-api/call.html
Callable is an object that can be called. Basically, all of those are callables: user-defined functions, instance methods, generator functions, coroutine functions, async generator functions, built-in functions, built-in methods, classes and class instances.
In order to be a classic callable - you need to have a dunder method "call" to be defined. As it is in my short demo code on the attached image.
I make a custom class inheriting all there is from the float class. And I add the only method of the topic - the call dunder. For fun sake upon using our object as function - we will return a float times the specified number. In order to return our Callable_Number other than the float - we need to also modify the multiplication dunder. Which is now by default returns the float type.
On the line 5 we initiate our class. By the way, we are calling the class to get it's instance. That's how classes themselves are callable. And we get our Callable_Number object as the result. The following print statement clearly shows the float value and the existence of the call dunder, thanks to the callable built-in function.
On the line 7 we CALL the "a" object, an INSTANCE of our class. That's where the call dunder is activated. Thus the multiplication as a result. As you see - our newly created "b" object lacks the call dunder. Since it is a usual float due to the mechanism I described previously.
Useful links:
1 - callable built-in function https://docs.python.org/3/library/functions.html#callable
2 - datamodel with callable types described https://docs.python.org/3/reference/datamodel.html
3 - expressions reference on calls https://docs.python.org/3/reference/expressions.html#calls
4 - C-API call protocol https://docs.python.org/3/c-api/call.html
The way I like my imports to be refactored in #python - is for them to be grouped and each name on a single line. It is highly readable, less prone for errors.
A simple way to refactor imports - is to use the isort library. I prefer to utilize the following flags:
mode 3 - stands for VERTICAL_HANGING_INDENT
force grid wrap 2 - to start grid wrapping with only 2 imported names
trailing comma - so that all lines are similar
With some shortcuts the final command looks like this:
You can edit the isort config file in order to NOT TO use all those flags each time. For these and other helpful details you can always refer to the isort official documentation: https://pycqa.github.io/isort/index.html
A simple way to refactor imports - is to use the isort library. I prefer to utilize the following flags:
mode 3 - stands for VERTICAL_HANGING_INDENT
force grid wrap 2 - to start grid wrapping with only 2 imported names
trailing comma - so that all lines are similar
With some shortcuts the final command looks like this:
isort file-name.py -m 3 -fgw 2 -tcYou can edit the isort config file in order to NOT TO use all those flags each time. For these and other helpful details you can always refer to the isort official documentation: https://pycqa.github.io/isort/index.html
During the 20-th PyErevan meetup one of the speakers - Ruben - showed how a #python #PyGame application can be #WASM-ed and shared through the net.
For fun sake I made my own humble visual #game and shared it through the Itch.io
- game hosted on https://rioran.itch.io/random-shifting-colors
- source code can be found on github https://github.com/Rioran/random-colors-visuals
For fun sake I made my own humble visual #game and shared it through the Itch.io
- game hosted on https://rioran.itch.io/random-shifting-colors
- source code can be found on github https://github.com/Rioran/random-colors-visuals
Telegram
PYerevan
Hey, Pythonistas!
Let me remind that we start our event in almost an hour, at 19:00!
We are looking forward to meeting you!
https://www.eventbrite.com/e/pyerevan-20-meetup-around-python-tickets-698442459197
Let me remind that we start our event in almost an hour, at 19:00!
We are looking forward to meeting you!
https://www.eventbrite.com/e/pyerevan-20-meetup-around-python-tickets-698442459197
Hey, dear #python enthusiasts, PyPI just went through a public audit! A huge masterpiece report can be found in the Trail Of Bits repository . Shortly: 8 medium and 6 low severity exposures, low test coverage, but overall all is good.
Yo, check out an awesome package and project manager for #python !
https://docs.astral.sh/uv/
Easily installed on Windows even with:
Though take notice:
Here are some official documentation highlights:
π A single tool to replace pip, pip-tools, pipx, poetry, pyenv, twine, virtualenv, and more.
β‘οΈ 10-100x faster than pip.
π Provides comprehensive project management, with a universal lockfile.
βοΈ Runs scripts, with support for inline dependency metadata.
π Installs and manages Python versions.
π Runs and installs tools published as Python packages.
π© Includes a pip-compatible interface for a performance boost with a familiar CLI.
π’ Supports Cargo-style workspaces for scalable projects.
πΎ Disk-space efficient, with a global cache for dependency deduplication.
β¬ Installable without Rust or Python via curl or pip.
π₯ Supports macOS, Linux, and Windows.
uv is backed by Astral, the creators of Ruff.
https://docs.astral.sh/uv/
Easily installed on Windows even with:
pip install uv
Though take notice:
If installing from PyPI, we recommend installing uv into an isolated environment, e.g., with pipx
Here are some official documentation highlights:
π A single tool to replace pip, pip-tools, pipx, poetry, pyenv, twine, virtualenv, and more.
β‘οΈ 10-100x faster than pip.
π Provides comprehensive project management, with a universal lockfile.
βοΈ Runs scripts, with support for inline dependency metadata.
π Installs and manages Python versions.
π Runs and installs tools published as Python packages.
π© Includes a pip-compatible interface for a performance boost with a familiar CLI.
π’ Supports Cargo-style workspaces for scalable projects.
πΎ Disk-space efficient, with a global cache for dependency deduplication.
β¬ Installable without Rust or Python via curl or pip.
π₯ Supports macOS, Linux, and Windows.
uv is backed by Astral, the creators of Ruff.
π₯1
Hey there! Here is a quick recap on asyncio Event Loop in #python
Asyncio itself is written in pure Python but depends on platform-specific C-functions.
Event Loop Policy is a global per-process object. Controls the Event Loop. Could be changed via API. Single event loop per thread by default.
Event Loop itself is an object too. Can be passed into different contexts. Has 2 stages: running or stopped. Relies on monotonic clock to avoid errors of going back in time (essentially the system's uptime).
Event loop collects coroutines, Tasks and Futures as handles. Assigns callbacks to these handles. Plans execution in a timed heap if possible. And checks for events needed by some handles.
Related links:
https://peps.python.org/pep-3153/ - Asynchronous IO support - precedes PEP 3156
https://peps.python.org/pep-3156/ - Asynchronous IO Support Rebooted: the βasyncioβ Module - the key PEP which inspired this post
https://peps.python.org/pep-0418/#time-monotonic - Add monotonic time, performance counter, and process time functions - monotonic related link
https://peps.python.org/pep-3148/ - futures - execute computations asynchronously
https://peps.python.org/pep-0380/ - Syntax for Delegating to a Subgenerator (yield from)
https://peps.python.org/pep-0492/ - Coroutines with async and await syntax
https://github.com/python/cpython/blob/main/Lib/asyncio/base_events.py - source code containing the actual loop of Event Loop inside the method called "run_forever". Each iteration is processed inside the method "_run_once".
https://github.com/python/cpython/blob/main/Lib/selectors.py - key "select" method which crucial for collecting events
https://www.youtube.com/watch?v=RIVcqT2OGPA - AsyncIO and the Event Loop explained by ArjanCodes
https://www.youtube.com/watch?v=Ww2HBNpu98g - Demystifying AsyncIO: Building Your Own Event Loop in Python β Arthur Pastel
Asyncio itself is written in pure Python but depends on platform-specific C-functions.
Event Loop Policy is a global per-process object. Controls the Event Loop. Could be changed via API. Single event loop per thread by default.
Event Loop itself is an object too. Can be passed into different contexts. Has 2 stages: running or stopped. Relies on monotonic clock to avoid errors of going back in time (essentially the system's uptime).
Event loop collects coroutines, Tasks and Futures as handles. Assigns callbacks to these handles. Plans execution in a timed heap if possible. And checks for events needed by some handles.
Related links:
https://peps.python.org/pep-3153/ - Asynchronous IO support - precedes PEP 3156
https://peps.python.org/pep-3156/ - Asynchronous IO Support Rebooted: the βasyncioβ Module - the key PEP which inspired this post
https://peps.python.org/pep-0418/#time-monotonic - Add monotonic time, performance counter, and process time functions - monotonic related link
https://peps.python.org/pep-3148/ - futures - execute computations asynchronously
https://peps.python.org/pep-0380/ - Syntax for Delegating to a Subgenerator (yield from)
https://peps.python.org/pep-0492/ - Coroutines with async and await syntax
https://github.com/python/cpython/blob/main/Lib/asyncio/base_events.py - source code containing the actual loop of Event Loop inside the method called "run_forever". Each iteration is processed inside the method "_run_once".
https://github.com/python/cpython/blob/main/Lib/selectors.py - key "select" method which crucial for collecting events
https://www.youtube.com/watch?v=RIVcqT2OGPA - AsyncIO and the Event Loop explained by ArjanCodes
https://www.youtube.com/watch?v=Ww2HBNpu98g - Demystifying AsyncIO: Building Your Own Event Loop in Python β Arthur Pastel