python-3.5

numpy.savetxt resulting a formatting mismatch error in python 3.5

…衆ロ難τιáo~ 提交于 2019-11-27 22:44:30
I'm trying to save a numpy matrix (Nx3, float64) into a txt file using numpy.savetxt: np.savetxt(f, mat, fmt='%.5f', delimiter=' ') This line worked in python 2.7, but in python 3.5, I'm getting the following error: TypeError: Mismatch between array dtype ('float64') and format specifier ('%.5f %.5f %.5f') When I'm stepping into the savetxt code, the print the error (traceback.format_exc()) in the catch block (numpy.lib.npyio, line 1158), the error is completely different: TypeError: write() argument must be str, not bytes The line of code resulting the exception is the following: fh.write

How to use asyncio with existing blocking library?

最后都变了- 提交于 2019-11-27 20:09:25
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)? Lets assume the functions are re-entrant. i.e it is fine to call foo again when already a previous foo

What is the difference between @types.coroutine and @asyncio.coroutine decorators?

寵の児 提交于 2019-11-27 18:50:41
Documentations say: @asyncio.coroutine Decorator to mark generator-based coroutines. This enables the generator use yield from to call async def coroutines, and also enables the generator to be called by async def coroutines, for instance using an await expression. _ @types.coroutine(gen_func) This function transforms a generator function into a coroutine function which returns a generator-based coroutine. The generator-based coroutine is still a generator iterator, but is also considered to be a coroutine object and is awaitable. However, it may not necessarily implement the __await__()

@asyncio.coroutine vs async def

爱⌒轻易说出口 提交于 2019-11-27 18:20:49
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? Zero Piraeus 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 be iterated over or passed to iter() , list() , tuple() and other built-ins. They also cannot

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

南楼画角 提交于 2019-11-27 18:07:43
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. 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 your machine (2.x, 3.x) you can specify what version of python to launch by py -2 or py -3 HaTiMSuM Python3

Test if function or method is normal or asynchronous

为君一笑 提交于 2019-11-27 17:09:28
问题 How can I find out if a function or method is a normal function or an async function? I would like my code to automatically support normal or async callbacks and need a way to test what type of function is passed. async def exampleAsyncCb(): pass def exampleNomralCb(): pass def isAsync(someFunc): #do cool dynamic python stuff on the function return True/False async def callCallback(cb, arg): if isAsync(cb): await cb(arg) else: cb(arg) And depending on what type of function gets passed it

How can I implement asyncio websockets in a class?

你。 提交于 2019-11-27 13:43:55
问题 I would like to connect to a websocket via asyncio and websockets , with a format as shown below. How would I be able to accomplish this? from websockets import connect class EchoWebsocket: def __init__(self): self.websocket = self._connect() def _connect(self): return connect("wss://echo.websocket.org") def send(self, message): self.websocket.send(message) def receive(self): return self.websocket.recv() echo = EchoWebsocket() echo.send("Hello!") print(echo.receive()) # "Hello!" 回答1: How to

How to set Python3.5.2 as default Python version on CentOS?

こ雲淡風輕ζ 提交于 2019-11-27 12:48:54
Is there a way to set the Python 3.5.2 as the default Python version on CentOS 7? currently, I have Python 2.7 installed as default and Python 3.5.2 installed separately. I used the following commands mv /usr/bin/python /usr/bin/python-old sudo ln -fs /usr/bin/python3 /usr/bin/python but after that yum gives the error. -bash: /usr/bin/yum: /usr/bin/python: bad interpreter: No such file or directory is there something I'm missing here? NOTE: its the similar but opposite question of Linux CentOS 7, how to set Python2.7 as default Python version? If this sudo ln -fs /usr/bin/python3 /usr/bin

How to use 'yield' inside async function?

六月ゝ 毕业季﹏ 提交于 2019-11-27 12:02:35
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 async function? Mikhail Gerasimov Upd: Starting with Python 3.6 we have asynchronous generators and

Why is str.translate much faster in Python 3.5 compared to Python 3.4?

久未见 提交于 2019-11-27 11:24:51
问题 I was trying to remove unwanted characters from a given string using text.translate() in Python 3.4. The minimal code is: import sys s = 'abcde12345@#@$#%$' mapper = dict.fromkeys(i for i in range(sys.maxunicode) if chr(i) in '@#$') print(s.translate(mapper)) It works as expected. However the same program when executed in Python 3.4 and Python 3.5 gives a large difference. The code to calculate timings is python3 -m timeit -s "import sys;s = 'abcde12345@#@$#%$'*1000 ; mapper = dict.fromkeys(i