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

As @jonrsharpe noted in a comment, this can be done with typing.Callable: from typing import Callable def my_function(func: Callable): Note: Callable on its own is equivalent to Callable[…, Any]. Such a Callable takes any number and type of arguments (…) and returns a value of any type (Any). If this is too unconstrained, one may … Read more

“Unicode Error “unicodeescape” codec can’t decode bytes… Cannot open text files in Python 3 [duplicate]

The problem is with the string “C:\Users\Eric\Desktop\beeline.txt” Here, \U in “C:\Users… starts an eight-character Unicode escape, such as \U00014321. In your code, the escape is followed by the character ‘s’, which is invalid. You either need to duplicate all backslashes: “C:\\Users\\Eric\\Desktop\\beeline.txt” Or prefix the string with r (to produce a raw string): r”C:\Users\Eric\Desktop\beeline.txt”

Download file from web in Python 3

If you want to obtain the contents of a web page into a variable, just read the response of urllib.request.urlopen: import urllib.request … url=”http://example.com/” response = urllib.request.urlopen(url) data = response.read() # a `bytes` object text = data.decode(‘utf-8’) # a `str`; this step can’t be used if data is binary The easiest way to download and … Read more

What does “SyntaxError: Missing parentheses in call to ‘print'” mean in Python?

This error message means that you are attempting to use Python 3 to follow an example or run a program that uses the Python 2 print statement: print “Hello, World!” The statement above does not work in Python 3. In Python 3 you need to add parentheses around the value to be printed: print(“Hello, World!”) … Read more

How to install python3 version of package via pip on Ubuntu?

Ubuntu 12.10+ and Fedora 13+ have a package called python3-pip which will install pip-3.2 (or pip-3.3, pip-3.4 or pip3 for newer versions) without needing this jumping through hoops. I came across this and fixed this without needing the likes of wget or virtualenvs (assuming Ubuntu 12.04): Install package python3-setuptools: run sudo aptitude install python3-setuptools, this … Read more