小编Alo*_*hor的帖子

如何使用带有 Joi.ref() 的数学运算来验证使用 Joi 的对象?

我想使用 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呢?

javascript validation json object joi

2
推荐指数
1
解决办法
1563
查看次数

什么是fcntl Unix/Linux系统调用的全名

fcntl告诉它的man页面用于操作文件描述符.但这个名字不容易记住.知道其全名将有助于记住此系统调用名称及其用法.我试图在互联网上找到但除了手册之外无法得到任何东西.

请有人告诉fcntl名称的全名或来源.

c operating-system system-calls fcntl

1
推荐指数
1
解决办法
244
查看次数

如何从 PostgreSQL 表中获取浮点值仅作为浮点而不是 psycopg2 中的小数?

我正在使用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'])手动将其转换为浮动。

python postgresql psycopg2

1
推荐指数
1
解决办法
4050
查看次数

如何使用python解析压缩的站点地图而不将其下载到磁盘?

我想解析压缩的站点地图,如www.example.com/sitemap.xml.gz,并收集站点地图中的所有网址,而不下载sitemap.xml.gz.

在下载sitemap.xml.gz并借助lxmlbeautifulsoup等解压缩后,有办法解析它.

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)

python sitemap parsing stream gunzip

0
推荐指数
1
解决办法
2482
查看次数

为 Django 项目中的可重用应用程序创建基于类的 Celery 任务

对于 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 django task celery django-celery

0
推荐指数
1
解决办法
5948
查看次数