GnuCash  5.6-150-g038405b370+
Public Member Functions | Properties
python.function_class.ClassFromFunctions Class Reference
Inheritance diagram for python.function_class.ClassFromFunctions:

Public Member Functions

def __new__ (cls, args, kargs)
 
def __init__ (self, args, kargs)
 
def get_instance (self)
 
def add_method (cls, function_name, method_name)
 Add the function, method_name to this class as a method named name. More...
 
def ya_add_classmethod (cls, function_name, method_name)
 Add the function, method_name to this class as a classmethod named name. More...
 
def ya_add_method (cls, function_name, method_name)
 Add the function, method_name to this class as a method named name. More...
 
def add_methods_with_prefix (cls, prefix, exclude=[])
 
def add_constructor_and_methods_with_prefix (cls, prefix, constructor, exclude=[])
 
def decorate_functions (cls, decorator, args)
 
def decorate_method (cls, decorator, method_name, args, kargs)
 decorate method method_name of class cls with decorator decorator More...
 

Properties

 instance = property(get_instance)
 

Detailed Description

Inherit this class to give yourself a python class that wraps a set of
functions that together constitute the methods of the class.

The method functions must all have as a first argument an object
holding the instance data. There must also be a function that
returns a new instance of the class, the constructor.

Your subclass must define
_module - The module where the method functions, including the
constructor can be found
_new_instance - The name of a function that serves as a constructor,
returning the instance data.

To access the instance data, use the read-only property instance.

To add some functions from _module as methods, call classmethods like
add_method and add_methods_with_prefix.

Definition at line 31 of file function_class.py.

Constructor & Destructor Documentation

◆ __init__()

def python.function_class.ClassFromFunctions.__init__ (   self,
  args,
  kargs 
)
Construct a new instance, using either the function
self._module[self._new_instance] or using existing instance
data. (specified with the keyword argument, instance)

if instance argument is None it will be ignored and the
constructor will be called to get a new instance

Pass the arguments that should be passed on to
self._module[self._new_instance]. Any arguments of that
are instances of ClassFromFunctions will be switched with the instance
data. (by calling the .instance property)

Definition at line 57 of file function_class.py.

57  def __init__(self, *args, **kargs):
58  """Construct a new instance, using either the function
59  self._module[self._new_instance] or using existing instance
60  data. (specified with the keyword argument, instance)
61 
62  if instance argument is None it will be ignored and the
63  constructor will be called to get a new instance
64 
65  Pass the arguments that should be passed on to
66  self._module[self._new_instance]. Any arguments of that
67  are instances of ClassFromFunctions will be switched with the instance
68  data. (by calling the .instance property)
69  """
70  if INSTANCE_ARGUMENT in kargs and kargs[INSTANCE_ARGUMENT] is not None:
71  inst = kargs[INSTANCE_ARGUMENT]
72  # Unwrap if someone passes a wrapper object as instance data,
73  # e.g. GncNumeric(instance=some_GncNumeric). This can happen
74  # when a method's return type is changed from a raw SWIG proxy
75  # to a wrapper class and callers still re-wrap the result.
76  if isinstance(inst, ClassFromFunctions):
77  inst = inst.instance
78  self.__instance = inst
79  else:
80  self.__instance = getattr(self._module, self._new_instance)(
81  *process_list_convert_to_instance(args),
82  **process_dict_convert_to_instance(kargs))
83 

Member Function Documentation

◆ add_constructor_and_methods_with_prefix()

def python.function_class.ClassFromFunctions.add_constructor_and_methods_with_prefix (   cls,
  prefix,
  constructor,
  exclude = [] 
)
Add a group of functions with the same prefix, and set the
_new_instance attribute to prefix + constructor. Don't add methods
in array exclude.

Definition at line 203 of file function_class.py.

203  def add_constructor_and_methods_with_prefix(cls, prefix, constructor, exclude=[]):
204  """Add a group of functions with the same prefix, and set the
205  _new_instance attribute to prefix + constructor. Don't add methods
206  in array exclude.
207  """
208  cls.add_methods_with_prefix(prefix, exclude=exclude)
209  cls._new_instance = prefix + constructor
210 

◆ add_method()

def python.function_class.ClassFromFunctions.add_method (   cls,
  function_name,
  method_name 
)

Add the function, method_name to this class as a method named name.

arguments:

Parameters
clsClass: class to add methods to
function_namestring: name of the function to add
method_namestring: name of the method that function will be called

function will be wrapped by method_function

Definition at line 96 of file function_class.py.

96  def add_method(cls, function_name, method_name):
97  """! Add the function, method_name to this class as a method named name
98 
99  arguments:
100  @param cls Class: class to add methods to
101  @param function_name string: name of the function to add
102  @param method_name string: name of the method that function will be called
103 
104  function will be wrapped by method_function"""
105 
106  def method_function(self, *meth_func_args, **meth_func_kargs):
107  """! wrapper method for function
108 
109  arguments:
110  @param self: FunctionClass instance. Will be turned to its instance property.
111  @param *meth_func_args: arguments to be passed to function. All FunctionClass
112  objects will be turned to their respective instances.
113  @param **meth_func_kargs: keyword arguments to be passed to function. All
114  FunctionClass objects will be turned to their respective instances."""
115  return getattr(self._module, function_name)(
116  self.instance,
117  *process_list_convert_to_instance(meth_func_args),
118  **process_dict_convert_to_instance(meth_func_kargs)
119  )
120 
121  setattr(cls, method_name, method_function)
122  setattr(method_function, "__name__", method_name)
123  return method_function
124 

