日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

常用代码参考模板

發布時間:2024/5/14 编程问答 43 豆豆
生活随笔 收集整理的這篇文章主要介紹了 常用代码参考模板 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

常用代碼參考模板

  • Python
    • 使用multiprocessing模塊并發任務
    • Python操作數據庫
    • subprocess執行cmd命令
  • Shell
    • 文件存在符&邏輯判斷符&運算符
    • 串行
    • 并發
    • 免交互登錄
    • 輸入數字運行相應命令 case ...when...
  • SQL
    • 賬號授權
    • 表操作
    • 常見函數使用---字符函數
    • 常見函數使用---日期函數
    • 常見函數使用---聚合函數

github https://github.com/yangle92/
知識庫

Python

使用multiprocessing模塊并發任務

from multiprocessing import Pool import time def ssh (ip):print ("login in %s "%ip)print(time.strftime("%Y-%m-%d %H:%M:%S", time.localtime()))time.sleep(10) if __name__ == '__main__':with Pool(50) as p:p.map(ssh,["192.168.1."+ str(i) for i in range(100)])

Python操作數據庫

import pymysql def Mysql_handle(result):# 打開數據庫連接db = pymysql.Connect(host='192.168.1.50',port=3306,user='root',passwd="root",db='test',charset="utf8")# 使用 cursor() 方法創建一個游標對象 cursorcursor = db.cursor()try:# 執行sql語句cursor.executemany("INSERT INTO csdn(url,title) VALUES (%s,%s)", result)# 提交到數據庫執行db.commit()except Exception as e:# 如果發生錯誤則回滾print(e)db.rollback()finally:sql2 = 'select * from csdn'cursor.execute(sql2)results = cursor.fetchall()print(results)# 關閉數據庫連接db.close()

subprocess執行cmd命令

# -*- coding:UTF-8 -*- import subprocess cmd='ifconfig' p = subprocess.Popen(cmd, stdout=subprocess.PIPE,shell=True) r1=p.stdout.readlines() r2=[i.strip().decode('gb2312') for i in r1 if i != b'\r\n' ] print(r2)

Shell

文件存在符&邏輯判斷符&運算符

-e ----判斷對象是否存在 -d ----判斷對象是否存在,并且為目錄 -f ----判斷對象是否存在,并且為常規文件 -L ----判斷對象是否存在,并且為符號鏈接 -h ----判斷對象是否存在,并且為軟鏈接 -s ----判斷對象是否存在,并且長度不為0 -r ----判斷對象是否存在,并且可讀 -w ----判斷對象是否存在,并且可寫 -x ----判斷對象是否存在,并且可執行 -c file ----    文件為字符特殊文件為真 -b file ----    文件為塊特殊文件為真 -t file ----    當文件描述符(默認為1)指定的設備為終端時為真 -O ----判斷對象是否存在,并且屬于當前用戶 -G ----判斷對象是否存在,并且屬于當前用戶組 -nt ----判斷file1是否比file2新 [ "/data/file1" -nt "/data/file2" ] -ot ---- 判斷file1是否比file2舊 [ "/data/file1" -ot "/data/file2" ] [ -x "/bin/ls" ] :判斷/bin/ls是否存在并有可執行權限 [ -n "$var" ] :判斷$var變量是否有值 [ "$a" = "$b" ] :判斷$a$b是否相等 -ne —比較兩個參數是否不相等 -lt —參數1是否小于參數2 -le —參數1是否小于等于參數2 -gt —參數1是否大于參數2 -ge —參數1是否大于等于參數2 -e 文件存在 -a 文件存在(已被棄用) -f 被測文件是一個regular文件(正常文件,非目錄或設備) -s 文件長度不為0 -b 被測對象是塊設備 -c 被測對象是字符設備 -p 被測對象是管道 -h 被測文件是符號連接 -L 被測文件是符號連接 -S(大寫) 被測文件是一個socket -t 關聯到一個終端設備的文件描述符。用來檢測腳本的stdin[-t0][-t1]是一個終端 -r 文件具有讀權限,針對運行腳本的用戶 -w 文件具有寫權限,針對運行腳本的用戶 -x 文件具有執行權限,針對運行腳本的用戶 -u set-user-id(suid)標志到文件,即普通用戶可以使用的root權限文件,通過chmod +s file實現 -k 設置粘貼位 -O 運行腳本的用戶是文件的所有者 -G 文件的group-id和運行腳本的用戶相同 -N 從文件最后被閱讀到現在,是否被修改 f1 -nt f2 文件f1是否比f2新 f1 -ot f2 文件f1是否比f2舊 f1 -ef f2 文件f1和f2是否硬連接到同一個文件 二元比較操作符,比較變量或比較數字 整數比較: -eq 等于 if [ "$a" -eq "$b" ] -ne 不等于 if [ "$a" -ne "$b" ] -gt 大于 if [ "$a" -gt "$b" ] -ge 大于等于 if [ "$a" -ge "$b" ] -lt 小于 if [ "$a" -lt "$b" ] -le 小于等于 if [ "$a" -le "$b" ] < 小于(需要雙括號) (( "$a" < "$b" )) <= 小于等于(...) (( "$a" <= "$b" )) > 大于(...) (( "$a" > "$b" )) >= 大于等于(...) (( "$a" >= "$b" )) 字符串比較: = 等于 if [ "$a" = "$b" ] ===等價 != 不等于 if [ "$a" = "$b" ] < 小于,在ASCII字母中的順序:if [[ "$a" < "$b" ]]if [ "$a" \< "$b" ] #需要對<進行轉義 > 大于 -z 字符串為null,即長度為0 -n 字符串不為null,即長度不為0 命令的間邏輯關系:邏輯與: &&第一個條件為假時,第二條件不用再判斷,最終結果已經有;第一個條件為真時,第二條件必須得判斷;邏輯或: ||

