From 1aa1ff19773848a53a4c13c482c2b277169eff0a Mon Sep 17 00:00:00 2001 From: John Wiegley Date: Wed, 28 Jan 2009 17:17:44 -0400 Subject: Fully exported the value_t type to Python. Now this code works from a value expression: def myvalue(incoming): import ledger x = ledger.Value("100.23") print x.type() return incoming + x --- python/py_value.cc | 295 ++++++++++++++++++++++++++++++++++++++++++++++++++--- 1 file changed, 283 insertions(+), 12 deletions(-) (limited to 'python') diff --git a/python/py_value.cc b/python/py_value.cc index 419d635d..f1b36acd 100644 --- a/python/py_value.cc +++ b/python/py_value.cc @@ -41,6 +41,37 @@ namespace ledger { using namespace boost::python; +boost::optional py_value_0(const value_t& amount) { + return amount.value(); +} +boost::optional py_value_1(const value_t& amount, + const boost::optional& moment) { + return amount.value(moment); +} +boost::optional py_value_2(const value_t& amount, + const boost::optional& moment, + const boost::optional& in_terms_of) { + return amount.value(moment, in_terms_of); +} + +void py_set_string(value_t& amount, const string& str) { + return amount.set_string(str); +} + +void py_read_1(value_t& amount, object in) { + if (PyFile_Check(in.ptr())) { + pyifstream instr(reinterpret_cast(in.ptr())); + amount.read(instr); + } else { + PyErr_SetString(PyExc_IOError, + "Argument to amount.parse(file) is not a file object"); + } +} +void py_read_2(value_t& amount, const std::string& str) { + const char * p = str.c_str(); + amount.read(p); +} + #define EXC_TRANSLATOR(type) \ void exc_translate_ ## type(const type& err) { \ PyErr_SetString(PyExc_ArithmeticError, err.what()); \ @@ -50,22 +81,259 @@ EXC_TRANSLATOR(value_error) void export_value() { + class_< value_t > ("Value") #if 0 - if (PyBool_Check(func.ptr())) - return extract(func)(); - else if (PyInt_Check(func.ptr())) - return long(extract(func)()); - else if (PyString_Check(func.ptr())) - return string_value(extract(func)()); + .def("initialize", &value_t::initialize) + .staticmethod("initialize") + .def("shutdown", &value_t::shutdown) + .staticmethod("shutdown") +#endif + +#ifdef HAVE_GDTOA + .def(init()) +#endif + .def(init()) + .def(init()) + .def(init()) + .def(init()) + .def(init()) +#if 0 + .def(init()) + .def(init()) +#endif + + .def(init()) + + .def("is_equal_to", &value_t::is_equal_to) + .def("is_less_than", &value_t::is_less_than) + .def("is_greater_than", &value_t::is_greater_than) + + .def(self == self) + .def(self == long()) + .def(long() == self) +#ifdef HAVE_GDTOA + .def(self == double()) + .def(double() == self) +#endif + + .def(self != self) + .def(self != long()) + .def(long() != self) +#ifdef HAVE_GDTOA + .def(self != double()) + .def(double() != self) +#endif + + .def(! self) + + .def(self < self) + .def(self < long()) + .def(long() < self) +#ifdef HAVE_GDTOA + .def(self < double()) + .def(double() < self) +#endif + + .def(self <= self) + .def(self <= long()) + .def(long() <= self) +#ifdef HAVE_GDTOA + .def(self <= double()) + .def(double() <= self) +#endif + + .def(self > self) + .def(self > long()) + .def(long() > self) +#ifdef HAVE_GDTOA + .def(self > double()) + .def(double() > self) +#endif - extract d(func); - if (d.check()) - return value_t(d()); - extract dt(func); - if (dt.check()) - return value_t(dt()); + .def(self >= self) + .def(self >= long()) + .def(long() >= self) +#ifdef HAVE_GDTOA + .def(self >= double()) + .def(double() >= self) #endif + .def(self += self) + .def(self += long()) +#ifdef HAVE_GDTOA + .def(self += double()) +#endif + + .def(self + self) + .def(self + long()) + .def(long() + self) +#ifdef HAVE_GDTOA + .def(self + double()) + .def(double() + self) +#endif + + .def(self -= self) + .def(self -= long()) +#ifdef HAVE_GDTOA + .def(self -= double()) +#endif + + .def(self - self) + .def(self - long()) + .def(long() - self) +#ifdef HAVE_GDTOA + .def(self - double()) + .def(double() - self) +#endif + + .def(self *= self) + .def(self *= long()) +#ifdef HAVE_GDTOA + .def(self *= double()) +#endif + + .def(self * self) + .def(self * long()) + .def(long() * self) +#ifdef HAVE_GDTOA + .def(self * double()) + .def(double() * self) +#endif + + .def(self /= self) + .def(self /= long()) +#ifdef HAVE_GDTOA + .def(self /= double()) +#endif + + .def(self / self) + .def(self / long()) + .def(long() / self) +#ifdef HAVE_GDTOA + .def(self / double()) + .def(double() / self) +#endif + + .def("add", &value_t::add, + return_value_policy()) + + .def("negate", &value_t::negate) + .def("in_place_negate", &value_t::in_place_negate) + .def("in_place_not", &value_t::in_place_not) + .def(- self) + + .def("abs", &value_t::abs) + .def("__abs__", &value_t::abs) + + .def("round", &value_t::round) + .def("in_place_round", &value_t::in_place_round) + .def("unround", &value_t::unround) + + .def("reduce", &value_t::reduce) + .def("in_place_reduce", &value_t::in_place_reduce) + + .def("value", py_value_0) + .def("value", py_value_1) + .def("value", py_value_2) + + .def("cost", &value_t::cost) + + .def("__nonzero__", &value_t::is_nonzero) + .def("is_nonzero", &value_t::is_nonzero) + .def("is_realzero", &value_t::is_realzero) + .def("is_zero", &value_t::is_zero) + .def("is_null", &value_t::is_null) + + .def("type", &value_t::type) + .def("is_type", &value_t::is_type) + + .def("is_boolean", &value_t::is_boolean) + .def("set_boolean", &value_t::set_boolean) + + .def("is_boolean", &value_t::is_boolean) + .def("set_boolean", &value_t::set_boolean) + + .def("is_datetime", &value_t::is_datetime) + .def("set_datetime", &value_t::set_datetime) + + .def("is_date", &value_t::is_date) + .def("set_date", &value_t::set_date) + + .def("is_long", &value_t::is_long) + .def("set_long", &value_t::set_long) + + .def("is_amount", &value_t::is_amount) + .def("set_amount", &value_t::set_amount) + + .def("is_balance", &value_t::is_balance) + .def("set_balance", &value_t::set_balance) + + .def("is_balance_pair", &value_t::is_balance_pair) + .def("set_balance_pair", &value_t::set_balance_pair) + + .def("is_string", &value_t::is_string) + .def("set_string", py_set_string) + + .def("is_sequence", &value_t::is_sequence) + .def("set_sequence", &value_t::set_sequence) + + .def("to_boolean", &value_t::to_boolean) + .def("to_long", &value_t::to_long) + .def("to_datetime", &value_t::to_datetime) + .def("to_date", &value_t::to_date) + .def("to_amount", &value_t::to_amount) + .def("to_balance", &value_t::to_balance) + .def("to_balance_pair", &value_t::to_balance_pair) + .def("to_string", &value_t::to_string) + .def("to_sequence", &value_t::to_sequence) + + .def("cast", &value_t::cast) + .def("in_place_cast", &value_t::in_place_cast) + + .def("simplify", &value_t::simplify) + .def("in_place_simplify", &value_t::in_place_simplify) + + .def("strip_annotations", &value_t::strip_annotations) + + // jww (2009-01-28): Allow for transparent exchanging with sequence + // protocol objects in Python too; and conversion to a list. +#if 0 + .def("__getitem__", &value_t::operator[]) +#endif + .def("push_back", &value_t::push_back) + .def("pop_back", &value_t::pop_back) + .def("size", &value_t::size) + + .def("label", &value_t::label) + + .def("dump", &value_t::dump) + .def("print", &value_t::print) + + .def("read", py_read_1) + .def("read", py_read_2) + .def("write", &value_t::write) + + .def("valid", &value_t::valid) + ; + + enum_< value_t::type_t >("ValueType") + .value("VOID", value_t::VOID) + .value("BOOLEAN", value_t::BOOLEAN) + .value("DATETIME", value_t::DATETIME) + .value("DATE", value_t::DATE) + .value("INTEGER", value_t::INTEGER) + .value("AMOUNT", value_t::AMOUNT) + .value("BALANCE", value_t::BALANCE) + .value("BALANCE_PAIR", value_t::BALANCE_PAIR) + .value("STRING", value_t::STRING) + .value("SEQUENCE", value_t::SEQUENCE) + .value("POINTER", value_t::POINTER) + ; + + scope().attr("NULL_VALUE") = NULL_VALUE; + scope().attr("string_value") = &string_value; + scope().attr("value_context") = &value_context; + register_optional_to_python(); #ifdef HAVE_GDTOA @@ -73,6 +341,9 @@ void export_value() #endif implicitly_convertible(); implicitly_convertible(); + implicitly_convertible(); + implicitly_convertible(); + implicitly_convertible(); #define EXC_TRANSLATE(type) \ register_exception_translator(&exc_translate_ ## type); -- cgit v1.2.3