Python : How to write a function in python

Tags: ,
Share the joy
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  

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..

Sharing is caring
Sharing is caring

About Namran Hussin

a soft spoken guy... with exceptional interest in computers and technology. I love to learn new thing and also love to break thing for the sake of learning.. but I do abide to the self-imposed limitation or certain thing such as social thing in life, thing can be done and thing that must be avoided at whatever cost such as drug,illegal tracking, smoke,illicit activity..etc.muahahaha let's share what we had in this short term of the life.! make it worth of the living.~

2 thoughts on “Python : How to write a function in python

  1. namran Post author

    the above function can be simplified a but further with the following :

    #!/usr/bin/python
     
    def getBM (eng): 
        if eng == 'No rain':
            return "Tiada Hujan"
        if eng == 'Rain':
            return "Hujan"
        if eng == 'fair':
            return "Cerah" 
        if eng == 'Weather Fair':
            return "Cuaca Baik"
        if eng == 'Clear':
            return "Cerah"
        if eng == 'Hazy':
            return "Berjerebu"
        if eng == 'Windy':
            return "Berangin"
        if eng == 'No rain':
            return "Tiada Hujan"
        if eng == 'Cloudy':
            return "Mendung"
        if eng == 'Cloudy over coastal areas':
            return "Mendung di kawasan pantai"
        if eng == 'Cloudy over inland areas':
            return "Mendung di kawasan pedalaman"
        if eng == 'Rain':
            return "Hujan"
        if eng == 'Intermittent rain occasionally moderate':
            return "Hujan sekejap-sekejap kadangkala sederhana"
        if eng == 'Intermittent rain occasionally heavy':
            return "Hujan sekejap-sekejap kadangkala lebat"
        if eng == 'Windspread intermittent rain':
            return "Hujan sekejap-sekejap yang menyeluruh"
        if eng == 'Isolated rain':
            return "Hujan di satu dua tempat"
        if eng == 'Isolated rain over coastal areas':
            return "Hujan di beberapa tempat di kawasan pantai"
        if eng == 'Isolated rain over inland areas':
            return "Hujan di beberapa tempat di kawasan pedalaman"
        if eng == 'Scattered rain':
            return "Hujan curah/sementara di satu dua tempat"
        if eng == 'Scattered rain over coastal areas':
            return "Hujan curah/sementara di kawasan pantai"
        if eng == 'Scattered rain over inland areas':
            return "Hujan curah/sementara di kawasan pedalaman"
        if eng == 'Showers':
            return "Hujan curah"
        if eng == 'Isolated showers':
            return "Hujan curah di satu dua tempat"
        if eng == 'Isolated showers over coastal areas':
            return "Hujan curah di kawasan pantai"
        if eng == 'Isolated showers over inland areas':
            return "Hujan curah di kawasan pedalaman"
        if eng == 'Thunderstorms':
            return "Ribut petir"
        if eng == 'Isolated thunderstorms':
            return "Ribut petir di satu dua tempat"
        if eng == 'Scattered thunderstorms':
            return "Ribut petir di beberapa tempat"
        if eng == 'Widespread thunderstorms':
            return "Ribut petir di kebanyakan tempat"
        else:
            return eng
     
    # example usage : 
    print "translating 'No rain' = " + getBM("No rain")
    print "translating 'No rain' = " + getBM("Scattered thunderstorms")
    print "translating 'No rain' = " + getBM("Scattered rain over inland areas")

    hmm. if putting into a file and read from there would be much more better.

  2. namran Post author

    yeah..

    to make it read from a text file and behave exactly as above.

    #!/usr/bin/python
     
    import csv
    # to read from a file with list of weather.
    reader = csv.reader(open("weather_list.txt", "rb"), delimiter=':', quoting=csv.QUOTE_NONE)
    # build up a reference list
    kamus = {}
    for row in reader:
     english_w=row[0].lstrip(' ').strip(' ')
     bm_w=row[1].lstrip(' ').strip(' ')
     kamus[english_w] = bm_w
     
    # a function to return exactly the same
    def getBM (eng) :
     return kamus[eng]
    # example usage : 
    print getBM ("Isolated showers");
    print getBM ("Rain");

    then create text file with the following content.

    [namran@nb-namran Desktop]$ cat weather_list.txt 
    Weather Fair:Tiada Hujan
    Clear:Cerah
    Hazy:Berjerebu
    Windy:Berangin
    No rain:Tiada Hujan
    Cloudy:Mendung
    Cloudy over coastal areas:Mendung di kawasan pantai
    Cloudy over inland areas:Mendung di kawasan pedalaman
    Rain:Hujan
    Intermittent rain occasionally moderate:Hujan sekejap-sekejap kadangkala sederhana
    Intermittent rain occasionally heavy:Hujan sekejap-sekejap kadangkala lebat
    Widespread intermittent rain:Hujan sekejap-sekejap yang menyeluruh
    Isolated rain:Hujan di satu dua tempat
    Isolated rain over coastal areas:Hujan di beberapa tempat di kawasan pantai
    Isolated rain over inland areas:Hujan di beberapa tempat di kawasan pedalaman
    Scattered rain:Hujan curah/sementara di satu dua tempat
    Scattered rain over coastal areas:Hujan curah/sementara di kawasan pantai
    Scattered rain over inland areas:Hujan curah/sementara di kawasan pedalaman
    Showers:Hujan curah
    Isolated showers:Hujan curah di satu dua tempat
    Isolated showers over coastal areas:Hujan curah di kawasan pantai
    Isolated showers over inland areas:Hujan curah di kawasan pedalaman
    Thunderstorms:Ribut petir
    Isolated thunderstorms:Ribut petir di satu dua tempat
    Scattered thunderstorms:Ribut petir di beberapa tempat
    Widespread thunderstorms:Ribut petir di kebanyakan tempat

Comments are closed.

%d bloggers like this: