Commit 4a32b96d authored by Jason Rhinelander's avatar Jason Rhinelander

Added constructors that take Goods

Without implicit cpp conversion this is needed to allow `Bundle(good,
123)` where `good` is a Good object, rather than just an id.
parent 9688a5df
#include "pyeris/core/bundles.hpp"
#include <pybind11/operators.h>
#include <pybind11/stl.h>
#include <eris/Good.hpp>
#include <sstream>
namespace pyeris { namespace core {
......@@ -11,10 +12,16 @@ void bind_bundles(py::module &m) {
.def(py::init<>(), "Construct an empty bundle")
.def(py::init<eris_id_t, double>(), "Construct a bundle containing a quantity of a single good (identified by the good's id)")
.def(py::init<const BundleSigned&>(), "Constructs a bundle by copying goods and quantities from another Bundle or BundleSigned")
.def("__init__", [](BundleSigned &bundle, SharedMember<Good> g, double q) { new (&bundle) BundleSigned; bundle.set(g, q); },
"Initializes a BundleSigned from a good and quantity")
.def("__init__", [](BundleSigned &bundle, const std::unordered_map<eris_id_t, double> &init) {
new (&bundle) BundleSigned;
for (auto &g : init) bundle.set(g.first, g.second);
}, "Initializes a BundleSigned from a dict of (id,quantity) pairs")
.def("__init__", [](BundleSigned &bundle, const std::unordered_map<SharedMember<Good>, double> &init) {
new (&bundle) BundleSigned;
for (auto &g : init) bundle.set(g.first, g.second);
}, "Initializes a BundleSigned from a dict of (good,quantity) pairs")
.def("__getitem__", (const double& (BundleSigned::*)(eris_id_t) const) &BundleSigned::operator[], "Accesses the quantity of a given good in the bundle. Returns 0 for goods that are not in the bundle (rather than throwing a KeyError)")
.def("__setitem__", &BundleSigned::set, "Sets the quantity of a given good in the bundle.")
.def("__delitem__", [](BundleSigned &bundle, eris_id_t gid) -> void {
......@@ -59,11 +66,17 @@ void bind_bundles(py::module &m) {
bundle
.def(py::init<>(), "Construct an empty bundle")
.def(py::init<eris_id_t, double>(), "Construct a bundle containing a quantity of a single good (identified by the good's id)")
.def("__init__", [](Bundle &bundle, SharedMember<Good> g, double q) { new (&bundle) Bundle; bundle.set(g, q); },
"Initializes a Bundle from a good and quantity")
.def(py::init<const BundleSigned&>(), "Constructs a bundle by copying goods and quantities from another Bundle or BundleSigned. Will raise BundleNegativityError if the copied bundle has any negative quantities.")
.def("__init__", [](Bundle &bundle, const std::unordered_map<eris_id_t, double> &init) {
new (&bundle) Bundle;
for (auto &g : init) bundle.set(g.first, g.second);
}, "Initializes a Bundle from a dict of (id,quantity) pairs")
.def("__init__", [](Bundle &bundle, const std::unordered_map<SharedMember<Good>, double> &init) {
new (&bundle) Bundle;
for (auto &g : init) bundle.set(g.first, g.second);
}, "Initializes a Bundle from a dict of (good,quantity) pairs")
.def("copy", [](const Bundle &b) { return Bundle(b); }, "Returns a copy of the Bundle")
.def("coverage", &Bundle::coverage, "Returns the multiple of the given bundle that would be required for the given bundle to have at least as much of each good as the object the method is called upon. Returns infinity if this bundle has positive quantities not found in the given bundle; returns NaN is both bundles are zero-bundles (that is, either empty or with 0 quantities for each good)")
.def("coverage_excess", &Bundle::coverageExcess, "Returns the excess associated with a coverage() call: that is, the quantities that would remain if this bundle were subtracted from the bundle `coverage(b)*b`")
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment