File: py_func.py

package info (click to toggle)
gnumeric 1.12.18-2
  • links: PTS, VCS
  • area: main
  • in suites: jessie, jessie-kfreebsd
  • size: 103,816 kB
  • ctags: 25,022
  • sloc: ansic: 278,696; xml: 54,477; sh: 11,677; perl: 4,216; makefile: 2,751; yacc: 1,324; python: 203
file content (64 lines) | stat: -rw-r--r-- 1,927 bytes parent folder | download | duplicates (3)
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
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):
	'@GNM_FUNC_HELP_NAME@BITAND:bitwise AND of its two arguments.\n'\
	'@GNM_FUNC_HELP_ARG@number1:first value\n'\
	'@GNM_FUNC_HELP_ARG@number2:second value\n'\
	'@GNM_FUNC_HELP_EXAMPLES@=PY_BITAND(12, 6)\n'\
	'@GNM_FUNC_HELP_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.