Home » Python (page 5)

Python

Python Logging Example

Logging is a process through which an application pushes strings to a handler. That string should be a line containing information about the piece of code from which it was sent and the context in that moment. It is a feature every application must have, and it’s as important as any other functionality you add to the application in question. ...

Read More »

How To Deadlock Your Python With getaddrinfo

What happens if you run this code? import os import socket import threading def lookup(): socket.getaddrinfo('python.org', 80) t = threading.Thread(target=lookup) t.start() if os.fork(): # Parent waits for child. os.wait() else: # Child hangs here. socket.getaddrinfo('mongodb.org', 80) On Linux, it completes in milliseconds. On Mac, it usually hangs. Why? Journey To The Center Of The Interpreter Anna Herlihy and I tackled ...

Read More »

Python Threading / Concurrency Example

Threads are processes which run in parallel to other threads. In a utopian scenario, if you split a big process in 2 threads, these threads will run in parallel so it would take half the time. This is not true in most cases. Using CPython, there is a mutex that prevents multiple native threads from executing Python byte codes at ...

Read More »

Python: Squashing ‘duplicate’ pairs together

As part of a data cleaning pipeline I had pairs of ids of duplicate addresses that I wanted to group together. I couldn’t work out how to solve the problem immediately so I simplified the problem into pairs of letters i.e. A B (A is the same as B) B C (B is the same as C) C D ... ...

Read More »

Python Decorator Tutorial

Sometimes, we encounter problems that require us to extend the behavior of a function, but we don’t want to change its implementation. Some of those problems could be: logging spent time, caching, validating parameters, etc. All these solutions are often needed in more than one function: you often need to log the spent time of every http connection; you often ...

Read More »

Python CSV Reader / Writer Example

CSV (comma-separated values) is a standard for exporting and importing data across multiple formats, such as MySQL and excel. It stores numbers and text in plain text. Each row of the file is a data record and every record consists of one or more fields which values are separated by commas. The use of the comma to separate every record’s ...

Read More »

Python: Parsing a JSON HTTP chunking stream

I’ve been playing around with meetup.com’s API again and this time wanted to consume the chunked HTTP RSVP stream and filter RSVPs for events I’m interested in. I use Python for most of my hacking these days and if HTTP requests are required the requests library is my first port of call. I started out with the following script import ...

Read More »

Black Pipe Testing A Connected Application In C

This is the fourth article in my series on “black pipe” testing. Here I describe testing libmongoc (the MongoDB C Driver) as a black pipe. Like any network client library, libmongoc cannot be fully tested as a black box. Traditional black box tests enter some input and check the output—this only validates one side of the system at a time. ...

Read More »

Test MongoDB Failure Scenarios With MockupDB

This is the fifth article in my series on “black pipe” testing. Traditional black box tests work well if your application takes inputs and returns output through one interface: the API. But connected applications have two interfaces: both the API and the messages they send and receive on the network. I call the validation of both ends a black pipe ...

Read More »

Want to take your WEB dev skills to the next level?

Grab our programming books for FREE!

Here are some of the eBooks you will get:

  • PHP Programming Cookbook
  • jQuery Programming Cookbook
  • Bootstrap Programming Cookbook
  • Building WEB Apps with Node.js
  • CSS Programming Cookbook
  • HTML5 Programming Cookbook
  • AngularJS Programming Cookbook