Two things I was happy about with Python 3.9

This article is the 23rd day of MDC Advent Calendar 2020. December 23, 2020 is a weekday. e? Did you stop talking about prime numbers? Yes, I wonder if I'm getting tired of it (by the way, the prime number is 20201227 most recently). Did you know about weekdays? Oh, that's rude.

Introduction

This is the n selection series that we will send you again. Now about Python. Moreover, it has been reduced from 5 to 2. What is "selection" anymore? When I recovered my Mac the other day, if I updated the Python version to the latest version, the tension increased a little, so I will write it with that glue. I was happy from my point of view, so I think that some people may feel joy in something different, but I would like it to be individual differences.

PEP616: Elimination of prefixes and suffixes

A method has been added that excludes the first or last string of the string. In the past, using regular expressions would have been a quick way to do it, but you can easily do it with removeprefix () or removesuffix ().

Previous method

>>> sample = 'test_sample_string'
>>> sample
'test_sample_string'
>>> import re
>>> re.sub('^' + re.escape('test_'), '', sample)
'sample_string'
>>> re.sub(re.escape('_string') + '$', '', sample)
'test_sample'

Yes, it's just like a regular expression.

The method added this time

>>> sample = 'test_sample_string'
>>> sample
'test_sample_string'
>>> sample.removeprefix('test_')
'sample_string'
>>> sample.removeprefix('test_sample_')
'string'
#Needless to say, if you meet the condition of the first character string, you can go anywhere.
>>> sample.removeprefix('sample_')
'test_sample_string'
#Must match as the first string
>>> sample.removesuffix('_string')
'test_sample'
>>> sample.removesuffix('test')
'test_sample_string'
#Similar to prefix

Other

By the way, I also tried Japanese. You did it.

>>> sample2 = 'Red pajamas blue pajamas yellow pajamas'
>>> sample2
'Red pajamas blue pajamas yellow pajamas'
>>> sample2.removeprefix('Red pajamas')
'Blue pajamas yellow pajamas'
>>> sample2.removesuffix('Yellow pajamas')
'Red pajamas blue pajamas'

PEP584: Combine dictionaries

The only way to combine two dictionaries was to prepare a new dictionary to combine and then do your best with update (), but from this time it seems that it is possible with operators. Specifically, it seems that you can easily do it with dic1 | dic2.

Previous method

>>> airport1 = {'HND': 'Tokyo', 'KIX': 'Osaka', 'NGO': 'Nagoya'}
>>> airport2 = {'NRT': 'Tokyo', 'ITM': 'Osaka', 'CTS': 'Sapporo'}
>>> airport = airport1.copy()
>>> airport
{'HND': 'Tokyo', 'KIX': 'Osaka', 'NGO': 'Nagoya'}
>>> airport.update(airport2)
>>> airport
{'HND': 'Tokyo', 'KIX': 'Osaka', 'NGO': 'Nagoya', 'NRT': 'Tokyo', 'ITM': 'Osaka', 'CTS': 'Sapporo'}

Hmmm, it's really annoying ...

The method added this time

>>> airport1 = {'HND': 'Tokyo', 'KIX': 'Osaka', 'NGO': 'Nagoya'}
>>> airport2 = {'NRT': 'Tokyo', 'ITM': 'Osaka', 'CTS': 'Sapporo'}
>>> airport1 | airport2
{'HND': 'Tokyo', 'KIX': 'Osaka', 'NGO': 'Nagoya', 'NRT': 'Tokyo', 'ITM': 'Osaka', 'CTS': 'Sapporo'}
#Combine two dictionaries
>>> airport = airport1 | airport2
#Of course, I'll put it in a new dictionary
>>> airport
{'HND': 'Tokyo', 'KIX': 'Osaka', 'NGO': 'Nagoya', 'NRT': 'Tokyo', 'ITM': 'Osaka', 'CTS': 'Sapporo'}
>>> airport['NRT']
'Tokyo'

Other

Sober|Of the assignment operator corresponding to|=It also supports.

