Category Archives: Python

Is related to the Python Programming language in some way

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

PyDev of the Week: Karsten Hilbert

This week we welcome Karsten Hilbert as our PyDev of the Week! Karsten took over the GNUmed project, which is a an open source electronic medical record program written mostly in Python. Let’s take a few moments to get to know him better!

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

I am a Medical Doctor in Germany.

Why did you start using Python?

The OSS project I got involved with (GNUmed) was written in Python. When I took over the balance of ease-of-use, clarity and batteries-included felt just right so I stayed with it.

Continue reading PyDev of the Week: Karsten Hilbert

Python 201 Book Writing Update: Part 1 is Ready

I’ve been busily working on my second book, Python 201: Intermediate Python. In part one of the book, there are 10 chapters. I recently finished up the last chapter for that part of the book. While I have some tweaks I want to do to a couple of the chapters in this part of the book, I’m going to leave them alone for now so I can get part 2 done. Then I’ll be going back to part 1 to do some updates. This also allows the early adopters time to read the first chapters and send me messages about typos or bugs.

For those of you who didn’t get in on the Kickstarter for the book, the first 10 chapters are as follows:

  • Chapter 1 – The argparse module
  • Chapter 2 – The collections module
  • Chapter 3 – The contextlib module (Context Managers)
  • Chapter 4 – The functools module (Function overloading, caching, etc)
  • Chapter 5 – All about imports
  • Chapter 6 – The importlib module
  • Chapter 7 – Iterators and Generators
  • Chapter 8 – The itertools module
  • Chapter 9 – The re module (An Intro to Regex in Python)
  • Chapter 10 – The typing module (Type Hinting)

There are currently 71 pages in the book so far in my Gumroad edition and over 80 pages in the Leanpub version. Leanpub is generated differently which means they use different fonts and font sizes, which is why that version has more pages.  Regardless, the book is coming along well and is still on track for a September, 2016 release!

Python 201: An Intro to Iterators and Generators

You have probably been using iterators and generators since you started programming in Python but you may not have realized it. In this article, we will learn what an iterator and a generator are. We will also be learning how they are created so we can create our own should we need to.

Iterators

An iterator is an object that will allow you to iterate over a container. The iterator in Python is implemented via two distinct methods: __iter__ and __next__. The __iter__ method is required for your container to provide iteration support. It will return the iterator object itself. But if you want to create an iterator object, then you will need to define __next__ as well, which will return the next item in the container.

Note: In Python 2, the naming convention was slightly different. You still needed __iter__, but __next__ was called next.

To make things extra clear, let’s go over a couple of definitions:

  • iterable – an object that has the __iter__ method defined
  • iterator – an object that has both __iter__ and __next__ defined where __iter__ will return the iterator object and __next__ will return the next element in the iteration.

Continue reading Python 201: An Intro to Iterators and Generators

PyDev of the Week: Mark Lutz

This week we have the honor of welcoming Mark Lutz as our PyDev of the Week. Mark is the author of the first Python book ever, Programming Python. He has also authored Learning Python and the Python Pocket Reference, all three of which were with O’Reilly publishing. Rather than rehash more of his background, let’s just jump into the interview so we can learn more about Mark Lutz!

 

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

I’m a software engineer, a Python book author and trainer, and one of the people who helped Python rise to the prominence it enjoys today.

I’ve been working in the software field for 3 decades, and earned BS and MS degrees in computer science from the University of Wisconsin. Back when I was being paid to do development, I worked on compilers for supercomputers and CAD systems, and large-scale applications at assorted start-ups.

Since quitting my “day job” two decades ago, I’ve been teaching Python classes in North America and Europe, and writing Python books that have sold over half a million units and span 11k published pages. To put that in perspective, back-of-the-envelope calculations suggest that the paper units of my books sold comprise about 650 tons (and counting).

If you’re interested in more details, check out my formal bio page at http://learning-python.com/books/formalbio.html.

 

Continue reading PyDev of the Week: Mark Lutz

PyDev of the Week: Nicole Harris

This week we welcome Nicole Harris (@nlhkabu) as our PyDev of the Week! Nicole is the lead designer of Warehouse, the replacement for the Python Packaging Index (PyPI). You can see a demo version of the site here. She is also working with O’Reilly publishing on a Django screencast, which you’ll hear more about below. Nicole also has a fun blog that is well worth checking out. Let’s take a few moments to get to know her better!

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

I’ve always been a creative person. I studied film and photography at university and got into web design (which eventually led me to Python) because I wanted to create an online portfolio of my animation works. I never launched that portfolio – but I did fall in love with the web.

In my spare time, I like to cook and sew. I’m also learning to speak French.

Continue reading PyDev of the Week: Nicole Harris