python-2.x

Keyboard interruptable blocking queue in Python

大憨熊 提交于 2020-02-01 15:34:54
问题 It seems import Queue Queue.Queue().get(timeout=10) is keyboard interruptible (ctrl-c) whereas import Queue Queue.Queue().get() is not. I could always create a loop; import Queue q = Queue() while True: try: q.get(timeout=1000) except Queue.Empty: pass but this seems like a strange thing to do. So, is there a way of getting an indefinitely waiting but keyboard interruptible Queue.get()? 回答1: Queue objects have this behavior because they lock using Condition objects form the threading module.

Keyboard interruptable blocking queue in Python

南楼画角 提交于 2020-02-01 15:32:05
问题 It seems import Queue Queue.Queue().get(timeout=10) is keyboard interruptible (ctrl-c) whereas import Queue Queue.Queue().get() is not. I could always create a loop; import Queue q = Queue() while True: try: q.get(timeout=1000) except Queue.Empty: pass but this seems like a strange thing to do. So, is there a way of getting an indefinitely waiting but keyboard interruptible Queue.get()? 回答1: Queue objects have this behavior because they lock using Condition objects form the threading module.

Keyboard interruptable blocking queue in Python

拟墨画扇 提交于 2020-02-01 15:31:36
问题 It seems import Queue Queue.Queue().get(timeout=10) is keyboard interruptible (ctrl-c) whereas import Queue Queue.Queue().get() is not. I could always create a loop; import Queue q = Queue() while True: try: q.get(timeout=1000) except Queue.Empty: pass but this seems like a strange thing to do. So, is there a way of getting an indefinitely waiting but keyboard interruptible Queue.get()? 回答1: Queue objects have this behavior because they lock using Condition objects form the threading module.

How to optionally repeat a program in python

匆匆过客 提交于 2020-01-30 11:13:05
问题 I'm learning python and had a quick question. I have to write a code to find the cube root, which I've done. I want to give the user the option of calculating another cube root, or quitting. Here's what I have come up with: x = int(raw_input('Enter an integer: ')) ## start guessing with 0 ans = 0 while ans*ans*ans < abs(x): ans = ans + 1 print 'current guess =', ans print 'last guess = ', ans print 'ans*ans*ans = ', ans*ans*ans ##if its a perfect cube if ans*ans*ans == abs(x): ## perfect, but

Dumping unicode with YAML

六月ゝ 毕业季﹏ 提交于 2020-01-24 19:49:05
问题 I'm creating yaml files from csv's that have a lot of unicode characters in them but I can't seem to get it to dump the unicode without it giving me a Decode Error. I'm using the ruamel.yaml library. UnicodeDecodeError: 'ascii' codec can't decode byte 0xc2 in position 11: ordinal not in range(128) I've tried parsing strings, unicode strings, encoding with "utf-8" nothing seems to work. I've seen a lot of examples that show adding a representer to solve the issue but they all seem to be using

Round Function does not work in v2.6.6, but works in v3.4.2

∥☆過路亽.° 提交于 2020-01-24 15:03:07
问题 My round function does not work in linux python 2.6.6, whereas it works fine in Windows 3.4.2 after using the following type of code: Array[i] = round(math.e ** AnotherArray[i], 4) v.3.4.2: 0.0025999999999999999 => 0.0026 v.2.6.6: 0.0025999999999999999 => 0.0025999999999999999 回答1: They both work the same, but Python 2.7 and up will round floating point numbers when printing their repr esentations , in order to not confuse users by the (language- and machine-independent) limitations of

Python - input of file path

坚强是说给别人听的谎言 提交于 2020-01-17 12:49:48
问题 this code works fine when I put the path of the file myself. but when I want to get it from users raw_input() it doesn't work. what can I do? import string import random print "enter number between 6 and 20" n = raw_input() print "enter pathway of file" p = raw_input() print "creating a new text file" new_file = open(p, "w") #the error on this line m = int(n) print random.choice(string.ascii_lowercase) for i in range(0,m): for j in range(0,m): new_file.write(random.choice(string.ascii

Not losing the quality of pictures saved with cv2.imwrite()

送分小仙女□ 提交于 2020-01-17 04:15:25
问题 I am wondering seriously about the effects of cv2.imwrite() function of OpenCV. I noticed that when I read pictures with cv2.imread() and save them again with cv2.imwrite() function, their quality is not the same any more for the human eyes. I ask you how can I keep the quality of the image the same as the original after saving it using cv2.imwrite() function. I ask this question because I have really a serious issue in a larger program and when I checked the quality of the pictures saved by

Not losing the quality of pictures saved with cv2.imwrite()

坚强是说给别人听的谎言 提交于 2020-01-17 04:15:09
问题 I am wondering seriously about the effects of cv2.imwrite() function of OpenCV. I noticed that when I read pictures with cv2.imread() and save them again with cv2.imwrite() function, their quality is not the same any more for the human eyes. I ask you how can I keep the quality of the image the same as the original after saving it using cv2.imwrite() function. I ask this question because I have really a serious issue in a larger program and when I checked the quality of the pictures saved by

Python - send login packet to Minecraft Server

邮差的信 提交于 2020-01-16 19:45:23
问题 Is there a way to send a login packet to minecraft server from python? Here is what i have right now: import socket client = socket.socket(socket.AF_INET, socket.SOCK_STREAM) addr = ("localhost", 25565) client.connect(addr) client.sendall(chr(0x02)) client.sendall(chr(0xFD)) client.sendall(chr(0xCD)) # After sending this line server still don't kick me client.sendall(chr(0x06)+str(117)+str(70)+str(-46)) # And now server kicks me :-( client.sendall(chr(0x03)+str("Hello World")) print client