How can I obtain the image size using a standard Python class (without using an external library)?

Here’s a Python 3 script that returns a tuple containing an image height and width for .png, .gif and .jpeg without using any external libraries (i.e., what Kurt McKee referenced). It should be relatively easy to transfer it to Python 2. import struct import imghdr def get_image_size(fname): ”’Determine the image type of fhandle and return its … Read more

What does from __future__ import absolute_import actually do?

The changelog is sloppily worded. from __future__ import absolute_import does not care about whether something is part of the standard library, and import string will not always give you the standard-library module with absolute imports on. from __future__ import absolute_import means that if you import string, Python will always look for a top-level string module, … Read more

Multiple (asynchronous) connections with urllib2 or other http library?

So, it’s 2016 😉 and we have Python 3.4+ with built-in asyncio module for asynchronous I/O. We can use aiohttp as HTTP client to download multiple URLs in parallel. import asyncio from aiohttp import ClientSession async def fetch(url): async with ClientSession() as session: async with session.get(url) as response: return await response.read() async def run(loop, r): … Read more

How to obtain image size using standard Python class (without using external library)?

Here’s a python 3 script that returns a tuple containing an image height and width for .png, .gif and .jpeg without using any external libraries (ie what Kurt McKee referenced above). Should be relatively easy to transfer it to Python 2. import struct import imghdr def get_image_size(fname): ”’Determine the image type of fhandle and return … Read more

Sort nested dictionary by value, and remainder by another value, in Python

Use the key argument for sorted(). It lets you specify a function that, given the actual item being sorted, returns a value that should be sorted by. If this value is a tuple, then it sorts like tuples sort – by the first value, and then by the second value. sorted(your_list, key=lambda x: (your_dict[x][‘downloads’], your_dict[x][‘date’]))

How does Python’s “super” do the right thing?

Change your code to this and I think it’ll explain things (presumably super is looking at where, say, B is in the __mro__?): class A(object): def __init__(self): print “A init” print self.__class__.__mro__ class B(A): def __init__(self): print “B init” print self.__class__.__mro__ super(B, self).__init__() class C(A): def __init__(self): print “C init” print self.__class__.__mro__ super(C, self).__init__() class … Read more