(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'))
Output:
['[2015-12-29 12:33] [ALPM] installed libgpg-error (1.21-1)\n',
'[2015-12-29 13:20] [ALPM] installed lib32-libgpg-error (1.21-1)\n',
"[2016-01-02 17:08] [PACMAN] Running 'pacman -S urbanterror'\n",
'[2016-01-05 18:54] [ALPM] installed perl-error (0.17024-1)\n',
'[2016-04-30 16:09] [ALPM] upgraded libgpg-error (1.21-1 -> 1.21-2)\n',
'[2016-05-08 13:17] [ALPM] upgraded libgpg-error (1.21-2 -> 1.22-1)\n',
'[2016-05-08 13:17] [ALPM] upgraded lib32-libgpg-error (1.21-1 -> 1.22-1)\n',
'[2016-07-02 13:14] [ALPM] upgraded libgpg-error (1.22-1 -> 1.23-1)\n',
'[2016-07-17 13:46] [ALPM] upgraded libgpg-error (1.23-1 -> 1.24-1)\n',
'[2016-08-16 20:16] [ALPM] upgraded lib32-libgpg-error (1.22-1 -> 1.24-1)\n',
'[2016-11-27 22:05] [ALPM] upgraded libgpg-error (1.24-1 -> 1.25-1)\n',
'[2016-12-29 22:47] [ALPM] upgraded libgpg-error (1.25-1 -> 1.26-1)\n',
'[2017-03-04 08:27] [ALPM] upgraded libgpg-error (1.26-1 -> 1.27-1)\n',
'[2017-06-14 22:08] [ALPM] upgraded perl-error (0.17024-1 -> 0.17024-2)\n',
'[2017-06-23 15:44] [ALPM] upgraded lib32-libgpg-error (1.24-1 -> 1.27-1)\n',
'[2017-06-23 15:45] [ALPM] installed haskell-errors (2.1.3-7)\n',
'[2017-06-23 15:45] [ALPM-SCRIPTLET] errors-2.1.3: dependency "safe-0.3.15-2Yd45VWFfdcKqKyispOifY" doesn\'t exist (ignoring)\n',
'[2017-06-23 15:45] [ALPM-SCRIPTLET] errors-2.1.3: dependency "transformers-compat-0.5.1.4-IuFogs8HAVUJBWVNMhtssu" doesn\'t exist (ignoring)\n',
'[2017-06-23 15:45] [ALPM-SCRIPTLET] errors-2.1.3: dependency "unexceptionalio-0.3.0-2pCLthhElB6B0sx66UGiue" doesn\'t exist (ignoring)\n']
import os
list(os | grep('spa'))
Output:
['_fspath',
'_spawnvef',
'fspath',
'spawnl',
'spawnle',
'spawnlp',
'spawnlpe',
'spawnv',
'spawnve',
'spawnvp',
'spawnvpe']