串行

#!/bin/bash Njob=15 #任務總數 for ((i=0; i<$Njob; i++)); do {echo "progress $i is sleeping for 3 seconds zzz…"sleep 3 } done echo -e "time-consuming: $SECONDS seconds" #顯示腳本執行耗時

并發

#!/bin/bash Njob=15 #任務總數 for ((i=0; i<$Njob; i++)); do {echo "progress $i is sleeping for 3 seconds zzz…"sleep 3 } & done wait echo -e "time-consuming: $SECONDS seconds" #顯示腳本執行耗時

免交互登錄

#!/bin/bash ############################################################## #創建10個用戶,并分別設置密碼,密碼要求10位且包含大小寫字母以及數字 #最后需要把每個用戶的密碼存在指定文件中#前提條件:安裝mkpasswd命令 ############################################################## #生成10個用戶的序列(00-09) for u in `seq -w 0 09` do#創建用戶useradd user_$u#生成密碼p=`mkpasswd -s 0 -l 10` #從標準輸入中讀取密碼進行修改(不安全)echo $p|passwd --stdin user_$u#常規修改密碼echo -e "$p\n$p"|passwd user_$u#將創建的用戶及對應的密碼記錄到日志文件中echo "user_$u $p" >> /tmp/userpassworddone done

輸入數字運行相應命令 case …when…

#!/bin/bash ############################################################## #輸入數字運行相應命令 ############################################################## echo "*cmd menu* 1-date 2-ls 3-who 4-pwd 0-exit " while : do #捕獲用戶鍵入值read -p "please input number :" nn1=`echo $n|sed s'/[0-9]//'g` #空輸入檢測 if [ -z "$n" ]thencontinuefi #非數字輸入檢測 if [ -n "$n1" ]thenexit 0fibreak done case $n in1)date;;2)ls;;3)who;;4)pwd;;0)break;;#輸入數字非1-4的提示*)echo "please input number is [1-4]" esac

SQL

賬號授權

在此界面輸入sqlplus /nolog回車 再輸入conn /as sysdba回車 select name from v$datafile; 查看數據庫文件存放地址,一會指定表空間路徑需要,回車后 1、創建表空間 create tablespace table1loggingdatafile 'D:\APP\ADMINISTRATOR\ORADATA\ITSM1\table1.dbf'size 1000mautoextend onnext 100m maxsize 2048mextent management local;create temporary tablespace table1_temptempfile 'D:\APP\ADMINISTRATOR\ORADATA\ITSM1\table1_temp.dbf'size 100mautoextend onnext 100m maxsize 2048mextent management local;3、創建用戶、并指定表空間 create user dbadmin identified by dbadmin default tablespace table1 temporary tablespace table1_temp;4、授權、登錄、dba權限等 grant connect,resource,dba to dbadmin; grant unlimited tablespace to dbadmin;

表操作

5、表操作/*創建表*/CREATE TABLE table1(Id_P int,LastName varchar(255),FirstName varchar(255),Address varchar(255),City varchar(255))/*修改表字段*/alter table table1 drop column Id_Palter table table1 add Id_P int ;/*創建序列*/create sequence Id_P start with 1 increment by 1; /*增加表內容*/INSERT INTO table1 VALUES (value1,value2,value3,...);/*制定單元格插入內容*/INSERT INTO table1(LastName,FirstName,Address,City) values ('Tony','Ann','America center park','NewYork' );INSERT INTO table1(LastName,FirstName,Address,City) values ('Anix','Aoteman','Earth ','Japanese' );INSERT INTO table1 values ('Tony','Ann','America center park','NewYork',ID_P.Nextval );INSERT INTO table1 values ('Anix','Aoteman','Earth ','Japanese',ID_P.Nextval );/*刪除表內容*/delete from table1 where table1.ID_P is null /*修改表內容*/UPDATE table1 set LastName='Obu' where table1.ID_P=2/*查詢表*/select * from table1 where table1.ID_P=2

常見函數使用—字符函數

