| 1 | // Copyright David Abrahams 2004. Distributed under the Boost |
|---|
| 2 | // Software License, Version 1.0. (See accompanying |
|---|
| 3 | // file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt) |
|---|
| 4 | #include <boost/python/module.hpp> |
|---|
| 5 | #define BOOST_ENABLE_ASSERT_HANDLER |
|---|
| 6 | #include <boost/assert.hpp> |
|---|
| 7 | |
|---|
| 8 | #include <boost/python/def.hpp> |
|---|
| 9 | #include <boost/python/class.hpp> |
|---|
| 10 | #include <boost/python/dict.hpp> |
|---|
| 11 | #include <exception> |
|---|
| 12 | #include <string> |
|---|
| 13 | |
|---|
| 14 | using namespace boost::python; |
|---|
| 15 | |
|---|
| 16 | object new_dict() |
|---|
| 17 | { |
|---|
| 18 | return dict(); |
|---|
| 19 | } |
|---|
| 20 | |
|---|
| 21 | object data_dict() |
|---|
| 22 | { |
|---|
| 23 | dict tmp1; |
|---|
| 24 | tmp1["key1"] = "value1"; |
|---|
| 25 | |
|---|
| 26 | dict tmp2; |
|---|
| 27 | tmp2["key2"] = "value2"; |
|---|
| 28 | tmp1[1] = tmp2; |
|---|
| 29 | return tmp1; |
|---|
| 30 | } |
|---|
| 31 | |
|---|
| 32 | object dict_from_sequence(object sequence) |
|---|
| 33 | { |
|---|
| 34 | return dict(sequence); |
|---|
| 35 | } |
|---|
| 36 | |
|---|
| 37 | object dict_keys(dict data) |
|---|
| 38 | { |
|---|
| 39 | return data.keys(); |
|---|
| 40 | } |
|---|
| 41 | |
|---|
| 42 | object dict_values(dict data) |
|---|
| 43 | { |
|---|
| 44 | return data.values(); |
|---|
| 45 | } |
|---|
| 46 | |
|---|
| 47 | object dict_items(dict data) |
|---|
| 48 | { |
|---|
| 49 | return data.items(); |
|---|
| 50 | } |
|---|
| 51 | |
|---|
| 52 | void work_with_dict(dict data1, dict data2) |
|---|
| 53 | { |
|---|
| 54 | if (!data1.has_key("k1")) { |
|---|
| 55 | throw std::runtime_error("dict does not have key 'k1'"); |
|---|
| 56 | } |
|---|
| 57 | data1.update(data2); |
|---|
| 58 | } |
|---|
| 59 | |
|---|
| 60 | void test_templates(object print) |
|---|
| 61 | { |
|---|
| 62 | std::string key = "key"; |
|---|
| 63 | |
|---|
| 64 | dict tmp; |
|---|
| 65 | tmp[1] = "a test string"; |
|---|
| 66 | print(tmp.get(1)); |
|---|
| 67 | //print(tmp[1]); |
|---|
| 68 | tmp[1.5] = 13; |
|---|
| 69 | print(tmp.get(1.5)); |
|---|
| 70 | print(tmp.get(44)); |
|---|
| 71 | print(tmp); |
|---|
| 72 | print(tmp.get(2,"default")); |
|---|
| 73 | print(tmp.setdefault(3,"default")); |
|---|
| 74 | |
|---|
| 75 | BOOST_ASSERT(!tmp.has_key(key)); |
|---|
| 76 | //print(tmp[3]); |
|---|
| 77 | } |
|---|
| 78 | |
|---|
| 79 | BOOST_PYTHON_MODULE(dict_ext) |
|---|
| 80 | { |
|---|
| 81 | def("new_dict", new_dict); |
|---|
| 82 | def("data_dict", data_dict); |
|---|
| 83 | def("dict_keys", dict_keys); |
|---|
| 84 | def("dict_values", dict_values); |
|---|
| 85 | def("dict_items", dict_items); |
|---|
| 86 | def("dict_from_sequence", dict_from_sequence); |
|---|
| 87 | def("work_with_dict", work_with_dict); |
|---|
| 88 | def("test_templates", test_templates); |
|---|
| 89 | } |
|---|
| 90 | |
|---|
| 91 | #include "module_tail.cpp" |
|---|