Archive

Posts Tagged ‘java’

Python equivalent of Java .jar files

January 5, 2014 Leave a comment

Problem
In Java, you can distribute your project in JAR format. It is essentially a ZIP file with some metadata. The project can be launched easily:

$ java -jar project.jar

What is its Python equivalent? How to distribute a Python project (with several modules and packages) in a single file?

Solution
The following is based on this post, written by bheklilr. Thanks for the tip.

Let’s see the following project structure:

MyApp/
    MyApp.py          <--- Main script
    alibrary/
        __init__.py
        alibrary.py
        errors.py
    anotherlib/
        __init__.py
        another.py
        errors.py
    configs/
        config.json
        logging.json

Rename the main script to __main__.py and compress the project to a zip file. The extension can be .egg:

myapp.egg/             <--- technically, it's just a zip file
    __main__.py        <--- Renamed from MyApp.py
    alibrary/
        __init__.py
        alibrary.py
        errors.py
    anotherlib/
        __init__.py
        another.py
        errors.py
    configs/
        config.json
        logging.json

How to zip it? Enter the project directory (MyApp/) and use this command:

zip -r ../myapp.egg .

Now you can launch the .egg file just like you launch a Java .jar file:

$ python myapp.egg

You can also use command-line arguments that are passed to __main__.py.

Categories: python Tags: , , , ,

Print colored text in terminal

March 16, 2011 3 comments

Problem

In the terminal, you want to print some texts in colored mode. For instance, warnings in red.

Solution

There is a module for this task called termcolor. You can install it with this command:

sudo pip install termcolor

Or, simply download it and put termcolor.py next to your script.

Usage

from termcolor import colored

print colored('hello', 'red'), colored('world', 'green')

The tip and the example are from this thread.

Update (20110518)

If you want to print something in red (warning/error) or green (OK), here is a simplified solution:

colorred = "\033[01;31m{0}\033[00m"
colorgrn = "\033[1;36m{0}\033[00m"

print colorred.format("Warning! Reactor meltdown. Evacuate immediately!")
print colorgrn.format("Ha-ha, just kidding!")

Same thing in Java:

String colorred = "\033[01;31m%s\033[00m\n";
System.out.printf(colorred, "# Warning! Approaching light speed. Fasten your seatbelts.");
Categories: python Tags: , , ,

StringBuilder functionality in Python

September 27, 2010 Leave a comment

Problem

You need to concatenate lots of string elements. Under Java we use a StringBuilder for this, but how to do that in Python?

Solution #1

Use a list, and join the elements of the list at the end. This is much more efficient than concatenating strings since strings are immutable objects, thus if you concatenate a string with another, the result is a NEW string object (the problem is the same with Java strings).

Example:

def g():
    sb = []
    for i in range(30):
        sb.append("abcdefg"[i%7])

    return ''.join(sb)

print g()   # abcdefgabcdefgabcdefgabcdefgab

Solution #2 (update 20120110)

Use a StringIO object and print to it. In short:

from cStringIO import StringIO

out = StringIO()
print >>out, 'arbitrary text'    # 'out' behaves like a file
return out.getvalue()