Re: please i need explanation
Hi! Since there is no stated question, I need to guess: n -= 1 (instead of "f -= 1") should work. Or maybe the question was a totally different one... -Kimmo 11.01.2013 17:35, kwakukwat...@gmail.com wrote: def factorial(n): if n<2: return 1 f = 1 while n>= 2: f *= n f -= 1 return f -- http://mail.python.org/mailman/listinfo/python-list
Keyboard hook in linux
Hi! I am working on a small console app for linux. The idea is to display some sensor values and the screen should update itself in, say, every 10 seconds. The user should have the possibly to change some configurations or gwt help by pressing different keys (like you can do when running e.g. 'top'). In other words: the program should NOT wait for the keypress (so input() is not the solution), but simply capture keypresses and act accordingly. If no key is pressed, the program should continue updating the screen. Practically I am looking for something similar than Pascal's "keypressed" function (http://www.freepascal.org/docs-html/rtl/crt/keypressed.html). The python code would be something like this: --- snip --- while True: if keypressed: ch=screen.getch() # From 'curses' # Test, if 'ch' is a valid key # Do what the user want read_sensors() update_screen() --- snip --- I have searched in the Web and in several tutorials (e.g. "Programming python"), but this seems to be a tricky one. The 'pyHook' library seems to offer a keyboard hook manager, but 'pyHook' is not available for linux :( IMHO, the 'curses' library offers no (direct) solution to this... Does anybody have an idea / a solution, how to capture keystrokes in linux? Kind regards, Kimmo -- http://mail.python.org/mailman/listinfo/python-list
Re: Keyboard hook in linux
Hi! Thanks, Michael, for your quick - and heplful - reply. 13.01.2013 18:46, Michael Torrie wrote: You're wrong. curses does offer a direct solution to this. Check the docs. Also here's a nice intro document for Python 3: http://docs.python.org/dev/howto/curses.html You are right :) The docs tell us (I somehow missed this when reading the doc last time): "It’s possible to change this behavior with the method nodelay(). After nodelay(1), getch() for the window becomes non-blocking and returns curses.ERR (a value of -1) when no input is ready. There’s also a halfdelay() function, which can be used to (in effect) set a timer on each getch(); if no input becomes available within a specified delay (measured in tenths of a second), curses raises an exception." This is actually funny: if you google for e.g. "capture keystrokes python", you will find masses of suggestions, none of them having this simple and elegant (i.e. python-like :) ) solution included. Now it works and my problem is solved. Thank you! Kind regards, Kimmo -- http://mail.python.org/mailman/listinfo/python-list
Re: Deviding N(1,2,3,..,N) part from numeric list as summation of each values(don't sorted) has highest as possible.
Hi! Could it be, "Nuen9", that you would like to find a split where the split sums are close to each other? In other words, you define the number of splits (in your example: 3) and the algortihm should test all possible combinations and select the split where the sum differences are smallest. Best, Kimmo 10.10.2016, 18:20, Nuen9 wrote: เมื่อ วันจันทร์ที่ 10 ตุลาคม ค.ศ. 2016 21 นาฬิกา 31 นาที 25 วินาที UTC+7, Steve D'Aprano เขียนว่า: On Tue, 11 Oct 2016 12:38 am, amornsak@gmail.com wrote: I'm sorry for my English language. OK, I have list = [10,20,30,40,110,50,18,32,5] and want to find three values of summation from my list by split any way list but three values of summation has closely example. [10,20,30], [40,110,50], [18,32,5] = 60, 200, 55 -> it is not closely *** 60 = 10+20+30, 200 = 40+110+50, 18+32+5 = 55 and [10], [20,30,40,110,50,18], [32,5] = 10, 268, 37 -> and it is not closely and [10,20,30,40],[110],[50,18,32,5] = 100,110,105 -> it's closely The Really answers that I want is [10,20,30,40],[110],[50,18,32,5] = 100,110,105 from list[10,20,30,40,110,50,18,32,5] And I don't know what I have to explain for my questions in English because My English so bad T_T Thank you for reading my text (; -- https://mail.python.org/mailman/listinfo/python-list
Re: Deviding N(1,2,3,..,N) part from numeric list as summation of each values(don't sorted) has highest as possible.
Hi! Here one possible solution: --- snip --- land = [10,20,30,40,110,50,18,32,5] landlength=len(land) winnersplit=[] for i in range(landlength-2): for j in range(landlength-1-i): splitsums=[sum(land[0:(i+1)]), sum(land[(i+1):(i+j+2)]), sum(land[(i+j+2):landlength])] differences=abs(splitsums[1]-splitsums[0])+abs(splitsums[2]-splitsums[1]) if (len(winnersplit)==0 or differences<(abs(sum(winnersplit[1])-sum(winnersplit[0]))+abs(sum(winnersplit[2])-sum(winnersplit[1]: winnersplit=[land[0:(i+1)], land[(i+1):(i+j+2)], land[(i+j+2):landlength]] print(splitsums," <<< WE HAVE A NEW LEADER! >>>") else: print(splitsums, " <<< split differences too large :( >>>") print("And the winner is ... ", winnersplit) --- snip --- HTH, Kimmo 10.10.2016, 19:25, Nuen9 kirjoitti: Hi! Could it be, "Nuen9", that you would like to find a split where the split sums are close to each other? In other words, you define the number of splits (in your example: 3) and the algortihm should test all possible combinations and select the split where the sum differences are smallest. Best, Kimmo Yes it is, I want example python code for finding my answers but I don't code my answers. please help me. -- https://mail.python.org/mailman/listinfo/python-list
SSL certification fails / tweepy
Dear list members! I have written I small python script for twitter mining utilising the 'tweepy' library. Since a couple of days I cannot use the script anymore, due to a "ssl certificate verification failed" error. The authentication with Twitter API succeess, but when I try to run the following code: --- snip --- api=tweepy.API(auth) # 'auth' is my authentification token print("Receiving network members for '", user_to_track, "' ... ") network_members = [] # # The user wants to get all followers of a twitter user # while True: try: for page in tweepy.Cursor(api.followers_ids, user_to_track).pages(): network_members.extend(page) print("Current follower count: ", len(network_members)) if len(page)==5000: time.sleep(60) break except tweepy.TweepError as e: # Did we exceed the rate limit? if e.message[0]["code"]==88: # Yes -> sleep for 15 minutes and then continue print("Rate limit exceeded, sleeping for 15 minutes ...") time.sleep(15*60) continue else: # Something else went wrong -> break out from the loop! print("Exception: ", e.message[0], " (", e.code[0], ") --> interrupting, sorry!") break --- snip --- the execution is broken with an "ŚSL_VERIFICATION_FAILED" error. I have tried both python 2.7 and 3.x, no difference :( I have also upgraded 'tweepy', no help. I suspect this being somehow related to my current system, i.e. openSuSE Tumbleweed. I tested the script on a ubuntu system and encountered no problems... But I need to get the script running also with my openSuSE :) Any ideas what could be causing this weird problem? Thanks in advance! Kimmo -- https://mail.python.org/mailman/listinfo/python-list
Re: SSL certification fails / tweepy [SOLVED]
Hi! 14.01.2016, 09:59, dieter wrote: "SSL_VERIFICATION_FAILED" is an error which occurs when an SSL ("https") connection is established. It happens when the SSL certificate (of the server and/or client) does not contain expected data - e.g. the certificate is no longer (or not yet) valid or its subject does not reference the connected entity or the certificate chain cannot be verified. Look for Python documentation on SSL verification to learn how to properly set up things for successful verfication. Among others, you will need have somewhere information about trusted root certificates. Thanks, dieter, for your reply. I have now re-installed both certificate packages and (open)ssl related python packages - and now it works! I still wonder why openSuSE certificates are not working out-of-box with python. Maybe some of the packages were not correctly installed... Anyway, thanks for your quick reply - the issue is now solved :) Best, Kimmo -- https://mail.python.org/mailman/listinfo/python-list
Re: merging two csv files
Hi! Is this a homework or something you need a quick solution for? For the latter: 'man paste' (on Linux) :) Anyway, some sample data and code would be good. BR, Kimmo 03.03.2016, 11:50, m.t.e...@student.rug.nl wrote: Hey! I want to merge column-wise two csv files, say: file1.csv and file2.csv, both containing two columns, into a new csv file. I could not find a good code for doing this. It never really worked. If you could show me the code with this example, I would highly appreciate it. Best wishes, Tiber -- https://mail.python.org/mailman/listinfo/python-list
Re: nested loops
Hi! > leonardo writes: how can i have it print a row of stars beside each number, like this?: how many seconds?: 5 5 * * * * * 4 * * * * 3 * * * 2 * * 1 * blast off! --- snip --- sec = int(input("How many seconds? ")) for i in range(0,sec): print str(sec-i)+":"+" *"*(sec-i) print "blast off!" --- snip --- Please note: the value for the upper bound is not included in the range. In my example, the actual range is from 0 to 4. HTH, Kimmo -- http://mail.python.org/mailman/listinfo/python-list