Other articles

  1. Using ipdb with Python 3.7.x breakpoint

    Python 3.7.x introduced a new method to insert a breakpoint in the code. Before Python 3.7.x to insert a debugging point we had to write import pdb; pdb.set_trace() which honestly I could never remember (and I also created a snippet on VS Code to auto complete it).

    Now you can just write breakpoint() that's it!

    Now... the only problem is that by default that command will use pdb which is not exactly the best debugger you can have. I usually use ipdb but there wasn't an intuitive way of using it... and no, just installing it in your virtual environment, it won't be used by default.

    How to use it then? It's very simple. The new debugging command will read an environment variable named PYTHONBREAKPOINT. If you set it properly, you will be able to use ipdb instead of pdb.

    export PYTHONBREAKPOINT=ipdb.set_trace
    

    At this point, any time you use breakpoint() in your code, ipdb will be used instead of pdb.

    References

    • https://hackernoon.com/python-3-7s-new-builtin-breakpoint-a-quick-tour-4f1aebc444c
    read more

    comments

  2. Go: defining methods on struct types

    In Go it's possible to define methods on struct types. The syntax needed for it can be a bit strange for people that are used to define classes and methods in Java, C# etc... but once you learn it it's quite easy to use.

    In my case for example I needed something that could contain a Timer object, a string and a method that could start the timer and call a method at the end of the Timer execution. I implemented it in this way:

    ``` {.toolbar:2 .lang:go .decode:true} type DeviceTimer struct { DeviceID string DeviceTimer *time.Timer }

    func (timer DeviceTimer) startTimer() { <-timer.DeviceTimer.C notifyDeviceTimerExpired(timer.DeviceID) }

    The key point is **row 6** "*func (timer DeviceTimer) startTimer() { ...
    }*" where I defined a method called **startTimer** and I specify timer
    **DeviceTimer** inside the func definition. This basically "extends" the
    struct DeviceTimer adding that method to it. This means that I can call
    that method in this way:
    
    ``` {.toolbar:2 .lang:go .decode:true}
    timer := time.NewTimer(time.Millisecond * 300)
    device_timer := DeviceTimer{"abc123", timer}
    go device_timer.startTimer()
    

    This is all you need to do. If you want to read more about this subject, I can suggest to read these two articles:

    Note: I'm not a Go expert and these are just my personal notes I'm taking during my learning experience. I'm very keen to share my notes with everyone, but please don't take them as notes from an expert Go developer.

    read more

    comments

  3. Using GtkIconView in Python: a small tutorial

    In these days I was looking for a simple tutorial to understand how to use GtkIconView, but the only thing I was able to find was an example in PHP-Gtk. So I decided to translate it in Python language, thinking it would be useful for other people trying to use that Gtk control. You can find the code here:

    [sourcecode language='python']
    import gtk
    import gobject

    DEFAULT_IMAGE_WIDTH = 100

    # Main Window setup
    window = gtk.Window(gtk.WINDOW_TOPLEVEL)
    window.set_size_request(400, 240)
    window.connect("destroy", gtk.main_quit)
    window.set_title("Python GtkIconView Test")

    # Add a VBox
    vbox = gtk.VBox()
    window.add(vbox)

    # Setup Scrolled Window
    scrolled_win = gtk.ScrolledWindow()
    scrolled_win.set_policy(gtk.POLICY_AUTOMATIC, gtk.POLICY_AUTOMATIC)

    # Setup ListStore to contain images and description
    model = gtk.ListStore(gtk.gdk.Pixbuf, gobject.TYPE_STRING)

    # Create a tuple with image files
    immagini = ("BD786-TFR.jpg", "guido_sottozero.jpg", "IMG_0056.JPG", "movies_card.jpg")

    for im in immagini:
    try:
    pixbuf = gtk.gdk.pixbuf_new_from_file(im)
    pix_w = pixbuf.get_width()
    pix_h = pixbuf.get_height()
    new_h = (pix_h * DEFAULT_IMAGE_WIDTH) / pix_w # Calculate the scaled height before resizing image
    scaled_pix = pixbuf.scale_simple(DEFAULT_IMAGE_WIDTH, new_h, gtk.gdk.INTERP_TILES)
    model.append((scaled_pix, im))
    except:
    pass

    # Setup GtkIconView
    view = gtk.IconView(model) # Pass the model stored in a ListStore to the GtkIconView
    view.set_pixbuf_column(0)
    view.set_text_column(1)
    view.set_selection_mode(gtk.SELECTION_MULTIPLE)
    view.set_columns(0)
    view.set_item_width(150)

    # Pack objects and show them all
    scrolled_win.add(view)
    vbox.pack_start(scrolled_win)
    window.show_all()

    gtk.main()
    [/sourcecode]

    The important thing to notice is that you have to store all the images in a GtkListStore and pass it to the GtkIconView as "model" parameter. I hope this example is clear. If you have any question, please comment this post and I'll try to answer.

    This is a screenshot of this example:

    gtkiconview

    read more

    comments

Page 1 / 2 »

social