My Python version is 3.5.1 I have a simple code (tests.py): If I run it with command ‘python tests.py’ I will get the results: Why it is happening? And how to fix it. I expect that each tests run will be independent (each test should pass), but it is not as we can see. Answer The array is shared by
Tag: python-3.x
Replace values in one dataframe with values in second dataframe in Python
I have a large dataframe (DF1) that contains a variable containing UK postcode data. Inevitably there are some typos in the data. However, after some work with regular expressions, I have created a second database that contains corrected versions of the postcode data (but only for those rows where the original postcode was incorrect) – DF2. (N.B. the index values
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,
Type hinting / annotation (PEP 484) for numpy.ndarray
Has anyone implemented type hinting for the specific numpy.ndarray class? Right now, I’m using typing.Any, but it would be nice to have something more specific. For instance if the NumPy people added a type alias for their array_like object class. Better yet, implement support at the dtype level, so that other objects would be supported, as well as ufunc. Answer
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
pexpect.expect() in python3 is throwing error as “must be in str , not bytes”
I am migrating my code to python 3.4.3. This code works fine in python 2.4.3. but here it throws error in python 3.4.3. should I use anything different from expect ? Here is my code snippet which gets the error: The error what I get is : Answer pexpect wants to log bytes, not decoded strings. You can just let
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: