我有一个 env.sh 其中包含:
export MULTI_LINES="line1\nline2\nline3"
Run Code Online (Sandbox Code Playgroud)
然后我使用源来运行它
source env.sh
Run Code Online (Sandbox Code Playgroud)
当我检查环境变量时:
printenv | grep MULTI_LINES
Run Code Online (Sandbox Code Playgroud)
表明
MULTI_LINES=line1\nline2\nline3
Run Code Online (Sandbox Code Playgroud)
如何将“\n”转义为换行符?
import sys
def worker(a):
try:
return 1 / a
except ZeroDivisionError:
return None
def master():
res = worker(0)
if not res:
print(sys.exc_info())
raise sys.exc_info()[0]
Run Code Online (Sandbox Code Playgroud)
作为上面的代码段,我有一堆函数,比如worker。他们已经有了自己的 try-except 块来处理异常。然后一个主函数将调用每个工人。现在,sys.exc_info() 将所有 None 返回到 3 个元素,如何在 master 函数中重新引发异常?我正在使用 Python 2.7
一个更新:我有 1000 多个工人,有些工人逻辑非常复杂,他们可能同时处理多种类型的异常。所以我的问题是我可以只从 master 提出这些例外而不是编辑作品吗?
假设我有一个对象模型A,它有一个名为created的字段,这是一个日期时间类型字段。
如果我使用注释来计算每天创建的A数,则可以使用
A.objects.annotate(date=Trunc('created', 'day', output_field=DateField()) ).values('date').order_by('date').annotate(count=Count('id'))
Run Code Online (Sandbox Code Playgroud)
之后,我可以得到结果,看起来像
[{date: '2018-07-22', count:1 }, {date: '2018-07-23', count:1 }, {date: '2018-07-25', count:1 }]
Run Code Online (Sandbox Code Playgroud)
但是,请注意,我错过了2018-07-24,因为它当天没有创建任何A. 有什么方法可以让结果包含{date: '2018-07-24', count:0 }在该查询集中吗?
我有两个查询执行相同的操作。1
SELECT *
FROM "Products_product"
WHERE ("Products_product"."id" IN
(SELECT U0."product_id"
FROM "Products_purchase" U0
WHERE (U0."state" = 1
AND U0."user_id" = 5))
AND "Products_product"."state" IN (1,
6,
3)
AND UPPER("Products_product"."title" :: TEXT) LIKE UPPER('%toronto%'))
ORDER BY "Products_product"."title_index" ASC
LIMIT 10;
Run Code Online (Sandbox Code Playgroud)
2
SELECT *
FROM "Products_product"
WHERE ("Products_product"."id" = ANY (ARRAY(
(SELECT U0."product_id"
FROM "Products_purchase" U0
WHERE (U0."state" = 1
AND U0."user_id" = 5))))
AND "Products_product"."state" IN (1,
6,
3)
AND UPPER("Products_product"."title" :: TEXT) LIKE UPPER('%toronto%'))
ORDER BY "Products_product"."title_index" ASC
LIMIT 10; …Run Code Online (Sandbox Code Playgroud) 我正在使用 Flask-testing 对 Postgres 应用程序进行一些单元测试。根据文档,我有以下代码。
from flask_testing import TestCase
from src.game.models import db
from src import create_app
class BaseTest(TestCase):
SQLALCHEMY_DATABASE_URI = 'postgresql://demo:demo@postgres:5432/test_db'
TESTING = True
def create_app(self):
# pass in test configuration
return create_app(self)
def setUp(self):
db.create_all()
def tearDown(self):
db.session.remove()
db.drop_all()
Run Code Online (Sandbox Code Playgroud)
当然我得到了这个错误
sqlalchemy.exc.OperationalError:(psycopg2.OperationalError)致命:数据库“test_db”不存在
我确实有一个数据库postgresql://demo:demo@postgres:5432/demo,它是我的生产数据库。
我如何test_db在这BaseTest堂课上进行创作?我正在使用Python 3.6和最新的flask和flask-sqlalchemy。非常感谢。
postgresql unit-testing flask flask-sqlalchemy flask-testing
class Author(models.Model):
name = models.CharField(max_length=120)
class Book(models.Model):
name = models.CharField(max_length=20)
author= models.ForeignKey(Author)
Run Code Online (Sandbox Code Playgroud)
考虑这两个模型。当我访问这个外键时,这两种方式有什么区别:
id= Book.objects.get(pk=1).author.id
id= Book.objects.get(pk=1).author_id
Run Code Online (Sandbox Code Playgroud) 我有一个模型A,其中有一个名为的字段name。如何获取pre_save信号中的先前值和新值?
@receiver(pre_save, sender=A)
def signal_product_manage_latest_version_id(
sender, instance, update_fields=None, **kwargs):
if 'name' in update_fields:
print(instance.name)
Run Code Online (Sandbox Code Playgroud)
name当我调用以下代码时,是旧值还是新值?
a = A.objects.create(name="John")
a.name = "Lee"
a.save()
Run Code Online (Sandbox Code Playgroud) django ×3
orm ×2
postgresql ×2
python ×2
arrays ×1
django-orm ×1
exception ×1
flask ×1
foreign-keys ×1
multiline ×1
set ×1
shell ×1
unit-testing ×1