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

Get Free Email Updates!

Signup now and receive an email once I publish new content.

I will never give away, trade or sell your email address. You can unsubscribe at any time.

Like

Related Post

One Response

  1. namran says:

    The server response can be checked as following :

    [namran@nb-namran ~]$ wget -O- --server-response http://dev.namran.net/index.html 2>&1 |less

    and it should shows the response as the following

    ...--2010-11-10 23:51:18-- http://dev.namran.net/index.html
    Resolving dev.namran.net... 210.48.154.27
    Connecting to dev.namran.net|210.48.154.27|:80... connected.
    HTTP request sent, awaiting response...
    HTTP/1.1 200 OK
    Date: Wed, 10 Nov 2010 15:51:20 GMT
    Server: Apache/2.2.3 (Red Hat)
    Last-Modified: Sun, 24 Oct 2010 08:54:00 GMT
    ETag: "5bf39c-d5-4935904475a00"
    Accept-Ranges: bytes
    Content-Length: 213
    Cache-Control: max-age=60, public, must-revalidate
    Expires: Thu, 15 Apr 2010 20:00:00 GMT
    Connection: close
    Content-Type: text/html; charset=UTF-8
    Length: 213 [text/html]
    Saving to: `STDOUT'
    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
    <html>
    <head>
    <title>Loading mesinPhotostat</title>
    <meta http-equiv="REFRESH" content="0;url=http:mesinPhotostat/"></HEAD>
    <BODY>
    Loading mesinPhotostat ...
    </BODY>
    </HTML>
    Reply

Anything to add?

X