Ticket #2537: 0001-more-robust-handling-of-fraction-separator-regex-det.patch

File 0001-more-robust-handling-of-fraction-separator-regex-det.patch, 1.9 KB (added by walter, 10 years ago)
  • calculate.py

    From e6077f00cc15dfe721469bb4f9962a193ca72324 Mon Sep 17 00:00:00 2001
    From: Walter Bender <walter@sugarlabs.org>
    Date: Sat, 1 Jan 2011 17:06:45 -0500
    Subject: [PATCH] more robust handling of fraction separator regex detection
    
    ---
     calculate.py |   14 +++++++++++++-
     1 files changed, 13 insertions(+), 1 deletions(-)
    
    diff --git a/calculate.py b/calculate.py
    index bda756c..1b89b88 100644
    a b  
    1 # -*- coding: UTF-8 -*-
     1# -*- coding: utf-8 -*-
    22# calculate.py, sugar calculator, by:
    33#   Reinier Heeres <reinier@heeres.eu>
    44#   Miguel Alvarez <miguel@laptop.org>
    import types 
    2424import os
    2525from gettext import gettext as _
    2626import string
     27import re
    2728import logging
    2829_logger = logging.getLogger('Calculate')
    2930
    from svgimage import SVGImage 
    4849from decimal import Decimal
    4950from rational import Rational
    5051
     52
    5153def findchar(text, chars, ofs=0):
    5254    '''
    5355    Find a character in set <chars> starting from offset ofs.
    class Calculate(ShareableActivity): 
    448450    def process_async(self, eqn):
    449451        """Parse and process an equation asynchronously."""
    450452
     453    def replace_fraction_seperator(self, text):
     454        '''
     455        Replace the fraction separator with '.'
     456        '''
     457        match = text.group(0)
     458        return match.replace(self.ml.fraction_sep, '.')
     459
    451460    def process(self):
    452461        """Parse the equation entered and show the result"""
    453462
    454463        s = unicode(self.text_entry.get_text())
    455464        label = unicode(self.label_entry.get_text())
    456465        _logger.debug('process(): parsing %r, label: %r', s, label)
     466        # Replace the fraction separator with a period before parsing
     467        # A simple replace won't work, e.g. plot(x*x,x=-1,0..1,)
     468        s = re.sub(r'\d+\,\d+', self.replace_fraction_seperator, s)
    457469        try:
    458470            tree = self.parser.parse(s)
    459471            res = self.parser.evaluate(tree)