Exemplo n.º 1
0
static PyObject*
nb_multiply (PyObject* o1, PyObject* o2) {
    float o1w = 0, o1h = 0, o1d = 0;
    float o2w = 0, o2h = 0, o2d = 0;
    PyObject *args, *result;
    PySoy_atoms_Size_Object *so1, *so2;

    // parse object 1
    if (PyLong_Check(o1)) {
        o1w = o1h = o1d = PyLong_AsDouble(o1);
    }
    else if (PyFloat_Check(o1)) {
        o1w = o1h = o1d = PyFloat_AsDouble(o1);
    }
    else if (PySoy_atoms_Size_Check(o1)) {
        so1 = (PySoy_atoms_Size_Object*)o1;
        o1w = soy_atoms_size_get_width(so1->g);
        o1h = soy_atoms_size_get_height(so1->g);
        o1d = soy_atoms_size_get_depth(so1->g);
    }
    else {
        PyErr_SetString(PyExc_TypeError, "unsupported operand type(s)");
        return NULL;
    }

    // parse object 2
    if (PyLong_Check(o2)) {
        o2w = o2h = o2d = PyLong_AsDouble(o2);
    }
    else if (PyFloat_Check(o2)) {
        o2w = o2h = o2d = PyFloat_AsDouble(o2);
    }
    else if (PySoy_atoms_Size_Check(o2)) {
        so2 = (PySoy_atoms_Size_Object*)o2;
        o2w = soy_atoms_size_get_width(so2->g);
        o2h = soy_atoms_size_get_height(so2->g);
        o2d = soy_atoms_size_get_depth(so2->g);
    } else {
        PyErr_SetString(PyExc_TypeError, "unsupported operand type(s)");
        return NULL;
    }

    // build args with calculated values
    args = Py_BuildValue("((fff))", o1w * o2w, o1h * o2h, o1d * o2d);

    // create result object
    result = tp_new(&PySoy_atoms_Size_Type, args, NULL);

    // decref args tuple and tmp
    Py_DECREF(args);

    // return calculated result
    return result;
}
Exemplo n.º 2
0
/*static*/ PyObject *
PickVarInfo_SetMixValues(PyObject *self, PyObject *args)
{
    PickVarInfoObject *obj = (PickVarInfoObject *)self;

    doubleVector  &vec = obj->data->GetMixValues();
    PyObject     *tuple;
    if(!PyArg_ParseTuple(args, "O", &tuple))
        return NULL;

    if(PyTuple_Check(tuple))
    {
        vec.resize(PyTuple_Size(tuple));
        for(int i = 0; i < PyTuple_Size(tuple); ++i)
        {
            PyObject *item = PyTuple_GET_ITEM(tuple, i);
            if(PyFloat_Check(item))
                vec[i] = PyFloat_AS_DOUBLE(item);
            else if(PyInt_Check(item))
                vec[i] = double(PyInt_AS_LONG(item));
            else if(PyLong_Check(item))
                vec[i] = PyLong_AsDouble(item);
            else
                vec[i] = 0.;
        }
    }
    else if(PyFloat_Check(tuple))
    {
        vec.resize(1);
        vec[0] = PyFloat_AS_DOUBLE(tuple);
    }
    else if(PyInt_Check(tuple))
    {
        vec.resize(1);
        vec[0] = double(PyInt_AS_LONG(tuple));
    }
    else if(PyLong_Check(tuple))
    {
        vec.resize(1);
        vec[0] = PyLong_AsDouble(tuple);
    }
    else
        return NULL;

    // Mark the mixValues in the object as modified.
    obj->data->SelectMixValues();

    Py_INCREF(Py_None);
    return Py_None;
}
/*static*/ PyObject *
ConstructDataBinningAttributes_SetBinBoundaries(PyObject *self, PyObject *args)
{
    ConstructDataBinningAttributesObject *obj = (ConstructDataBinningAttributesObject *)self;

    doubleVector  &vec = obj->data->GetBinBoundaries();
    PyObject     *tuple;
    if(!PyArg_ParseTuple(args, "O", &tuple))
        return NULL;

    if(PyTuple_Check(tuple))
    {
        vec.resize(PyTuple_Size(tuple));
        for(int i = 0; i < PyTuple_Size(tuple); ++i)
        {
            PyObject *item = PyTuple_GET_ITEM(tuple, i);
            if(PyFloat_Check(item))
                vec[i] = PyFloat_AS_DOUBLE(item);
            else if(PyInt_Check(item))
                vec[i] = double(PyInt_AS_LONG(item));
            else if(PyLong_Check(item))
                vec[i] = PyLong_AsDouble(item);
            else
                vec[i] = 0.;
        }
    }
    else if(PyFloat_Check(tuple))
    {
        vec.resize(1);
        vec[0] = PyFloat_AS_DOUBLE(tuple);
    }
    else if(PyInt_Check(tuple))
    {
        vec.resize(1);
        vec[0] = double(PyInt_AS_LONG(tuple));
    }
    else if(PyLong_Check(tuple))
    {
        vec.resize(1);
        vec[0] = PyLong_AsDouble(tuple);
    }
    else
        return NULL;

    // Mark the binBoundaries in the object as modified.
    obj->data->SelectBinBoundaries();

    Py_INCREF(Py_None);
    return Py_None;
}
Exemplo n.º 4
0
static PyObject*
nb_multiply (PyObject* o1, PyObject* o2) {
    double o1x, o1y, o1z = 0;
    double o2x, o2y, o2z = 0;
    PyObject *args, *result;
	PySoy_atoms_Rotation_Object *so1, *so2;

    // parse object 1
    if (PyLong_Check(o1)) {
	  o1x = o1y = o1z = PyLong_AsDouble(o1);	  
	} else if (PyFloat_Check(o1)) {
	  o1x = o1y = o1z = PyFloat_AsDouble(o1);	  
	} else if (PySoy_atoms_Rotation_Check(o1)) {
	  so1 = (PySoy_atoms_Rotation_Object*)o1;
	  o1x = soy_atoms_rotation_get_alpha(so1->g);
	  o1y = soy_atoms_rotation_get_beta(so1->g);
	  o1z = soy_atoms_rotation_get_gamma(so1->g);
	} else {
        PyErr_SetString(PyExc_TypeError, "unsupported operand type(s)");
        return NULL;
    }

    // parse object 2
    if (PyLong_Check(o2)) {
	  o2x = o2y = o2z = PyLong_AsDouble(o2);	  
	} else if (PyFloat_Check(o2)) {
	  o2x = o2y = o2z = PyFloat_AsDouble(o2);	  
	} else if (PySoy_atoms_Rotation_Check(o2)) {
	  so2 = (PySoy_atoms_Rotation_Object*)o2;
	  o2x = soy_atoms_rotation_get_alpha(so2->g);
	  o2y = soy_atoms_rotation_get_beta(so2->g);
	  o2z = soy_atoms_rotation_get_gamma(so2->g);
	} else {
        PyErr_SetString(PyExc_TypeError, "unsupported operand type(s)");
        return NULL;
    }

    // build args with calculated values
    args = Py_BuildValue("((fff))", o1x * o2x, o1y * o2y, o1z * o2z);

    // create result object
    result = tp_new(&PySoy_atoms_Rotation_Type, args, NULL);

    // decref args tuple
    Py_DECREF(args);

    // return calculated result
    return result;
}
Exemplo n.º 5
0
static PyObject*
nb_add (PyObject* o1, PyObject* o2) {
    float o1x = 0, o1y = 0, o1z = 0;
    float o2x = 0, o2y = 0, o2z = 0;
    PyObject *args, *result;
    PySoy_atoms_Position_Object *so1, *so2;

    // parse object 1
    if (PyLong_Check(o1)) {
        o1x = o1y = o1z = PyLong_AsDouble(o1);
    } else if (PyFloat_Check(o1)) {
        o1x = o1y = o1z = PyFloat_AsDouble(o1);
    } else if (PySoy_atoms_Position_Check(o1)) {
        so1 = (PySoy_atoms_Position_Object*)o1;
        o1x = soy_atoms_position_get_x(so1->g);
        o1y = soy_atoms_position_get_y(so1->g);
        o1z = soy_atoms_position_get_z(so1->g);
    } else {
        PyErr_SetString(PyExc_TypeError, "unsupported operand type(s)");
        return NULL;
    }

    // parse object 2
    if (PyLong_Check(o2)) {
        o2x = o2y = o2z = PyLong_AsDouble(o2);
    } else if (PyFloat_Check(o2)) {
        o2x = o2y = o2z = PyFloat_AsDouble(o2);
    } else if (PySoy_atoms_Position_Check(o2)) {
        so2 = (PySoy_atoms_Position_Object*)o2;
        o2x = soy_atoms_position_get_x(so2->g);
        o2y = soy_atoms_position_get_y(so2->g);
        o2z = soy_atoms_position_get_z(so2->g);
    } else {
        PyErr_SetString(PyExc_TypeError, "unsupported operand type(s)");
        return NULL;
    }

    // build args with calculated values
    args = Py_BuildValue("((fff))", o1x + o2x, o1y + o2y, o1z + o2z);

    // create result object
    result = tp_new(&PySoy_atoms_Position_Type, args, NULL);

    // decref args tuple and tmp
    Py_DECREF(args);

    // return calculated result
    return result;
}
Exemplo n.º 6
0
static int
complex_coerce(PyObject **pv, PyObject **pw)
{
	Py_complex cval;
	cval.imag = 0.;
	if (PyInt_Check(*pw)) {
		cval.real = (double)PyInt_AsLong(*pw);
		*pw = PyComplex_FromCComplex(cval);
		Py_INCREF(*pv);
		return 0;
	}
	else if (PyLong_Check(*pw)) {
		cval.real = PyLong_AsDouble(*pw);
		if (cval.real == -1.0 && PyErr_Occurred())
			return -1;
		*pw = PyComplex_FromCComplex(cval);
		Py_INCREF(*pv);
		return 0;
	}
	else if (PyFloat_Check(*pw)) {
		cval.real = PyFloat_AsDouble(*pw);
		*pw = PyComplex_FromCComplex(cval);
		Py_INCREF(*pv);
		return 0;
	}
	else if (PyComplex_Check(*pw)) {
		Py_INCREF(*pv);
		Py_INCREF(*pw);
		return 0;
	}
	return 1; /* Can't do it */
}
Exemplo n.º 7
0
static int
to_complex(PyObject **pobj, Py_complex *pc)
{
    PyObject *obj = *pobj;

    pc->real = pc->imag = 0.0;
    if (PyInt_Check(obj)) {
        pc->real = PyInt_AS_LONG(obj);
        return 0;
    }
    if (PyLong_Check(obj)) {
        pc->real = PyLong_AsDouble(obj);
        if (pc->real == -1.0 && PyErr_Occurred()) {
            *pobj = NULL;
            return -1;
        }
        return 0;
    }
    if (PyFloat_Check(obj)) {
        pc->real = PyFloat_AsDouble(obj);
        return 0;
    }
    Py_INCREF(Py_NotImplemented);
    *pobj = Py_NotImplemented;
    return -1;
}
Exemplo n.º 8
0
int EnumValue_coerce(PyObject **pv, PyObject **pw)
{
    if (PyInt_Check(*pw))
    {
        long x = PyInt_AsLong(*pw);
        *pw = NewEnumValue(NULL, x);
        Py_INCREF(*pv);
        return 0;
    }
    else if (PyLong_Check(*pw))
    {
        double x = PyLong_AsDouble(*pw);
        if (x == -1.0 && PyErr_Occurred())
            return -1;
        *pw = NewEnumValue(NULL, (long)x);
        Py_INCREF(*pv);
        return 0;
    }
    else if (PyFloat_Check(*pw))
    {
        double x = PyFloat_AsDouble(*pw);
        *pw = NewEnumValue(NULL, (long)x);
        Py_INCREF(*pv);
        return 0;
    }
    else if (IsEnumValue(*pw))
    {
        Py_INCREF(*pv);
        Py_INCREF(*pw);
        return 0;
    }
    return 1; // Can't do it
}
SWIGINTERN int
  SWIG_AsVal_double(PyObject *obj, double *val)
{
  if (PyFloat_Check(obj)) {
    if (val) *val = PyFloat_AS_DOUBLE(obj);
    return 1;
  }  
  if (PyInt_Check(obj)) {
    if (val) *val = PyInt_AS_LONG(obj);
    return 1;
  }
  if (PyLong_Check(obj)) {
    double v = PyLong_AsDouble(obj);
    if (!PyErr_Occurred()) {
      if (val) *val = v;
      return 1;
    } else {
      if (!val) PyErr_Clear();
      return 0;
    }
  }
  if (val) {
    SWIG_type_error("double", obj);
  }
  return 0;
}
Exemplo n.º 10
0
template<> double fromPy<double>(PyObject* obj) {
#if PY_MAJOR_VERSION <= 2
    if (PyInt_Check(obj)) return PyInt_AsLong(obj);
#endif
    if (PyFloat_Check(obj)) return PyFloat_AsDouble(obj);
    if (PyLong_Check(obj)) return PyLong_AsDouble(obj);
    errMsg("argument is not a double");    
}
Exemplo n.º 11
0
int convert_2d_pylist( PyObject *list, double ***array, int *rows, int *cols ) {
    int i, j;
    PyObject *item;
    PyObject *num;
    // Is the object a list?
    if (!PyList_Check(list))
        return -1;
    *rows = PySequence_Length(list);
    
    // Does the list contain elements?
    if (*rows < 0) {
        return -1;
    }

    item = PySequence_GetItem(list,0);
    if(!PySequence_Check(item)) {
        return -1;
    }
    *cols = PySequence_Length(item);
    if(*cols < 0) {
        return -1;
    }
    Py_DECREF(item);

    *array = (double **)malloc(*rows * sizeof(double*) );
    for( i = 0; i < *rows; i++ ) {
        (*array)[i] = (double*)malloc(*cols*sizeof(double));
    }
        
    
    for( i = 0; i < *rows; i++ ) {
        item = PySequence_GetItem(list,i);
        if(!PySequence_Check(item)) {
            return -1;
        }
        if( PySequence_Length(item) != *cols) {
            return -1;
        }
        for( j = 0; j < *cols; j++ ) {
            num = PySequence_GetItem( item, j );
            if (PyInt_Check(num)) {
                (*array)[i][j] = (double)PyInt_AsLong( num );
            }
            else if (PyLong_Check(num)) {
                (*array)[i][j] = PyLong_AsDouble( num );
            }
            else if (PyFloat_Check(num)) {
                (*array)[i][j] = PyFloat_AsDouble( num );
            }
            else {
                return -1;
            }
        }
        Py_DECREF(item);        
    }
    return 0;
}
 void PW_EXPORT _extract(PyObject *obj, float &val)
 {
     if (PyInt_Check(obj))
         val = (float)PyInt_AS_LONG(obj);
     else if (PyLong_Check(obj))
         val = (float)PyLong_AsDouble(obj);
     else
         val = (float)PyFloat_AsDouble(obj);
 }
 void _extract(PyObject *obj, double &val)
 {
     if (PyInt_Check(obj))
         val = (double)PyInt_AS_LONG(obj);
     else if (PyLong_Check(obj))
         val = PyLong_AsDouble(obj);
     else
         val = PyFloat_AsDouble(obj);
 }
