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

PyDev of the Week: R. David Murray

This week we welcome R. David Murray (@rdavidmurray) as our PyDev of the Week. Mr. Murray is a core developer of the Python language and is currently maintainer of the email module. He has a Python blog, although it hasn’t been updated in a while. You might want to check out his Github profile to see some of the other projects he is working on. Let’s spend some time getting to know our fellow Pythonista!

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

In my youth I thought I wanted to be a Particle Physicist.  Went to the University of Pennsylvania to study physics, and indulged my second love (computers, which at that time were just at the IBM mainframe/TRS 80 stage) by getting a work study job in the Physics Department computing facility.  By my second year there I had decided that Physics itself was just too…messy, but that I loved the math and especially working with computers.  So I graduated with a BA in computer mathematics, and have proceeded to use the computer half and not the math half for most of my career.

Outside of work, I love reading Science fiction and Contra Dancing which my wife and I do twice a week on average.  It keeps me healthy and feeling younger (I find the (live) music wonderful, and it is a dance form that attracts all ages, which is psychologically beneficial, and as an added benefit it is great exercise).

Continue reading PyDev of the Week: R. David Murray

Python 201: An Intro to importlib

Python provides the importlib package as part of its standard library of modules. Its purpose is to provide the implementation to Python’s import statement (and the __import__() function). In addition, importlib gives the programmer the ability to create their own custom objects (AKA an importer) that can be used in the import process.

What about imp?

There is another module called imp that provides an interface to the mechanisms behind Python’s import statement. This module was deprecated in Python 3.4. It is intended that importlib should be used in its place.

This module is pretty complicated, so we’ll be limiting the scope of this article to the following topics:

  • Dynamic imports
  • Checking is a module can be imported
  • Importing from the source file itself

Let’s get started by looking at dynamic imports!

Continue reading Python 201: An Intro to importlib

Python 201 – super

I’ve written about super briefly in the past, but decided to take another go at writing something more interesting about this particular Python function.

The super built-in function was introduced way back in Python 2.2. The super function will return a proxy object that will delegate method calls to a parent or sibling class of type. If that was a little unclear, what it allows you to do is access inherited methods that have been overridden in a class. The super function has two use cases. The first is in single inheritance where super can be used to refer to the parent class or classes without actually naming them explicitly. This can make your code more maintainable in the future. This is similar to the behavior that you will find in other programming languages, like Dylan’s *next-method*.

The second use case is in a dynamic execution environment where super supports cooperative multiple inheritance. This is actually a pretty unique use case that may only apply to Python as it is not found in languages that only support single inheritance nor in statically compiled languages.

super has had its fair share of controversy even among core developers. The original documentation was confusing and using super was tricky. There were some who even labeled super as harmful, although that article seems to apply more to the Python 2 implementation of super then the Python 3 version. We will start out this chapter by looking at how to call super in both Python 2 and 3. Then we will learn about Method Resolution Order.

Continue reading Python 201 – super

Python 101: An Intro to Benchmarking your code

What does it mean to benchmark ones code? The main idea behind benchmarking or profiling is to figure out how fast your code executes and where the bottlenecks are. The main reason to do this sort of thing is for optimization. You will run into situations where you need your code to run faster because your business needs have changed. When this happens, you will need to figure out what parts of your code are slowing it down.

This chapter will only cover how to profile your code using a variety of tools. It will not go into actually optimizing your code. Let’s get started!

Continue reading Python 101: An Intro to Benchmarking your code

PyDev of the Week: Wesley Chun

This week we welcome Wesley Chun as our PyDev of the Week! Wesley Chun, MSCS, is author of Prentice Hall’s bestselling “Core Python” series (corepython.com), the “Python Fundamentals” companion videos, co-author of “Python Web Development with Django” (withdjango.com), and has written for Linux Journal, CNET, and InformIT. In addition to being an engineer at Google, he runs CyberWeb (cyberwebconsulting.com), a consultancy specializing in Python training. Wesley has over 25 years of programming, teaching, and writing experience, including more than a decade of Python. While at Yahoo!, helped create Yahoo!Mail using Python. Wesley holds degrees in Computer Science, Mathematics, and Music from the University of California. He is based in Silicon Valley and can be reached on Twitter (twitter.com/wescpy) or Google+ (google.com/+WesleyChun). Let’s spend a few minutes getting to know him better!

 

