python-3.5

How to specify multiple return types using type-hints

痞子三分冷 提交于 2019-11-27 00:43:12
I have a function in python that can either return a bool or a list . Is there a way to specify the return types using type hints. For example, Is this the correct way to do it? def foo(id) -> list or bool: ... Bhargav Rao From the documentation class typing.Union Union type; Union[X, Y] means either X or Y. Hence the proper way to represent more than one return data type is from typing import Union def foo(client_id: str) -> Union[list,bool] But do note that typing is not enforced. Python continues to remain a dynamically-typed language. The annotation syntax has been developed to help during

pip3 error - '_NamespacePath' object has no attribute 'sort'

我们两清 提交于 2019-11-27 00:34:18
问题 I tried to install a package through pip3, and I got this error. Every pip/pip3 command that I run gives me this error- alexg@hitbox:~$ pip3 -V Traceback (most recent call last): File "/usr/local/bin/pip3", line 7, in <module> from pip import main File "/home/alexg/.local/lib/python3.5/site-packages/pip/__init__.py", line 26, in <module> from pip.utils import get_installed_distributions, get_prog File "/home/alexg/.local/lib/python3.5/site-packages/pip/utils/__init__.py", line 27, in <module>

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

流过昼夜 提交于 2019-11-27 00:29:19
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. knbk 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 django.urls import reverse Note that Django 2.0 removes some features that previously were in django.core

Is it a good idea to dynamically create variables?

自古美人都是妖i 提交于 2019-11-26 21:58:28
问题 I recently found out how to dynamically create variables in python through this method: vars()['my_variable'] = 'Some Value' Thus creating the variable my_variable . My question is, is this a good idea? Or should I always declare the variables ahead of time? 回答1: I think it's preferable to use a dictionnary if it's possible: vars_dict = {} vars_dict["my_variable"] = 'Some Value' vars_dict["my_variable2"] = 'Some Value' I think it's more pythonic. 回答2: This is a bad idea, since it gets much

Registering a Python list property to QML in pyside2

青春壹個敷衍的年華 提交于 2019-11-26 21:45:23
问题 I'm trying to load a spreadsheet and pass a list of the worksheets back to my QML interface. But I'm unable to find a way to provide a list(and later a dictionary) back to the QML script. Here's my QML: FileDialog { id: openDialog title: "Open spreadsheet" nameFilters: [ "Excel files (*.xls *.xlsx)", "All files (*)" ] selectedNameFilter: "Excel files (*.xls *.xlsx)" onAccepted: { file.load(fileUrl) console.log(file.name) console.log(file.sheetnames) } onRejected: { console.log("Rejected") } }

numpy.savetxt resulting a formatting mismatch error in python 3.5

孤者浪人 提交于 2019-11-26 21:07:46
问题 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()

How can I specify the function type in my type hints?

柔情痞子 提交于 2019-11-26 20:28:14
I want to use type hints in my current Python 3.5 project. My function should receive a function as parameter. How can I specify the type function in my type hints? import typing def my_function(name:typing.AnyStr, func: typing.Function) -> None: # However, typing.Function does not exist. # How can I specify the type function for the parameter `func`? # do some processing pass I checked PEP 483 , but could not find a function type hint there. Jim Fasarakis Hilliard As @jonrsharpe noted in a comment, this can be done with typing.Callable : from typing import AnyStr, Callable def my_function

How can I periodically execute a function with asyncio?

烂漫一生 提交于 2019-11-26 19:59:54
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? A. Jesse Jiryu Davis For Python versions below 3.5: import asyncio @asyncio.coroutine def periodic(): while True: print('periodic') yield from asyncio.sleep(1) def stop(): task.cancel() loop = asyncio.get_event

How to disable password request for a Jupyter notebook session?

依然范特西╮ 提交于 2019-11-26 19:51:21
问题 I have been launching Jupyter Notebook for years using the following command: jupyter-notebook --port=7000 --no-browser --no-mathjax When I try to open the jupyter on the browser it ask me for a password, even though I have never set any before. It is important to note that If I do set the port to a value different than 7000 (eg., the default 8888) the interface will open with no problem I am running jupyter locally, and on the following setup: Python 3.5.2 With the following modules

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

二次信任 提交于 2019-11-26 19:37:52
问题 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