File:  [CENS] / python / pyGiNaC / wrappers / lst.py
Revision 1.2: download - view: text, annotated - select for diffs - revision graph
Wed Apr 11 20:31:31 2001 UTC (16 years, 7 months ago) by pearu
Branches: MAIN
CVS tags: HEAD
Exposed lots of functions. Documented most important classes/functions. minor_version = 2

# This file is part of the PyGiNaC package.
# http://cens.ioc.ee/projects/pyginac/
#
# $Revision: 1.2 $
# $Id: lst.py,v 1.2 2001-04-11 20:31:31 pearu Exp $
#
# Copyright 2001 Pearu Peterson all rights reserved,
# Pearu Peterson <pearu@cens.ioc.ee>
# Permission to use, modify, and distribute this software is given under the
# terms of the LGPL.  See http://www.fsf.org
#
# NO WARRANTY IS EXPRESSED OR IMPLIED.  USE AT YOUR OWN RISK.
#
# TODO:
# Other Python list methods

depends = ['basic']

uses = ['ex']

#STARTPROTO

class lst(basic):
    """GiNaC list of expressions."""
    def __init__(self,*args):
        """lst() - construct empty GiNaC list.
    lst(*args) - construct GiNaC list from argument tuple (len(args)<=10)
    lst(seq)   - construct GiNaC list from an arbitrary Python sequence.
    lst(ex(lst(...))) - returns lst(...)
    """
    def append(self,b):
        pass
    def prepend(self,b):
        pass
    def __len__(self):
        """Number of members."""
    def __getitem__(self,key):
        pass
    def __setitem__(self,key,value):
        pass
    def __delitem__(self,key):
        pass

#ENDPROTO


wrapperclass = '''
class lst_w : public GiNaC::lst {
  PyObject * self;
public:
  lst_w(python::ref);

  lst_w(PyObject * self_): GiNaC::lst(), self(self_) {
    DEBUG_C("lst_w::lst_w()");
  }
/*
  lst_w(PyObject * self_, const GiNaC::lst & l): GiNaC::lst(l), self(self_) {
    DEBUG_C("lst_w::lst_w(lst)");
  }
*/
  lst_w(PyObject * self_, const GiNaC::ex & l)
  : GiNaC::lst(ex_to_lst_w(l)), self(self_) {
    DEBUG_C("lst_w::lst_w(ex)");
  }
  lst_w(PyObject * self_, python::ref obj)
  : GiNaC::lst(lst_w(obj)), self(self_) {
    DEBUG_C("lst_w::lst_w(ref)");
  }
'''

for i in range(1,nof_lst_params):
    wrapperclass +='''
  lst_w(PyObject * self_, '''+', '.join(['python::ref param'+`j+1` for j in range(i+1)])+\
    ') : GiNaC::lst('+', '.join(['ex_w(param'+`j+1`+')' for j in range(i+1)])+'''),
  self(self_) { DEBUG_C("lst_w::lst_w(%s*ex)"); }
'''%(i+1)
    
wrapperclass +='''
  ~lst_w() { DEBUG_C("lst_w::~lst_w()"); }
  void delitem (python::ref);
  inline void append_w(python::ref obj) { this->append(ex_w(obj)); }
  inline void prepend_w(python::ref obj) { this->prepend(ex_w(obj)); }
};

'''

protos = '''
'''

builder = '''
python::class_builder<lst_w> lst_w_class(this_module, "_lst_w");
python::class_builder<GiNaC::lst, lst_w> lst_class(this_module, "lst");
lst_class.declare_base(lst_w_class);
lst_class.declare_base(basic_class);
lst_py_class = python::as_object(lst_class.get_extension_class());
'''

constructors = '''
lst_class.def(python::constructor<>());
//lst_class.def(python::constructor<const GiNaC::lst &>());
lst_class.def(python::constructor<const GiNaC::ex &>());
lst_class.def(python::constructor<python::ref>());
'''

for i in range(1,nof_lst_params):
    constructors += '''lst_class.def(python::constructor<'''+\
    ', '.join(['python::ref' for j in range(i+1)])+\
    '''>());
'''

defs = '''
lst_class.def(&basic_w::python_str, "__str__");
lst_class.def(&basic_w::python_repr, "__repr__");

lst_class.def(&lst_w::append_w, "append");
lst_class.def(&lst_w::prepend_w, "prepend");
lst_class.def(&lst_w::nops, "__len__");
lst_class.def(&basic_w::getitem, "__getitem__");
lst_class.def(&basic_w::setitem, "__setitem__");
lst_class.def(&lst_w::delitem, "__delitem__");
'''

implementation = '''
EX_TO_BASIC(lst)

lst_w::lst_w(python::ref obj)
: GiNaC::lst(), self() {
  PyObject * o = obj.get();
  DEBUG_C("lst_w::lst_w(raw:ref)");
  if (PySequence_Check(o)) {
    std::size_t l = PySequence_Length(o);
    for(std::size_t i = 0; i < l; ++i)
      this->append(ex_w(python::ref(PySequence_GetItem(o,i))));
  } else {
    this->append(ex_w(python::ref(o)));
  }
}
void lst_w::delitem (python::ref index) {
  PyObject * o = index.get();
  int l = this->nops();
  int start, step_size, n_steps;
  DEBUG_M("lst::delitem(ref)");
  start = parse_subindex(o, &step_size, &n_steps, l);
  DEBUG_M("start="<<start<<",step_size="<<step_size<<",n_steps="<<n_steps)
  if (start < 0)
    throw python::error_already_set();
  if (n_steps==PseudoIndex || n_steps==RubberIndex) {
    PyErr_SetString(PyExc_NotImplementedError, "lst() delitem PseudoIndex|RubberIndex");
    throw python::error_already_set();
  }
  GiNaC::exlist::iterator viter = this->seq.begin();
  if (n_steps==SingleIndex) {
    for(int i = 0; i < start; ++i,++viter);
    this->seq.erase(viter);
  } else { /*
    int k = start;
    for(int i = 0; i < start; ++i,++viter);
    for (int i=0;i<n_steps;++i,k += step_size) {
      this->seq.erase(viter);
      if (i<n_steps)
        for(int m=0;m<step_size;++m,++viter);
    }
    */
    PyErr_SetString(PyExc_NotImplementedError, "lst() delitem slice");
    throw python::error_already_set();
  }
}
'''

FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>