File: py_func.py

package info (click to toggle)
gnumeric 1.4.3-4
  • links: PTS
  • area: main
  • in suites: sarge
  • size: 71,576 kB
  • ctags: 28,555
  • sloc: ansic: 282,333; xml: 45,788; sh: 8,479; makefile: 3,119; yacc: 1,129; lisp: 200; perl: 173; python: 86
file content (68 lines) | stat: -rw-r--r-- 1,908 bytes parent folder | download | duplicates (6)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
from Gnumeric import *
import string

# Here is a function with variable number of arguments
def func_printf(format, *args):
	'@FUNCTION=PY_PRINTF\n'\
	'@SYNTAX=PY_PRINTF (format,...)\n'\
	'@DESCRIPTION='\
	'\n'\
	'@EXAMPLES=\n'\
	'PY_PRINTF("Test: %.2f",12) equals "Test: 12.00")'\
	'\n'\
	'@SEEALSO='

	try:
		val = format % args
	except TypeError:
		raise GnumericError, GnumericErrorVALUE
	else:
		return val

# Here is a function with one argument
def func_capwords(str):
	'@FUNCTION=PY_CAPWORDS\n'\
	'@SYNTAX=PY_CAPWORDS (sentence)\n'\
	'@DESCRIPTION='\
	'\n'\
	'@EXAMPLES=\n'\
	'PY_CAPWORDS("Hello world") equals "Hello World")'\
	'\n'\
	'@SEEALSO='

	return string.capwords(str)

# Here is a function which calls a spreadsheet function
def func_bitand(num1, num2):
	'@FUNCTION=PY_BITAND\n'\
	'@SYNTAX=PY_BITAND (num)\n'\
	'@DESCRIPTION=The BITAND function returns bitwise and-ing '\
	'of its arguments.'\
	'\n'\
	'@EXAMPLES=\n'\
	'PY_BITAND(6, 2) equals 2)'\
	'\n'\
	'@SEEALSO=BITAND'
	gnm_bitand=functions['bitand']
	return gnm_bitand(num1, num2)

test_functions = {
	# Here we tell Gnumeric that the Python function 'func_printf'
	# provides the spreadsheet function 'py_printf', and that
	# 'py_printf' may be called with any number of arguments [1].
	'py_printf': func_printf,

	# 'func_capwords' provides the spreadsheet function 'py_capwords'.
	# 'py_capwords' should be called with a single argument.
	# This should be a string ('s'), and the argument name is 'sentence'.
	'py_capwords': ('s', 'sentence', func_capwords),

	# 'func_bitand' provides 'bitand', which should be called with two
	# numeric arguments (ff) named 'num1' and 'num2'. 
	'py_bitand':   ('ff', 'num1, num2', func_bitand)
}


# [1] Actually, the 'def func_printf' statement says that it requires at least
#     one argument. But there is no way to express that in the syntax used in
#     the 'test_functions' dictionary.