diff options
author | John Wiegley <johnw@newartisans.com> | 2009-01-28 17:17:44 -0400 |
---|---|---|
committer | John Wiegley <johnw@newartisans.com> | 2009-01-28 17:17:44 -0400 |
commit | 1aa1ff19773848a53a4c13c482c2b277169eff0a (patch) | |
tree | a3476799c6ed95f2eb834650775e88a97cff5b7c /python | |
parent | b205090bb76a6c8a80a745b19388aae90e296409 (diff) | |
download | fork-ledger-1aa1ff19773848a53a4c13c482c2b277169eff0a.tar.gz fork-ledger-1aa1ff19773848a53a4c13c482c2b277169eff0a.tar.bz2 fork-ledger-1aa1ff19773848a53a4c13c482c2b277169eff0a.zip |
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
Diffstat (limited to 'python')
-rw-r--r-- | python/py_value.cc | 295 |
1 files changed, 283 insertions, 12 deletions
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<value_t> py_value_0(const value_t& amount) { + return amount.value(); +} +boost::optional<value_t> py_value_1(const value_t& amount, + const boost::optional<datetime_t>& moment) { + return amount.value(moment); +} +boost::optional<value_t> py_value_2(const value_t& amount, + const boost::optional<datetime_t>& moment, + const boost::optional<commodity_t&>& 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<PyFileObject *>(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<bool>(func)(); - else if (PyInt_Check(func.ptr())) - return long(extract<int>(func)()); - else if (PyString_Check(func.ptr())) - return string_value(extract<string>(func)()); + .def("initialize", &value_t::initialize) + .staticmethod("initialize") + .def("shutdown", &value_t::shutdown) + .staticmethod("shutdown") +#endif + +#ifdef HAVE_GDTOA + .def(init<double>()) +#endif + .def(init<long>()) + .def(init<std::string>()) + .def(init<date_t>()) + .def(init<datetime_t>()) + .def(init<amount_t>()) +#if 0 + .def(init<balance_t>()) + .def(init<balance_pair_t>()) +#endif + + .def(init<value_t>()) + + .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<date_t> d(func); - if (d.check()) - return value_t(d()); - extract<datetime_t> 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<reference_existing_object>()) + + .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<value_t>(); #ifdef HAVE_GDTOA @@ -73,6 +341,9 @@ void export_value() #endif implicitly_convertible<long, value_t>(); implicitly_convertible<string, value_t>(); + implicitly_convertible<date_t, value_t>(); + implicitly_convertible<datetime_t, value_t>(); + implicitly_convertible<amount_t, value_t>(); #define EXC_TRANSLATE(type) \ register_exception_translator<type>(&exc_translate_ ## type); |