Book Review: Python Projects for Kids

I get asked by publishers to review books from time to time. Last month, Packt asked me if I’d be willing to review for their book, Python Projects for Kids by Jessica Ingrassellino. Frankly I tend to avoid beginning Python books now because they tend to be very similar, but I thought this one might be interesting.


Quick Review

  • Why I picked it up: In this case, because Packt Publishing asked me to
  • Why I finished it: Mostly because Packt personnel badgered me to do so
  • I’d give it to: Not really sure. There are much better, more in-depth beginner books for Python out there

Continue reading Book Review: Python Projects for Kids

Python 101: An Intro to ftplib

The File Transfer Protocol (FTP) is used by many companies and organizations for sharing data. Python provides an File Transfer Protocol module in its standard library called ftplib that implements the client side of the FTP protocol. You can learn all about the File Transfer Protocol by reading the RFC 959 document on the Internet. However the full specification is outside the scope of this article. Instead we will focus on the following topics:

  • Connecting to an FTP server
  • Navigating it’s structure
  • Downloading files from the FTP server
  • Uploading files to an FTP server

Let’s get started!

Continue reading Python 101: An Intro to ftplib

PyDev of the Week: Amber Brown

This week we welcome Amber Brown (@hawkieowl) as our PyDev of the Week. Amber is one of the core developers of the Twisted package. She has a fun little twisted blog that you might enjoy checking out. You can also see what Amber’s been up to by checking out her Github profile. She has also been interviewed by DjangoGirls. Let’s take a few moments to get to know her better!

Can you tell us a little about yourself (hobbies, education, etc):

I’m an Australian, born in Perth, but ultimately hailing from several small country towns along the west. I’m self-educated in software development and IT, and worked for half a decade in local government as IT, and the past three years in software development full-time. My hobbies are board/card games (Go and MTG, mostly!), video games (Factorio, XCOM 2), and the occasional photography. Programming isn’t so much a hobby for me as a life; it’s questionably healthy for me, I’m sure, but it is what it is! The most surprising aspect of me is generally my age, being 22. My middle name is Hawkie (from HawkOwl, my handle), and like most trans women, I enjoy salt and puns.

Continue reading PyDev of the Week: Amber Brown

Python 101: Redirecting stdout

Redirecting stdout to something most developers will need to do at some point or other. It can be useful to redirect stdout to a file or to a file-like object. I have also redirected stdout to a text control in some of my desktop GUI projects. In this article we will look at the following:

  • Redirecting stdout to a file (simple)
  • The Shell redirection method
  • Redirecting stdout using a custom context manager
  • Python 3’s contextlib.redirect_stdout()
  • Redirect stdout to a wxPython text control

Continue reading Python 101: Redirecting stdout

Book Review: Modular Programming with Python

Earlier this year or late 2015, Packt Publishing asked me to be technical reviewer for a book called “Modular Programming with Python” by Erik Westra. It sounded really interesting and it ended up being one of the best books I’ve read from Packt. Note that I am the sole technical reviewer of the book. I’m not sure why Packt went that route for this book as they normally have 2-3 reviewers, but perhaps they are trying to accelerate their writing process.


Quick Review

  • Why I picked it up: Primarily because Packt asked me to. However it really is an interesting book
  • Why I finished it: Because the book is short and very well written
  • I’d give it to: An intermediate developer who needs to brush up on their code organizational skills

Continue reading Book Review: Modular Programming with Python

PyDev of the Week: Georg Brandl

This week we welcome Georg Brandl (@birkenfeld) as our PyDev of the Week. Georg is a core developer of the Python language and has been for over 10 years. He is also a part of the Pocoo team. You can see what projects get him excited by checking out his Github profile. Let’s take a few moments to learn more about him!

Can you tell us a little about yourself (hobbies, education, etc):

I studied physics and during my PhD project was working at a large German research center, where software is a big part of conducting research, and external scientists come and use our facilitiy for their own experiments. Meanwhile I’m still at the same place, but working on that software as my main job. We’re using Python extensively, basically giving our guests a highly fancified Python shell as the main user interface.

