Keywords: ftplib, python, datetime, dateutil, relative dates


  1. Building Skills

  2. Dive Into Python



import ftplib             #Import Ftp Library
ftp=ftplib.FTP('')      #Connect to a ftp site
ftp.login('username','password')   #Provide Username
ftp.dir()                          #List directory folder,prints to std.out

list=[]    #define a variable that will hold ftp site pwd
def get(line=''):
    global list   #let's you use the list you have created outside of function

print list         #print the list
for i in list:
    print i        #prints the list line by line


import datetime
from dateutil.relativedelta import relativedelta


#We need our final step to look like:, 10, 1)
#We do"

a + relativedelta(months=+1)



def f(x):

is equivalent to:

def f(x):
f = g(f)

date conversion

string from time now

import time
now = time.localtime(time.time())

import time

string parse date

(2007, 12, 1, 0, 0, 0, 5, 335, -1)

string from date


string parse time

a='08:15 pm'
import time
time.strptime(a,"%I:%M %p")
(1900, 1, 1, 20, 15, 0, 0, 1, -1)

string from time

time.strftime("%H:%M:%S",time.strptime(a,"%I:%M %p"))

string from date and time

a='04/24/96 00:00:00'
time.strftime("%Y%m%d",time.strptime(a,"%m/%d/%y %H:%M:%S"))

Directive       Meaning         Notes
%a      Locale's abbreviated weekday name.      
%A      Locale's full weekday name.     
%b      Locale's abbreviated month name.        
%B      Locale's full month name.       
%c      Locale's appropriate date and time representation.      
%d      Day of the month as a decimal number [01,31].   
%H      Hour (24-hour clock) as a decimal number [00,23].       
%I      Hour (12-hour clock) as a decimal number [01,12].       
%j      Day of the year as a decimal number [001,366].  
%m      Month as a decimal number [01,12].      
%M      Minute as a decimal number [00,59].     
%p      Locale's equivalent of either AM or PM.         (1)
%S      Second as a decimal number [00,61].     (2)
%U      Week number of the year (Sunday as the first day of the week) as a decimal number [00,53]. All days in a new year preceding the first Sunday are considered to be in week 0.    (3)
%w      Weekday as a decimal number [0(Sunday),6].      
%W      Week number of the year (Monday as the first day of the week) as a decimal number [00,53]. All days in a new year preceding the first Monday are considered to be in week 0.    (3)
%x      Locale's appropriate date representation.       
%X      Locale's appropriate time representation.       
%y      Year without century as a decimal number [00,99].       
%Y      Year with century as a decimal number.  
%Z      Time zone name (no characters if no time zone exists).  
%%      A literal "%" character.        

from string to date list


from string to datetime


from datetime to string

time.strftime('%Y%m%d', or time.strftime("%Y%m%d",time.localtime(time.time()))

comapre two string

if datetime(*time.strptime(str(EFF_DATE),"%Y%m%d")[:3]).date() > datetime(*time.strptime(str(CODING_DATE),"%Y%m%d")[:3]).date()

today minus - one day

from datetime import timedelta

Today minus one day in shell script

date --date='1 days ago' '+%Y-%m-%d'
date --date='28 days ago' '+%Y-%m-%d'

Today plus one month

Here is how you add one month

from dateutil import relativedelta
datetime.datetime(2011, 1, 20, 0, 0), 2, 20)

from string to date to string

If you need to make it a 2 digit date and 3 digit year.

checkdate2="%02d/%02d/%02d" %(int(m),int(d),int(y))
print newdate


import MySQLdb

python and sqlalchemy

"""This program will proccess some files and move some info to mysql database.What should be important to you is just the database calls. See for a program that you can copy and run. Example howto simple, sqlalchemy. Try it. No more insert into ...."""

from sqlalchemy import *

#db = create_engine('sqlite:///tutorial.db')
db = create_engine('mysql://user:pass@localhost/dbname')

db.echo = False  # Try changing this to True and see what happens

metadata = BoundMetaData(db)

#users = Table('users', metadata,
#    Column('user_id', Integer, primary_key=True),
#    Column('name', String(40)),
#    Column('age', Integer),
#    Column('password', String),

mytable=Table('mytable', metadata, autoload=True)


#i = users.insert()
#i.execute(name='Mary', age=30, password='secret')
#i.execute({'name': 'John', 'age': 42},
#          {'name': 'Susan', 'age': 57},
#          {'name': 'Carl', 'age': 33})

#End of created tables.

#-----------Read qp files from a folder---------
import os
import sys
import time
#Folder to get the qp files from
folder = '/home/lucas/mydata

#See if folder exists
if os.path.exists(folder):
    if debuglevel > 0: print >>sys.stderr, 'Folder Found: '+str(folder)
    if debuglevel > 0: print >>sys.stderr, 'Folder not Found: '+folder
    raise RuntimeError,"Folder not Found"

#Get file names in folder
if debuglevel > 0: print >>sys.stderr, 'Found: '+str(len(filesinfolder))+' files'

import time


