Sunday, September 22, 2013

Jython - funny and simple scripts - first steps .

I play today with jython and can be fun but seams to be to slow in a linux os.

Jython is invoked using the "jython" script and it's an implementation of Python for the JVM.

Install the package jython in your linux distro and you can start to deal with java and python.

When you use jython then script will start with :

#!/usr/bin/env jython

I make also some very simple scripts...

First script make one button and give a action to exit.

#!/usr/bin/env jython
from javax import *
import java
from java import *
import sys

frame = awt.Frame(size=(500,100))
frame.background = 255,255,0
def exit(event):

my_button = awt.Button("Exit!", actionPerformed=exit)

The output is:

The script is easy to make ... it's like gtk with add, pack and action ...

Let's see the next script : one list.

from javax import *
from java import awt
import sys
python_list.append('text 1')
python_list.append('text 2')
python_list.append('text 3')
python_list.append('text 4')
python_list.append('text 5')

frame=awt.Frame("test list")

... and this is the gui with the list:

I make a simple list and add to the gui using pack() function.

The jython is not easy is much to learn and if you want then go to this website.

Check system , distro and commands using python scripts .

This is a simple example with two functions.

First will check the linux command : ls linux command.

The next function will give us some infos about system.

import shlex 
import subprocess
from subprocess import Popen, PIPE

import platform

def check_command(command):
    cmd='which ' + command 
    output = Popen(shlex.split(cmd), stdout=PIPE).communicate()[0]
    command_path =output.split('\n')[0]
    print command_path
    return command_path

def check_platform():
    arch, exe = platform.architecture()
    my_system = platform.system()
    if my_system == 'Linux':
        distro_name, distro_version, distro_id = platform.linux_distribution()
    elif my_system == 'Darwin':
        distro_name, distro_version, distro_id = platform.mac_ver()
    elif my_system == 'Windows':
 distro_name, distro_version, distro_id = platform.win32_ver()
    elif my_system == 'Java':
 distro_name, distro_version, distro_id = platform.java_ver()
    processor = platform.processor() or 'i386'
    print processor, my_system, arch, distro_name, distro_version, distro_id
    return processor, my_system, arch, distro_name, distro_version, distro_id



This python script can be use with any scripts when we need to test commands and system , distro version.