>>> airport1
{'HND': 'Tokyo', 'KIX': 'Osaka', 'NGO': 'Nagoya'}
>>> airport2
{'NRT': 'Tokyo', 'ITM': 'Osaka', 'CTS': 'Sapporo'}
>>> airport1 |= airport2
>>> airport1
{'HND': 'Tokyo', 'KIX': 'Osaka', 'NGO': 'Nagoya', 'NRT': 'Tokyo', 'ITM': 'Osaka', 'CTS': 'Sapporo'}

It seems that it can be used when turning a for statement.

Other

By the way, when the key is covered, it seems that it is prioritized to specify it later ( dic2 in dic1 | dic2) as it was realized by update.

>>> code1 = {'Tokyo': 'HND', 'Osaka': 'KIX', 'Nagoya': 'NGO'}
>>> code2 = {'Tokyo': 'NRT', 'Osaka': 'ITM', 'CTS': 'Sapporo'}
>>> code1 | code2
{'Tokyo': 'NRT', 'Osaka': 'ITM', 'Nagoya': 'NGO', 'CTS': 'Sapporo'}
>>> code2 | code1
{'Tokyo': 'HND', 'Osaka': 'KIX', 'CTS': 'Sapporo', 'Nagoya': 'NGO'}

bonus

It's a familiar bonus every time (it seems that there are some tsukkomi that are not so familiar).

Upgrade pyenv (anyenv)

git clone git://github.com/yyuu/pyenv-update.git ${PYENV_ROOT}/plugins/pyenv-update

If you put in the plug-in, it was OK ... I didn't know it for a long time ... (I did a git pull every time).

Finally

If you look at the Release Notes, there are many more. How limited my use is ... Then Enjoy Python!

Recommended Posts

Two things I was happy about with Python 3.9
Three things I was addicted to when using Python and MySQL with Docker
3 things I noticed by analyzing twitter followers with python
I was addicted to scraping with Selenium (+ Python) in 2020
I tried fp-growth with python
I tried scraping with Python
I made blackjack with python!
I tried gRPC with Python
I made blackjack with Python.
What I was careful about when implementing Airflow with docker-compose
I was hooked for 2 minutes with the Python debugger pdb
I can't install python3 with pyenv-vertualenv
I tried web scraping with python.
Where I was worried about heroku
I made a fortune with Python.
What was surprising about Python classes
I sent an SMS with Python
[Python] Join two tables with pandas
I liked the tweet with python. ..
I learned about processes in Python
I played with PyQt5 and Python3
I tried running prolog with python 3.8.2.
I tried SMTP communication with Python
What I was addicted to with json.dumps in Python base64 encoding
Homework was a pain, so I do management accounting with Python
Two things I was addicted to building Django + Apache + Nginx on Windows
About launching an instance with an encrypted EBS volume (where I was addicted)
I made a character counter with Python
I was addicted to creating a Python venv environment with VS Code
I drew a heatmap with seaborn [Python]
I wanted to solve ABC160 with Python
I tried sending an email with python.
I tried non-photorealistic rendering with Python + opencv
Use Python from Java with Jython. I was also addicted to it.
I want to analyze logs with Python
[Python] Summarize the rudimentary things about multithreading
I want to play with aws with python
I tried a functional language with Python
I installed and used Numba with Python3.5
What I did with a Python array
I made a Hex map with Python
Happy GUI construction with electron and python
I made a roguelike game with Python
What I was addicted to Python autorun
I wanted to solve ABC172 with Python
I made a simple blackjack with Python
I made a configuration file with Python
#I tried something like Vlookup with Python # 2
I made a neuron simulator with Python
Python practice data analysis Summary of learning that I hit about 10 with 100 knocks
I was able to mock AWS-Batch with python, moto, so I will leave it
A note I was addicted to when running Python with Visual Studio Code
A story that I was addicted to when I made SFTP communication with python
I tried "smoothing" the image with Python + OpenCV
I tried hundreds of millions of SQLite with python
[Python] I introduced Word2Vec and played with it.
I made a weather forecast bot-like with Python.
I want to use MATLAB feval with python
I made a GUI application with Python + PyQt5
Generate two correlated pseudo-random numbers (with Python sample)
I wanted to solve NOMURA Contest 2020 with Python