Python sleep – How to make a time delay in Python

Just yesterday I implemented Python’s sleep function in order for my code to work. I needed to make an HTTP request to Facebook’s API. They immediately returned a response but continued processing in the background. Their processing took a few extra milliseconds, and I needed them to finish before I continued. A 1-second delay was the difference between the code working perfectly and not working at all. Here’s how it’s done!

Tools:

  • 1 ea python

1Import the time module

We will first want to import Python’s time module: import time This module ships with Python so it should be available on any system. Although it is not necessary for this guide, I highly recommend getting familiar with the time module. Read more about the time module here.

2Use time.sleep to set the delay

You can set a delay in your Python script by passing the number of seconds you want to delay to the sleep function: time.sleep(5) This means you want the script to delay 5 seconds before continuing. The sleep function also accepts floats if you want to give a more precise number: time.sleep(1.75) This will sleep for 1 second and 750 milliseconds. You can also use a float to delay for less than a second like this: time.sleep(0.5) This will sleep for half of a second. You can and should read more about Python’s sleep function here.


Now learn about:


Discuss this guide

var disqus_shortname = ‘howchootest’; (function() { var dsq = document.createElement(‘script’); dsq.type = ‘text/javascript’; dsq.async = true; dsq.src = ‘//’ + disqus_shortname + ‘.disqus.com/embed.js’; (document.getElementsByTagName(‘head’)[0] || document.getElementsByTagName(‘body’)[0]).appendChild(dsq); })();

Tools:

  • 1 ea python

Understand Python loops

There are two kinds of loops in Python. In this guide we’ll cover both the “for” loop and the “while” loop. We’ll also cover how to control these loops by continuing and breaking out.

1For loop

The for loop in Python is used to iterate through sequence data types. Some example of sequence data types in Python are string, list, tuple, xrange, etc. Supposing you have a list: my_list = [2, 3, 5, 7, 11] To iterate through this list using a for loop you can do the following: for item in my_list: print item The for loop always looks like this. item is a variable name that we choose. As we iterate through the list each element will be assigned to the item variable within the block of code. For loops are used when we want to look at each element in some sequence. So the block of code inside the for loop is the code that will be executed once for each element in the sequence.

2While loop

A while loop is used to repeatedly execute a statement as long as the condition is true. Unlike for loops, while is not connected with any sequence. For loops iterate for as many elements as are in the sequence, but while loops iterate as long as the condition is True. Here is a simple example to print all even numbers less than 100. i = 0 while i < 100: if i % 2 == 0: print i i = i + 1 In this example, we are setting a counter i. Then we set up a while loop to execute the following statement as long as the condition i <= 100 is True. The statement simply tests to see if the number we are testing is even by using the modulo operator. In this case, we need to manually increment to counter which is why we finish with i = i + 1. So each time we loop through, the value of i will be increased by 1.

3Continuing in a loop

Sometimes you’ll want to continue to the next iteration without executing the following code. Continue can be used the same way in both for loops and while loops. We could rewrite the previous example using continue. for item in xrange(100): if not item % 2 == 0: continue print item This does the same thing except we are using a for loop and xrange. Xrange gives us a range of numbers between 0 and 100. And instead of only printing item if it is even, we continue to the next iteration if it is not even. The continue statement means we won’t execute the code following it, and instead move on to the next iteration through the loop.

4Breaking out of a loop

In some cases, we will want to stop executing code altogether if a condition is met within a loop. For example, if we want to test a list to see if it contains any odd numbers we can do this: test_list = [2, 4, 6, 8, 9, 10] contains_odd = False for item in test_list: if not item % 2 == 0: contains_odd = True break In this case, we are setting up a test list called test_list. This contains some arbitrary values. Then we are setting a flag contains_odd. We will start by assuming that the list contains no odd numbers, then iterate through the list and test for odds. If we find an odd number we set contains_odd to True and completely break out of the code. Due to the nature of our problem, if we find an odd value in the list there is no reason to keep testing.


Now learn about:


Discuss this guide

var disqus_shortname = ‘howchootest’; (function() { var dsq = document.createElement(‘script’); dsq.type = ‘text/javascript’; dsq.async = true; dsq.src = ‘//’ + disqus_shortname + ‘.disqus.com/embed.js’; (document.getElementsByTagName(‘head’)[0] || document.getElementsByTagName(‘body’)[0]).appendChild(dsq); })();

How to change the HipChat notification sound

This guide shows you how to change the HipChat notification sound in both Mac and Windows. Presently, the notification sound cannot be changed through either native application. This can be annoying when you have an office full of people using the same sound.

1Find a new notification sound

Find an audio file that you’d like to use instead and rename it as notify.wav. Note: the filetype must be .wav. There are tons of free converters you can use to accomplish this. Alternatively, Lifehacker has a pretty sweet set of notification .wavs that you can download for free here.

2OS X: Navigate to the app

Quit HipChat. Navigate to Applications > HipChat.app. Right-click HipChat.app and select “Show Package Contents”.

OS X: Navigate to the app

3OS X: Find and replace notify.wav

You will now be browsing the contents of HipChat.app. Navigate to Contents > Resources > notify.wav. After you’ve found your new audio file and renamed it to notify.wav, copy it into that directory, replacing the existing one. Reopen HipChat and you’re good to go!

OS X: Find and replace notify.wav

4Windows: Find and replace notify.wav

Quit hipchat. Navigate to the following path:
C:Program Files (x86)AtlassianHipChatnotify.wavReplace notify.wav with your new notify.wav sound file. Reopen HipChat.

5A note on upgrades

There is an obvious downside to this approach: each time you update the Hipchat app, your new sound file will be overwritten. Therefore, it’s recommended you back up your notify.wav file somewhere else (and bookmark this guide) so that you can repeat this process if need be.


Now learn about:


Discuss this guide

var disqus_shortname = ‘howchootest’; (function() { var dsq = document.createElement(‘script’); dsq.type = ‘text/javascript’; dsq.async = true; dsq.src = ‘//’ + disqus_shortname + ‘.disqus.com/embed.js’; (document.getElementsByTagName(‘head’)[0] || document.getElementsByTagName(‘body’)[0]).appendChild(dsq); })();

How to install iPython

If you’re a Python programmer, you should be using iPython. Use the steps below to install.

1Install iPython

You can install easily with pip: pip install ipython or easy_install: easy_install ipython Or if you’re having trouble, you can visit the more detailed installation guide.


Now learn about:


Discuss this guide

var disqus_shortname = ‘howchootest’; (function() { var dsq = document.createElement(‘script’); dsq.type = ‘text/javascript’; dsq.async = true; dsq.src = ‘//’ + disqus_shortname + ‘.disqus.com/embed.js’; (document.getElementsByTagName(‘head’)[0] || document.getElementsByTagName(‘body’)[0]).appendChild(dsq); })();

Check ATT upgrade eligibility via text

I’ve been eying a coworker’s Galaxy Note 3 as a replacement for my aging iPhone 4s. However, I couldn’t justify spending $700 so I wanted to check my AT&T; upgrade eligibility. I found out you can do this without logging into your AT&T; account or talking to an account rep.

1Dial *639# and press the Call button

A confirmation message will appear.

Dial *639# and press the Call button

2Check your text messages

After a few seconds, ATT will send you a text containing your upgrade eligibility.

Check your text messages

Note:

I guess I’ll be waiting a while to upgrade! 😉


Now learn about:


Discuss this guide

var disqus_shortname = ‘howchootest’; (function() { var dsq = document.createElement(‘script’); dsq.type = ‘text/javascript’; dsq.async = true; dsq.src = ‘//’ + disqus_shortname + ‘.disqus.com/embed.js’; (document.getElementsByTagName(‘head’)[0] || document.getElementsByTagName(‘body’)[0]).appendChild(dsq); })();

Add a text shadow using CSS3

Text shadows are a beautiful and subtle effect for your web site. CSS3 text shadows are currently support on all versions of Chrome, Safari, Firefox 3.5+, and IE 10+.

Tools:

  • 1 ea browser
  • 1 ea text editor

1text-shadow: 0px 1px 2px #eee;

To apply text-shadow to an HTML element (ie h1) use the standard css syntax: h1 { text-shadow: 0px 1px 2px #eee; } The css property is called text-shadow and from left to right the values are as follows: – 0px represents the shadow offset to the right. – 1px represents the shadow offset below. – 2px represents the spread or blur distance. – Finally, the #eee is the color of the shadow. The offsets can all be negative values as well. So using “-1px” for the first value would move the shadow 1px to the left.


Now learn about:


Discuss this guide

var disqus_shortname = ‘howchootest’; (function() { var dsq = document.createElement(‘script’); dsq.type = ‘text/javascript’; dsq.async = true; dsq.src = ‘//’ + disqus_shortname + ‘.disqus.com/embed.js’; (document.getElementsByTagName(‘head’)[0] || document.getElementsByTagName(‘body’)[0]).appendChild(dsq); })();

Tools:

  • 1 ea browser
  • 1 ea text editor