Get a site
  • Guide

    How to create a watchdog in Python to look for filesystem changes

    Hey guys, I’m back! And for the ones of you that have asked me what happened to The Python Corner… long story short: no panic, I’m not dead (yet). I’ve been just very busy with my everyday work, but … who care, let’s talk about Python now, ok? Today’s post is about watchdogs. A watchdog is a little piece of software that monitors our filesystem looking for any changes (like the creation, change or deletion of a file or of a directory). When a change occurs, the watchdog report it to us raising a specific event that we can handle. For example, let’s suppose you have developed a program that…

  • Guide

    How to create a Windows Service in Python

    Hi guys, today’s post is just for the ones of you that work with the “OS of the misoriented slashes”: Microsoft Windows. 🙂 Have you ever had the need of writing a Python script that could run in background as a Windows Service? In this post you will learn how to do it in less than 10 minutes, no jokes. I will skip all the introduction about Windows Services, how convenient they could be, how much could be appreciated the fact that they can be run in background even when the user is logged off etc… I mean, if you can code in Python and you use Windows I bet you…

  • Guide

    How to make your code faster by using a cache in Python

    If the first concern of a developer is to be sure that the code they write works well, the second one is to make sure that it run fast. This is expecially true when you’re dealing with web applications, where the scalability of your application is a crucial topic. For this reason, one of the most important tool we can use to improve the speed of our code is the use of a cache system. A cache system is a component that stores data so that future requests for data we already served in the past, could be accomplished faster. There are a lot of solutions that can be used…

  • Guide

    Generators in Python — should I use them?

    Following to a request of a reader, today we’re going to discuss when to use iterators and generators in Python. First of all, it’s important to know what iterators and generators are, so if you don’t know exactly what they are, I suggest to have a look at my previous article on this topic. Now that everything is clear, we can start analyzing when to use these features. Let’s start saying that if you have read my previous article, the use of the iterator protocol should be quite clear: you use iterator protocol when you have a custom object that you want to be “iterable”. That’s it, so easy. If…

  • Guide

    Lambdas and functions in Python

    In my last post I discussed some ways to avoid nested code in Python and discussing the ”filter” and ”map” functions I mentioned the lambda funcions. After that article, some reader asked me to write a little more about this topic, so … here I am. 🙂 Let’s start with a mantra. If you want to know what something is, in Python, just use your REPL. So, start the Python REPL and define a lambda: Python 3.6.2 |Anaconda custom (64-bit)| (default, Sep 19 2017, 08:03:39) [MSC v.1900 64 bit (AMD64)] on win32 Type “help”, “copyright”, “credits” or “license” for more information. >>> my_lambda = lambda x: x+1 Now, try to ask Python…

  • Guide

    The art of avoiding nested code

    Today’s article is about nested code and how to avoid it. Why we should try to it? Well the answer is inside your heart, and in your Python interpreter… Start your REPL and write: import this you will get the “Zen Of Python” by Tim Peters. The Zen of Python, by Tim Peters Beautiful is better than ugly. Explicit is better than implicit. Simple is better than complex. Complex is better than complicated. Flat is better than nested. Sparse is better than dense. Readability counts. Special cases aren’t special enough to break the rules. Although practicality beats purity. Errors should never pass silently. Unless explicitly silenced. In the face of…

  • Guide

    Logging in Python

    One of the most underestimated topic that I’ve seen in my working experience is the logs management. A lot of people don’t care at all about logging the execution of their programs and I’ve seen lot of code released in production environment that don’t log anything. To log seems to be a waste of time to them, expecially if the code they’re writing is apparently simple. So, why bother logging the execution of a program if the program can run great with no logs? Actually, logging the execution of you own program avoids you lots of headaches when something goes wrong in production and make your coding experience easier. Besides,…

  • Guide

    Writing a FUSE filesystem in Python

    We ran into a problem last week. Our web application produces a lot of documents that have to be accessed frequently for a couple of months after they’re created. However, in less than a year these documents will be almost never accessed anymore, but we need to keep them available for the web application and for tons of other legacy apps that might need to access them. Now, these documents take a lot of space on our expensive but super fast storage system (let’s call it primary storage system or PSS from now on) and we would like to be able to move them on the cheaper, not so good…

  • Guide

    Working with Exception in Python

    According to the official documentation, an exception is “an error detected during execution not unconditionally fatal”. Let’s start the interpreter and write: >>> 5/0Traceback (most recent call last): File "<pyshell#7>", line 1, in <module> 5/0ZeroDivisionError: division by zero As you can see we asked to the interpreter to divide the number 5 by 0. Even if our request was syntattically correct when the interpreter tried to compute it, it “raised” the ZeroDivisionError exception to signal us that we asked something impossible. There are a lot of builtin exceptions in the base library to handle different kind of errors (system errors, value errors, I/O errors, Arithmetic errors etc…) and to know…

  • Guide

    Python Metaclasses

    Working with Python means working with objects because in Python, everything is an object. So, for example: >>> type(1)<class 'int'>>>> type('x')<class 'str'> As you can see, even basic types like integer and strings are objects, in particular they are respectively instances of int and str classes. So, since everything is an object and given that an object is an instance of a class… what is a class? Let’s check it: >>> type(int)<class 'type'>>>> type(str)<class 'type'> It turns out that classes are object too, specifically they are instances of the “type” class, or better, they are instances of the “type” metaclass. A metaclass is the class of a class and the…