Re: python for everyday tasks
On Saturday 23 November 2013 02:01:26 Steven D'Aprano wrote: > * Python is not Java, and Java is not Python either: > > http://dirtsimple.org/2004/12/python-is-not-java.html > http://dirtsimple.org/2004/12/java-is-not-python-either.html Thanks for all those references. There's this statement in the first article: "Got a switch statement? The Python translation is a hash table, not a bunch of if-then statments. Got a bunch of if-then's that wouldn't be a switch statement in Java because strings are involved? It's still a hash table. " I can't figure out how would you translate a switch statement into hash table in general case. -- https://mail.python.org/mailman/listinfo/python-list
Caching function results
Suppose, I have some resource-intensive tasks implemented as functions in Python. Those are called repeatedly in my program. It's guranteed that a call with the same arguments always produces the same return value. I want to cache the arguments and return values and in case of repititive call immediately return the result without doing expensive calculations. I intend to do it this way: # module 'cache.py' class Cache: def save_result(self, handle, args): """Save calculated result to cache.""" <...> def load_result(self, handle, args, result): """Load previously calculated result from cache. Return None is it's unavailable.""" <...> def save_to_file(self, filename): """Save all cached data to a file.""" def __init__(self, filename=None): # Optionally loads previously cached data from a file def clear(self): <...> # module 'calculations.py' import cache _cache = cache.Cache() def big_task(arg1, arg2, arg3=None): cached_value = _cache.load_result('big_task', (arg1, arg2, arg3)) if cached_value is not None: return cached_value result = <...> _cache.save_result('big_task', (arg1, arg2, arg3), result) return result The arguments and return values are almost always: * ints * floats * tuple or lists of ints or floats I think Cache object will store data in a dictionary. I'll convert lists to tuples before storing them. I'd also like to limit the size of the cache (in MB) and get rid of old cached data. Don't know how yet. Do you like this design or maybe there's a better way with Python's included batteries? -- https://mail.python.org/mailman/listinfo/python-list
Using map()
I checked my modules with pylint and saw the following warning: W: 25,29: Used builtin function 'map' (bad-builtin) Why is the use of map() discouraged? It' such a useful thing. -- https://mail.python.org/mailman/listinfo/python-list
object().__dict__
There are some basics about Python objects I don't understand. Consider this snippet: class X: pass ... x = X() dir(x) ['__class__', '__delattr__', '__dict__', '__dir__', '__doc__', '__eq__', '__format__', '__ge__', '__getattribute__', '__gt__', '__hash__', '__init__', '__le__', '__lt__', '__module__', '__ne__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__setattr__', '__sizeof__', '__str__', '__subclasshook__', '__weakref__'] x.foo = 11 And now I want to make a simple object in a shorter way, without declaring X class: y = object() dir(y) ['__class__', '__delattr__', '__dir__', '__doc__', '__eq__', '__format__', '__ge__', '__getattribute__', '__gt__', '__hash__', '__init__', '__le__', '__lt__', '__ne__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__setattr__', '__sizeof__', '__str__', '__subclasshook__'] y.foo = 12 Traceback (most recent call last): File "", line 1, in AttributeError: 'object' object has no attribute 'foo' The attribute list is different now and there's no __dict__ and the object does not accept new attributes. Please explain what's going on. -- https://mail.python.org/mailman/listinfo/python-list
Re: anyone tell me why my program will not run?
On суббота, 21 ноября 2015 г. 6:30:02 MSK, Dylan Riley wrote: i am learning python and was tasked with making a program that flips a coin 100 times and then tells you the number of heads and tails. First, you have a syntax error: if result = heads: should be: if result == heads: Second, there's incorrent indentation, your while loop is almost empty (not syntax error though). -- https://mail.python.org/mailman/listinfo/python-list
Re: anyone tell me why my program will not run?
On суббота, 21 ноября 2015 г. 6:30:02 MSK, Dylan Riley wrote: Also some more notes: heads = int("1") tails = int("2") Why use this strange initialization? The usual way: heads = 1 tails = 2 gives the same result. while flips != 0: flips -= 1 There's no need to use while and flips variable: for _ in range(100): if random.randint(heads, tails) == heads: headscount += 1 else: tailscount += 1 Also, it's good to put import at the beginning. -- https://mail.python.org/mailman/listinfo/python-list
urllib.request with proxy and HTTPS
Hello, I'm trying to make an HTTPS request with urllib. OS: Gentoo Python: 3.6.1 openssl: 1.0.2l This is my test code: = CODE BLOCK BEGIN = import ssl import urllib.request from lxml import etree PROXY = 'proxy.vpn.local:' URL = "https://google.com"; proxy = urllib.request.ProxyHandler({'http': PROXY}) #context = ssl.SSLContext(ssl.PROTOCOL_TLSv1_1) context = ssl.SSLContext() context.verify_mode = ssl.CERT_REQUIRED context.check_hostname = True secure_handler = urllib.request.HTTPSHandler(context = context) opener = urllib.request.build_opener(proxy, secure_handler) opener.addheaders = [('User-Agent', 'Mozilla/5.0 (Windows NT 6.1; Win64; x64; rv:54.0) Gecko/20100101 Firefox/54.0')] response = opener.open(URL) tree = etree.parse(response, parser=etree.HTMLParser()) print(tree.docinfo.doctype) = CODE BLOCK END = My first problem is that CERTIFICATE_VERIFY_FAILED error happens. I've found that something similar happens in macOS since Python installs its own set of trusted CA. But this isn't macOS and I can fetch HTTPS normally with curl and other tools. = TRACE BLOCK BEGIN = Traceback (most recent call last): File "/usr/lib64/python3.6/urllib/request.py", line 1318, in do_open encode_chunked=req.has_header('Transfer-encoding')) File "/usr/lib64/python3.6/http/client.py", line 1239, in request self._send_request(method, url, body, headers, encode_chunked) File "/usr/lib64/python3.6/http/client.py", line 1285, in _send_request self.endheaders(body, encode_chunked=encode_chunked) File "/usr/lib64/python3.6/http/client.py", line 1234, in endheaders self._send_output(message_body, encode_chunked=encode_chunked) File "/usr/lib64/python3.6/http/client.py", line 1026, in _send_output self.send(msg) File "/usr/lib64/python3.6/http/client.py", line 964, in send self.connect() File "/usr/lib64/python3.6/http/client.py", line 1400, in connect server_hostname=server_hostname) File "/usr/lib64/python3.6/ssl.py", line 401, in wrap_socket _context=self, _session=session) File "/usr/lib64/python3.6/ssl.py", line 808, in __init__ self.do_handshake() File "/usr/lib64/python3.6/ssl.py", line 1061, in do_handshake self._sslobj.do_handshake() File "/usr/lib64/python3.6/ssl.py", line 683, in do_handshake self._sslobj.do_handshake() ssl.SSLError: [SSL: CERTIFICATE_VERIFY_FAILED] certificate verify failed (_ssl.c:749) During handling of the above exception, another exception occurred: Traceback (most recent call last): File "./https_test.py", line 21, in response = opener.open(URL) File "/usr/lib64/python3.6/urllib/request.py", line 526, in open response = self._open(req, data) File "/usr/lib64/python3.6/urllib/request.py", line 544, in _open '_open', req) File "/usr/lib64/python3.6/urllib/request.py", line 504, in _call_chain result = func(*args) File "/usr/lib64/python3.6/urllib/request.py", line 1361, in https_open context=self._context, check_hostname=self._check_hostname) File "/usr/lib64/python3.6/urllib/request.py", line 1320, in do_open raise URLError(err) urllib.error.URLError: certificate verify failed (_ssl.c:749)> = TRACE BLOCK END = Second problem is that for HTTP requests proxy is used, but for HTTPS it makes a direct connection (verified with tcpdump). I've read at docs.python.org that previous versions of Python couldn't handle HTTPS with proxy but that shortcoming seems to have gone now. Please help :) -- https://mail.python.org/mailman/listinfo/python-list