Archive for the 'python' Category

Python Tips

Setelah beberapa waktu bekerja dengan Google AppEngine, akhirnya saya memberanikan diri untuk menulis sedikit tips tentang bahasa pemrograman Python

  1. Gunakan spasi sebagai indent. Apakah itu 4 spasi (umumnya) atau 2 spasi atau 8, terserah anda. Jangan gunakan tab. Ini untuk memudahkan mendebug aplikasi biar anda pindah2 platform, dengan konsisten menggunakan indentasi spasi tertentu, biar kode anda di transfer kemana2, dibuka dengan editor apa aja, indentasi tetep N spasi.
  2. Python itu case-sensitive. true adalah identifier, sedang True adalah boolean bernilai benar. Begitu juga dengan False dan None
  3. Assigning Multiple Values anda bisa:
    (a,b,c) = (1, 2, 3)
  4. Manfaatkan List Comprehension, daripada:
    lines = 'Hello World'
    words = []
    for word in lines.split(’ ‘):
        words.append(word.strip())
    

    anda bisa juga menggunakan:

    words = [word.strip() for word in lines.split(' ')]
  5. Scoping
    i = 1
    def do_it():
        i = i + 1
    

    program diatas akan gagal karena anda memodifikasi variabel global / diluar fungsi do_int. Ini implementasi yg bener:

    i = 1
    def do_it():
        global i
        i = i + 1
    
  6. Terakhir: Easter Egg
    >>> from __future__ import braces
      File "<stdin>", line 1
    SyntaxError: not a chance
    

Wednesday, April 30th, 2008 | Tags: Articles, python | 2 Comments

1428H

Sebuah sesi IDLE pendek saya malam ini:

Python 2.5.1 (r251:54863, Apr 18 2007, 08:51:08) [MSC v.1310 32 bit (Intel)] on win32
Type "copyright", "credits" or "license()" for more information.

    ****************************************************************
    Personal firewall software may warn about the connection IDLE
    makes to its subprocess using this computer’s internal loopback
    interface.  This connection is not visible on any external
    interface and no data is sent to or received from the Internet.
    ****************************************************************

IDLE 1.2.1
>>> f = lambda x: isinstance(x, dict) and x.keys()[0]+f(x.values()[0]) or x
>>> print f({"Selamat Idul Fitri 1428H,\n":
	    {"Mohon Maaf":" Lahir dan Batin"}})
Selamat Idul Fitri 1428H,
Mohon Maaf Lahir dan Batin
>>>

Friday, October 12th, 2007 | Tags: Weblog, lebaran, python | 5 Comments

Update SpeedyUsage Script

Tampaknya Telkom ngerombak struktur website TelkomSpeedy.com. Jadi tool untuk ngecek usage speedy nya sempet ga jalan selama beberapa hari. Ditambah lagi dengan monitor saya yang wafat mulai awal minggu kemaren, jadi saya sempet offline selama beberapa hari.

Berikut adalah revisi terbaru skrip untuk ngecek usage speedy tanpa harus nge-download flash nya dan tanpa clutter content yang lain. Cuman usage aja. Keuntungan dari update ini juga bahwa untuk beberapa divre, telkom menggunakan secure http, jadi jelas lebih aman. Dan skrip baru ini mendukung user dari seluruh penjuru indonesia.

  1. speedy21.js - untuk pengguna windows.
  2. speedy21.py
    NB:, rename ekstensi file ke py dan execute command:chmod +x fileanda.py.

Jangan lupa juga, untuk mengedit file ini dan mengganti username dan password sesuai dengan username dan password speedy anda. Semoga bermanfaat.

Thursday, April 26th, 2007 | Tags: Articles, Javascript, Speedy, python | 6 Comments

Checking Speedy Usage in Python

My first python project, converting speedy.js (JavaScript speedy bandwidth usage checking) into python script. Thanks to this project, I’ve learned little knowledge about some python library like: urllib, urllib2, cookielib, and regular expression.

Read the rest of this entry »

Saturday, March 10th, 2007 | Tags: Articles, Speedy, python | 6 Comments