I just upgraded my Windows machines to Python 3.10.1. I’ve shied away from 3.X.0
releases ever since one of them broke something on Windows — I don’t recall the version, or the reason, and I’d assume release testing has improved so that it’s unlikely to recur, but I suppose I’ve become superstitious in my age.
Upgrading Virtualenv
One of the annoyances of upgrading is replacing virtual environments and getting the appropriate editors/IDEs familiar with the new state of affairs. My prior approach was to rename the .venv
folder to something like .venv39
(backup before deleting), build a new .venv
with the new Python version, and then (after installing packages and testing) delete the previous (.venv39
). Apparently this is wasted effort. Instead, just:
C:\Users\[USER]\AppData\Local\Programs\Python310\python.exe -m venv --upgrade .venv
Done, problem solved. Not sure if there are edge cases (package incompatibilities?), but it seems to be working just fine.
Pattern Matching
I also found a first use case for pattern matching. This will be internal code, so can quite freely try out the new features (some clients are getting rather close to EOL with their 3.6).
To simplify the use case, let’s suppose that I wanted to run a series of named capturing regular expressions on some text documents. The item to extract may look like this: 1mm
,15mm
, and 10.5 mm
; or like these: 1 to 2mm
and 10-15mm
. While there are many ways to handle this, this allows for using pattern matching on the re.Match’s groupdict.
pat1 = re.compile(r'(?P<measure>\d+)\s*?mm', re.I) pat2 = re.compile(r'(?P<measure1>\d+)\s*?(?:mm)?\s*?(?:to|-)\s*?(?P<measure2>\d+)\s*?mm', re.I) pattern_list = [pat1, pat2] for pat in pattern_list: for m in pat.finditer(text): d = m.groupdict() match list(d.keys()): case ['measure']: print(d['measure']) ... case ['measure1', 'measure2']: print(d['measure1'], d['measure2']) ... case other: raise ValueError(f'Unexpected case: {other}')
Note that this particular case will end up capturing both cases (e.g., if the text were 1mm to 2mm
, three total matches would be output). I am still building this particular application, so I am interested in how this case works compared to alternatives.