How to process syntax errors

sohcahtoa82 at gmail.com sohcahtoa82 at gmail.com
Wed Oct 12 13:54:14 EDT 2016


On Wednesday, October 12, 2016 at 3:01:26 AM UTC-7, mr.pune... at gmail.com wrote:
> Hi All
> 
> Its really good to see that some discussion happening around this topic. Sorry I was out from my work for sometime so couldn't follow up but I really find it useful. It gives me good opportunity to know python better as I recently started learning python. 
> 
> Ok so I tell you why I need to catch syntax error during compilation and process them. See below example, 
> 
> # I created a platform class with different method in a file and making it as a package. 
> class platform:
>     def connect(self):
>         # connect device 
>     def destroy(self):
>         # destroy device
>     def config(self, command):
>         # Send command to configure device
>     def show(self, command):
>         # check device health
> 
> Now person who wants to write a script using above package can simply use below approach. Which does not make him to have knowledge in python. 
> 
> DUT = platform()
> DUT connect
> DUT config {commands}
> DUT show {commands}
> DUT destroy
> 
> 
> But I know this is not easy to do in python. As they all invalid syntax for python language. So I thought of grabing syntax errors and manipulate them as below internally to call them while code is compiling. 
> 
> DUT = platform()
> DUT.connect()
> DUT.config(commands)
> DUT.show(commands)
> DUT.destroy()
> 
> Hope you understand my need of doing this. If there is another solution to achieve what I am trying to. Please let me know. 
> 
> Thanks, Puneet

You have two possible solutions:

1. Write an interpreter that can read an interpret the commands without parentheses.
2. Tell your users to use the dot "." operator and parentheses.  You don't need to teach them Python, just how to properly format the script.

Personally, I'd go with option #2.

But as others have said, you can't get Python to just "work" without parentheses.  Sure, you COULD attempt to import the script the users have written and catch the SyntaxError exception, but what would you do with it?  At that point, you're going to have to interpret the line of code in the script yourself, and at that point, you should just be writing an interpreter and not attempting to treat the script as Python.



More information about the Python-list mailing list