Continue reading PyDev of the Week: Georg Brandl

Python 201: What are descriptors?

Descriptors were introduced to Python way back in version 2.2. They provide the developer with the ability to add managed attributes to objects. The methods needed to create a descriptor are __get__, __set__ and __delete__. If you define any of these methods, then you have created a descriptor.

The idea behind the descriptor is to get, set or delete attributes from your object’s dictionary. When you access a class attribute, this starts the lookup chain. Should the looked up value be an object with one of our descriptor methods defined, then the descriptor method will be invoked.

Descriptors power a lot of the magic of Python’s internals. They are what make properties, methods and even the super function work. They are also used to implement the new style classes that were also introduced in Python 2.2.


The Descriptor Protocol

The protocol to create a descriptor is really quite easy. You only need to define one or more of the following:

  • __get__(self, obj, type=None), returns value
  • __set__(self, obj, value), returns None
  • __delete__(self, obj), returns None

Once you’ve defined at least one, you have created a descriptor. If you can you define both __get__ and __set__, you will have created a data descriptor. A descriptor with only __get__() defined are known as non-data descriptors and are usually used for methods. The reason for this distinction in descriptor types is that if an instance’s dictionary happens to have a data descriptor, the descriptor will take precedence during the lookup. If the instance’s dictionary has an entry that matches up with a non-data descriptor, then the dictionary’s own entry will take precedence over the descriptor.

You can also create a read-only descriptor if you define both __get__ and __set__, but raise an AttributeError when the __set__ method is called.

Continue reading Python 201: What are descriptors?

Python: How to Create an Exception Logging Decorator

The other day, I decided I wanted to create a decorator to catch exceptions and log them. I found a rather complex example on Github that I used for some ideas on how to approach this task and came up with the following:

# exception_decor.py
 
import functools
import logging
 
def create_logger():
    """
    Creates a logging object and returns it
    """
    logger = logging.getLogger("example_logger")
    logger.setLevel(logging.INFO)
 
    # create the logging file handler
    fh = logging.FileHandler("/path/to/test.log")
 
    fmt = '%(asctime)s - %(name)s - %(levelname)s - %(message)s'
    formatter = logging.Formatter(fmt)
    fh.setFormatter(formatter)
 
    # add handler to logger object
    logger.addHandler(fh)
    return logger
 
 
def exception(function):
    """
    A decorator that wraps the passed in function and logs 
    exceptions should one occur
    """
    @functools.wraps(function)
    def wrapper(*args, **kwargs):
        logger = create_logger()
        try:
            return function(*args, **kwargs)
        except:
            # log the exception
            err = "There was an exception in  "
            err += function.__name__
            logger.exception(err)
 
            # re-raise the exception
            raise
    return wrapper

In this code, we have two functions. The first one creates a logging object and returns it. The second function is our decorator function. Here we wrap the passed in function in a try/except and log any exceptions that occur using our logger. You will note that I am also logging the function name the the exception occurred in.

Now we just need to test this decorator out. To do so, you can create a new Python script and add the following code to it. Make sure you save this in the same location that you saved the code above.

from exception_decor import exception
 
@exception
def zero_divide():
    1 / 0
 
if __name__ == '__main__':
    zero_divide()

When you run this code from the command line, you should end up with a log file that has the following contents:

2016-06-09 08:26:50,874 - example_logger - ERROR - There was an exception in  zero_divide
Traceback (most recent call last):
  File "/home/mike/exception_decor.py", line 29, in wrapper
    return function(*args, **kwargs)
  File "/home/mike/test_exceptions.py", line 5, in zero_divide
    1 / 0
ZeroDivisionError: integer division or modulo by zero

I thought this was a handy piece of code and I hope you will find it useful too!

UPDATE: An astute reader pointed out that it would be a good idea to generalize this script such that you can pass the decorator a logger object. So let’s look at how that works!

Passing a logger to our decorator

First off, let’s split our logging code off into its own module. Let’s call it exception_logger.py. Here’s the code to put into that file:

