Skip to content
Advertisement

Tag: python-3.x

Linking python enums

I have some Enum classes that I want to link up recursively. Looking at the line color = ItemColors[Items(value)._name_].value.value, it seems a bit clunky. Am I misunderstanding something about Enum usage? Is there a better way to do it? Answer Building on @brni’s answer, I came up with a smoother solution:

How to convert python logging level name to integer code

As of Python 3.2, logging.Logger.setLevel accepts a string level such as ‘INFO’ instead of the corresponding integer constant. This is very handy except that you can’t compare the levels numerically that way and most other logging methods accept integers only. How do I convert a level string to a numerical level using the functions provided by the logging package? Specifically,

Correct way use type hints / generics to describe arguments of type class (“type”)

This appears to be similar to Type Hinting: Argument Of Type Class, however, the accepted answer there does not actually answer my question, so perhaps the question was expressed incorrectly (?) I have a serialization/deserialization framework that would benefit greatly from [IDE-supported] type hinting. The API looks something like this: The serialization method is fine, but deserialize type hinting is

Why doesn’t contextmanager reraise exception?

I want to replace class’s __enter__/__exit__ functions with single function decorated as contextlib.contextmanager, here’s code: When we got exception inside Test’s __exit__, we pass it to _cm’s __exit__, it works fine, I see exception inside _cm. But then, when I decide to reraise inside _cm, it doesn’t happen: after Test’s __exit__ I don’t see exception (and code works wrong, without

no module named fuzzywuzzy

I installed fuzzywuzzy with pip for python3. When I do pip list I see However when I try to import is I get an error. Does anyone have experience with this problem? Answer Are you sure you ran pip3 and not just pip? The latter only installs Python 2 packages.

Iterating over two lists one after another

I have two lists list1 and list2 of numbers, and I want to iterate over them with the same instructions. Like this: But that feels redundant. I know I can write for item in list1 + list2:, but it has a price of running-time. Is there a way do that without loose time? Answer This can be done with itertools.chain:

Advertisement