Get information about a function in python, looking at source code
the following code comes from开发者_如何学编程 the matplotlib gallery:
#!/usr/bin/env python
from pylab import *
x = array([10, 8, 13, 9, 11, 14, 6, 4, 12, 7, 5])
y = array([8.04, 6.95, 7.58, 8.81, 8.33, 9.96, 7.24, 4.26, 10.84, 4.82, 5.68])
I am new to python, and would like to change the content of x
and y
from an input file. I have two short questions:
- I could guess what
array
means, but once I see it on the code, how can I know to which library it belongs and more information about it? Should I use some kind of python debug commands? - How do I insert the content of my
input file into
x
?
Thanks
As to your 1. that's due to bad habits of the person giving you that program. It should have been:
#!/usr/bin/env python
import pylab
x = pylab.array([10, 8, 13, 9, 11, 14, 6, 4, 12, 7, 5])
y = pylab.array([8.04, 6.95, 7.58, 8.81, 8.33, 9.96, 7.24, 4.26, 10.84, 4.82, 5.68])
help(pylab.array)
or
#!/usr/bin/env python
from pylab import array
x = array([10, 8, 13, 9, 11, 14, 6, 4, 12, 7, 5])
y = array([8.04, 6.95, 7.58, 8.81, 8.33, 9.96, 7.24, 4.26, 10.84, 4.82, 5.68])
help(array)
Use the help(something)
. It helps :)
Using explicit imports is really handy if programs become a bit more complex. The only case I know where from package import *
is nice is when playing at the python prompt, trying stuff out.
As to the file, can you sketch how the file looks like? And I believe numpy has an array load function somewhere, look at the cookbook.
(1) Fire up the python console and use the dir() function on whatever object/class/variable you want to know more about. It'll print out all methods and properties of the object.
>> import pylab
>> x = pylab.array([10, 8, 13, 9, 11, 14, 6, 4, 12, 7, 5])
>> dir(x)
(2) look up File Objects in the python documentation
Use the
help
function to find out what an object can do.import pylab help(pylab.array)
"What library it belongs to" is a sort of funny question. You can easily know what module you are using it from if you refactor your code not to use
import *
. (Never ever ever useimport *
.) That does not mean that's it's original home—pylab
is just a place where a bunch of stuff is pooled for convenience.pylab
gets it frommatplotlib.pylab
which gets it fromnumpy
which gets it fromnumpy.core.multiarray
(which is a C extension module). You can see its original home by looking atpylab.array.__module__
when it's important, which is not often.- Personally, I think you're better of not using
pylab
at all, just getting stuff fromnumpy
,scipy
, andmatplotlib
as you need it to keep things organized. To quote The Zen of Python: Namespaces are one honking great idea -- let's do more of those!
- Personally, I think you're better of not using
How to build an numpy array from a file depends on the format of the file.
numpy.fromfile
/pylab.fromfile
can import a binary file storing the array's data. (This is the same format used bynumpy.tofile
—big shock there.)
array
appears to be a function/class in pylab
. You could do help(array)
to find out more - this relies on the presence of a proper docstring for array
.
from module import *
is a problematic idiom in several ways precisely for the reason that it makes it hard to find out where identifiers come from.
import pylab
x = pylab.array(...)
might be a better way.
Replying to question #2, if your file looks like this:
10 8.04
8 6.95
13 7.58
9 8.81
11 8.33
14 9.96
6 7.24
4 4.26
12 10.84
7 4.82
5 5.68
Then I would do like this:
x_list = list()
y_list = list()
fp = open('coords.txt', 'r')
for line in fp:
line = line.strip() # Remove trailing whitespace
if not line: continue # Skip empty lines
a,b = line.split() # Split line by whitespace, storing coords in a & b
x_list.append(a) # Add a to x_list
y_list.append(b) # Add b to y_list
fp.close()
x = array(x_list)
y = array(y_list)
精彩评论