@coolfish
2017-04-10T03:47:29.000000Z
字数 6415
阅读 1398
# 安装
pip install pithy --extra-index-url http://pypi.dev.elenet.me/eleme/eleme --trusted-host pypi.dev.elenet.me
# 更新
pip install -U pithy --extra-index-url http://pypi.dev.elenet.me/eleme/eleme --trusted-host pypi.dev.elenet.me
# 安装
pip install pithy --extra-index-url http://pypi.elenet.me/eleme/eleme --trusted-host pypi.elenet.me
# 更新
pip install -U pithy --extra-index-url http://pypi.elenet.me/eleme/eleme --trusted-host pypi.elenet.me
(pyevn)➜pithy-cli init
请选择项目类型,输入interface或者app: # 输入项目类型
请输入项目名称,如sargeras-api-test: # 输入项目名称
开始创建hgbac项目
开始渲染...
开始渲染 interface/.gitignore.jinja
开始渲染 interface/apis/__init__.py.jinja
开始渲染 interface/common/__init__.py.jinja
开始渲染 interface/db/__init__.py.jinja
开始渲染 interface/README.MD.jinja
开始渲染 interface/requirements.txt.jinja
开始渲染 interface/test_suites/__init__.py.jinja
生成成功,请使用编辑器打开该项目
进入项目文件夹内,执行git init,然后与gitlib上的项目仓库关联
from pithy import request
@request(url='http://xxxx/login', method='post')
def login(phone=None, password=None):
"""
登录
"""
headers = {'xx': xx, 'xx': xx}
body = {
'phone': phone,
'password': password
}
# 使用
response = login('13111111111', '123abc').to_json() # 解析json字符,输出为字典
response = login('13111111111', '123abc').json # 解析json字符,输出为字典
response = login('13111111111', '123abc').to_content() # 输出为字符串
response = login('13111111111', '123abc').content # 输出为字符串
response = login('13111111111', '123abc').get_cookie() # 输出cookie对象
response = login('13111111111', '123abc').cookie # 输出cookie对象
# 结果取值, 假设此处response = {'a': 1, 'b': { 'c': [1, 2, 3, 4]}}
response = login('13111111111', '123abc').json
print response.b.c # 通过点号取值,结果为[1, 2, 3, 4]
print response('$.a') # 通过object path取值,结果为1
for i in response('$..c[@>3]'): # 通过object path取值,结果为选中c字典里大于3的元素
print i
from pithy import request
@request(url='http://xxxx/login')
def login(phone=None, password=None):
"""
登录
"""
headers = {'xx': xx, 'xx': xx}
params = {
'phone': phone,
'password': password
}
from pithy import request
@request(url='http://xxxx/login', method='post', data_type='form')
def login(phone=None, password=None):
"""
登录
"""
headers = {'xx': xx, 'xx': xx}
body = {
'phone': phone,
'password': password
}
from pithy import request
class PithyApp(object):
"""
PithyApp 相关接口
"""
def __init__(self):
self.session = None # 定义session,方便后面操作, 同时也可以不定义self.session,会自动创建
self.base_url = 'http://xxx.com' # 指定base_url,也可以不指定
@request(url='login', method='form')
def _login(self, phone=None, password=None):
"""
登录接口
"""
headers = {'xx': xx, 'xx': xx}
body = {
'phone': phone,
'password': password
}
def login(self, phone=None, password=None):
"""
登录
"""
login_response = self._login('13111111111', '111111').to_json()
self.session.cookies.set('session', login_response.data.session)
@request(url='pithy-webapi/get_info')
def get_info(self):
"""
获取信息
"""
pass
# 使用,此处两个接口使用同一request session请求
pithy = Pithy()
pithy.login('13111111111', '123abc').json
pithy.get_info().json
from pithy import request, make_session
@request(url=config.BASE_URL + '/pithy-webapi/login', method='form')
def login(phone=None, password=None, session=None):
"""
登录
"""
headers = {'xx': xx, 'xx': xx}
body = {
'phone': phone,
'password': password
}
# 调用
session = make_session()
response = login('13111111111', '123abc', session=session).json
这里使用sqlalchemy orm,使用方法如下:
# 因执行了sys.module['pithy_db'] = xxx,所以当加载后,可以直接from pithy_db import ...
from pithy import db
pithy_db = db("mysql://user:password@host:port/database?charset=utf8", 'pithy_db')
# 使用从模块导入方式
from pithy_db import o_order, session
query_result = session.query(o_order).all()
# 不使用从模块导入的方式
o_order = pithy_db.o_order
session = pithy_db.session
session.query(o_order).all()
from pithy import HumanDateTime
# 解析时间戳
print repr(HumanDateTime(1490842267))
print HumanDateTime(1490842267000)
print HumanDateTime(1490842267.11111)
print HumanDateTime(1490842267111.01)
# 解析字符串格式日期
print HumanDateTime('2017-02-02')
print HumanDateTime('Thu Mar 30 14:21:20 2017')
print HumanDateTime(time.ctime())
print HumanDateTime('2017-3-3')
print HumanDateTime('3/3/2016')
print HumanDateTime('2017-02-02 00:00:00')
# 解析datetime或date类型时间
print HumanDateTime(datetime(year=2018, month=11, day=30, hour=11))
print HumanDateTime(date(year=2018, month=11, day=30))
# 增加减少时间
print HumanDateTime('2017-02-02').add_day(1)
print HumanDateTime('2017-02-02').sub_day(1)
print HumanDateTime('2017-02-02').add_hour(1)
print HumanDateTime('2017-02-02').sub_hour(1)
print HumanDateTime('2017-02-02').add(days=1, hours=1, weeks=1, minutes=1, seconds=6)
print HumanDateTime('2017-02-02').sub(days=1, hours=1, weeks=1, minutes=1, seconds=6)
# 转换为时间戳
print HumanDateTime(1490842267.11111).timestamp_second
print HumanDateTime(1490842267.11111).timestamp_microsecond
print HumanDateTime('2017-02-02 12:12:12.1111').add_day(1).timestamp_microsecond
print HumanDateTime('2017-02-02 12:12:12 1111').add_day(1).timestamp_microsecond
# 比较大小
print HumanDateTime('2017-02-02 12:12:12 1111') < HumanDateTime('2017-02-02 12:12:11 1111')
print HumanDateTime('2017-02-02 12:12:12 1111') < HumanDateTime('2017-02-02 12:13:11 1111')
print HumanDateTime('2017-02-02 12:12:12 1111') < '2017-02-02 12:11:11'
print HumanDateTime('2017-02-02 12:12:12 1111') < '2017-02-02 12:13:11 1111'
print HumanDateTime('2017-02-02 12:12:12 1111') == '2017-02-02 12:13:11 1111'
print HumanDateTime('2017-02-02 12:12:12 1111') == '2017-02-02 12:13:12 1111'
print HumanDateTime('2017-02-02 12:12:12 1111') <= '2017-02-02 12:13:11 1111'
print HumanDateTime('2017-02-02 12:12:12 1111') >= '2017-02-02 12:13:11 1111'
print HumanDateTime('2017-02-02 12:12:12 1111') != time.time()
print HumanDateTime('2017-02-02 12:12:12 1111') <= time.time()
print HumanDateTime('2017-02-02 12:12:12 1111') >= time.time()
# 约等于或者接近
print HumanDateTime('2017-02-02 12:12:12 1111').approach('2017-02-02 12:12:11 1111')
print HumanDateTime('2017-02-02 12:12:12 1111').approach('2017-02-02 12:12:10 1111')
print HumanDateTime('2017-02-02 12:12:12 1111').approach('2017-02-02 12:12:10 1111', offset=2)
print HumanDateTime('2017-02-02 12:12:12 1111').approach('2017-02-02 12:12:14 1111', offset=2)
# 调用datetime的方法和属性
print HumanDateTime('2017-02-02 12:12:12 1111').day
print HumanDateTime('2017-02-02 12:12:12 1111').year
print HumanDateTime('2017-02-02 12:12:12 1111').second
print HumanDateTime('2017-02-02 12:12:12 1111').date()
优化JSON字符串和字典的取值方式
# 1、操作JSON的KEY
from pithy.tools import JSONProcessor
dict_data = {'a': 1, 'b': {'a': [1, 2, 3, 4]}}
json_data = json.dumps(dict_data)
result = JSONProcessor(json_data)
print result.a # 结果:1
print result.b.a # 结果:[1, 2, 3, 4]
# 这里使用的object path的取值方式,详细语法见:http://objectpath.org/reference.html
for i in result('$..a[@>3]'): # 结果: 4
print i
# 2、操作字典的KEY
dict_data = {'a': 1, 'b': {'a': [1, 2, 3, 4]}}
result = JSONProcessor(dict_data)
print result.a # 1
print result.b.a # [1, 2, 3, 4]