在 Kotlin 中,我希望有一个接口,要求实现类具有特定的构造函数。像这样的东西:
interface Inter<T> {
// Must have constructor (t: T)
}
class Impl(t: String): Inter<String>
Run Code Online (Sandbox Code Playgroud)
如何实现这一目标?
我有一个返回文件的 Django 视图。该FileResponse是专为这一目的。但是,我不明白如何测试这个。
现在我使用HttpResponse并像这样测试它:
response = client.get(url)
io = io = BytesIO(response.content)
Run Code Online (Sandbox Code Playgroud)
该io对象现在可用于进一步测试。
但是,如果我尝试以下 with FileResponse(派生自StreamingHttpResponse并因此streaming_content代替content),我会收到以下异常:
TypeError: a bytes-like object is required, not 'map'
如果我将map对象转换为bytes这样:
response = client.get(url)
io = io = BytesIO(bytes(response.streaming_content))
Run Code Online (Sandbox Code Playgroud)
我得到另一个例外: TypeError: 'bytes' object cannot be interpreted as an integer
如何BytesIO从 中获取对象FileResponse.streaming_content?
我有一个 Django 模型:
class Book(models.Model):
author = models.ForeignKey(Author, on_delete=models.PROTECT)
@property
def slug(self):
return slugify(self.author.name)
Run Code Online (Sandbox Code Playgroud)
现在,如果我将slug字段添加到 admin list_display,每个实例都会有一个单独的查询。
如何只对所有实例进行一次查询?
我试图select_related在ModelAdmin课堂上使用,但我没有得到它的工作。
我有一个用 Kotlin 编写的 Spring 实体:
@Entity
class Book(
@Id
var id: Long? = null,
var title: String? = null, // cannot actually be null, immutable
var isInStock: Boolean? = null, // cannot actually be null
var description: String? = null,
)
Run Code Online (Sandbox Code Playgroud)
所有字段都必须可为空,因为 Spring 需要初始化空对象。
但是,这使得实体的使用变得复杂,因为我总是必须将可为空的类型转换为其不可为空的等效类型。这不是语义:我想真正了解哪些字段实际上可以为空,哪些字段不能(或者只能null在初始化过程中)。
此外,某些字段是可变的,但有些字段在创建实体后不应更改。最好使用 Kotlinval并var做出这种区分。
所以,我想与以下课程一起工作:
class BetterBook(
val id: Long,
val title: String,
var isInStock: Boolean,
var description: String? = null,
)
Run Code Online (Sandbox Code Playgroud)
这样就很清楚哪些字段是可变的,哪些字段可以null。
我考虑过创建一个包装器Book。有人有过类似的想法吗?这在架构上是一个好的解决方案吗?
我有一个看起来像这样的图(这是著名的 Wine 数据集):
正如您所看到的,x 轴标签重叠,因此我需要旋转。
注意!我对旋转 x 刻度不感兴趣(如此处所述),但对标签文本(即alcohol、malic_acid等)感兴趣。
创建绘图的逻辑如下:我使用 创建一个网格axd = fig.subplot_mosaic(...),然后为底部绘图设置标签axd[...].set_xlabel("something")。set_xlabel如果能接受一个参数那就太好了rotation,但不幸的是事实并非如此。
django ×2
kotlin ×2
python ×2
axis-labels ×1
django-admin ×1
django-views ×1
generics ×1
matplotlib ×1
nullable ×1
seaborn ×1
semantics ×1
spring ×1
subplot ×1