Skip to content

Commit

Permalink
Making more copies and optional parameter in build.
Browse files Browse the repository at this point in the history
  • Loading branch information
rik0 committed Nov 21, 2012
1 parent e4a31d6 commit 7198ce6
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 13 deletions.
10 changes: 3 additions & 7 deletions pynetsym/simulation.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@
from pynetsym.agent_db import PythonPickler
from pynetsym.node_manager import NodeManager
from pynetsym.termination import TerminationChecker

from pynetsym.util.component_creator import ComponentCreator


__all__ = [
Expand Down Expand Up @@ -347,12 +347,8 @@ def gather_additional_agents(cls):
cls, 'additional_agents', acc_type=set)

def setup(self):
graph_options = getattr(self, 'graph_options', {})
## deepcopy: no object sharing between different simulation
## executions!
graph_options = copy.deepcopy(graph_options)
self.graph = self.graph_type(**graph_options)

graph_creator = ComponentCreator(self, 'graph')
graph_creator.build(set_=True)

def create_node_db(self):
self.node_db = agent_db.AgentDB(PythonPickler(), dict())
Expand Down
14 changes: 8 additions & 6 deletions pynetsym/util/component_creator.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import copy
import inspect

from pynetsym import error
Expand Down Expand Up @@ -42,10 +43,10 @@ def process_arg_spec(self, arg_spec):

def options(self, factory):
try:
return factory.options
return copy.copy(factory.options)
except AttributeError:
try:
return getattr(self.context, self.options_name)
return copy.copy(getattr(self.context, self.options_name))
except AttributeError:
if inspect.isfunction(factory):
arg_spec = inspect.getargspec(factory)
Expand All @@ -55,19 +56,20 @@ def options(self, factory):
raise ComponentError(
'Cannot find options for %s' % (
self.component_name, ))
return self.process_arg_spec(arg_spec)
return copy.copy(self.process_arg_spec(arg_spec))

def parameters(self, options, simulation_parameters):
program_specified_parameters = getattr(self.context,
self.parameters_name, {})
program_specified_parameters = copy.deepcopy(getattr(self.context,
self.parameters_name, {}))
overriding_parameters = extract_subdictionary(
simulation_parameters, options)
program_specified_parameters.update(overriding_parameters)
return program_specified_parameters



def build(self, parameters, set_=False):
def build(self, parameters=None, set_=False):
parameters = {} if parameters is None else parameters
factory = self.factory()
options = self.options(factory)
parameters = self.parameters(options, parameters)
Expand Down

0 comments on commit 7198ce6

Please sign in to comment.