Skip to content

Commit

Permalink
python 3 compatibility: removing unnecessary dict_var.keys() calls in…
Browse files Browse the repository at this point in the history
… iterations; using any() to test if filter result empty; converting dict.keys() result (which has type dict_keys in python 3) to list
  • Loading branch information
jonrkarr committed Aug 10, 2016
1 parent 0e0a370 commit 0dbfb2b
Show file tree
Hide file tree
Showing 4 changed files with 16 additions and 16 deletions.
4 changes: 2 additions & 2 deletions Sequential_WC_Simulator/core/SimulationEngine.py
Original file line number Diff line number Diff line change
Expand Up @@ -107,11 +107,11 @@ def message_queues( ):
def print_simulation_state( ):

logger.debug( ' ' + '\t'.join( ['Sender', 'Message types sent'] ) )
for sender in MessageTypesRegistry.senders.keys():
for sender in MessageTypesRegistry.senders:
logger.debug( ' ' + sender + '\t' + ', '.join( MessageTypesRegistry.senders[sender] ) )

logger.debug( ' ' + '\t'.join( ['Receiver', 'Message types by priority'] ) )
for receiver in MessageTypesRegistry.receiver_priorities.keys():
for receiver in MessageTypesRegistry.receiver_priorities:
logger.debug( ' ' + sender + '\t' + ', '.join( MessageTypesRegistry.receiver_priorities[receiver] ) )

@staticmethod
Expand Down
8 changes: 4 additions & 4 deletions Sequential_WC_Simulator/multialgorithm/CellState.py
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ def __init__( self, name, initial_population, initial_fluxes=None,

self.population = {}
try:
for specie_name in initial_population.keys():
for specie_name in initial_population:
initial_flux_given = None
if initial_fluxes is not None and specie_name in initial_fluxes:
initial_flux_given = initial_fluxes[specie_name]
Expand All @@ -112,7 +112,7 @@ def __init__( self, name, initial_population, initial_fluxes=None,
my_level = logging.NOTSET
if log:
my_level = logging.DEBUG
for specie_name in initial_population.keys():
for specie_name in initial_population:
setup_logger(specie_name, level=my_level )
log = logging.getLogger(specie_name)
# write log header
Expand Down Expand Up @@ -151,7 +151,7 @@ def handle_event( self, event_list ):

logging.getLogger( self.logger_name ).debug(
"ADJUST_POPULATION_BY_DISCRETE_MODEL: {}".format( str(population_changes) ) )
for specie_name in population_changes.population_change.keys():
for specie_name in population_changes.population_change:
if not specie_name in self.population:
self.population[specie_name] = Specie( specie_name, 0 )

Expand All @@ -166,7 +166,7 @@ def handle_event( self, event_list ):
logging.getLogger( self.logger_name ).debug(
"ADJUST_POPULATION_BY_CONTINUOUS_MODEL: {}".format( str(population_changes) ) )

for specie_name in population_changes.population_change.keys():
for specie_name in population_changes.population_change:
# raise exeception if an ADJUST_POPULATION_BY_CONTINUOUS_MODEL event acts on a
# non-existent species because the species has no flux, and we don't want a default flux
if not specie_name in self.population:
Expand Down
16 changes: 8 additions & 8 deletions Sequential_WC_Simulator/multialgorithm/shared_cell_state.py
Original file line number Diff line number Diff line change
Expand Up @@ -72,10 +72,10 @@ def __init__( self, name, initial_population, initial_fluxes=None, retain_histor

try:
if initial_fluxes is not None:
for specie_id in initial_population.keys():
for specie_id in initial_population:
self.init_cell_state_specie( specie_id, initial_population[specie_id], initial_fluxes[specie_id] )
else:
for specie_id in initial_population.keys():
for specie_id in initial_population:
self.init_cell_state_specie( specie_id, initial_population[specie_id] )
except AssertionError as e:
sys.stderr.write( "Cannot initialize SharedMemoryCellState: {}.\n".format( e.message ) )
Expand All @@ -100,7 +100,7 @@ def __init__( self, name, initial_population, initial_fluxes=None, retain_histor
my_level = logging.NOTSET
if log:
my_level = logging.DEBUG
for specie_name in initial_population.keys():
for specie_name in initial_population:
setup_logger(specie_name, level=my_level )
log = logging.getLogger(specie_name)
# write log header
Expand Down Expand Up @@ -183,7 +183,7 @@ def history_debug(self):
print("{}\t{}\t{}".format( len(self.history['time']), self.history['time'][0],
self.history['time'][-1] ))
print("Specie\t#values\tfirst\tlast")
for s in self.history['population'].keys():
for s in self.history['population']:
print("{}\t{}\t{}\t{}".format( s, len(self.history['population'][s]),
self.history['population'][s][0], self.history['population'][s][-1] ))
else:
Expand Down Expand Up @@ -213,7 +213,7 @@ def __check_access_time( self, time, species ):
ValueError: if specie in species is being accessed at a time earlier than a prior access
"""
early_accesses = filter( lambda s: time < self.last_access_time[s], species)
if early_accesses:
if any(early_accesses):
raise ValueError( "Error: earlier access of specie(s): {}".format( early_accesses ))

def __update_access_times( self, time, species ):
Expand Down Expand Up @@ -252,9 +252,9 @@ def adjust_discretely( self, time, adjustments ):
ValueError: adjustment attempts to change the population of an unknown species
ValueError: if population goes negative
"""
self._check_species( time, adjustments.keys() )
self._check_species( time, list( adjustments.keys() ) )
self.time = time
for specie in adjustments.keys():
for specie in adjustments:
try:
self.population[specie].discrete_adjustment( adjustments[specie] )
self.__update_access_times( time, [specie] )
Expand All @@ -274,7 +274,7 @@ def adjust_continuously( self, time, adjustments ):
ValueError: adjustment attempts to change the population of a non-existent species
ValueError: if population goes negative
"""
self._check_species( time, adjustments.keys() )
self._check_species( time, list( adjustments.keys() ) )
self.time = time

# record simulation state history
Expand Down
4 changes: 2 additions & 2 deletions tests/test_CellStateMultiObj.py
Original file line number Diff line number Diff line change
Expand Up @@ -141,7 +141,7 @@ def testSimulation( self ):
)

# GET_POPULATION
for time in pop_history_dict['get_pop'].keys():
for time in pop_history_dict['get_pop']:
TestSimObj.send_event( time, cs1, GET_POPULATION,
event_body=GET_POPULATION.body( set(['x']) )
)
Expand Down Expand Up @@ -170,7 +170,7 @@ def testSimulationDefaultPopulation( self ):
event_body=ADJUST_POPULATION_BY_DISCRETE_MODEL.body( { specie:Pop_adjust } ) )

# GET_POPULATION
for time in pop_history_dict['get_pop'].keys():
for time in pop_history_dict['get_pop']:
TestSimObj.send_event( time, cs, GET_POPULATION,
event_body=GET_POPULATION.body( set([ specie ]) )
)
Expand Down

0 comments on commit 0dbfb2b

Please sign in to comment.