PyScripter: a very nice small IDE for Windows

April 8, 2014 Leave a comment

I found a very nice small Python IDE for Windows called PyScripter.

PyScripter is a free and open-source Python Integrated Development Environment (IDE) created with the ambition to become competitive in functionality with commercial Windows-based IDEs available for other languages. Being built in a compiled language is rather snappier than some of the other Python IDEs and provides an extensive blend of features that make it a productive Python development environment.” (source)

The install file is less than 5 MB and PyScripter provides all the features you want from a modern IDE: syntax highlight, run code and see the output, code completion, built-in Python shell, debugger, etc.

Here is a youtube video for beginners that shows first how to install Python on Windows, then presents PyScripter.

Categories: python, windows Tags: , ,

imgur album downloader

April 5, 2014 Leave a comment

This little script can download all the images in an imgur gallery.

Categories: python Tags: , ,

Improving the sidebar of /r/python

March 21, 2014 Leave a comment

In the sidebar of /r/python, there was a very unpythonic infinite loop for a long time:

while 1:
    # do something

Today I sent a message to the moderators and they changed it:

while

Update: I got a message from them.

Categories: python Tags: , , ,

get the command pip3

March 15, 2014 Leave a comment

Problem
You have Python 2 and Python 3 on the same machine. You want to install a package that requires Python 3. You cannot use the command “pip” because it will install the package as if it were written in Python 2. You want a “pip3” command.

Solution
I found the solution here.

Steps:

$ curl -O https://raw.github.com/pypa/pip/master/contrib/get-pip.py
$ sudo python3 get-pip.py

Now you have a “pip3” command that you can use to install Python 3 libraries.

Categories: python Tags: ,

installing pgmagick

March 4, 2014 Leave a comment

pgmagick is yet another boost.python based wrapper for GraphicsMagick.

GraphicsMagick is the swiss army knife of image processing. …it provides a robust and efficient collection of tools and libraries which support reading, writing, and manipulating an image in over 88 major formats including important formats like DPX, GIF, JPEG, JPEG-2000, PNG, PDF, PNM, and TIFF.” (source)

Here I found an interesting blog post on how to remove image backgrounds with a Python script (comments on it here). The script uses the pgmagick library.

Problem
How to install pgmagick and GraphicsMagick? That is, the following line shouldn’t drop any error :)

>>> import pgmagick
>>>

Solution
It may not be an optimal solution because it installed on my machine LOTS OF extra packages… However, it worked for me.

$ sudo add-apt-repository ppa:dhor/myway
$ sudo apt-get update
$ sudo apt-get install graphicsmagick

$ sudo apt-get install libmagick++-dev
$ sudo apt-get install libboost-python-dev
$ sudo pip install pgmagick -U

Usage example
scale example (copied from here):

>>> from pgmagick import Image, FilterTypes
>>> im = Image('input.jpg')
>>> im.quality(100)
>>> im.filterType(FilterTypes.SincFilter)
>>> im.scale('100x100')
>>> im.sharpen(1.0)
>>> im.write('output.jpg')

Links

Categories: python, ubuntu Tags: ,

Jinja2 example for generating a local file using a template

February 25, 2014 Leave a comment

Here I want to show you how to generate an HTML file (a local file) using a template with the Jinja2 template engine.

Python source (proba.py)

#!/usr/bin/env python

import os
from jinja2 import Environment, FileSystemLoader

PATH = os.path.dirname(os.path.abspath(__file__))
TEMPLATE_ENVIRONMENT = Environment(
    autoescape=False,
    loader=FileSystemLoader(os.path.join(PATH, 'templates')),
    trim_blocks=False)


def render_template(template_filename, context):
    return TEMPLATE_ENVIRONMENT.get_template(template_filename).render(context)


def create_index_html():
    fname = "output.html"
    urls = ['http://example.com/1', 'http://example.com/2', 'http://example.com/3']
    context = {
        'urls': urls
    }
    #
    with open(fname, 'w') as f:
        html = render_template('index.html', context)
        f.write(html)


def main():
    create_index_html()

########################################

if __name__ == "__main__":
    main()

Jinja2 template (templates/index.html)

<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8"/>
    <title>Proba</title>
</head>
<body>
<center>
    <h1>Proba</h1>
    <p>{{ urls|length }} links</p>
</center>
<ol align="left">
{% set counter = 0 -%}
{% for url in urls -%}
<li><a href="{{ url }}">{{ url }}</a></li>
{% set counter = counter + 1 -%}
{% endfor -%}
</ol>
</body>
</html>

Resulting output
If you execute proba.py, you will get this output:

<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8"/>
    <title>Proba</title>
</head>
<body>
<center>
    <h1>Proba</h1>
    <p>3 links</p>
</center>
<ol align="left">
<li><a href="http://example.com/1">http://example.com/1</a></li>
<li><a href="http://example.com/2">http://example.com/2</a></li>
<li><a href="http://example.com/3">http://example.com/3</a></li>
</ol>
</body>
</html>

You can find all these files here (GitHub link).

Categories: python Tags: , ,
Follow

Get every new post delivered to your Inbox.

Join 63 other followers