Remap Mysql tuple to dictionary
Hello I want to convert a Mysql resulset to a dictionary. I made some code myself, and want to ask you if I do this the right way. def remapmysql(a): return (a[0], (a[1:])) def test_map(): count = 10 # count of simulated records l1 = range(0, count) l2 = range(count , 2 * count ) l3 = range(2 * count, 3 * count ) z1 = zip(l1, l2, l3) # simulate a mysql resultset d1 = dict(map(remapmysql,z1)) return d1 -- http://mail.python.org/mailman/listinfo/python-list
Re: Remap Mysql tuple to dictionary
Fredrik Lundh wrote: > if you care about performance, and is using a recent Python, you could yes i do ;-) > try doing > >d1 = dict((row[0], row[1:]) for row in z1) > > instead, and see if that runs faster (this uses a generator expression > instead of a callback and a full list). > > > I changed it and it saves me some time so I leave it like that! with 10 test records: >>> dict+map (1, 2, 3) -> {1: (2, 3)}: 1.343 seconds. dict+gen-expr (1, 2, 3) -> {1: (2, 3)}: 0.861 seconds. >>> dict+map (1, 2, 3) -> {1: (2, 3)}: 1.397 seconds. dict+gen-expr (1, 2, 3) -> {1: (2, 3)}: 0.943 seconds. with 50 test records: >>> dict+map (1, 2, 3) -> {1: (2, 3)}: 13.297 seconds. dict+gen-expr (1, 2, 3) -> {1: (2, 3)}: 8.335 seconds. >>> dict+map (1, 2, 3) -> {1: (2, 3)}: 14.548 seconds. dict+gen-expr (1, 2, 3) -> {1: (2, 3)}: 9.793 seconds. >>> thank you!! -- http://mail.python.org/mailman/listinfo/python-list
Re: Remap Mysql tuple to dictionary
Tim Chase wrote: >> def remapmysql(a): >> return (a[0], (a[1:])) >> >> def test_map(): >> count = 10 # count of simulated records >> l1 = range(0, count) >> l2 = range(count , 2 * count ) >> l3 = range(2 * count, 3 * count ) >> z1 = zip(l1, l2, l3) # simulate a mysql resultset >> >> d1 = dict(map(remapmysql,z1)) >> >> return d1 > > I'm not sure the map() is needed, as it could just be > > >>> d1 = dict((row[0], row[1:]) for row in z1) > > which worked in my tests. > > However either seems to work fairly well. > > -tkc > thank you!! changed it, see previous post. -- http://mail.python.org/mailman/listinfo/python-list
Re: Remap Mysql tuple to dictionary
Dennis Lee Bieber wrote: > It might be more economical to perform the conversion while fetching > the data: > > mdict = {} > for rec in crsr: > mdict[rec[0]] = rec[1:] > I didn't think of that. I just took the fetchall() from my first version (where I looped through the tuples, which was very slow) I just dropped the .fetchall() part, and as you said: it works fine, with 1 copy less. (but it doesn't save me time) dict((int(row[0]), row[1:]) for row in cursor) thanks! -- http://mail.python.org/mailman/listinfo/python-list
setting extra data to a wx.textctrl
Hello group! I have an application which uses a lot of mysql data fields, all the same data type (floats). I created a panel which executes a "SELECT * FROM tablename" and makes as much fields as needed, using de cursor.description as wx.statictext and the cursors field contents copied into wx.textctrls. At creation time, I loop over all the fields in the record and create a tuple which contains ((textctrl1, fieldname1), (textctrl2, fieldname2), ...) so I can keep track of which textctrl holds which piece of fielddata. The problem I'm having is: to know the fieldname in an text_event, I use event.GetEventObject(), then perform an iteration over the tuple and when I find a match I use the field name to update the mysqltable. When having a few fields, this is ok. But I have over 100 fields in 1 record and it really slows things down. Now my question is: should I use a python dictionary (with an object as first lookup field) ? On windows, I've seen a "Tag" property in a textbox which was meant to be used for this kind of stuff. Maybe it's better to override the wx.textctrl so I can add an extra string value? Anyone having the best solution for this ? thx! -- http://mail.python.org/mailman/listinfo/python-list
Re: setting extra data to a wx.textctrl
[EMAIL PROTECTED] wrote: > On May 10, 10:51 pm, Pom <[EMAIL PROTECTED]> wrote: >> Hello group! >> >> I have an application which uses a lot of mysql data fields, all the >> same data type (floats). >> >> I created a panel which executes a "SELECT * FROM tablename" and makes >> as much fields as needed, using de cursor.description as wx.statictext >> and the cursors field contents copied into wx.textctrls. >> >> At creation time, I loop over all the fields in the record and create a >> tuple which contains ((textctrl1, fieldname1), (textctrl2, fieldname2), >> ...) so I can keep track of which textctrl holds which piece of fielddata. >> >> The problem I'm having is: >> >> to know the fieldname in an text_event, I use event.GetEventObject(), >> then perform an iteration over the tuple and when I find a match I use >> the field name to update the mysqltable. >> When having a few fields, this is ok. But I have over 100 fields in 1 >> record and it really slows things down. >> >> Now my question is: should I use a python dictionary (with an object as >> first lookup field) ? >> >> On windows, I've seen a "Tag" property in a textbox which was meant to >> be used for this kind of stuff. Maybe it's better to override the >> wx.textctrl so I can add an extra string value? >> >> Anyone having the best solution for this ? >> >> thx! > > Both of your ideas seem sound to me. You could also look into using > statically assigned IDs that increment by one. Then you could just > increment or decrement by one and look up the field by ID. Of course, > that might get ugly and there are some IDs that are supposedly > reserved. But it's an idea. > > Also, I've heard that Dabo (http://dabodev.com/) is good for database > work. You might look at that. To get the quickest and most on target > answers to wxPython questions, I recommend the wxPython users-group > mailing list: http://www.wxpython.org/maillist.php > > Mike > thx! -- http://mail.python.org/mailman/listinfo/python-list
wx textctrl font style
Hello I have a tuple of strings which I must show in a textctrl, each item in the tuple representing one line of text. The first three lines of text should each have another style (fontsize and color) i'm using this code to achieve this: tmppos = self.txtInfo.GetInsertionPoint() # get the cursor pos self.txtInfo.write(str(csr[0])+'\n') tmppos2 = self.txtInfo.GetInsertionPoint() # get the new cursor pos self.txtInfo.SetStyle(tmppos, tmppos2, wx.TextAttr("BLUE", wx.NullColour, tmpnewfont)) self.txtInfo.write(str(csr[1])+'\n') tmppos3 = self.txtInfo.GetInsertionPoint() # get the new cursor pos self.txtInfo.SetStyle(tmppos2, tmppos3, wx.TextAttr("BLUE", wx.NullColour, tmpnewfont2)) self.txtInfo.write(str(csr[2])+'\n') tmppos4 = self.txtInfo.GetInsertionPoint() # get the new cursor pos self.txtInfo.SetStyle(tmppos3, tmppos4, wx.TextAttr(wx.Nullcolour, wx.NullColour, tmpnewfont3)) Now, I was wondering if this is the right way of doing this, because I have the feeling this code sucks. Anybody has a better way of doing this? Thx !!! -- http://mail.python.org/mailman/listinfo/python-list
Re: wx textctrl font style
[EMAIL PROTECTED] wrote: > On Mar 31, 3:13 am, Pom <[EMAIL PROTECTED]> wrote: >> Hello >> >> I have a tuple of strings which I must show in a textctrl, each item in >> the tuple representing one line of text. >> >> The first three lines of text should each have another style (fontsize >> and color) >> >> i'm using this code to achieve this: >>tmppos = self.txtInfo.GetInsertionPoint() # get the cursor pos >>self.txtInfo.write(str(csr[0])+'\n') >>tmppos2 = self.txtInfo.GetInsertionPoint() # get the new cursor pos >>self.txtInfo.SetStyle(tmppos, tmppos2, wx.TextAttr("BLUE", >> wx.NullColour, tmpnewfont)) >>self.txtInfo.write(str(csr[1])+'\n') >>tmppos3 = self.txtInfo.GetInsertionPoint() # get the new cursor pos >>self.txtInfo.SetStyle(tmppos2, tmppos3, wx.TextAttr("BLUE", >> wx.NullColour, tmpnewfont2)) >>self.txtInfo.write(str(csr[2])+'\n') >>tmppos4 = self.txtInfo.GetInsertionPoint() # get the new cursor pos >>self.txtInfo.SetStyle(tmppos3, tmppos4, wx.TextAttr(wx.Nullcolour, >> wx.NullColour, tmpnewfont3)) >> >> Now, I was wondering if this is the right way of doing this, because I >> have the feeling this code sucks. >> >> Anybody has a better way of doing this? >> >> Thx !!! > > You could experiment with the "Rich Text" style of the text control > (see the wxPython Demo). You might also mess with refactoring your > code a little and doing a FOR loop. > > # untested code!!! > fonts = [tmpnewfont, tmpnewfont2, tmpnewfont3] > count = 0 > tmppos = self.txtInfo.GetInsertionPoint() > self.txtInfo.write(str(csr[count])+'\n') > for i in range(3): > tmppos2 = self.txtInfo.GetInsertionPoint() > self.txtInfo.SetStyle(tmppos, tmppos2, wx.TextAttr("BLUE", > wx.NullColour, fonts[count])) > tmppos = tmppos2 > count += 1 > self.txtInfo.write(str(csr[count])+'\n') > > > I also noticed a FancyText widget. I've used the HtmlWindow widget for > an About screen before and I thought it was pretty cool if you know > HTML. You might also try dropping an email to the wxPython Users Group > at http://wxpython.org/maillist.php > > Hope this helps! > > Mike > Thx alot ! I know HTML, so I certainly will skip the richtext. I did'nt see that fancytext widget, I'll take a look and decide between that and the htmlwindow. I'm making a simple addressbook, which I can't find like I need it (with mysql db) Kindly regards -- http://mail.python.org/mailman/listinfo/python-list
emulate a serial port in windows (create a virtual 'com' port)
Hello how can I emulate a serial port in windows? I want to intercept data sent to a 'com'port by a proprietary program. It sends statistics to a serial display, and I want that data in my python program (that display isn't needed). Is this possible? I've seen this kind of serial ports being created when using USB and bleutooth adapters, so i guess it's possible ??? Thanks in advance Greets Pom -- http://mail.python.org/mailman/listinfo/python-list
Dynamically fill prepared (PDF?) forms
Hello I'm looking for a way to fill in forms with data retrieved from a mysql db. But what I'm especially looking for is an easy way to create the layout of the forms itself. Handling data from mysql is not a problem for me. Making static programmed PDF output using Reportlab is not a problem. But I want to give the user the possibility to change the layout of the outputted PDF without making changes to my source code. So I want to create PDF output based on an external layout file. I'll hope you understand what I want to achieve... (and sorry for my bad english) Greetings! Pom -- http://mail.python.org/mailman/listinfo/python-list