Re: Cancel threads after timeout
I am not sure which database you're using, but all the mainstream RDBMS is server-client Architecture, when you're running one DML(including the SELECT), the RDBMS setting up a server process, the query running in that process instead of your client process, so I guess your problem can solved in the database layer more elegant than in your program. 1. Investigate the reason why you're take more time 2. Find your DBA or yourself, setting the related parameters in the database. 3. If possible, using some embedded language in the database (like PL/SQL in Oracle), it is more convenient. Regards, Jason 2013/1/26 hyperboreean > Here's the use case I want to implement - I have to generate a report > from multiple database servers. This report should be generated every 2 > hours. Sometimes it happens that a query on one of the database servers > takes longer than expected and impedes the generation of this report > (that's right, the queries are ran sequential). What I am trying to > achieve is to parallelize the queries on each database server and to be > able to cancel one of them if it takes longer than X minutes. > threading.Thread doesn't support this and seems that in > general programming languages don't implement a way to cancel threads > from the outside. > > Now, I've read all the stackoverflow threads about killing a thread, > canceling a thread after a timeout, but all of them imply that you are > able to check from within the thread if you should end the computation > or not - that's not really my case, where the computation is a SQL > query. > > So, what I have in mind is something like: the main loop starts a > threading.Thread which in turn is responsible for starting another > thread in which the actual computation happens (could be a > threading.Thread or a multiprocessing.Process) *and* checks if the > specified timeout has passed. If the time is up, it exits, letting the > main loop know. > > Lots of words, no code - let me know if you have any suggestions, ideas > to this rant. > > Thanks! > > > -- > http://mail.python.org/mailman/listinfo/python-list > -- Best wishes, Jason Ma -- http://mail.python.org/mailman/listinfo/python-list
Re: Formatting a column's value output
Τη Κυριακή, 27 Ιανουαρίου 2013 12:26:44 π.μ. UTC+2, ο χρήστης Michael Torrie έγραψε: > A tuple is one method for passing variables into the string formatter. > > So if you need to display something twice, just put in two "%s" in the > > format string, and pass it the same variable twice. Yes i know what a tuple is, iam just telling that the next code: try: cur.execute( '''SELECT URL, hits FROM counters ORDER BY hits DESC''' ) except MySQLdb.Error, e: print ( "Query Error: ", sys.exc_info()[1].excepinfo()[2] ) else: data = cur.fetchall() for row in data: print ( "" ) for item in row: print( ''' %s ''' % (item, item) ) sys.exit(0) = 1. ruteruns a dataset 2. seperate each rows 3. itermate over the items of a row. Okey, so far BUT i want the url linking to happen only for the URL column's value, and not for the hits column too. How do i apply the url link to the URL column's value only? -- http://mail.python.org/mailman/listinfo/python-list
Re: Formatting a column's value output
On Sun, Jan 27, 2013 at 8:04 PM, Ferrous Cranus wrote: > Okey, so far BUT i want the url linking to happen only for the URL column's > value, and not for the hits column too. How do i apply the url link to the > URL column's value only? Step 1: Learn to read documentation. Step 2: Learn to write code, rather than just ask someone else to do it for free. ChrisA -- http://mail.python.org/mailman/listinfo/python-list
Re: Formatting a column's value output
Τη Κυριακή, 27 Ιανουαρίου 2013 11:08:15 π.μ. UTC+2, ο χρήστης Chris Angelico έγραψε: > On Sun, Jan 27, 2013 at 8:04 PM, Ferrous Cranus wrote: > > > Okey, so far BUT i want the url linking to happen only for the URL column's > > value, and not for the hits column too. How do i apply the url link to the > > URL column's value only? > > > > Step 1: Learn to read documentation. > > Step 2: Learn to write code, rather than just ask someone else to do > > it for free. I have tried code and i have showed to you, bu i cannot get it to work as i want, thats why i ask. The solution you provided me its not correct. -- http://mail.python.org/mailman/listinfo/python-list
Re: Formatting a column's value output
On Sun, Jan 27, 2013 at 8:16 PM, Ferrous Cranus wrote: > Τη Κυριακή, 27 Ιανουαρίου 2013 11:08:15 π.μ. UTC+2, ο χρήστης Chris Angelico > έγραψε: >> On Sun, Jan 27, 2013 at 8:04 PM, Ferrous Cranus >> wrote: >> >> > Okey, so far BUT i want the url linking to happen only for the URL >> > column's value, and not for the hits column too. How do i apply the url >> > link to the URL column's value only? >> >> >> >> Step 1: Learn to read documentation. >> >> Step 2: Learn to write code, rather than just ask someone else to do >> >> it for free. > > I have tried code and i have showed to you, bu i cannot get it to work as i > want, thats why i ask. The solution you provided me its not correct. You have been given a number of hints. Coding requires work, not just complaining that someone else's code "is not correct". Either pay someone to do the work, or do it yourself, but don't expect and demand that volunteers do it for you. http://www.catb.org/esr/faqs/smart-questions.html ChrisA -- http://mail.python.org/mailman/listinfo/python-list
Re: Formatting a column's value output
Τη Κυριακή, 27 Ιανουαρίου 2013 11:26:38 π.μ. UTC+2, ο χρήστης Chris Angelico έγραψε: > You have been given a number of hints. Coding requires work, not just > > complaining that someone else's code "is not correct". Either pay > > someone to do the work, or do it yourself, but don't expect and demand > > that volunteers do it for you. This is a free usenet newsgroup about python problems, so i expect free *voluntary* help. If you dont want to help then dont. Its not that i didnt try to write the code, i tried and cannot proceed from there. -- http://mail.python.org/mailman/listinfo/python-list
Re: Cancel threads after timeout
On 01/26, Matt Jones wrote: The SQL part is not under control and getting it there requires more development time than the management is willing to allocate. So this would be a first step before refactoring that part. I'm aware that the database might not notice that I don't want to receive the result of the query, but I'm willing to assume that risk for now. Thank you. > It sounds like your real problem is with your SQL query... Is that part of > this problem under your control? Can you break the query into smaller, > quicker, pieces that you can run in a reasonable amount of time? > > If not, nesting threads might be your best programmatic solution. Heed > Jason's warning though that the SQL Server my still be working even if you > cancel an operation from the outside (which could compound your problem). > > *Matt Jones* > > > On Sat, Jan 26, 2013 at 9:43 AM, Jason Friedman wrote: > > > > Sometimes it happens that a query on one of the database servers > > > takes longer than expected and impedes the generation of this report > > > (that's right, the queries are ran sequential). What I am trying to > > > achieve is to parallelize the queries on each database server and to be > > > able to cancel one of them if it takes longer than X minutes. > > > > Only answering a small portion of your question > > Assuming you are able to figure out how to "cancel" a thread or > > process on your side, it is possible the database itself will not > > respect that. In other words, if you execute "SELECT ..." singly, > > outside of Python, and type CNTL-C, does your database quickly > > recognize you are no longer interested in the result set and stop its > > work? > > -- > > http://mail.python.org/mailman/listinfo/python-list > > > -- > http://mail.python.org/mailman/listinfo/python-list -- http://mail.python.org/mailman/listinfo/python-list
Re: The utter mess of current timezone definitions (was: Comparing offset-aware and offset-naive datetimes?)
In article , Ben Finney wrote: > Roy Smith writes: > > > but I happen to know its offset is 0 (i.e. GMT). > > As further fuel for your hatred: GMT is not the same thing as UTC+0, and > never has been. (See the definitions of those two separate timezones for > more; Wikipedia's articles are probably a good start.) Yes, I am aware of the difference between GMT and UTC (and UT0, UT1, etc). None of which really matters for my purpose. In this case, it just so happens that the original string is: Sat, 26 Jan 2013 20:10:34 GMT so if anybody is conflating GMT and UTC, it's the dateutil parser :-) -- http://mail.python.org/mailman/listinfo/python-list
Re: Comparing offset-aware and offset-naive datetimes?
In article , Ben Finney wrote: > Roy Smith writes: > > > I have two datetimes. One is offset-naive. The other is offset-aware, > > but I happen to know its offset is 0 (i.e. GMT). > > Do you know the timezone of the offset-naive value? > > Or is the above mixed up, and you mean âone is offset-aware; the other > is offset-naive, but I happen to know its offset is UTC+0â? Well, actually, what I wrote was correct, but incomplete. The naive value came from a unix timestamp, so I know it is UTC. The aware value came from dateutil's parsing of a string that looks like: Sat, 26 Jan 2013 20:10:34 GMT so I know that one's UTC too :-) Unfortunately, the boto documentation describes it only as "The string timestamp representing the last time this object was modified in S3" without specifying the exact format. I'm not sure if it's showing up in GMT because my server happens to be running on GMT, or if it always does, so I'd rather let dateutil figure it out for me. > In which case, you can create a new timezone-aware value using the > timezone-naive value and the timezone you've decided to apply:: > > >>> timestamp_c = timestamp_a.replace(tzinfo=timezone_for_a) That's what I ended up with, thanks: s3_time = dateutil.parser.parse(s3_key.last_modified) info = os.stat(file) file_time = datetime.utcfromtimestamp(info.st_mtime) utc = dateutil.tz.tzutc() file_time = file_time.replace(tzinfo=utc) if s3_time < file_time: > What I'm not fine with is politicians who think it's a fun game to > fiddle with the specific timezone definitions with little advance notice > and leave we programmers to clean up the mess they keep making. Those > people are sorely in need of a nasal infestation of parrot fleas. Yes, indeed. I've been around long enough to remember when calendar code had the "Nixon flag" and the "Ford flag", named for the first two yahoos who started playing timekeeper for a day. That was before the Olson database existed. -- http://mail.python.org/mailman/listinfo/python-list
Re: Fonts & Tinker
On Friday, January 25, 2013 10:41:36 PM UTC-6, Angel wrote: > I am changing the default font for a Tkinter application: > > > > class FuelControl(Tkinter.Frame): > > def __init__(self,master): > > self.version='0.02' > > self.font=tkFont.Font(family="Helvetica",size=18) > > print self.font.actual() You may want to check out these universal Tkinter widget methods: w.option_add(pattern, value, priority=None) w.option_clear() w.option_get(name, classname) w.option_readfile(fileName, priority=None) http://infohost.nmt.edu/tcc/help/pubs/tkinter/web/universal.html While you are there, poke around the docs a bit because there is tons of good info you are going to need in the future. May want to get familiar with the new ttk widgets and themes. -- http://mail.python.org/mailman/listinfo/python-list
Re: Formatting a column's value output
On 01/27/2013 02:04 AM, Ferrous Cranus wrote: >[...] > data = cur.fetchall() > for row in data: > print ( "" ) > > for item in row: > print( ''' href='http://www.%s?show=log'>%s ''' % (item, item) ) >[...] > Okey, so far BUT i want the url linking to happen only for the URL column's > value, and not for the hits column too. How do i apply the url link to the > URL column's value only? Ferrous, 'row' has two items (the url and the hit count) in it, right? So print each separately rather than in a loop: data = cur.fetchall() for row in data: url = row[0] hits = row[1] print ( "" ) print( " %s : % (url, hits) ) -- http://mail.python.org/mailman/listinfo/python-list
Algorithm. Tony Gaddis book 2 Starting python3
Hi Question 3 Chp2 Page 76 Adds2 to a and assigns the result to b. I have several attemtps,would like to check my answer.help please At 80 i need all the help i can find. Thanks George Smart -- http://mail.python.org/mailman/listinfo/python-list
Re: Algorithm. Tony Gaddis book 2 Starting python3
On Sun, Jan 27, 2013 at 11:29 AM, wrote: > Hi > Question 3 Chp2 Page 76 > Adds2 to a and assigns the result to b. > I have several attemtps,would like to check my answer.help please > At 80 i need all the help i can find. > Thanks George Smart > -- > http://mail.python.org/mailman/listinfo/python-list > Your question is not clear. Neither is your subject line. It seems that maybe you are using a book written by Tony Gaddis. I googled that and didn't come up with anything. Anyway, I don't have that book. You will get an answer to your question if you ask it more clearly -- perhaps quote from the book. Also, paste into your question the code you have tried and the results you have gotten. -- Joel Goldstick http://joelgoldstick.com -- http://mail.python.org/mailman/listinfo/python-list
Re: Algorithm. Tony Gaddis book 2 Starting python3
On 01/27/2013 09:29 AM, [email protected] wrote: > Hi > Question 3 Chp2 Page 76 > Adds2 to a and assigns the result to b. > I have several attemtps,would like to check my answer.help please > At 80 i need all the help i can find. > Thanks George Smart Awesome that you are learning Python! Indeed it is for all ages! What does the question say, exactly (maybe quote it)? And definitely post your answer to the question as well. It's better for us to work with what you have already. Michael -- http://mail.python.org/mailman/listinfo/python-list
Re: Formatting a column's value output
On 27-Jan-2013 17:12, [email protected] wrote: On 01/27/2013 02:04 AM, Ferrous Cranus wrote: [...] data = cur.fetchall() for row in data: print ( "" ) for item in row: print( ''' %s ''' % (item, item) ) [...] Okey, so far BUT i want the url linking to happen only for the URL column's value, and not for the hits column too. How do i apply the url link to the URL column's value only? Ferrous, 'row' has two items (the url and the hit count) in it, right? So print each separately rather than in a loop: data = cur.fetchall() for row in data: url = row[0] hits = row[1] print ( "" ) print( " %s : % (url, hits) ) It is nice to see some constructive feedback to Ferrous from the python-list. This will hopefully help to get Ferrous on the right track. -- http://mail.python.org/mailman/listinfo/python-list
Re: Formatting a column's value output
Τη Κυριακή, 27 Ιανουαρίου 2013 6:12:59 μ.μ. UTC+2, ο χρήστης [email protected] έγραψε: > On 01/27/2013 02:04 AM, Ferrous Cranus wrote: > > >[...] > > > data = cur.fetchall() > > > for row in data: > > > print ( "" ) > > > > > > for item in row: > > > print( ''' > href='http://www.%s?show=log'>%s ''' % (item, item) ) > > >[...] > > > Okey, so far BUT i want the url linking to happen only for the URL column's > > > value, and not for the hits column too. How do i apply the url link to the > > > URL column's value only? > > > > Ferrous, > > > > 'row' has two items (the url and the hit count) in it, right? > > So print each separately rather than in a loop: > > > > data = cur.fetchall() > > for row in data: > > url = row[0] > > hits = row[1] > > print ( "" ) > > print( " %s : % (url, > hits) ) Yes that can work, but i want to ask you if its possible to do the same thing from within the loop as i have it coded it until now. Is there a way to seperate the values within the loop? -- http://mail.python.org/mailman/listinfo/python-list
Re: Formatting a column's value output
On Sun, Jan 27, 2013 at 1:05 PM, Κώστας Παπαδόπουλος wrote: > Τη Κυριακή, 27 Ιανουαρίου 2013 6:12:59 μ.μ. UTC+2, ο χρήστης > [email protected] έγραψε: > > On 01/27/2013 02:04 AM, Ferrous Cranus wrote: > > > > >[...] > > > > > data = cur.fetchall() > > > > > for row in data: > > > > > print ( "" ) > > > > > > > > > > for item in row: > > > > > print( ''' > > href='http://www.%s?show=log'>%s > ''' % (item, item) ) > > > > >[...] > > > > > Okey, so far BUT i want the url linking to happen only for the URL > column's > > > > > value, and not for the hits column too. How do i apply the url link to > the > > > > > URL column's value only? > > > > > > > > Ferrous, > > > > > > > > 'row' has two items (the url and the hit count) in it, right? > > > > So print each separately rather than in a loop: > > > > > > > > data = cur.fetchall() > > > > for row in data: > > > > url = row[0] > > > > hits = row[1] > > > > print ( "" ) > > > > print( " %s : % > (url, hits) ) > > Yes that can work, but i want to ask you if its possible to do the same > thing from within the loop as i have it coded it until now. > > Is there a way to seperate the values within the loop? > for row in data: print( " %s : " % (row[0], row[1]) ) > -- > http://mail.python.org/mailman/listinfo/python-list > -- Joel Goldstick http://joelgoldstick.com -- http://mail.python.org/mailman/listinfo/python-list
Re: Formatting a column's value output
Τη Κυριακή, 27 Ιανουαρίου 2013 8:36:42 μ.μ. UTC+2, ο χρήστης Joel Goldstick έγραψε: > On Sun, Jan 27, 2013 at 1:05 PM, Κώστας Παπαδόπουλος > wrote: > > Τη Κυριακή, 27 Ιανουαρίου 2013 6:12:59 μ.μ. UTC+2, ο χρήστης [email protected] > έγραψε: > > > > > > On 01/27/2013 02:04 AM, Ferrous Cranus wrote: > > > > > > >[...] > > > > > > > data = cur.fetchall() > > > > > > > for row in data: > > > > > > > print ( "" ) > > > > > > > > > > > > > > for item in row: > > > > > > > print( ''' > > href='http://www.%s?show=log'>%s ''' % (item, item) ) > > > > > > > >[...] > > > > > > > Okey, so far BUT i want the url linking to happen only for the URL > > > column's > > > > > > > value, and not for the hits column too. How do i apply the url link to the > > > > > > > URL column's value only? > > > > > > > > > > > > Ferrous, > > > > > > > > > > > > 'row' has two items (the url and the hit count) in it, right? > > > > > > So print each separately rather than in a loop: > > > > > > > > > > > > data = cur.fetchall() > > > > > > for row in data: > > > > > > url = row[0] > > > > > > hits = row[1] > > > > > > print ( "" ) > > > > > > print( " %s : % > > (url, hits) ) > > > > Yes that can work, but i want to ask you if its possible to do the same thing > from within the loop as i have it coded it until now. > > > > Is there a way to seperate the values within the loop? > > > > for row in data: > > print( " %s : " % > (row[0], row[1]) ) This is not correct. the attribute is for url (row[0]) only, not for url and hits too. i want the following working code: = data = cur.fetchall() for row in data: url = row[0] hits = row[1] print ( "" ) print( " %s " % (url, url) ) print( " %s " % (hits) ) = inside the loop i posted initially, if its possible. -- http://mail.python.org/mailman/listinfo/python-list
Re: looking for advice python
I am in a class and was just looking for different advice. This is the first time iv ever tried to do this. That's all that iv taken from two chapters and wondering how bad I did. I also like to learn. Thanks for everyones input -- http://mail.python.org/mailman/listinfo/python-list
Re: Formatting a column's value output
On 01/27/2013 11:44 AM, Κώστας Παπαδόπουλος wrote: > This is not correct. > the attribute is for url (row[0]) only, not for url and hits too. > > i want the following working code: > > = > data = cur.fetchall() > for row in data: > url = row[0] > hits = row[1] > print ( "" ) > print( " %s > " % (url, url) ) > print( " %s " % (hits) ) > = > > inside the loop i posted initially, if its possible. The easiest way is to separate them when you read 'row' data = cur.fetchall() for row in data: for url,hits in row: print ( "" ) print( " %s : % (url, hits) ) Note that sql query you used guaranties that each row will contain exactly two items so there is no point in using a loop to go through each row item as you would if you did not know how many items it contained. But if you insist on going through them item by item in a loop, then you need to figure out which item is which inside the loop. One way is like: data = cur.fetchall() for row in data: print ( "" ) item_position = 0 for item in row: if item_position == 0: print( " %s :" % (item[0], item[0])) if item_position == 1: print( " %s " % item[1] ) item_position += 1 There are many variations of that. You can simplify the above a little by getting rid of the "item_position=" lines and replacing "for item in row:" with "for item_position, item = enumerate (item):" But I think you can see the very first code example above is way more simple. By the way, it would be a lot easier to read your posts if you would snip out the extra blank lines that Google Groups adds. There are some suggestions in http://wiki.python.org/moin/GoogleGroupsPython -- http://mail.python.org/mailman/listinfo/python-list
Re: Algorithm. Tony Gaddis book 2 Starting python3
Hello George, I found the book you're referring to via google (searching for: tony gaddis starting out with python). The question is this: 3. Write assignment statements that perform the following operations with the variables a,b and c. a. Adds 2 to a and assigns the result to b b. Multiplies b times 4 and assigns the result to a c. Divides a by 3.14 and assigns the result to b d. Subtracts 8 from and assigns the result to a The typical way this list helps in homework assignments is for the student to post his solution first, and then let someone explain if/why something is wrong, and how to go about the right solution. I think you will have a lot of fun learning python. It's very rewarding. Best regards, Stefaan. -- http://mail.python.org/mailman/listinfo/python-list
Re: Algorithm. Tony Gaddis book 2 Starting python3
d. Subtracts 8 from and assigns the result to a this should read: "Subtracts 8 from b and assigns the result to a" Funny how none of the assignments involve a variable c, despite the question :) -- http://mail.python.org/mailman/listinfo/python-list
Re: Formatting a column's value output
Τη Κυριακή, 27 Ιανουαρίου 2013 9:12:16 μ.μ. UTC+2, ο χρήστης [email protected] έγραψε: > Yes indeed, there is no need to use a loop since i know the exact number of items i'am expecting. Thanks you very much for clarifying this to me: One last thing i want to ask you: try: cur.execute( '''SELECT host, userOS, browser, hits, lastvisit FROM visitors WHERE counterID = (SELECT ID FROM counters WHERE URL = %s) ORDER BY lastvisit DESC''', (htmlpage,) ) except MySQLdb.Error, e: print ( "Query Error: ", sys.exc_info()[1].excepinfo()[2] ) else: data = cur.fetchall() for host, useros, browser, hits, lastvisit in data: print ( "" ) for item in host, useros, browser, hits, lastvisit.strftime('%A %e %b, %H:%M').decode('cp1253').encode('utf8'): print ( " %s " % item ) sys.exit(0) === That would be also written as: for row in data: print ("tr>") for item in row: print( "blah blah blah" ) And that would make the code easier to read and more clear, but its that 'lastvisit' column's value than needs formating,hence it makes me use the above syntax. Is there any simpler way to write the above working code without the need to specify all of the columns' names into the loop? -- http://mail.python.org/mailman/listinfo/python-list
Re: Formatting a column's value output
On 01/27/2013 03:24 PM, Κώστας Παπαδόπουλος wrote: Τη Κυριακή, 27 Ιανουαρίου 2013 9:12:16 μ.μ. UTC+2, ο χρήστης [email protected] έγραψε: >> > > Yes indeed, there is no need to use a loop since i know the exact number of items i'am expecting. Thanks you very much for clarifying this to me: > One last thing i want to ask you: > > > try: > cur.execute( '''SELECT host, userOS, browser, hits, lastvisit FROM visitors > WHERE counterID = (SELECT ID FROM counters WHERE URL = %s) ORDER BY lastvisit DESC''', (htmlpage,) ) > except MySQLdb.Error, e: > print ( "Query Error: ", sys.exc_info()[1].excepinfo()[2] ) > else: > data = cur.fetchall() > > for host, useros, browser, hits, lastvisit in data: > print ( "" ) > > for item in host, useros, browser, hits, lastvisit.strftime('%A %e %b, %H:%M').decode('cp1253').encode('utf8'): > print ( " %s " % item ) > > sys.exit(0) > === > > That would be also written as: > > for row in data: > print ("tr>") > for item in row: > print( "blah blah blah" ) > > And that would make the code easier to read and more clear, but its that 'lastvisit' column's value than needs formating,hence it makes me use the above syntax. > > Is there any simpler way to write the above working code without the need to specify all of the columns' names into the loop? You can write: for row in data: print ("tr>") row = list(row) row[-1] = row[-1].strftime(...) for item in row: print( "blah blah blah" ) - mitya -- Lark's Tongue Guide to Python: http://lightbird.net/larks/ The existence of any evil anywhere at any time absolutely ruins a total optimism. George Santayana -- http://mail.python.org/mailman/listinfo/python-list
Re: Formatting a column's value output
On 01/27/2013 01:50 PM, Mitya Sirenef wrote: > On 01/27/2013 03:24 PM, Κώστας Παπαδόπουλος wrote: >> Τη Κυριακή, 27 Ιανουαρίου 2013 9:12:16 μ.μ. UTC+2, ο χρήστης >> [email protected] έγραψε: > >> > > > > Yes indeed, there is no need to use a loop since i know the exact > number of items i'am expecting. Thanks you very much for clarifying this > to me: > > One last thing i want to ask you: > > > > > > try: > > cur.execute( '''SELECT host, userOS, browser, hits, lastvisit FROM > visitors > > WHERE counterID = (SELECT ID FROM counters WHERE URL = %s) ORDER BY > lastvisit DESC''', (htmlpage,) ) > > except MySQLdb.Error, e: > > print ( "Query Error: ", sys.exc_info()[1].excepinfo()[2] ) > > else: > > data = cur.fetchall() > > > > for host, useros, browser, hits, lastvisit in data: > > print ( "" ) > > > > for item in host, useros, browser, hits, lastvisit.strftime('%A %e > %b, %H:%M').decode('cp1253').encode('utf8'): > > print ( " %s " % item ) > > > > sys.exit(0) > > === > > > > That would be also written as: > > > > for row in data: > > print ("tr>") > > for item in row: > > print( "blah blah blah" ) > > > > And that would make the code easier to read and more clear, but its > that 'lastvisit' column's value than needs formating,hence it makes me > use the above syntax. > > > > Is there any simpler way to write the above working code without the > need to specify all of the columns' names into the loop? > > > You can write: > > for row in data: > print ("tr>") > row = list(row) > row[-1] = row[-1].strftime(...) > for item in row: > print( "blah blah blah" ) Or alternatively, for row in data: print ("tr>") for item_num, item in enumerate (row): if item_num != len(row) - 1: print( "blah blah blah" ) else: print( item.strftime(...) ) Or, being a little clearer, LASTVISIT_POS = 4 for row in data: print ("tr>") for item_num, item in enumerate (row): if item_num != LASTVISIT_POS: print( "blah blah blah" ) else: print( item.strftime(...) ) -- http://mail.python.org/mailman/listinfo/python-list
simple tkinter battery monitor
I tried to write a simple battery monitor for laptops which shows normally just
the battery percentage, and when is clicked some more info.
If I click just one time it works, but if I click a second time, the additional
info Label seems to be empty (but it holds the dimension of the StringVar
content, even if it isn't displayed)
Where am I wrong?
---
#!/usr/bin/python3.2
from re import findall, search
from threading import Thread
from time import sleep
from subprocess import Popen, call, PIPE, STDOUT
from tkinter import *
class battery_monitor:
def __init__(self):
root=Tk()
#root.geometry("-0+0")
root.overrideredirect(True)
root.wm_attributes("-topmost", 1)
self.battery_string=StringVar()
self.battery_percent=StringVar()
self.battery_label=Label(root, extvariable=self.battery_string,
font=("fixed", 9))
self.battery_icon=Label(root,
textvariable=self.battery_percent, font=("fixed", 9), width=3)
self.battery_icon.grid()
t=Thread(target=self.update_battery_level_loop)
t.start()
root.bind("", self.display_details)
self.root=root
root.mainloop()
# displays a message about details of battery status
# i.e. "on-line" or "charging, 20 min left" and so on
def display_details(self, event):
self.battery_icon.grid_remove()
self.battery_label.grid()
self.root.update_idletasks()
sleep(1)
self.battery_label.grid_remove()
self.battery_icon.grid()
self.root.update_idletasks()
# dummy function used just to test the GUI
def read_battery_level(self):
self.level=100
return "battery is full"
# threaded function, should constantly update the battery level
def update_battery_level_loop(self):
self.read_battery_level()
while True:
self.battery_percent.set(self.level)
self.battery_string.set(self.read_battery_level())
sleep(5)
##
#
# main
battery_monitor()
--
http://mail.python.org/mailman/listinfo/python-list
Re: simple tkinter battery monitor
On 01/27/2013 05:59 PM, [email protected] wrote: I tried to write a simple battery monitor for laptops which shows normally just the battery percentage, and when is clicked some more info. If I click just one time it works, but if I click a second time, the additional info Label seems to be empty (but it holds the dimension of the StringVar content, even if it isn't displayed) Where am I wrong? See inline comment. --- #!/usr/bin/python3.2 from re import findall, search from threading import Thread from time import sleep from subprocess import Popen, call, PIPE, STDOUT from tkinter import * class battery_monitor: def __init__(self): root=Tk() #root.geometry("-0+0") root.overrideredirect(True) root.wm_attributes("-topmost", 1) self.battery_string=StringVar() self.battery_percent=StringVar() self.battery_label=Label(root, extvariable=self.battery_string, font=("fixed", 9)) I don't know tkinter very well, and I don't have 3.2, but the keyword seems to be wrong. Don't you want textvariable= ?? self.battery_icon=Label(root, textvariable=self.battery_percent, font=("fixed", 9), width=3) self.battery_icon.grid() t=Thread(target=self.update_battery_level_loop) t.start() root.bind("", self.display_details) self.root=root root.mainloop() # displays a message about details of battery status # i.e. "on-line" or "charging, 20 min left" and so on def display_details(self, event): self.battery_icon.grid_remove() self.battery_label.grid() self.root.update_idletasks() sleep(1) Never use sleep() inside a gui's main thread. There are other ways to delay the UI without locking it up. self.battery_label.grid_remove() self.battery_icon.grid() self.root.update_idletasks() # dummy function used just to test the GUI def read_battery_level(self): self.level=100 return "battery is full" # threaded function, should constantly update the battery level def update_battery_level_loop(self): self.read_battery_level() while True: self.battery_percent.set(self.level) self.battery_string.set(self.read_battery_level()) sleep(5) This sleep() is okay. ## # # main battery_monitor() -- DaveA -- http://mail.python.org/mailman/listinfo/python-list
Re: simple tkinter battery monitor
On Sunday, January 27, 2013 4:59:20 PM UTC-6, [email protected] wrote: > I tried to write a simple battery monitor for laptops > which shows normally just the battery percentage, and when > is clicked some more info. > > If I click just one time it works, but if I click a second > time, the additional info Label seems to be empty (but it > holds the dimension of the StringVar content, even if it > isn't displayed) > > Where am I wrong? Before i discover your code logic error, i want to expose style errors. > --- > #!/usr/bin/python3.2 > > from re import findall, search > from threading import Thread > from time import sleep > from subprocess import Popen, call, PIPE, STDOUT > from tkinter import * I am wondering why you would "selectively" import from the re module, which is a quite normal sized module, but then do the global import from tkinter, of which who's namespace is terribly polluted due to lack of packaging. > class battery_monitor: Bad naming convention here! ALL class identifiers must (at the very least) /start/ with a capital letter. My strong opinion is to cap EVERY word. So for example: OptionMenu NOT Optionmenu ComboBox NOT Combobox etc... Using "lowercase_with_underscores" should be reserved for interface methods and global functions. > def __init__(self): > root=Tk() > #root.geometry("-0+0") > root.overrideredirect(True) > root.wm_attributes("-topmost", 1) I just hate when people use 1 and 0 for True and False. I know Python allows such non-sense, but i just hate it because it can cause subtle bugs -- and most logical people agree with me. > self.battery_string=StringVar() > self.battery_percent=StringVar() Two issues here. First you use a function/interface style to define a variable. Then you go and add insult to injury by using an ambiguous name. You should have used something like: "batteryStringVar" and "batteryPercentVar". > self.battery_label=Label(root, > extvariable=self.battery_string, font=("fixed", 9)) > self.battery_icon=Label(root, > textvariable=self.battery_percent, font=("fixed", 9), width=3) > self.battery_icon.grid() > t=Thread(target=self.update_battery_level_loop) Don't assign variables without leaving buffer spaces around the equals sign. Only omit the spaces when passing arguments to a class, method, or function. > t.start() > root.bind("", self.display_details) > self.root=root > root.mainloop() > > # displays a message about details of battery status > # i.e. "on-line" or "charging, 20 min left" and so on Oh gawd this is a major pet peeve of mine Always place a comment at the same indention level of the function or class that it references. AND NEVER, EVER, place a comment OUTSIDE of a function, method, or class like you have done here. > def display_details(self, event): > self.battery_icon.grid_remove() > self.battery_label.grid() > self.root.update_idletasks() > sleep(1) You may want to look into the Universal Tkinter widget method: "w.after(ms, func)". > self.battery_label.grid_remove() > self.battery_icon.grid() > self.root.update_idletasks() > > # dummy function used just to test the GUI > def read_battery_level(self): > self.level=100 > return "battery is full" > > # threaded function, should constantly update the battery level > def update_battery_level_loop(self): > self.read_battery_level() > while True: > self.battery_percent.set(self.level) > self.battery_string.set(self.read_battery_level()) > sleep(5) Finally, i don't understand why you are using such a deep indention level. Unknown Source said: "Four spaces thou shalt indent, and the number of thou indention shall be four." Only after you clean up these style abominations by submitting a new example of your code will i offer any more help. Thanks. -- http://mail.python.org/mailman/listinfo/python-list
Re: Cancel threads after timeout
> On 26Jan2013 09:48, Matt Jones wrote: > | It sounds like your real problem is with your SQL query... Is that part of > | this problem under your control? Can you break the query into smaller, > | quicker, pieces that you can run in a reasonable amount of time? > > Another option to investigate is whether you can ask the database itself > to limit the run time of a query. Of course, that will abort the query > but so does your proposed solution. > > Another approach might be to simply run each query regularly (with a > pause between so the database is not spending its whole life running > your query). Snapshot each latest result. Compute your report from the > latest pair of snapshots at any given time on an independent schedule. > It may not be valid for what you need, but if it is then this decouples > you from the query time completely. Along these lines, if you are running on Linux then the bash shell comes with a "timeout" command, which you can prepend to snapshot requests. -- http://mail.python.org/mailman/listinfo/python-list
Re: Cancel threads after timeout
On 27Jan2013 21:57, Jason Friedman wrote: | > On 26Jan2013 09:48, Matt Jones wrote: | > | It sounds like your real problem is with your SQL query... Is that part of | > | this problem under your control? Can you break the query into smaller, | > | quicker, pieces that you can run in a reasonable amount of time? | > | > Another option to investigate is whether you can ask the database itself | > to limit the run time of a query. Of course, that will abort the query | > but so does your proposed solution. | > | > Another approach might be to simply run each query regularly (with a | > pause between so the database is not spending its whole life running | > your query). Snapshot each latest result. Compute your report from the | > latest pair of snapshots at any given time on an independent schedule. | > It may not be valid for what you need, but if it is then this decouples | > you from the query time completely. | | Along these lines, if you are running on Linux then the bash shell | comes with a "timeout" command, which you can prepend to snapshot | requests. I was thinking that if he does something equivalent to: while :; do run-sql-query1-with-snapshot-of-result; sleep 900; done & while :; do run-sql-query2-with-snapshot-of-result; sleep 900; done & while : do report on latest pair of snapshots sleep 7200 done Then he doesn't need any timeouts. Cheers, -- Cameron Simpson -- http://mail.python.org/mailman/listinfo/python-list
