GnuCash  4.2-242-g15d3eb48d
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

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

Variables

string 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()

def 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 funtion 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 255 of file function_class.py.

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

◆ extract_attributes_with_prefix()

def 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 373 of file function_class.py.

373 def extract_attributes_with_prefix(obj, prefix):
374  """Generator that iterates through the attributes of an object and
375  for any attribute that matches a prefix, this yields
376  the attribute name, the attribute value, and the text that appears
377  after the prefix in the name
378  """
379  for attr_name, attr_value in iter(obj.__dict__.items()):
380  if attr_name.startswith(prefix):
381  after_prefix = attr_name[ len(prefix): ]
382  yield attr_name, attr_value, after_prefix
383 

◆ method_function_returns_instance()

def 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 226 of file function_class.py.

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

◆ methods_return_instance()

def 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 384 of file function_class.py.

384 def methods_return_instance(cls, function_dict):
385  """Iterates through a dictionary of function name strings and instance names
386  and sets the function to return the associated instance
387  """
388  for func_name, instance_name in iter(function_dict.items()):
389  setattr(cls, func_name,
390  method_function_returns_instance( getattr(cls, func_name), instance_name))
391 
392 

◆ process_dict_convert_to_instance()

def 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 361 of file function_class.py.

361 def process_dict_convert_to_instance(value_dict):
362  """Return a dict built from value_dict, where if a value is in an instance
363  of ClassFromFunctions, we put value.instance in the dict instead.
364 
365  Things that are not instances of ClassFromFunctions are returned to
366  the new dict unchanged.
367  """
368  return {
369  key: return_instance_if_value_has_it(value) for key, value in value_dict.items()
370  }
371 
372 

◆ process_list_convert_to_instance()

def 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 351 of file function_class.py.

351 def process_list_convert_to_instance( value_list ):
352  """Return a list built from value_list, where if a value is in an instance
353  of ClassFromFunctions, we put value.instance in the list instead.
354 
355  Things that are not instances of ClassFromFunctions are returned to
356  the new list unchanged.
357  """
358  return [ return_instance_if_value_has_it(value)
359  for value in value_list ]
360 

◆ return_instance_if_value_has_it()

def 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 342 of file function_class.py.

342 def return_instance_if_value_has_it(value):
343  """Return value.instance if value is an instance of ClassFromFunctions,
344  else return value
345  """
346  if isinstance(value, ClassFromFunctions):
347  return value.instance
348  else:
349  return value
350