Share the Knowledge
RSS icon Home icon
  • How to delete Python .pyc and .pyo files on Ubuntu 14.04

    Posted on September 2nd, 2014 webmaster No comments         

    I just realized today that Ubuntu has a command called pyclean already installed by default that will recursively delete all .pyc and .pyo files.

    For example, to recursively delete .pyc and .pyo files from the current working directory, you can do:

    pyclean .


    If you’re not on Ubuntu, you can run this command instead:

    find . -name “*.pyc” -delete

  • How to make images responsive by default in Bootstrap 3

    Posted on July 23rd, 2014 webmaster No comments         

    I’m currently working on a simple blog app in Django and noticed that my images are not getting resized by default when using Bootstrap 3.

    To change this behavior, simply change the img tag in bootstrap.css to this:

    img {
      display: inline-block;
      height: auto;
      max-width: 100%;
      border: 0;


  • A Python function for flattening irregular list of lists

    Posted on July 20th, 2014 webmaster No comments         

    Here’s a nice short function for flattening irregular lists in Python that I found on Stackoverflow a few weeks back.  It returns a generator so should work fine for very big lists.

    def flatten_list(object_list):
        Flattens a list of objects.
        for element in object_list:
            if isinstance(element, collections.Iterable) and not \
                    isinstance(element, basestring):
                for sublist in flatten_list(element):
                    yield sublist
                yield element

    I tested it with lists like the following and it did the job:

    • [None]
    • [ ]
    • [[], []]
    • ['a', 'b', 'c']
    • [['a'], ['b', 'c']]
    • [['a', 'b'], ['c'], 'd']
    • ['a']
    • [['a']]
  • Converting YouTube videos to MP3

    Posted on July 4th, 2014 webmaster No comments         

    I was looking for a new Django project to start a few weeks back and I wanted it to be something I could use myself and involves running tasks in the background as I wanted to play around with Celery.  I usually use YouTube nowadays to listen to music while I’m working so I figured it might be a good idea to write a web app where a user can enter the video’s URL and it will extract the audio and convert it to MP3 format.  I thought this was a perfect project to do next, as the conversion process is perfect to run in the background and I could actually use this app once finished.  I don’t have a data plan so it would be really nice to download some stuff from YouTube that I could listen to on my commute to work or when taking my daily walks.

    Luckily, someone already built the harder part of downloading the videos and doing the conversion.  There’s this open source project called youtube-dl that’s actually written in Python, so even better.  All I have to do now is create the frontend and the Celery tasks to run the processes in the background.

    For server hosting, I used DigitalOcean as I find them to be a great value for the money: $5 a month for a VPS with 1 vCPU, 512MB of RAM, 20GB disk space.  I don’t really need more than the 20GB of disk space as I’m not planning on storing every download, I could just run a scheduled job (which Celery has a module for called ‘celerybeat’, which is basically cron) to clean up the files.

    I used an existing Bootstrap template that I used for another project, which is very simple but does the job.  The final product ended up looking like this:


    Because of the simple UI, the app works great on tablets and phones as well.  I currently set the limit for the maximum video length to 3 hours, which should be more than enough for most videos people would want to convert.

    Check it out at:

  • How to create a self-signed X.509 SSL certificate with OpenSSL in one command line

    Posted on June 18th, 2014 webmaster No comments         

    I must have ran this command a hundred times by now but could never remember it:

    sudo openssl req -x509 -nodes -days 3650 -newkey rsa:2048 -keyout example.key -out example.crt

    This will create a self-signed SSL certificate with an expiration of 10 years using a 2048-bit key.  You can use this certificate with Apache2, Nginx, etc.