'Python'에 해당되는 글 10

  1. 2009.04.09 pickle in binary file
  2. 2009.04.01 "Short-circuit" conditional calls in Python
  3. 2008.06.23 command Line Arguments
  4. 2008.06.23 pickle & unpickle
  5. 2008.05.11 function dic
  6. 2008.02.15 Dive Into Python -- python from novice to pro
  7. 2008.02.15 Python的应用
  8. 2008.01.11 regular expression
  9. 2008.01.03 connect to DB in python
  10. 2007.12.04 python
Programming/Python | Posted by 알 수 없는 사용자 2009. 4. 9. 22:35

pickle in binary file

Pickler Protocols and cPickle

In recent Python releases, the pickler introduced the notion of protocolsstorage formats for pickled data. Specify the desired protocol by passing an extra parameter to the pickling calls (but not to unpickling calls: the protocol is automatically determined from the pickled data):

pickle.dump(object, file, protocol)

Pickled data may be created in either text or binary protocols. By default, the storage protocol is text (also known as protocol 0). In text mode, the files used to store pickled objects may be opened in text mode as in the earlier examples, and the pickled data is printable ASCII text, which can be read (it's essentially instructions for a stack machine).

The alternative protocols (protocols 1 and 2) store the pickled data in binary format and require that files be opened in binary mode (e.g., rb, wb). Protocol 1 is the original binary format; protocol 2, added in Python 2.3, has improved support for pickling of new-style classes. Binary format is slightly more efficient, but it cannot be inspected. An older option to pickling calls, the bin argument, has been subsumed by using a pickling protocol higher than 0. The pickle module also provides a HIGHEST_PROTOCOL variable that can be passed in to automatically select the maximum value.

One note: if you use the default text protocol, make sure you open pickle files in text mode later. On some platforms, opening text data in binary mode may cause unpickling errors due to line-end formats on Windows:

>>> f = open('temp', 'w')                  # text mode file on Windows
>>> pickle.dump(('ex', 'parrot'), f) # use default text protocol
>>> f.close( )
>>>
>>> pickle.load(open('temp', 'r')) # OK in text mode
('ex', 'parrot')
>>> pickle.load(open('temp', 'rb')) # fails in binary
Traceback (most recent call last):
File "<pyshell#337>", line 1, in -toplevel-
pickle.load(open('temp', 'rb'))
...lines deleted...
ValueError: insecure string pickle

One way to sidestep this potential issue is to always use binary mode for your files, even for the text pickle protocol. Since you must open files in binary mode for the binary pickler protocols anyhow (higher than the default 0), this isn't a bad habit to get into:

>>> f = open('temp', 'wb')                 # create in binary mode
>>> pickle.dump(('ex', 'parrot'), f) # use text protocol
>>> f.close( )
>>>
>>> pickle.load(open('temp', 'rb'))
('ex', 'parrot')
>>> pickle.load(open('temp', 'r'))
('ex', 'parrot')
Programming/Python | Posted by 알 수 없는 사용자 2009. 4. 1. 04:54

"Short-circuit" conditional calls in Python

# Normal statement-based flow control
if <cond1>:   func1()
elif <cond2>: func2()
else:         func3()

# Equivalent "short circuit" expression
(<cond1> and func1()) or (<cond2> and func2()) or (func3())

# Example "short circuit" expression
>>> x = 3
>>> def pr(s): return s
>>> (x==1 and pr('one')) or (x==2 and pr('two')) or (pr('other'))
'other'
>>> x = 2
>>> (x==1 and pr('one')) or (x==2 and pr('two')) or (pr('other'))
'two'

from :http://gnosis.cx/publish/programming/charming_python_13.html

Programming/Python | Posted by 알 수 없는 사용자 2008. 6. 23. 12:13

command Line Arguments

sys.argv
is an array that contains the command-line arguments passed to the program

for example,
>> python file2DIC -f CityU.utf8.seg -d CityU.DIC

then the value of sys.argv is :
sys.argv [ 0 ] = "file2DIC"
sys.argv [ 1 ] = "-f"
sys.argv [ 2 ] = "CityU.utf8.seg"
sys.argv [ 3 ] = "-d"
sys.argv [ 4 ] = "CityU.DIC"

remember, if you'd like to use it in python, don't forget to add the following lines in your programming:
import sys
Programming/Python | Posted by 알 수 없는 사용자 2008. 6. 23. 12:10

pickle & unpickle

import pickle

# pickling list
f = open ( "pickle.dat", "w" )
mixList = [ 'one', 1 'two', 2 ]
pickle.dump ( mixList, f )
f.close ()

#unpickling
f = open ( "pickle.dat", "r" )
mixList = pickle.load ( f )
print mixList
f.close ()
Programming/Python | Posted by 알 수 없는 사용자 2008. 5. 11. 02:07

function dic

Functions as object

switch = {
            'one' : function1,
            'two' : function2,
            'three' : function3
            }

try:
    result = switch[choice]
except KeyError :
    print 'I didn't understand your choice.'
else :
    result ()


Programming/Python | Posted by 알 수 없는 사용자 2008. 2. 15. 23:16

Dive Into Python -- python from novice to pro

[Dive Into Python]

Dive Into Python is a Python book for experienced programmers. You can buy a printed copy, read it online, or download it in a variety of formats. It is also available in multiple languages.

The complete text is available online. You can read the revision history to see what's new. Updated 20 May 2004

Programming/Python | Posted by 알 수 없는 사용자 2008. 2. 15. 22:40

Python的应用



Zope-应用服务器

Plone-内容管理系统

Django-鼓励快速开发的web framework

Twisted - Python Network Application Framework Python的网络应用程序框架

TurboGears - 另一个Web应用快速开发框架

Bit Torrent - 著名的BT下载工具

2006年的Google编程大赛已经将Python作为参赛语言之一
Programming/Python | Posted by 알 수 없는 사용자 2008. 1. 11. 16:47

regular expression

Simple Patterns

  • metacharacters :
    • .  ^  $  *  +  ?  {  [  ]  \  |  (  )
  • [abc] = [a-c] : will match any of the characters "a", "b" or "c"
  • metacharacters are not active inside classes.
    • [abc$] : will match any of the characters of "a", "b", "c" or "$"
    • "$" is usually a metacharacter, but inside a character class it's stripped of its special nature.
  • not within the range-- complementing
    • [^5] : will match any character except "5"
  • .
    • match anything except a newline character
  • "\" is used to escape all the metacharactes so you can still match them in patterns
    • \[ or \\ : match "[" or "\"
  • \d = [0-9]
    • match any decimal digit
  • \D = [^0-9]
    • match any non-digit character
  • \s = [\t\n\r\f\v]
    • match nay whitespace character
  • \S = [^\t\n\r\f\v]
    • match any non-whitespace character
  • \w = [a-zA-Z0-9_]
    • match any alphanumeric character
  • \W = [^a-zA-Z0-9_]
    • match any non-alphanumeric character


Programming/Python | Posted by 알 수 없는 사용자 2008. 1. 3. 23:28

connect to DB in python

import MySQLdb

# Create a connection object and create a cursor
Con = MySQLdb.Connect ( host = "kle.postech.ac.kr", user="joe", passwd = "secrete", db = "onto_web" )
Cursor = Con.cursor ()

#Make SQL string and execute it
sql = "show tables"
Cursor.execute ( sql )

#Fetch all results from the cursor into a sequence and close the connection
Results = Cursor.fetchall ()
Con.close ()
Programming/Python | Posted by 알 수 없는 사용자 2007. 12. 4. 09:10

python




From www.python.or.kr
( These are PDF files. )

1. 개요
2. 파이썬 기초
3. while문
4. list, for문
5. tuples
6. functions
7. dictionary
8. files
9. exceptions
10. classes
11. module
12. string
13. regular expression
14. Tkinter GUI
15. 수치연산 모듈
16. Internet Programming