frepple::utils::PythonInterpreter Class Reference

This class is used to maintain the Python interpreter. More...

#include <utils.h>

List of all members.

Static Public Member Functions

static void addThread ()
static void deleteThread ()
static void execute (const char *)
static void executeFile (string)
static void finalize ()
static PyObject * getModule ()
static const char * getPythonEncoding ()
static void initialize (int argc, char **argv)
static void registerGlobalMethod (const char *, PyCFunction, int, const char *, bool=true)
static void registerGlobalMethod (const char *, PyCFunctionWithKeywords, int, const char *, bool=true)

Detailed Description

This class is used to maintain the Python interpreter.

A single interpreter is used throughout the lifetime of the application.
The implementation is implemented in a thread-safe way (within the limitations of the Python threading model, of course).

During the initialization the code checks for a file 'init.py' in its search path and, if it does exist, the statements in the file will be executed. In this way a library of globally available functions can easily be initialized.

The stderr and stdout streams of Python are redirected by default to the frePPLe log stream.

The following frePPLe functions are available from within Python.
All of these are in the module called frePPLe.

  • The following classes and their attributes are accessible for reading and writing.
    Each object has a toXML() method that returns its XML representation as a string, or writes it to a file is a file is passed as argument.
    • buffer
    • buffer_default
    • buffer_infinite
    • buffer_procure
    • calendar
      • addBucket(integer identifier)
      • events()
    • calendarBucket
    • calendar_boolean
    • calendar_double
    • calendar_void
    • customer
    • customer_default
    • demand
    • demand_default
    • flow
    • flowplan
    • item
    • item_default
    • load
    • loadplan
    • location
    • location_default
    • operation
    • operation_alternate
      • addAlternate(operation=x, priority=y, effective_start=z1, effective_end=z2)
    • operation_fixed_time
    • operation_routing
      • addStep(tuple of operations)
    • operation_time_per
    • operationplan
    • parameters
    • problem (read-only)
    • resource
      • plan(list of dates to define the time periods)
    • resource_default
    • resource_infinite
    • setup_matrix
    • setup_matrix_default
    • solver
      • solve()
    • solver_mrp
      • commit()
      • rollback()
  • The following functions or attributes return iterators over the frePPLe objects:
    • buffers()
    • buffer.members
    • buffer.flows
    • buffer.flowplans
    • calendar.buckets
    • calendars()
    • customers()
    • customer.members
    • demands()
    • demand.operationplans
    • demand.pegging
    • demand.members
    • operation.flows
    • operation.loads
    • items()
    • item.members
    • locations()
    • location.members
    • operations()
    • operation.operationplans
    • problems()
    • resources()
    • resource.members
    • resource.loads
    • resource.loadplans
    • setup_matrices()
    • solvers()
  • printsize():
    Prints information about the memory consumption.
  • loadmodule(string [,parameter=value, ...]):
    Dynamically load a module in memory.
  • readXMLdata(string [,bool] [,bool]):
    Processes an XML string passed as argument.
  • log(string):
    Prints a string to the frePPLe log file.
    This is used for redirecting the stdout and stderr of Python.
  • readXMLfile(string [,bool] [,bool]):
    Read an XML-file.
  • saveXMLfile(string):
    Save the model to an XML-file.
  • saveplan(string):
    Save the main plan information to a file.
  • erase(boolean):
    Erase the model (arg true) or only the plan (arg false, default).
  • version:
    A string variable with the version number.

The technical implementation is inspired by and inherited from the following article: "Embedding Python in Multi-Threaded C/C++ Applications", see http://www.linuxjournal.com/article/3641

Definition at line 524 of file utils.h.


Member Function Documentation

void frepple::utils::PythonInterpreter::addThread ( )
static

Create a new Python thread state.
Each OS-level thread needs to initialize a Python thread state as well. When a new thread is created in the OS, this method should be called to create a Python thread state as well.
See the Python PyGILState_Ensure API.

Definition at line 154 of file pythonutils.cpp.

void frepple::utils::PythonInterpreter::deleteThread ( )
static

Delete a Python thread state.
Each OS-level thread has a Python thread state. When an OS thread is deleted, this method should be called to delete the Python thread state as well.
See the Python PyGILState_Release API.

Definition at line 170 of file pythonutils.cpp.

void frepple::utils::PythonInterpreter::execute ( const char *  cmd)
static

Execute some python code.

Definition at line 183 of file pythonutils.cpp.

void frepple::utils::PythonInterpreter::executeFile ( string  filename)
static

Execute a file with Python code.

Definition at line 223 of file pythonutils.cpp.

void frepple::utils::PythonInterpreter::finalize ( )
static

Finalizes the interpreter.

Definition at line 142 of file pythonutils.cpp.

static PyObject* frepple::utils::PythonInterpreter::getModule ( )
inlinestatic

Return a pointer to the main extension module.

Definition at line 556 of file utils.h.

const char * frepple::utils::PythonInterpreter::getPythonEncoding ( )
static

Return the preferred encoding of the Python interpreter.

Definition at line 46 of file pythonutils.cpp.

void frepple::utils::PythonInterpreter::initialize ( int  argc,
char **  argv 
)
static

Initializes the interpreter.

Definition at line 52 of file pythonutils.cpp.

void frepple::utils::PythonInterpreter::registerGlobalMethod ( const char *  name,
PyCFunction  method,
int  flags,
const char *  doc,
bool  lock = true 
)
static

Register a new method to Python.
Arguments:

  • The name of the built-in function/method
  • The function that implements it.
  • Combination of METH_* flags, which mostly describe the args expected by the C func.
  • The doc attribute, or NULL.

Definition at line 243 of file pythonutils.cpp.

void frepple::utils::PythonInterpreter::registerGlobalMethod ( const char *  c,
PyCFunctionWithKeywords  f,
int  i,
const char *  d,
bool  b = true 
)
static

Register a new method to Python.

Definition at line 299 of file pythonutils.cpp.


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