Other articles

  1. Skipping tests depending on the Python version

    Sometimes we want to run certain tests only on a specific version of Python.

    Suppose you are migrating a large project from Python 2 to Python 3 and you know in advance that certain tests won't run under Python 3.

    Chances are that during the migration you are already using the six library. The six libraries have two boolean properties which are initialised to True depending on the Python version which is being used: PY2 when running under Python 2 and PY3 when running under Python 3.

    This library, combined with the skipIf method of unittest library can be used to easily skip tests when using Python 3:

    import six
    import unittest
    class MyTestCase(unittest.TestCase):
        @unittest.skipIf(six.PY3, "not compatible with Python 3")
        def test_example(self):
            # This test won't run under Python 3


    Thanks to my colleague Nicola for giving me the inspiration to write this post.

    read more


  2. Installing Python and virtualenv on OSX

    Every time I need to install Python on OSX or whenever a colleague asks for help, I have to search fo the most updated instructions on Google, and every time I find different ways of doing the exact same thing.

    Tired of this, I decided to write down my own notes. Please note that I don't claim this to be the best way of installing Python on OSX. It works fine for me so use it at your own risk.


    To follow these instructions you need to at least have installed brew on OSX. Please follow the instructions on the official website: https://brew.sh

    Installing Python 3.7.x and Python 2.7.x

    Even if I strongly suggest to start every new project with Python 3 (since Python 2 will only be supported until the end of 2019), there may be use cases when version 2 is still required, so I will give you the instructions to install both.

    Installing Python 3.7.x

    brew install python

    This will install Python 3 by default.

    Installing Python 2.7.x

    brew install [email protected]

    This will install version 2 of Python.

    Add the Python locations to PATH

    Edit your .bashrc or .zshrc and add this:

    export PATH="/usr/local/opt/python/libexec/bin:/usr/local/bin:$PATH"

    You will need to close your terminal and reopen it for the changes to be applied. Once you have done it, you can verify if Python 3 and Python 2 have been installed correctly:

    python --version
    Python 3.7.1


    python2 --version
    Python 2.7.15

    Install virtualenv and virtualenvwrapper

    When working with Python, it's a good thing not to install packages system wide, but confine them in virtual environments. A good and well tested way of doing that is to use virtualenv (and its companion virtualenvwrapper) which makes the most common operations easier.

    pip install virtualenv
    pip install virtualenvwrapper

    Those (and only those) two packages will be installed system wide, because we will need them to be available outside of a virtual environment.

    Configure virtualenv

    Edit again your .bashrc (or .zshrc) and add these lines:

    export WORKON_HOME=~/.virtualenvs
    [ -f /usr/local/bin/virtualenvwrapper.sh ] && source /usr/local/bin/virtualenvwrapper.sh

    This will configure the default location where to store your virtual environments and will run a command every time you open a new terminal, to make sure virtualenvwrapper can work correctly.

    Test if the installed tools are working

    To make sure everything has been configured correctly, please close and reopen your terminal and let's try to create a new virtual environment:

    mkvirtualenv test

    which should output something like this:

    Using base prefix '/usr/local/Cellar/python/3.7.1/Frameworks/Python.framework/Versions/3.7'
    New python executable in /Users/andrea/.virtualenvs/test/bin/python3.7
    Also creating executable in /Users/andrea/.virtualenvs/test/bin/python
    Installing setuptools, pip, wheel...
    virtualenvwrapper.user_scripts creating /Users/andrea/.virtualenvs/test/bin/predeactivate
    virtualenvwrapper.user_scripts creating /Users/andrea/.virtualenvs/test/bin/postdeactivate
    virtualenvwrapper.user_scripts creating /Users/andrea/.virtualenvs/test/bin/preactivate
    virtualenvwrapper.user_scripts creating /Users/andrea/.virtualenvs/test/bin/postactivate
    virtualenvwrapper.user_scripts creating /Users/andrea/.virtualenvs/test/bin/get_env_details
    (test) ➜  ~

    If you see something similar, it means that the virtual environment has been created correctly. Please note that by default this command will create an environment base on Python 3. Do you need to create one for Python 2? No problem, you just need to do the following:

    mkvirtualenv -p /usr/local/bin/python2 test

    which should output this:

    Running virtualenv with interpreter /usr/local/bin/python2
    New python executable in /Users/andrea/.virtualenvs/test/bin/python2.7
    Also creating executable in /Users/andrea/.virtualenvs/test/bin/python
    Installing setuptools, pip, wheel...
    virtualenvwrapper.user_scripts creating /Users/andrea/.virtualenvs/test/bin/predeactivate
    virtualenvwrapper.user_scripts creating /Users/andrea/.virtualenvs/test/bin/postdeactivate
    virtualenvwrapper.user_scripts creating /Users/andrea/.virtualenvs/test/bin/preactivate
    virtualenvwrapper.user_scripts creating /Users/andrea/.virtualenvs/test/bin/postactivate
    virtualenvwrapper.user_scripts creating /Users/andrea/.virtualenvs/test/bin/get_env_details
    (test) ➜  ~


    That's all you have to do to install and configure Python and virtualenv on OSX. If you have problems, comments or questions, feel free to leave a comment on this post.

    read more


  3. Why I mentor on Exercism.io

    Exercism (https://exercism.io) is a platform that has been created to help people improving their coding skills, thanks to the volunteers mentoring the students. There are tracks for almost all the popular languages and each track has coding tests that the students can download, solve offline using their preferred editor, and test the solution against the provided unit tests. Once the solution is ready to be reviewed (or even if it's not complete but the student needs help), it can be submitted to the website and it will go in a queue where the first available mentor will pick it and start mentoring.

    The service is free to use for all the students and the mentors are all volunteers (this doesn't mean that the platform doesn't have any costs. If you are curious about the resources needed to keep the platform alive, you can give a look at this answer on Reddit.

    When I found out about the platform, I decided to use it (as student) to improve my Go coding skills. I must say that I've been learning a lot from the mentors and some of them are putting a lot of effort to give you all the possible advices to improve your coding style. In a single exercise once, I learnt at least five things about Go I didn't know before!

    I've been a Python developer (professionally) for the last 5 years, but I've never considered myself an "expert". I decided to give it a try with mentoring, because I felt I wanted to give something back to the community, so I registered as mentor too and started mentoring in the Python track.

    The first surprise has been that mentoring other students, I was probably learning more than how much I was teaching. First of all, once you already know how to solve a problem, it's always interesting to look at other possible solutions. I've found sometimes that students were providing better (more concise and readable) solutions than mine. Last but not least, before advising someone about conding style or a more idiomatic solution, I always double check things from different sources. There is nothing wrong making mistakes, especially if you are learning... but it would be damaging for the student if I was teaching them something wrong, so I need to be sure about what I say. This of course makes me study, even the basic things, again and again and at the end of the day, my skills are better too.

    Once you join the mentors group, you are invited to a private Slack where you can count on the help of other mentors (we have channels for each track/language) or ask questions. So, if you are not sure about something, you can always ask around.

    If my story and experience convinced you, Exercism is looking for more mentors! The more we have available, the less time the students have to wait in a queue to be mentored. You can find all the instructions at this address https://mentoring.exercism.io

    read more


Page 1 / 50 »