I can't seem to find a good explanation of how to use Python modules. Take for example the urllib module. It has commands such as
req = urllib.request.Request().
How would I find out what specific commands, like this one, are in certain Python modules?
For all the examples I've seen of people using specific Python modules, they just know what to type, and how to use them.
Any suggestions will be much appreciated.
My flow chart looks something like this:
Reading the published documentation (or use help(moduleName) which gives you the same information without an internet connection in a harder to read format). This can be overly verbose if you're only looking for one tidbit of information, in which case I move on to...
Finding tutorials or similar stack overflow posts using specific keywords in your favorite search engine. This is generally the approach you will use 99% of the time.
Just recursively poking around with dir() and __doc__ if you think the answer for what you're looking for is going to be relatively obvious (usually if the module has relatively simple functions such as math that are obvious by the name)
Looking at the source of the module if you really want to see how things works.
Related
For example, when i use matplotlib as plt, a possible statement is like below:
plt.plot(x,y,color='blue')
so how can i get what arguments like 'color' i can pass to the 'plot' function, and what is the proper values for that argument?
Especially when i use some modules.
thanks for any answers.
I'm a little disappointed that this post is being downvoted because I think it's a very legitimate question. In particular, I appreciate that you asked not what the answer was but instead how you could find it for yourself in the future.
Exploring Local Python Documentation
Python has a very robust built-in documentation system as well as a very active and supportive community. At any point in time, you can use the help function to examine a particular object that you want more information on - this will pull up the documentation for that object. In your example, you could do something like:
help(plt.plot)
to pull up the documentation for the matplotlib.pyplot.plot function. Outside of a running Python process, you can use the pydoc command line tool to read and explore that same documentation. Something like:
$ pydoc matplotlib.pyplot.plot
Running that in the shell will display the same documentation as the help command example.
Writing Documentation
As a good citizen of the Python ecosystem, you'll naturally want to document your own code. This is done pretty simply by adding a docstring to the top of a function, class, or module. Docstrings are denoted with a triple quotation mark """, seen in the examples below:
"""This module contains some example classes and functions"""
class MyClass(object):
"""MyClass does some things"""
pass
def my_function(a):
"""Calculates the sum"""
return(a)
There are many different documentation styles that people prefer, so what you choose is up to you. Though I would recommend the official docstring standards outlined in PEP 257.
Finding Online Resources
It's also often useful to take advantage of online documentation and resources. The official Python documentation includes all of the builtin documentation for the standard libraries as well as a tutorial for developers who are new to Python!
As it seems that you're relatively new to the ecosystem yourself, here's some more resources that you might find useful:
Learn Python the Hard Way
CodeAcademy has a Python track
StackOverflow, obviously
I'm fairly new to the language and am wondering if it's possible to see the functions that are being used in a certain Python 3 module?
Specifically the ones in the "ipaddress" module, thanks!
Is this what you are looking for?
https://docs.python.org/3/library/ipaddress.html
Generally, this type of information can be found in the languages docs.
As mentioned by John in the comments, implementations can be found here:
http://hg.python.org/cpython/file/3.5/Lib/ipaddress.py
In addition to getting the source code online, if you have a standard Python install it should be already sitting in an easily found location on your hard-drive. In my case it is found as a file in C:\Python34\Lib. Needless to say, if you go this route you need to be careful to not modify (or, even worse, delete) the file.
I recently began working with Web2Py, after a long period of learning Python. As a beginner, I'm used to having modules and knowing their attributes to getting my work done. For example, I can import the time module and look up the time.sleep() function in the existing documentation to see how I can employ it the way I see fit.
Web2Py, however, employs a lot of Python code that, while easy on the eyes and relatively straightforward if you take the time to study it, isn't exactly covered in detail in the manual. For instance, I occasionally see a response.something in the code, and I can't find the details of the response function anywhere; or I'll see a URL pop up in curly brackets which references a file in, say, my static folders that has syntax like {{=URL(r = "request", c = 'static', f = 'image.png')}} with no explanations of the various attributes of the URL call anywhere in the manual. The manual appears to cover only the aspects of configuring the MVCs and doing specific tasks.
What I'm trying to say is: is there a python.org type documentation for all the exceptional Python functions that appear in Web2Py code?
I'll be much obliged. Thanks!
These are covered in chapter 4 of the web2py book
response
URL
Long story short, a piece of code that I'm working with at work has the line:
from System import System
with a later bit of code of:
desc_ = System()
xmlParser = Parser(desc_.getDocument())
# xmlParser.setEntityBase(self.dtdBase)
for featureXMLfile in featureXmlList.split(","):
print featureXMLfile
xmlParser.parse(featureXMLfile)
feat = desc_.get(featureName)
return feat
Parser is an XML parser in Java (it's included in a different import), but I don't get what the desc_ bit is doing. I mean obviously, it somehow holds the feature that we're trying to pull out, but I don't entirely see where. Is System a standard library in Python or Java, or am I looking at something custom?
Unfortunately, everyone else in my group is out for Christmas Eve vacation, so I can't ask them directly. Thank you for your help. I'm still not horribly familiar with Python.
This isn't from the standard library, so you'll need to check your system (Python has plenty of introspection to help you with that).
You can tell as Python modules in the standard library use lowercase names as per PEP-8, or by searching the library reference.
Note as well that Python has it's own XML parsing tools that will be much nicer to work with in Python than Java's.
Edit: As you have noted in the comments you are using Jython, it seems likely this is Java's System package.
millimoose indicated the correct answer in his comment, but neglected to submit it as an answer, so I'm posting to indicate the correct answer. It was indeed a custom module built by my company. I was able to determine this by typing import System; print(System) into the interpreter.
i am learning python.. i want to do certain kind of scripting in python.. like,
i want to communicate 'wmic' commands through dos promt.. store the result a file..
access some sqlite database, take the data it has and compare with the result i stored..
now, what i dont get is that, how should i proceed? is there any specific frameworks or modules/libraries? like, win32api / com or what else?
pls guide me what things i should follow/learn to accomplish what i intend to do..
thanks
for your project look here
http://tgolden.sc.sabren.com/python/wmi/index.html
http://docs.python.org/library/sqlite3.html
here is list of generic python modules
http://docs.python.org/modindex.html
http://docs.python.org/ - here you can find all information you need with examples.
One of the particularly attractive features of Python is the "batteries included" philosophy: The standard library is huge, and extremely well thought out in 90% of the modules I've ever used. Conversely, this means that a good approach is to learn it first before branching out and installing third-party libraries that may be much less well supported and, ultimately, have no advantage.
In practice, this means I'd recommend keeping https://docs.python.org/release/2.6.5/library/index.html close to your heart, and delve into the documentation for the sqlite3 and probably the subprocess modules. You may or may not want win32api later (I've never worked with wmic, so I'm not sure what you'd need), or come back here when you have concrete questions and already explored the standard library offering.