chun_wesley_c

 

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

 

I’ve been a software engineer by profession, coding in Python, JavaScript, and a few others, at big companies as well as startups. I’m currently at Google, a nice blend of both worlds. I enjoy helping the next generation of developers get up-to-speed on the latest technologies and best practices with the least amount of friction, meaning that in addition to coding, I also teach technology, publish books & blogposts, and give talks at conferences. Stuff I like to do away from the keyboard include family time, piano, personal finance, world travel, physical fitness (cycling, yoga, biking, basketball), poker, and learning a few words in as many languages as I can.

 

Continue reading PyDev of the Week: Wesley Chun

Python 3: An Intro to Encryption

Python 3 doesn’t have very much in its standard library that deals with encryption. Instead, you get hashing libraries. We’ll take a brief look at those in the chapter, but the primary focus will be on the following 3rd party packages: PyCrypto and cryptography. We will learn how to encrypt and decrypt strings with both of these libraries.


Hashing

If you need secure hashes or message digest algorithms, then Python’s standard library has you covered in the hashlib module. It includes the FIPS secure hash algorithms SHA1, SHA224, SHA256, SHA384, and SHA512 as well as RSA’s MD5 algorithm. Python also supports the adler32 and crc32 hash functions, but those are in the zlib module.

One of the most popular uses of hashes is storing the hash of a password instead of the password itself. Of course, the hash has to be a good one or it can be decrypted. Another popular use case for hashes is to hash a file and then send the file and its hash separately. Then the person receiving the file can run a hash on the file to see if it matches the hash that was sent. If it does, then that means no one has changed the file in transit.

Continue reading Python 3: An Intro to Encryption

Python 101: How to timeout a subprocess

The other day I ran into a use case where I needed to communicate with a subprocess I had started but I needed it to timeout. Unfortunately, Python 2 does not have a way to timeout the communicate method call so it just blocks until it either returns or the process itself closes. There are lots of different approaches that I found on StackOverflow, but I think my favorite was using Python’s threading module’s Timer class:

import subprocess
 
from threading import Timer
 
kill = lambda process: process.kill()
cmd = ['ping', 'www.google.com']
ping = subprocess.Popen(
    cmd, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
 
my_timer = Timer(5, kill, [ping])
 
try:
    my_timer.start()
    stdout, stderr = ping.communicate()
finally:
    my_timer.cancel()

Continue reading Python 101: How to timeout a subprocess

PyDev of the Week: Ed Leafe

This week we welcome Ed Leafe (@EdLeafe) as our PyDev of the Week! Ed is one of the co-creators of the Dabo Application Framework, which is a neat wrapper around wxPython and is aimed at building database driven applications, although you can use it for more than that. Ed also authors a fun tech blog. Let’s take a few minutes to get to know him better!

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

I’ve had many careers, with software development being the latest and longest one. While my degree is in Biochemistry, CS was a minor, so computers have always been somewhere in the picture. For example, I’ve had 3 startup businesses, but they were all in the construction field, and unlike today, we had to be profitable from the start. But to tie that to today, I partnered with an architect (an actual licensed building architect, not a self-important programmer) to come up with 3-D visualizations of decks, additions, etc, that used photographs as a base with a CAD program drawing on top. The CAD items were linked to a database, so we could generate both a parts list and an estimate directly from the drawing. If the customer wanted to change something, such as add a window, we made the change on the drawing, and the estimate updated accordingly. Oh, and this was in the 1989-91 time frame, running on a Mac SE.

I’m also a photographer. I exhibited quite a bit when I was younger, and am starting to get back into it. Here’s some of my recent work: https://goo.gl/FR7PwX

Continue reading PyDev of the Week: Ed Leafe