python-3.5

Generator-based coroutine versus native coroutine

 ̄綄美尐妖づ 提交于 2019-11-30 04:13:50
问题 I just read PEP0492 talking about the new approach on coroutines but the PEP failed to make me understand the difference between generator-based coroutines and native ones. Can someone tell me the difference (maybe with examples)? For what I understood they uses different words (yield/yield from and await/async/yield). I understand that at the end of a native coroutine a yield is expected, but this also stands true for generator-based ones. 回答1: There is no functional difference. "Native

Python Pandas: if the data is NaN, then change to be 0, else change to be 1 in data frame

荒凉一梦 提交于 2019-11-30 04:11:51
问题 I have a DataFrame:df as following: row id name age url 1 e1 tom NaN http1 2 e2 john 25 NaN 3 e3 lucy NaN http3 4 e4 tick 29 NaN I want to change the NaN to be 0, else to be 1 in the columns: age, url. My code is following, but it is wrong. import Pandas as pd df[['age', 'url']].applymap(lambda x: 0 if x=='NaN' else x) I want to get the following result: row id name age url 1 e1 tom 0 1 2 e2 john 1 0 3 e3 lucy 0 1 4 e4 tick 1 0 Thanks for your help! 回答1: You can use where with fillna and

asyncio aiohttp progress bar with tqdm

不羁的心 提交于 2019-11-30 03:42:56
I'm attempting to integrate a tqdm progress bar to monitor POST requests generated with aiohttp in Python 3.5. I have a working progress bar but can't seem to gather results using as_completed() . Pointers gratefully received. Examples I've found suggest using the following pattern, which is incompatible with Python 3.5 async def definitions: for f in tqdm.tqdm(asyncio.as_completed(tasks), total=len(coros)): yield from f Working (albeit redacted) async code without the progress bar: def async_classify(records): async def fetch(session, name, sequence): url = 'https://app.example.com/api/v0

Which standard library modules are required to run the Python 3.5 interpreter?

こ雲淡風輕ζ 提交于 2019-11-30 03:28:09
问题 Here's a CPython program that tries to initialize the interpreter with an empty sys.path : #include <Python.h> int main(int argc, char** argv) { wchar_t* program = NULL; wchar_t* sys_path = NULL; Py_NoSiteFlag = 1; program = Py_DecodeLocale(argv[0], NULL); Py_SetProgramName(program); sys_path = Py_DecodeLocale("", NULL); Py_SetPath(sys_path); Py_Initialize(); PyMem_RawFree(program); PyMem_RawFree(sys_path); Py_Finalize(); } Executing the program above raises the following error: Fatal Python

How to use async/await in Python 3.5?

这一生的挚爱 提交于 2019-11-29 22:48:29
#!/usr/bin/env python3 # -*- coding: utf-8 -*- import time async def foo(): await time.sleep(1) foo() I couldn't make this dead simple example to run: RuntimeWarning: coroutine 'foo' was never awaited foo() Running coroutines requires an event loop . Use the asyncio() library to create one: import asyncio # Python 3.7+ asyncio.run(foo()) or # Python 3.6 and older loop = asyncio.get_event_loop() loop.run_until_complete(foo()) Also see the Tasks and Coroutines chapter of the asyncio documentation . If you already have a loop running, you'd want to run additional coroutines concurrently by

Why can't I use a starred expression?

风流意气都作罢 提交于 2019-11-29 22:20:31
My code $ python Python 3.5.2 |Continuum Analytics, Inc.| (default, Jul 2 2016, 17:53:06) [GCC 4.4.7 20120313 (Red Hat 4.4.7-1)] on linux Type "help", "copyright", "credits" or "license" for more information. >>> a = (1, 2) >>> '%d %d %d' % (0, *a) '0 1 2' >>> '%d %d %d' % (*a, 3) '1 2 3' >>> '%d %d' % (*a) File "<stdin>", line 1 SyntaxError: can't use starred expression here >>> My question, why? In a more serious tone: I'd like an answer, or a reference, that details all the ins and outs of using a starred expression, as it happens that I am sometimes surprised from its behaviours...

Using abc.ABCMeta in a way it is compatible both with Python 2.7 and Python 3.5

五迷三道 提交于 2019-11-29 20:45:53
I'd like to create a class which has abc.ABCMeta as a metaclass and is compatible both with Python 2.7 and Python 3.5. Until now, I only succeeded doing this either on 2.7 or on 3.5 - but never on both versions simultaneously. Could someone give me a hand? Python 2.7: import abc class SomeAbstractClass(object): __metaclass__ = abc.ABCMeta @abc.abstractmethod def do_something(self): pass Python 3.5: import abc class SomeAbstractClass(metaclass=abc.ABCMeta): @abc.abstractmethod def do_something(self): pass Testing If we run the following test using the suitable version of the Python interpreter

No module named 'matplotlib.pyplot'; 'matplotlib' is not a package

不羁的心 提交于 2019-11-29 18:13:47
Have found a similar issue , however haven't found proper solution. Here's a code: import matplotlib.pyplot as plt plt.plot([1,2,3],[4,2,5]) plt.show() Run, got the message: ImportError: No module named 'matplotlib.pyplot'; 'matplotlib' is not a package I run Linux Mint 18 with preinstalled python-2.7 and python-3.5 (I use python3), before that I was installing modules with a simple sudo apt-get install method and that worked great. Before running this the code above, I've installed matplotlib in a usual way sudo apt-get install python-matplotlib . As it haven't worked out, started to look for

How to update chrome driver for windows 10

孤街浪徒 提交于 2019-11-29 16:26:42
Hello I am looking to update my chrome driver to the latest version but ant find any information on updating the driver just info on installing it. What do I need to do to update the driver to the latest version? chromedriver is a single self-contained executable file. Just replace your existing version with a newer one. download the latest version of chromedriver_win32.zip from https://sites.google.com/a/chromium.org/chromedriver/downloads unzip the file to extract chromedriver.exe replace your existing file with this new executable. Recently I found that chromedriver has almost one-to-one

PermissionError While Installing TensorFlow Using Virtualenv

半腔热情 提交于 2019-11-29 16:20:33
I was installing tensorflow using virtualenv. The following commands worked fine. $ virtualenv ~/.tensorflow/bin/activate $ pip install --upgrade tensorflow But if I try: $ virtualenv ~/.tensorflow/bin/activate $ pip3 install tensorflow I got PermissionError: I tried the last command with sudo. $ sudo pip3 install tensorflow Then it seems like I can import tensorflow outside virtualenv. (Is it correct?) How can I install tensorflow for python 3 only inside virtualenv? By the way, I am using pip 9.0.1 for both python 2.7.12 and 3.5.2. The version of virtualenv is 15.0.1. If your virtual