#Go through files, and transfer each policy 
for file in filesinfolder:
    if file[-4:]=='.DAT':
        #Get Filename:
        filename = folder + file
        if debuglevel > 0: print >>sys.stderr, 'Filename: ',filename

        myinsert = mytable.insert()
        #j['CreatedTime']=time.strftime("%H:%M:%S",time.strptime(y.qpfiledict['BIND-TIME'],"%I:%M %p"))
        #do some more stuff

sqlalchemy and mssql

#MSSQL 2008 

import sqlalchemy
e = sqlalchemy.create_engine("mssql+pyodbc://Username:Password@mydsnname")

from sqlalchemy.orm import sessionmaker
Session = sessionmaker(bind=e, autoflush=True, autocommit=True)
session = Session()

Stored Procedure and sqlalchemy

#You need to separate the parameters with a "," and you use ":somename" and {'somename':'somevalue"} as a way of passing data.
myresults=session.execute("assp_ReportDailyTransactions @start_date=:start,@end_date=:end",params={'start':"20100701",'end':"20100719"}) 
myresults=session.execute("assp_ReportDailyTransactions @start_date=:start,@end_date=:end",params={'start':"20100701",'end':"20100719"}).fetchall() 

for i in myresults:
    print i.customername
    print i.lastname

ceODBC vs pyodbc


import ceODBC
cursor = connection.cursor()
cursor.execute('select * from mytable')
print 'ceODBC',a


import pyodbc
cursor = cnxn.cursor()
cursor.execute('select * from mytable')
print 'pyodbc',a

dsn-less connection

import pyodbc
cnxn = pyodbc.connect("SERVER=xxxx;UID=xxx;PWD=xxx;DRIVER={TDS};TDS_Version=7.0")
c.execute('select D017038P.PH_POLICY_HISTORY_NUMBER_38 as PH_POLICY_HISTORY_NUMBER_38_THIS_IS_OVER_30 from D017038P').fetchone()
('    7547', )
print c.description
(('PH_POLICY_HISTORY_NUMBER_38_THIS_IS_OVER_30', <type 'str'>, None, 8, None, None, False),)

import ceODBC
connection = ceODBC.Connection("SERVER=xxx;UID=xx;PWD=xxx;DRIVER={TDS};TDS_Version=7.0")
c.execute('select xxx.xxxxxx_HISTORY_NUMBER_38 as xxxxxxxx_HISTORY_NUMBER_38_THIS_IS_OVER_30 from xxxx').fetchone()
('    7547',)
print c.description
[('xxxxxxxxx_HISTORY_NUMBER_38_THIS_IS_OVER_30', <type 'ceODBC.StringVar'>, 8, 8, 0, 0, False)]


#mssql connection string. (On Linux using unixODBC you will have to provide a driver name, on Windows driver name defaults to 'SQL Server')

sqlalchemy in virtualenv

aptitude install python-virtualenv
python /usr/lib/python2.4/site-packages/ ENV

export PYTHONPATH=/home/lucas/ENV/lib/python2.4/site-packages/

mkdir tmp
cd tmp
svn checkout sqlalchemy
cd sqlalchemy
python install --prefix /home/lucas/ENV


 ~/ENV/bin/easy_install Elixir-0.5.2-py2.4.egg

[ SQLAlchemy Slides]

python and writelines

* Since python under 3.0 cannot write new lines, or writelines doesn't add a new line character at the end of a line you can do use a csv module to get rid of that problem.

import csv
writer = csv.writer(open("some.csv", "wb"))

Object Oriented

class name(object):

>>> class kk(object):
...     pass
>>> l=kk()
>>> dir(l)
['__class__', '__delattr__', '__dict__', '__doc__', '__getattribute__', '__hash__', '__init__', '__module__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__setattr__', '__str__', '__weakref__']
>>> class zz:
...     pass
>>> o=zz()
>>> dir(o)
['__doc__', '__module__']


def getblocks(allblocks):
    for i in dir(blocks):
        if i=='Element':
        if type(x)==type:
            if issubclass(x,object):
                print x
    return allblocks


def fillblock(self,**kwargs):
        if debuglevel > 0: print >>sys.stderr, 'Filling block, kwargs:' , kwargs
        for i in dir(self):
            if isinstance(x,Element):
                print x.return_value()

Python Path

Locate bin and your modules

Mac OS X and paths

your app was launched" is completely ambiguous. Is is so ambiguous, in fact, that when a Mac OS X application starts up by way of LaunchServices (i.e. Finder, /usr/bin/open, etc.) it will have a current directory of /. Yes, that's right. It starts in the root of your filesystem, a completely useless place. The reason for this is that on Mac OS X, you should always always always always ALWAYS ALWAYS find files in one of the following ways:

- Inside your application bundle (via CFBundle or NSBundle functions) - A predefined system path (via FSFindFolder or NSSearchPathForDirectoriesInDomains, NOT by hardcoding something) or a subdirectory of one (i.e. ~/Library/Application Support/MyApplication) - Chosen by the user with some kind of system dialog - A "remembered" (using an alias) location that was chosen by the user with some kind of dialog at one point another (system dialogs do this, so that they remember where the user last chose files from)

