?
Integrating python and java:
Py4J: Py4J enables Python programs runningin a Python interpreter to dynamically access Java objects in a JVM. Methodsare called as if the Java objects resided in the Python interpreter and Javacollections can be accessed through standard Python collection methods. Py4Jalso enables Java programs to call back Python objects.
Here is a brief example of what you
can do with Py4J. The following Python program creates a?java.util.Random?instance from a JVM and calls some of its
methods. It also accesses a custom Java class,?AdditionApplicationto add the generated numbers.
>>>from py4j.java_gateway importJavaGateway
>>>gateway =JavaGateway()?????????????????? # connect to the JVM
>>>random = gateway.jvm.java.util.Random()?? # create a java.util.Random instance
>>>number1 = random.nextInt(10)????????????? # call the Random.nextInt method
>>>number2 = random.nextInt(10)
>>>print(number1, number2)
(2, 7)
>>>addition_app = gateway.entry_point?????????????? # get the AdditionApplication instance
>>>value = addition_app.addition(number1,
number2)) # call the addition
method
>>>print(value)
9
This is the Java program that was executing
at the same time (no code was generated and no tool was required to run these
programs). The?AdditionApplication
app?instance is
the?gateway.entry_pointin the previous code snippet.Note that the Java program must be started before executing the Python codeabove. In other words, the Py4J does not start a JVM.
import py4j.GatewayServer;
public class AdditionApplication {
? public int addition(intfirst, int second) {
??? returnfirst + second;
? }
? public static void main(String[] args) {
??? AdditionApplication app= new AdditionApplication();
??? // app is now the gateway.entry_point
??? GatewayServer server= new GatewayServer(app);
??? server.start();
? }
}
Source:https://www.py4j.org/index.html
?
?
SQLalchemy:SQLAlchemy is the Python SQL toolkit and Object Relational Mapper that givesapplication developers the full power and flexibility of SQL.
SQLAlchemy連接數(shù)據(jù)庫(kù)(MyAQL, SQLite, PostgreSQL),創(chuàng)建數(shù)據(jù)表
SQLAlchemy中的一個(gè)session可以看作是一個(gè)transaction,每個(gè)操作(基本上)對(duì)應(yīng)一條或者多條SQL語句,這些SQL語句需要發(fā)送到數(shù)據(jù)庫(kù)服務(wù)器才能被真正執(zhí)行,而整個(gè)transaction需要commit才能真正生效,如果沒有提交,一旦程序掛了,所有未提交的事務(wù)都會(huì)被回滾到事務(wù)開始之前的狀態(tài)。
Commit就是告訴數(shù)據(jù)庫(kù)服務(wù)器提交事務(wù)
Flush預(yù)提交,等于提交到數(shù)據(jù)庫(kù)內(nèi)存,還未寫入數(shù)據(jù)庫(kù)文件;
Commit就是把內(nèi)存里面的東西直接寫入,可以提供查詢了。
?
?
Psycopg2是python的postgresql數(shù)據(jù)庫(kù)接口
?
?
ORM
Object Relational Mapping 對(duì)象關(guān)系映射。簡(jiǎn)單來說,ORM將數(shù)據(jù)庫(kù)中的表與面向?qū)ο笳Z言中的類建立了一種對(duì)應(yīng)關(guān)系。這樣,我們要操作數(shù)據(jù)庫(kù),數(shù)據(jù)庫(kù)中的表或者表中的一條記錄就可以直接通過操作類或者類實(shí)例來完成。
?
連接數(shù)據(jù)庫(kù):
Fromsqlalchemy import create_engine
Engine= create_engine(‘mysql+mysqldb://root@localhost:3306/blog’)
Print(engine)
連接了默認(rèn)運(yùn)行在3306端口的MySQL中的blog數(shù)據(jù)庫(kù)
https://www.cnblogs.com/mrchige/p/6389588.html
?
?
?
?
?
?
?
CRUD是指在做計(jì)算處理時(shí)的增加(Create)、讀取查詢(Retrieve)、更新(Update)和刪除(Delete).CRUD主要被用在描述軟件系統(tǒng)中數(shù)據(jù)庫(kù)或者持久層的基本操作功能。
增刪改查
?
?
?
?
?
?
APScheduler是一個(gè)python第三方庫(kù),用來提供python后臺(tái)程序。包含四個(gè)組件,分別是:
Trigger:任務(wù)觸發(fā)器組件,提供任務(wù)觸發(fā)方式
Job stores:任務(wù)商店組件,提供任務(wù)保存方式
Executors:任務(wù)調(diào)度組件,提供任務(wù)調(diào)度方式
Schedulers:任務(wù)調(diào)度組件,提供任務(wù)工作方式
from apscheduler.schedulers.blocking importBlockingScheduler
importtime
# 實(shí)例化一個(gè)調(diào)度器
scheduler =BlockingScheduler()
def job1():
??? print "%s: 執(zhí)行任務(wù)"? % time.asctime()
# 添加任務(wù)并設(shè)置觸發(fā)方式為3s一次
scheduler.add_job(job1,'interval', seconds=3)
# 開始運(yùn)行調(diào)度器
scheduler.start()
OBS:
桶bucket是OBS 中存儲(chǔ)對(duì)象的容器。對(duì)象存儲(chǔ)提供了基于桶和對(duì)象的扁平化存儲(chǔ)方式,桶中的所有對(duì)象都處于同一邏輯層級(jí),去除了文件系統(tǒng)中多層級(jí)樹形目錄結(jié)構(gòu)。
AK Access Key ID: 訪問密鑰ID。與私有訪問密鑰關(guān)聯(lián)的唯一標(biāo)識(shí)符;訪問密鑰ID和私有訪問密鑰一起使用,對(duì)請(qǐng)求進(jìn)行加密簽名。
SK Secret Access
Key:與訪問密鑰ID結(jié)合使用的密鑰,對(duì)請(qǐng)求進(jìn)行加密簽名,可標(biāo)識(shí)發(fā)送方,并防止請(qǐng)求被修改。
Uuid 通用唯一識(shí)別碼(Universally Unique Identifier, 縮寫UUID)是用于計(jì)算機(jī)體系中以識(shí)別信息數(shù)目的一個(gè)128位標(biāo)識(shí)符
相關(guān)術(shù)語:全局唯一標(biāo)識(shí)符(GUID)根據(jù)標(biāo)準(zhǔn)方法生成,不依賴中央機(jī)構(gòu)的注冊(cè)和分配,UUID具有唯一性,這與其他大多數(shù)編號(hào)方案不同。重復(fù)UUID碼概率接近零,可以忽略不計(jì)。
IAM identity andaccess management is a framework of policies and technologies for ensuring thatthe proper people in an enterprise have the appropriate access to technologyresources.
Type 不會(huì)認(rèn)為子類是父類
Isinstance 會(huì)認(rèn)為子類是父類
?
?
?
?
?
?
deffunc(*num, **words):
?????? print(“num:”+str(num))
?????? print(“word:”+str(words))
func(1,3,5,7,word=”pyhton”,another_word=”java”)
#num:(1,3,5,7)
#words:{‘word’:’python’, ‘a(chǎn)nother_word’:’java’}
*用來表示接受一個(gè)tuple,**用來表示字典
?
?
?
?
Connectionreset by peer:
The
remote server has sent you a RST packet,whichindicates an immediate dropping of the connection, rather than the usualhandshake. This bypasses the normal half-closed state transition. I like thisdescription: connection reset by peer is the TCP/IP equivalent of slamming thephone back on the hook. It’s more polite than merely not replying, leaving onehanging. But it’s not the FIN-ACK expected of the truly polite TCP/IPconverseur.
?
?
Pycharm整體縮進(jìn)的快捷鍵(sublime不適用)
選中多行,按tab進(jìn)行縮進(jìn),按tab+shift去除縮進(jìn)
?
Pythonmock
Mock是python中一個(gè)用于支持單元測(cè)試的庫(kù),它的主要功能是使用mock對(duì)象代替掉指定的python對(duì)象,以到達(dá)模擬對(duì)象的行為。
Fromunittest import mock
Importrequests
Def c(url):
?????? Resp = requests.get(url)
?????? # further process with resp
使用一個(gè)mock對(duì)象替換掉上面的requests.get函數(shù),然后執(zhí)行函數(shù)c時(shí),c調(diào)用requests.get的返回值就能夠由我們的mock對(duì)象來決定,而不需要服務(wù)器的參與。
Mock對(duì)象是mock模塊中最重要的概念,mock對(duì)象就是Mock模塊中的一個(gè)類的實(shí)例,這個(gè)類的實(shí)例可以用來替換其他的python對(duì)象,來達(dá)到模擬的效果,mock類的定義如下:
ClassMock(…………)
Mock對(duì)象的一般用法是這樣的:
[if !supportLists]1.??????[endif]找到你要替換的對(duì)象,這個(gè)對(duì)象可以是一個(gè)類,或者一個(gè)函數(shù),或者是一個(gè)類實(shí)例。
[if !supportLists]2.??????[endif]然后實(shí)例化MOCK類得到一個(gè)mock對(duì)象,并且設(shè)置這個(gè)mock對(duì)象的行為,比如被調(diào)用的時(shí)候返回什么值,被訪問成員的時(shí)候返回什么值等。
[if !supportLists]3.??????[endif]使用這個(gè)mock對(duì)象替換掉我們想替換的對(duì)象,也就是步驟1中確定的對(duì)象。
[if !supportLists]4.??????[endif]之后就可以開始寫測(cè)試代碼,這個(gè)時(shí)候我們可以保證我們替換掉的對(duì)象在測(cè)試用例執(zhí)行的過程中行為和我們預(yù)設(shè)的一樣。
Mock類定義的參數(shù):name;這個(gè)是用來命名一個(gè)mock對(duì)象,只是起到標(biāo)識(shí)的作用,當(dāng)你print一個(gè)mock對(duì)象的時(shí)候,可以看到它的name。
?????? Return_value:這個(gè)字段可以指定一個(gè)值,(或者對(duì)象),當(dāng)mock對(duì)象被調(diào)用時(shí),如果side_effect函數(shù)返回的是DEFAULT,則對(duì)mock對(duì)象的調(diào)用會(huì)返回return_value指定的值
?????? Side_effect:這個(gè)參數(shù)指向一個(gè)可調(diào)用的對(duì)象,一般就是函數(shù),當(dāng)mock對(duì)象被調(diào)用時(shí),如果該函數(shù)返回值不是DEFAULT時(shí),那么以該函數(shù)的返回值作為mock對(duì)象調(diào)用的返回值。
注意,如果測(cè)試對(duì)象中有celery方法時(shí),需要對(duì)其進(jìn)行mock處理
Mock對(duì)象的自動(dòng)創(chuàng)建:當(dāng)訪問一個(gè)mock對(duì)象不存在的屬性時(shí),mock會(huì)自動(dòng)建立一個(gè)子mock對(duì)象,并且把正在訪問的屬性指向它,這個(gè)功能對(duì)于實(shí)現(xiàn)多級(jí)屬性的mock很方便。
?
Mock:https://segmentfault.com/a/1190000002965620
?
Pytest:http://www.itdecent.cn/p/a754e3d47671
一個(gè)debug:
Importerror: cannot import name‘thread_local’ from ‘IAM’ (unknown location)
The name of the module is the problem,python already has a module named IAM. I changed the name into IAMfile to avoidthis error.
From datetime import date
Import time
D1 = date(2018, 7, 31)
D2 = date.today()
D3 = date.fromtimestamp(time.time())
獲取星期。標(biāo)準(zhǔn)格式1-7
D2.isoweekday()
獲取星期,格式0-6
D2.weekday()
格式化顯示
D3.strftime(‘%Y/%m/%d’)
From datetime import time
T = time(1,2,3) #01:02:03
格式化顯示
t.strftime(‘%H:%M:%S’)
from datetime import datetime
本地當(dāng)前時(shí)間,帶時(shí)區(qū)
dt3 = datetime.now()
from datetime import datetime, timedelta
d1 = datetime(2018, 7, 31, 20, 49, 55)
d2 = datetime(2018, 7, 31, 20, 55, 23)
delta2 = timedelta(days=1,hours=1)
d3 = d1 + delta2
提取天數(shù):delta2.days
提取除天數(shù)以外的秒數(shù):delta2.seconds
總共的秒數(shù)delta2.total_seconds()
兩個(gè)date或者datetime對(duì)象相減時(shí)可以返回一個(gè)timedelta對(duì)象