For some documentation purposes, I need to run some lines of python code, and put the output in the docstring of the classes.
The results should look like this:
>>> from sklearn.cluster import KMeans
>>> import numpy as np
>>> X = np.array([[1, 2], [1, 4], [1, 0],
... [4, 2], [4, 4], [4, 0]])
>>> kmeans = KMeans(n_clusters=2, random_state=0).fit(X)
>>> kmeans.labels_
array([0, 0, 0, 1, 1, 1], dtype=int32)
>>> kmeans.predict([[0, 0], [4, 4]])
array([0, 1], dtype=int32)
>>> kmeans.cluster_centers_
array([[ 1., 2.],
[ 4., 2.]])
Now my question is, assuming I have a file with those few lines of code in it, how can I run it with python
so that I get such an output.
Bash has a similar option where you can have the following in a file, let say demo.sh
mkdir /tmp/test1
touch /tmp/test1/1
ls /tmp/test1
And you can run it as bash -x demo.sh
and get the following output:
$ bash -x /tmp/tmp.sh
+ mkdir /tmp/test1
+ touch /tmp/test1/1
+ ls /tmp/test1
1
Is there a way I could do the same with python
?