小编Sym*_*nen的帖子

Nuxt.js - API 调用的最佳场所

我是 Vue.js Nuxt 和所有前端内容的新手。

我有一个关于 API 调用的问题。我不确定什么是正确的方法,这里的最佳实践。

我有一家店。在那个商店中,我有调用我的 API 并设置状态的操作,例如。

async fetchArticle({ state, commit }, uuid) {
    const response = await this.$axios.get(`articles/${uuid}/`)
    commit('SET_ARTICLE', response.data)
},
Run Code Online (Sandbox Code Playgroud)

这很好,它适用于一个组件。

但是如果我只想获取文章而不改变状态怎么办。

为了 DRY,我想到的第一件事是创建获取数据并在需要的地方使用的服务层。

这是正确的方法吗?在哪里可以找到一些可以从中汲取灵感的真实示例?

vue.js axios vuex nuxt.js

9
推荐指数
2
解决办法
6260
查看次数

TypeScript Set<> 集合通过索引获取值

大家好,我是 TypeScript 的新手,也许我没有看到明显的东西,但这是我的问题:

const someSet = new Set();
someSet.add(1)
console.log(someSet[0])
Run Code Online (Sandbox Code Playgroud)

给了我 undefined 谁能解释一下为什么我们不能通过索引获得价值?我该怎么做?

collections set typescript

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

在哪里配置日志记录?

我想知道在哪里配置和初始化与日志记录模块相关的内容?

例如,我编写了一些类,并且想在执行方法时记录一些信息。我应该在模块顶部的init或以上类中配置登录:

# LOGGING STUFF <--- Should be here ? 

class SomeClass:

    def __init__(self):
        # class stuff
        # LOGGING STUFF <--- Or should be here ? 

    def some_method(self):
        # method stuff
        # LOGGING SOME INFO    

    def some_method2(self):
        # method stuff
        # LOGGING SOME INFO
Run Code Online (Sandbox Code Playgroud)

最佳做法是什么?

python logging project

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

如何模拟需要 Response 对象的 pydantic BaseModel?

我正在为我的 API 客户端编写测试。我需要模拟该get函数,以便它不会发出任何请求。因此,Response我不想返回一个对象,而是想返回一个MagicMock. 但随后 pydantic 加注ValidationError因为它要去模型。

我有以下 pydantic 模型:

class Meta(BaseModel):
    raw: Optional[str]
    response: Optional[Response]

    class Config:
        arbitrary_types_allowed = True
Run Code Online (Sandbox Code Playgroud)

这引发了:

>   ???
E   pydantic.error_wrappers.ValidationError: 1 validation error for OneCallResponse
E   meta -> response
E     instance of Response expected (type=type_error.arbitrary_type; expected_arbitrary_type=Response)
Run Code Online (Sandbox Code Playgroud)

一种解决方案是添加UnionwithMagicMock但我真的不想更改测试代码。事实并非如此。

class Meta(BaseModel):
    raw: Optional[str]
    response: Optional[Union[Response, MagicMock]]

    class Config:
        arbitrary_types_allowed = True

Run Code Online (Sandbox Code Playgroud)

有什么想法如何修补/模拟它吗?

python unit-testing mocking python-3.x pydantic

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

文件编码相同的代码Windows,Linux

有人能告诉我为什么相同的代码在Linux上正常工作但它不在Windows上吗?

在每个操作系统上执行以下代码:

print("Output :" + open("data.txt", "r").read())
Run Code Online (Sandbox Code Playgroud)

在Windows上出现错误:

UnicodeDecodeError: 'charmap' codec can't decode byte 0x98 in position 1: character maps to <undefined>
Run Code Online (Sandbox Code Playgroud)

但在Linux上一切正常:

Output :???
Run Code Online (Sandbox Code Playgroud)

问题出在哪儿 ?

python linux windows text-files python-3.x

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