From b8bb1a88f88b5a9708499eeb87c0fb74635578e6 Mon Sep 17 00:00:00 2001 From: "FeRD (Frank Dana)" Date: Sat, 7 Sep 2019 17:06:04 -0400 Subject: [PATCH] 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 --- src/bindings/python/openshot.i | 28 ++++++++++++++++++++++++++++ 1 file changed, 28 insertions(+) diff --git a/src/bindings/python/openshot.i b/src/bindings/python/openshot.i index 512224ef..329ec28f 100644 --- a/src/bindings/python/openshot.i +++ b/src/bindings/python/openshot.i @@ -120,6 +120,34 @@ } } +/* Instantiate the required template specializations */ +%template() std::map; + +/* Make openshot.Fraction more Pythonic */ +%extend openshot::Fraction { +%{ + #include + #include +%} + double __float__() { + return $self->ToDouble(); + } + int __int__() { + return $self->ToInt(); + } + std::map GetMap() { + std::map 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"