1. Using Python ipdb from Jupyter

    If we try to use the usual ipdb commands from a Jupyter (IPython notebook)

    import ipdb; ipdb.set_trace()

    we will get a similar error:

    MultipleInstanceError                     Traceback (most recent call last)
    <ipython-input-1-f2b356251c56> in <module>()
        1 a=4
    ----> 2 import ipdb; ipdb.set_trace()
        3 b=5
        4 print a
        5 print b
    /home/nnn/anaconda/lib/python2.7/site-packages/ipdb/__init__.py in <module>()
        14 # You should have received a copy of the GNU General Public License along with this program. If not, see http://www.gnu.org/licenses/.
    ---> 16 from ipdb.__main__ import set_trace, post_mortem, pm, run, runcall, runeval, launch_ipdb_on_exception
        18 pm                       # please pyflakes
    /home/nnn/anaconda/lib/python2.7/site-packages/ipdb/__main__.py in <module>()
        71         # the instance method will create a new one without loading the config.
        72         # i.e: if we are in an embed instance we do not want to load the config.
    ---> 73         ipapp = TerminalIPythonApp.instance()
        74         shell = get_ipython()
        75         def_colors = shell.colors
    /home/nnn/anaconda/lib/python2.7/site-packages/traitlets/config/configurable.pyc in instance(cls, *args, **kwargs)
        413             raise MultipleInstanceError(
        414                 'Multiple incompatible subclass instances of '
    --> 415                 '%s are being created.' % cls.__name__
        416             )
    MultipleInstanceError: Multiple incompatible subclass instances of TerminalIPythonApp are being created.

    The solution is to use Tracer instead:

    from IPython.core.debugger import Tracer

    Source: http://stackoverflow.com/questions/35613249/using-ipdb-to-debug-python-code-in-one-cell-jupyter-or-ipython

    read more


  2. How to publish a Python package to PyPI

    PyPI is the Python Package Index, that archive that let you install a package using pip, for example: pip install Flask

    In the past days I started writing a Python API client for Toshl expense manager and I decided to publish the library on PyPI. You can have a look at my library here https://github.com/andreagrandi/toshl-python (please note: it's still in development and Toshl API is not even public yet) in case you are not sure how to structure it.

    I found a nice guide but it wasn't complete (for example it didn't say how to sign packages) so I decided to rewrite it adding more information.

    Create PyPI accounts

    To publish packages on PyPI you need to create two accounts: one for the production server and another one for the test server. When you register, please specify (if you have one, but I really hope you do) the PGP id of your public key. Once the accounts are created, you need to create a file named .pypirc in your \$HOME folder containing the following configuration:

    index-servers =

    Please substitute your_username and your_password with the details you sent during the registration.

    Preparing the package

    I assume you have structured your library in the proper way and have included a setup.py with all the configuration (it's not something specific to PyPI so you should have done it already). If you haven't I remember you can give a look at my library here https://github.com/andreagrandi/toshl-python in particular to the setup.py:

    from setuptools import setup, find_packages
        author='Andrea Grandi',
        description='Python client library for Toshl API.',

    Upload the package to PyPI Test server

    The first time you upload the package you will need to register it:

    python setup.py register -r pypitest

    and then you will need to build the package and upload it (please note I'm using the --sign to sign the package with PGP):

    python setup.py sdist upload --sign -r pypitest

    Upload the package to PyPI production server

    Once you have verified that you are able to build and upload the package to the test server (without getting any errors), you should upload it to the production server:

    python setup.py register -r pypi
    python setup.py sdist upload --sign -r pypi

    This is everything you need to do if you want to publish a Python package on PyPI. Happy coding!

    read more


  3. Using a light sensor with BBC micro:bit and MicroPython

    A light sensor is a small component with a particular characteristic: it is basically a resistor and its resistance decreases if the light is more intense. To use it with micro:bit we need to use one of the analogic ports. To build this circuit you will need a breadboard, 3 jumper wires, a 10k resistance and possibly a Kitronik breadboard kit.

    The project

    I wanted to realise a simple project where, depending on the light intensity captured by the light sensor, the micro:bit shows an image of the Sun if the light is intense and an image of the Moon if the light is less intense.

    Here is the complete circuit scheme:


    "Image Copyright © Kitronik"

    and here is a picture of the finished project I created:


    The source code I needed is available here:

    and as a demo I realised this small video:

    read more


  4. Prototyping BBC micro:bit projects with Kitronik breadboard kit

    BBC micro:bit has a few IO pins that can be used to interact with external devices. The problem with the board is that it's not easy to connect the classic jumper wires (those that we normally connect to a breadboard) to the micro:bit, unless using a crocodile clip and being limited to just 3 pins.

    Kitronik breadboard kit solves this problem, offering an interface where the micro:bit can be plugged and all the pins are easily connectable to the breadboard using normal male/female jumper wires.

    I've built a very simple circuit following an example you can find on this manual https://www.kitronik.co.uk/pdf/5603_inventors_kit_for_the_bbc_microbit_tutorial_book.pdf


    To build the circuit you also need 4 male/female jumper wires and two buttons. All this circuit does is to connect the buttons to the micro:bit pins that relate to those buttons. Basically pressing those buttons is the same as pressing button A or button B on the micro:bit board. Here you can see the schema in detail:

    Screenshot 2016-02-0714.30.36

    I've also made a short video so that you can see it in action:

    And of course the source code is available too:

    read more


« Page 2 / 48 »