我正在尝试建立一个长期运行的Pull订阅Google Cloud PubSub主题.我使用的代码非常类似于此处文档中给出的示例,即:
def receive_messages(project, subscription_name):
"""Receives messages from a pull subscription."""
subscriber = pubsub_v1.SubscriberClient()
subscription_path = subscriber.subscription_path(
project, subscription_name)
def callback(message):
print('Received message: {}'.format(message))
message.ack()
subscriber.subscribe(subscription_path, callback=callback)
# The subscriber is non-blocking, so we must keep the main thread from
# exiting to allow it to process messages in the background.
print('Listening for messages on {}'.format(subscription_path))
while True:
time.sleep(60)
Run Code Online (Sandbox Code Playgroud)
问题是我有时收到以下追溯:
Exception in thread Consumer helper: consume bidirectional stream:
Traceback (most recent call last):
File "/usr/lib/python3.5/threading.py", line 914, …Run Code Online (Sandbox Code Playgroud) 我想添加一个自动增量列,它不是现有 MySQL 数据库的主键。
此操作所需的服务器上发出的命令如下:
ALTER TABLE `mytable` ADD `id` INT UNIQUE NOT NULL AUTO_INCREMENT FIRST
Run Code Online (Sandbox Code Playgroud)
我面临的问题是如何通过 Alembic 迁移复制此表更改。我试过了:
from alembic import op
import sqlalchemy as sa
def upgrade():
op.add_column('mytable', sa.Colummn('id', sa.INTEGER(),
nullable=False, autoincrement=True)
Run Code Online (Sandbox Code Playgroud)
但是当我尝试使用以下命令插入一行时:
INSERT INTO `mytable` (`col1`, `col2`) VALUES (`bar`);
Run Code Online (Sandbox Code Playgroud)
其中col1,col2是不可为空的列。当我插入这条记录时,我希望表自动为我生成 id。
ERROR 1364 (HY000): Field 'id' doesn't have a default value
Run Code Online (Sandbox Code Playgroud)
如果我使用以下命令检查 Alembic 自动生成的 sql:
alembic upgrade 'hash-of-revision' --sql
Run Code Online (Sandbox Code Playgroud)
它吐出,对于给定的修订:
ALTER TABLE mytable ADD COLUMN id INTEGER NOT NULL;
Run Code Online (Sandbox Code Playgroud)
这意味着 Alembic 或 SQLAlchemyautoincrement …
我试图在Golang解组数据,我发现一个奇怪的现象时JSON对象中的一些关键有下划线(_在里面).
举个例子:
package main
import (
"encoding/json"
"fmt"
)
func main() {
var jsonBlob = []byte(`{"name": "Quoll", "order": "Dasyuromorphia"}`)
type Animal struct {
Name string `json: "name"`
Order string `json: "order"`
}
var animal Animal
err := json.Unmarshal(jsonBlob, &animal)
if err != nil {
fmt.Println("error:", err)
}
fmt.Printf("%+v", animal)
}
Run Code Online (Sandbox Code Playgroud)
这运行得非常好.但是,如果我更改某个键以包含下划线:
var jsonBlob = []byte(`{"name": "Quoll", "order_": "Dasyuromorphia"}`)
Run Code Online (Sandbox Code Playgroud)
我希望将其纳入Animal.Order,我正在尝试:
type Animal struct {
Name string `json: "name"`
Order string `json: "order_"`
}
Run Code Online (Sandbox Code Playgroud)
我很难读到数据.如何将arbirary键映射到我想要的结构元素?这是一个带有示例的操场的链接.
我是python的新手程序员。我在尝试提取一系列推文tweepy的文本并将其保存到文本文件时遇到麻烦(我省略了身份验证和内容)
search = api.search("hello", count=10)
textlist=[]
for i in range(0,len(search)):
textlist.append( search[i].text.replace('\n', '' ) )
f = open('temp.txt', 'w')
for i in range(0,len(idlist)):
f.write(textlist[i].encode('utf-8') + '\n')
Run Code Online (Sandbox Code Playgroud)
但是在某些长推文中,末尾的文本被截断,并且在每个字符串的末尾出现了三个点字符“ ...”,因此有时我会丢失链接或主题标签。如何避免这种情况?
我正在尝试测试一个被记忆的方法lru_cache(因为它是一个昂贵的数据库调用)。和pytest-mock.
代码的简化版本是:
class User:
def __init__(self, file):
# load a file
@lru_cache
def get(self, user_id):
# do expensive call
Run Code Online (Sandbox Code Playgroud)
然后我正在测试:
class TestUser:
def test_get_is_called(self, mocker):
data = mocker.ANY
user = User(data)
repository.get(user_id)
open_mock = mocker.patch('builtins.open', mocker.mock_open())
open_mock.assert_called_with('/foo')
Run Code Online (Sandbox Code Playgroud)
但我收到以下错误:
TypeError: unhashable type: '_ANY'
Run Code Online (Sandbox Code Playgroud)
发生这种情况是因为functools.lru_cache需要存储的密钥是可散列的,即有一个方法__hash__或__cmp__实现。
我怎样才能在嘲笑者中嘲笑这些方法以使其工作?
我试过了
user.__hash__.return_value = 'foo'
Run Code Online (Sandbox Code Playgroud)
没有运气。