python-3.4

python 3.4: random.choice on Enum

别等时光非礼了梦想. 提交于 2019-12-04 22:24:01
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 ? 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(Foo)) This works because Enum does support iteration . Demo: >>> from enum import Enum >>> import random >>>

Waiting for a task to complete after KeyboardInterrupt in asyncio

只愿长相守 提交于 2019-12-04 19:39:00
I'm trying to understand how does asyncio work. In my scenario client makes a tcp connection to the server, sends a login string, if authenticated - receives a stream of chars. Finally on KeyboardInterrupt sends logoff string to the server and hapilly disconnects. Currently I'm stuck on the final part as my logoff method/task is destroyed before it has a chance to complete. ^CTask was destroyed but it is pending! source_traceback: Object created at (most recent call last): File "tst.py", line 101, in <module> client.login() File "tst.py", line 29, in login logoff_tsk = self.loop.create_task

Writing a pandas dataframe to a word document table via pywin32

大城市里の小女人 提交于 2019-12-04 19:30:25
I am currently working on a script that needs to write to a .docx file for presentation purposes. I use pandas to handle all my data calculations in the script. I am looking to write a pandas dataframe into a table at a bookmark in a word.docx file using PyWIN32. The dataframe consists of floats. The psuedo code is something like this. frame = DataFrame(np.arange(28).reshape((4,7)), columns=['Text1',...'Text7']) With pywin32 imported... wordApp = win32.gencache.EnsureDispatch('Word.Application') wordApp.Visible = False doc = wordApp.Documents.Open(os.getcwd()+'\\template.docx') rng = doc

Python AES encryption without extra module

梦想与她 提交于 2019-12-04 17:58:02
问题 Is it possible to encrypt/decrypt data with AES without installing extra modules? I need to send/recieve data from C# , wich is encrypted with the System.Security.Cryptography reference. UPDATE I have tried to use PyAES, but that is too old. I updated some things to make that work, but it didn't. I've also can't install because it latest version is 3.3 while my version is 3.4 . 回答1: I'm using Cryptography library. Cryptography is an actively developed library that provides cryptographic

How do I escape forward slashes in python, so that open() sees my file as a filename to write, instead of a filepath to read?

风格不统一 提交于 2019-12-04 17:40:43
问题 Let me preface this by saying I'm not exactly sure what is happening with my code; I'm fairly new to programming. I've been working on creating an individual final project for my python CS class that checks my teacher's website on a daily basis and determines if he's changed any of the web pages on his website since the last time the program ran or not. The step I'm working on right now is as follows: def write_pages_files(): ''' Writes the various page files from the website's links '''

Call dll function works in IronPython, doesn't work in CPython3.4 gives “No method matches given arguments” error

烂漫一生 提交于 2019-12-04 16:34:06
For a project I need to include a DLL in Python. I'm using CPython3.4 and for including the dll I use pythonnet clr module (pythonnet-2.0.0.dev1-cp34-none-win_amd64.whl). In the dll I need a function that gives me a continuous update of a measurement. The dll is written in VB.net, the function that I need is shown below: Public Sub AdviseStart(ByVal item As Integer, ByVal a As Action(Of Object)) Implements IConversation.AdviseStart _parameterPoller.RegisterCallback(item, a) End Sub This is the code that I have written in python to call this function: import clr clr.AddReference('dll name')

What is the correct format to upgrade pip3 when the default pip is pip2?

我们两清 提交于 2019-12-04 16:05:46
问题 I develop for both Python 2 and 3. Thus, I have to use both pip2 and pip3. When using pip3 - I receive this upgrade request (last two lines): $ pip3 install arrow Requirement already satisfied (use --upgrade to upgrade): arrow in c:\program files (x86)\python3.5.1\lib\site-packages Requirement already satisfied (use --upgrade to upgrade): python-dateutil in c:\program files (x86)\python3.5.1\lib\site-packages (from arrow) Requirement already satisfied (use --upgrade to upgrade): six>=1.5 in c

Pyinstaller with scipy.signal ImportError: DLL load failed

久未见 提交于 2019-12-04 15:09:19
问题 Using PyInstaller 3.3, Python 3.4 I have compiled a program that needs to use the scipy.signal.periodogram() function, and it's causing the .exe to crash the instant it starts up. I've been searching all over stack overflow and other sites for a way to try to include the right DLLs, but to no avail. Please help me figure out if I need to include some specific DLLs in my .spec file or include some other hidden-imports! Here is my PyInstaller command: pyinstaller --noupx --hidden-import=tkinter

Getting values from functions that run as asyncio tasks

孤街浪徒 提交于 2019-12-04 14:59:11
问题 I was trying the following code: import asyncio @asyncio.coroutine def func_normal(): print("A") yield from asyncio.sleep(5) print("B") return 'saad' @asyncio.coroutine def func_infinite(): i = 0 while i<10: print("--"+str(i)) i = i+1 return('saad2') loop = asyncio.get_event_loop() tasks = [ asyncio.async(func_normal()), asyncio.async(func_infinite())] loop.run_until_complete(asyncio.wait(tasks)) loop.close() I can't figure out how to get values in variables from these functions. I can't do

Python 3.4 hex to Japanese Characters

£可爱£侵袭症+ 提交于 2019-12-04 14:53:54
I am currently writing a script to pull information off my site which contains Japanese characters. So far I have my script pulling out the data off the site. It has return as a string: "\xe5\xb9\xb4\xe3\x81\xab\xe4\xb8\x80\xe5\xba\xa6\xe3\x81\xae\xe6\x99\xb4\xe3\x82\x8c\xe5\xa7\xbf" Using an online hex to text tool, I am giving: 年に一度の晴れ姿 I know this phrase is correct, but my question is how do I convert it in python? When I run something like: name = "\xe5\xb9\xb4\xe3\x81\xab\xe4\xb8\x80\xe5\xba\xa6\xe3\x81\xae\xe6\x99\xb4\xe3\x82\x8c\xe5\xa7\xbf" print(name) I am giving this: å¹´ã«ä¸åº¦ã®æ