小编Pro*_*o Q的帖子

如何使用 Matter.js 判断两个物体是否接触

我使用Matter.js作为物理引擎,我发现我可以检测碰撞。

但是,我不知道如何判断碰撞发生后两个物体是否相互接触。有什么办法可以做到这一点吗?

(在我的特定情况下,我希望球仅在接触特定的地面时才跳跃。)

javascript physics matter.js

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

如何使用默认值将列添加到数据库

我有一个数据库,我正在尝试向其中添加一列。此列应保存 type 的信息timestamp,并且我希望完成后每一行都具有相同的时间戳(当前时间)。

我目前已经尝试过:

cursor.execute('''ALTER TABLE my_table ADD COLUMN time timestamp DEFAULT ?''', (datetime.datetime.utcnow(),))
Run Code Online (Sandbox Code Playgroud)

结果是sqlite3.OperationalError: near "?": syntax error.

然后我尝试了:

cursor.execute(f'''ALTER TABLE my_table ADD COLUMN time timestamp DEFAULT {datetime.datetime.utcnow()}''')
Run Code Online (Sandbox Code Playgroud)

结果是sqlite3.OperationalError: near "-": syntax error.

另外,做

cursor.execute(f'''ALTER TABLE my_table ADD COLUMN time timestamp DEFAULT CURRENT_TIMESTAMP''')
Run Code Online (Sandbox Code Playgroud)

结果是sqlite3.OperationalError: Cannot add a column with non-constant default

如何添加新列并设置该列中的值?(通过DEFAULT或其他一些机制。)

sql sqlite date create-table python-3.x

6
推荐指数
2
解决办法
5127
查看次数

如何使用 init-hook 让 pylint 正确导入我的文件

我按照这些说明如何让 pylint 使用init-hook.

但是,当我运行终端命令时,pylint server.py --rcfile=../.pylintrc我得到了TypeError: expected str, bytes or os.PathLike object, not NoneType. (我认为这是因为找到 rcfile 的部分init-hook在某种程度上失败了,但我不确定。)

这是我的.pylintrc文件直至相关部分(其余部分只是默认模板):

[MASTER]

# A comma-separated list of package or module names from where C extensions may
# be loaded. Extensions are loading into the active Python interpreter and may
# run arbitrary code.
extension-pkg-whitelist=

# Specify a score threshold to be exceeded before program exits with error.
fail-under=10.0

# Add files …
Run Code Online (Sandbox Code Playgroud)

python pylint python-3.x pylintrc

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

有没有办法在正则表达集中使用特殊字符?

我正在使用python,现在我想要一些东西来匹配直到一个空格或字符串的结尾,所以我有正则表达式".*?[ $]".

但经过测试并看到它无效后,我查看了文档,发现特殊字符在集合中失去了特殊含义.

有没有办法将这个含义放回到集合中?

python regex

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

如何使用 Google Apps 脚本向 Google 文档添加评论

我正在查看文档,发现无法使用 Google Apps 脚本添加评论。

滚动了一下后,我发现该addComment()函数已被弃用。

目前是否有任何方法可以使用 GAS 添加评论到 Google 文档(不使用已弃用的脚本)?

google-apps-script

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

如何在 Python 的 timeit 中使用 else

我是使用 timeit 模块的新手,我很难让多行代码片段在 timeit 内运行。

什么工作:

timeit.timeit(stmt = "if True: print('hi');")
Run Code Online (Sandbox Code Playgroud)

什么不起作用(这些都无法运行):

timeit.timeit(stmt = "if True: print('hi'); else: print('bye')")
timeit.timeit(stmt = "if True: print('hi') else: print('bye')")
timeit.timeit(stmt = "if True: print('hi');; else: print('bye')")
Run Code Online (Sandbox Code Playgroud)

我发现我可以使用三引号来封装多行代码段,但我宁愿只在一行上输入。

有没有办法在 timeit 的一行中使用 else 语句?

python timeit

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

如何创建抽象类属性(可能是只读的)

我花了很多时间研究这个,但没有一个答案看起来像我想要的那样.

我有一个带有类属性的抽象类,我希望每个子类都被强制实现

class AbstractFoo():
    forceThis = 0
Run Code Online (Sandbox Code Playgroud)

所以,当我这样做

class RealFoo(AbstractFoo):
    pass
Run Code Online (Sandbox Code Playgroud)

它会抛出一个错误,告诉我在实现之前它无法创建类forceThis.

我怎样才能做到这一点?

(我不希望该属性是只读的,但如果这是唯一的解决方案,我会接受它.)

对于一个类方法,我发现我可以做到

from abc import ABCMeta, abstractmethod

class AbstractFoo(metaclass=ABCMeta):
    @classmethod
    @abstractmethod
    def forceThis():
        """This must be implemented"""
Run Code Online (Sandbox Code Playgroud)

以便

class RealFoo(AbstractFoo):
    pass
Run Code Online (Sandbox Code Playgroud)

至少抛出错误 TypeError: Can't instantiate abstract class EZ with abstract methods forceThis

(虽然它不强制forceThis成为一种类方法.)

如何为类属性弹出类似的错误?

python abstract-class exception class-properties

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

如何从Python中停止打印OpenCV错误消息

此问题相同,除了Python,不是C ++。

我在OpenCV中有一条错误消息正在打印,即使我在它周围进行了除外捕获也是如此。

链接问题解决方案建议使用重定向功能,但是此注释表明该功能在Python for OpenCV中不存在。

如何停止打印OpenCV错误消息,同时仍然允许我完全打印所需的内容?

python opencv

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

如何让 Mypy 认识到对两个整数进行排序会返回两个整数

我的代码如下:

from typing import Tuple

a: Tuple[int, int] = tuple(sorted([1, 3]))
Run Code Online (Sandbox Code Playgroud)

麦皮告诉我:

赋值中的类型不兼容(表达式的类型为“Tuple[int, ...]”,变量的类型为“Tuple[int, int]”)

我究竟做错了什么?为什么 Mypy 无法计算出排序后的元组将返回恰好两个整数?

int tuples typechecking python-3.x mypy

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

MuZero伪代码中的奖励值是否错位?

MuZero是一种深度强化学习技术,刚刚发布,我一直在尝试通过查看其伪代码和 Medium 上的这篇有用教程来实现它。

然而,我对伪代码训练期间如何处理奖励感到困惑,如果有人能够验证我是否正确地阅读了代码,那就太好了,如果我是,请解释为什么这个训练算法有效。

这是训练函数(来自伪代码):

def update_weights(optimizer: tf.train.Optimizer, network: Network, batch,
                   weight_decay: float):
  loss = 0
  for image, actions, targets in batch:
    # Initial step, from the real observation.
    value, reward, policy_logits, hidden_state = network.initial_inference(
        image)
    predictions = [(1.0, value, reward, policy_logits)]

    # Recurrent steps, from action and previous hidden state.
    for action in actions:
      value, reward, policy_logits, hidden_state = network.recurrent_inference(
          hidden_state, action)
      predictions.append((1.0 / len(actions), value, reward, policy_logits))

      hidden_state = tf.scale_gradient(hidden_state, 0.5) …
Run Code Online (Sandbox Code Playgroud)

python algorithm artificial-intelligence structure machine-learning

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