Exemplo n.º 14
0
static PyObject *
View2DAttributes_mul(PyObject *v, PyObject *w)
{
    PyObject *retval = NewView2DAttributes(0);
    View2DAttributes *c = PyView2DAttributes_FromPyObject(retval);

    View2DAttributes *a;
    double val = 1.;
    bool arg1isObject = PyView2DAttributes_Check(v);
    bool arg2isObject = PyView2DAttributes_Check(w);

    if(arg1isObject && arg2isObject)
    {
        return NULL;
    }
    else
    {
        PyObject *num;

        if(arg1isObject)
        {
            a = ((View2DAttributesObject *)v)->data;
            num = w;
        }
        else
        {
            a = ((View2DAttributesObject *)w)->data;
            num = v;
        }

        if(PyFloat_Check(num))
            val = PyFloat_AS_DOUBLE(num);
        else if(PyInt_Check(num))
            val = double(PyInt_AS_LONG(num));
        else if(PyLong_Check(num))
            val = PyLong_AsDouble(num);
        else
        {
            cerr << "MUL: Expected numeric argument is not a number!" << endl;
        }

        c->GetWindowCoords()[0] = a->GetWindowCoords()[0] * val;
        c->GetWindowCoords()[1] = a->GetWindowCoords()[1] * val;
        c->GetWindowCoords()[2] = a->GetWindowCoords()[2] * val;
        c->GetWindowCoords()[3] = a->GetWindowCoords()[3] * val;

        c->GetViewportCoords()[0] = a->GetViewportCoords()[0] * val;
        c->GetViewportCoords()[1] = a->GetViewportCoords()[1] * val;
        c->GetViewportCoords()[2] = a->GetViewportCoords()[2] * val;
        c->GetViewportCoords()[3] = a->GetViewportCoords()[3] * val;

        c->SetFullFrameAutoThreshold(a->GetFullFrameAutoThreshold() * val);
        c->SetFullFrameActivationMode(a->GetFullFrameActivationMode());
    }

    return retval;
}
Exemplo n.º 15
0
  std::complex<T> complex_from_python(PyObject* p, boost::python::type<T>)
  {
      if (PyInt_Check(p)) return std::complex<T>(PyInt_AS_LONG(p));
      if (PyLong_Check(p)) return std::complex<T>(PyLong_AsDouble(p));
      if (PyFloat_Check(p)) return std::complex<T>(PyFloat_AS_DOUBLE(p));

      expect_complex(p);

      return std::complex<T>(
        static_cast<T>(PyComplex_RealAsDouble(p)),
        static_cast<T>(PyComplex_ImagAsDouble(p)));
  }
