1

I have a python file that contains that is part of a bigger program, that is file is used to generate some of the same data used, However while I would use this program to get the data it's so big, old and undocumented that no one know how it works.

I need to processes the data generated by these files, is there a way to mimic the modules this calls dynamically i.e. I provide an actual method that I am interested in and then I provide a fake response for any method call that hasn't been overridden.

I had an attempt at doing this with overriding the __getattrib__ method but failed horribly (I don't have the source available anymore as it got erased by mistake)

2 Answers 2

2

Isn't it easier to just add the methods that are called?

But yes, you can fake it with for example the Mock library.

Sign up to request clarification or add additional context in comments.

3 Comments

no sadly it isn't as the methods called while the same a slightly different from file to file I'll have a look into the Mock Library thanks
@Bickerx2 I'm not sure it would make a difference that they are slightly different. The Mock library can fake a response, but most likely you'd have to tell it what to fake so that the caller doesn't fail anyway.
I think the mock library might indeed be the solution here. Otherwise I am going to have to make some serious changes to the files and then regex them (a prospect I am not looking forward to doing).
0

You can not fake functions like __add__ with __getattribute__.

Here you can find all methods that need to be implemented the class side e.g. __add__

I implemeted a Proxy that fakes them. It can even emulate Integers.

Once you have overwritten __getattribute__ this may come in handy.

class X(object):
    def __getattribute__(self, name): ...

    def use_this_getattribute(self):
        ... # uses getattribute from above

    @objectGetattributeFunction
    def use_original_getattribute(self):
        ...

1 Comment

Interesting method but considering how complex the actual code I would be hiding works (i.e. lots of unknown side-effects) I think I will be using Mock for now but, if it doesn't work I might have another look at overriding _getattribute_

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.