Wednesday, September 28, 2011

smartgrid: currency format and text-align


controllers/default.py
def admin():
    products = SQLFORM.grid(db.Product,deletable=False, paginate=10)
    return dict(products = products)

models/db.py
db.define_table('Product',
    Field('Part_Number'),
    Field('List_Price', 'decimal(13,2)'))

views/default.py

{{extend 'layout.html'}}
<h2>Product Table</h2>
{{=products}}

By default, all text is aligned to left. You can align it right for decimal fields by adding the following in db.py:

db.Product.List_Price.represent = lambda value, row: XML(DIV(value,_style='text-align: right;')) 


or currency format:

db.Product.List_Price.represent = lambda value, row: '$ %.2f' % (0.0 if value == None else value)


or both:


db.Product.List_Price.represent = lambda value, row: DIV('$ %.2f' % (0.0
if value == None else value), _style='text-align: right;')

Ref: https://groups.google.com/group/web2py/browse_thread/thread/97a3e861957e758b/21bdc438c387abd3#21bdc438c387abd3


Thursday, September 15, 2011

web2py: Input field alignment for decimal and integer

By default, all the fileds are aligned to left.


If you want to align to right such decimal and integer, you can simply add the following to your base.css.

input.decimal, input.integer, input.double { 
    text-align: right; 

Then it will be like this,


You can also use "Tight input widget" developed by Kenji san at S-Cubism.

Tight input widget

Monday, September 12, 2011

A simple password generator with wxPython

This is a simple password generator.


  • Providing First and Last Name, then click Get Password! to generate the password
  • Line3, 4: you can change the combination here
  • Line20 - line25: Use 4 digits of last name to generat the numbers in password
  • Line27 - line28: Get the digits for the given alphabet 
  • Line57: Use lambda to pass multiple parameters



  1. import wx
  2. ALP = "HIJKLMNOPQRSTUVWXYZABCDEFG "
  3. NBR = "456789012345678901234567897"
  4. KEY = dict(zip(ALP, NBR))
  5. def getpass(event, firstname, lastname):
  6.     password = ''
  7.    
  8.     # Input both name are required
  9.     if firstname == '' or lastname == '':
  10.         passText.SetValue('Input both first and last name !')
  11.     else:    
  12.         firstname = firstname.upper()
  13.         lastname = lastname.upper()
  14.        
  15.         length = len(lastname)
  16.         # Fill in blank for the short last name
  17.         if length < 4:
  18.             lasttemp = lastname + '   '
  19.         else:
  20.             lasttemp = lastname
  21.         last4 = lasttemp[0:4]
  22.      
  23.         for i in last4:
  24.             password += KEY[i]
  25.         passText.SetValue('Password: ' + firstname + password)
  26. if __name__ == '__main__':
  27.     app = wx.App()
  28.     win = wx.Frame(None, title='Password Generator',
  29.             size = (300, 200), style=wx.SYSTEM_MENU|wx.CAPTION|wx.CLOSE_BOX)
  30.     bkg = wx.Panel(win)
  31.     font = wx.Font(11, wx.FONTFAMILY_DEFAULT, wx.FONTSTYLE_NORMAL, wx.FONTWEIGHT_NORMAL)
  32.     bkg.SetFont(font)
  33.     firstTexts = wx.StaticText(bkg, -1, 'First Name: ', size=(90, 22))
  34.     firstTextc = wx.TextCtrl(bkg, -1, size=(200, 22))
  35.    
  36.     hboxf = wx.BoxSizer()
  37.     hboxf.Add(firstTexts, proportion=0, flag=wx.ALL, border=0)
  38.     hboxf.Add(firstTextc, proportion=0, flag=wx.ALL, border=0)
  39.    
  40.     lastTexts = wx.StaticText(bkg, -1, 'Last Name: ', size=(90, 22))
  41.     lastTextc = wx.TextCtrl(bkg, -1, size=(200, 22))
  42.    
  43.     hboxl = wx.BoxSizer()
  44.     hboxl.Add(lastTexts, proportion=0, flag=wx.ALL, border=0)
  45.     hboxl.Add(lastTextc, proportion=0, flag=wx.ALL, border=0)
  46.    
  47.     submitButton = wx.Button(bkg, label='Get Password!', size=(290, 22))
  48.     submitButton.Bind(wx.EVT_BUTTON,
  49.             lambda event: getpass(event, firstTextc.GetValue(), lastTextc.GetValue()))
  50.     passText = wx.TextCtrl(bkg, -1, '', style=wx.TE_READONLY|wx.BORDER_NONE, size=(290, 22))
  51.     hboxp = wx.BoxSizer()
  52.     hboxp.Add(passText, proportion=0, flag=wx.ALL, border=0)
  53.     vbox = wx.BoxSizer(wx.VERTICAL)
  54.     vbox.Add(hboxf, proportion=0, flag=wx.EXPAND|wx.ALL, border=5)
  55.     vbox.Add(hboxl, proportion=0, flag=wx.EXPAND|wx.ALL, border=5)
  56.     vbox.Add(submitButton,  proportion=0, flag=wx.ALL, border=5)
  57.     vbox.Add(hboxp, proportion=0, flag=wx.EXPAND|wx.ALL, border=5)
  58.     bkg.SetSizer(vbox)
  59.     win.Show()
  60.     app.MainLoop()

Tuesday, September 6, 2011

How to connect DB2 with Python pyodbc

There are very few infromation about it and this is how I connect to DB2 from python and web2py.

My environment
-----------------------------------------------------------
OS: Windows 7 x86
iSeries Access: V5R3M0 - SI26600
Python: 2.5
pyodbc: pyodbc-2.1.7.win32-py2.5.exe
web2py: 1.98.2 (Source Code)
-----------------------------------------------------------

Pre-Requirements
IBM iSeries Access is installed on your machine so that you have the iSeries Access ODBC Driver.If you use DB2, it's already installed on you machine, isn't it?

1. Install pyodbc
Download and install from http://code.google.com/p/pyodbc/downloads/list.

2. Create ODBC Data Source

2.1 Go to: Control Panel - System and Security - Administrative Tools - Data Sources (ODBC)
2.2 Click Add and select iSeries Access ODBC Driver


2.3 Type Data source name and System (Your AS400 Name)


2.4 On Server tab, type SQL default library


2.5 Click Advanced button at the bottom 
2.6 Change Commit mode: from (*CHG) to (*NONE)

Very important:  If you don't change, you might get the following error.
Error: ('HY000', '[HY000] [IBM][iSeries Access ODBC Driver][DB2 UDB]SQL7008 - TEST in MYLIB not valid for operation. (-7008)


3. Connect from web2py
Now you are ready to connect.

3.1 Restart your web2py if it's already started
3.2 Create new app like AS400
3.3 Edit DB.PY as follows.
# -*- coding: utf-8 -*-
db = DAL('db2://DSN=MYDSN;UID=YourID;PWD=YourPassword')
db.define_table('test',
    Field('name'))
3.4 Save and click "database administration"



3.5 Check on AS400




3.6 Now click on "Insert new test" from web2py, input Name and click submit



3.7 Check on AS400 - Oh my got! It works !!!


* You can create separate DSN to connect different libraries

4 Connect from Python Shell
Notice: I'm using "." instead of "/". If you want,  you can change "Naming Conversion" to *SYS on the Server tab in the data source you created.

4.1 SELECT

>>> import pyodbc
>>> conn = pyodbc.connect('DSN=MYDSN;UID=YourID;PWD=YourPassword')
>>> cursor = conn.cursor()
>>> cursor.execute("SELECT * FROM MYLIB.TEST")
<pyodbc.Cursor object at 0x01E6A598>
>>> for row in cursor:
print row
4.2 INSERT
(1, 'Omi Chiba')
>>> cursor.execute("INSERT INTO MYLIB.TEST VALUES(DEFAULT, 'PYTHON ROLL')")
<pyodbc.Cursor object at 0x01E6A598>




4.3 Check on AS400