Ipdb and method documentation - python

I have to analyze methods a foreign API, and how I usually do it it to write a test script, or find an example code, do a
ipdb.set_trace()
Where I want to experiment, and than take a look at currently available variables, objects and their methods. However, when I want to check the documentation the way Ipython offers
object.method?
I get
*** SyntaxError: invalid syntax (<stdin>, line 1)
If I try
help(object.method)
It gives
*** No help on (object.method)
Does that mean that there is no documentation for the selected method, or am I using the wrong way of calling it?

Actually !help(object.method) works, you just need to signify with ! that it's a python command, not ipdb help command. Though convenient question mark doesn't work that way unfortunately.

The help() function is actually a wrapper around pydoc.help() which means that you can do something like:
ipdb> import math
ipdb> import pydoc
ipdb> pydoc.help(math.log)

Related

How to find the help of the .builtin function in python?

In python suppose to find the help of the specific function we use help(function-name) but how to find the same for the .built in function in python such as help(.builtin-function name) but in the command terminal it throws error stating the respective keyword is not found? Thanks in advance and sorry for the language(not comparatively lucid)
I don't get the really correct answer, but here is a link with all the built-in functions sorted in alphabetic order : https://docs.python.org/3/library/functions.html
Enjoy !
What you are trying to do is access the documentation of a method, e.g. help(str.strip). You must provide the name of the method together with the class name (str in this case).
From here:
The built-in function help() invokes the online help system in the
interactive interpreter, which uses pydoc to generate its
documentation as text on the console.
And
For modules, classes, functions and methods, the displayed
documentation is derived from the docstring (i.e. the __doc__
attribute) of the object, and recursively of its documentable members.
So, you can simply access __doc__ attribute of the functions. For example,
print(help.__doc__)
Define the builtin 'help'.
This is a wrapper around pydoc.help that provides a helpful message
when 'help' is typed at the Python interactive prompt.
Calling help() at the Python prompt starts an interactive help
session. Calling help(thing) prints help for the python object
'thing'.
This works with any built-in functions or methods.

How to get description on Python Method?

I know that to get help for a function, we use help(func) or ?func, but what about a method? simply using help(method) or ?method won't do anything.
I tried using a preloaded object name before a method, it worked. But are there any other way?
I found this: https://www.programiz.com/python-programming/methods/built-in/help
Try these on Python shell.
help('random thing')
help('print')
help('def')
from math import *
help('math.pow')
Each one will show different results - but the last couple should help you understand how it works for what you are asking.
This format is inappropriate ?method and will produce an error. If you're having trouble understanding some Python object then help() will do.
help(func)
help(method)
help(Class.method)

Checking arguments of a function in ipython

I'm using the jupyter ipython notebook by anaconda.
IS there a quick way of looking at the arguments of a function like we do in RStudio? For e.g. ?merge displays documentation for merge in the lower right window of RStudio.
I was specifically looking for arguments of matplotlib.figure() which I found here but this is time consuming: http://matplotlib.org/api/pyplot_api.html#matplotlib.pyplot.figure
Found this post related to the question: Getting list of parameter names inside python function
but not sure if it is the same question.
You can try help(matplotlib.figure) after importing matplotlib
Type matplotlib.figure? at the command prompt, and it'll give you the signature and documentation:
In [1]: import matplotlib
In [2]: matplotlib.figure?
Type: module
String form: <module 'matplotlib.figure' from '~/venv/lib/python2.7/site-packages/matplotlib/figure.pyc'>
File: ~/venv/lib/python2.7/site-packages/matplotlib/figure.py
Docstring:
The figure module provides the top-level
:class:`~matplotlib.artist.Artist`, the :class:`Figure`, which
contains all the plot elements. The following classes are defined
:class:`SubplotParams`
control the default spacing of the subplots
:class:`Figure`
top level container for all plot elements
From the IPython introduction:
Exploring your objects
Typing object_name? will print all sorts of details about any object, including docstrings, function definition lines (for call arguments) and constructor details for classes. To get specific information on an object, you can use the magic commands %pdoc, %pdef, %psource and %pfile
You can also use the standard Python help() function; the output is a little more verbose and not coloured, like the ipython object? command however.

Module Instantiation in myhdl

I'm currently looking into myHdl to see if it's worth using or not. However, I've come across a hiccup regarding the instantiation of modules. I've got two files, one that's a module and one that's the testbench. Inside the testbench, I've instantiated the module following the example they have on the website:
http://www.myhdl.org/examples/flipflops.html
The instantiation specifically is this line: dff_inst = dff(q, d, clk)
However, I get an error when I try to run the testbench:
Exception TypeError: 'isinstance() arg 2 must be a class, type, or tuple of classes and types' in <generator object _LabelGenerator at 0x7f6070b2ea50> ignored
I assume this has something to do with the fact that I have two separate files, so my guess is that python isn't finding the dff module(since it's in a separate file). I tried adding in an import dff line, but that simply gave me a 'module' object is not callable type error, which makes sense.
Looking in the documentation, they don't have a full .py file, so I'm not sure how they're linking these testbenches with the module. They specifically mention a hierarchy system and being able to instantiate other modules, but I can't seem to get it to work.
From what I understand from documentation, it looks like they're just writing the testbench and the module in the same file. However, to my understanding, it looks like they imply you can import modules, but I can't figure out how that's done. Is there just some simple thing I'm overlooking?
After experimenting a bit, it seems like I just need to use the following command: from dff import dff,
which makes a lot of sense.

Ruby equivalent to Python's help()?

When working in interactive Python, I tend to rely on the built-in help() function to tell me what something expects and/or returns, and print out any documentation that might help me. Is there a Ruby equivalent to this function?
I'm looking for something I could use in irb. For example, in interactive Python I could type:
>>> help(1)
which would then print
Help on int object:
class int(object) | int(x[, base])
-> integer | |
Convert a string or number to an integer, if possible. A ...
It's now late 2014 and here's the two ways to get the Python help() *similarity, as long as you have the Ruby Docs installed:
From inside irb, You can call the help method with a string describing what you're looking for.
Example 1: help 'Array' for the Array class
Example 2: help 'Array#each' for the Array class each method.
From the command line, outside of irb, you can use the ri program:
Example 1: ri 'Array' for the Array class
Example 2: ri 'Array#each' for the Array class each method.
* Not quite as good as Python's, but still better than nothing
It's definitely a poor cousin to iPython's help, and one of the main features I miss after moving to Ruby, but you can also use ri from within irb. I'd recommend the wirble gem as an easy way to set this up.
Try using ri from the command line.
It takes a class name, method, or module as an argument, and gives you appropriate documentation. Many popular gems come with this form of documentation, as well, so it should typically work even beyond the scope of core Ruby modules.
There's supposed to be irb_help. But like mentioned in that post, it's broken in my ruby install as well.
For quick shell access to ruby documentation, just type ri followed by the method you're wanting to learn more about (from your shell).
For example:
ri puts
This must be fired up in your shell, not your irb (interactive ruby environment)
If you're in your irb environment, then another way, is to simply type help followed by the method you want to learn more about as follows:
help puts
However, this assumes that you have configured your Ruby environment correctly for that (help) to work properly within irb. I usually just have another shell open, and just use the ri directly for quick access when I'm in doubt about a certain method or arguments to a method.

Categories