That's it. You should ALMOST NEVER use any other way to find stuff.

ALL data files of a correctly constructed Mac OS X application should be in the Resources folder of the bundle. If you want to have user editable versions of these files, then on startup you should make copies of the in-Resources files somewhere like ~/Library/Application Support/MyApplication if they do not exist and use those. You may also add functionality to your application to reveal this path to the user. For example, the Scripts menu in has an "Open Scripts Folder" menu.

Canonical way of detecting py2app, py2exe

def find_packager():

to worry about though

It's possible to build cx_Freeze-frozen applications that are not picked up by this (i.e. ConsoleKeepPath). Bundlebuilder is never really detectable, but you should NEVER use bundlebuilder. py2app is capable of performing all of the operations that bundlebuilder can do (and then some), with the added bonus that its implementation is more correct. I hope to deprecate bundlebuilder in Python 2.5.

Canonical way of finding your main script

import os def mainScriptDir():

Finding your main script should be done in the same way that your main script determines if it is main or not! For applications frozen with py2exe, there IS effectively no on-disk main script (the bytecode is shoved into the executable). Finding your main script is not a useful thing to do from a packaged application anyway, so only use this when find_packager() returns None.

Default invariants for py2app

- sys.frozen == 'macosx_app' - os.getcwd() is the Resources directory of your application's bundle when the application starts. This is a convenience to make most scripts work "out of the box" (but probably after using the argv_emulation option). - os.environ['RESOURCEPATH'] is the absolute path to the Resources directory of your application's bundle when it started - The user's "site-packages" directory WILL NOT be considered by default, so you - Any Python code, dynamic libraries, and frameworks that are determined to be used, aren't explicitly excluded, and aren't located in a system path (/usr/lib or /System) WILL end up somewhere accessible from the runtime in your application bundle. You SHOULD ONLY depend on the fact that the code can be located at runtime, NOT where it is in the application bundle. - NO non-code will be included from packages UNLESS the package is explicitly included using the "packages" option. This can also happen implicitly by way of a recipe. - Such explicitly included packages are included as-is, but may be stripped of 'CVS' and '.svn' subdirectories. You CAN depend on data files that are in such packages, but you should ONLY reference them relative to the dirname of the package's file.

Don't use bundlebuilder

bundlebuilder does a number of things incorrectly and incompletely. Don't use it anymore, ever. A typical py2app setup script is quite a bit simpler than the bundlebuilder version, anyway.

Using Python packages as data file containers

It's common to include necessary data files inside of packages, for example:


To locate such data files, you should always do something equivalent to the following:

import mypackage mydatafilepath = os.path.join(os.path.dirname(os.path.abspath(mypackage.file)), 'mydatafile')

This method will not work in the face of zip imports, so such packages must be explicitly included as-is with packaging solutions such as py2app or py2exe.

If you *actually* want to find something relative to the "application"

Note that this code only belongs in a poorly designed OS X application, but I'm going to tell you anyway since this seems to be what you asked for after a painful reading of the referenced wxPython-users thread:

def find_packager(): 
    """ Detect packaging systems such as py2app and py2exe """ 
            frozen = getattr(sys, 'frozen', None) 
            if not frozen: 
                # COULD be certain cx_Freeze options or bundlebuilder, nothing to worry about though 
                return None 
            elif frozen in ('dll', 'console_exe', 'windows_exe'): 
                return 'py2exe' 
            elif frozen in ('macosx_app',): 
                return 'py2app' 
            elif frozen is True: 
                # it doesn't ALWAYS set this 
                return 'cx_Freeze' 
                return '<unknown packager: %r>' % (frozen,) 

# use find_packager() above
import os
def getApplicationPath():
     packager = find_packager()
     if packager == 'py2exe':
         # note that another approach is
         return os.path.dirname(os.path.abspath(sys.argv[0]))
     elif packager == 'py2app':
         # from path/ -> path, even if  
something in your application chdir'ed in the meantime
         import __main__
         return os.path.dirname(__main__.__file__)

Also note that this isn't bulletproof.

For example, in one of my applications, I use NSIS on top of py2exe to provide a "single file executable" (for which the above code would return a nonsense temporary directory). In the Mac OS X version of this application all of my resources are located in the Resources folder of the application bundle, and I locate them using OS X specific APIs (NSBundle, etc.) since the application is written with PyObjC. In the Win32 version, NSIS sets the current directory to the location of the "single file executable", and the windows version searches that directory for the Mac OS X application, and uses the resources folder of its application bundle for data files. I emulate many of the localization and file-finding capabilities of Cocoa in the Windows code. This is of course a strange way to do things on Windows, since the canonical way is to use an installer, but in this case the application *is* an installer (to put data on a peripheral device, not more software for the machine) and is rarely executed, so it makes perfect sense to do it in this manner.

MyWiki: PythonManual (last edited 2011-02-26 00:14:58 by LukaszSzybalski)