我想使用 Joi 来验证对象,其中使用 Joi.ref() 和乘法运算。
var object = {
a: 5,
b: 6
}
// this is wrong as Joi.ref('a')*2 is now allowed in max()
var schema = Joi.object({
a: Joi.number().integer(),
b: Joi.number().integer().min(1).max(Joi.ref('a')*2)
})
Run Code Online (Sandbox Code Playgroud)
Joi.ref('a')*2不被允许。那么我怎样才能验证对象b<=2*a呢?
fcntl告诉它的man页面用于操作文件描述符.但这个名字不容易记住.知道其全名将有助于记住此系统调用名称及其用法.我试图在互联网上找到但除了手册之外无法得到任何东西.
请有人告诉fcntl名称的全名或来源.
我正在使用psycopg2来查询PostgreSQL数据库。
import psycopg2
import psycopg2.extras
DB_CONNECTION = {
'host': os.getenv('PG_HOST'),
'database': os.getenv('PG_DB_NAME'),
'user': os.getenv('PG_USER'),
'password': os.getenv('PG_PASSWORD')
}
connection = psycopg2.connect(**DB_CONNECTION)
connection.autocommit = True
cursor = connection.cursor(cursor_factory = psycopg2.extras.RealDictCursor)
# amount column type is numeric in table
query = 'SELECT userid, amount FROM some_table WHERE userid = %(userid)s'
cursor.execute(query, {'userid': 1234})
rows = cursor.fetchall()
print(rows[0]['amount']) # This is value is object of decimal.Decimal
Run Code Online (Sandbox Code Playgroud)
rows[0]['amount']是decimal.Decimal类的对象。有没有简单干净的方法来获取这个值float?我不想float(rows[0]['amount'])手动将其转换为浮动。
我想解析压缩的站点地图,如www.example.com/sitemap.xml.gz,并收集站点地图中的所有网址,而不下载sitemap.xml.gz.
在下载sitemap.xml.gz并借助lxml或beautifulsoup等解压缩后,有办法解析它.
def parse_sitemap_gz(url):
r = requests.get(url, stream=True)
if 200 != r.status_code:
return False
file_name = url.split('/')[-1]
# download the sitemap file
with open(file_name, 'wb') as f:
if not r.ok:
print 'error in %s'%(url)
for block in r.iter_content(1024):
if not block:
break
f.write(block) # can I parse it without writing to file
f.flush()
# decompress gz file
subprocess.call(['gunzip', '-f', file_name])
# parse xml file
page = lxml.html.parse(file_name[0:-3])
all_urls = page.xpath('//url/loc/text()')
#print all_urls
# delete sitemap file …Run Code Online (Sandbox Code Playgroud) 对于 Django 项目来说,创建基于函数的任务非常干净。只需在 django 应用程序中创建tasks.py并开始编写任务,就像这个示例一样,该示例取自官方celery文档:http://docs.celeryproject.org/en/latest/django/first-steps-with-django.html
from __future__ import absolute_import, unicode_literals
from celery import shared_task
@shared_task
def add(x, y):
return x + y
@shared_task
def mul(x, y):
return x * y
Run Code Online (Sandbox Code Playgroud)
但有时基于函数的任务是紧密耦合的并且可重用性不高。所以我想创建基于类的芹菜任务,该任务记录在官方网站中。在遵循https://github.com/celery/celery/issues/3874之后,我可以创建示例任务,但我不确定它是否是创建基于类的任务的正确方法。
from __future__ import absolute_import, unicode_literals
from celery import shared_task, Task
import time
from celery import current_app
@shared_task
def add(x, y):
time.sleep(5)
return x + y
@shared_task
def mul(x, y):
return x * y
# Sample class based task for testing
class AnotherTask(current_app.Task):
name = 'tasks.another_task'
def …Run Code Online (Sandbox Code Playgroud) python ×3
c ×1
celery ×1
django ×1
fcntl ×1
gunzip ×1
javascript ×1
joi ×1
json ×1
object ×1
parsing ×1
postgresql ×1
psycopg2 ×1
sitemap ×1
stream ×1
system-calls ×1
task ×1
validation ×1