How to read APIs (Python)(PyBrain)

  • Thread starter K29
  • Start date
In summary, the conversation discusses the difficulty in understanding APIs, particularly in regards to the *args and **kwargs parameters. The provided documentation lacks information about these parameters, leading to confusion and the need to consult other sources, such as forums or the source code. The use of *args and **kwargs is a common practice in the Python language, where a function can take a variable number of positional and keyword arguments.
  • #1
K29
108
0
I am having trouble getting the hang of understanding APIs. After going through tutorials I find myself wanting to see what parameters I can pass to an object. So I look in the api and I see stuff like *args and **kwargs

e.g.
http://pybrain.org/docs/api/structu...pybrain.structure.networks.FeedForwardNetwork

I've looked everywhere to try and find what those *args consist of, but I'm at a loss. Anyone frequently consult API documentation and know what I should be reading?
 
Technology news on Phys.org
  • #2
K29 said:
I am having trouble getting the hang of understanding APIs. After going through tutorials I find myself wanting to see what parameters I can pass to an object. So I look in the api and I see stuff like *args and **kwargs

e.g.
http://pybrain.org/docs/api/structu...pybrain.structure.networks.FeedForwardNetwork

I've looked everywhere to try and find what those *args consist of, but I'm at a loss. Anyone frequently consult API documentation and know what I should be reading?
The docs in the link you provided are pretty poor, IMO, as they don't give any information about the arguments of the functions listed, such as what they represent and their types.

About all that one can infer from this page are that arg represents a pointer to something, and that kwargs is a pointer to a pointer to something. In other words, arg holds the address of something, and kwargs holds an address that is the address of something.
 
  • #3
This looks like something where you will have to find an associated forum and look at the FAQ. And it does not appear to actively maintained. Which is a bad sign. I would start by reading through this page:

http://pybrain.org/docs/modindex.html The documentation appears more sensible there. You can backwards relate the class information down to the API.
 
  • #4
Even worse, the **args argument to class pybrain.structure.networks.Network(name=None, **args) should have been **kwargs.

Sometimes the only way to see how those extra list arguments (*args) and keyword arguments (**kwargs) are used is to RTFC ("Read The Ffing Code").

This is a dirty little secret to python: You can write functions that take a variable number of positional arguments, and an arbitrary dictionary of keyword arguments. That extra kruft is what these *args and **kwargs are receiving. This approach is often used in constructors of derived classes so that stuff not needed by the derived class but is needed by a base class can be easily collected and passed to the base class constructors.Addendum
See the latter half of section 4.7.2 plus sections 4.7.3 and 4.7.4 in the python tutorial. Link: http://docs.python.org/2/tutorial/controlflow.html#keyword-arguments.
 
Last edited:
  • #5
Expanding slightly on what D H says, a function taking the arguments (*args,**kwargs) can take any parameters at all. If you run:
Code:
def f(*args,**kwargs):
    print args
    print kwargs

f(1,2,3,x=1,y="n")
you'll find that args is a list containing all of the "bare" parameters ([1,2,3], in this case), and kwargs is a dict containing all of the keyword parameters keyed by the keywords(["x":1,"y":"n"], in this case). So the docs tell you precisely nothing. You'll have to either look at the code or try to talk to someone who knows what they're talking about.

I note that the index page (http://pybrain.org/docs/index.html) links to a Google Group that might be able to help. It requires you to sign up to see, which I haven't done, so I don't know if it's active at all.
 
  • #6
Thanks for all the help everyone.

I tried the modindex first. But its links into the API. Going up the class tree didnt reveal anything new to me as far as **args go. Well I tried the google group first. It looked somewhat active, but after 12 hours of no response I decided to read the code. To be honest reading the code was simple enough. In fact, there was one place in the API that said something along the lines of "this is not all the methods that are available." Very poor documentation indeed. Fortunately the source is very neat and clearly commented.

Also thanks all for the explanations of **args and **kwargs. I was unaware of that.
 

Related to How to read APIs (Python)(PyBrain)

1. How do I access an API in Python?

To access an API in Python, you will need to use a library or module specifically designed for working with APIs. Some popular options include requests, urllib, and PyCurl. These libraries will handle the communication between your Python code and the API, making it easy to retrieve and work with data.

2. What is an API key and how do I get one?

An API key is a unique identifier that allows you to access an API. Some APIs require an API key to authenticate your requests and track your usage. To get an API key, you will need to visit the developer portal of the API you want to use and follow their instructions for obtaining a key. This process may involve creating an account, providing some personal information, and agreeing to terms of use.

3. How do I read data from an API in Python?

Reading data from an API in Python typically involves sending a request to the API and then parsing the response. The specific steps will depend on the library or module you are using, but generally, you will need to specify the API endpoint, any required parameters or headers, and the type of request (e.g. GET or POST). Once you receive a response, you can use built-in Python functions or libraries like json or pandas to format and manipulate the data as needed.

4. How can I handle errors when working with APIs in Python?

Errors can occur when working with APIs in Python for various reasons, such as incorrect requests, server issues, or rate limiting. To handle errors, you can use try-except blocks in your code to catch and handle specific types of errors. Additionally, most API libraries or modules will have built-in error handling mechanisms that you can utilize.

5. Is PyBrain a good library for working with APIs in Python?

PyBrain is a popular library for machine learning and neural networks in Python, but it is not specifically designed for working with APIs. While it does have some functionality for working with web data, there are other libraries that may be better suited for API usage. It is recommended to research and compare different libraries to determine which one best fits your project's needs.

Similar threads

  • Programming and Computer Science
Replies
3
Views
1K
  • Programming and Computer Science
Replies
10
Views
2K
  • Programming and Computer Science
Replies
11
Views
2K
  • Programming and Computer Science
Replies
3
Views
1K
  • Programming and Computer Science
Replies
4
Views
947
  • MATLAB, Maple, Mathematica, LaTeX
Replies
3
Views
2K
Replies
6
Views
1K
  • Nuclear Engineering
Replies
3
Views
1K
  • Nuclear Engineering
Replies
5
Views
3K
Back
Top