我正在编写一个使用 SQLAlchemy 数据库的FastAPI应用程序。我已经从 FastAPI 文档中复制了示例,为了简洁起见,简化了数据库模式。完整的来源在这篇文章的底部。
这有效。我可以uvicorn sql_app.main:app通过 Swagger 文档运行它并与数据库交互。当它运行时,它会test.db在工作目录中创建一个。
现在我想添加一个单元测试。像这样的东西。
from fastapi import status
from fastapi.testclient import TestClient
from pytest import fixture
from main import app
@fixture
def client() -> TestClient:
return TestClient(app)
def test_fast_sql(client: TestClient):
response = client.get("/users/")
assert response.status_code == status.HTTP_200_OK
assert response.json() == []
Run Code Online (Sandbox Code Playgroud)
使用下面的源代码,这将test.db工作目录中的 用作数据库。相反,我想为在测试结束时删除的每个单元测试创建一个新数据库。
我可以将全局database.engine和database.SessionLocal在运行时创建的对象内,像这样:
class UserDatabase:
def __init__(self, directory: Path):
directory.mkdir(exist_ok=True, parents=True)
sqlalchemy_database_url = f"sqlite:///{directory}/store.db"
self.engine = create_engine( …Run Code Online (Sandbox Code Playgroud) 我有一个抽象的Scala类Base,它有子类Derived1和Derived2.Base定义一个函数f(),它返回与其实现类相同类型的对象.所以Derived1.f()返回Derived1并Derived2.f()返回Derived2.我如何在Scala中写这个?
这是我到目前为止所提出的.
package com.github.wpm.cancan
abstract class Base {
def f[C <: Base]: C
}
case class Derived1(x: Int) extends Base {
def f[Derived1] = Derived1(x + 1)
}
case class Derived2(x: Int) extends Base {
def f[Derived2] = Derived2(x + 2)
}
Run Code Online (Sandbox Code Playgroud)
这给出了以下编译器错误:
type mismatch;
[error] found : com.github.wpm.cancan.Derived1
[error] required: Derived1
[error] def f[Derived1] = Derived1(x + 1)
type mismatch;
[error] found : …Run Code Online (Sandbox Code Playgroud) 我正在使用Scala dispatch HTTP库,版本0.10.1.我向一个返回HTTP 301永久重定向的URL发出请求.例如,http: //wikipedia.com返回301,重定向到http://www.wikipedia.org/.我如何使用dispatch获取重定向的URL?
在教程之后,这就是我所做的.
import dispatch._, Defaults._
val svc = url("http://wikipedia.com")
val r = Http(svc OK as.String)
r()
Run Code Online (Sandbox Code Playgroud)
这会引发"意外响应状态:301"异常.据推测,我需要查询r重定向URL 的值,或者可能指定除OK定义之外的其他参数,但我无法弄清楚如何从文档中做什么.
我正在使用命令行工具训练 NER 模型python -m spacy train。我用来gold.docs_to_json将带注释的文档转换为JSON 可序列化格式。
命令行训练工具同时使用训练集和开发集。我不确定命令行工具为我管理训练/开发拆分提供了多少帮助。
我认为问题(1)和(2)的答案都是“否”,但我想仔细检查一下。
从实践来看,即使您正在训练生产模型进行固定次数的迭代,您也始终必须传递非空开发集。现在我只是传递了我的训练数据的副本,但看起来很奇怪,所以我想知道是否还缺少其他一些程序。
有关训练的 spaCy 文档主要讨论编写您自己的迭代循环。我已经做了足够多的工作,我确信如果我编写自己的代码,我可以完成上述任何工作,但对于这些基本的训练操作,我宁愿不编写代码,而只使用命令行工具来完成所有操作。
我想用以下(text, label)对训练TextCategorizer模型.
标签颜色:
标签动物:
我正在复制TextCategorizer文档中的示例代码.
textcat = TextCategorizer(nlp.vocab)
losses = {}
optimizer = nlp.begin_training()
textcat.update([doc1, doc2], [gold1, gold2], losses=losses, sgd=optimizer)
Run Code Online (Sandbox Code Playgroud)
doc变量可能只是nlp("The door is brown.")等等.什么应该gold1和gold2?我猜他们应该是GoldParse对象,但我不知道你如何表示那些文本分类信息.
我想定义一个函数f [Int] -> Int,f如果列表的长度是一个完美的正方形,则该函数等于列表中元素的总和,否则是未定义的.在Haskell中这样做的惯用方法是什么?
在命令式语言中,我会添加一行来assert sqrt(len(xs)) is integer为未定义的案例生成异常.在像Haskell这样的强类型函数语言中,您希望在类型系统中构建未定义的条件,但是由于没有"长度完美平方列表"类型,因此无法在此处完成.
我宁愿让程序在未定义的情况下暂停,而不是让函数返回Nothing.
我的程序使用Click进行命令行处理。它有一个带有必需参数的主命令。此命令具有采用可选参数的子命令。不同的子命令采用不同的选项,但它们都需要来自其父命令的相同参数。我想让命令行看起来像这样:
python myprogram.py argument-value subcommand1 --option-1=value
Run Code Online (Sandbox Code Playgroud)
我可以像这样使用 Click 写这个
import click
@click.group()
@click.argument("argument")
@click.pass_context
def main(context, argument):
"""ARGUMENT is required for both subcommands"""
context.obj = {"argument": argument}
@click.command()
@click.option("--option-1", help="option for subcommand 1")
@click.pass_context
def subcommand1(context, option_1):
print("subcommand 1: %s %s" % (context.obj["argument"], option_1))
@click.command()
@click.option("--option-2", help="option for subcommand 2")
@click.pass_context
def subcommand2(context, option_2):
print("subcommand 2: %s %s" % (context.obj["argument"], option_2))
main.add_command(subcommand1)
main.add_command(subcommand2)
if __name__ == "__main__":
main()
Run Code Online (Sandbox Code Playgroud)
顶级帮助信息正是我想要的。
python myprogram.py --help
Usage: myprogram.py [OPTIONS] ARGUMENT COMMAND [ARGS]... …Run Code Online (Sandbox Code Playgroud) 当我运行以下Python程序时
from pyspark.ml.classification import LinearSVC
from pyspark.sql import SparkSession
spark = SparkSession.builder.appName("Sparkmodel").getOrCreate()
data = spark.read.format("libsvm").load("/usr/local/spark/data/mllib/sample_libsvm_data.txt")
model = LinearSVC().fit(data)
model.save("mymodel")
LinearSVC.load("mymodel")
Run Code Online (Sandbox Code Playgroud)
失败load并显示“java.lang.NoSuchMethodException”。
/anaconda3/envs/scratch/bin/python /Users/billmcn/src/toy/sparkmodel/sparkmodel/little.py
Using Spark's default log4j profile: org/apache/spark/log4j-defaults.properties
Setting default log level to "WARN".
To adjust logging level use sc.setLogLevel(newLevel). For SparkR, use setLogLevel(newLevel).
17/11/12 13:23:05 WARN NativeCodeLoader: Unable to load native-hadoop library for your platform... using builtin-java classes where applicable
17/11/12 13:23:06 WARN Utils: Service 'SparkUI' could not bind on port 4040. Attempting port 4041.
17/11/12 …Run Code Online (Sandbox Code Playgroud) 我想在我的 Python Spark 文本分类管道中使用词嵌入。该文档展示了如何训练您自己的嵌入,但我想使用像 GloVe 这样的预训练集。有没有某种方法可以使用预训练的嵌入来初始化 Word2VecModel 还是我必须编写自己的转换器类?
apache-spark word2vec pyspark apache-spark-ml word-embedding
我无法从 Docker 映像设置开发 Elasticsearch/Kibana 实例。我无法让 Kibana 服务连接到 Elasticsearch 服务。
我有以下docker-compose.yml文件。
services:
elasticsearch:
container_name: elasticsearch
image: docker.elastic.co/elasticsearch/elasticsearch:8.4.0
environment:
- discovery.type=single-node
- xpack.security.enabled=true
ports:
- "9200:9200"
kibana:
container_name: kibana
image: docker.elastic.co/kibana/kibana:8.4.0
environment:
- ELASTICSEARCH_URL=http://elasticsearch:9200
- xpack.security.enabled=true
ports:
- "5601:5601"
Run Code Online (Sandbox Code Playgroud)
我一开始没有创建和运行任何容器docker compose up。根据自动启用安全性启动 Elastic Stack中的说明,我期望看到以下内容:
A password is generated for the elastic user and output to the terminal, plus an enrollment token for enrolling Kibana.
Run Code Online (Sandbox Code Playgroud)
不生成密码。当我查看日志时,我从 Kibana 容器中看到以下内容。
[ERROR][elasticsearch-service] Unable to retrieve version information from Elasticsearch nodes. …Run Code Online (Sandbox Code Playgroud) apache-spark ×2
pyspark ×2
scala ×2
spacy ×2
docker ×1
fastapi ×1
generics ×1
haskell ×1
kibana ×1
polymorphism ×1
python ×1
python-click ×1
return-type ×1
sqlalchemy ×1
test-fixture ×1
word2vec ×1