Python Forum
grep in python interpreter
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
grep in python interpreter
#1
I opened GNOME terminal, ran the following commands

>>> python
>>> import os
>>> dir(os)

It listed me many functioned present in this module, but they were all horizontally aligned and so were difficult to parse through at one look, so , I wanted to grep a particular function supported here named "system", so I did

>>> dir(os) | grep system

It gave me an error when I did this. Also I am not able to run shell commands in interpreter like I could have had used the "sed" tool to organize all functions presented to me by dir(os) aligned vertically one by one instead of horizontally all toghether.

How can I grep to find out if that particular function exists and also how can I show all the functions one by one, each function presented on a single line?
Reply
#2
Once you type 'python', you are in the Python interpreter, not the shell. So you can't use shell commands. If you want to search things, you need to use Python search tools. Note that dir(os) returns a list:

Output:
>>> os_names = dir(os) >>> 'system' in os_names True >>> os_names.index('system') 309 >>> [name for name in os_names if 'sys' in name] ['sys', 'sysconf', 'sysconf_names', 'system']
Craig "Ichabod" O'Brien - xenomind.com
I wish you happiness.
Recommended Tutorials: BBCode, functions, classes, text adventures
Reply
#3
Thanks and also hasattr(os,'system') solved my problem about finding functions but one more problem still persists, how do I list all the funtions in os vertically one by one, not horizontally?
Reply
#4
You could do a loop:

for name in dir(os):
    print(name)
There is also the pprint (pretty print) module:

from pprint import pprint
pprint(dir(os))
Craig "Ichabod" O'Brien - xenomind.com
I wish you happiness.
Recommended Tutorials: BBCode, functions, classes, text adventures
Reply
#5
pip install pdir2 link.
Give a nice overview of a package.
Using a better REPL that original(not good),
you get a lot more info automatic like overview all function/method,all doc string ect.
Ipython -- Ptpython(what i use) -- bpython
In browser Jupyter NoteBook.

Her how it with in cmder and ptpython.
[Image: XoPvrI.jpg]
Reply
#6
never knew about pdir -- very handy!
Reply
#7
(Jul-08-2017, 01:44 PM)rahul1913 Wrote: >>> dir(os) | grep system

Interesting idea. I had this idea over and over with generators. Piping generators together like shell commands in a shell.

The funny thing is, that you can make it possible in Python.
This example includes: custom Exception, raising exceptions, defining behavior with __ror__, using a dict like a switch statement, generator, context manager
import types


class GrepError(Exception):
   pass


class Grep:
    def __init__(self, search_string):
        self.search_string = search_string
   
    def __ror__(self, other):
        action = {
            types.ModuleType: dir,
            list: iter,
            tuple: iter,
            dict: iter,
            set: iter,
            str: self.__file,
            }
        function = action.get(type(other))
        if function is None:
            raise GrepError('{} is not supported'.format(type(other)))
        iterable = function(other)
        for entry in iterable:
            if self.search_string in entry:
                yield entry

    def __file(self, path):
        with open(path) as fd:
            for line in fd:
                yield line
A stripped down solution which supports only ModuleType and without error checking, just for better understanding what happens.
class Grep:
    def __init__(self, search_string):
        self.search_string = search_string
  
    def __ror__(self, other):
        for entry in dir(other):
            if self.search_string in entry:
                yield entry
list('/var/log/pacman.log' | grep('error'))
import os
list(os | grep('spa'))
Output:
['_fspath', '_spawnvef', 'fspath', 'spawnl', 'spawnle', 'spawnlp', 'spawnlpe', 'spawnv', 'spawnve', 'spawnvp', 'spawnvpe']
Almost dead, but too lazy to die: https://sourceserver.info
All humans together. We don't need politicians!
Reply


Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020