Given a string s representing a valid expression, implement a basic calculator to evaluate it, and return the result of the evaluation. s consists of digits, '+', '-', '(', ')', and ' '.
def calculate(self, s: str) -> int:
curnum = 0
sign = 1
cursigns = []
curproduct = 1
res = 0
s = '(' + s + ')'
for i, c in enumerate(s):
if c.isdecimal():
curnum = curnum * 10 + int(c)
if i + 1 == len(s) - 1 or not s[i + 1].isdecimal():
res += curnum * curproduct * sign
curnum = 0
elif c == '-':
sign = -1
elif c == '+':
sign = 1
elif c == ')':
curproduct *= cursigns.pop()
elif c == '(':
cursigns.append(sign)
curproduct *= cursigns[-1]
sign = 1
return res