Python Forum
Code review needed for a basic repl implementation
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Code review needed for a basic repl implementation
#1
Here is a very basic object orientated implementation of a read evaluate print loop AKA REPL. I encluded REShell to demonstrate how one could use Reple.

I would like to know the quality of this code.
  1. Is it easy to read and understand
  2. Is there any bugs I missed.
  3. Would this be better implemented using functions instead of the class driven object orientated approach.

#-*-coding:utf8;-*-
#qpy:3
#qpy:console

import re
import sre_constants


PS1 = '>> '


class Repl(object):
    def __init__(self, ps1=PS1):
        self.ps1 = ps1
        
    def read(self):
        return input(self.ps1)
        
    def evaluate(self):
        raise NotImplementedError('You must define this in a sub-class')
        
    def run(self):
        while 1:
            try:
                self.evaluate()
            except KeyboardInterrupt:
                sys.exit(1)


class REShell(Repl):
    def __init__(self, data, ps1=PS1):
        self.ps1 = ps1
        self.data = data
        
    def evaluate(self):
        try:
            user_input = self.read()
            expression = re.compile(user_input)
            print(*expression.findall(self.data), sep='\n')
            print(user_input)
        except sre_constants.error as error:
            print(error)
            
        
def source_code():
    with open(__file__, 'r') as source:
        return source.read()
        

if __name__ == '__main__':
    data = source_code()
    print(data)
    shell = REShell(data)
    shell.run()
   
Reply
#2
Can't give any feedback as your code has no indentation. when pasting between the code tags, use the sequence "Ctrl + Shift + V" this should preserve the indentation. Use the "Preview Post" button to make sure the code is presented as you expect before hitting the "Post Reply" button.
If it ain't broke, I just haven't gotten to it yet.
OS: Windows 10, openSuse 42.3, freeBSD 11, Raspian "Stretch"
Python 3.6.5, IDE: PyCharm 2018 Community Edition
Reply
#3
Quote:Is it easy to read and understand

This is subjective but in my opinion, it's not very easy to understand.  

First of all, there are no comments.    I don't want to have to read all of your code to learn what it does and how to use it.  I only want to read your code if I want to know how it does what it does or to fix bugs or enhance it.

People frequently make the "but my code is self-documenting" argument. That's great, it should be.  Self-documenting code makes maintaining your code much easier.  But as a user of your code, I don't want to read your code.  I want to read a docstring that says "this is what this function does, what the arguments are, what it returns, and the exceptions it throws".

Second, the variable and method names are vague, ambiguous, or cryptic.  For example ps1 is only meaningful if you know that the bash shell uses PS1 as it's prompt variable.  A better variable name -- in terms of readability -- would have been "prompt".  Also, "data" as a variable name is incredibly ambiguous.

The read method in the Repl class is another example of a vague or ambiguous name.


Quote:Is there any bugs I missed.
I have no idea. Try to break your code.  Give it garbage, give it empty strings, give it a binary file as input, etc...


Quote:Would this be better implemented using functions instead of the class driven object orientated approach.

I don't see anything that screams "this needs to be written with classes" .  If you're going to require the Repl class to be an abstract base class, you should just make it one  https://docs.python.org/3/library/abc.html.

But it doesn't seem to need to be one so why do it?
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Secure deploy the code without providing the implementation mikepy 1 2,264 Aug-05-2022, 12:58 AM
Last Post: Skaperen
  First web scraping project using bs4, review my code OhNoSegFaultAgain 2 2,980 Mar-30-2019, 04:45 PM
Last Post: OhNoSegFaultAgain
  Code Review Help dmcquay 4 3,647 Jan-05-2018, 11:20 PM
Last Post: dmcquay

Forum Jump:

User Panel Messages

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