Exemplo n.º 16
0
template<> int fromPy<int>(PyObject *obj) {
#if PY_MAJOR_VERSION <= 2
    if (PyInt_Check(obj)) return PyInt_AsLong(obj);
#endif
    if (PyLong_Check(obj)) return PyLong_AsDouble(obj);
    if (PyFloat_Check(obj)) {
        double a = PyFloat_AsDouble(obj);
        if (fabs(a-floor(a+0.5)) > 1e-5)
            errMsg("argument is not an int");    
        return (int) (a+0.5);
    }
    errMsg("argument is not an int");       
}
Exemplo n.º 17
0
static PyObject*
validate_float_promote( Member* member, PyObject* owner, PyObject* oldvalue, PyObject* newvalue )
{
    if( PyFloat_Check( newvalue ) )
        return newref( newvalue );
    if( PyInt_Check( newvalue ) )
        return PyFloat_FromDouble( static_cast<double>( PyInt_AS_LONG( newvalue ) ) );
    if( PyLong_Check( newvalue ) )
    {
        double val = PyLong_AsDouble( newvalue );
        if( val < 0.0 && PyErr_Occurred() )
            return 0;
        return PyFloat_FromDouble( val );
    }
    return validate_type_fail( member, owner, newvalue, "float" );
}
Exemplo n.º 18
0
static PyObject *
Text3DObject_SetRotations(PyObject *self, PyObject *args)
{
    Text3DObjectObject *obj = (Text3DObjectObject *)self;

    double dvals[3];
    dvals[0] = obj->data->GetRotations()[0];
    dvals[1] = obj->data->GetRotations()[1];
    dvals[2] = obj->data->GetRotations()[2];
    if(!PyArg_ParseTuple(args, "ddd", &dvals[0], &dvals[1], &dvals[2]))
    {
        PyObject     *tuple;
        if(!PyArg_ParseTuple(args, "O", &tuple))
            return NULL;

        if(PyTuple_Check(tuple))
        {
            if(PyTuple_Size(tuple) != 3)
                return NULL;

            PyErr_Clear();
            for(int i = 0; i < PyTuple_Size(tuple); ++i)
            {
                PyObject *item = PyTuple_GET_ITEM(tuple, i);
                if(PyFloat_Check(item))
                    dvals[i] = PyFloat_AS_DOUBLE(item);
                else if(PyInt_Check(item))
                    dvals[i] = double(PyInt_AS_LONG(item));
                else if(PyLong_Check(item))
                    dvals[i] = PyLong_AsDouble(item);
                else
                    dvals[i] = 0.;
            }
        }
        else
            return NULL;
    }

    // Mark the rotations in the object as modified.
    obj->data->SetRotations(dvals);

/*CUSTOM*/
    UpdateAnnotationHelper(obj->data);

    Py_INCREF(Py_None);
    return Py_None;
}
Exemplo n.º 19
0
/*static*/ PyObject *
ColorAttribute_SetColor(PyObject *self, PyObject *args)
{
    ColorAttributeObject *obj = (ColorAttributeObject *)self;

    unsigned char *cvals = obj->data->GetColor();
    if(!PyArg_ParseTuple(args, "cccc", &cvals[0], &cvals[1], &cvals[2], &cvals[3]))
    {
        PyObject     *tuple;
        if(!PyArg_ParseTuple(args, "O", &tuple))
            return NULL;

        if(PyTuple_Check(tuple))
        {
            if(PyTuple_Size(tuple) != 4)
                return NULL;

            PyErr_Clear();
            for(int i = 0; i < PyTuple_Size(tuple); ++i)
            {
                int c;
                PyObject *item = PyTuple_GET_ITEM(tuple, i);
                if(PyFloat_Check(item))
                    c = int(PyFloat_AS_DOUBLE(item));
                else if(PyInt_Check(item))
                    c = int(PyInt_AS_LONG(item));
                else if(PyLong_Check(item))
                    c = int(PyLong_AsDouble(item));
                else
                    c = 0;

                if(c < 0) c = 0;
                if(c > 255) c = 255;
                cvals[i] = (unsigned char)(c);
            }
        }
        else
            return NULL;
    }

    // Mark the color in the object as modified.
    obj->data->SelectColor();

    Py_INCREF(Py_None);
    return Py_None;
}
Exemplo n.º 20
0
static PyObject *
TimeSliderObject_SetPosition(PyObject *self, PyObject *args)
{
    TimeSliderObjectObject *obj = (TimeSliderObjectObject *)self;

    double *dvals = obj->data->GetPosition();
    if(!PyArg_ParseTuple(args, "dd", &dvals[0], &dvals[1]))
    {
        PyObject     *tuple;
        if(!PyArg_ParseTuple(args, "O", &tuple))
            return NULL;

        if(PyTuple_Check(tuple))
        {
            if(PyTuple_Size(tuple) != 2)
                return NULL;

            PyErr_Clear();
            for(int i = 0; i < PyTuple_Size(tuple); ++i)
            {
                PyObject *item = PyTuple_GET_ITEM(tuple, i);
                if(PyFloat_Check(item))
                    dvals[i] = (PyFloat_AS_DOUBLE(item));
                else if(PyInt_Check(item))
                    dvals[i] = double(PyInt_AS_LONG(item));
                else if(PyLong_Check(item))
                    dvals[i] = (PyLong_AsDouble(item));
                else
                    dvals[i] = 0.;
            }
        }
        else
            return NULL;
    }

    // Mark the position in the object as modified.
    obj->data->SelectPosition();
/* CUSTOM */
    UpdateAnnotationHelper(obj->data);

    Py_INCREF(Py_None);
    return Py_None;
}
Exemplo n.º 21
0
PYTHON_METHOD_DEFINITION(ptNotify, addVarNumber, args)
{
    char* name;
    PyObject* number = NULL;
    if (!PyArg_ParseTuple(args, "s|O", &name, &number))
    {
        PyErr_SetString(PyExc_TypeError, "addVarNumber expects a string and optional number");
        PYTHON_RETURN_ERROR;
    }

    if (number == NULL || number == Py_None)
        self->fThis->AddVarNull(name);
    else if (PyInt_Check(number))
        self->fThis->AddVarNumber(name, static_cast<int32_t>(PyInt_AsLong(number)));
    else if (PyLong_Check(number))
    {
        // try as int first
        long i = PyLong_AsLong(number);
        if (!PyErr_Occurred())
        {
            self->fThis->AddVarNumber(name, static_cast<int32_t>(i));
        }
        else
        {
            // OverflowError, try float
            PyErr_Clear();
            self->fThis->AddVarNumber(name, (float)PyLong_AsDouble(number));
        }
    }
    else if (PyNumber_Check(number))
    {
        PyObject* f = PyNumber_Float(number);
        self->fThis->AddVarNumber(name, (float)PyFloat_AsDouble(f));
        Py_DECREF(f);
    } 
    else
    {
        PyErr_SetString(PyExc_TypeError, "addVarNumber expects a string and optional number");
        PYTHON_RETURN_ERROR;
    }
    PYTHON_RETURN_NONE;
}
Exemplo n.º 22
0
/* adapted from Python source, floatobject.c:convert_to_double */
int
convert_to_double(PyObject *obj, double *dbl)
{
    if (PyFloat_Check(obj)) {
        *dbl = PyFloat_AS_DOUBLE(obj);
#ifndef IS_PY3K
    } else if (PyInt_Check(obj)) {
        *dbl = (double)PyInt_AS_LONG(obj);
#endif
    } else if (PyLong_Check(obj)) {
        *dbl = PyLong_AsDouble(obj);
        if (*dbl == -1.0 && PyErr_Occurred()) {
            return 0;
        }
    } else {
        PyErr_SetString(PyExc_ValueError, "Expected a number");
        return 0;
    }
    return 1;
}
/*static*/ PyObject *
ReplicateAttributes_SetNewPeriodicOrigin(PyObject *self, PyObject *args)
{
    ReplicateAttributesObject *obj = (ReplicateAttributesObject *)self;

    double *dvals = obj->data->GetNewPeriodicOrigin();
    if(!PyArg_ParseTuple(args, "ddd", &dvals[0], &dvals[1], &dvals[2]))
    {
        PyObject     *tuple;
        if(!PyArg_ParseTuple(args, "O", &tuple))
            return NULL;

        if(PyTuple_Check(tuple))
        {
            if(PyTuple_Size(tuple) != 3)
                return NULL;

            PyErr_Clear();
            for(int i = 0; i < PyTuple_Size(tuple); ++i)
            {
                PyObject *item = PyTuple_GET_ITEM(tuple, i);
                if(PyFloat_Check(item))
                    dvals[i] = PyFloat_AS_DOUBLE(item);
                else if(PyInt_Check(item))
                    dvals[i] = double(PyInt_AS_LONG(item));
                else if(PyLong_Check(item))
                    dvals[i] = PyLong_AsDouble(item);
                else
                    dvals[i] = 0.;
            }
        }
        else
            return NULL;
    }

    // Mark the newPeriodicOrigin in the object as modified.
    obj->data->SelectNewPeriodicOrigin();

    Py_INCREF(Py_None);
    return Py_None;
}
Exemplo n.º 24
0
/*static*/ PyObject *
WindowInformation_SetWindowSize(PyObject *self, PyObject *args)
{
    WindowInformationObject *obj = (WindowInformationObject *)self;

    int *ivals = obj->data->GetWindowSize();
    if(!PyArg_ParseTuple(args, "ii", &ivals[0], &ivals[1]))
    {
        PyObject     *tuple;
        if(!PyArg_ParseTuple(args, "O", &tuple))
            return NULL;

        if(PyTuple_Check(tuple))
        {
            if(PyTuple_Size(tuple) != 2)
                return NULL;

            PyErr_Clear();
            for(int i = 0; i < PyTuple_Size(tuple); ++i)
            {
                PyObject *item = PyTuple_GET_ITEM(tuple, i);
                if(PyFloat_Check(item))
                    ivals[i] = int(PyFloat_AS_DOUBLE(item));
                else if(PyInt_Check(item))
                    ivals[i] = int(PyInt_AS_LONG(item));
                else if(PyLong_Check(item))
                    ivals[i] = int(PyLong_AsDouble(item));
                else
                    ivals[i] = 0;
            }
        }
        else
            return NULL;
    }

    // Mark the windowSize in the object as modified.
    obj->data->SelectWindowSize();

    Py_INCREF(Py_None);
    return Py_None;
}
Exemplo n.º 25
0
/*static*/ PyObject *
WindowInformation_SetExtents(PyObject *self, PyObject *args)
{
    WindowInformationObject *obj = (WindowInformationObject *)self;

    double *dvals = obj->data->GetExtents();
    if(!PyArg_ParseTuple(args, "dddddd", &dvals[0], &dvals[1], &dvals[2], &dvals[3], &dvals[4], &dvals[5]))
    {
        PyObject     *tuple;
        if(!PyArg_ParseTuple(args, "O", &tuple))
            return NULL;

        if(PyTuple_Check(tuple))
        {
            if(PyTuple_Size(tuple) != 6)
                return NULL;

            PyErr_Clear();
            for(int i = 0; i < PyTuple_Size(tuple); ++i)
            {
                PyObject *item = PyTuple_GET_ITEM(tuple, i);
                if(PyFloat_Check(item))
                    dvals[i] = PyFloat_AS_DOUBLE(item);
                else if(PyInt_Check(item))
                    dvals[i] = double(PyInt_AS_LONG(item));
                else if(PyLong_Check(item))
                    dvals[i] = PyLong_AsDouble(item);
                else
                    dvals[i] = 0.;
            }
        }
        else
            return NULL;
    }

    // Mark the extents in the object as modified.
    obj->data->SelectExtents();

    Py_INCREF(Py_None);
    return Py_None;
}
/*static*/ PyObject *
RadialResampleAttributes_SetCenter(PyObject *self, PyObject *args)
{
    RadialResampleAttributesObject *obj = (RadialResampleAttributesObject *)self;

    float *fvals = obj->data->GetCenter();
    if(!PyArg_ParseTuple(args, "fff", &fvals[0], &fvals[1], &fvals[2]))
    {
        PyObject     *tuple;
        if(!PyArg_ParseTuple(args, "O", &tuple))
            return NULL;

        if(PyTuple_Check(tuple))
        {
            if(PyTuple_Size(tuple) != 3)
                return NULL;

            PyErr_Clear();
            for(int i = 0; i < PyTuple_Size(tuple); ++i)
            {
                PyObject *item = PyTuple_GET_ITEM(tuple, i);
                if(PyFloat_Check(item))
                    fvals[i] = float(PyFloat_AS_DOUBLE(item));
                else if(PyInt_Check(item))
                    fvals[i] = float(PyInt_AS_LONG(item));
                else if(PyLong_Check(item))
                    fvals[i] = float(PyLong_AsDouble(item));
                else
                    fvals[i] = 0.;
            }
        }
        else
            return NULL;
    }

    // Mark the center in the object as modified.
    obj->data->SelectCenter();

    Py_INCREF(Py_None);
    return Py_None;
}
/*static*/ PyObject *
TransferFunctionWidget_SetPosition(PyObject *self, PyObject *args)
{
    TransferFunctionWidgetObject *obj = (TransferFunctionWidgetObject *)self;

    float *fvals = obj->data->GetPosition();
    if(!PyArg_ParseTuple(args, "ffffffff", &fvals[0], &fvals[1], &fvals[2], &fvals[3], &fvals[4], &fvals[5], &fvals[6], &fvals[7]))
    {
        PyObject     *tuple;
        if(!PyArg_ParseTuple(args, "O", &tuple))
            return NULL;

        if(PyTuple_Check(tuple))
        {
            if(PyTuple_Size(tuple) != 8)
                return NULL;

            PyErr_Clear();
            for(int i = 0; i < PyTuple_Size(tuple); ++i)
            {
                PyObject *item = PyTuple_GET_ITEM(tuple, i);
                if(PyFloat_Check(item))
                    fvals[i] = float(PyFloat_AS_DOUBLE(item));
                else if(PyInt_Check(item))
                    fvals[i] = float(PyInt_AS_LONG(item));
                else if(PyLong_Check(item))
                    fvals[i] = float(PyLong_AsDouble(item));
                else
                    fvals[i] = 0.;
            }
        }
        else
            return NULL;
    }

    // Mark the Position in the object as modified.
    obj->data->SelectPosition();

    Py_INCREF(Py_None);
    return Py_None;
}
Exemplo n.º 28
0
static double *toDoubleArray(PyObject *arg, int *len)
{
    if (PySequence_Check(arg))
    {
        *len = PySequence_Size(arg);
        double *array = new double[*len + 1];

        for (int i = 0; i < *len; i++) {
            PyObject *obj = PySequence_GetItem(arg, i);

            if (PyFloat_Check(obj))
            {
                array[i] = PyFloat_AsDouble(obj);
                Py_DECREF(obj);
            }
#if PY_MAJOR_VERSION < 3
            else if (PyInt_Check(obj))
            {
                array[i] = (double) PyInt_AsLong(obj);
                Py_DECREF(obj);
            }
#endif
            else if (PyLong_Check(obj))
            {
                array[i] = PyLong_AsDouble(obj);
                Py_DECREF(obj);
            }
            else
            {
                Py_DECREF(obj);
                delete[] array;
                return NULL;
            }
        }

        return array;
    }

    return NULL;
}
Exemplo n.º 29
0
static PyObject *aqbanking_Account_transactions(aqbanking_Account* self, PyObject *args, PyObject *kwds)
{
	int rv;
	double tmpDateTime = 0;
	const char *bank_code;
	const char *account_no;
#if PY_VERSION_HEX >= 0x03030000
	bank_code = PyUnicode_AsUTF8(self->bank_code);
	account_no = PyUnicode_AsUTF8(self->no);
#else
	PyObject *s = _PyUnicode_AsDefaultEncodedString(self->bank_code, NULL);
	bank_code = PyBytes_AS_STRING(s);
	s = _PyUnicode_AsDefaultEncodedString(self->no, NULL);
	account_no = PyBytes_AS_STRING(s);
#endif
	GWEN_TIME *gwTime;
	const char *dateFrom=NULL, *dateTo=NULL;
	static char *kwlist[] = {"dateFrom", "dateTo", NULL};
	if (! PyArg_ParseTupleAndKeywords(args, kwds, "|ss", kwlist, &dateFrom, &dateTo))
	{
		return NULL;
	}

	AB_ACCOUNT *a;
	AB_JOB *job = 0;
	AB_JOB_LIST2 *jl = 0;
	AB_IMEXPORTER_CONTEXT *ctx = 0;
	AB_IMEXPORTER_ACCOUNTINFO *ai;
	/*aqbanking_Transaction *trans = NULL;*/
	PyObject *transList = PyList_New(0);

	// Valid data set?
	if (self->no == NULL)
	{
		PyErr_SetString(PyExc_AttributeError, "no");
	}
	if (self->bank_code == NULL)
	{
		PyErr_SetString(PyExc_AttributeError, "bank_code");
	}

	// Initialize aqbanking.
	rv = AB_create(self);
	if (rv > 0)
	{
		Py_DECREF(transList);
		return NULL;
	}

	// Let us find the account!
	a = AB_Banking_GetAccountByCodeAndNumber(self->ab, bank_code, account_no);
	if (!a)
	{
		PyErr_SetString(AccountNotFound, "Could not find the given account! ");
		Py_DECREF(transList);
		return NULL;
	}

	// Create job and execute it.
	job = AB_JobGetTransactions_new(a);
	if (dateFrom != NULL)
	{
		gwTime = GWEN_Time_fromString(dateFrom, "YYYYMMDD");
		AB_JobGetTransactions_SetFromTime(job, gwTime);
	}
	if (dateTo != NULL)
	{
		gwTime = GWEN_Time_fromString(dateTo, "YYYYMMDD");
		AB_JobGetTransactions_SetToTime(job, gwTime);
	}
	// Check for availability
	rv = AB_Job_CheckAvailability(job);
	if (rv) {
		PyErr_SetString(ExecutionFailed, "Transaction retrieval is not supported!");
		Py_DECREF(transList);
		return NULL;
	}

	jl = AB_Job_List2_new();
	AB_Job_List2_PushBack(jl, job);
	ctx = AB_ImExporterContext_new();
	rv = AB_Banking_ExecuteJobs(self->ab, jl, ctx);

	if (rv)
	{
		PyErr_SetString(ExecutionFailed, "Could not retrieve transactions!");
		Py_DECREF(transList);
		return NULL;
	}

	// With success. No process the result.
	ai = AB_ImExporterContext_GetFirstAccountInfo (ctx);
	while(ai)
	{
		const AB_TRANSACTION *t;
		
		t = AB_ImExporterAccountInfo_GetFirstTransaction(ai);
		while(t) {
			const AB_VALUE *v;
			AB_TRANSACTION_STATUS state;

			v=AB_Transaction_GetValue(t);
			if (v) {
				const GWEN_STRINGLIST *sl;
				const GWEN_TIME *tdtime;
				const char *purpose;
				const char *remoteName;
				aqbanking_Transaction *trans = (aqbanking_Transaction*) PyObject_CallObject((PyObject *) &aqbanking_TransactionType, NULL);

				/* The purpose (memo field) might contain multiple lines.
				 * Therefore AqBanking stores the purpose in a string list
				 * of which the first entry is used in this tutorial */
				sl = AB_Transaction_GetPurpose(t);
				if (sl)
				{
					purpose = GWEN_StringList_FirstString(sl);
					if (purpose == NULL) {
						purpose = "";
					}
				}
				else
				{
					purpose = "";
				}

#ifdef DEBUGSTDERR
				fprintf(stderr, "[%-10d]: [%-10s/%-10s][%-10s/%-10s] %-32s (%.2f %s)\n",
						AB_Transaction_GetUniqueId(t),
						AB_Transaction_GetRemoteIban(t),
						AB_Transaction_GetRemoteBic(t),
						AB_Transaction_GetRemoteAccountNumber(t),
						AB_Transaction_GetRemoteBankCode(t),
						purpose,
						AB_Value_GetValueAsDouble(v),
						AB_Value_GetCurrency(v)
				);
#endif

				tdtime = AB_Transaction_GetDate(t);
				tmpDateTime = PyLong_AsDouble(PyLong_FromSize_t(GWEN_Time_Seconds(tdtime)));
				trans->date = PyDate_FromTimestamp(Py_BuildValue("(O)", PyFloat_FromDouble(tmpDateTime)));
				tdtime = AB_Transaction_GetValutaDate(t);
				tmpDateTime = PyLong_AsDouble(PyLong_FromSize_t(GWEN_Time_Seconds(tdtime)));
				trans->valutaDate = PyDate_FromTimestamp(Py_BuildValue("(O)", PyFloat_FromDouble(tmpDateTime)));
				trans->purpose = PyUnicode_FromString(purpose);

				// Local user
				if (AB_Transaction_GetLocalAccountNumber(t) == NULL) {
					trans->localAccount = Py_None;
					Py_INCREF(Py_None);
				} else {
					trans->localAccount = PyUnicode_FromString(AB_Transaction_GetLocalAccountNumber(t));
				}
				if (AB_Transaction_GetLocalBankCode(t) == NULL) {
					trans->localBank = Py_None;
					Py_INCREF(Py_None);
				} else {
					trans->localBank = PyUnicode_FromString(AB_Transaction_GetLocalBankCode(t));
				}
				if (AB_Transaction_GetLocalIban(t) == NULL) {
					trans->localIban = Py_None;
					Py_INCREF(Py_None);
				} else {
					trans->localIban = PyUnicode_FromString(AB_Transaction_GetLocalIban(t));
				}
				if (AB_Transaction_GetLocalBic(t) == NULL) {
					trans->localBic = Py_None;
					Py_INCREF(Py_None);
				} else {
					trans->localBic = PyUnicode_FromString(AB_Transaction_GetLocalBic(t));
				}
				if (AB_Transaction_GetLocalName(t) == NULL) {
					trans->localName = Py_None;
					Py_INCREF(Py_None);
				} else {
				        trans->localName = PyUnicode_FromString(AB_Transaction_GetLocalName(t));
				}

				// Remote user
				if (AB_Transaction_GetRemoteAccountNumber(t) == NULL) {
					trans->remoteAccount = Py_None;
					Py_INCREF(Py_None);
				} else {
					trans->remoteAccount = PyUnicode_FromString(AB_Transaction_GetRemoteAccountNumber(t));
				}
				if (AB_Transaction_GetRemoteBankCode(t) == NULL) {
					trans->remoteBank = Py_None;
					Py_INCREF(Py_None);
				} else {
					trans->remoteBank = PyUnicode_FromString(AB_Transaction_GetRemoteBankCode(t));
				}
				if (AB_Transaction_GetRemoteIban(t) == NULL) {
					trans->remoteIban = Py_None;
					Py_INCREF(Py_None);
				} else {
					trans->remoteIban = PyUnicode_FromString(AB_Transaction_GetRemoteIban(t));
				}
				if (AB_Transaction_GetRemoteBic(t) == NULL) {
					trans->remoteBic = Py_None;
					Py_INCREF(Py_None);
				} else {
					trans->remoteBic = PyUnicode_FromString(AB_Transaction_GetRemoteBic(t));
				}
				if (AB_Transaction_GetRemoteName(t) == NULL) {
					trans->remoteName = Py_None;
					Py_INCREF(Py_None);
				} else {
					sl = AB_Transaction_GetRemoteName(t);
					remoteName = GWEN_StringList_FirstString(sl);
					if (remoteName == NULL) {
						trans->remoteName = Py_None;
					} else {
						trans->remoteName = PyUnicode_FromString(remoteName);
					}
				}

				trans->value = PyFloat_FromDouble(AB_Value_GetValueAsDouble(v));
				trans->currency = PyUnicode_FromString("EUR");
				trans->uniqueId = PyLong_FromLong(AB_Transaction_GetUniqueId(t));
				if (AB_Transaction_GetTransactionText(t) == NULL) {
					trans->transactionText = PyUnicode_FromString("");
				} else {
					trans->transactionText = PyUnicode_FromString(AB_Transaction_GetTransactionText(t));
				}
				trans->transactionCode = PyLong_FromLong(AB_Transaction_GetTransactionCode(t));
				trans->textKey = PyLong_FromLong(AB_Transaction_GetTextKey(t));
				trans->textKeyExt = PyLong_FromLong(AB_Transaction_GetTextKeyExt(t));
				if (AB_Transaction_GetMandateId(t) == NULL) {
					trans->sepaMandateId = Py_None;
				} else {
					trans->sepaMandateId = PyUnicode_FromString(AB_Transaction_GetMandateId(t));
				}
				if (AB_Transaction_GetCustomerReference(t) == NULL) {
					trans->customerReference = PyUnicode_FromString("");
				} else {
					trans->customerReference = PyUnicode_FromString(AB_Transaction_GetCustomerReference(t));
				}
				if (AB_Transaction_GetBankReference(t) == NULL) {
					trans->bankReference = PyUnicode_FromString("");
				} else {
					trans->bankReference = PyUnicode_FromString(AB_Transaction_GetBankReference(t));
				}
				if (AB_Transaction_GetEndToEndReference(t) == NULL) {
					trans->endToEndReference = PyUnicode_FromString("");
				} else {
					trans->endToEndReference = PyUnicode_FromString(AB_Transaction_GetEndToEndReference(t));
				}
				trans->state = 0;
				state = AB_Transaction_GetStatus(t);
				switch(state)
				{
					case AB_Transaction_StatusUnknown:
						trans->state = -1;
						break;
					case AB_Transaction_StatusNone:
						trans->state = 0;
						break;
					case AB_Transaction_StatusAccepted:
						trans->state = 1;
						break;
					case AB_Transaction_StatusRejected:
						trans->state = 2;
						break;
					case AB_Transaction_StatusPending:
						trans->state = 4;
						break;
					case AB_Transaction_StatusSending:
						trans->state = 8;
						break;
					case AB_Transaction_StatusAutoReconciled:
						trans->state = 16;
						break;
					case AB_Transaction_StatusManuallyReconciled:
						trans->state = 32;
						break;
					case AB_Transaction_StatusRevoked:
						trans->state = 64;
						break;
					case AB_Transaction_StatusAborted:
						trans->state = 128;
						break;
				}

				PyList_Append(transList, (PyObject *)trans);
				Py_DECREF(trans);
			}
			t = AB_ImExporterAccountInfo_GetNextTransaction(ai);
		} 
		ai = AB_ImExporterContext_GetNextAccountInfo(ctx);
	}

	// Free jobs.
	AB_Job_free(job);
	AB_Job_List2_free(jl);
	AB_ImExporterContext_free(ctx);

	// Exit aqbanking.
	rv = AB_free(self);
	if (rv > 0)
	{
		//Py_XDECREF(trans);
		Py_DECREF(transList);
		return NULL;
	}

	return transList;
}
/*static*/ PyObject *
ConstructDataBinningAttributes_SetBinType(PyObject *self, PyObject *args)
{
    ConstructDataBinningAttributesObject *obj = (ConstructDataBinningAttributesObject *)self;

    unsignedCharVector  &vec = obj->data->GetBinType();
    PyObject     *tuple;
    if(!PyArg_ParseTuple(args, "O", &tuple))
        return NULL;

    if(PyTuple_Check(tuple))
    {
        vec.resize(PyTuple_Size(tuple));
        for(int i = 0; i < PyTuple_Size(tuple); ++i)
        {
            int c;
            PyObject *item = PyTuple_GET_ITEM(tuple, i);
            if(PyFloat_Check(item))
                c = int(PyFloat_AS_DOUBLE(item));
            else if(PyInt_Check(item))
                c = int(PyInt_AS_LONG(item));
            else if(PyLong_Check(item))
                c = int(PyLong_AsDouble(item));
            else
                c = 0;

            if(c < 0) c = 0;
            if(c > 255) c = 255;
            vec[i] = (unsigned char)(c);
        }
    }
    else if(PyFloat_Check(tuple))
    {
        vec.resize(1);
        int c = int(PyFloat_AS_DOUBLE(tuple));
        if(c < 0) c = 0;
        if(c > 255) c = 255;
        vec[0] = (unsigned char)(c);
    }
    else if(PyInt_Check(tuple))
    {
        vec.resize(1);
        int c = int(PyInt_AS_LONG(tuple));
        if(c < 0) c = 0;
        if(c > 255) c = 255;
        vec[0] = (unsigned char)(c);
    }
    else if(PyLong_Check(tuple))
    {
        vec.resize(1);
        int c = PyLong_AsLong(tuple);
        if(c < 0) c = 0;
        if(c > 255) c = 255;
        vec[0] = (unsigned char)(c);
    }
    else
        return NULL;

    // Mark the binType in the object as modified.
    obj->data->SelectBinType();

    Py_INCREF(Py_None);
    return Py_None;
}