Quick Programming Tip: The Silver Searcher

The Silver Searcher is a code searching tool, it searches your entire code base in a very fast and efficient manner and it supports regex. I often use it when my IDE’s search capabilities are slow or can’t find what I’m looking for. You don’t have to use it exclusively, you can combine it with your IDE’s search capabilities to do your job faster, and most of the time it is also much easier to search using ag.

For example, In Pycharm, I can open up the integrated terminal (ALT+F12) and type ag my-query to get the list of files and lines which match my query, this is much faster than trying to search via the IDE.

Thanks for reading!

Python Fire – Turning any Python script into a command line tool

It’s been a while since I wrote a post and I’m also going to make this one a short one.

Python Fire is a pretty cool Python library from Google that lets you turn any Python script into a command line tool, this means that you don’t have to write any logic for handling command line parameters and options, but, if you script has sophisticated use cases then you’ll be probably be better by using argparse from the standard library.

You can install Python fire from pip by running these commands:

pip install ipython
pip install fire

Then, to turn your script into a command line tool, all you need to do is import fire and and call the Fire method by passing it a class. Fire parses the class and generates logic for handling command line arguments for each method the class has. For example:

class LzwCommandLineAdapter:
"""
This will be used with Google Fire for easy usage,
so we can avoid argparse/optparse.
"""

@staticmethod
def encode(filename, out="encoded.txt"):
"""
Encodes the text from the give filename.
"""
pass

@staticmethod
def decode(filename, out="decoded.txt"):
"""
Decodes the text from the given filename.
"""
pass

if __name__ == "__main__":
import fire
fire.Fire(LzwCommandLineAdapter)

When we run the script, we get the following result:

➜  Desktop python lzw.py
Type: LzwCommandLineAdapter
File: ~/Desktop/lzw.py
Docstring: This will be used with Google Fire for easy usage,
so we can avoid argparse/optparse.
Usage: lzw.py
lzw.py decode
lzw.py encode
➜ Desktop python lzw.py decode
Fire trace:
Initial component
Instantiated class "LzwCommandLineAdapter" (lzw.py:248)
Accessed property "decode" (lzw.py:264)
('The function received no value for the required argument:', 'filename')
Type: function
String form:
File: ~/Desktop/lzw.py
Line: 264
Docstring: Decodes the text from the given filename.
Usage: lzw.py decode FILENAME [OUT]
lzw.py decode --filename FILENAME [--out OUT]

This is a pretty nice solution when you’re writing lots of Python scripts to experiment with things. Thank you for reading!

Further reading