#
#!/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()
环境
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="准备新建子项目名称>>" />
<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 运行本的命令得到返回参数:
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'])
$页面编码设置,文件第一行加入
#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
#设置返回下载属性
response = HttpResponse(mimetype='text/csv')
response['Content-Disposition'] = 'attachment; filename=%s.csv' % filename
$使用session和数据库
http://www.woodpecker.org.cn/obp/django/django-stepbystep/newtest/doc/tut05.html
$新建子项目
settiongs.py + INSTALLED_APPS = '总包名.新建包名',
manage.py startapp 包名
$表链关系
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)
当然使用 shell 一行就能解决!
不过 需求使用 python 所以
#coding=UTF-8
import os
import re
import sys
#递归遍历指定的目录
#param:
# array -- 递归寄存数组
# level -- 递归的层数,用这个参数来控制打印的缩进
# 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 "请输入参数 -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()
/* 可用系统表,得到元数据 */
user_tables
all_tab_columns
->constraint_type
'U':唯一;'P':主键;'R':外键;
->constraint_name
user_indexes
all_constraints
查看表结构
desc
问题描述:
dic_flow (id,status_id,status_name)
post_status(id,pre_status_id,post_status_id)
关系: dic_flow.status_id-(1.1)>post_status.pre_status_id,post_status_id
要结果: 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
子查询(性能优化版)
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
再唠叨句
左连(性能再优化版)
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 中 运行参数手机
_copyright=$(python findIncBizDataFile.py copyright)
可以参考:
cygWin: http://blog.chinaunix.net/u/19782/showart_408547.html
1>. 安装cygwin,他可以满足你大部分Linux命令的需要。真是个好东西。
下载的地址为: http://www.cygwin.com/
完成后设置 bin 到path中 就ok!
安装 gvim gvim72.exe
http://www.vim.org/
grep
文本查找: grep -nr "string with space" ./*
find
find . -name '*[html !py]'
文件根据时间: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 每次执行会询问?
-exec 直接运行!
|args 直接运行 但上得结果默认插入后句最后。
gvim 乱码解决:http://www.cnblogs.com/shipfi/archive/2008/04/07/1140025.html
把以下设置添加到.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
光标定位:
参考:
http://blog.csdn.net/lanphaday/archive/2007/10/28/1852726.aspx
http://www.zhuaxia.com/item/852269537
在web抓取,有些网站会判断过度读取会有验证码输入:
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]
有时间 在学习下 匹配。
摘要: 此方法在快速建立项目原型上有很大帮助(这最关键的就是速度,用的好原型很快就能建立起来)
先确定下“快速原型”环境定义:
使用:ant+hibernate_xdoclet+hibernate_create_table
过程:定义bean(xdoclet) -> hbm映射文件 -> hibernate.hbm2ddl.auto... 阅读全文
MySQL
存取控制包含2个阶段:
- 阶段1:服务器检查是否允许你连接。
- 阶段2:假定你能连接,服务器检查你发出的每个请求。看你是否有足够的权限实施它。例如,如果你从数据库表中选择(select)行或从数据库删除表,服务器确定你对表有SELECT权限或对数据库有DROP权限。
参考 :
5.8. MySQL用户账户管理
1.权限查看
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.权限设置
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。两个账户均为超级用户账户,具有完全的权限可以做任何事情。一个账户
('monty'@'localhost')只用于从本机连接时。另一个账户('monty'@'%')可用于从其它主机连接。
mysql> GRANT RELOAD,PROCESS ON *.* TO 'admin'@'localhost';
该账户只用于从本机连接。授予了RELOAD和PROCESS管理权限。这些权限允许admin用户执行mysqladmin
reload、mysqladmin
refresh和mysqladmin
flush-xxx命令,以及mysqladmin
processlist。未授予访问数据库的权限。你可以通过GRANT语句添加此类权限。
mysql> GRANT USAGE ON *.* TO 'dummy'@'localhost';
一个账户有用户名dummy,没有密码。该账户只用于从本机连接。未授予权限。通过GRANT语句中的USAGE权限,你可以创建账户而不授予任何权限。它可以将所有全局权限设为'N'。假定你将在以后将具体权限授予该账户。
下面的例子创建3个账户,允许它们访问专用数据库。每个账户的用户名为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数据库,但只能从本机访问。
· 第2个账户可以访问expenses数据库,但只能从主机whitehouse.gov访问。
· 第3个账户可以访问customer数据库,但只能从主机server.domain访问。
要想不用GRANT设置custom账户,使用INSERT语句直接修改 授权表:
5.8.3. 从MySQL删除用户账户
DROP USER user;
|