Archive

Archive for August, 2011

Project Euler

August 19, 2011 Leave a comment

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 :)

Categories: project euler, python Tags: ,

Redirect stdout to a file

August 17, 2011 Leave a comment

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

August 14, 2011 Leave a comment

“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

August 13, 2011 Leave a comment

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

August 10, 2011 Leave a comment

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.

Categories: python Tags: , , ,
Follow

Get every new post delivered to your Inbox.

Join 63 other followers