I think you should definitely get rid of the try/except statements. The conversion is either going to work, or it is not, so you really don't need them and they're probably slowing everything down a LOT.
If the database is very large, you might want to consider converting outside of sage, with a command line program, say. Perhaps some small python script would suit better. On May 10, 3:27 pm, tvn <nguyenthanh...@gmail.com> wrote: > I am trying to read in a database containing lines of numbers (in string > format) and convert each of these numbers to Rational. > For example > '1.2 3 4 5/6 7 8.2' > becomes > [6/5, 3, 4, 5/6, 7, 41/5] > > I wrote the below small utility function that takes in a string s and > converts it to rational if possible. The problem is that I feel it's too > slow when having to apply to a large database. Is there a way to speed up > the process ? > > def str2rat(s): > try: > return Rational(s) > except TypeError: > pass > > try: > return Rational(RealNumber(s)) > except TypeError: > print 'W: cannot convert %s to rational' %s > return None > > Thanks -- To post to this group, send email to sage-support@googlegroups.com To unsubscribe from this group, send email to sage-support+unsubscr...@googlegroups.com For more options, visit this group at http://groups.google.com/group/sage-support URL: http://www.sagemath.org