Archive
screenshot.py
I made a simple wrapper script called screenshot.py that calls phantomjs and convert to do the real job. The advantage of screenshot.py is its very simple usage.
Usage
screenshot.py -full http://reddit.com full.jpg
Screenshot of the entire page (can be very high).
screenshot.py -window http://reddit.com window.jpg
Screenshot of the area that you see in the browser.
screenshot.py -thumb http://reddit.com thumb.jpg
Thumbnail of the area that you see in the browser.
Links
- sample screenshots
- project’s page: https://github.com/jabbalaci/screenshot.py
- this project of mine appeared in ImportPython Weekly Newsletter – Issue No 39
Flask: linkify a text
Problem
I have a text that I present in a Flask application. The text can contain URLs, and I would like to linkify them, i.e. make them clickable links. Example:
before:
visit http://google.com for...
after:
visit <a href="http://google.com">http://google.com</a> for...
Solution
Before rolling out an own solution, it’s a good idea to check if there is a package for this problem. There is :), and it’s called bleach. Its usage couldn’t be simpler:
>>> import bleach
>>> bleach.linkify('an http://example.com url')
u'an <a href="http://example.com" rel="nofollow">http://example.com</a> url
Flask integration
In your main file (that calls app.run()) add the following filter:
import bleach
@app.template_filter('linkify')
def linkify(s):
return bleach.linkify(s)
Then use it in your jinja2 templates:
Description: {{ entry.description|linkify|safe }}
Warning! Apply the “safe” filter only if you trust the origin of the text you want to present in a linkified format.
subtract a day from a python date
Problem
When working with dates, I prefer the order yyyy-mm-dd (e.g. 2015-07-10). Its main advantage is that if you use it as a prefix and sort your entries, you get them in chronological order. (Another advantage is that in my home country we use this order, so this is much closer to my thinking).
So, I faced the following problem: having a date as a string (“2015-07-10”), calculate the date one day before and produce a string again (“2015-07-09”).
Solution
>>> from datetime import datetime, timedelta
>>> date = "2015-07-10"
>>> today = datetime.strptime(date, '%Y-%m-%d')
>>> today
datetime.datetime(2015, 7, 10, 0, 0)
>>> yesterday = today - timedelta(days=1)
>>> yesterday
datetime.datetime(2015, 7, 9, 0, 0)
>>> yesterday.strftime('%Y-%m-%d')
'2015-07-09'
Links
- http://stackoverflow.com/questions/466345/converting-string-into-datetime
- http://stackoverflow.com/questions/441147/how-can-i-subtract-a-day-from-a-python-date
Remark
Back to dates: if I need to write a date in English, I write it like this: “May 12, 1984”. Avoid “05-12-1984”, because what is it? May 12? Or December 5? God knows only.
Python course in Milan, Italy
In the last two days I gave an intensive Python course at the University of Milan, Italy. It was good. As I saw the students liked it too :)
Here is the schedule of the course:
Day 1 ===== - introduction - string data type - list data type - loops (for, while) - tuple data type - list comprehension - control structures - functions Day 2 ===== - set - dictionary - global variables - file handling - classes, objects - modules - random numbers - downloading webpages - JSON serialization


You must be logged in to post a comment.