summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorJohn Wiegley <johnw@newartisans.com>2007-05-07 10:25:25 +0000
committerJohn Wiegley <johnw@newartisans.com>2008-04-13 03:38:39 -0400
commitc211335760f2c5883aed34c31aeb6ce7e8e51bf9 (patch)
treea8b6f4fcfb971ef2eee06bce22e1aab25d9ad4ef /src
parenta07e20c14e5ba3597a855276ad9a195343aee42f (diff)
downloadfork-ledger-c211335760f2c5883aed34c31aeb6ce7e8e51bf9.tar.gz
fork-ledger-c211335760f2c5883aed34c31aeb6ce7e8e51bf9.tar.bz2
fork-ledger-c211335760f2c5883aed34c31aeb6ce7e8e51bf9.zip
Extended Python amount class.
Diffstat (limited to 'src')
-rw-r--r--src/amount.h39
-rw-r--r--src/py_amount.cc31
-rw-r--r--src/py_times.cc2
-rw-r--r--src/pyutils.h4
4 files changed, 39 insertions, 37 deletions
diff --git a/src/amount.h b/src/amount.h
index f185cf7b..f5253b04 100644
--- a/src/amount.h
+++ b/src/amount.h
@@ -368,22 +368,24 @@ public:
* Truth tests. An amount may be truth test in several ways:
*
* sign() returns an integer less than, greater than, or equal to
- * zero depending on whether an amount is negative, zero, or greater
- * than zero. Note that this function tests the actual value of the
- * amount -- using its internal precision -- and not the display
- * value. To test its display value, use: `round().sign()'.
+ * zero depending on whether the amount is negative, zero, or
+ * greater than zero. Note that this function tests the actual
+ * value of the amount -- using its internal precision -- and not
+ * the display value. To test its display value, use:
+ * `round().sign()'.
*
* is_nonzero(), or operator bool, returns true if an amount's
* display value is not zero.
*
* is_zero() returns true if an amount's display value is zero.
- * Thus, $0.0001 is considered zero().
+ * Thus, $0.0001 is considered zero if the current display precision
+ * for dollars is two decimal places.
*
* is_realzero() returns true if an amount's actual value is zero.
- * $0.0001 is not considered is_realzero().
+ * Thus, $0.0001 is never considered realzero.
*
* is_null() returns true if an amount has no value and no
- * commodity. This occurs only if an unitialized amount has never
+ * commodity. This only occurs if an uninitialized amount has never
* been assigned a value.
*/
int sign() const;
@@ -401,7 +403,11 @@ public:
}
bool is_null() const {
- return ! quantity && ! has_commodity();
+ if (! quantity) {
+ assert(! has_commodity());
+ return true;
+ }
+ return false;
}
/**
@@ -440,11 +446,10 @@ public:
* Commodity-related methods. The following methods relate to an
* amount's commodity:
*
- * has_commodity() returns true if the amount has a commodity.
- *
* commodity() returns an amount's commodity. If the amount has no
- * commodity, then the value returned will be equal to
- * `commodity_t::null_commodity'.
+ * commodity, the value returned is `current_pool->null_commodity'.
+ *
+ * has_commodity() returns true if the amount has a commodity.
*
* set_commodity(commodity_t) sets an amount's commodity to the
* given value. Note that this merely sets the current amount to
@@ -458,9 +463,9 @@ public:
* number() returns a commodity-less version of an amount. This is
* useful for accessing just the numeric portion of an amount.
*/
- bool has_commodity() const;
commodity_t& commodity() const;
+ bool has_commodity() const;
void set_commodity(commodity_t& comm) {
commodity_ = &comm;
}
@@ -683,15 +688,15 @@ inline amount_t amount_t::round() const {
return round(commodity().precision());
}
-inline bool amount_t::has_commodity() const {
- return commodity_ && commodity_ != commodity_->parent().null_commodity;
-}
-
inline commodity_t& amount_t::commodity() const {
// jww (2007-05-02): Should be a way to access null_commodity better
return has_commodity() ? *commodity_ : *current_pool->null_commodity;
}
+inline bool amount_t::has_commodity() const {
+ return commodity_ && commodity_ != commodity_->parent().null_commodity;
+}
+
} // namespace ledger
#endif // _AMOUNT_H
diff --git a/src/py_amount.cc b/src/py_amount.cc
index 2d93067d..8721f879 100644
--- a/src/py_amount.cc
+++ b/src/py_amount.cc
@@ -51,6 +51,10 @@ void export_amount()
.staticmethod("shutdown")
#endif
+ .add_static_property("current_pool",
+ make_getter(&amount_t::current_pool,
+ return_value_policy<reference_existing_object>()))
+
#if 0
.add_static_property("keep_base", &amount_t::keep_base)
@@ -58,13 +62,12 @@ void export_amount()
.add_static_property("keep_date", &amount_t::keep_date)
.add_static_property("keep_tag", &amount_t::keep_tag)
- .add_static_property("full_strings", &amount_t::full_strings)
+ .add_static_property("stream_fullstrings", &amount_t::stream_fullstrings)
#endif
.def(init<double>())
.def(init<long>())
.def(init<std::string>())
- .def(init<char *>())
.def("exact", &amount_t::exact)
.staticmethod("exact")
@@ -151,6 +154,8 @@ void export_amount()
.def(self / double())
.def(double() / self)
+ .def("precision", &amount_t::precision)
+
.def("negate", &amount_t::negate)
.def("in_place_negate", &amount_t::in_place_negate,
return_value_policy<reference_existing_object>())
@@ -176,9 +181,9 @@ void export_amount()
.def("sign", &amount_t::sign)
.def("__nonzero__", &amount_t::is_nonzero)
- .def("nonzero", &amount_t::is_nonzero)
- .def("zero", &amount_t::is_zero)
- .def("realzero", &amount_t::is_realzero)
+ .def("is_nonzero", &amount_t::is_nonzero)
+ .def("is_zero", &amount_t::is_zero)
+ .def("is_realzero", &amount_t::is_realzero)
.def("is_null", &amount_t::is_null)
.def("to_double", &amount_t::to_double)
@@ -192,39 +197,31 @@ void export_amount()
.def("quantity_string", &amount_t::quantity_string)
- .def("has_commodity", &amount_t::has_commodity)
-
.add_property("commodity",
make_function(&amount_t::commodity,
return_value_policy<reference_existing_object>()),
make_function(&amount_t::set_commodity,
with_custodian_and_ward<1, 2>()))
+ .def("has_commodity", &amount_t::has_commodity)
.def("clear_commodity", &amount_t::clear_commodity)
.def("number", &amount_t::number)
.def("annotate_commodity", &amount_t::annotate_commodity)
- .def("strip_annotations", &amount_t::strip_annotations)
-
-#if 0
- // jww (2007-05-03): This method depends on annotation_t
+ .def("commodity_annotated", &amount_t::commodity_annotated)
.def("annotation_details", &amount_t::annotation_details)
-#endif
+ .def("strip_annotations", &amount_t::strip_annotations)
- // 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
.def("valid", &amount_t::valid)
;
- python_optional<amount_t>();
+ register_optional_to_python<amount_t>();
implicitly_convertible<double, amount_t>();
implicitly_convertible<long, amount_t>();
diff --git a/src/py_times.cc b/src/py_times.cc
index f509e0d0..861c43c9 100644
--- a/src/py_times.cc
+++ b/src/py_times.cc
@@ -95,7 +95,7 @@ void export_times()
date_python_conversion();
datetime_python_conversion();
- python_optional<moment_t>();
+ register_optional_to_python<moment_t>();
}
} // namespace ledger
diff --git a/src/pyutils.h b/src/pyutils.h
index 84a0db7e..42d5f1e0 100644
--- a/src/pyutils.h
+++ b/src/pyutils.h
@@ -21,7 +21,7 @@ struct register_python_conversion
};
template <typename T>
-struct python_optional : public boost::noncopyable
+struct register_optional_to_python : public boost::noncopyable
{
struct optional_to_python
{
@@ -67,7 +67,7 @@ struct python_optional : public boost::noncopyable
}
};
- explicit python_optional() {
+ explicit register_optional_to_python() {
register_python_conversion<boost::optional<T>,
optional_to_python, optional_from_python>();
}