小编use*_*081的帖子

上下文管理器的类型提示

我想要一个 pyplot 图形的上下文管理器,基本上像这样:

from contextlib import contextmanager
import matplotlib.pyplot as plt

@contextmanager
def subplots():
  (fig, ax) = plt.subplots()
  try:
    yield (fig, ax)
  finally:
    plt.close(fig)

Run Code Online (Sandbox Code Playgroud)

是否可以对返回的元组实现类型提示?天真的

import typing
def subplots() -> typing.Tuple[plt.Figure, plt.Axes]
Run Code Online (Sandbox Code Playgroud)

不起作用。

type-hinting contextmanager python-3.x

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

Django ORM 中 F 表达式的逻辑 OR

我有一个查询,例如

User.objects.annotate(
    x=Value(False, output_field=BooleanField()),
    y=Value(True, output_field=BooleanField())
).annotate(
    z=F('x').bitor(F('y'))  # HOW TO DO THIS?
).values('z')
Run Code Online (Sandbox Code Playgroud)

它适用于 SQLite,但不适用于 PostgreSQL。错误是

LINE 1: SELECT (false | true) AS "z" FROM "auth_user"  LIMIT 21
                      ^
HINT:  No operator matches the given name and argument types. You might need to add explicit type casts.
Run Code Online (Sandbox Code Playgroud)

我应该如何在带注释的字段上实现一致的逻辑或?

谢谢。

sqlite django postgresql orm

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

SQL SELECT中的布尔逻辑

有没有更简单的实施方法

select (case when ((a is null) or (b is null)) then null else (case when
(a = b) then true else false end) end) from ...
Run Code Online (Sandbox Code Playgroud)

在PostgreSQL或其他主要的RDBMS中呢?

sql postgresql boolean-expression

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