List index method for complex list item types?
I'm a Python newbie who just started learning the language a few weeks ago. So these are beginner questions. I have a list of sockets that I use for select.select calls like this: ReadList,WriteList,EventList = select.select(self.SocketList,[],[],3) In parallel with that list of sockets I want something that is like a list of socket,PacketFragment pairs. I need this because I could do recv and get part of a sent packet. I want to loop around and each time add more to the fragment until I get a full packet which has an internal byte structure of the types I'm expecting. The idea is every time I do self.SocketList.append(NewSocket) also do self.SocketPacketFragmentList.append((NewSocket,'')) so that the index into SocketList is the same as the index into SocketPacketFragmentList. So I can use self.SocketList.index(SomeSocket) find an item in SocketList and then know how to find it in the other list. MySocketIndex = self.SocketList.index(MyTargetSocket) Then do: MySubList = self.SocketPacketFragmentList[MySocketIndex] Then MyPacketFragment = MySubList[1] But a thought struck me while writing this: Does Python not provide a way to search a list of sublists to find something on, say, the value of the first sublist item field as a way to find the index to the item one wants in the parent list? There does not appear to be an alternative to lists that has better functionality for this purpose. Dictionaries are immutable, right? One can't use dictionaries for doing look-ups on dynamically changing lists? For efficiency's sake it seems to me one wants a search function on lists that returns two things: - index where the item found. - full item which matches on the particular field one is looking up on. Am I wrong in thinking Python doesn't really provide an automated way to search lists of complex things? Also, ii C++ one can use STL iterators to move thru a list or deque. But if one needs to advance thru a list with array indexes that does Python index in each time if one is looping thru the list? In Python maybe the trick is to use ii = 0 For item in List # see if item matches ii = ii + 1 and then somehow pop out of the for loop once one finds a match? -- http://mail.python.org/mailman/listinfo/python-list
On threads and constructors
I have a class: class ServerThreadManager(threading.Thread): def __init__(self): threading.Thread.__init__(self) # and a bunch of constructor statements def run(self): self.ReqHandlingLoop() # and a bunch of other methods ServerObj = ServerThreadManager() print "starting ServerThreadManager" ServerObj.start() ServerObj.KeyboardWatcherLoop() Here's what I want to know: 1) In __init__ I added that other __init__ call from a post here or from an article on a web page. Does that make sense to do? Why is it necessary? Do parent constructors always run in Python? If so, before or after child constructors? 2) Can I assume that constructors run to completion before returning and that my later call to start() happens after the constructor finished? 3) I'm having weird problems with an assignment in the constructor that make me wonder whether I need to know more about thread locks. I do this: self.AcceptListenerSocket = socket.socket(socket.AF_INET, socket.SOCK_STREAM) self.SocketList = [self.AcceptListenerSocket] # We start out with a single socket in our list. print "len(self.SocketList) = %d" % len(self.SocketList) self.SocketPacketFragmentsList = [] print "len(self.SocketPacketFragmentsList) = %d" % len(self.SocketPacketFragmentsList) self.SocketPacketFragmentsList.append([self.AcceptListenerSocket,'']) print "len(self.SocketPacketFragmentsList) = %d" % len(self.SocketPacketFragmentsList) Note how I do: self.SocketPacketFragmentsList = [] and then self.SocketPacketFragmentsList.append([self.AcceptListenerSocket,'']) That works in the sense that I get a length of 1 in the list after the append. But I originally did this which did not generate a runtime error but which left the tested list length at 0: self.SocketPacketFragmentsList = [[self.AcceptListenerSocket,'']] The result when tested is len == 0. Why didn't that work? With simpler types at the Python command line that sort of nested list assignment worked. Also, downstream from the append when I test the self.SocketPacketFragmentsList's length in the started thread it is len == 0 again. Yet self.SocketList keeps being len == 1 as I expected. I understand STL and threads over in C++ and write really complex stuff in C++. But in Python I'm a beginner fighting to do the same sorts of things and having a rough time of it. Newbieism is no fun. -- http://mail.python.org/mailman/listinfo/python-list
Re: List index method for complex list item types?
Mike, I'm trying to figure out dictionaries using the documentation. Clicking on "dictionary type" takes me to "2.3.8 Mapping Types -- classdict". Is that the documentation for the dictionary type? If so, I do not see an "append" or "add" or "insert" method defined in the list of methods on that page. Here's what they list: Operation Result Notes len(a) the number of items in a a[k] the item of a with key k (1) a[k] = v set a[k] to v del a[k] remove a[k] from a (1) a.clear() remove all items from a a.copy() a (shallow) copy of a a.has_key(k) True if a has a key k, else False k in a Equivalent to a.has_key(k) (2) k not in a Equivalent to not a.has_key(k) (2) a.items() a copy of a's list of (key, value) pairs (3) a.keys() a copy of a's list of keys (3) a.update([b]) updates (and overwrites) key/value pairs from b (9) a.fromkeys(seq[, value]) Creates a new dictionary with keys from seq and values set to value (7) a.values() a copy of a's list of values (3) a.get(k[, x]) a[k] if k in a, else x (4) a.setdefault(k[, x]) a[k] if k in a, else x (also setting it) (5) a.pop(k[, x]) a[k] if k in a, else x (and remove k) (8) a.popitem() remove and return an arbitrary (key, value) pair (6) a.iteritems() return an iterator over (key, value) pairs (2), (3) a.iterkeys() return an iterator over the mapping's keys (2), (3) a.itervalues() return an iterator over the mapping's values ( -- http://mail.python.org/mailman/listinfo/python-list
C regex equiv to Python implementation?
I've been writing code in Python to prototype part of an application. I've used the re regular expression pattern matcher. Now I have to take what I've written and recode it in C to fit in an existing C app. Anyway, is there a way to use the re regular expression evaluator in C? Is it written in C? Or does anyone know of a similar one that does search and match in close to the same fashion with begin and end values returned? -- http://mail.python.org/mailman/listinfo/python-list
Re: C regex equiv to Python implementation?
Ganesan, I'm trying to stay portable between Windows and Linux. My app will run on Linux when deployed. But we do a lot of simulation on Windows because of better dev tools available on Windows. So I really want a regular expression implementation that'll compile under MS VS 2003 C++ and also under Gnu C++ for Linux on both x86 and ARM targets. -- http://mail.python.org/mailman/listinfo/python-list
Can dictionaries be nested?
I'm parsing some data of the form:
OuterName1 InnerName1=5,InnerName2=7,InnerName3=34;
OuterName2 InnerNameX=43,InnerNameY=67,InnerName3=21;
OuterName3
and so on
These are fake names I've made up to illustrate the point more clearly.
(the embedded device device can't produce XML and this is what I have
to deal with)
I want to populate a nested set of dictionaries where the outer most
dictionary has look-ups on the OuterNames:
InnerDict = OuterDict["OuterName2"]
Then InnerDict[InnerName3] would yield 21 in the above example.
First, can dictionaries contain dictionaries?
Second, how to create each successive inner dictionary when populating
it? Python doesn't have constructors and (having all of 4 weeks of
Python experience) it isn't clear to me whether in nested while loops
that variables ever go out of scope.
If I do:
OuterDict = {}
while populating dictionaries
InnerDict = {}
while inner stuff to populate
InnerDict["InnerName1"] = 5
.. and so on
:
OuterDict["OuterName1"] = InnerDict
then when I loop around the second time will the same InnerDict get
set back to an empty dictionary and therefore wipe out the dictionary I
put at OuterDict["OuterName1"] ?
I need a new inner dictionary each time thru the outer while loop. It
is not clear to me how to do this.
--
http://mail.python.org/mailman/listinfo/python-list
