python-3.4

python 3.4: random.choice on Enum

耗尽温柔 提交于 2019-12-22 01:24:53
问题 I would like to use random.choice on an Enum. I tried : class Foo(Enum): a = 0 b = 1 c = 2 bar = random.choice(Foo) But this code is not working, how can I do that ? 回答1: An Enum is not a sequence , so you cannot pass it to random.choice() , which tries to pick an index between 0 and len(Foo) . Like a dictionary, index access to an Enum instead expects enumeration names to be passed in, so Foo[<integer>] fails here with a KeyError . You can cast it to a list first: bar = random.choice(list

How to emulate socket.socketpair on Windows

半世苍凉 提交于 2019-12-21 20:39:13
问题 The standard Python function socket.socketpair is unfortunately not available on Windows (as of Python 3.4.1), how can I write a replacement that works on both Unix and Windows? 回答1: Python 3.5 includes Windows support for socket.socketpair() . For Python 2.7+, you can use the backports.socketpair package (which I authored) on PyPi: import socket import backports.socketpair s1, s2 = socket.socketpair() 回答2: import socket import errno def create_sock_pair(port=0): """Create socket pair. If

How to use sys.path_hooks for customized loading of modules?

夙愿已清 提交于 2019-12-21 17:58:07
问题 I hope the following question is not too long. But otherwise I cannot explain by problem and what I want: Learned from How to use importlib to import modules from arbitrary sources? (my question of yesterday) I have written a specfic loader for a new file type (.xxx). (In fact the xxx is an encrypted version of a pyc to protect code from being stolen). I would like just to add an import hook for the new file type "xxx" without affecting the other types (.py, .pyc, .pyd) in any way. Now, the

VIM: Use python3 interpreter in python-mode

女生的网名这么多〃 提交于 2019-12-20 09:44:53
问题 I have recently switched to vim and configured it for Python-programming using this tutorial. Before, I have made sure that vim supports python3 (vim --version shows +python/dyn and +python3/dyn) using this article. But when executing a file from python-mode, still the python2.7 interpreter is chosen. How can I configure vim (or the python-mode) to run files on the python3 interpreter? My OS is Ubuntu 14.04 x64. Thanks in advance! 回答1: Try adding this to your .vimrc file let g:pymode_python =

Python3 project remove __pycache__ folders and .pyc files

不羁岁月 提交于 2019-12-20 08:18:12
问题 What is the BEST way to clear out all the __pycache__ folders and .pyc/.pyo files from a python3 project. I have seen multiple users suggest the pyclean script bundled with Debian, but this does not remove the folders. I want a simple way to clean up the project before pushing the files to my DVS. 回答1: You can do it manually with the next command: find . | grep -E "(__pycache__|\.pyc|\.pyo$)" | xargs rm -rf This will remove all *.pyc files and __pycache__ directories recursively in the

Python3 project remove __pycache__ folders and .pyc files

一笑奈何 提交于 2019-12-20 08:18:06
问题 What is the BEST way to clear out all the __pycache__ folders and .pyc/.pyo files from a python3 project. I have seen multiple users suggest the pyclean script bundled with Debian, but this does not remove the folders. I want a simple way to clean up the project before pushing the files to my DVS. 回答1: You can do it manually with the next command: find . | grep -E "(__pycache__|\.pyc|\.pyo$)" | xargs rm -rf This will remove all *.pyc files and __pycache__ directories recursively in the

Getting error: write() takes no keyword arguments

∥☆過路亽.° 提交于 2019-12-20 08:06:36
问题 gd = open("gamedata.py" , "rb+") gd.write(CharHealth = 100) gd.close I'm receiving the error message: write() takes no keyword arguments, and I cannot figure out why. My best interpretation is that the code is trying to interpret (CharHealth = 100) as a keyword argument instead of writing it to gamedata.py. I want to write (CharHealth = 100) (as a line of code) along with other code to gamedata.py 回答1: If you want to write text, then pass in a bytes object , not Python syntax: gd.write(b

My code works for a single list but not for a nested list. I need to improve it in means of functionality

允我心安 提交于 2019-12-20 07:24:01
问题 I have an assignment to do. I found out how to execute what is required in my own way but the solution is only partial. It doesn't work for a nested list. These are my codes. def calc_averages(): allprices =[ ['', '', '', '', 1.0, 2.0, 1.2, 1.3, 1.1, '', '', ''], ['', '', '', 1.2, 1.0, 2.0, 1.2, 1.3, 1.1, '', '', ''], ['', '', '', 1.2, '', 1.8, 1.3, 1.1, '', '', '', ''], ['', '', '', '', 1.0, 2.0, 1.2, 1.2, '', '', '', ''], ['', '', '', '', 1.0, 2.0, 1.1, 1.2, 1.4, 1.8, 1.9, 2.2] ] averages =

How do I use cx_Freeze on mac?

社会主义新天地 提交于 2019-12-20 03:29:11
问题 I used python 3.4 and cx_Freeze on my mac. I was trying to convert my python script into a stand alone application here is the code I got in my setup.py file: application_title = "Death Dodger 1.0" main_python_file = "DeathDodger-1.0.py" import sys from cx_Freeze import setup, Executable base = None if sys.platform == "win32": base = "Win32GUI" includes = ["atexit","re"] setup( name = application_title, version = "1.0", description = "Sample cx_Freeze script", options = {"build_exe" : {

How do I change the overall theme of a tkinter application?

非 Y 不嫁゛ 提交于 2019-12-20 02:12:24
问题 I want to change the theme of my tkinter application to clam. What is the code and where do I put it? I have tried: from tkinter import * from tkinter.ttk import * s=ttk.Style() s.theme_use('clam') 回答1: To change the theme, call .theme_use() with the theme's name as the argument. From https://infohost.nmt.edu/tcc/help/pubs/tkinter/web/ttk-theme-layer.html A number of operations related to themes require that you have available an instance of the ttk.Style() class (in the Python sense of class