我正在用flask写一个小的REST服务:
from flask import Flask
app = Flask(__name__)
app.config['count']=0
@app.route("/count")
def get_count():
app.config['count']+=1
return str(app.config['count']-1)
@app.route("/reset")
def reset():
app.config['count']=0
return str(app.config['count'])
if __name__ == "__main__":
app.run(debug = True)
Run Code Online (Sandbox Code Playgroud)
顺序调用/count应该返回0, 1, 2, ...。呼叫/reset应重置计数器。
为了对其进行测试,我遵循了烧瓶和鼻子教程 ::
#!/usr/bin/python
import flask_server as server
import unittest
from nose.tools import assert_equals
class TestUnitBase(unittest.TestCase):
def setUp(self):
print "Setting up!"
server.app.testing=True
self.app = server.app.test_client()
def test_count_is_zero_in_new_server(self):
"""/count should return 0 for a new server"""
response = self.app.get("/count")
data, status …Run Code Online (Sandbox Code Playgroud) 以下 JSON 模式描述了经纬度坐标的有效 JSON:
{
"title": "coordinates",
"type": "object",
"properties": {
"longitude": {
"type": "number",
"minimum": -180,
"maximum":180,
"exclusiveMinimum": false,
"exclusiveMaximum": false
},
"latitude": {
"type": "number",
"minimum": -180,
"maximum":180,
"exclusiveMinimum": false,
"exclusiveMaximum": false
}
},
"required": ["longitude", "latitude"],
"additionalProperties":false
}
Run Code Online (Sandbox Code Playgroud)
该required设置将该latitude属性设置为强制。
有没有办法为latitude密钥定义别名,以便客户端可以使用latitude或lat- 但不能两者都使用,也不能同时使用两者?
我想知道我的浏览器选项卡是否处于焦点状态,以及浏览器本身是在前台还是在后台。
是否有一个javascript接口可以查询浏览器的前台/后台状态?
更新按照下面Palpatim的回答,我找到了这个关于页面可见性 API 的实用 HTML5Rocks 教程。
我正在使用nosetest工具来断言python unittest:
...
from nose.tools import assert_equals, assert_almost_equal
class TestPolycircles(unittest.TestCase):
def setUp(self):
self.latitude = 32.074322
self.longitude = 34.792081
self.radius_meters = 100
self.number_of_vertices = 36
self.vertices = polycircles.circle(latitude=self.latitude,
longitude=self.longitude,
radius=self.radius_meters,
number_of_vertices=self.number_of_vertices)
def test_number_of_vertices(self):
"""Asserts that the number of vertices in the approximation polygon
matches the input."""
assert_equals(len(self.vertices), self.number_of_vertices)
...
Run Code Online (Sandbox Code Playgroud)
当我跑步时python setup.py test,我得到一个弃用警告:
...
Asserts that the number of vertices in the approximation polygon ...
/Users/adamatan/personal/polycircles/polycircles/test/test_polycircles.py:22:
DeprecationWarning: Please use assertEqual instead.
assert_equals(len(self.vertices), self.number_of_vertices)
ok
...
Run Code Online (Sandbox Code Playgroud)
我assertEqual在鼻子工具中找不到任何东西.这个警告来自哪里,我该如何解决?
在PostgreSQL中,使用IN运算符检查字段是否在给定列表中:
SELECT * FROM stars WHERE star_type IN ('Nova', 'Planet');
Run Code Online (Sandbox Code Playgroud)
什么是SQLAlchemy等效的INSQL查询?
indb_session.query(Star).filter(Star.star_type in ('Nova', 'Planet'))
Run Code Online (Sandbox Code Playgroud)
查询返回空.
or_db_session.query(Star).\
filter(or_(
Star.star_type == 'Nova',
Star.star_type == 'Planet'
))
Run Code Online (Sandbox Code Playgroud)
此查询返回正确的结果,但它不优雅且难以扩展.
我试图根据数组中整数变量的存在使用 SQLALchemy ORM 对象过滤 PostgreSQL 记录,但我找不到正确的方法。
我有一个带有整数数组的PostgreSQL 表:
my_db=> \d test_arr;
Table "public.test_arr"
Column | Type | Modifiers
----------+-----------+-------------------------------------------------------
id | integer | not null default nextval('test_arr_id_seq'::regclass)
partners | integer[] |
Run Code Online (Sandbox Code Playgroud)
该表包含一些值:
my_db=> SELECT * FROM test_arr;
id | partners
----+----------
12 | {1,2,3}
13 | {2,3,4}
14 | {3,4,5}
15 | {4,5,6}
(4 rows)
Run Code Online (Sandbox Code Playgroud)
查询表,其包含数字的行2中partners排列使用PostgreSQL中完成ANY关键字:
my_db=> SELECT * FROM test_arr WHERE 2 = ANY(partners);
id | partners
----+----------
12 | …Run Code Online (Sandbox Code Playgroud) 在 PostgreSQL 中,显示域的属性是使用 完成的\dD,例如:
\dD dom_reason
List of domains
Schema | Name | Type | Modifier | Check
....
Run Code Online (Sandbox Code Playgroud)
在 DataGrip 中,\尚不支持表示法。有没有办法使用直接 SQL 查询域属性?
根据手册,Protobuf 3.0.0支持JSON序列化:
JSON 中定义良好的编码,作为二进制原始编码的替代方案。
我尝试过什么
json.dumps(instance)这提出了TypeError(repr(o) + " is not JSON serializable")instance.to_json()(或类似的)功能如何将 Python 原型对象序列化为 JSON?
我正在尝试在docker容器上安装Flask应用程序.该应用程序需要通过pip安装一些软件包.
我在端口9000上使用本地(主机,而不是容器)pip repo.因此,我尝试了以下方法:
pip install -i 127.0.0.1:9000/simple my_custom_package
Run Code Online (Sandbox Code Playgroud)
此调用适用于主机,但当我在容器上运行时,我得到:
Collecting my_custom_package
Url '127.0.0.1:9000/simple/my_custom_package/' is ignored.
It is either a non-existing path or lacks a specific scheme.
Run Code Online (Sandbox Code Playgroud)
我做了一些尝试curl,但似乎容器不能简单地访问主机上的端口.
如何从OSX上的Docker容器访问主机端口?
我有一个应用程序和一个服务器端推送发件人.当新通知到达时,服务器会发送一个空推送消息,该消息仅包含徽章更新.
当应用程序在后台时,徽章已成功更新.但是,当应用程序位于前台时,徽章根本不会更新 - 推送会传递给应用程序,而应用程序会将其丢弃.
明显的解决方法是从应用程序中捕获推送并更新徽章.由于某些技术原因,这需要一些时间才能生效(开发时间,应用商店检查时间,不经常升级的用户等)
我想知道是否有办法环绕这个并使用服务器端APN推送更新徽章,无论应用程序状态,前景或背景如何.
有没有办法在应用程序位于前台时使用推送消息更改iOS应用程序徽章,而无需处理应用程序内的推送通知?
python ×4
postgresql ×3
json ×2
nose ×2
sqlalchemy ×2
alias ×1
arrays ×1
browser ×1
datagrip ×1
deprecated ×1
docker ×1
flask ×1
foreground ×1
html ×1
ios ×1
javascript ×1
jsonschema ×1
macos ×1
orm ×1
pip ×1
psql ×1
python-3.3 ×1
sql ×1
unit-testing ×1
validation ×1