Work to get Python tests running again.

This commit is contained in:
John Wiegley 2007-05-04 09:53:05 +00:00
parent 6c7e35dc17
commit 0214a136c2
6 changed files with 122 additions and 111 deletions

View file

@ -76,11 +76,9 @@ class amount_t
ordered_field_operators<amount_t, unsigned long, ordered_field_operators<amount_t, unsigned long,
ordered_field_operators<amount_t, double> > > > 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: public:
class bigint_t;
typedef uint_least16_t precision_t;
/** /**
* The initialize and shutdown methods ready the amount subsystem * The initialize and shutdown methods ready the amount subsystem
* for use. Normally they are called by `ledger::initialize' and * for use. Normally they are called by `ledger::initialize' and
@ -89,6 +87,11 @@ public:
static void initialize(); static void initialize();
static void shutdown(); static void shutdown();
public:
class bigint_t;
typedef uint_least16_t precision_t;
/** /**
* The default_pool is a static variable indicating which commodity * The default_pool is a static variable indicating which commodity
* pool should be used when none is specified. * pool should be used when none is specified.

View file

@ -45,15 +45,16 @@
namespace ledger { namespace ledger {
class annotated_commodity_t;
class commodity_t class commodity_t
: public equality_comparable1<commodity_t, noncopyable> : public equality_comparable1<commodity_t, noncopyable>
{ {
friend class commodity_pool_t;
class base_t : public noncopyable class base_t : public noncopyable
{ {
private: public:
friend class commodity_pool_t;
friend class commodity_t;
typedef std::map<const moment_t, amount_t> history_map; typedef std::map<const moment_t, amount_t> history_map;
typedef std::pair<const moment_t, amount_t> history_pair; 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 commodity_t& comm) const;
virtual bool operator==(const annotated_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() { commodity_t& referent() {

View file

@ -7,42 +7,20 @@ namespace ledger {
using namespace boost::python; using namespace boost::python;
void py_parse_1(amount_t& amount, const string& str, void py_parse_1(amount_t& amount, const string& str, unsigned char flags) {
unsigned char flags) {
amount.parse(str, flags); amount.parse(str, flags);
} }
void py_parse_2(amount_t& amount, const string& str) { void py_parse_2(amount_t& amount, const string& str) {
amount.parse(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); return amount.round(prec);
} }
amount_t py_round_2(amount_t& amount) { amount_t py_round_2(const amount_t& amount) {
return amount.round(); 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) \ #define EXC_TRANSLATOR(type) \
void exc_translate_ ## type(const type& err) { \ void exc_translate_ ## type(const type& err) { \
PyErr_SetString(PyExc_ArithmeticError, err.what()); \ PyErr_SetString(PyExc_ArithmeticError, err.what()); \
@ -214,15 +192,16 @@ void export_amount()
.def("strip_annotations", &amount_t::strip_annotations) .def("strip_annotations", &amount_t::strip_annotations)
#if 0 #if 0
.def("price", &amount_t::price) // jww (2007-05-03): This method depends on annotation_t
.def("date", &amount_t::date) .def("annotation_details", &amount_t::annotation_details)
.def("tag", &amount_t::tag)
#endif #endif
// jww (2007-05-03): There are four versions of this method now
.def("parse", py_parse_1) .def("parse", py_parse_1)
.def("parse", py_parse_2) .def("parse", py_parse_2)
#if 0 #if 0
// jww (2007-05-03): This method has two forms
.def("parse_conversion", &amount_t::parse_conversion) .def("parse_conversion", &amount_t::parse_conversion)
.staticmethod("parse_conversion") .staticmethod("parse_conversion")
#endif #endif
@ -230,59 +209,6 @@ void export_amount()
.def("valid", &amount_t::valid) .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) \ #define EXC_TRANSLATE(type) \
register_exception_translator<type>(&exc_translate_ ## type); register_exception_translator<type>(&exc_translate_ ## type);

93
src/py_commodity.cc Normal file
View file

@ -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

View file

@ -8,7 +8,6 @@ void export_amount();
#if 0 #if 0
void export_balance(); void export_balance();
void export_value(); void export_value();
void export_journal(); void export_journal();
void export_parser(); void export_parser();
void export_option(); void export_option();
@ -16,8 +15,6 @@ void export_walk();
void export_report(); void export_report();
void export_format(); void export_format();
void export_valexpr(); void export_valexpr();
void shutdown_option();
#endif #endif
void initialize_for_python() void initialize_for_python()
@ -26,7 +23,6 @@ void initialize_for_python()
#if 0 #if 0
export_balance(); export_balance();
export_value(); export_value();
export_journal(); export_journal();
export_parser(); export_parser();
export_option(); export_option();
@ -37,18 +33,12 @@ void initialize_for_python()
#endif #endif
} }
void shutdown_for_python()
{
#if 0
shutdown_option();
#endif
}
} }
ledger::session_t python_session; ledger::session_t python_session;
BOOST_PYTHON_MODULE(ledger) BOOST_PYTHON_MODULE(ledger)
{ {
ledger::initialize_for_python();
ledger::set_session_context(&python_session); ledger::set_session_context(&python_session);
} }

View file

@ -1,6 +1,4 @@
import sys import sys
sys.path.append("/home/johnw/src/ledger")
sys.path.append("/home/johnw/Products/ledger")
import unittest import unittest
import exceptions import exceptions
@ -362,7 +360,7 @@ class BasicAmountTestCase(unittest.TestCase):
def testIntegerConversion(self): def testIntegerConversion(self):
x1 = amount(123456) x1 = amount(123456)
self.assertEqual(True, bool(x1)) self.assertTrue(x1)
self.assertEqual(123456, int(x1)) self.assertEqual(123456, int(x1))
self.assertEqual(123456.0, float(x1)) self.assertEqual(123456.0, float(x1))
self.assertEqual("123456", x1.to_string()) self.assertEqual("123456", x1.to_string())
@ -373,7 +371,7 @@ class BasicAmountTestCase(unittest.TestCase):
def testFractionalConversion(self): def testFractionalConversion(self):
x1 = amount(1234.56) x1 = amount(1234.56)
self.assertEqual(True, not (not x1)) self.assertTrue(x1)
self.assertEqual(1234, int(x1)) self.assertEqual(1234, int(x1))
self.assertEqual(1234.56, float(x1)) self.assertEqual(1234.56, float(x1))
self.assertEqual("1234.56", x1.to_string()) self.assertEqual("1234.56", x1.to_string())
@ -432,8 +430,8 @@ class BasicAmountTestCase(unittest.TestCase):
x1 = amount("1234") x1 = amount("1234")
x2 = amount("1234.56") x2 = amount("1234.56")
self.assertTrue(not x0) self.assertFalse(x0)
self.assertTrue(x1 ) self.assertTrue(x1)
self.assertTrue(x2) self.assertTrue(x2)
self.assertTrue(x0.valid()) self.assertTrue(x0.valid())
@ -444,12 +442,12 @@ class BasicAmountTestCase(unittest.TestCase):
x0 = amount() x0 = amount()
x1 = amount("0.000000000000000000001") x1 = amount("0.000000000000000000001")
self.assertTrue(not x0) self.assertFalse(x0)
self.assertTrue(x1) self.assertTrue(x1)
self.assertTrue(x0.zero()) self.assertTrue(x0.zero())
self.assertTrue(x0.realzero()) self.assertTrue(x0.realzero())
self.assertTrue(not x1.zero()) self.assertFalse(x1.zero())
self.assertTrue(not x1.realzero()) self.assertFalse(x1.realzero())
self.assertTrue(x0.valid()) self.assertTrue(x0.valid())
self.assertTrue(x1.valid()) self.assertTrue(x1.valid())
@ -491,7 +489,7 @@ class BasicAmountTestCase(unittest.TestCase):
x3 = amount("1") x3 = amount("1")
x4 = amount("-1") x4 = amount("-1")
self.assertTrue(not x0.sign()) self.assertEqual(x0.sign(), 0)
self.assertTrue(x1.sign() > 0) self.assertTrue(x1.sign() > 0)
self.assertTrue(x2.sign() < 0) self.assertTrue(x2.sign() < 0)
self.assertTrue(x3.sign() > 0) self.assertTrue(x3.sign() > 0)