How to print all available python standard library methods in the terminal - python

I am trying to figure out how to list out all the standard library functions/methods (still learning the difference, I'm a noob). So I get how to import a module and use dir() and help(). These have been a great help and when I try to rewrite a program I learned to write and I get lost I try to use the dir() and help() to spark my memory.
However, for example, I was re-looking through some code and I had .replace() and I was scouring through my imported modules to find it and then found out it's in the standard library and a sub-part of the str() function. Is there something I can type into dir() that will spit out str()?
How can I print out all top level built in function/methods like I would when I do dir(re)?
I've tried re-wording this question in search engines several ways and I can't find anything and the results get muddied with "python list _____".
Thanks so much!

Is there something I can type into dir() that will spit out str()?
dir() on a value of that type.
dir('hello') # string methods
dir(5) # integer methods
dir([]) # list methods

Ah, I never tried typing just "dir()" that listed I could put in dir(__builtins__) and that is what I was looking for. I'll leave it here in case someone else is a noob too and knows where they can start. Just start with dir() and go down the rabbit hole!

Related

Not able to understand the implementation of the os module in cpython

So I wanted to check out some implementations of standard libraries. I started with the os library with the code being here on github.
I took one method for example os.listdir() and I have absolutely no idea how it is implemented even after looking at the code ( pardon this noob ). I have following questions:
os.__all__ do not list this method but I think it is definitely a method as print(type(os.listdir)) listed <class 'builtin_function_or_method'> and I searched on google to find all the builtin functions which I found on this doc page and this is not one of them.
There is not such exclusive function named listdir defined in the module. In the code, from my limited understanding, the function is taken from globals() and put into a support_fd set. How this method is being called I do not understand.
I think the main problem I have is how that module is designed and I was not able to find any resources online to explain in simpler terms hence I am asking here for pointers.
EDIT: For those who are asking, I tried the following code in onlinegdb
import os
if "listdir" in os.__all__:
print("Yes")
print(os.listdir())
The result is only main.py, it should also print Yes, maybe the platform onlinegdb is the problem but it clearly shows the output of listdir as main.py.
After having discussion in the comments I see now that this is more of a online python version problem and not an issue with python or the module itself.

Parsing Function names, arguments, return value from python file?

Im trying to get all the function names, with their arguments, and return's of a python file, using python script.
Tried to use AST library with FunctionDef class but couldn't able to extract the information I'm looking for.
Thanks.
This answer recommends the inspect module for getting a list of functions; it can be used (but isn't required) for accessing function parameter names. To the best of my knowledge, Python's design doesn't really allow getting this information about a function's return values; so you might have to annotate those yourself, be it with a docstring, custom attribute, type hint, etc.

How to determine which Python standard library module(s) contain a certain method?

Given a method name, how to determine which module(s) in the standard library contain this method?
E.g. If I am told about a method called strip(), but told nothing about how it works or that it is part of str, how would I go and find out which module it belongs to? I obliviously mean using Python itself to find out, not Googling "Python strip" :)
The trouble is, strip is not defined in any module. It is not a part of the standard library at all, but a method on str, which in turn is a built in class. So there isn't really any way of iterating through modules to find it.
You could use modulefinder to determin all the loaded modules then loop though each one and to get a list of methods using inspect.getmembers looping though those to find what you are looking for. I don't thing there is a built-in way to do this.
https://python.readthedocs.org/en/v2.7.2/library/modulefinder.html
https://docs.python.org/2/library/inspect.html

PyDoc on a shared object

Small simple question, but I cannot find the answer!
Is it possible to use PyDoc on a shared object used as a module in python ? Let say that I declare all the help in that *.so and I'm able to use them with help(module or class name). But I'd like to use PyDoc to create an HTML from these. So this way, I won't have to write the doc two times!
Let me know if it's possible, or if there's a small hack :)
Also, in the C files, I'm able to put the description of each function! But when I type help(my_class) I don't have a description of the class and then the list of function. What am I doing wrong ?
So, what I want is when I do this in the C files:
return PyObject_NEW(my_class, &my_class_type);
Later, in Python, after I loaded the module, I can do:
help(my_class)
and see the help, like it would do for help(int)!!
Thanks!
So, I partially solved my problem.
I still can't use pydoc on an *.so file. But now, I can add modules, which is nice. Everything is available here: http://docs.python.org/extending/newtypes.html
But I'm still looking for the pydoc solution!

Python Strongly type lists

I am using eclipse for python and I am facing a problem. I have many classes with many properties and want a list of objects from one of my declared classes. The problem is: When I am accessing any item from the list, the IDE does not know its type because in python we do not declare the variable with type, so there is no auto complete and I have to go to the class to copy the attribute name.
To make idea more clear:
class AutomataBranch(object):
def __init__(selfparams):
self.Name="";
self.nodes=[];
class LanguageAutomata(object):
def __init__(selfparams):
self.cfgAutomata=[];#This has AutomaBranch Type
Now in any method in LanguageAutomata class if I wrote:
cfgAutomata. Then it wont give me the Name attribute
Is there any solution for that?
Python is strongly typed and Python lists are too. Your problem come from the fact that Python is dynamically typed. Therefor a var can contain any type, and therefor no IDE can guess what is the type of your parameter, nor give you code completion for the methods.
This is how it is, there is no clean workaround. If it's a problem, then maybe dynamics language is not you predilection tool and you should use something that fit your development style. There are tools for everybody.
8 years later and we actually have a solution in Python 3.6.
PEP484 allows you to annotate your variables primarily for IDEs and linting:
Modifying #Hani's answer:
x : AutomataBranch = self.cfgAutomata[i]
This is now picked up by any good IDE to highlight errors and allow autocomplete.
I think you mean to say "statically typed" instead of "strongly typed." Python is strongly typed. You just don't know what that type is at compile time.
With that said, you really need to abandon the idea that you're going to find any IDEs that work as well for Python as they do for Java or C#. Python's dynamic typing makes this difficult. In fact, I tend to find that powerful IDEs are more of a burden than a help.
I think I found a good managable solution. Actually it is trivial but may help (I used it now).
When I want to access the list then I assign the object which I want to access to a variable ex:
x = AutomataBranch()
x = self.cfgAutomata[i]
The first line is used only to make the IDE knows that x is from AutomatBranch type. After that when I press x then all methods and properties are visualized.
I think it is some how good.

Categories