/py-grep

An exercism.io exercise: a lightweight grep in python.

Primary LanguagePython

Grep

This was an exercism exercise(https://exercism.io/my/solutions/5c9438ad6d344680a6987bd21023ec15).

In the master branch, I took a conventional python approach: A helper class that uses instance variables to make state available to its methods.

In the functional branch, the main action is functions, not a class. State travels from function to function as a parameter. There are a couple of helper dataclasses, but they have no methods besides init, and I use them read-only.

The exercism README contents are below:

###########################################

Search a file for lines matching a regular expression pattern. Return the line number and contents of each matching line.

The Unix grep command can be used to search for lines in one or more files that match a user-provided search query (known as the pattern).

The grep command takes three arguments:

  1. The pattern used to match lines in a file.
  2. Zero or more flags to customize the matching behavior.
  3. One or more files in which to search for matching lines.

Your task is to implement the grep function, which should read the contents of the specified files, find the lines that match the specified pattern and then output those lines as a single string. Note that the lines should be output in the order in which they were found, with the first matching line in the first file being output first.

As an example, suppose there is a file named "input.txt" with the following contents:

hello
world
hello again

If we were to call grep "hello" input.txt, the returned string should be:

hello
hello again

Flags

As said earlier, the grep command should also support the following flags:

  • -n Print the line numbers of each matching line.
  • -l Print only the names of files that contain at least one matching line.
  • -i Match line using a case-insensitive comparison.
  • -v Invert the program -- collect all lines that fail to match the pattern.
  • -x Only match entire lines, instead of lines that contain a match.

If we run grep -n "hello" input.txt, the -n flag will require the matching lines to be prefixed with its line number:

1:hello
3:hello again

And if we run grep -i "HELLO" input.txt, we'll do a case-insensitive match, and the output will be:

hello
hello again

The grep command should support multiple flags at once.

For example, running grep -l -v "hello" file1.txt file2.txt should print the names of files that do not contain the string "hello".

Exception messages

Sometimes it is necessary to raise an exception. When you do this, you should include a meaningful error message to indicate what the source of the error is. This makes your code more readable and helps significantly with debugging. Not every exercise will require you to raise an exception, but for those that do, the tests will only pass if you include a message.

To raise a message with an exception, just write it as an argument to the exception type. For example, instead of raise Exception, you should write:

raise Exception("Meaningful message indicating the source of the error")

Running the tests

To run the tests, run the appropriate command below (why they are different):

  • Python 2.7: py.test grep_test.py
  • Python 3.4+: pytest grep_test.py

Alternatively, you can tell Python to run the pytest module (allowing the same command to be used regardless of Python version): python -m pytest grep_test.py

Common pytest options

  • -v : enable verbose output
  • -x : stop running tests on first failure
  • --ff : run failures from previous test before running other test cases

For other options, see python -m pytest -h

Submitting Exercises

Note that, when trying to submit an exercise, make sure the solution is in the $EXERCISM_WORKSPACE/python/grep directory.

You can find your Exercism workspace by running exercism debug and looking for the line that starts with Workspace.

For more detailed information about running tests, code style and linting, please see Running the Tests.

Source

Conversation with Nate Foster. http://www.cs.cornell.edu/Courses/cs3110/2014sp/hw/0/ps0.pdf

Submitting Incomplete Solutions

It's possible to submit an incomplete solution so you can see how others have completed the exercise.