A more Pythonic openshot.Fraction

Extend openshot::Fraction in the Python wrapper, to permit:
```py
f = openshot.Fraction(16, 9)
float(f) # returns 1.777777777
int(f) # returns 2
print(f) # outputs "16:9"
dict(f.GetMap()) # in Python dict form
This commit is contained in:
FeRD (Frank Dana)
2019-09-07 17:06:04 -04:00
parent f8c180c1f4
commit b8bb1a88f8

View File

@@ -120,6 +120,34 @@
}
}
/* Instantiate the required template specializations */
%template() std::map<std::string, int>;
/* Make openshot.Fraction more Pythonic */
%extend openshot::Fraction {
%{
#include <sstream>
#include <map>
%}
double __float__() {
return $self->ToDouble();
}
int __int__() {
return $self->ToInt();
}
std::map<std::string, int> GetMap() {
std::map<std::string, int> map1;
map1.insert({"num", $self->num});
map1.insert({"den", $self->den});
return map1;
}
std::string __repr__() {
std::ostringstream result;
result << $self->num << ":" << $self->den;
return result.str();
}
}
%include "OpenShotVersion.h"
%include "../../../include/ReaderBase.h"
%include "../../../include/WriterBase.h"