Returning multiple objects from a pytest fixture

Usually in order to avoid tuples and beautify your code, you can join them back together to one unit as a class, which has been done for you, using collections.namedtuple: import collections EventListener = collections.namedtuple(‘EventListener’, ‘event listener’) Now modify your fixture: @pytest.fixture def event_listener(): e = EventListener(EventEmitter(), Listener()) e.event.subscribe({‘event’ : [e.listener.operation]}) return e Now modify … Read more

How to deal with “divide by zero” with pandas dataframes when manipulating columns? [duplicate]

It would probably be more useful to use a dataframe that actually has zero in the denominator (see the last row of column two). one two three four five a 0.469112 -0.282863 -1.509059 bar True b 0.932424 1.224234 7.823421 bar False c -1.135632 1.212112 -0.173215 bar False d 0.232424 2.342112 0.982342 unbar True e 0.119209 … Read more

Getting all superclasses in Python 3

Use the __mro__ attribute: >>> class A: … pass … >>> class B: … pass … >>> class C(A, B): … pass … >>> C.__mro__ (<class ‘__main__.C’>, <class ‘__main__.A’>, <class ‘__main__.B’>, <class ‘object’>) This is a special attribute populated at class instantiation time: class.__mro__ This attribute is a tuple of classes that are considered when … Read more

How to see progress of Dask compute task?

If you’re using the single machine scheduler then do this: from dask.diagnostics import ProgressBar ProgressBar().register() http://dask.pydata.org/en/latest/diagnostics-local.html If you’re using the distributed scheduler then do this: from dask.distributed import progress result = df.id.count.persist() progress(result) Or just use the dashboard http://dask.pydata.org/en/latest/diagnostics-distributed.html

Why is `True is False == False`, False in Python? [duplicate]

Based on python documentation about operator precedence : Note that comparisons, membership tests, and identity tests, all have the same precedence and have a left-to-right chaining feature as described in the Comparisons section. So actually you have a chained statement like following : >>> (True is False) and (False==False) False You can assume that the … Read more

Ansible create a virtualenv using the venv module

I ran into the same issue tonight and found that specifying the full path to the interpreter, including the arguments, worked for me (at least it does in ansible==2.2.2.0): – pip: requirements: /website/requirements.txt virtualenv: /opt/website-venv virtualenv_command: /usr/bin/python3.6 -m venv or – pip: requirements: /opt/project/requirements_prod.txt virtualenv: /opt/.virtualenv/project_env virtualenv_python: python3

Python argparse – Mutually exclusive group with default if no argument is given

You could have each of your actions update the same variable, supplying stdout as the default value for that variable. Consider this program: import argparse parser = argparse.ArgumentParser() group = parser.add_mutually_exclusive_group() group.add_argument( ‘-s’, ‘–stdout’, action=’store_const’, dest=”type”, const=”s”, default=”s”) group.add_argument( ‘-c’, ‘–csv’, action=’store_const’, dest=”type”, const=”c”) group.add_argument( ‘-t’, ‘–txt’, action=’store_const’, dest=”type”, const=”t”) args = parser.parse_args() print args … Read more

Activating conda environment with its full path

Update for conda 4.4 and up: You need to specify the conda environment path to activate. The new conda activate command should not need the full path to an “activate script” any longer, since the command is now “built-in” to conda. So something like: conda activate (fullpath)/env-name-here should work. The command you have specified activates … Read more