File: py_progress.cc

package info (click to toggle)
cadabra2 2.4.3.2-3
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 78,796 kB
  • sloc: ansic: 133,450; cpp: 92,064; python: 1,530; javascript: 203; sh: 184; xml: 182; objc: 53; makefile: 51
file content (57 lines) | stat: -rw-r--r-- 1,487 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
#include <iostream>

#include "py_progress.hh"
#include "py_helpers.hh"

namespace cadabra {
	namespace py = pybind11;

	py::list ProgressMonitor_totals_helper(ProgressMonitor& self)
		{
		py::list list;
		auto totals = self.totals();
		for (auto& total : totals)
			list.append(total);
		return list;
		}

	ProgressMonitor *get_progress_monitor()
		{
		try {
			pybind11::dict globals = get_globals();
			ProgressMonitor* pm;
			if (scope_has(globals, "__cdb_progress_monitor__")) {
				pm = globals["__cdb_progress_monitor__"].cast<ProgressMonitor*>();
				}
			else {
				if (scope_has(globals, "server") && py::hasattr(globals["server"], "send_progress_update")) {
					pm = new ProgressMonitor(globals["server"].attr("send_progress_update"));
				}
				else
					pm = new ProgressMonitor();
				globals["__cdb_progress_monitor__"] = pm;
				}
			return pm;
			}
		catch (pybind11::error_already_set& ex) {
			std::cerr << "*!?!?" << ex.what() << std::endl;
			return nullptr;
			}
		}


	void init_progress_monitor(py::module& m)
		{
		py::class_<ProgressMonitor>(m, "ProgressMonitor")
		.def("print", &ProgressMonitor::print)
		.def("totals", &ProgressMonitor_totals_helper);

		pybind11::class_<ProgressMonitor::Total>(m, "Total")
		.def_readonly("name", &ProgressMonitor::Total::name)
		.def_readonly("call_count", &ProgressMonitor::Total::call_count)
		.def_readonly("total_steps", &ProgressMonitor::Total::total_steps)
		.def("__str__", &ProgressMonitor::Total::str);

		}

	}