1.12.42
[gnumeric.git] / plugins / py-func / py_func.py
blob21002aff4fe8b1691be9fe75d17a244a63daa377
1 from Gnumeric import *
2 import string
4 # Here is a function with variable number of arguments
5 def func_printf(format, *args):
6 '@FUNCTION=PY_PRINTF\n'\
7 '@SYNTAX=PY_PRINTF (format,...)\n'\
8 '@DESCRIPTION='\
9 '\n'\
10 '@EXAMPLES=\n'\
11 'PY_PRINTF("Test: %.2f",12) equals "Test: 12.00")'\
12 '\n'\
13 '@SEEALSO='
15 try:
16 val = format % args
17 except TypeError:
18 raise GnumericError, GnumericErrorVALUE
19 else:
20 return val
22 # Here is a function with one argument
23 def func_capwords(str):
24 '@FUNCTION=PY_CAPWORDS\n'\
25 '@SYNTAX=PY_CAPWORDS (sentence)\n'\
26 '@DESCRIPTION='\
27 '\n'\
28 '@EXAMPLES=\n'\
29 'PY_CAPWORDS("Hello world") equals "Hello World")'\
30 '\n'\
31 '@SEEALSO='
33 return string.capwords(str)
35 # Here is a function which calls a spreadsheet function
36 def func_bitand(num1, num2):
37 '@GNM_FUNC_HELP_NAME@BITAND:bitwise AND of its two arguments.\n'\
38 '@GNM_FUNC_HELP_ARG@number1:first value\n'\
39 '@GNM_FUNC_HELP_ARG@number2:second value\n'\
40 '@GNM_FUNC_HELP_EXAMPLES@=PY_BITAND(12, 6)\n'\
41 '@GNM_FUNC_HELP_SEEALSO@BITAND'
42 gnm_bitand=functions['bitand']
43 return gnm_bitand(num1, num2)
45 test_functions = {
46 # Here we tell Gnumeric that the Python function 'func_printf'
47 # provides the spreadsheet function 'py_printf', and that
48 # 'py_printf' may be called with any number of arguments [1].
49 'py_printf': func_printf,
51 # 'func_capwords' provides the spreadsheet function 'py_capwords'.
52 # 'py_capwords' should be called with a single argument.
53 # This should be a string ('s'), and the argument name is 'sentence'.
54 'py_capwords': ('s', 'sentence', func_capwords),
56 # 'func_bitand' provides 'bitand', which should be called with two
57 # numeric arguments (ff) named 'num1' and 'num2'.
58 'py_bitand': ('ff', 'num1, num2', func_bitand)
62 # [1] Actually, the 'def func_printf' statement says that it requires at least
63 # one argument. But there is no way to express that in the syntax used in
64 # the 'test_functions' dictionary.