MCPcopy Create free account
hub / github.com/easy-graph/Easy-Graph / argmap

Class argmap

easygraph/utils/decorators.py:260–1076  ·  view source on GitHub ↗

A decorator to apply a map to arguments before calling the function This class provides a decorator that maps (transforms) arguments of the function before the function is called. Thus for example, we have similar code in many functions to determine whether an argument is the number of

Source from the content-addressed store, hash-verified

258
259
260class argmap:
261 """A decorator to apply a map to arguments before calling the function
262
263 This class provides a decorator that maps (transforms) arguments of the function
264 before the function is called. Thus for example, we have similar code
265 in many functions to determine whether an argument is the number of nodes
266 to be created, or a list of nodes to be handled. The decorator provides
267 the code to accept either -- transforming the indicated argument into a
268 list of nodes before the actual function is called.
269
270 This decorator class allows us to process single or multiple arguments.
271 The arguments to be processed can be specified by string, naming the argument,
272 or by index, specifying the item in the args list.
273
274 Parameters
275 ----------
276 func : callable
277 The function to apply to arguments
278
279 *args : iterable of (int, str or tuple)
280 A list of parameters, specified either as strings (their names), ints
281 (numerical indices) or tuples, which may contain ints, strings, and
282 (recursively) tuples. Each indicates which parameters the decorator
283 should map. Tuples indicate that the map function takes (and returns)
284 multiple parameters in the same order and nested structure as indicated
285 here.
286
287 try_finally : bool (default: False)
288 When True, wrap the function call in a try-finally block with code
289 for the finally block created by `func`. This is used when the map
290 function constructs an object (like a file handle) that requires
291 post-processing (like closing).
292
293 Examples
294 --------
295 Most of these examples use `@argmap(...)` to apply the decorator to
296 the function defined on the next line.
297 In the EasyGraph codebase however, `argmap` is used within a function to
298 construct a decorator. That is, the decorator defines a mapping function
299 and then uses `argmap` to build and return a decorated function.
300 A simple example is a decorator that specifies which currency to report money.
301 The decorator (named `convert_to`) would be used like::
302
303 @convert_to("US_Dollars", "income")
304 def show_me_the_money(name, income):
305 print(f"{name} : {income}")
306
307 And the code to create the decorator might be::
308
309 def convert_to(currency, which_arg):
310 def _convert(amount):
311 if amount.currency != currency:
312 amount = amount.to_currency(currency)
313 return amount
314 return argmap(_convert, which_arg)
315
316 Despite this common idiom for argmap, most of the following examples
317 use the `@argmap(...)` idiom to save space.

Callers 3

not_implemented_forFunction · 0.85
open_fileFunction · 0.85
nodes_or_numberFunction · 0.85

Calls 1

compileMethod · 0.80

Tested by

no test coverage detected