GnuCash c935c2f+
Loading...
Searching...
No Matches
Data Structures | Functions | Variables
function_class.py File Reference

Library for making python classes from a set of functions. More...

Go to the source code of this file.

Data Structures

class  python.function_class.ClassFromFunctions
 

Functions

 python.function_class.method_function_returns_instance (method_function, cls)
 
 python.function_class.method_function_returns_instance_list (method_function, cls)
 
 python.function_class.methods_return_instance_lists (cls, function_dict)
 
 python.function_class.default_arguments_decorator (function, *args, **kargs)
 Decorates a function to give it default, positional and keyword arguments.
 
 python.function_class.return_instance_if_value_has_it (value)
 
 python.function_class.process_list_convert_to_instance (value_list)
 
 python.function_class.process_dict_convert_to_instance (value_dict)
 
 python.function_class.extract_attributes_with_prefix (obj, prefix)
 
 python.function_class.methods_return_instance (cls, function_dict)
 

Variables

str python.function_class.INSTANCE_ARGUMENT = "instance"
 

Detailed Description

Library for making python classes from a set of functions.

Author
Mark Jenkins, ParIT Worker Co-operative mark@.nosp@m.pari.nosp@m.t.ca
Jeff Green, ParIT Worker Co-operative jeff@.nosp@m.pari.nosp@m.t.ca

Definition in file function_class.py.

Function Documentation

◆ default_arguments_decorator()

python.function_class.default_arguments_decorator (   function,
args,
**  kargs 
)

Decorates a function to give it default, positional and keyword arguments.

mimics python behavior when setting defaults in function/method arguments. arguments can be set for positional or keyword arguments.

kargs_pos contains positions of the keyword arguments.

Exceptions
ATypeError will be raised if an argument is set as a positional and keyword argument at the same time.
Note
It might be possible to get keyword argument positional information using introspection to avoid having to specify them manually

a keyword argument default will be overwritten by a positional argument at the actual function call

this function modifies the docstring of the wrapped function to reflect the defaults.

You can't use this decorator with @, because this function has more than one argument.

arguments:

Parameters
*argsoptional positional defaults
kargs_posdict with keyword arguments as key and their position in the argument list as value
**kargsoptional keyword defaults
Returns
new_function wrapping original function

Definition at line 262 of file function_class.py.

262def default_arguments_decorator(function, *args, **kargs):
263 """! Decorates a function to give it default, positional and keyword arguments
264
265 mimics python behavior when setting defaults in function/method arguments.
266 arguments can be set for positional or keyword arguments.
267
268 kargs_pos contains positions of the keyword arguments.
269 @exception A TypeError will be raised if an argument is set as a positional and keyword argument
270 at the same time.
271 @note It might be possible to get keyword argument positional information using
272 introspection to avoid having to specify them manually
273
274 a keyword argument default will be overwritten by a positional argument at the
275 actual function call
276
277 this function modifies the docstring of the wrapped function to reflect
278 the defaults.
279
280 You can't use this decorator with @, because this function has more
281 than one argument.
282
283 arguments:
284 @param *args: optional positional defaults
285 @param kargs_pos: dict with keyword arguments as key and their position in the argument list as value
286 @param **kargs: optional keyword defaults
287
288 @return new_function wrapping original function
289 """
290
291 def new_function(*function_args, **function_kargs):
292 kargs_pos = {}
293 if "kargs_pos" in kargs:
294 kargs_pos = kargs.pop("kargs_pos")
295 new_argset = list(function_args)
296 new_argset.extend(args[len(function_args) :])
297 new_kargset = {**kargs, **function_kargs}
298 for karg_pos in kargs_pos:
299 if karg_pos in new_kargset:
300 pos_karg = kargs_pos[karg_pos]
301 if pos_karg < len(new_argset):
302 new_kargset.pop(karg_pos)
303
304 return function(*new_argset, **new_kargset)
305
306 kargs_pos = {} if "kargs_pos" not in kargs else kargs["kargs_pos"]
307 for karg_pos in kargs_pos:
308 if karg_pos in kargs:
309 pos_karg = kargs_pos[karg_pos]
310 if pos_karg < len(args):
311 raise TypeError(
312 "default_arguments_decorator() got multiple values for argument '%s'"
313 % karg_pos
314 )
315
316 if new_function.__doc__ is None:
317 new_function.__doc__ = ""
318 if len(args):
319 firstarg = True
320 new_function.__doc__ += "positional argument defaults:\n"
321 for arg in args:
322 if not firstarg:
323 new_function.__doc__ += ", "
324 else:
325 new_function.__doc__ += " "
326 firstarg = False
327 new_function.__doc__ += str(arg)
328 new_function.__doc__ += "\n"
329 if len(kargs):
330 new_function.__doc__ += "keyword argument defaults:\n"
331 for karg in kargs:
332 if karg != "kargs_pos":
333 new_function.__doc__ += (
334 " " + str(karg) + " = " + str(kargs[karg]) + "\n"
335 )
336 if kargs_pos:
337 new_function.__doc__ += "keyword argument positions:\n"
338 for karg in kargs_pos:
339 new_function.__doc__ += (
340 " " + str(karg) + " is at pos " + str(kargs_pos[karg]) + "\n"
341 )
342 if len(args) or len(kargs):
343 new_function.__doc__ += (
344 "(defaults have been set by default_arguments_decorator method)"
345 )
346 return new_function
347
348

