python-3.2

Python/Gtk3 : How to add a Gtk.Entry to a Gtk.MessageDialog?

旧巷老猫 提交于 2019-12-04 04:37:14
问题 Good morning, I'm trying to add a Gtk.Entry to a Gtk.MessageDialog . With the following code it seems that I added the Gtk.Entry but it's not visible on the dialog window (Python3/Gtk3) : #!/usr/bin/python3 from gi.repository import Gtk def get_user_pw(parent, message, default=''): dialogWindow = Gtk.MessageDialog(parent, Gtk.DialogFlags.MODAL | Gtk.DialogFlags.DESTROY_WITH_PARENT, Gtk.MessageType.QUESTION, Gtk.ButtonsType.OK_CANCEL, message) dialogBox = dialogWindow.get_content_area()

AttributeError: 'Pool' object has no attribute '__exit__'

谁说胖子不能爱 提交于 2019-12-03 13:13:25
I'm doing some multiprocessing python scripts using multiprocessing.Pool . These scripts look like the following: from multiprocessing import Pool def f(x): return x*x if __name__ == '__main__': with Pool(processes=4) as pool: # start 4 worker processes print(pool.map(f, range(10))) # prints "[0, 1, 4,..., 81]" When running this with Python 3.4, everything is fine. However, when using Python 2.6 or 3.1 I get this error: AttributeError: 'Pool' object has no attribute '__exit__' Using Python 2.7 or 3.2 , the error is essentially the same: AttributeError: __exit__ Why does this happen and how can

Hashing an immutable dictionary in Python

怎甘沉沦 提交于 2019-12-03 10:30:51
Short version: What's the best hashing algorithm for a multiset implemented as a dictionary of unordered items? I'm trying to hash an immutable multiset (which is a bag or multiset in other languages: like a mathematical set except that it can hold more than one of each element) implemented as a dictionary. I've created a subclass of the standard library class collections.Counter , similar to the advice here: Python hashable dicts , which recommends a hash function like so: class FrozenCounter(collections.Counter): # ... def __hash__(self): return hash(tuple(sorted(self.items()))) Creating the

Usage of pickle.dump in Python

走远了吗. 提交于 2019-12-03 08:32:25
问题 I'm trying to learn how to use the pickle module in Python: import pickle x = 123 f = open('data.txt','w') pickle.dump(x,f) Here's what I get: Traceback (most recent call last): File "D:\python\test.py", line 5, in <module> pickle.dump(x,f) TypeError: must be str, not bytes However, this code works just fine: import pickle dump = pickle.dump(123) print(dump) What am I doing wrong? 回答1: The problem is that you're opening the file in text mode. You need to use binary here: >>> f = open('data

Python dictionary.keys() error

戏子无情 提交于 2019-12-03 05:31:53
问题 I am trying to use the .keys() and instead of getting a list of the keys like always have in the past. However I get this. b = { 'video':0, 'music':23 } k = b.keys() print( k[0] ) >>>TypeError: 'dict_keys' object does not support indexing print( k ) dict_keys(['music', 'video']) it should just print ['music', 'video'] unless I'm going crazy. What's going on? 回答1: Python 3 changed the behavior of dict.keys such that it now returns a dict_keys object, which is iterable but not indexable (it's

Usage of pickle.dump in Python

筅森魡賤 提交于 2019-12-03 00:01:20
I'm trying to learn how to use the pickle module in Python: import pickle x = 123 f = open('data.txt','w') pickle.dump(x,f) Here's what I get: Traceback (most recent call last): File "D:\python\test.py", line 5, in <module> pickle.dump(x,f) TypeError: must be str, not bytes However, this code works just fine: import pickle dump = pickle.dump(123) print(dump) What am I doing wrong? The problem is that you're opening the file in text mode. You need to use binary here: >>> f = open('data.txt','w') >>> pickle.dump(123,f) Traceback (most recent call last): File "<stdin>", line 1, in <module>

Python dictionary.keys() error

删除回忆录丶 提交于 2019-12-02 17:52:47
I am trying to use the .keys() and instead of getting a list of the keys like always have in the past. However I get this. b = { 'video':0, 'music':23 } k = b.keys() print( k[0] ) >>>TypeError: 'dict_keys' object does not support indexing print( k ) dict_keys(['music', 'video']) it should just print ['music', 'video'] unless I'm going crazy. What's going on? Python 3 changed the behavior of dict.keys such that it now returns a dict_keys object, which is iterable but not indexable (it's like the old dict.iterkeys , which is gone now). You can get the Python 2 result back with an explicit call

idle-python3.2 not starting: complains of NameError: name 'Tk' is not defined

一世执手 提交于 2019-12-02 11:31:57
running linux mint 13 idle-python3.2 did work from the terminal before, now it does not. Calling idle-python3.2 from the terminal throws the error below BUT I can run Idle by double clicking on the script! Here's the very simple script that broke it all(?) http://pastebin.com/pP9An3UU FWIW, the script does not appear to work either (nothing happens when I run it). Here's the error in the terminal when trying to run Idle3.2 from the terminal: File "tkinter.py", line 2, in <module> tk = Tk() NameError: name 'Tk' is not defined here's the full error http://pastebin.com/J5AfAQyV I know tkinter is

How can I get rid of multiple nested for loops?

妖精的绣舞 提交于 2019-12-02 06:45:47
问题 I have a Python (3.2) script that searches for points with a property that I want. But it has this ugly part: for x in range(0,p): for y in range(0,p): for z in range(0,p): for s in range(0,p): for t in range(0,p): for w in range(0,p): for u in range(0,p): if isagoodpoint(x,y,z,s,t,w,u,p): print(x,y,z,s,t,w,u) else: pass Is there something I can do so that it looks a bit better? 回答1: You can use itertools to simplify your code: from itertools import product def print_good_points(p, dimensions

Python 3.2 logging with config file results in KeyError: 'formatters' on Raspbian

↘锁芯ラ 提交于 2019-12-02 04:36:57
I equipped my Python application with logging capability and it works flawlessly on my Windows system with Python 3.4. But when I deploy the application on my Raspberry Pi with Raspbian and Python 3.2, I receive the following error: Traceback (most recent call last): File "aurora/aurora_websocket.py", line 265, in <module> logging.config.fileConfig('logging.conf') File "/usr/lib/python3.2/logging/config.py", line 70, in fileConfig formatters = _create_formatters(cp) File "/usr/lib/python3.2/logging/config.py", line 106, in _create_formatters flist = cp["formatters"]["keys"] File "/usr/lib