run¶
-
class
batchpy.run.
Run
(batch, saveresult=True, **parameters)[source]¶ A batchpy run base class
This class is intended as a base class from which custom user defined run objects can inherit. In the custom run class the
run()
method needs to be redefined to actually run the wanted computations and return the result as a dictionary.Parameters: batch :
Batch()
objectThe batch the run belongs to
saveresult : boolean, optional
Save the results to disk or not, if not the result is available in the _result attribute
**parameters :
Keyword parameters which modify the run instance
Notes
Batchpy runs should not be created directly but through the batch methods
add_run()
oradd_factorial_runs()
.Examples
>>> class Myrun(batchpy.Run): ... def run(self,mypar=5): ... # some conplicated computation ... return {'val': 2*mypar} ... >>> batch = batchpy.Batch('mybatch') >>> run = Myrun(batch,saveresult=False,mypar=5) >>> run() {'val': 10} >>>
-
run
()[source]¶ Perform calculations and return the result
This method should be overwritten in a user defined child class to perform the actual computations.
Parameters: parameters
parameters can be defined as named parameters. The use of
**kwargs
is not supported.Returns: res : dict
a dictionary with the results of the run
Examples
>>> class Myrun(batchpy.Run): ... def run(self,mypar=5): ... # some conplicated computation ... return {'val': 2*mypar} ...
-
__call__
()[source]¶ Checks if the run results are already computed and compute them if not.
When a run is called the class checks if the results are available in memory or on the disk.
When the result is available in memory, it is returned. When it is available on disk, it is loaded and returned.
When the result is not available it is computed using the
run
method and the results are stored on disk (if the_saveresult
attribute is true) or in the_result
attribute (otherwise).The computation is timed and the runtime is saved in the
runtime
attribute.Returns: res : anything
Results.
Examples
>>> batch = batchpy.Batch('mybatch') >>> run = Myrun(batch,saveresult=False,mypar=5) >>> run() {'val': 10}
-
load
()[source]¶ Checks if the run results are already computed and return them if so.
When the result is available in memory, it is returned. When it is available on disk, it is loaded and returned. When the result is not computed yet this returns
None
.Returns: res : anything,
None
Results, returns
None
if the results are not available.Examples
>>> run.load() {'val': 10}
-
clear
()[source]¶ Tries to erase the run results from the disk
Returns: success : bool
True
if the run was deleted from the disk,False
otherwise.Examples
>>> run.clear() True
-
id
¶ Property returning the id.
-
parameters
¶ Property returning the run parameters.
-
result
¶ Property alias to self.load().
-
done
¶ Property returning if the run is done or not.
-
runtime
¶ Property returning the computation time of a run
-
index
¶ Property returning the run index in its batch.
-
filename
¶ Property returning the filename of the run.
-
generate_id
(parameters)[source]¶ Generates an id hash from the parameters.
The id hash is used to identify a run. It is hashed from the parameters used to create the run to ensure that when even a single parameter is changed the run ids are different. The id is stored in the :code`id` attribute.
Parameters: parameters : dict
a dictionary with parameters from which to compute the hash.
Returns: id : string
the id hash of this run
Notes
When classes, methods or functions are supplied as parameters, the hash is created using their name attribute. This avoids ids being different when python is restarted. A hash created from the function itself would be different each time python starts as the object resides in a different memory location.
Examples
>>> run.generate_id(run.parameters) '10ae24979c5028fa873651bca338152dc0484245'
-
-
class
batchpy.run.
ResultRun
(batch, id)[source]¶ A class to load run results.
Parameters: batch :
Batch()
objectThe batch the run belongs to.
id : str or list of str
An id or list of ids specifying previously saved runs.
Notes
Batchpy result runs should not be created directly but through the batch methods
add_resultrun()
.Examples
>>> batch = batchpy.Batch('mybatch') >>> run = batchpy.ResultRun(batch,'3ecc784a9d5cf26eb6420de2a43f04b310073925')