GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Home
  • About
  • Learn Python
  • Archives
  • Contact
  • Google
  • Youdao
  • Xian Guo
  • Zhua Xia
  • My Yahoo!
  • newsgator
  • Bloglines
  • iNezha
  • Get the value of PI without a constant

This view

  • Original page
  • New address

GeezerVison is showing pythonadventures.wordpress.com — java – Python Adventures

Python Adventures

Welcome to the Jungle!
  • Home
  • About
  • Learn Python
  • Archives
  • Contact

Archive

Posts Tagged ‘java’

Python equivalent of Java .jar files

January 5, 2014 Jabba Laci 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: .egg, .jar, distribution, java, zip

Python vs Java in relation to logos

September 9, 2013 Jabba Laci 1 comment

Emanuel Couto explains the difference between Python and Java, comparing the logos of the two languages:

“I read almost all comments and I there is one point that all of you missed! The logo is important for productivity!

The Python logo is obviously a snake. The name of the language, however, was based on Monty Python. So what this means is that you are going to have good laughs when the python bytes you. Specially when the code is too long to debug or understand.

The Java logo is a cup of coffee. What this means is that you are going to need coffee when programming in Java or you will fall asleep. In fact you are going to need loads of coffee when your code is near production stage. BTW I don’t drink coffee, so my job is twice as hard.

Jython is when you give coffee to a snake. Jython users are people that drank toomuch coffee and over time it doesn’t have any effect anymore. Since the python needs to sleep, the solution is giving coffee so that Java programmers can also have laughs at late hours of the day.

And that folks is a true story.” (source)

Categories: fun, python Tags: java, logo

Print colored text in terminal

March 16, 2011 Jabba Laci 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: color, java, termcolor, terminal

StringBuilder functionality in Python

September 27, 2010 Jabba Laci 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()
Categories: python Tags: concatenate, java, string, stringbuffer, stringbuilder, StringIO
RSS feed
  • Google
  • Youdao
  • Xian Guo
  • Zhua Xia
  • My Yahoo!
  • newsgator
  • Bloglines
  • iNezha

Blog Stats

  • 1,659,944 hits

Random Post

Click here for a random post

Recent Posts

  • Get the value of PI without a constant
  • In a graph, find the largest complete subgraph
  • How to solve a two-variable equation with Sympy
  • Autoformat your Python code with Ruff
  • Play a sound file from memory

ascii bash beautifulsoup book bpython challenge command-line convert cron crontab date datetime distance dropbox email eric exercise extract file firefox github google hacking heroku html ide imdb imgur install ipython java jinja2 json linux module modules MongoDB movies news pdf perl pi pip print pylint pypi python3 python shell quote readline reddit redirect refactoring regexp requests reverse review shell sound stdout string tab text threads time tip tutorial unicode url video virtualenv virtual environment virtualenvwrapper xml zip

Archives

  • December 2025 (1)
  • December 2024 (2)
  • March 2024 (1)
  • February 2024 (3)
  • June 2023 (1)
  • December 2022 (1)
  • May 2022 (1)
  • March 2022 (1)
  • February 2022 (2)
  • October 2020 (3)
  • September 2020 (2)
  • July 2020 (1)
  • December 2019 (1)
  • November 2019 (7)
  • June 2019 (2)
  • May 2019 (1)
  • February 2019 (1)
  • January 2019 (1)
  • August 2018 (1)
  • July 2018 (4)
  • June 2018 (5)
  • May 2018 (1)
  • March 2018 (2)
  • February 2018 (1)
  • January 2018 (2)
  • December 2017 (2)
  • October 2017 (1)
  • August 2017 (1)
  • May 2017 (2)
  • April 2017 (1)
  • March 2017 (1)
  • February 2017 (3)
  • January 2017 (5)
  • December 2016 (3)
  • October 2016 (2)
  • September 2016 (1)
  • August 2016 (4)
  • July 2016 (3)
  • June 2016 (2)
  • May 2016 (2)
  • April 2016 (1)
  • March 2016 (4)
  • February 2016 (1)
  • January 2016 (4)
  • December 2015 (5)
  • November 2015 (8)
  • October 2015 (2)
  • September 2015 (3)
  • August 2015 (6)
  • July 2015 (4)
  • June 2015 (1)
  • May 2015 (3)
  • April 2015 (2)
  • February 2015 (1)
  • January 2015 (6)
  • December 2014 (5)
  • November 2014 (2)
  • October 2014 (1)
  • September 2014 (1)
  • August 2014 (14)
  • July 2014 (3)
  • June 2014 (6)
  • May 2014 (3)
  • April 2014 (2)
  • March 2014 (4)
  • February 2014 (3)
  • January 2014 (19)
  • December 2013 (8)
  • November 2013 (9)
  • October 2013 (4)
  • September 2013 (10)
  • August 2013 (16)
  • July 2013 (4)
  • June 2013 (7)
  • May 2013 (7)
  • April 2013 (3)
  • March 2013 (12)
  • February 2013 (2)
  • January 2013 (10)
  • December 2012 (18)
  • November 2012 (3)
  • October 2012 (4)
  • September 2012 (5)
  • August 2012 (1)
  • July 2012 (1)
  • May 2012 (8)
  • April 2012 (9)
  • March 2012 (17)
  • February 2012 (3)
  • January 2012 (8)
  • November 2011 (11)
  • October 2011 (7)
  • September 2011 (17)
  • August 2011 (5)
  • June 2011 (1)
  • May 2011 (7)
  • April 2011 (21)
  • March 2011 (21)
  • February 2011 (7)
  • December 2010 (4)
  • November 2010 (1)
  • October 2010 (16)
  • September 2010 (15)

Meta

  • Create account
  • Log in
  • Entries feed
  • Comments feed
  • WordPress.com
Privacy & Cookies: This site uses cookies. By continuing to use this website, you agree to their use.
To find out more, including how to control cookies, see here: Cookie Policy
  • Subscribe Subscribed
  • Python Adventures
    View site in Reader
    Manage subscriptionsSign upLog in
    Report this content
    Collapse this bar
Create your own website, free on WordPress.com.Create your own website.
Start free
You are visitor 01402503 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.