diff options
author | John Wiegley <johnw@newartisans.com> | 2007-05-04 09:53:05 +0000 |
---|---|---|
committer | John Wiegley <johnw@newartisans.com> | 2008-04-13 03:38:37 -0400 |
commit | 0214a136c2b21b3cff3dfc94095d2badc3136b1b (patch) | |
tree | 94807dcc6ee60f650f880f5421367847c3b96192 | |
parent | 6c7e35dc17504c41bb177b3d1327c6b6cee4017d (diff) | |
download | fork-ledger-0214a136c2b21b3cff3dfc94095d2badc3136b1b.tar.gz fork-ledger-0214a136c2b21b3cff3dfc94095d2badc3136b1b.tar.bz2 fork-ledger-0214a136c2b21b3cff3dfc94095d2badc3136b1b.zip |
Work to get Python tests running again.
-rw-r--r-- | src/amount.h | 11 | ||||
-rw-r--r-- | src/commodity.h | 11 | ||||
-rw-r--r-- | src/py_amount.cc | 88 | ||||
-rw-r--r-- | src/py_commodity.cc | 93 | ||||
-rw-r--r-- | src/pyledger.cc | 12 | ||||
-rw-r--r-- | tests/python/numerics/BasicAmount.py | 18 |
6 files changed, 122 insertions, 111 deletions
diff --git a/src/amount.h b/src/amount.h index 280613d7..e15c9704 100644 --- a/src/amount.h +++ b/src/amount.h @@ -76,11 +76,9 @@ class amount_t ordered_field_operators<amount_t, unsigned long, ordered_field_operators<amount_t, double> > > > { + // jww (2007-05-03): Make this private, and then make + // ledger::initialize into a member function of session_t. public: - class bigint_t; - - typedef uint_least16_t precision_t; - /** * The initialize and shutdown methods ready the amount subsystem * for use. Normally they are called by `ledger::initialize' and @@ -89,6 +87,11 @@ public: static void initialize(); static void shutdown(); +public: + class bigint_t; + + typedef uint_least16_t precision_t; + /** * The default_pool is a static variable indicating which commodity * pool should be used when none is specified. diff --git a/src/commodity.h b/src/commodity.h index 7b9a7be4..72ded1e4 100644 --- a/src/commodity.h +++ b/src/commodity.h @@ -45,15 +45,16 @@ namespace ledger { +class annotated_commodity_t; + class commodity_t : public equality_comparable1<commodity_t, noncopyable> { + friend class commodity_pool_t; + class base_t : public noncopyable { - private: - friend class commodity_pool_t; - friend class commodity_t; - + public: typedef std::map<const moment_t, amount_t> history_map; typedef std::pair<const moment_t, amount_t> history_pair; @@ -285,7 +286,7 @@ public: virtual bool operator==(const commodity_t& comm) const; virtual bool operator==(const annotated_commodity_t& comm) const { - return *this == static_cast<commodity_t&>(comm); + return *this == static_cast<const commodity_t&>(comm); } commodity_t& referent() { diff --git a/src/py_amount.cc b/src/py_amount.cc index f152ceb1..c1dd7649 100644 --- a/src/py_amount.cc +++ b/src/py_amount.cc @@ -7,42 +7,20 @@ namespace ledger { using namespace boost::python; -void py_parse_1(amount_t& amount, const string& str, - unsigned char flags) { +void py_parse_1(amount_t& amount, const string& str, unsigned char flags) { amount.parse(str, flags); } void py_parse_2(amount_t& amount, const string& str) { amount.parse(str); } -amount_t py_round_1(amount_t& amount, unsigned int prec) { +amount_t py_round_1(const amount_t& amount, amount_t::precision_t prec) { return amount.round(prec); } -amount_t py_round_2(amount_t& amount) { +amount_t py_round_2(const amount_t& amount) { return amount.round(); } -#if 0 -struct commodity_updater_wrap : public commodity_base_t::updater_t -{ - PyObject * self; - commodity_updater_wrap(PyObject * self_) : self(self_) {} - - virtual void operator()(commodity_base_t& commodity, - const moment_t& moment, - const moment_t& date, - const moment_t& last, - amount_t& price) { - call_method<void>(self, "__call__", commodity, moment, date, last, price); - } -}; - -commodity_t * py_find_commodity(const string& symbol) -{ - return commodity_t::find(symbol); -} -#endif - #define EXC_TRANSLATOR(type) \ void exc_translate_ ## type(const type& err) { \ PyErr_SetString(PyExc_ArithmeticError, err.what()); \ @@ -214,15 +192,16 @@ void export_amount() .def("strip_annotations", &amount_t::strip_annotations) #if 0 - .def("price", &amount_t::price) - .def("date", &amount_t::date) - .def("tag", &amount_t::tag) + // jww (2007-05-03): This method depends on annotation_t + .def("annotation_details", &amount_t::annotation_details) #endif + // jww (2007-05-03): There are four versions of this method now .def("parse", py_parse_1) .def("parse", py_parse_2) #if 0 + // jww (2007-05-03): This method has two forms .def("parse_conversion", &amount_t::parse_conversion) .staticmethod("parse_conversion") #endif @@ -230,59 +209,6 @@ void export_amount() .def("valid", &amount_t::valid) ; -#if 0 - class_< commodity_base_t::updater_t, commodity_updater_wrap, - boost::noncopyable > - ("updater") - ; -#endif - - scope().attr("COMMODITY_STYLE_DEFAULTS") = COMMODITY_STYLE_DEFAULTS; - scope().attr("COMMODITY_STYLE_SUFFIXED") = COMMODITY_STYLE_SUFFIXED; - scope().attr("COMMODITY_STYLE_SEPARATED") = COMMODITY_STYLE_SEPARATED; - scope().attr("COMMODITY_STYLE_EUROPEAN") = COMMODITY_STYLE_EUROPEAN; - scope().attr("COMMODITY_STYLE_THOUSANDS") = COMMODITY_STYLE_THOUSANDS; - scope().attr("COMMODITY_STYLE_NOMARKET") = COMMODITY_STYLE_NOMARKET; - scope().attr("COMMODITY_STYLE_BUILTIN") = COMMODITY_STYLE_BUILTIN; - -#if 0 - class_< commodity_t > ("commodity") - .add_property("symbol", &commodity_t::symbol) - - .add_property("name", &commodity_t::name, &commodity_t::set_name) - .add_property("note", &commodity_t::note, &commodity_t::set_note) - .add_property("precision", &commodity_t::precision, - &commodity_t::set_precision) - .add_property("flags", &commodity_t::flags, &commodity_t::set_flags) - .add_property("add_flags", &commodity_t::add_flags) - .add_property("drop_flags", &commodity_t::drop_flags) - //.add_property("updater", &commodity_t::updater) - - .add_property("smaller", - make_getter(&commodity_t::smaller, - return_value_policy<reference_existing_object>()), - make_setter(&commodity_t::smaller, - return_value_policy<reference_existing_object>())) - .add_property("larger", - make_getter(&commodity_t::larger, - return_value_policy<reference_existing_object>()), - make_setter(&commodity_t::larger, - return_value_policy<reference_existing_object>())) - - .def(self_ns::str(self)) - - .def("find", py_find_commodity, - return_value_policy<reference_existing_object>()) - .staticmethod("find") - - .def("add_price", &commodity_t::add_price) - .def("remove_price", &commodity_t::remove_price) - .def("value", &commodity_t::value) - - .def("valid", &commodity_t::valid) - ; -#endif - #define EXC_TRANSLATE(type) \ register_exception_translator<type>(&exc_translate_ ## type); diff --git a/src/py_commodity.cc b/src/py_commodity.cc new file mode 100644 index 00000000..eb94821f --- /dev/null +++ b/src/py_commodity.cc @@ -0,0 +1,93 @@ +#include "pyinterp.h" +#include "amount.h" + +#include <boost/python/exception_translator.hpp> + +namespace ledger { + +using namespace boost::python; + +struct commodity_updater_wrap : public commodity_base_t::updater_t +{ + PyObject * self; + commodity_updater_wrap(PyObject * self_) : self(self_) {} + + virtual void operator()(commodity_base_t& commodity, + const moment_t& moment, + const moment_t& date, + const moment_t& last, + amount_t& price) { + call_method<void>(self, "__call__", commodity, moment, date, last, price); + } +}; + +commodity_t * py_find_commodity(const string& symbol) +{ + return commodity_t::find(symbol); +} + +#define EXC_TRANSLATOR(type) \ + void exc_translate_ ## type(const type& err) { \ + PyErr_SetString(PyExc_ArithmeticError, err.what()); \ + } + +EXC_TRANSLATOR(commodity_error) + +void export_commodity() +{ + class_< commodity_base_t::updater_t, commodity_updater_wrap, + boost::noncopyable > + ("updater") + ; + + scope().attr("COMMODITY_STYLE_DEFAULTS") = COMMODITY_STYLE_DEFAULTS; + scope().attr("COMMODITY_STYLE_SUFFIXED") = COMMODITY_STYLE_SUFFIXED; + scope().attr("COMMODITY_STYLE_SEPARATED") = COMMODITY_STYLE_SEPARATED; + scope().attr("COMMODITY_STYLE_EUROPEAN") = COMMODITY_STYLE_EUROPEAN; + scope().attr("COMMODITY_STYLE_THOUSANDS") = COMMODITY_STYLE_THOUSANDS; + scope().attr("COMMODITY_STYLE_NOMARKET") = COMMODITY_STYLE_NOMARKET; + scope().attr("COMMODITY_STYLE_BUILTIN") = COMMODITY_STYLE_BUILTIN; + + class_< commodity_t > ("commodity") + .add_property("symbol", &commodity_t::symbol) + + .add_property("name", &commodity_t::name, &commodity_t::set_name) + .add_property("note", &commodity_t::note, &commodity_t::set_note) + .add_property("precision", &commodity_t::precision, + &commodity_t::set_precision) + .add_property("flags", &commodity_t::flags, &commodity_t::set_flags) + .add_property("add_flags", &commodity_t::add_flags) + .add_property("drop_flags", &commodity_t::drop_flags) + //.add_property("updater", &commodity_t::updater) + + .add_property("smaller", + make_getter(&commodity_t::smaller, + return_value_policy<reference_existing_object>()), + make_setter(&commodity_t::smaller, + return_value_policy<reference_existing_object>())) + .add_property("larger", + make_getter(&commodity_t::larger, + return_value_policy<reference_existing_object>()), + make_setter(&commodity_t::larger, + return_value_policy<reference_existing_object>())) + + .def(self_ns::str(self)) + + .def("find", py_find_commodity, + return_value_policy<reference_existing_object>()) + .staticmethod("find") + + .def("add_price", &commodity_t::add_price) + .def("remove_price", &commodity_t::remove_price) + .def("value", &commodity_t::value) + + .def("valid", &commodity_t::valid) + ; + +#define EXC_TRANSLATE(type) \ + register_exception_translator<type>(&exc_translate_ ## type); + + EXC_TRANSLATE(commodity_error); +} + +} // namespace ledger diff --git a/src/pyledger.cc b/src/pyledger.cc index 688374a6..08e6dbd1 100644 --- a/src/pyledger.cc +++ b/src/pyledger.cc @@ -8,7 +8,6 @@ void export_amount(); #if 0 void export_balance(); void export_value(); - void export_journal(); void export_parser(); void export_option(); @@ -16,8 +15,6 @@ void export_walk(); void export_report(); void export_format(); void export_valexpr(); - -void shutdown_option(); #endif void initialize_for_python() @@ -26,7 +23,6 @@ void initialize_for_python() #if 0 export_balance(); export_value(); - export_journal(); export_parser(); export_option(); @@ -37,18 +33,12 @@ void initialize_for_python() #endif } -void shutdown_for_python() -{ -#if 0 - shutdown_option(); -#endif -} - } ledger::session_t python_session; BOOST_PYTHON_MODULE(ledger) { + ledger::initialize_for_python(); ledger::set_session_context(&python_session); } diff --git a/tests/python/numerics/BasicAmount.py b/tests/python/numerics/BasicAmount.py index bdc7dbe9..4dfb3a7a 100644 --- a/tests/python/numerics/BasicAmount.py +++ b/tests/python/numerics/BasicAmount.py @@ -1,6 +1,4 @@ import sys -sys.path.append("/home/johnw/src/ledger") -sys.path.append("/home/johnw/Products/ledger") import unittest import exceptions @@ -362,7 +360,7 @@ class BasicAmountTestCase(unittest.TestCase): def testIntegerConversion(self): x1 = amount(123456) - self.assertEqual(True, bool(x1)) + self.assertTrue(x1) self.assertEqual(123456, int(x1)) self.assertEqual(123456.0, float(x1)) self.assertEqual("123456", x1.to_string()) @@ -373,7 +371,7 @@ class BasicAmountTestCase(unittest.TestCase): def testFractionalConversion(self): x1 = amount(1234.56) - self.assertEqual(True, not (not x1)) + self.assertTrue(x1) self.assertEqual(1234, int(x1)) self.assertEqual(1234.56, float(x1)) self.assertEqual("1234.56", x1.to_string()) @@ -432,8 +430,8 @@ class BasicAmountTestCase(unittest.TestCase): x1 = amount("1234") x2 = amount("1234.56") - self.assertTrue(not x0) - self.assertTrue(x1 ) + self.assertFalse(x0) + self.assertTrue(x1) self.assertTrue(x2) self.assertTrue(x0.valid()) @@ -444,12 +442,12 @@ class BasicAmountTestCase(unittest.TestCase): x0 = amount() x1 = amount("0.000000000000000000001") - self.assertTrue(not x0) + self.assertFalse(x0) self.assertTrue(x1) self.assertTrue(x0.zero()) self.assertTrue(x0.realzero()) - self.assertTrue(not x1.zero()) - self.assertTrue(not x1.realzero()) + self.assertFalse(x1.zero()) + self.assertFalse(x1.realzero()) self.assertTrue(x0.valid()) self.assertTrue(x1.valid()) @@ -491,7 +489,7 @@ class BasicAmountTestCase(unittest.TestCase): x3 = amount("1") x4 = amount("-1") - self.assertTrue(not x0.sign()) + self.assertEqual(x0.sign(), 0) self.assertTrue(x1.sign() > 0) self.assertTrue(x2.sign() < 0) self.assertTrue(x3.sign() > 0) |