Contents
- Books
- Python
-
date conversion
- string from time now
- string parse date
- string from date
- string parse time
- string from time
- string from date and time
- from string to date list
- from string to datetime
- from datetime to string
- comapre two string
- today minus - one day
- Today minus one day in shell script
- Today plus one month
- from string to date to string
- python-mysqldb
- python and sqlalchemy
- python and writelines
- Object Oriented
- Python Path
Books
Python
Ftplib
- Connect to Ftp Site
import ftplib #Import Ftp Library
ftp=ftplib.FTP('example.com') #Connect to a ftp site
ftp.login('username','password') #Provide Username
ftp.dir() #List directory folder,prints to std.out
ftp.quit()- How to retrieve list into python variable
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
list.append(line)- Now call the ftp dir command:
ftp.dir('./',get)
print list #print the list
for i in list:
print i #prints the list line by line
dateutil
- Add a month to a date,
import datetime
from dateutil.relativedelta import relativedelta
date1='10/1/2007'
#We need our final step to look like:
a=datetime.date(2007, 10, 1)
#We do"
b=date1.split('/')
a=datetime.date(b[2],b[0],b[1])
a + relativedelta(months=+1)
decorators
- Member access with . operator
instance.method() instance.attribute instance.attribute.another
- Functions/methods are not the only things that are callable
- Decorators apply a callable to a function at creation time:
@g
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 a='12/31/2007'
string parse date
time.strptime(a,"%m/%d/%Y") (2007, 12, 1, 0, 0, 0, 5, 335, -1)
string from date
time.strftime("%Y%m%d",time.strptime(a,"%m/%d/%Y"))
'20071201'
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"))
'20:15:00'
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"))
'19960424'
time.strftime("%Y%m%d",time.localtime(time.time())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
time.strptime('20080101',"%Y%m%d")[:3]
from string to datetime
datetime.datetime(*time.strptime('20080101',"%Y%m%d")[:3])
from datetime to string
time.strftime('%Y%m%d',datetime.now().timetuple()) 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 oneday=timedelta(days=1) datetime.now().date()-oneday
Today minus one day in shell script
date --date='1 days ago' '+%Y-%m-%d' or date --date='28 days ago' '+%Y-%m-%d'
Today plus one month
Here is how you add one month
from dateutil import relativedelta a datetime.datetime(2011, 1, 20, 0, 0) a.date()+relativedelta.relativedelta(months=+1) datetime.date(2011, 2, 20)
from string to date to string
If you need to make it a 2 digit date and 3 digit year.
checkdate='2/4/11'
(m,d,y)=checkdate.split('/')
checkdate2="%02d/%02d/%02d" %(int(m),int(d),int(y))
newdate=time.strftime("%m/%d/%Y",time.strptime(str(checkdate2),"%m/%d/%y"))
print newdate
python-mysqldb
import MySQLdb db=MySQLdb.connect(host="localhost",user="joebob",passwd="moonpie",db="thangs") #stuff db,close()
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 http://www.rmunn.com/sqlalchemy-tutorial/tutorial.html 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.create()
#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
debuglevel=0
#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)
else:
if debuglevel > 0: print >>sys.stderr, 'Folder not Found: '+folder
raise RuntimeError,"Folder not Found"
#Get file names in folder
filesinfolder=os.listdir(folder)
if debuglevel > 0: print >>sys.stderr, 'Found: '+str(len(filesinfolder))+' files'
import time
y=myclass.example()
#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={}
j['LastName']=y.qpfiledict['LAST-NAME-OF-CUSTOMER']
j['FirstName']=y.qpfiledict['FIRST-NAME-OF-CUSTOMER']
j['MiddleName']=y.qpfiledict['MIDDLE-INITIAL']
j['EffDate']=y.qpfiledict['EFF-DATE']
j['ExpDate']=y.qpfiledict['EXP-DATE']
j['CreatedUid']='MyCompany'
#j['CreatedDate']=y.y.qpfiledict['EFF-DATE']=time.strftime("%Y%m%d",time.strptime(y.Qpfiledict['EFF-DATE'],"%Y%m%d/%Y"))
#j['CreatedTime']=time.strftime("%H:%M:%S",time.strptime(y.qpfiledict['BIND-TIME'],"%I:%M %p"))
#do some more stuff
myinsert.execute(j)
sqlalchemy and mssql
The most important part is the url of the connection string. First you need to setup unixODBC then you can use:
#MSSQL 2008
import sqlalchemy
e = sqlalchemy.create_engine("mssql+pyodbc://Username:Password@mydsnname")
e.echo=True
#e.echo=False
metadata=sqlalchemy.MetaData(e)
from sqlalchemy.orm import sessionmaker
Session = sessionmaker(bind=e, autoflush=True, autocommit=True)
session = Session()
Stored Procedure and sqlalchemy
- If you want to execute a stored procedure that requires parameters to be passed in you can use:
#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"})
or
myresults=session.execute("assp_ReportDailyTransactions @start_date=:start,@end_date=:end",params={'start':"20100701",'end':"20100719"}).fetchall() - Then the rows are tuple so you can call each column by its column name like:
for i in myresults:
print i.customername
print i.lastname
ceODBC vs pyodbc
ceODBC
import ceODBC
connection = ceODBC.Connection("DSN=MYDBNAME;UID=USERNAME;PWD=PASSWORD")
cursor = connection.cursor()
cursor.execute('select * from mytable')
a=cursor.fetchall()
print 'ceODBC',a
pyodbc
import pyodbc
cnxn = pyodbc.connect("DSN=MYDBNAME;UID=USERNAME;PWD=PASSWORD")
cursor = cnxn.cursor()
cursor.execute('select * from mytable')
a=cursor.fetchall()
print 'pyodbc',a
dsn-less connection
- This is for pyodbc but you can use it ceODBC as well.
import pyodbc
cnxn = pyodbc.connect("SERVER=xxxx;UID=xxx;PWD=xxx;DRIVER={TDS};TDS_Version=7.0")
c=cnxn.cursor()
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=connection.cursor()
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)]
sqlalchemy
#mssql connection string. (On Linux using unixODBC you will have to provide a driver name, on Windows driver name defaults to 'SQL Server')
mssql_db=create_engine('mssql://user:pass@hostname:1433/mydatabase?driver=TDS')
sqlalchemy in virtualenv
aptitude install python-virtualenv cd python /usr/lib/python2.4/site-packages/virtualenv.py ENV
- Add your virtual path to PYTHONPATH:
export PYTHONPATH=/home/lucas/ENV/lib/python2.4/site-packages/ echo $PYTHONPATH
- Download the your program or svn, and install it:
mkdir tmp cd tmp svn checkout http://svn.sqlalchemy.org/sqlalchemy/trunk sqlalchemy cd sqlalchemy python setup.py install --prefix /home/lucas/ENV
- Now run the python in this way:
/home/lucas/ENV/bin/python
- And that will have all the virtual environment files
- To install stuff into it
~/ENV/bin/easy_install Elixir-0.5.2-py2.4.egg
[cleverdevil.org/elixirtalk/slides.pdf 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"))
writer.writerows(someiterable)- The csv module will write new line character at the end of line.
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__']
issubclass
def getblocks(allblocks):
for i in dir(blocks):
x=getattr(blocks,i)
if i=='Element':
continue
if type(x)==type:
if issubclass(x,object):
allblocks[i]=x()
print x
else:
continue
return allblocks
isinstance
def fillblock(self,**kwargs):
if debuglevel > 0: print >>sys.stderr, 'Filling block, kwargs:' , kwargs
for i in dir(self):
x=getattr(self,i)
if isinstance(x,Element):
print x.return_value()
Python Path
Locate bin and your modules
Mac OS X and paths
- From a Mac OS X application's perspective, the "directory from which
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 Mail.app has an "Open Scripts Folder" menu.
Canonical way of detecting py2app, py2exe
def find_packager():
- import sys 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'
- return 'py2app'
- # it doesn't ALWAYS set this return 'cx_Freeze'
return '<unknown packager: %r>' % (frozen,)
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():
import main return os.path.dirname(os.path.abspath(main.file))
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:
mypackage/
init.py mydatafile
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'
else:
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/Foo.app/Contents/Resources -> path, even if
something in your application chdir'ed in the meantime
return
os.path.dirname(os.path.dirname(os.path.dirname(os.environ['RESOURCEPATH
'])))
else:
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.