Handling exceptions in the Python Client API

Monday, 18 December 2017 · 5 messages · last reply 21 Dec 2017
JR
Jonathan Robie18 Dec 2017 23:22 #
I am now happily using BaseX from Jupyter Notebooks for Greek data - see these links: http://jonathanrobie.biblicalhumanities.org/blog/2017/12/08/jupyter-tutorial/ http://jonathanrobie.biblicalhumanities.org/assets/greeksyntax-tutorial-proiel.html But I need to know how to handle exceptions, e.g. if the notebook user writes a query with an error in it. And I would also like to return some result if the query returns an empty sequence. Here is the current code: https://github.com/biblicalhumanities/greek-new-testament/blob/master/labnotes/greeksyntax/lowfat.py How can I improve what I am doing? Jonathan
CG
Christian Grün19 Dec 2017 17:02 #
Quoted text · Jonathan Robie · 2 lines
> http://jonathanrobie.biblicalhumanities.org/blog/2017/12/08/jupyter-tutorial/ > http://jonathanrobie.biblicalhumanities.org/assets/greeksyntax-tutorial-proiel.html
Looks good!
Quoted text · Jonathan Robie · 2 lines
> But I need to know how to handle exceptions, e.g. if the notebook user > writes a query with an error in it.
Have you tried to play around with try/except [1]? Well, there are surely other BaseX users out there that have more experience with Python. [1] https://docs.python.org/3/tutorial/errors.html
JR
Jonathan Robie19 Dec 2017 18:42 #
Thanks! The try/except part is the part I understand. The part I don't understand is how to find the exception thrown by BaseX, I want to show syntax errors in a user friendly way like what you see in the GUI so people don't always have to go back to the GUI to figure out what's wrong with their queries. Jonathan
Quoted text · Christian Grün · 18 lines
On Tue, Dec 19, 2017 at 11:02 AM, Christian Grün <christian.gruen at gmail.com> wrote: > > http://jonathanrobie.biblicalhumanities.org/blog/ > 2017/12/08/jupyter-tutorial/ > > http://jonathanrobie.biblicalhumanities.org/assets/ > greeksyntax-tutorial-proiel.html > > Looks good! > > > But I need to know how to handle exceptions, e.g. if the notebook user > > writes a query with an error in it. > > Have you tried to play around with try/except [1]? Well, there are > surely other BaseX users out there that have more experience with > Python. > > [1] https://docs.python.org/3/tutorial/errors.html >
CG
Christian Grünin reply to Jonathan Robie, 21 Dec 2017, 20:3021 Dec 2017 19:54 #
Thanks for the update!
Quoted text · Jonathan Robie · 60 lines
On Thu, Dec 21, 2017 at 7:30 PM, Jonathan Robie <jonathan.robie at gmail.com> wrote: > OK, I did the obvious and examined the exception, which is apparently an > OSError. So this code works well: > > def xquery(self, query): > try: > result = self.session.query(query).execute() > except OSError as err: > print("Error:", err) > else: > return result > > e.g. if I do this call: > > q.xquery("7 + 3 = <num>10</num") > > I get this output: > > Error: Stopped at ., 1/21: > [XPST0003] Expecting '>'. > > Posting here in case someone else finds it helpful. > > Thanks! > > Jonathan > > On Tue, Dec 19, 2017 at 11:41 AM, Jonathan Robie <jonathan.robie at gmail.com> > wrote: >> >> Thanks! The try/except part is the part I understand. The part I don't >> understand is how to find the exception thrown by BaseX, I want to show >> syntax errors in a user friendly way like what you see in the GUI so people >> don't always have to go back to the GUI to figure out what's wrong with >> their queries. >> >> Jonathan >> >> On Tue, Dec 19, 2017 at 11:02 AM, Christian Grün >> <christian.gruen at gmail.com> wrote: >>> >>> > >>> > http://jonathanrobie.biblicalhumanities.org/blog/2017/12/08/jupyter-tutorial/ >>> > >>> > http://jonathanrobie.biblicalhumanities.org/assets/greeksyntax-tutorial-proiel.html >>> >>> Looks good! >>> >>> > But I need to know how to handle exceptions, e.g. if the notebook user >>> > writes a query with an error in it. >>> >>> Have you tried to play around with try/except [1]? Well, there are >>> surely other BaseX users out there that have more experience with >>> Python. >>> >>> [1] https://docs.python.org/3/tutorial/errors.html >> >> >
JR
Jonathan Robiein reply to Jonathan Robie, 19 Dec 2017, 18:4221 Dec 2017 20:30 #
OK, I did the obvious and examined the exception, which is apparently an OSError. So this code works well: def xquery(self, query): try: result = self.session.query(query).execute() except OSError as err: print("Error:", err) else: return result e.g. if I do this call: q.xquery("7 + 3 = <num>10</num") I get this output: Error: Stopped at ., 1/21: [XPST0003] Expecting '>'. Posting here in case someone else finds it helpful. Thanks! Jonathan
Quoted text · Jonathan Robie · 31 lines
On Tue, Dec 19, 2017 at 11:41 AM, Jonathan Robie <jonathan.robie at gmail.com> wrote: > Thanks! The try/except part is the part I understand. The part I don't > understand is how to find the exception thrown by BaseX, I want to show > syntax errors in a user friendly way like what you see in the GUI so people > don't always have to go back to the GUI to figure out what's wrong with > their queries. > > Jonathan > > On Tue, Dec 19, 2017 at 11:02 AM, Christian Grün < > christian.gruen at gmail.com> wrote: > >> > http://jonathanrobie.biblicalhumanities.org/blog/2017/12/08/ >> jupyter-tutorial/ >> > http://jonathanrobie.biblicalhumanities.org/assets/greeksynt >> ax-tutorial-proiel.html >> >> Looks good! >> >> > But I need to know how to handle exceptions, e.g. if the notebook user >> > writes a query with an error in it. >> >> Have you tried to play around with try/except [1]? Well, there are >> surely other BaseX users out there that have more experience with >> Python. >> >> [1] https://docs.python.org/3/tutorial/errors.html >> > >

⚡Generated with XQuery