|
| 1 | +# Copyright 2018 The Bazel Authors. All rights reserved. |
| 2 | +# |
| 3 | +# Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +# you may not use this file except in compliance with the License. |
| 5 | +# You may obtain a copy of the License at |
| 6 | +# |
| 7 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +# |
| 9 | +# Unless required by applicable law or agreed to in writing, software |
| 10 | +# distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +# See the License for the specific language governing permissions and |
| 13 | +# limitations under the License. |
| 14 | + |
| 15 | +"""Skylark module for working with function objects where some parameters are |
| 16 | + bound before the call. |
| 17 | +
|
| 18 | +Similar to https://docs.python.org/3/library/functools.html#functools.partial. |
| 19 | +""" |
| 20 | + |
| 21 | +def _call(partial, *args, **kwargs): |
| 22 | + """Calls a partial created using `make`. |
| 23 | +
|
| 24 | + Args: |
| 25 | + partial: The partial to be called. |
| 26 | + *args: Additional positional arguments to be appended to the ones given to |
| 27 | + make. |
| 28 | + **kwargs: Additional keyword arguments to augment and override the ones |
| 29 | + given to make. |
| 30 | +
|
| 31 | + Returns: |
| 32 | + Whatever the function in the partial returns. |
| 33 | + """ |
| 34 | + function_args = partial.args + args |
| 35 | + function_kwargs = dict(partial.kwargs) |
| 36 | + function_kwargs.update(kwargs) |
| 37 | + return partial.function(*function_args, **function_kwargs) |
| 38 | + |
| 39 | +def _make(func, *args, **kwargs): |
| 40 | + """Creates a partial that can be called using `call`. |
| 41 | +
|
| 42 | + A partial can have args assigned to it at the make site, and can have args |
| 43 | + passed to it at the call sites. |
| 44 | +
|
| 45 | + A partial 'function' can be defined with positional args and kwargs: |
| 46 | +
|
| 47 | + # function with no args |
| 48 | + def function1(): |
| 49 | + ... |
| 50 | +
|
| 51 | + # function with 2 args |
| 52 | + def function2(arg1, arg2): |
| 53 | + ... |
| 54 | +
|
| 55 | + # function with 2 args and keyword args |
| 56 | + def function3(arg1, arg2, x, y): |
| 57 | + ... |
| 58 | +
|
| 59 | + The positional args passed to the function are the args passed into make |
| 60 | + followed by any additional positional args given to call. The below example |
| 61 | + illustrates a function with two positional arguments where one is supplied by |
| 62 | + make and the other by call: |
| 63 | +
|
| 64 | + # function demonstrating 1 arg at make site, and 1 arg at call site |
| 65 | + def _foo(make_arg1, func_arg1): |
| 66 | + print(make_arg1 + " " + func_arg1 + "!") |
| 67 | +
|
| 68 | + For example: |
| 69 | +
|
| 70 | + hi_func = partial.make(_foo, "Hello") |
| 71 | + bye_func = partial.make(_foo, "Goodbye") |
| 72 | + partial.call(hi_func, "Jennifer") |
| 73 | + partial.call(hi_func, "Dave") |
| 74 | + partial.call(bye_func, "Jennifer") |
| 75 | + partial.call(bye_func, "Dave") |
| 76 | +
|
| 77 | + prints: |
| 78 | + |
| 79 | + "Hello, Jennifer!" |
| 80 | + "Hello, Dave!" |
| 81 | + "Goodbye, Jennifer!" |
| 82 | + "Goodbye, Dave!" |
| 83 | +
|
| 84 | + The keyword args given to the function are the kwargs passed into make |
| 85 | + unioned with the keyword args given to call. In case of a conflict, the |
| 86 | + keyword args given to call take precedence. This allows you to set a default |
| 87 | + value for keyword arguments and override it at the call site. |
| 88 | +
|
| 89 | + Example with a make site arg, a call site arg, a make site kwarg and a |
| 90 | + call site kwarg: |
| 91 | + |
| 92 | + def _foo(make_arg1, call_arg1, make_location, call_location): |
| 93 | + print(make_arg1 + " is from " + make_location + " and " + |
| 94 | + call_arg1 + " is from " + call_location + "!") |
| 95 | +
|
| 96 | + func = partial.make(_foo, "Ben", make_location="Hollywood") |
| 97 | + partial.call(func, "Jennifer", call_location="Denver") |
| 98 | +
|
| 99 | + Prints "Ben is from Hollywood and Jennifer is from Denver!". |
| 100 | +
|
| 101 | + partial.call(func, "Jennifer", make_location="LA", call_location="Denver") |
| 102 | +
|
| 103 | + Prints "Ben is from LA and Jennifer is from Denver!". |
| 104 | + |
| 105 | + Note that keyword args may not overlap with positional args, regardless of |
| 106 | + whether they are given during the make or call step. For instance, you can't |
| 107 | + do: |
| 108 | +
|
| 109 | + def foo(x): |
| 110 | + pass |
| 111 | +
|
| 112 | + func = partial.make(foo, 1) |
| 113 | + partial.call(func, x=2) |
| 114 | +
|
| 115 | +
|
| 116 | + Args: |
| 117 | + func: The function to be called. |
| 118 | + *args: Positional arguments to be passed to function. |
| 119 | + **kwargs: Keyword arguments to be passed to function. Note that these can |
| 120 | + be overridden at the call sites. |
| 121 | +
|
| 122 | + Returns: |
| 123 | + A new `partial` that can be called using `call` |
| 124 | + """ |
| 125 | + return struct(function=func, args=args, kwargs=kwargs) |
| 126 | + |
| 127 | +partial = struct( |
| 128 | + make=_make, |
| 129 | + call=_call, |
| 130 | +) |
0 commit comments