0

I've created a class (item) which simply has position and state data (number denotes an unique enumeration of the items, and state can be 0,1).

class item:
 def __init__(self, number, state):
 self.number = number
 self.state = state
 def __repr__(self):
 return('item(%s,%s)'%(self.position,self.state))

I'm trying to create a container/collection for the items, a random subset of which have their state set to 1, for this currently I'm using:

from random import randint
N = 1024
num_defectives = 10
list_of_items = [item(i,0) for i in range(0,N)]
for i in range(0, num_defectives):
 r = randint(0,N)
 list_of_items[r].state = 1

But I want to wrap this up into something that isn't a raw list (because I don't think that;s particularly good hanging round my code), but which keeps track of the sum of the states of the items.

Ideally it will also have an inaccessible list of the positions of items with state 1 (I'm only allowed to query the sum of the states, or whether a subset I have contains a member of these items with state 1).

Currently I'm selecting random members of this list, and testing the sum of the states like so:

items_to_test = [randint(0,n) for i in range(0,groupSize)]
group = []
for item in items_to_test:
 group.append(inList[item])
states = sum([x.state for x in group])
if states: 
 #do some stuff to find an item with state 1, remove it from list_of_items
else:
 #remove all the items in group from list_of_items

Basically, I'm looking for a cleaner way to do all of this behaviour via a collection.

I've looked the answers to this question:

How to subclass Python list without type problems?

and this question:

What to consider before subclassing list?

and read this:

http://docs.python.org/2/reference/datamodel.html#emulating-container-types

but I'm still unsure of where to start (other than subclassing object), as I'm quite unfamiliar with all of the 'magic' functions that python provides..

asked Dec 9, 2013 at 14:25

1 Answer 1

1

Start by making a simple wrapper around a list. Chances are, you don't need anything more complicated. I think your use case is too specialized for subclassing list.

from random import randint
# user-defined class names are conventionally capitalized.
class Item:
 ...
class ItemCollection:
 def __init__(self, N=1024, num_defectives=10):
 self.list_of_items = [Item(i,0) for i in range(0,N)]]
 for i in range(0, self.num_defectives):
 r = randint(0,N)
 self.list_of_items[r].state = 1
 self.state_sum = sum(x.state for x in self.list_of_items)
 def states_of_one(self):
 return [ x from self.list_of_items if x.state == 1 ]
 def has_state_of_one(self, i):
 return self.list_of_items[i].state == 1
answered Dec 9, 2013 at 15:06
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.