Skip to content

Commit

Permalink
moved the function map to excellib, this should make it easier for pe…
Browse files Browse the repository at this point in the history
…ople to add new functions
  • Loading branch information
dgorissen committed Feb 21, 2012
1 parent a438fd1 commit 35619db
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 14 deletions.
20 changes: 7 additions & 13 deletions src/pycel/excelcompiler.py
@@ -1,3 +1,4 @@
from import excellib
from excellib import *
from excelutil import *
from excelwrapper import ExcelComWrapper
Expand Down Expand Up @@ -254,22 +255,13 @@ def __init__(self,*args):
super(FunctionNode,self).__init__(*args)
self.numargs = 0

# map excel functions onto their python equivalents, taking care to avoid name clashes
self.funmap = {
"ln":"xlog",
"min":"xmin",
"min":"xmin",
"max":"xmax",
"sum":"xsum",
"gammaln":"lgamma"
}
# map excel functions onto their python equivalents
self.funmap = excellib.FUNCTION_MAP

def emit(self,ast,context=None):
xfun = self.tvalue.lower()
fun = self.tvalue.lower()
str = ''

fun = self.funmap.get(xfun,xfun)

# Get the arguments
args = self.children(ast)

Expand Down Expand Up @@ -331,7 +323,9 @@ def emit(self,ast,context=None):
elif fun == "or":
str = "any([" + ",".join([n.emit(ast,context=context) for n in args]) + "])"
else:
str = fun + "(" + ",".join([n.emit(ast,context=context) for n in args]) + ")"
# map to the correct name
f = self.funmap.get(fun,fun)
str = f + "(" + ",".join([n.emit(ast,context=context) for n in args]) + ")"

return str

Expand Down
27 changes: 26 additions & 1 deletion src/pycel/excellib.py
Expand Up @@ -5,7 +5,32 @@
import numpy as np
from math import log
from pycel.excelutil import flatten


######################################################################################
# A dictionary that maps excel function names onto python equivalents. You should
# only add an entry to this map if the python name is different to the excel name
# (which it may need to be to prevent conflicts with existing python functions
# with that name, e.g., max).

# So if excel defines a function foobar(), all you have to do is add a function
# called foobar to this module. You only need to add it to the function map,
# if you want to use a different name in the python code.

# Note: some functions (if, pi, atan2, and, or, array, ...) are already taken care of
# in the FunctionNode code, so adding them here will have no effect.
FUNCTION_MAP = {
"ln":"xlog",
"min":"xmin",
"min":"xmin",
"max":"xmax",
"sum":"xsum",
"gammaln":"lgamma"
}

######################################################################################
# List of excel equivalent functions
# TODO: needs unit testing

def value(text):
# make the distinction for naca numbers
if text.find('.') > 0:
Expand Down

0 comments on commit 35619db

Please sign in to comment.