Build a dependency graph in python

Assuming your input from above is given as a string in raw: import networkx as nx import re regex = re.compile(r’^([A-Z]+)::Requires\s+=\s([A-Z”]+)$’) G = nx.DiGraph() roots = set() for l in raw.splitlines(): if len(l): target, prereq = regex.match(l).groups() if prereq == ‘””‘: roots.add(target) else: G.add_edge(prereq, target) Now print the tree(s): for s in roots: print s … Read more

Why am I getting an error message in Python ‘cannot import name NoneType’?

There is no longer a NoneType reference in the types modules. You should just check for identity with None directly, i.e. obj is None. An alternative way, if you really need the NoneType, would be to get it using: NoneType = type(None) This is actually the exact same way types.NoneType was previously defined, before it … Read more

Getting information for bins from the histogram function

The return values of plt.hist are: Returns: tuple : (n, bins, patches) or ([n0, n1, …], bins, [patches0, patches1,…]) So all you need to do is capture the return values appropriately. For example: import numpy as np import matplotlib.pyplot as plt # generate some uniformly distributed data x = np.random.rand(1000) # create the histogram (n, … Read more

How to convert pandas dataframe to nested dictionary

I think you were very close. Use groupby and to_dict: df = df.groupby(‘Name’)[[‘Chain’,’Food’,’Healthy’]] .apply(lambda x: x.set_index(‘Chain’).to_dict(orient=”index”)) .to_dict() print (df) {‘George’: {‘KFC’: {‘Healthy’: False, ‘Food’: ‘chicken’}, ‘McDonalds’: {‘Healthy’: False, ‘Food’: ‘burger’}}, ‘John’: {‘McDonalds’: {‘Healthy’: True, ‘Food’: ‘salad’}, ‘Wendys’: {‘Healthy’: False, ‘Food’: ‘burger’}}}

Why do I get no such table error when installing Apache Airflow on Mac?

You need to perform initialization after installation: $ export AIRFLOW_HOME=some/dir $ airflow db init # or `airflow initdb` for the legacy 1.X If AIRFLOW_HOME is unset, ~/airflow/ will be created and used. This is where the config and logs will be stored; if you want to reset the configuration, remove the dir stored in AIRFLOW_HOME … Read more

Why is numpy.array so slow?

Numpy is optimised for large amounts of data. Give it a tiny 3 length array and, unsurprisingly, it performs poorly. Consider a separate test import timeit reps = 100 pythonTest = timeit.Timer(‘a = [0.] * 1000000’) numpyTest = timeit.Timer(‘a = numpy.zeros(1000000)’, setup=’import numpy’) uninitialised = timeit.Timer(‘a = numpy.empty(1000000)’, setup=’import numpy’) # empty simply allocates the … Read more

Updating openssl in python 2.7

Please refer to http://rkulla.blogspot.kr/2014/03/the-path-to-homebrew.html After upgrading openssl to 1.0.1j by homebrew on MAC, but system python still referred to old version 0.9.8. It turned out the python referred to openssl. So I have installed new python with brewed openssl and finished this issue on Mac, not yet Ubuntu. On Mac OS X version 10.10 and … Read more

File not found.