# exception_logger.py
 
import logging
 
def create_logger():
    """
    Creates a logging object and returns it
    """
    logger = logging.getLogger("example_logger")
    logger.setLevel(logging.INFO)
 
    # create the logging file handler
    fh = logging.FileHandler(r"/path/to/test.log")
 
    fmt = '%(asctime)s - %(name)s - %(levelname)s - %(message)s'
    formatter = logging.Formatter(fmt)
    fh.setFormatter(formatter)
 
    # add handler to logger object
    logger.addHandler(fh)
    return logger
 
logger = create_logger()

Next we need to modify our decorator code so we can accept a logger as an argument. Be sure to save it as exception_decor.py

# exception_decor.py
 
import functools
 
 
def exception(logger):
    """
    A decorator that wraps the passed in function and logs 
    exceptions should one occur
 
    @param logger: The logging object
    """
 
    def decorator(func):
 
        def wrapper(*args, **kwargs):
            try:
                return func(*args, **kwargs)
            except:
                # log the exception
                err = "There was an exception in  "
                err += func.__name__
                logger.exception(err)
 
            # re-raise the exception
            raise
        return wrapper
    return decorator

You will note that we have multiple levels of nested functions here. Be sure to study it closely to understand what’s going on. Finally we need to modify our testing script:

from exception_decor import exception
from exception_logger import logger
 
@exception(logger)
def zero_divide():
    1 / 0
 
if __name__ == '__main__':
    zero_divide()

Here we import our decorator and our logger. Then we decorate our function and pass the decorator our logger object. If you run this code, you should see the same file generated as you did in the first example. Have fun!

Python: An Intro to Regular Expressions

Regular expressions are basically a tiny language all their own that you can use inside of Python and many other programming languages. You will often hear regular expressions referred to as “regex”, “regexp” or just “RE”. Some languages, such as Perl and Ruby, actually support regular expression syntax directly in the language itself. Python only supports them via a library that you need to import. The primary use for regular expressions is matching strings. You create the string matching rules using a regular expression and then you apply it to a string to see if there are any matches.

The regular expression “language” is actually pretty small, so you won’t be able to use it for all your string matching needs. Besides that, while there are some tasks that you can use a regular expression for, it may end up so complicated that it becomes difficult to debug. In cases like that, you should just use Python. It should be noted that Python is an excellent language for text parsing in its own right and can be used for anything you do in a regular expression. However, it may take a lot more code to do so and be slower than the regular expression because regular expressions are compiled down and executed in C.

Continue reading Python: An Intro to Regular Expressions

PyDev of the Week: Kenneth Reitz

This week we welcome Kenneth Reitz (@kennethreitz) as our PyDev of the Week! Kenneth is the developer behind the Requests package, which is one of my favorites. He is also the co-author of the upcoming O’Reilly Book, The Hitchhiker’s Guide to Python. You can visit his website to see what he’s been up to or visit his Github profile to see what projects he’s passionate about. Let’s take some time to get to know him better!

Can you tell us a little about yourself (hobbies, education, etc):

kenneth_reitz

Hi, my name is Kenneth Reitz. I have a deep love for excellent design, synthesizers, cameras, software, and Python. I create a great number of things, but my magnum opus is “Requests: HTTP for Humans”: one of the most widely-used Python packages available today.

Most of my day-time life revolves around Python and serving the Python Community. I am the Language Owner of Python at Heroku, where I wear many different hats. My evenings are typically filled with either more Python, taking and editing photos, producing electronic music, or consuming massive amounts of media (music, TV shows, films, articles, videos, books). I occasionally enjoy playing video games from my childhood.

I have an excellent education, despite dropping out of university (for computer science) within a year of arriving. Best decision I ever made! I consider myself an autodiadict, and do not learn well in a classroom setting.

I found out earlier this year that I have bipolar I disorder, which has been quite an adventure. It is both a blessing and a curse. I’ve learned quite a lot while going through the process of diagnosis, management, and cultivating a self-awareness around this.

Continue reading PyDev of the Week: Kenneth Reitz