Changeset bb8cdbb in git


Ignore:
Timestamp:
Aug 6, 2019, 1:17:03 PM (4 years ago)
Author:
Murray Heymann <heymann.murray@…>
Branches:
(u'spielwiese', '828514cf6e480e4bafc26df99217bf2a1ed1ef45')
Children:
86420e9c548fd4f1645ed4114c5f79fc3488a8a6
Parents:
7fb01017897be638682b5f17f6ef8eb851bc7273
Message:
Change initialize and finalize of python
Location:
machine_learning
Files:
3 edited

Legend:

Unmodified
Added
Removed
  • machine_learning/mlpredict.c

    r7fb0101 rbb8cdbb  
     1/**
     2 * @file   mlpredict.c
     3 * @brief  Functions for using python to do machine learning in Singular
     4 *
     5 * @author Murray Heymann
     6 * @date   August 2019
     7 */
    18#include <stdio.h>
    29#include <Python.h>
     
    2027}
    2128
     29
    2230/**
    23  * Initialise the machine learning system by downloading the helpfiles if
     31 * Initialise the machine learning system by starting the python
     32 * interpreter,  downloading the helpfiles if
    2433 * not present, and calculating the bag of words vectors for the helpfiles,
    25  * saving this info on the local system. 
     34 * saving this info on the local system.
    2635 *
    2736 * @return An integer: 1 if successful, 0 if some error were to occur.
    2837 */
    29 
    3038int ml_initialise()
    3139{
     
    3442        PyObject *pValue = NULL;
    3543
     44        if (!Py_IsInitialized()) {
     45                Py_Initialize();
     46        }
    3647        pValue = call_python_function(lookuptable, init_table_on_system);
    3748        if (pValue != NULL) {
     
    4354
    4455}
     56
     57/**
     58 * Finalize the python interpreter
     59 *
     60 * @return An integer: 1 if successful, 0 if not.
     61 */
     62int ml_finalise()
     63{
     64        if (Py_IsInitialized()) {
     65                Py_Finalize();
     66        }
     67        return 1;
     68}
     69
     70
    4571
    4672/**
     
    5682        PyObject *pName = NULL, *pModule = NULL, *pFunc = NULL;
    5783        PyObject *pValue = NULL;
    58         int retValue = 1;
    5984               
    60         Py_Initialize();
     85        if (!Py_IsInitialized()) {
     86                Py_Initialize();
     87        }
    6188
    6289        /* import the module */
    63         pName = PyUnicode_DecodeFSDefault(module);
     90        pName = PyString_FromString(module);
    6491        pModule = PyImport_Import(pName);
    6592        Py_DECREF(pName);
     
    90117                PyErr_Print();
    91118                fprintf(stderr, "Failed to load \"%s\"\n", module);
    92                 retValue = 0;
    93         }
    94         if (Py_FinalizeEx() < 0) {
    95                 retValue = 0;
    96119        }
    97120
     
    118141                                           int *pred_len)
    119142{
    120         Py_Initialize();
    121143
    122144
    123145
    124         if (Py_FinalizeEx() < 0) {
    125                 return 0;
    126         } else {
    127                 return 1;
    128         }
     146        return 1;
    129147}
    130148
  • machine_learning/mlpredict.h

    r7fb0101 rbb8cdbb  
    2727
    2828/**
    29  * Initialise the machine learning system by downloading the helpfiles if
     29 * Initialise the machine learning system by starting the python
     30 * interpreter,  downloading the helpfiles if
    3031 * not present, and calculating the bag of words vectors for the helpfiles,
    31  * saving this info on the local system. 
     32 * saving this info on the local system.
    3233 *
    3334 * @return An integer: 1 if successful, 0 if some error were to occur.
    3435 */
    3536int ml_initialise();
     37
     38/**
     39 * Finalize the python interpreter
     40 *
     41 * @return An integer: 1 if successful, 0 if not.
     42 */
     43int ml_finalise();
    3644
    3745/**
  • machine_learning/testml.c

    r7fb0101 rbb8cdbb  
    1010        }
    1111        i = ml_initialise();
    12        
    1312        printf("Returnvalue for initialise: %d\n", i);
     13
     14        i = ml_finalise();
     15        printf("Returnvalue for finalise: %d\n", i);
    1416
    1517        return 0;
Note: See TracChangeset for help on using the changeset viewer.