--字符函數 select substr('abcdefg',1,5)substr, --字符串截取instr('abcdefg','bc') instr, --查找子串'Hello'||'World' concat, --連接trim(' wish ') trim, --去前后空格rtrim('wish ') rtrim, --去后面空格ltrim(' wish') ltrim, --去前面空格trim(leading 'w' from 'wish') deleteprefix, --去前綴trim(trailing 'h' from 'wish') deletetrailing, --去后綴trim('w' from 'wish') trim1,ascii('A') A1, ascii('a') A2, --ascii(轉換為對應的十進制數)chr(65) C1, chr(97) C2, --chr(十進制轉對應字符)length('abcdefg') len, --length lower('WISH')lower, upper('wish')upper, initcap('wish')initcap, --大小寫變換replace('wish1','1','youhappy') replace, --替換translate('wish1','1','y')translate, --轉換,對應一位(前面的位數大于等于后面的位數)translate('wish1','sh1','hy')translate1,concat('11','22') concat          --連接from dual;

常見函數使用—日期函數

--日期 --年 yyyy yyy yy year --月 month mm mon month --日+星期 dd ddd(一年中第幾天) dy day --小時 hh hh24 --分 mi --秒 ssselect to_char(sysdate,'yyyy-mm-dd hh24:mi:ss')currenttime, to_char(sysdate,'yyyy') year,to_char(sysdate,'mm') month,to_char(sysdate,'dd') day,to_char(sysdate,'day') week,to_char(sysdate,'hh24')hour,to_char(sysdate,'mi') minute,to_char(sysdate,'ss') second from dual;select to_date('2009-07-04 05:02:01','yyyy-mm-dd hh24:mi:ss')currenttime,to_char(to_date('2009-07-04 05:02:01','yyyy-mm-dd hh24:mi:ss'),'yyyy')year,to_char(to_date('2009-07-04 05:02:01','yyyy-mm-dd hh24:mi:ss'),'mm')month,to_char(to_date('2009-07-04 05:02:01','yyyy-mm-dd hh24:mi:ss'),'dd') day,to_char(to_date('2009-07-04 05:02:01','yyyy-mm-dd hh24:mi:ss'),'day') week,to_char(to_date('2009-07-04 05:02:01','yyyy-mm-dd hh24:mi:ss'),'day','NLS_DATE_LANGUAGE=American') week, --設置語言to_char(to_date('2009-07-04 05:02:01','yyyy-mm-dd hh24:mi:ss'),'hh24')hour,to_char(to_date('2009-07-04 05:02:01','yyyy-mm-dd hh24:mi:ss'),'mi') minute,to_char(to_date('2009-07-04 05:02:01','yyyy-mm-dd hh24:mi:ss'),'ss') second from dual;--months_betweenselect months_between(to_date('03-31-2014','MM-DD-YYYY'),to_date('12-31-2013','MM-DD-YYYY')) "MONTHS"FROM DUAL;--next_day select sysdate today, next_day(sysdate,6) nextweek from dual;--時間區間 select cardid, borrowdate from borrow where to_date(borrowdate,'yyyy-mm-dd hh24:mi:ss') between to_date('2014-02-01 00:00:00','yyyy-mm-dd hh24:mi:ss') and to_date('2014-05-01 00:00:00','yyyy-mm-dd hh24:mi:ss'); --interval 間隔 select to_char(sysdate,'yyyy-mm-dd hh24:mi:ss') currenttime,to_char(sysdate - interval '7' year,'yyyy-mm-dd hh24:mi:ss') intervalyear, to_char(sysdate - interval '7' month,'yyyy-mm-dd hh24:mi:ss') intervalMonth, to_char(sysdate - interval '7' day,'yyyy-mm-dd hh24:mi:ss') intervalday, to_char(sysdate - interval '7' hour,'yyyy-mm-dd hh24:mi:ss') intervalHour, to_char(sysdate - interval '7' minute,'yyyy-mm-dd hh24:mi:ss') intervalMinute, to_char(sysdate - interval '7' second,'yyyy-mm-dd hh24:mi:ss') intervalSecond from dual; --add_months 增加月份 select add_months(sysdate,12) newtime from dual;--extract select extract(month from sysdate) "This Month", extract(year from add_months(sysdate,36)) " Years" from dual;

常見函數使用—聚合函數

--count select count(1) as count from student;--效率最高 select count(*) as count from student; select count(distinct score) from student; --avg --distinct|all select avg(score) score from student; select avg(distinct score) from student; select classno,avg(score) score from student group by classno;--max --distinct|all select max(score) from student; select classno, max(score) score from student group by classno;--min --distinct|all select min(score) from student; select classno, min(score) score from student group by classno;--stddev(standard deviation)標準差 select stddev(score) from student; select classno, stddev(score) score from student group by classno;--sum select sum(score) from student; select classno, sum(score) score from student group by classno;--median--中位數 select median(score) from student; select classno, median(score) score from student group by classno;

總結

以上是生活随笔為你收集整理的常用代码参考模板的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。