2018-04-17 16:33:38 +00:00
|
|
|
# Copyright 2018 The Bazel Authors. All rights reserved.
|
|
|
|
#
|
|
|
|
# Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
# you may not use this file except in compliance with the License.
|
|
|
|
# You may obtain a copy of the License at
|
|
|
|
#
|
|
|
|
# http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
#
|
|
|
|
# Unless required by applicable law or agreed to in writing, software
|
|
|
|
# distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
# See the License for the specific language governing permissions and
|
|
|
|
# limitations under the License.
|
|
|
|
|
2018-09-28 13:09:18 +00:00
|
|
|
"""Starlark module for working with partial function objects.
|
2018-04-17 16:52:23 +00:00
|
|
|
|
|
|
|
Partial function objects allow some parameters are bound before the call.
|
2018-04-17 16:33:38 +00:00
|
|
|
|
|
|
|
Similar to https://docs.python.org/3/library/functools.html#functools.partial.
|
|
|
|
"""
|
|
|
|
|
|
|
|
def _call(partial, *args, **kwargs):
|
2018-06-12 17:09:57 +00:00
|
|
|
"""Calls a partial created using `make`.
|
|
|
|
|
|
|
|
Args:
|
|
|
|
partial: The partial to be called.
|
|
|
|
*args: Additional positional arguments to be appended to the ones given to
|
|
|
|
make.
|
|
|
|
**kwargs: Additional keyword arguments to augment and override the ones
|
|
|
|
given to make.
|
|
|
|
|
|
|
|
Returns:
|
|
|
|
Whatever the function in the partial returns.
|
|
|
|
"""
|
|
|
|
function_args = partial.args + args
|
|
|
|
function_kwargs = dict(partial.kwargs)
|
|
|
|
function_kwargs.update(kwargs)
|
|
|
|
return partial.function(*function_args, **function_kwargs)
|
2018-04-17 16:33:38 +00:00
|
|
|
|
|
|
|
def _make(func, *args, **kwargs):
|
2018-06-12 17:09:57 +00:00
|
|
|
"""Creates a partial that can be called using `call`.
|
2018-04-17 16:33:38 +00:00
|
|
|
|
2018-06-12 17:09:57 +00:00
|
|
|
A partial can have args assigned to it at the make site, and can have args
|
|
|
|
passed to it at the call sites.
|
2018-04-17 16:33:38 +00:00
|
|
|
|
2018-06-12 17:09:57 +00:00
|
|
|
A partial 'function' can be defined with positional args and kwargs:
|
2018-04-17 16:33:38 +00:00
|
|
|
|
2018-06-12 17:09:57 +00:00
|
|
|
# function with no args
|
|
|
|
def function1():
|
|
|
|
...
|
2018-04-17 16:33:38 +00:00
|
|
|
|
2018-06-12 17:09:57 +00:00
|
|
|
# function with 2 args
|
|
|
|
def function2(arg1, arg2):
|
|
|
|
...
|
2018-04-17 16:33:38 +00:00
|
|
|
|
2018-06-12 17:09:57 +00:00
|
|
|
# function with 2 args and keyword args
|
|
|
|
def function3(arg1, arg2, x, y):
|
|
|
|
...
|
2018-04-17 16:33:38 +00:00
|
|
|
|
2018-06-12 17:09:57 +00:00
|
|
|
The positional args passed to the function are the args passed into make
|
|
|
|
followed by any additional positional args given to call. The below example
|
|
|
|
illustrates a function with two positional arguments where one is supplied by
|
|
|
|
make and the other by call:
|
2018-04-17 16:33:38 +00:00
|
|
|
|
2018-06-12 17:09:57 +00:00
|
|
|
# function demonstrating 1 arg at make site, and 1 arg at call site
|
|
|
|
def _foo(make_arg1, func_arg1):
|
|
|
|
print(make_arg1 + " " + func_arg1 + "!")
|
2018-04-17 16:33:38 +00:00
|
|
|
|
2018-06-12 17:09:57 +00:00
|
|
|
For example:
|
2018-04-17 16:33:38 +00:00
|
|
|
|
2018-06-12 17:09:57 +00:00
|
|
|
hi_func = partial.make(_foo, "Hello")
|
|
|
|
bye_func = partial.make(_foo, "Goodbye")
|
|
|
|
partial.call(hi_func, "Jennifer")
|
|
|
|
partial.call(hi_func, "Dave")
|
|
|
|
partial.call(bye_func, "Jennifer")
|
|
|
|
partial.call(bye_func, "Dave")
|
2018-04-17 16:33:38 +00:00
|
|
|
|
2018-06-12 17:09:57 +00:00
|
|
|
prints:
|
2018-04-17 16:52:23 +00:00
|
|
|
|
2018-06-12 17:09:57 +00:00
|
|
|
"Hello, Jennifer!"
|
|
|
|
"Hello, Dave!"
|
|
|
|
"Goodbye, Jennifer!"
|
|
|
|
"Goodbye, Dave!"
|
2018-04-17 16:33:38 +00:00
|
|
|
|
2018-06-12 17:09:57 +00:00
|
|
|
The keyword args given to the function are the kwargs passed into make
|
|
|
|
unioned with the keyword args given to call. In case of a conflict, the
|
|
|
|
keyword args given to call take precedence. This allows you to set a default
|
|
|
|
value for keyword arguments and override it at the call site.
|
2018-04-17 16:33:38 +00:00
|
|
|
|
2018-06-12 17:09:57 +00:00
|
|
|
Example with a make site arg, a call site arg, a make site kwarg and a
|
|
|
|
call site kwarg:
|
2018-04-17 16:52:23 +00:00
|
|
|
|
2018-06-12 17:09:57 +00:00
|
|
|
def _foo(make_arg1, call_arg1, make_location, call_location):
|
|
|
|
print(make_arg1 + " is from " + make_location + " and " +
|
|
|
|
call_arg1 + " is from " + call_location + "!")
|
2018-04-17 16:33:38 +00:00
|
|
|
|
2018-06-12 17:09:57 +00:00
|
|
|
func = partial.make(_foo, "Ben", make_location="Hollywood")
|
|
|
|
partial.call(func, "Jennifer", call_location="Denver")
|
2018-04-17 16:33:38 +00:00
|
|
|
|
2018-06-12 17:09:57 +00:00
|
|
|
Prints "Ben is from Hollywood and Jennifer is from Denver!".
|
2018-04-17 16:33:38 +00:00
|
|
|
|
2018-06-12 17:09:57 +00:00
|
|
|
partial.call(func, "Jennifer", make_location="LA", call_location="Denver")
|
2018-04-17 16:33:38 +00:00
|
|
|
|
2018-06-12 17:09:57 +00:00
|
|
|
Prints "Ben is from LA and Jennifer is from Denver!".
|
2018-04-17 16:52:23 +00:00
|
|
|
|
2018-06-12 17:09:57 +00:00
|
|
|
Note that keyword args may not overlap with positional args, regardless of
|
|
|
|
whether they are given during the make or call step. For instance, you can't
|
|
|
|
do:
|
2018-04-17 16:33:38 +00:00
|
|
|
|
2018-06-12 17:09:57 +00:00
|
|
|
def foo(x):
|
|
|
|
pass
|
2018-04-17 16:33:38 +00:00
|
|
|
|
2018-06-12 17:09:57 +00:00
|
|
|
func = partial.make(foo, 1)
|
|
|
|
partial.call(func, x=2)
|
2018-04-17 16:33:38 +00:00
|
|
|
|
2018-06-12 17:09:57 +00:00
|
|
|
Args:
|
|
|
|
func: The function to be called.
|
|
|
|
*args: Positional arguments to be passed to function.
|
|
|
|
**kwargs: Keyword arguments to be passed to function. Note that these can
|
|
|
|
be overridden at the call sites.
|
2018-04-17 16:33:38 +00:00
|
|
|
|
2018-06-12 17:09:57 +00:00
|
|
|
Returns:
|
|
|
|
A new `partial` that can be called using `call`
|
|
|
|
"""
|
|
|
|
return struct(function = func, args = args, kwargs = kwargs)
|
2018-04-17 16:33:38 +00:00
|
|
|
|
|
|
|
partial = struct(
|
2018-06-12 17:09:57 +00:00
|
|
|
make = _make,
|
|
|
|
call = _call,
|
2018-04-17 16:33:38 +00:00
|
|
|
)
|