◆ add_methods_with_prefix()

def python.function_class.ClassFromFunctions.add_methods_with_prefix (   cls,
  prefix,
  exclude = [] 
)
Add a group of functions with the same prefix, exclude methods
in array exclude.

Definition at line 192 of file function_class.py.

192  def add_methods_with_prefix(cls, prefix, exclude=[]):
193  """Add a group of functions with the same prefix, exclude methods
194  in array exclude.
195  """
196  for function_name, function_value, after_prefix in \
197  extract_attributes_with_prefix(cls._module, prefix):
198 
199  if not (function_name in exclude):
200  cls.add_method(function_name, after_prefix)
201 

◆ decorate_method()

def python.function_class.ClassFromFunctions.decorate_method (   cls,
  decorator,
  method_name,
  args,
  kargs 
)

decorate method method_name of class cls with decorator decorator

in difference to decorate_functions() this allows to provide additional arguments for the decorator function.

arguments:

Parameters
clsclass
decoratorfunction to decorate method
method_namename of method to decorate (string)
*argspositional arguments for decorator
**kargskeyword arguments for decorator

Definition at line 218 of file function_class.py.

218  def decorate_method(cls, decorator, method_name, *args, **kargs):
219  """! decorate method method_name of class cls with decorator decorator
220 
221  in difference to decorate_functions() this allows to provide additional
222  arguments for the decorator function.
223 
224  arguments:
225  @param cls: class
226  @param decorator: function to decorate method
227  @param method_name: name of method to decorate (string)
228  @param *args: positional arguments for decorator
229  @param **kargs: keyword arguments for decorator"""
230  setattr(cls, method_name,
231  decorator(getattr(cls, method_name), *args, **kargs))
232 

◆ get_instance()

def python.function_class.ClassFromFunctions.get_instance (   self)
Get the instance data.

You can also call the instance property

Definition at line 84 of file function_class.py.

84  def get_instance(self):
85  """Get the instance data.
86 
87  You can also call the instance property
88  """
89  return self.__instance
90 

◆ ya_add_classmethod()

def python.function_class.ClassFromFunctions.ya_add_classmethod (   cls,
  function_name,
  method_name 
)

Add the function, method_name to this class as a classmethod named name.

Taken from function_class and modified from add_method() to add classmethod instead of method and not to turn self argument to self.instance.

arguments:

Parameters
clsClass: class to add methods to
function_namestring: name of the function to add
method_namestring: name of the classmethod that function will be called

function will be wrapped by method_function

Definition at line 126 of file function_class.py.

126  def ya_add_classmethod(cls, function_name, method_name):
127  """! Add the function, method_name to this class as a classmethod named name
128 
129  Taken from function_class and modified from add_method() to add classmethod
130  instead of method and not to turn self argument to self.instance.
131 
132  arguments:
133  @param cls Class: class to add methods to
134  @param function_name string: name of the function to add
135  @param method_name string: name of the classmethod that function will be called
136 
137  function will be wrapped by method_function"""
138 
139  def method_function(self, *meth_func_args, **meth_func_kargs):
140  """! wrapper method for function
141 
142  arguments:
143  @param self: FunctionClass instance.
144  @param *meth_func_args: arguments to be passed to function. All FunctionClass
145  objects will be turned to their respective instances.
146  @param **meth_func_kargs: keyword arguments to be passed to function. All
147  FunctionClass objects will be turned to their respective instances."""
148  return getattr(self._module, function_name)(
149  self,
150  *process_list_convert_to_instance(meth_func_args),
151  **process_dict_convert_to_instance(meth_func_kargs)
152  )
153 
154  setattr(cls, method_name, classmethod(method_function))
155  setattr(method_function, "__name__", method_name)
156  return method_function
157 

◆ ya_add_method()

def python.function_class.ClassFromFunctions.ya_add_method (   cls,
  function_name,
  method_name 
)

Add the function, method_name to this class as a method named name.

Taken from function_class. Modified to not turn self to self.instance as add_method() does.

arguments:

Parameters
clsClass: class to add methods to
function_namestring: name of the function to add
method_namestring: name of the method that function will be called

function will be wrapped by method_function

Definition at line 159 of file function_class.py.

159  def ya_add_method(cls, function_name, method_name):
160  """! Add the function, method_name to this class as a method named name
161 
162  Taken from function_class. Modified to not turn self to self.instance
163  as add_method() does.
164 
165  arguments:
166  @param cls Class: class to add methods to
167  @param function_name string: name of the function to add
168  @param method_name string: name of the method that function will be called
169 
170  function will be wrapped by method_function"""
171 
172  def method_function(self, *meth_func_args, **meth_func_kargs):
173  """! wrapper method for function
174 
175  arguments:
176  @param self: FunctionClass instance.
177  @param *meth_func_args: arguments to be passed to function. All FunctionClass
178  objects will be turned to their respective instances.
179  @param **meth_func_kargs: keyword arguments to be passed to function. All
180  FunctionClass objects will be turned to their respective instances."""
181  return getattr(self._module, function_name)(
182  self,
183  *process_list_convert_to_instance(meth_func_args),
184  **process_dict_convert_to_instance(meth_func_kargs)
185  )
186 
187  setattr(cls, method_name, method_function)
188  setattr(method_function, "__name__", method_name)
189  return method_function
190 

The documentation for this class was generated from the following file: