a day to write, a day to change

Latest

python’s time:pymongo

MongoDB is document-based NoSQL DB. NoSQL is abbreviation of Not just SQL (can solved the problem).

The installer that I tried was for 32-bit (both mongodb and pymongo), and it has big minus. You can only store 2 Gb db files. If you want to use it for serious apps, you have to switch on 64-bit.

pymongo first try

Mengganti default provider di modem Haier

Default provider di modem Haier biasanya AHA. Jika ingin mengubahnya menjadi Smart, pertama temukan DialupCfg.ini, biasanya ada di C:\Program Files\Haier Dialer\UserData\Default\DialupCfg.ini. Ubah property DafaultCfg=AHA menjadi DafaultCfg=Smart. Restart aplikasi Haier Dialer.

 

Observer pattern

Remember it for subscribe/unsubscribe or register/unregister. We give you our service until you unsubscribe.

Strategy pattern

Untuk flash memory saja, saya akan mengingat pattern ini ketika ada method yang naik kelas menjadi class/interface.

Software professional must remember

Here is a minimal list of the things that every software professional should be
conversant with:

• Design patterns. You ought to be able to describe all 24 patterns in the GOF book
and have a working knowledge of many of the patterns in the POSA books.

• Design principles. You should know the SOLID principles and have a good
understanding of the component principles.

• Methods. You should understand XP, Scrum, Lean, Kanban, Waterfall,
Structured Analysis, and Structured Design.

• Disciplines. You should practice TDD, Object-Oriented design, Structured
Programming, Continuous Integration, and Pair Programming.

• Artifacts: You should know how to use: UML, DFDs, Structure Charts, Petri
Nets, State Transition Diagrams and Tables, flow charts, and decision tables.

(from clean coder page 18-19).

clean coder quotes

Design your code to be easy
to test. And the best way to do that is to write your tests first, before you write
the code that passes them. (page 13).

Catatan kecil tentang Scrum

    -Daily standup meeting 30m in early day.
    -In Scrum the only constant was change while waterfall consider stability in each process.
    -Scrum Master’s job also to protect developer from outside disturbance.
    -New focus: Manager not dictate, but developer team decide themselves how to accomplishing their work.
    -Post-Execution inspection.

python’s time:html parser

Nyoba-nyoba code dari Dive Into Python tentang html parser.
Code ini akan membuka dokumen html www.detik.com dan mengenali semua link di dalamnya.

import urllibfrom sgmllib import SGMLParser

class URLLister(SGMLParser):
    def reset(self):
        SGMLParser.reset(self)
        self.urls = []

    def start_a(self, attrs):
        href = [v for k, v in attrs if k=='href']
        if href:
            self.urls.extend(href)

sock = urllib.urlopen("http://www.detik.com")
parser = URLLister()
parser.feed(sock.read())
sock.close()
parser.close()
for url in parser.urls: print url

python’s time:using sqlautocode

While playing with sqlalchemy, I want to generate entity class for tables in database. That’s sqlautocode come in.

The first thing to do  to use sqlautocode is declare class DummyConfig where we put connection string.

I have a sqlite file ‘sqlal.db’ and table users and employee.

class DummyConfig:
    def __init__(self, engine='sqlite:///D:/PROYEK/Sample And Exercise goes HERE/pyCoba/src/db/sqlal.db'):
        self.engine  = engine

    example = True
    schema = None
    interactive = None

declare some variable and print your entity class.


from sqlautocode.declarative import ModelFactory
from sqlalchemy.orm import class_mapper

config = DummyConfig()
factory = ModelFactory(config)

factory.used_model_names = []
factory.used_table_names = []

User = factory.create_model(factory._metadata.tables['users'])
Employee = factory.create_model(factory._metadata.tables['employee'])

class_mapper(User, True)
class_mapper(Employee, True)

print User.__repr__()
print Employee.__repr__()

This is the output

class User(DeclarativeBase):
 __tablename__ = 'users'

 #column definitions
 fullname = Column(u'fullname', VARCHAR(length=None, convert_unicode=False, assert_unicode=None, unicode_error=None, _warn_on_bytestring=False))
 id = Column(u'id', INTEGER(), primary_key=True, nullable=False)
 name = Column(u'name', VARCHAR(length=None, convert_unicode=False, assert_unicode=None, unicode_error=None, _warn_on_bytestring=False))
 password = Column(u'password', VARCHAR(length=None, convert_unicode=False, assert_unicode=None, unicode_error=None, _warn_on_bytestring=False))

 #relation definitions

class Employee(DeclarativeBase):
 __tablename__ = 'employee'

 #column definitions
 id = Column(u'id', INTEGER(), primary_key=True, nullable=False)
 nama = Column(u'nama', CHAR(length=None, convert_unicode=False, assert_unicode=None, unicode_error=None, _warn_on_bytestring=False))

 #relation definitions

python’s time:how to add column in ArcSDE Tables and fill the value

## Script untuk menambah kolom OLD_FID, OLD_TDATAID, dan UNIT, kemudian mengisinya.
## Inputan : Feature Dataset dan Unit.
## Dilakukan per unit.
import sys, os, re, arcgisscripting, datetime, logging

gp = arcgisscripting.create()
inDataset = gp.GetParameterAsText(0)
unit = gp.GetParameterAsText(1)
logging_path = gp.GetParameterAsText(2)
##  set logging ke file
logging.basicConfig(level=logging.DEBUG,
                    format='%(asctime)s %(levelname)s %(message)s',
                    filename=logging_path,
                    filemode='w')

def log_me(gp, msg, is_info):
    gp.AddMessage(str_log)
    if is_info == 1:
        logging.info(str_log)
    else:
        logging.error(str_log)

##boolean method special jika ada error di tengah jalan, maka feature yang dicatat di list_proceed_fc akan di-ignore.
def isInListProceed(nama_fc):

##   list_proceed_fc mencatat feature yang telah diproses, misal: 'SDE.MRuteBM', 'SDE.MTrafo', dan 'SDE.MBlokGardu'
##   list_proceed_fc=('SDE.MRuteBM', 'SDE.MTrafo', 'SDE.MBlokGardu')
    
   list_proceed_fc=('')
   for fcPro in list_proceed_fc:
       if fcPro == nama_fc:
           return True
   return False 

try:
    from time import gmtime, strftime
    str_log = ""

    str_log = "Unit : %s " % unit
    log_me(gp,str_log,1)
    gp.workspace = inDataset

    # Get a list of feature classes in the workspace.
    fcs = gp.ListFeatureClasses()
        
    # Loop through the list of feature classes.
    fcs.reset()
    fc = fcs.next()
    
##    Deklarasi list feature class yang akan diproses
    listProceedFc = []

    while fc:
        listProceedFc.append(str(fc))
        
##      log feature yang akan diproses        
        str_log = "list featureclass : %s " % listProceedFc
        logging.info(str_log)
	
        str_log = "Nama featureclass : %s " % fc
        log_me(gp, str_log, 1)

        
##        Syarat add field  dan pengisian data adalah tidak ada user lain yang locking
##        Add field OLD_FID, Double(38,8)
        try:    
            gp.AddField_management(fc, "OLD_FID", "DOUBLE", 38, 8)
        except Exception, ErrorFieldExists:
            str_log = "Kolom OLD_FID telah ditambahkan."
            log_me(gp, str_log, 1)
            
##        Add field OLD_TDATAID, Double(38,8)
        try:
            gp.AddField_management(fc, "OLD_TDATAID", "DOUBLE", 38, 8)
        except Exception, ErrorFieldExists:
            str_log = "Kolom OLD_TDATAID telah ditambahkan."
            log_me(gp, str_log, 1)

##        Add field UNIT, Text(50)
        try:
            gp.AddField_management(fc, "UNIT", "TEXT", 50)
        except Exception, ErrorFieldExists:
            str_log = "Kolom UNIT telah ditambahkan."
            log_me(gp, str_log, 1)

##        Add field OLD_GLOBALID, Text(50)
        try:    
            gp.AddField_management(fc, "OLD_GLOBALID", "TEXT", 50)
        except Exception, ErrorFieldExists:
            str_log = "Kolom OLD_GLOBALID telah ditambahkan."
            log_me(gp, str_log, 1)

##      update row cursor OLD_FID sangat lambreta. coba ganti dengan gp.CalculateField_management
        gp.AddMessage("CalculateField OLD_FID from OBJECTID on %s " % strftime("%a, %d %b %Y %H:%M:%S", gmtime()))
        gp.CalculateField_management(fc,"OLD_FID", "!OBJECTID!", "PYTHON")

##      update field OLD_GLOBALID menggunakan value dari GLOBALID
        gp.AddMessage("CalculateField OLD_GLOBALID from GLOBALID on %s " % strftime("%a, %d %b %Y %H:%M:%S", gmtime()))
        gp.CalculateField_management(fc,"OLD_GLOBALID", "!GLOBALID!", "PYTHON")
        
##        update old_tdataid terpaksa menggunakan cursor karena calculatefield tidak stabil
        str_log = "Sedang memproses OLD_TDATAID dari TDATA_ID yang <> None dan <> 0 ..."
        log_me(gp, str_log, 1)
        try:
    ##      Get data feature class         
            rows = gp.UpdateCursor(fc, "TDATA_ID IS NOT NULL AND TDATA_ID > 0", "", "TDATA_ID; OLD_TDATAID")
            row = rows.Next()

            i=1
            while row <> None:
##                tulis ke log jika sudah mencapai 5000 record
                if i % 5000 == 0:
                    str_log = "Record %s ke %s." % (fc,i)
                    logging.info(str_log)
                                        
                objTDATAID_old = row.GetValue("TDATA_ID")
                row.SetValue("OLD_TDATAID", objTDATAID_old)
                rows.UpdateRow(row)
                i += 1
                row = rows.Next()
       
            # Delete row yang digunakan sebagai koleksi data feature class
            del row, rows
        except Exception, ErrorTDataID:
            del row, rows
            str_log = "Error set value TDataID %s" % str(ErrorTDataID)
            logging.error(str_log)
        

        str_log = "CalculateField UNIT from x on %s " % strftime("%a, %d %b %Y %H:%M:%S", gmtime())
        log_me(gp, str_log, 1)
        gp.CalculateField_management(fc,"UNIT", "x", "PYTHON", "x = '" + unit + "'")

        fc = fcs.next()
except Exception, ErrorDesc:
    str_log = "error desc %s" % str(ErrorDesc)
    log_me(gp, str_log, 0)

Follow

Get every new post delivered to your Inbox.