python-3.5

Difference between numpy dot() and Python 3.5+ matrix multiplication @

。_饼干妹妹 提交于 2019-11-26 19:31:55
I recently moved to Python 3.5 and noticed the new matrix multiplication operator (@) sometimes behaves differently from the numpy dot operator. In example, for 3d arrays: import numpy as np a = np.random.rand(8,13,13) b = np.random.rand(8,13,13) c = a @ b # Python 3.5+ d = np.dot(a, b) The @ operator returns an array of shape: c.shape (8, 13, 13) while the np.dot() function returns: d.shape (8, 13, 8, 13) How can I reproduce the same result with numpy dot? Are there any other significant differences? The @ operator calls the array's __matmul__ method, not dot . This method is also present in

@asyncio.coroutine vs async def

断了今生、忘了曾经 提交于 2019-11-26 19:18:20
问题 With the asyncio library I've seen, @asyncio.coroutine def function(): ... and async def function(): ... used interchangeably. Is there any functional difference between the two? 回答1: Yes, there are functional differences between native coroutines using async def syntax and generator-based coroutines using the asyncio.coroutine decorator. According to PEP 492, which introduces the async def syntax: Native coroutine objects do not implement __iter__ and __next__ methods. Therefore, they cannot

'python3' is not recognized as an internal or external command, operable program or batch file

半世苍凉 提交于 2019-11-26 19:10:52
问题 I am using python 3.5.2 version in windows 7 and tried using python3 app.py. I am getting this error message 'python3' is not recognized as an internal or external command, operable program or batch file. any specific cause about why python3 command is not working? I also verified that the PATH is added to environment variables. 回答1: There is no python3.exe file, that is why it fails. Try: py instead. py is just a launcher for python.exe. If you have more than one python versions installed on

How to use asyncio with existing blocking library?

北城余情 提交于 2019-11-26 18:17:05
问题 I have few blocking functions foo , bar and I can't change those (Some internal library I don't control. Talks to one or more network services). How do I use it as async?. E.g. I wan't to do the following. results = [] for inp in inps: val = foo(inp) result = bar(val) results.append(result) This will be inefficient as I can call foo for the second input while I am waiting for the first and same for bar . How do I wrap them such that they are usable with asyncio (i.e new async , await syntax)?

Self-reference or forward-reference of type annotations in Python [duplicate]

血红的双手。 提交于 2019-11-26 17:50:33
问题 This question already has an answer here: How do I specify that the return type of a method is the same as the class itself? 4 answers I'm trying to figure out how self-reference of types work with python3's type annotations - the docs don't specify anything regarding this. As an example: from typing import TypeVar, Optional, Generic T = TypeVar('T') class Node(Generic[T]): left = None right = None value = None def __init__( self, value: Optional[T], left: Optional[Node[T]]=None, right:

How to use 'yield' inside async function?

随声附和 提交于 2019-11-26 15:52:59
问题 I want to use generator yield and async functions. I read this topic, and wrote next code: import asyncio async def createGenerator(): mylist = range(3) for i in mylist: await asyncio.sleep(1) yield i*i async def start(): mygenerator = await createGenerator() for i in mygenerator: print(i) loop = asyncio.get_event_loop() try: loop.run_until_complete(start()) except KeyboardInterrupt: loop.stop() pass But i got the error: SyntaxError: 'yield' inside async function How to use yield generator in

What is the '@=' symbol for in Python?

独自空忆成欢 提交于 2019-11-26 13:01:44
I know @ is for decorators, but what is @= for in Python? Is it just reservation for some future idea? This is just one of my many questions while reading tokenizer.py . rightfold From the documentation : The @ (at) operator is intended to be used for matrix multiplication. No builtin Python types implement this operator. The @ operator was introduced in Python 3.5. @= is matrix multiplication followed by assignment, as you would expect. They map to __matmul__ , __rmatmul__ or __imatmul__ similar to how + and += map to __add__ , __radd__ or __iadd__ . The operator and the rationale behind it

ImportError: No module named 'django.core.urlresolvers'

前提是你 提交于 2019-11-26 09:21:44
问题 Hi I am working on Django project where I need to create a form for inputs. I tried to import reverse from django.core.urlresolvers . I got an error: line 2, in from django.core.urlresolvers import reverse ImportError: No module named \'django.core.urlresolvers\' I am using Python 3.5.2, Django 2.0 and MySQL. 回答1: Django 2.0 removes the django.core.urlresolvers module, which was moved to django.urls in version 1.10. You should change any import to use django.urls instead, like this: from

How can I periodically execute a function with asyncio?

删除回忆录丶 提交于 2019-11-26 07:27:27
问题 I\'m migrating from tornado to asyncio , and I can\'t find the asyncio equivalent of tornado \'s PeriodicCallback . (A PeriodicCallback takes two arguments: the function to run and the number of milliseconds between calls.) Is there such an equivalent in asyncio ? If not, what would be the cleanest way to implement this without running the risk of getting a RecursionError after a while? 回答1: For Python versions below 3.5: import asyncio @asyncio.coroutine def periodic(): while True: print(

Pandas: How can I use the apply() function for a single column?

China☆狼群 提交于 2019-11-26 04:07:33
问题 I have a pandas data frame with two columns. I need to change the values of the first column without affecting the second one and get back the whole data frame with just first column values changed. How can I do that using apply in pandas? 回答1: Given a sample dataframe df as: a,b 1,2 2,3 3,4 4,5 what you want is: df['a'] = df['a'].apply(lambda x: x + 1) that returns: a b 0 2 2 1 3 3 2 4 4 3 5 5 回答2: For a single column better to use map() , like this: df = pd.DataFrame([{'a': 15, 'b': 15, 'c'