A few weeks ago I started to learn Go (aka Golang). I enjoy it very much, I think it’s a very nice and logical language. It is similar to Python in several aspects, so it’s not difficult to start after Python.
If you watch some videos about Go, you will notice that most presentations are made with the same presentation software. And the most interesting part is that you can include Go code snippets in the presentation that you can execute with a button click. The output is also shown. That is, if you want to show some code, you don’t need to jump around between a terminal and the presentation; everything is at one place.
Example: http://www.youtube.com/watch?v=f6kdp27TYZs at 6:57.
You want to do the same with Python code snippets. You want to use this presentation system and you want to embed runnable Python codes in it.
These presentations are done with the Go package called
present. Here I won’t explain how to set up Go on your machine, I leave it as an exercise (link1, link2). Once you have Go installed on your machine, install the present package:
$ go get code.google.com/p/go.talks/present
The good news is that
present is prepared to run scripts (whose first line starts with a shebang). The file that is responsible for executing Go programs and eventually Python scripts is here:
However, I found a bug in socket.go (reported here). The last line of the function
shebang() must be changed (Update 20140227: my patch has been integrated since then, so if you install the
present package, you’ll have the correct version):
return fs, fs[1:]
return fs, fs
After this compile present. Enter the directory
$GOPATH/src/code.google.com/p/go.talks/present and execute the command “
go install“. It will place the binary “
present” in the folder
Try it out
I put a simple demo presentation here: https://github.com/jabbalaci/go-present-for-python . Download the files and launch “present” in the directory where the downloaded files are. Visit http://127.0.0.1:3999/ and check out the demo that contains a Python and a Go code snippet. Don’t forget to click on the “
Run” buttons :)
Update (20140215) — convert presentation to PDF
You can convert your presentation to PDF easily. Navigate to the first slide in Firefox (I prefer this browser) and go to File -> Print…. Select “print to file”, change the page orientation to “landscape” and hide all the headers and footers (blank). Firefox will print every slide, each slide on a new page.
This blog post got included in Go Newsletter Issue #21 (Feb. 2014). Awesome!
Right now I’m at the Write the Docs conference in Budapest, Hungary. Today I gave a lightning talk with the title “Using Go’s present with Python code snippets”. The presentation converted to PDF is here.
I was looking for some info about scraping techniques when I found this presentation: http://www.pycon.it/media/stuff/slides/tecniche-di-scraping-in-python.pdf.
If you visit the main page at http://www.pycon.it/, you can get to the summary of all the presentations since 2007. In some cases slides and videos are also available.
- “PyCon” in the United States
- “EuroPython” in Europe
- “PyCon Asia Pacific” in Singapore
- “PyCon AR” in Argentina
- “PyCon AU” in Australia
- “Python Brasil” in Brazil
- “PyCon FR” in France
- “PyCon India”
- “PyCon Ireland”
- “PyCon Italia” in Italy
- “Kiwi PyCon” in New Zealand
- “PyCon PL” in Poland
- “PyCon UK” in the United Kingdom
- “SciPy (US)”
- “SciPy (India)”
See http://www.pycon.org/ for an up-to-date list.
“Generators are a simple and powerful tool for creating iterators. They are written like regular functions but use the
yield statement whenever they want to return data. Each time
next() is called, the generator resumes where it left-off (it remembers all the data values and which statement was last executed).”
Let’s rewrite our Fibonacci function using generators. In the previous approach, we specified how many Fibonacci numbers we want to get. The function calculated all of them and returned a list containing all the elements. With generators, we can calculate the numbers one by one. The new function will calculate a number, return it, and suspend its execution. When we call it again, it will resume where it left off and it runs until it computes another number, etc.
First let’s see a Fibonacci function that calculates the numbers in an infinite loop:
#!/usr/bin/env python def fib(): a, b = 0, 1 while True: print a # the current number is here a, b = b, a+b fib()
In order to rewrite it in the form of a generator, we need to locate the part where the current value is calculated. This is the line with
print a. We only need to replace this with
yield a. It means that the function will return this value and suspend its execution until called again.
So, with generators it will look like this:
#!/usr/bin/env python def fib(): a, b = 0, 1 while True: yield a a, b = b, a+b f = fib() for i in range(10): # print the first ten Fibonacci numbers print f.next(), # 0 1 1 2 3 5 8 13 21 34
It is also possible to get a slice from the values of a generator. For instance, we want the 5th, 6th, and 7th Fibonacci numbers:
#!/usr/bin/env python from itertools import islice def fib(): a, b = 0, 1 while True: yield a a, b = b, a+b for i in islice(fib(), 5, 8): print i # 5 8 13