Python : How to write a function in python

Due some circumtance , I had opted to use python script..

Well, for python script , you need to be extra careful with the white space
and tab..

this is an example of using python function to ease the work.

it might not be beautiful or simplified enough .. but enough to drive me nut
looking for the white space.

tips : do not combine usage of “tab” and “space” inside one python script… be consistent.
if using tab.. use tab till the end..
if using space.. use space all the time.
else it will drive you nut before you can nail where is the missing space.. 🙂

#!/usr/bin/python
import re
def getBM (eng):
    a = re.compile('No rain')
    tiada_hujan = a.match(eng)
    b = re.compile('rain')
    hujan = b.match(eng)
    c = re.compile('fair')
    cerah = c.match(eng)
    d = re.compile('Weather Fair')
    cerah2 = d.match(eng)
    p1 = re.compile('Weather Fair')
    p2 = re.compile('Clear')
    p3 = re.compile('Hazy')
    p4 = re.compile('Windy')
    p5 = re.compile('No rain')
    p6 = re.compile('Cloudy')
    p7 = re.compile('Cloudy over coastal areas')
    p8 = re.compile('Cloudy over inland areas')
    p9 = re.compile('Rain')
    p10 = re.compile('Intermittent rain occasionally moderate')
    p11 = re.compile('Intermittent rain occasionally heavy')
    p12 = re.compile('Widespread intermittent rain')
    p13 = re.compile('Isolated rain')
    p14 = re.compile('Isolated rain over coastal areas')
    p15 = re.compile('Isolated rain over inland areas')
    p16 = re.compile('Scattered rain')
    p17 = re.compile('Scattered rain over coastal areas')
    p18 = re.compile('Scattered rain over inland areas')
    p19 = re.compile('Showers')
    p20 = re.compile('Isolated showers')
    p21 = re.compile('Isolated showers over coastal areas')
    p22 = re.compile('Isolated showers over inland areas')
    p23 = re.compile('Thunderstorms')
    p24 = re.compile('Isolated thunderstorms')
    p25 = re.compile('Scattered thunderstorms')
    p26 = re.compile('Widespread thunderstorms') 
    mp1 = p1.match(eng)
    mp2 = p2.match(eng)
    mp3 = p3.match(eng)
    mp4 = p4.match(eng)
    mp5 = p5.match(eng)
    mp6 = p6.match(eng)
    mp7 = p7.match(eng)
    mp8 = p8.match(eng)
    mp9 = p9.match(eng)
    mp10 = p10.match(eng)
    mp11 = p11.match(eng)
    mp12 = p12.match(eng)
    mp13 = p13.match(eng)
    mp14 = p14.match(eng)
    mp15 = p15.match(eng)
    mp16 = p16.match(eng)
    mp17 = p17.match(eng)
    mp18 = p18.match(eng)
    mp19 = p19.match(eng)
    mp20 = p20.match(eng)
    mp21 = p21.match(eng)
    mp22 = p22.match(eng)
    mp23 = p23.match(eng)
    mp24 = p24.match(eng)
    mp25 = p25.match(eng)
    mp26 = p26.match(eng)
 
    if tiada_hujan:
        return "Tiada Hujan"
    if hujan:
        return "Hujan"
    if cerah:
        return "Cerah" 
    if cerah2:
        return "Cerah"
    if mp1:
        return "Cuaca Baik"
    if mp2:
        return "Cerah"
    if mp3:
        return "Berjerebu"
    if mp4:
        return "Berangin"
    if mp5:
        return "Tiada Hujan"
    if mp6:
        return "Mendung"
    if mp7:
        return "Mendung di kawasan pantai"
    if mp8:
        return "Mendung di kawasan pedalaman"
    if mp9:
        return "Hujan"
    if mp10:
        return "Hujan sekejap-sekejap kadangkala sederhana"
    if mp11:
        return "Hujan sekejap-sekejap kadangkala lebat"
    if mp12:
        return "Hujan sekejap-sekejap yang menyeluruh"
    if mp13:
        return "Hujan di satu dua tempat"
    if mp14:
        return "Hujan di beberapa tempat di kawasan pantai"
    if mp15:
        return "Hujan di beberapa tempat di kawasan pedalaman"
    if mp16:
        return "Hujan curah/sementara di satu dua tempat"
    if mp17:
        return "Hujan curah/sementara di kawasan pantai"
    if mp18:
        return "Hujan curah/sementara di kawasan pedalaman"
    if mp19:
        return "Hujan curah"
    if mp20:
        return "Hujan curah di satu dua tempat"
    if mp21:
        return "Hujan curah di kawasan pantai"
    if mp22:
        return "Hujan curah di kawasan pedalaman"
    if mp23:
        return "Ribut petir"
    if mp24:
        return "Ribut petir di satu dua tempat"
    if mp25:
        return "Ribut petir di beberapa tempat"
    if mp26:
        return "Ribut petir di kebanyakan tempat"
    else:
        return eng
 
# example usage : 
print "translating 'No rain' = " + getBM("No rain")

then to test it..

Related Post

2 Responses

  1. guest says:

    Hello, I’d rather use killall or
    ps xa|awk ‘/[f]irefox/ {print $1}’|xargs kill -9.

    Reply
  2. namran says:

    yes .. can use killall too , if you using the default firefox installation.

    but it will not kill the custom firefox which had launched from a custom script like the following ..

    /usr/local/lib/firefox-3.5.13/firefox-bin

    and .. producing list of process as follows :

    [namran@nb-namran ~]$ ps xa |grep firefox
     4626 ?        S      0:00 /bin/sh /home/namran/.bin/firefox
     4630 ?        S      0:00 /bin/sh /usr/local/lib/firefox-3.5.13/run-mozilla.sh /usr/local/lib/firefox-3.5.13/firefox-bin
     4634 ?        RLl  174:47 /usr/local/lib/firefox-3.5.13/firefox-bin
    Reply

Leave a Reply to namran Cancel reply

Your email address will not be published. Required fields are marked *