Skip to content

Commit

Permalink
Fraction: Implement dict type methods
Browse files Browse the repository at this point in the history
  • Loading branch information
ferdnyc committed Jan 27, 2021
1 parent acd573f commit 72e09dc
Showing 1 changed file with 51 additions and 0 deletions.
51 changes: 51 additions & 0 deletions bindings/python/openshot.i
Expand Up @@ -137,19 +137,56 @@
%{
#include <sstream>
#include <map>

static std::vector<std::string> _keys{"num", "den"};
static int fracError = 0;
%}
double __float__() {
return $self->ToDouble();
}
int __int__() {
return $self->ToInt();
}
/* Dictionary-type methods */
int __len__() {
return _keys.size();
}
%exception __getitem__ {
$action
if (fracError == 1) {
fracError = 0; // Clear flag for reuse
PyErr_SetString(PyExc_KeyError, "Key not found");
SWIG_fail;
}
}
const std::string __getitem__(int index) {
if (index < _keys.size()) {
return _keys[index];
}
/* Otherwise, raise an exception */
fracError = 1;
return "";
}
int __getitem__(const std::string& key) {
if (key == "num") {
return $self->num;
} else if (key == "den") {
return $self->den;
}
/* Otherwise, raise an exception */
fracError = 1;
return 0;
}
bool __contains__(const std::string& key) {
return bool(std::find(_keys.begin(), _keys.end(), key) != _keys.end());
}
std::map<std::string, int> GetMap() {
std::map<std::string, int> map1;
map1.insert({"num", $self->num});
map1.insert({"den", $self->den});
return map1;
}
/* Display methods */
const std::string __string__() {
std::ostringstream result;
result << $self->num << ":" << $self->den;
Expand All @@ -160,6 +197,20 @@
result << "Fraction(" << $self->num << ", " << $self->den << ")";
return result.str();
}
/* Implement dict methods in Python */
%pythoncode %{
def __iter__(self):
return iter(self.GetMap())
def keys(self):
_items = self.GetMap()
return _items.keys()
def items(self):
_items = self.GetMap()
return _items.items()
def values(self):
_items = self.GetMap()
return _items.values()
%}
}

%extend openshot::OpenShotVersion {
Expand Down

0 comments on commit 72e09dc

Please sign in to comment.