Skip to content
Christophe Pradal edited this page Dec 18, 2018 · 3 revisions

Python wrapping of StructureAnalysis libraries

NB: All the methodology will be tested first on stat_tool

Review the current methodology

  • Look at the organisation of stat_tool
    • Get the doc string
    • Get the tests
    • Enhance the design
  • Adapt aml.py to this organisation

HowTo

Howto manage a StatError argument as input of the different methods?

Wraps the C++ method in Python. If the method fail, raise the errors. Other else return the result.

from functools import wraps

mod = __stat_tool.stat_tool
def wrapper(f):
    @wraps(f)
    def method(filename):
        error = __stat_tool.stat_tool.StatError(__stat_tool.stat_tool.nb_error)
        data = f(error, filename)
        if not data:
            raise Exception(str(error))
        return data
    return ascii_read

mod.klass.method = wrapper(mod.klass.method)

Howto manage std::cout stream in C++ functions?

Use a decorator function like:

from functools import wraps

mod = __stat_tool.stat_tool
def wrapper(f):
    @wraps(f)
    def f_comparison(self, other):
        sstream = mod.std.Ostringstream(mod.std.ios_openmode.S__OUT)
        f(self, sstream, other)
        return sstream.str()
    return f_comparison

mod.FrequencyDistribution.f_comparison = wrapper(mod.FrequencyDistribution.f__comparison)

Method to wrap

Tests