@coolfish
2017-04-10T03:47:29.000000Z
字数 6415
阅读 1484
# 安装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').jsonprint response.b.c # 通过点号取值,结果为[1, 2, 3, 4]print response('$.a') # 通过object path取值,结果为1for 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 requestclass 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').jsonpithy.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 dbpithy_db = db("mysql://user:password@host:port/database?charset=utf8", 'pithy_db')# 使用从模块导入方式from pithy_db import o_order, sessionquery_result = session.query(o_order).all()# 不使用从模块导入的方式o_order = pithy_db.o_ordersession = pithy_db.sessionsession.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_secondprint HumanDateTime(1490842267.11111).timestamp_microsecondprint HumanDateTime('2017-02-02 12:12:12.1111').add_day(1).timestamp_microsecondprint 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').dayprint HumanDateTime('2017-02-02 12:12:12 1111').yearprint HumanDateTime('2017-02-02 12:12:12 1111').secondprint HumanDateTime('2017-02-02 12:12:12 1111').date()
优化JSON字符串和字典的取值方式
# 1、操作JSON的KEYfrom pithy.tools import JSONProcessordict_data = {'a': 1, 'b': {'a': [1, 2, 3, 4]}}json_data = json.dumps(dict_data)result = JSONProcessor(json_data)print result.a # 结果:1print result.b.a # 结果:[1, 2, 3, 4]# 这里使用的object path的取值方式,详细语法见:http://objectpath.org/reference.htmlfor i in result('$..a[@>3]'): # 结果: 4print i# 2、操作字典的KEYdict_data = {'a': 1, 'b': {'a': [1, 2, 3, 4]}}result = JSONProcessor(dict_data)print result.a # 1print result.b.a # [1, 2, 3, 4]