Today I will tell you about the massive success that is whypy3.com. With hundreds of users a day (on the best day when it reached page two of Hacker News and hundreds actually being 103), it has been a tremendous success in the lucrative Python code snippet market. By presenting small snippets of code displaying cool features of Python 3, I was able to single–handedly convert millions (1e-6 millions to be exact) of Python 2 users to true Python 3 believers.
It all started when I saw a tweet about a cool Python 3 feature I haven’t seen before. This amazing feature automatically resolves any exception in your code by suppressing it. Who needs pesky exceptions anyway? Alternatively, you can use it to cleanly ignore expected exceptions instead of the usual
from contextlib import suppress
There are obviously way better and bigger reasons to finally make that move to Python 3. But what if you can be lured in by some cool cheap tricks? And that’s exactly why I created whypy3.com. It’s a tool that us Python 3 lovers can use to try and slowly wear down on an insistent boss or colleague. It’s also a fun way for me to share all my favorite Python 3 features so I don’t forget them.
I was initially going to to do the usual static S3 website with CloudFront/CloudFlare. But I also wanted it to be easy for other people to contribute snippets. The obvious choice was GitHub, and since I’m already using GitHub, why not give GitHub Pages a try? Getting it up and running was a breeze. To make it easier to contribute without editing HTML, I decided to use the full blown Jekyll setup. I had to fight a little bit with Jekyll to get highlighting working, but overall it took no time to get a solid looking site up and running.
After posting to Hacker News, I even got a few pull requests for more snippets. To this day, I still get some Twitter interactions here and there. I don’t expect this to become a huge project with actual millions of users, but at the end of the day this was pretty fun, I learned some new technologies, and I probably convinced someone to at least start thinking about moving to Python 3.
Do you use Python 3? Please share your favorite feature!
Django 1.10 added a new style of middleware with a different interface and a new setting called
MIDDLWARE instead of
MIDDLEWARE_CLASSES. Creating a class that supports both is easy enough with MiddlewareMixin, but that only works with Django 1.10 and above. What if you want to create middleware that can work with all versions of Django so it can be easily shared?
Writing a compatible middleware is not too hard. The trick is having a fallback for when the import fails on any earlier versions of Django. I couldn’t find a full example anywhere and it took me a few attempts to get it just right, so I thought I’d share my results to save you some time.
from django.core.exceptions import MiddlewareNotUsed
from django.shortcuts import redirect
from django.utils.deprecation import MiddlewareMixin
MiddlewareMixin = object
def __init__(self, *args, **kwargs):
raise MiddlewareNotUsed('DISABLE_MIDDLEWARE is set')
super(CompatibleMiddleware, self).__init__(*args, **kwargs)
def process_request(self, request):
if request.path == '/':
def process_response(self, request, response):
CompatibleMiddleware can now be used in both
MIDDLEWARE_CLASSES. It should also work with any version of Django so it’s easier to share.
I have seen a few “staying safe online” guides lately. I wrote one of my own a while back after some of my friends were threatened online and got worried. This guide should be a good starting point for most common casual internet users. It’s important to remember that no matter what you do if it’s online, it can be hacked.
- Never reuse passwords
- Some websites are easier to hack than others
- Hackers will try the same password on other websites
- Use 1Password for easier management
- Don’t use simple passwords
- Hackers guess passwords all the time
- There are easy automatic tools that enumerate all password options
- Don’t use your name, birthday, SSN, or any public information in passwords
- Keep your computer & phone up-to-date
- Old software has known and easily exploitable vulnerabilities
- Never click links in emails
- Clicking the wrong link can give control of your accounts to hackers
- Manually browse to the website even if the email looks legit
- Always logout on public computers
- Preferably never login on public computers in the first place
- Data can be linger even after logging out
- Some public computers record your passwords
- If it was put online, it will stay online
- Any private information shared can help hacking
- Your name and birth year can be enough to guess your SSN
- Click the little lock icon on top and follow instructions
- Set everything to private
- Hide your birth year
- Click the little triangle on the top right and choose Settings
- Enable login alerts to be notified of hacks
- Enable login approvals
- Enable trusted contacts in case your account is hacked
Securing Google Account
- Enable 2 factor authentication
- Check Google+ privacy