Archive

Author Archive

[flask] validate a URL

August 23, 2015 Leave a comment

Problem
In a Flask application I wanted to verify if a user-given URL is valid.

Solution
I found a simple validator package for that called validators (see it on GitHub).

Sample usage:

$ pip install validators
$ python
>>> import validators
>>> url = "http://index.hu"
>>> validators.url(url)
True
>>> url = "http://index.h/"
>>> validators.url(url)
ValidationFailure(func=url, args={'value': 'http://index.h/', 'require_tld': True})

The ValidationFailure class implements the __bool__ method, so you can easily check if validation failed:

if not validators.url(url):
    flash("Error: you must provide a valid URL!")
Categories: flask, python Tags: ,

[flask] show render time on a page

August 22, 2015 Leave a comment

Problem
For debugging purposes, I wanted to see the rendering time of each page.

Solution
I found a solution here. Here are the links:

Note that Flask-DebugToolbar is very useful but it really slowed down my application. I even switched it off and I only use it when I want to debug something. When the issue is solved I switch it off again.

working with zip files

August 21, 2015 Leave a comment

Problem

In a project of mine I had to deal with folders, where a folder can contain several thousands of small text files. I kept this project on Dropbox, so I could use it on all my machines. However, Dropbox is quite slow when trying to synchronize several thousand files. So I decided to put files in a folder into a zip file.

So the question is: how to deal with zip files? How to do basic operations with them: create zip, delete from zip, list zip, add to zip, move to zip, extract from zip, etc.

Solution

In this project of mine I used the external zip command as well as the zipfile package from the stdlib. Let’s see both of them.

Manipulating zip files from the command-line
Let’s see some examples. Compress every .json file in the current directory except the desc.json file:

zip -9 files.zip *.json -x desc.json

The switch “-9” gives the best compression, files.zip is the output, and “-x” is short for “--exclude“. From Python you can call it as an external command with os.system() for instance.

The previous example creates a zip file and leaves the original files. Now let’s move files into a zip file (and delete the original files when they were added successfully to the archive):

zip -9 -m files.zip *.json -x desc.json

Delete a file from an archive:

zip -d files.zip desc.json

It will delete desc.json from the zip file.

List the content of a zip file:

zipinfo files.zip

Add a file to the archive:

zip -g files.zip new.json

Where “-g” means: grow.

Extract just one file from a zip file:

unzip files.zip this.json

It will extract this.json from the archive.

Read the content of a zip file in Python
OK, say we have a zip file that contains some files. How to get the filenames? How to read them? I found some nice examples here.

List the file names in a zip file:

import zipfile

zfile = zipfile.ZipFile("files.zip", "r")

for name in zfile.namelist():
    print(name)

Read files in a zip file:

import zipfile

zfile = zipfile.ZipFile("files.zip", "r")

for name in zfile.namelist():
    data = zfile.read(name)
    print(data)

Links

Categories: bash, python Tags: ,

trending Python repositories on GitHub

August 9, 2015 Leave a comment

https://github.com/trending?l=python

Find what repositories the GitHub community is most excited about today.

Categories: python Tags: ,

Reading (writing) unicode text from (to) files

August 6, 2015 Leave a comment

Problem
You want to write some special characters to a file (e.g. f.write("voilá")) but you get immediately some unicode error in your face.

Solution
Instead of messing with the encode, decode methods, use the codecs module.

import codecs

# read
with codecs.open(fname, "r", "utf-8") as f:
    text = f.read()

# write
with codecs.open(tmp, "w", "utf-8") as to:
    to.write(text)

As can be seen, its usage is very similar to the well-known open function.

This tip is from here.

Categories: python Tags: , ,

screenshot.py

July 26, 2015 Leave a comment

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

Flask: linkify a text

July 12, 2015 Leave a comment

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.

Follow

Get every new post delivered to your Inbox.

Join 97 other followers