News for hackers. Stuff that matters.
Re:A Handwritten Math Parser in 100 lines of Python by ogogmad on Thursday September 17, 2020 @08:55AM (#24504000)
It's pretty horrible, but this adds support for parentheses inside the `full_eval` function:

  from itertools import takewhile, dropwhile

  def split_paren(expr):
      left = ''.join(takewhile(lambda c: c != '(', expr))
      right = ''.join(takewhile(lambda c: c != ')', expr[::-1]))[::-1]
      inside = ''.join(dropwhile(lambda c: c != ')', ''.join(dropwhile(lambda c: c != '(', expr))[::-1]))[::-1][1:-1]
      return left, inside, right

  def full_eval(expr):
      if '(' in expr:
          left, inside, right = split_paren(expr)
          return full_eval(left + str(full_eval(inside)) + right)
      else:
          return calc(expr)
Re:A Handwritten Math Parser in 100 lines of Python by wryun on Thursday September 17, 2020 @09:04AM (#24504085)
Neat! Well, sort of. Sometimes, certain things would look nicer not written in Python...