◆ extract_attributes_with_prefix()

python.function_class.extract_attributes_with_prefix (   obj,
  prefix 
)
Generator that iterates through the attributes of an object and
for any attribute that matches a prefix, this yields
the attribute name, the attribute value, and the text that appears
after the prefix in the name

Definition at line 380 of file function_class.py.

380def extract_attributes_with_prefix(obj, prefix):
381 """Generator that iterates through the attributes of an object and
382 for any attribute that matches a prefix, this yields
383 the attribute name, the attribute value, and the text that appears
384 after the prefix in the name
385 """
386 for attr_name, attr_value in iter(obj.__dict__.items()):
387 if attr_name.startswith(prefix):
388 after_prefix = attr_name[ len(prefix): ]
389 yield attr_name, attr_value, after_prefix
390

◆ method_function_returns_instance()

python.function_class.method_function_returns_instance (   method_function,
  cls 
)
A function decorator that is used to decorate method functions that
return instance data, to return instances instead.

You can't use this decorator with @, because this function has a second
argument.

Definition at line 233 of file function_class.py.

233def method_function_returns_instance(method_function, cls):
234 """A function decorator that is used to decorate method functions that
235 return instance data, to return instances instead.
236
237 You can't use this decorator with @, because this function has a second
238 argument.
239 """
240 assert( 'instance' == INSTANCE_ARGUMENT )
241 def new_function(*args, **kargs):
242 kargs_cls = { INSTANCE_ARGUMENT : method_function(*args, **kargs) }
243 if kargs_cls['instance'] == None:
244 return None
245 else:
246 return cls( **kargs_cls )
247
248 return new_function
249

◆ method_function_returns_instance_list()

python.function_class.method_function_returns_instance_list (   method_function,
  cls 
)

Definition at line 250 of file function_class.py.

250def method_function_returns_instance_list(method_function, cls):
251 def new_function(*args, **kargs):
252 return [ cls( **{INSTANCE_ARGUMENT: item} )
253 for item in method_function(*args, **kargs) ]
254 return new_function
255

◆ methods_return_instance()

python.function_class.methods_return_instance (   cls,
  function_dict 
)
Iterates through a dictionary of function name strings and instance names
and sets the function to return the associated instance

Definition at line 391 of file function_class.py.

391def methods_return_instance(cls, function_dict):
392 """Iterates through a dictionary of function name strings and instance names
393 and sets the function to return the associated instance
394 """
395 for func_name, instance_name in iter(function_dict.items()):
396 setattr(cls, func_name,
397 method_function_returns_instance( getattr(cls, func_name), instance_name))
398

◆ methods_return_instance_lists()

python.function_class.methods_return_instance_lists (   cls,
  function_dict 
)

Definition at line 256 of file function_class.py.

256def methods_return_instance_lists(cls, function_dict):
257 for func_name, instance_name in iter(function_dict.items()):
258 setattr(cls, func_name,
259 method_function_returns_instance_list(
260 getattr(cls, func_name), instance_name))
261

◆ process_dict_convert_to_instance()

python.function_class.process_dict_convert_to_instance (   value_dict)
Return a dict built from value_dict, where if a value is in an instance
of ClassFromFunctions, we put value.instance in the dict instead.

Things that are not instances of ClassFromFunctions are returned to
the new dict unchanged.

Definition at line 368 of file function_class.py.

368def process_dict_convert_to_instance(value_dict):
369 """Return a dict built from value_dict, where if a value is in an instance
370 of ClassFromFunctions, we put value.instance in the dict instead.
371
372 Things that are not instances of ClassFromFunctions are returned to
373 the new dict unchanged.
374 """
375 return {
376 key: return_instance_if_value_has_it(value) for key, value in value_dict.items()
377 }
378
379

◆ process_list_convert_to_instance()

python.function_class.process_list_convert_to_instance (   value_list)
Return a list built from value_list, where if a value is in an instance
of ClassFromFunctions, we put value.instance in the list instead.

Things that are not instances of ClassFromFunctions are returned to
the new list unchanged.

Definition at line 358 of file function_class.py.

358def process_list_convert_to_instance( value_list ):
359 """Return a list built from value_list, where if a value is in an instance
360 of ClassFromFunctions, we put value.instance in the list instead.
361
362 Things that are not instances of ClassFromFunctions are returned to
363 the new list unchanged.
364 """
365 return [ return_instance_if_value_has_it(value)
366 for value in value_list ]
367

◆ return_instance_if_value_has_it()

python.function_class.return_instance_if_value_has_it (   value)
Return value.instance if value is an instance of ClassFromFunctions,
else return value

Definition at line 349 of file function_class.py.

349def return_instance_if_value_has_it(value):
350 """Return value.instance if value is an instance of ClassFromFunctions,
351 else return value
352 """
353 if isinstance(value, ClassFromFunctions):
354 return value.instance
355 else:
356 return value
357

Variable Documentation

◆ INSTANCE_ARGUMENT

str python.function_class.INSTANCE_ARGUMENT = "instance"

Definition at line 29 of file function_class.py.