best way to do this ...

T

Tonino

Hi ALL,

I am having a issue with a particular storage of data problem and
wanted to know if I am going about it the wrong way.

I have a list of reference numbers associated with a product and I have
a socket that listens on a port. Now what happens is the client app
connects to the server - sends the product and the associated ref
numbers (there are a few per product). I need to be able to store these
in a data structure so that when the same product is called again -
it can display the ref numbers for that product.

I am new to OO programming - so please have mercy ;)

eg:


class ProductData:
def __init__(self,prod):
self.prod = prod
self.ref = []
def add_ref(self,refno):
self.ref.append(refno)
def __str__(self):
return "%s : %s" % (self.prod,",".join(self.ref))


products = ['table', 'chair', 'couch']
ref_numbers = ['1234','4567', '7890']

detail = {}
id = 0

for p in products:
detail[id] = ProductData(p)
for r in ref_numbers:
detail[id].add_ref(r)
id = id + 1

for a in detail:
print detail[a]


so I will get:

table : 1234,4567,7890
chair : 1234,4567,7890
couch : 1234,4567,7890

BUT I will need to be able to have this in a while loop - as there is a
socket listener there.

I keep getting this error:

for a in Detail:
RuntimeError: dictionary changed size during iteration


any ideas ?

Thanks
Tonino

what I want to be able to get is a dict of the products and associated
 
T

Thomas Guettler

Am Tue, 17 May 2005 05:26:15 -0700 schrieb Tonino:
Hi ALL,

I am having a issue with a particular storage of data problem and
wanted to know if I am going about it the wrong way.

I have a list of reference numbers associated with a product and I have
a socket that listens on a port. Now what happens is the client app
connects to the server - sends the product and the associated ref
numbers (there are a few per product). I need to be able to store these
in a data structure so that when the same product is called again -
it can display the ref numbers for that product.

The smallest database is the filesystem. Create a directory for every
product. Store the references in a file in this directory.
for a in Detail:
RuntimeError: dictionary changed size during iteration

I did not see any dict in your code, but this message means, that you
can't change the dictionary in the loop. Maybe this helps:

for a, b in list(mydict.items()):
....


Thomas
 
T

Tonino

hmm - but I want to store the data in memory eather than a filesystem
.... it is not an issue if the program terminates - it is almost needed
while running and does not need to remember where it is ..

the dirctionary is detail = {}

will try the list() function - thanks

Tonino
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,774
Messages
2,569,596
Members
45,144
Latest member
KetoBaseReviews
Top