#
#!/usr/bin/python
#coding=UTF-8
#sql 參考
#http://www.kitebird.com/articles/pydbapi.html
import cx_Oracle
import re,os,sys
logTxt="./log"
dataFile = "./data.txt"
fileCharset = "GBK"
dbCharset = "GBK"
username = "...."
userpwd = "...."
host ="...."
dbname="...."
#dbname="..."
MuById_SQL="select count(*) from mn00_tbl_music tm where tm.id=:mid"
CRById_SQL="select tc.music_id from mn05_tbl_copyright tc where tc.copyright_id=:crid"
CRById_In_Music_SQL="update mn05_tbl_copyright set music_id=:mid where copyright_id=:crid"
CRByMNameAndAName_SQL=" select tc.copyright_id from mn05_tbl_copyright tc where (tc.actor_src_name=':acName' and tc.music_src_name=':muName' ) or (tc.actor_name=':acName' and tc.music_name=':muName') ";
# 60056601146|失意酒(童安格)|失意酒(童安格)|10030149
# CRID | 編輯后歌曲(歌手)|歌曲(歌手) |MID
def action(strs):
result = selectSql( subSql(MuById_SQL, "mid", strs[3]) )
if( int(result[0][0])>0):
actionCR(strs[0],strs[3])
if( strs[1] and strs[1]!="" ):
actionName(strs[1],strs[3])
if( strs[2] and strs[2]!="" ):
actionName(strs[2],strs[3])
def actionName(mu_ac,mid):
p = re.compile("(.*)\((.*)\)")
m = p.match( mu_ac )
if(m):
crmaSql=subSql(CRByMNameAndAName_SQL,"muName", m.group(1))
crmaSql=subSql(crmaSql,"acName",m.group(2))
for row in selectSql( crmaSql ):
actionCR(row[0].__str__() ,mid)
#count = cursor.rowcount
def actionCR(crid,mid):
result = selectSql( subSql(CRById_SQL, "crid", crid ) )
if( result[0][0]==None or int(result[0][0])==0):
upCr=subSql(CRById_In_Music_SQL,"crid",crid)
upCr=subSql(upCr,"mid",mid)
if(execSql(upCr)>0):
_log('match crid='+crid+' muid='+mid)
def subSql(strSql,subName,subValue):
p = re.compile(":"+subName)
return p.sub( subValue, strSql)
def selectSql(sql):
print 'select='+sql
cursor.execute(sql.encode(dbCharset))
return cursor.fetchall()
def execSql(sql):
print 'exec='+sql
cursor.execute(sql.encode(dbCharset))
connection.commit()
return cursor.rowcount
def _log(show):
print show
log = 'echo '+show+' >> '+logTxt
os.system(log)
try:
connStr=username+"/"+userpwd+"@"+dbname
print connStr
connection = cx_Oracle.connect(connStr)
cursor = connection.cursor()
file = open(dataFile)
if(len(sys.argv)==2):
startRow=int(sys.argv[1])
else:
startRow=1
row = 1
for str in file.readlines():
if(row>=startRow):
str = re.compile("\n").sub( "", str )
strs = unicode(str, fileCharset).split("|")
_log('start row='+row.__str__()+' crid='+strs[0]+' muid='+strs[3])
try:
action(strs)
except:
_log('err row='+row.__str__())
_log('end row='+row.__str__()+' crid='+strs[0]+' muid='+strs[3])
row+=1
finally:
file.close()
cursor.close()
connection.close()
環(huán)境
py2.5
Django
eclipes + py 插件
ant + 自定義build
MySQLdb
參考:
http://www.woodpecker.org.cn/obp/django/django-stepbystep/newtest/doc/tut06.html
pybuild.properties
project.name=myTurtle
<?xml version="1.0"?>
<project default="" basedir=".">
<property file="pybuild.properties" />
<target name="startproject">
<exec executable="cmd">
<arg value="/c" />
<arg value="django-admin.py startproject " />
<arg value="${project.name}" />
</exec>
</target>
<target name="buildCProject">
<input addproperty="newProject" message="準(zhǔn)備新建子項目名稱>>" />
<exec executable="cmd">
<arg value="/c" />
<arg value=" python ./${project.name}/manage.py startapp ${newProject}" />
</exec>
</target>
<target name="run">
<exec executable="cmd">
<arg value="/c" />
<arg value="start python ./${project.name}/manage.py runserver" />
</exec>
</target>
<target name="end">
<exec executable="cmd">
<arg value="/c" />
<arg value="tskill python" />
</exec>
</target>
<target name="db_init">
<exec executable="cmd">
<arg value="/c" />
<arg value=" python ./${project.name}/manage.py syncdb" />
</exec>
</target>
</project>
$亂碼問題
settings.py
LANGUAGE_CODE = 'zh-cn'
TIME_ZONE = 'Asia/Shanghai'
request.encoding='utf8'
$python 運行本的命令得到返回參數(shù):
fp=os.popen( "dir ", "r ")
x=fp.read()
$掛載資源
urls.py patterns + (r'^$', 'test.test.index'),
$頁面返回
from django.http import HttpResponse
def index(request):
return HttpResponse("Hello, Django.")
$取得html訪問值
if request.POST.has_key('a')
int(request.POST['a'])
$頁面編碼設(shè)置,文件第一行加入
#coding=UTF-8
$使用模板
settings.py TEMPLATE_DIRS + './templates', #模板路徑
#方法1
from django.shortcuts import render_to_response
def index(request):
return render_to_response('list.html', {'address': address})
#templates/list.html
<h2>通訊錄</h2>
<table border="1">
<tr><th>姓名</th><th>地址</th></tr>
{% for user in address %}
<tr>
<td>{{ user.name }}</td>
<td>{{ user.address }}</td>
</tr>
{% endfor %}
</table>
#方法2
from django.http import HttpResponse
from django.template import loader, Context
response = HttpResponse(mimetype='text/csv')
response['Content-Disposition'] = 'attachment; filename=%s.csv' % filename
t = loader.get_template('csv.html')
c = Context({'data': address,})
response.write(t.render(c))
return response
#HTML
{% for row in data %}"{{ row.0|addslashes}}", "{{ row.1|addslashes}}",{% endfor %}
$提供下載
from django.template import loader, Context
#設(shè)置返回下載屬性
response = HttpResponse(mimetype='text/csv')
response['Content-Disposition'] = 'attachment; filename=%s.csv' % filename
$使用session和數(shù)據(jù)庫
http://www.woodpecker.org.cn/obp/django/django-stepbystep/newtest/doc/tut05.html
$新建子項目
settiongs.py + INSTALLED_APPS = '總包名.新建包名',
manage.py startapp 包名
$表鏈關(guān)系
http://www.woodpecker.org.cn/obp/django/django-faq/model-api.html
迭代
python ./demo.py /xx/google/g2/
#coding=UTF-8
import os
import re
import sys
dir='/home/liukaiyi/桌面/py/'
todir='/home/liukaiyi/桌面/test/'
if os.path.isdir(dir)==False:
os.mkdir(dir)
if os.path.isdir(todir)==False:
os.mkdir(todir)
usedir=sys.argv[1]
dir+=usedir+'/'
todir+=usedir+'/'
mk_dir = '/'
for tdir in dir.split('/'):
if tdir=="":
continue
mk_dir += tdir+'/'
if os.path.isdir(mk_dir)==False:
os.mkdir(mk_dir)
mk_todir = '/'
for tdir in todir.split('/'):
if tdir=="":
continue
mk_todir += tdir+"/"
if os.path.isdir(mk_todir)==False:
os.mkdir(mk_todir)
def action(path,tpath,file):
print '>>>>>>>>>>>>>>'+path,tpath,file
def listAll(rootpath):
tpath = todir+re.sub(dir,"",rootpath)
if os.path.isdir(tpath)==False:
os.mkdir(tpath)
print 'mkdir>'+tpath
for file in os.listdir(rootpath):
if os.path.isfile(rootpath+file):
filename = ".".join(file.split('.')[0:-1])
bool = False
for tfile in os.listdir(tpath) :
if ".".join(tfile.split('.')[0:-1]) == filename :
bool=True
if bool==False:
action(rootpath,tpath,file)
for file in os.listdir(rootpath):
path = rootpath+file+'/'
if os.path.isdir(path):
listAll(path)
listAll(dir)
當(dāng)然使用 shell 一行就能解決!
不過 需求使用 python 所以
#coding=UTF-8
import os
import re
import sys
#遞歸遍歷指定的目錄
#param:
# array -- 遞歸寄存數(shù)組
# level -- 遞歸的層數(shù),用這個參數(shù)來控制打印的縮進(jìn)
# path == 遍歷起始絕對路徑
#return:
# array 返回 {"type":文件類型(f|d),"path":物理文件路徑name":文件名"exp":文件擴展名,稱,"level":深度}
# 參考:http://www.javaeye.com/topic/116670
_comms = {'-s':'策略','-p':'查找路徑','-c':'替換命令'}
_mapSub = ("path", "name", "exp","type","level")
_mapSub_re = {}
for tmp in _mapSub:
_mapSub_re[tmp] = re.compile("\$\s*\{\s*"+tmp+"\s*\}")
def listAll(array, level, path):
for file in os.listdir(path):
if os.path.isfile(path + "\\" + file):
fe = file.split(".")
path = re.sub("\\\\", "/", path)
if len(fe)>=2:
array.append({_mapSub[3]:"f", _mapSub[0]:path, _mapSub[1]:".".join(fe[0:-1]), _mapSub[2]:fe[-1], _mapSub[4]:str(level + 1)})
else:
array.append({_mapSub[3]:"d", _mapSub[0]:path, _mapSub[1]:file, _mapSub[2]:"", _mapSub[4]:str(level + 1)})
else:
array.append({_mapSub[3]:"d", _mapSub[0]:path, _mapSub[1]:file, _mapSub[2]:"", _mapSub[4]:str(level + 1)})
listAll(array, level + 1, path + "\\" + file)
def _main():
if len(sys.argv)==1:
print "請輸入?yún)?shù) -s 策略 -p 查找路徑 -c 替換命令 "
print ' 如 :listdir.py -p . -s findMp3 -c "ls ${path}/${name}.${exp}" '
exit(0)
argvs = sys.argv[1:]
#argvs = '-s&findMp3&-p&.&-c&"ls ${path}/${name}.${exp}"'.split("&")
for tc in _comms.keys():
for i in range(len(argvs)):
if(argvs[i]==tc):
_comms[tc]=argvs[i+1]
#
reLGPath = re.compile("^\s*\\.")
if reLGPath.match(_comms['-p']):
_comms['-p'] = os.path.abspath(_comms['-p'])
_comms['-p'] = re.sub("\\\\","/",_comms['-p'])
script = _comms['-s']+'()'
for fmap in eval(script):
tcomm = _comms['-c']
for tk in _mapSub_re.keys():
tcomm = _mapSub_re[tk].sub(fmap[tk]+"", tcomm+"")
#print tcomm
os.system(tcomm)
#***********************************************************************************************
# 策略 添加
#***********************************************************************************************
#查找 mp3 策略
def findMp3():
array = []
mp3Array = []
listAll(array, 0, _comms['-p'])
p = re.compile("[mM][pP]3")
for tmap in array:
# 類型 文件 擴展名 mp3
if tmap[_mapSub[3]] == "f" and p.match(tmap[_mapSub[2]]) :
mp3Array.append(tmap)
return mp3Array
#***********************************************************************************************
# 測試代碼 listdir.py -p . -s findMp3 -c "ls ${path}/${name}.${exp}"
# 可替換 ${path} ${name} ${exp} ${level} ${type}
#***********************************************************************************************
_main()
/* 可用系統(tǒng)表,得到元數(shù)據(jù) */
user_tables
all_tab_columns
->constraint_type
'U':唯一;'P':主鍵;'R':外鍵;
->constraint_name
user_indexes
all_constraints
查看表結(jié)構(gòu)
desc
問題描述:
dic_flow (id,status_id,status_name)
post_status(id,pre_status_id,post_status_id)
關(guān)系: dic_flow.status_id-(1.1)>post_status.pre_status_id,post_status_id
要結(jié)果: pre_status_id,pre_status_name,post_status_id,post_status_name
create table dic_flow(id int,status_id int , status_name varchar(20));
create table post_status(id int ,pre_status_id int ,post_status_id int);
insert into dic_flow values(1,1,'google');
insert into dic_flow values(2,3,'baidu');
insert into dic_flow values(3,8,'yahoo');
insert into post_status values(1,8,3);
select
tp.pre_status_id as pre_status_id,
( select td.status_name from dic_flow td where td.status_id=tp.pre_status_id) as pre_status_name ,
tp.post_status_id as post_status_id,
( select td.status_name from dic_flow td where td.status_id=tp.post_status_id) as post_status_name
from post_status tp;
+---------------+-----------------+----------------+------------------+
| pre_status_id | pre_status_name | post_status_id | post_status_name |
+---------------+-----------------+----------------+------------------+
| 8 | yahoo | 3 | baidu |
+---------------+-----------------+----------------+------------------+
1 row in set (0.00 sec)
Skynet(309290723) 21:11:57
子查詢(性能優(yōu)化版)
select
tp.pre_status_id as pre_status_id,
td1.status_name as pre_status_name ,
tp.post_status_id as post_status_id,
td2.status_name as post_status_name
from post_status tp , dic_flow td1 , dic_flow td2
where tp.pre_status_id=td1.status_id and tp.post_status_id=td2.status_id ;
Skynet(309290723) 21:12:08
+---------------+-----------------+----------------+------------------+
| pre_status_id | pre_status_name | post_status_id | post_status_name |
+---------------+-----------------+----------------+------------------+
| 8 | yahoo | 3 | baidu |
+---------------+-----------------+----------------+------------------+
Skynet(309290723) 21:16:35
再嘮叨句
左連(性能再優(yōu)化版)
select
tp.pre_status_id as pre_status_id,
td1.status_name as pre_status_name ,
tp.post_status_id as post_status_id,
td2.status_name as post_status_name
from post_status tp
left join dic_flow td1 on tp.pre_status_id=td1.status_id
left join dic_flow td2 on tp.post_status_id=td2.status_id ;
shell 中 運行參數(shù)手機
_copyright=$(python findIncBizDataFile.py copyright)
可以參考:
cygWin: http://blog.chinaunix.net/u/19782/showart_408547.html
1>. 安裝cygwin,他可以滿足你大部分Linux命令的需要。真是個好東西。
下載的地址為: http://www.cygwin.com/
完成后設(shè)置 bin 到path中 就ok!
安裝 gvim gvim72.exe
http://www.vim.org/
grep
文本查找: grep -nr "string with space" ./*
find
find . -name '*[html !py]'
文件根據(jù)時間:find . -type f -amin +1
find . -name '*c*' -type f -maxdepth 2
find . -name '*ction*.java' -exec cat -n {} ; |less
find + grep
find . -type f -name "*.txt" |xargs grep 'use this' -n
查詢比 file 文件新的.
find . -type f -name "*.txt" -newer ./lib/hadoop-0.18.1/kfs-0.1.LICENSE.txt|xargs grep 'sf' -ln
find+cp
find ./lib -type f -name '*.jar' -exec cp -rf {} ./mn_mod_admin/WebRoot/WEB-INF/lib ;
替換:
find -type f -name Root -exec sed -i 's/59.151.33.197/cvs.xunjienet.cn/g' {} ;
管道:
-ok 每次執(zhí)行會詢問?
-exec 直接運行!
|args 直接運行 但上得結(jié)果默認(rèn)插入后句最后。
gvim 亂碼解決:http://www.cnblogs.com/shipfi/archive/2008/04/07/1140025.html
把以下設(shè)置添加到.vimrc中。
set encoding=utf-8
set fileencodings=utf-8,chinese,latin-1
if has("win32")
set fileencoding=chinese
else
set fileencoding=utf-8
endif
language message zh_CN.utf-8
"解決菜單亂碼
source $VIMRUNTIME/delmenu.vim
source $VIMRUNTIME/menu.vim
命令集合:
:ls :n
:edit filename
:split :vsplit Ctrl-w w
gvim +11 file.txt
顯示行號:set nu
替換:http://ks.cn.yahoo.com/question/1408012501095.html
:%s/four/4/g
光標(biāo)定位:
參考:
http://blog.csdn.net/lanphaday/archive/2007/10/28/1852726.aspx
http://www.zhuaxia.com/item/852269537
在web抓取,有些網(wǎng)站會判斷過度讀取會有驗證碼輸入:


java
package Main;
import java.awt.image.BufferedImage;
import java.net.URL;
import javax.imageio.ImageIO;
public class Test {
public static void main(String[] args) throws Exception {
BufferedImage img = ImageIO.read(new URL("file:/C:/src/python/gve/22.jpeg"));
int imgWith = img.getWidth();
int imgHeight = img.getHeight();
int bgint = img.getRGB(0, 0);
for (int y = 0; y < imgHeight; y++) {
System.out.println();
for (int x = 0; x < imgWith; x++) {
System.out.print(getBit(img, x, y,bgint));
}
}
}
private static char getBit(BufferedImage img, int x, int y,int bgint) {
int intCurtColor;
intCurtColor = img.getRGB(x, y);
return (intCurtColor == bgint) ? '0' : ' ';
}
}
000 000 0 0 00 0 0 0 0
0 000 00 0 0 0 0 0
00 00 0 0 00 0 00 0 00 0
0000 0 0 0 0 0 0 00 0 0
00 0 0 0 00 0
00 0 00 0 0 00 0
0 0 0 0 0 0 00 0
00 00 0 0 0 0 0 00 0
0 0 0 00 0 0 0 0
0 00 0 0 0 00
0 0 00 0 0 00
00 0 0 00 0 0 0 0
00 0 0 0 00 0 0 0 0 0
0 0 00 0 0 00 0000
python
#encoding=utf-8
import Image,ImageEnhance,ImageFilter
import sys
image_name = "./22.jpeg"
#去處 干擾點
im = Image.open(image_name)
im = im.filter(ImageFilter.MedianFilter())
enhancer = ImageEnhance.Contrast(im)
im = enhancer.enhance(2)
im = im.convert('1')
#im.show() #測試查看
s = 12 #啟始 切割點 x
t = 2 #啟始 切割點 y
w = 10 #切割 寬 +y
h = 15 #切割 長 +x
im_new = []
for i in range(4): #驗證碼切割
im1 = im.crop((s+w*i+i*2,t,s+w*(i+1)+i*2,h))
im_new.append(im1)
#im_new[0].show()#測試查看
xsize, ysize = im_new[0].size
gd = []
for i in range(ysize):
tmp=[]
for j in range(xsize):
if( im_new[0].getpixel((j,i)) == 255 ):
tmp.append(1)
else:
tmp.append(0)
gd.append(tmp)
#看效果
for i in range(ysize):
print gd[i]
比如 "0"
[ 1, 1, 0, 0, 0, 0, 1, 1, 1, 1]
[0, 0, 0, 0, 0, 0, 0, 1, 1, 1]
[0, 0, 0, 1, 1, 0, 0, 0, 0, 1]
[0, 0, 1, 1, 1, 1, 0, 0, 0, 1]
[0, 0, 1, 1, 1, 1, 0, 0, 0, 1]
[0, 0, 1, 1, 1, 1, 0, 0, 0, 1]
[0, 0, 1, 1, 1, 1, 0, 0, 0, 1]
[0, 0, 1, 1, 1, 1, 0, 0, 0, 1]
[0, 0, 1, 1, 1, 1, 0, 0, 0, 1]
[0, 0, 0, 1, 1, 1, 0, 0, 0, 1]
[0, 0, 0, 1, 1, 0, 0, 0, 0, 1]
[ 1, 0, 0, 0, 0, 0, 0, 0, 1, 1]
[ 1, 1, 0, 0, 0, 0, 0, 1, 1, 1]
有時間 在學(xué)習(xí)下 匹配。
摘要: 此方法在快速建立項目原型上有很大幫助(這最關(guān)鍵的就是速度,用的好原型很快就能建立起來)
先確定下“快速原型”環(huán)境定義:
使用:ant+hibernate_xdoclet+hibernate_create_table
過程:定義bean(xdoclet) -> hbm映射文件 -> hibernate.hbm2ddl.auto... 閱讀全文
MySQL
存取控制包含2個階段:
- 階段1:服務(wù)器檢查是否允許你連接。
- 階段2:假定你能連接,服務(wù)器檢查你發(fā)出的每個請求。看你是否有足夠的權(quán)限實施它。例如,如果你從數(shù)據(jù)庫表中選擇(select)行或從數(shù)據(jù)庫刪除表,服務(wù)器確定你對表有SELECT權(quán)限或?qū)?shù)據(jù)庫有DROP權(quán)限。
參考 :
5.8. MySQL用戶賬戶管理
1.權(quán)限查看
mysql> show grants for 'root'@'localhost' ;
+---------------------------------------------------------------------+
| Grants for root@localhost |
+---------------------------------------------------------------------+
| GRANT ALL PRIVILEGES ON *.* TO 'root'@'localhost' WITH GRANT OPTION |
+---------------------------------------------------------------------+
1 row in set (0.06 sec)
2.權(quán)限設(shè)置
5.8.2. 向MySQL增加新用戶賬戶
mysql> GRANT ALL PRIVILEGES ON *.* TO 'monty'@'localhost'
-> IDENTIFIED BY 'some_pass';
mysql> GRANT ALL PRIVILEGES ON *.* TO 'monty'@'%'
-> IDENTIFIED BY 'some_pass' WITH GRANT OPTION;
其中兩個賬戶有相同的用戶名monty和密碼some_pass。兩個賬戶均為超級用戶賬戶,具有完全的權(quán)限可以做任何事情。一個賬戶
('monty'@'localhost')只用于從本機連接時。另一個賬戶('monty'@'%')可用于從其它主機連接。
mysql> GRANT RELOAD,PROCESS ON *.* TO 'admin'@'localhost';
該賬戶只用于從本機連接。授予了RELOAD和PROCESS管理權(quán)限。這些權(quán)限允許admin用戶執(zhí)行mysqladmin
reload、mysqladmin
refresh和mysqladmin
flush-xxx命令,以及mysqladmin
processlist。未授予訪問數(shù)據(jù)庫的權(quán)限。你可以通過GRANT語句添加此類權(quán)限。
mysql> GRANT USAGE ON *.* TO 'dummy'@'localhost';
一個賬戶有用戶名dummy,沒有密碼。該賬戶只用于從本機連接。未授予權(quán)限。通過GRANT語句中的USAGE權(quán)限,你可以創(chuàng)建賬戶而不授予任何權(quán)限。它可以將所有全局權(quán)限設(shè)為'N'。假定你將在以后將具體權(quán)限授予該賬戶。
下面的例子創(chuàng)建3個賬戶,允許它們訪問專用數(shù)據(jù)庫。每個賬戶的用戶名為custom,密碼為obscure。
mysql> GRANT SELECT,INSERT,UPDATE,DELETE,CREATE,DROP
-> ON bankaccount.*
-> TO 'custom'@'localhost'
-> IDENTIFIED BY 'obscure';
mysql> GRANT SELECT,INSERT,UPDATE,DELETE,CREATE,DROP
-> ON expenses.*
-> TO 'custom'@'whitehouse.gov'
-> IDENTIFIED BY 'obscure';
mysql> GRANT SELECT,INSERT,UPDATE,DELETE,CREATE,DROP
-> ON customer.*
-> TO 'custom'@'server.domain'
-> IDENTIFIED BY 'obscure';
這3個賬戶可以用于:
· 第1個賬戶可以訪問bankaccount數(shù)據(jù)庫,但只能從本機訪問。
· 第2個賬戶可以訪問expenses數(shù)據(jù)庫,但只能從主機whitehouse.gov訪問。
· 第3個賬戶可以訪問customer數(shù)據(jù)庫,但只能從主機server.domain訪問。
要想不用GRANT設(shè)置custom賬戶,使用INSERT語句直接修改 授權(quán)表:
5.8.3. 從MySQL刪除用戶賬戶
DROP USER user;
|