Take the 2-minute tour ×
Programmers Stack Exchange is a question and answer site for professional programmers interested in conceptual questions about software development. It's 100% free, no registration required.

I'm writing a naive simulator for testing code which will run on an embedded system. The code being tested will import a bunch of modules, call functions in these modules and receive data on which it will act. I need to manage the state of the system, i.e. the calling code must get the correct responses from my simulator in correct order. This should simulate the actual system to an extent.

Some of the modules are proprietary, I have an API for them, but their internals are not available to me. Thus I have to create mockup modules with mockup functions that return values similar to the actual values. The code that I'm testing cannot be modified, so whatever that code calls, must exist in my simulator environment.

I currently have a global Simulator object, and each of the mockup module functions map to that single instance. It works but it feels hacky. Here's a simplified example of the code:

# A.py - Mockup of the real module A, unavailable to me.
import simulator
def read():
    return simulator.sim.read(module = "a")

def other_function():
    return simulator.sim.other_function(module = "a")


# B.py - Mockup of the real module B, unavailable to me.
import simulator
def read():
    return simulator.sim.read(module = "b")


# simulator.py
class Simulator(object):
    """ There's only ever going to be one of these in existence """
    def __init__(self):
        self.buffers = {"a": [], "b": []}
    def read(self, module = "a"):
        return "\r\n".join(self.buffers[module]))
    def other_function(self, module = "a"):
        return ....

sim = Simulator()

# Code that is being tested
# This code cannot be modified, it must run as it is in the simulator environment.

import A
import B
data = A.foo()
if data:

I want to be able to extend this in the future and it's going to be tricky with what I currently have. Thus I suppose my question boils down to: Is there a more pythonic design pattern?

share|improve this question
If all those modules are hardcoded to the existence of such a global simulator object, why are they not part of the simulator to begin with? Or a subtype of the Simulator type where they extend the basic functionality with all that stuff? –  poke May 8 '13 at 9:51
@poke I tried to clarify the question a little bit and added some more code. Does it make more sense now? The mockups can't be part of the simulator to begin with, because the calling code will import A and then call A.read() at some point. This module must exist in one way or another. –  msvalkon May 8 '13 at 10:29
Is the purpose to write a simulator or to test your code? The simulator needing to stand on it's on will require a different design than one where the simulator is incidental and the real purpose is to test the embedded code. –  dietbuddha May 13 '13 at 19:18
add comment

Know someone who can answer? Share a link to this question via email, Google+, Twitter, or Facebook.

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Browse other questions tagged or ask your own question.