python - How to stop elements in list from changing? -



python - How to stop elements in list from changing? -

i'm not sure going on here. elements in list maintain changing.

table._row_gen generator instance, , creates rows table. source here.

it returns dict; can dict straight table._row_gen.stats.

def test_append(self): start_stats = {'step':250,'offset':13,'inc':113,'danger':0,'input':none} gen = statgenerator(start_stats) table = pathtable(gen) stats_list = [] in xrange(50): stats_list.append(table._row_gen.stats) print stats_list[i] table._row_gen.next() assert stats_list[0]['step'] == 250

this should build list of rows stats_list.

the print works fine , displays right output:

{'danger': 0, 'input': none, 'step': 250, 'inc': 113, 'offset': 13} {'enc': false, 'danger': 113, 'rnd': 216, 'step': 252, 'limit': 55552, 'offset': 13, 'inpu {'enc': false, 'danger': 226, 'rnd': 163, 'step': 254, 'limit': 41984, 'offset': 13, 'inpu {'enc': false, 'danger': 339, 'rnd': 151, 'step': 0, 'limit': 38912, 'offset': 26, 'input' {'enc': false, 'danger': 452, 'rnd': 212, 'step': 2, 'limit': 54528, 'offset': 26, 'input' {'enc': false, 'danger': 565, 'rnd': 64, 'step': 4, 'limit': 16640, 'offset': 26, 'input'

but list malformed, , assert fails:

> assert stats_list[0]['step'] == 250 e assert 94 == 250 test_int_path_table.py:47: assertionerror

after loop:

print stats_list[0] {'enc': true, 'danger': 5650, 'rnd': 6, 'step': 94, 'limit': 1792, 'offset': 26, 'input':

94 should lastly row. yet of rows reporting same:

print stats_list[0] stats_list[48] true

i don't understand why happening, , list right (like print output).

the simple solution create copy. this, can do

def test_append(self): start_stats = {'step':250,'offset':13,'inc':113,'danger':0,'input':none} gen = statgenerator(start_stats) table = pathtable(gen) stats_list = [] in xrange(50): stats_list.append(table._row_gen.stats.copy()) # <=== re-create phone call makes new re-create , solves problem. print stats_list[i] table._row_gen.next() assert stats_list[0]['step'] == 250

also, next phone call @ end of block extraneous. can combined using

def test_append(self): start_stats = {'step':250,'offset':13,'inc':113,'danger':0,'input':none} gen = statgenerator(start_stats) table = pathtable(gen) stats_list = [] in xrange(50): stats_list.append(next(table._row_gen).copy()) print stats_list[i] assert stats_list[0]['step'] == 250

if you're in command of pastebin code, stats property should homecoming defensive copy.

@property def stats(self): homecoming self._stats.copy()

and next method should homecoming defensive re-create too.

def next(self): ''' generates , returns stats. ''' row = self._stats row['step'] = self._genstepid(self._stats['step']) row['offset'] = self._genoffset(self._stats['offset'], self._stats['step']) row['danger'] = self._gendanger(self._stats['danger'], self.stats['inc']) row['rnd'] = self._genrnd(self.rnlut[self._stats['step']], self._stats['offset']) row['limit'] = self._gendangerlimit(self._stats['rnd']) row['enc'] = self._genenc(self._stats['danger'], self._stats['limit']) homecoming self.stats

if above 2 blocks, re-create in first 2 no longer necessary.

python

Comments

Popular posts from this blog

java - How to set log4j.defaultInitOverride property to false in jboss server 6 -

c - GStreamer 1.0 1.4.5 RTSP Example Server sends 503 Service unavailable -

Using ajax with sonata admin list view pagination -