Archive
Project Euler
“Project Euler is a series of challenging mathematical/computer programming problems that will require more than just mathematical insights to solve. Although mathematics will help you arrive at elegant and efficient methods, the use of a computer and programming skills will be required to solve most problems.”
I heard about it some time ago but I looked at it just two days ago. It’s fun! :) If you learn (know) Python and you like to solve mathematical problems with your computer then try Project Euler.
Maybe I will start posting here my solutions.
Articles
How I Failed, Failed, and Finally Succeeded at Learning How to Code
Update (20110909)
In three weeks I could gather 71 points. Now I’m ranked 71st in my country :)

Redirect stdout to a file
Problem
Your script produces some output to the standard output and you want to redirect it to a file. For instance, you want to suppress the output to /dev/null. However, you want to do this redirection from your script and not in the shell.
Solution
Add the following lines to the beginning of your script (before the first print call):
old_stdout = sys.stdout sys.stdout = open(os.devnull, 'w')
Of course, instead of os.devnull you can specify a normal text file too. To restore printing to stdout, point sys.stdout to old_stdout.
Related
In this thread you will see a solution for redirecting the stdout to a string.
the bpython interpreter
“bpython is a fancy interface to the Python interpreter for Unix-like operating systems (I hear it works fine on OS X). It is released under the MIT License. It has the following features:
- In-line syntax highlighting.
- Readline-like autocomplete with suggestions displayed as you type.
- Expected parameter list for any Python function.
- “Rewind” function to pop the last line of code from memory and re-evaluate.
- Send the code you’ve entered off to a pastebin.
- Save the code you’ve entered to a file.
- Auto-indentation.
- Python 3 support.
For more, see our about-page or just skip right to the screenshots.”
Installation
sudo apt-get install bpython
Tip: add the following line to your ~/.bashrc file:
alias bpy='bpython'
Then you can start the bpython interpreter with “p“. Short and simple :)
Alternatives
- classic “python” interpreter
- ipython
Video tutorials at marakana.com
“Marakana‘s raison d’être is to help people get better at what they do professionally. We accomplish this by organizing software training courses (both public and private) as well as publishing learning resources, sharing knowledge from industry leaders, providing a place to share useful tidbits and supporting the community. Our focus is open source software.”
Their Python videos are here.
Prettify a JSON file
Problem
You want to make a JSON file (string) human-readable.
Solution
curl -s http://www.reddit.com/r/nsfw/.json | python -mjson.tool
For some more alternatives please refer to this post.
Update (20121021)
Another way is to use jq, a powerful command-line tool for querying JSON files.

You must be logged in to post a comment.