[c237fe]: doc / source / about_pyke / cooking_functions.txt Maximize Restore History

Download this file

cooking_functions.txt    177 lines (134 with data), 5.9 kB

  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
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
.. $Id$
..
.. Copyright Š 2008 Bruce Frederiksen
..
.. Permission is hereby granted, free of charge, to any person obtaining a copy
.. of this software and associated documentation files (the "Software"), to deal
.. in the Software without restriction, including without limitation the rights
.. to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
.. copies of the Software, and to permit persons to whom the Software is
.. furnished to do so, subject to the following conditions:
..
.. The above copyright notice and this permission notice shall be included in
.. all copies or substantial portions of the Software.
..
.. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
.. IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
.. FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
.. AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
.. LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
.. OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
.. THE SOFTWARE.
restindex
crumb: Cooking Functions
page-description:
Explanation of how Pyke "cooks" Python functions.
/description
format: rest
encoding: utf8
output-encoding: utf8
include: yes
initialheaderlevel: 2
/restindex
uservalues
filedate: $Id$
/uservalues
===========================
Cooking Python Functions
===========================
"Cooking" a Python function means customizing it. And you customize it by
cooking certain parameter values into it as constant values.
Cooking a single parameter value
================================
First you define the function that you want to cook with an extra parameter
at the start:
>>> def foo(cooked, standard):
... print("foo called with cooked: %s, standard: %s" %
... (cooked, standard))
Now you can call this function with two parameters:
>>> foo('a', 'b')
foo called with cooked: a, standard: b
But your real intention is that it appear to be a function taking one
parameter, with the first parameter cooked in.
This is done with the ``partial`` class of the functools_ module in the
standard Python library.
>>> from functools import partial
And then using ``partial`` to cook the first parameter:
>>> cooked1 = partial(foo, 'cooked_value1')
Now ``cooked_foo`` is a function that takes one parameter:
>>> cooked1('value1')
foo called with cooked: cooked_value1, standard: value1
>>> cooked1('value2')
foo called with cooked: cooked_value1, standard: value2
And you can make other cooked functions from foo with other cooked values:
>>> cooked2 = partial(foo, 'cooked_value2')
>>> cooked2('value1')
foo called with cooked: cooked_value2, standard: value1
>>> cooked2('value2')
foo called with cooked: cooked_value2, standard: value2
And you can still use the first cooked function, so now you have two functions
for the price of one!
>>> cooked1('value3')
foo called with cooked: cooked_value1, standard: value3
>>> cooked1('value4')
foo called with cooked: cooked_value1, standard: value4
>>> cooked2('value5')
foo called with cooked: cooked_value2, standard: value5
>>> cooked2('value6')
foo called with cooked: cooked_value2, standard: value6
And you can keep going with this to make as many functions as you care to
from your single starting function.
Cooking a Function Call Graph
=============================
This same technique can be used to cook a function call graph, by making the
subordinate function a cooked parameter:
>>> def bar(child_fun, a):
... print("bar called with:", a)
... return child_fun(a)
And now you can cook which function ``bar`` calls the same way you cook any
other parameter:
>>> bar_float = partial(bar, float)
>>> bar_float('123')
bar called with: 123
123.0
>>> bar_min = partial(bar, min)
>>> bar_min((3,2,5))
bar called with: (3, 2, 5)
2
And, of course, you can use cooked functions as these subordinate functions
too:
>>> bar_cooked1 = partial(bar, cooked1)
>>> bar_cooked1('abc')
bar called with: abc
foo called with cooked: cooked_value1, standard: abc
Which means that you can create function call graphs to any depth:
>>> bar_bar_min = partial(bar, bar_min)
>>> bar_bar_min((3,2,5))
bar called with: (3, 2, 5)
bar called with: (3, 2, 5)
2
Cooking Several Parameters
==========================
In general, you may want to cook several values for each function. Some of
these values may specify which subordinate functions to call, others may just
fix certain constant values for the function.
Pyke does this using a single extra parameter called ``context``, which is a
read-only dictionary. It can then prepare this dictionary with as many values
as it needs and then cook the whole dictionary into the function using
``partial``.
Pyke translates each individual access to a cooked parameter into a dictionary
lookup on ``context`` that looks up that parameter name::
context['parameter_name']
The Need for Pyke
=================
Now that you understand how Pyke cooks Python functions, you should be able
to understand how this technique can achieve the "order of magnitude"
improvements to Adaptability/Customization, Performance and Code Reuse
discussed on the `About Pyke`_ page.
You should also now see the need for a tool like Pyke to assemble all of
these functions to fit specific situations and use cases.
.. note::
Pyke calls a customized function call graph a *plan*. Plans_ are explained
later, after you've been introduced to `Logic Programming in Pyke`_.
And, finally, you should start to get a sense for how "programming in the
large" with Pyke dovetails with "programming in the small" with Python.