我是 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,我想到的第一件事是创建获取数据并在需要的地方使用的服务层。
这是正确的方法吗?在哪里可以找到一些可以从中汲取灵感的真实示例?
大家好,我是 TypeScript 的新手,也许我没有看到明显的东西,但这是我的问题:
const someSet = new Set();
someSet.add(1)
console.log(someSet[0])
Run Code Online (Sandbox Code Playgroud)
给了我 undefined 谁能解释一下为什么我们不能通过索引获得价值?我该怎么做?
我想知道在哪里配置和初始化与日志记录模块相关的内容?
例如,我编写了一些类,并且想在执行方法时记录一些信息。我应该在模块顶部的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)
最佳做法是什么?
我正在为我的 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)
有什么想法如何修补/模拟它吗?
有人能告诉我为什么相同的代码在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 ×3
python-3.x ×2
axios ×1
collections ×1
linux ×1
logging ×1
mocking ×1
nuxt.js ×1
project ×1
pydantic ×1
set ×1
text-files ×1
typescript ×1
unit-testing ×1
vue.js ×1
vuex ×1
windows ×1