How do I put docstrings on Enums?

For many IDE’s now in 2022, the following will populate intellisense: class MyEnum(Enum): “”” MyEnum purpose and general doc string “”” VALUE = “Value” “”” This is the Value selection. Use this for Values “”” BUILD = “Build” “”” This is the Build selection. Use this for Buildings “”” Example in VSCode:

How to convert defaultdict of defaultdicts [of defaultdicts] to dict of dicts [of dicts]?

You can recurse over the tree, replacing each defaultdict instance with a dict produced by a dict comprehension: def default_to_regular(d): if isinstance(d, defaultdict): d = {k: default_to_regular(v) for k, v in d.items()} return d Demo: >>> from collections import defaultdict >>> factory = lambda: defaultdict(factory) >>> defdict = factory() >>> defdict[‘one’][‘two’][‘three’][‘four’] = 5 >>> defdict … Read more

Efficiently filtering out ‘None’ items in a list comprehension in which a function is called

Add an if into your comprehension like: l = [y for y in (f(x) for x in [1,2,3,4]) if y is not None] By placing a Generator Expression inside the list comprehension you will only need to evaluate the function once. In addition the generator expression is a generator so takes no extra intermediate storage. … Read more

Why does using “from __future__ import print_function” break Python2-style print?

The whole point of from __future__ import print_function is to bring the print function from Python 3 into Python 2.6+. Thus, it must be used like a function here: from __future__ import print_function import sys, os, time for x in range(0,10): print(x, sep=’ ‘, end=”) # No need for sep here, but okay 🙂 time.sleep(1) … Read more

How to sort a pandas dataFrame by two or more columns?

As of the 0.17.0 release, the sort method was deprecated in favor of sort_values. sort was completely removed in the 0.20.0 release. The arguments (and results) remain the same: df.sort_values([‘a’, ‘b’], ascending=[True, False]) You can use the ascending argument of sort: df.sort([‘a’, ‘b’], ascending=[True, False]) For example: In [11]: df1 = pd.DataFrame(np.random.randint(1, 5, (10,2)), columns=[‘a’,’b’]) … Read more

What is the cross-platform method of enumerating serial ports in Python (including virtual ports)?

This is what I’ve been using. It’s a mashup of the methods I posted above. I’d still like to see better solutions, though. # A function that tries to list serial ports on most common platforms def list_serial_ports(): system_name = platform.system() if system_name == “Windows”: # Scan for available ports. available = [] for i … Read more

difference between quit and exit in python

The short answer is: both exit() and quit() are instances of the same Quitter class, the difference is in naming only, that must be added to increase user-friendliness of the interpreter. For more details let’s check out the source: http://hg.python.org/cpython In Lib/site.py (python-2.7) we see the following: def setquit(): “””Define new builtins ‘quit’ and ‘exit’. … Read more

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)