How is this dealt with in SymPy?

58 views
Skip to first unread message

Jennifer White

unread,
Mar 15, 2015, 9:38:44 AM3/15/15
to sy...@googlegroups.com
Hi,
I'm new to contributing to SymPy and I'm just starting with fixing some simple little TODOs in the code. I'm currently working on some checks that are needed for polynomials (things like checking certain values are integers). Before I went ahead and changed things I wanted to check: what is the preferred action of the code in the event where this condition fails? Should it just print out an error message? Return nothing from the current function? I just wondered what the standard handling of this was in SymPy.
Thanks

Jason Moore

unread,
Mar 15, 2015, 12:44:53 PM3/15/15
to sy...@googlegroups.com
It typically should raise an error. You can do something like `raise ValueError('Wrong input!')`.

--
You received this message because you are subscribed to the Google Groups "sympy" group.
To unsubscribe from this group and stop receiving emails from it, send an email to sympy+un...@googlegroups.com.
To post to this group, send email to sy...@googlegroups.com.
Visit this group at http://groups.google.com/group/sympy.
To view this discussion on the web visit https://groups.google.com/d/msgid/sympy/a617808a-0cf2-4c84-87b9-122fa1bb04c6%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Aaron Meurer

unread,
Mar 15, 2015, 2:07:49 PM3/15/15
to sy...@googlegroups.com
Right, the typical Pythonic way to handle this situation is to raise
an exception. The most common exceptions for SymPy are

- TypeError: The input type does not make sense. This is the error
that likely makes sense for your example.
- ValueError: The input type is OK, but the value doesn't make sense.
The difference between ValueError and TypeError can sometimes be
subtle.
- NotImplementedError: The result should work, but it hasn't been
implemented yet.
- A custom exception: Many modules have custom exception subclasses
that are used in that module, like PolynomialError and MatrixError.
It's best to search the code you're changing to see if there are any
exception subclasses you should be using.

It's also worth keeping in mind with SymPy that there is a second way
of approaching this situation, which is to leave the expression
unevaluated. Or if the input makes no mathematical sense, returning
nan is also sometimes used.

I hope that helps. I can't say specifically what your code should do
without knowing more about what it is.

Aaron Meurer
> https://groups.google.com/d/msgid/sympy/CAP7f1AjJJO0KQdvUkxePRuBMgN2FmOFviy6NRgz8LTyJ-PHqKw%40mail.gmail.com.

Jennifer White

unread,
Mar 15, 2015, 4:49:22 PM3/15/15
to sy...@googlegroups.com
Thanks very much. I imagine in these cases the ValueError would make most sense as, for example, we have to check that something is a natural number so we must check that it is whole and also that it is non-negative.
Reply all
Reply to author
Forward
0 new messages