top of page
Search
Writer's pictureJack Orenstein

Building new commands

Updated: Oct 28, 2023

grep is very useful, and you can call it from marcel, e.g.

grep "def render_compact" *.py

Running this in marcel's marcel/object directory yields:

error.py:    def render_compact(self):
file.py:    def render_compact(self):
historyrecord.py:    def render_compact(self):
process.py:    def render_compact(self):
renderable.py:    def render_compact(self):

This output is not structured, so it is difficult to process further. It would be nice if there were a marcel version of grep that produced structured output, e.g.

(error.py, '        return self.render_compact()')
(error.py, '    def render_compact(self):')
(error.py, '        out = self.render_compact()')
(file.py, '    def render_compact(self):')
(historyrecord.py, '    def render_compact(self):')
(process.py, '    def render_compact(self):')
(renderable.py, '        return self.render_compact()')
(renderable.py, '    def render_compact(self):')

This output consists of (File object, string from file) tuples.


A simple version of grep can be built in marcel itself, using parameterized pipelines:

grem = (| pattern, *files: read --label (files) | \
                          select (file, line: pattern in line) |)

This new (pseudo-) operator is named grem. It has two parameters: pattern, a string to search for; and files, one or more filenames (or globs). The read operator reads the contents of each file, and the --label flag includes a label with each line, the File containing that line. The select operator is used to keep only those (file, line) pairs in which the line contains the given pattern.


So now you can write grem "def set_env" *.py and get the structured result shown above.


If you want grem to be available in all marcel sessions, you can define it in your config file (~/.marcel.py), assigning it to the RUN_ON_STARTUP variable, e.g.

RUN_ON_STARTUP = '''
grem = (| pattern, *files: read -l (files) | select (f, l: pattern in l) |)
'''

(You can add more marcel commands to RUN_ON_STARTUP.)




78 views0 comments

Recent Posts

See All

Marcel in more places

Thanks to two different pilot errors on my part, I have broken the packaging system of two OSes that package marcel. I would have had no...

Marcel and Jupyter

For years, I kept hearing about iPython, as a vastly improved Python REPL. I played with it a little bit, and yes, I saw that. I found...

Comments


bottom of page