我有一个看起来像这样的函数:
def check_for_errors(result):
if 'success' in result:
return True
if 'error' in result:
raise TypeError
return False
Run Code Online (Sandbox Code Playgroud)
在成功运行此函数时,我应该得到一个bool
,但是如果有错误我应该得到一个TypeError
- 这是可以的,因为我在另一个函数中处理它.
我的函数第一行看起来像这样:
def check_for_errors(result: str) -> bool:
Run Code Online (Sandbox Code Playgroud)
我的问题是:我应该在提示中提及错误吗?
我有这段代码:
import enum
class Color(enum.Enum):
RED = '1'
BLUE = '2'
GREEN = '3'
def get_color_return_something(some_color):
pass
Run Code Online (Sandbox Code Playgroud)
some_color
如果我想从Color枚举中接收值(例如:),如何在此函数中的变量中正确添加类型注释Color.RED
?
我试图使用我的一些网站作为我的iframe
一个不同的网站.
我的问题是 - 其他网站始终不断更改其IP地址,并且没有域名.
所以,我读到你可以将这个lint添加到/etc/nginx/nginx.conf
:
add_header X-Frame-Options "ALLOW-FROM https://subdomain.example.com/";
Run Code Online (Sandbox Code Playgroud)
我的问题是:可以允许我的网站从所有IP地址和域中导入为iframe吗?为了达到这个目的,我应该写些什么?
我使用的是Ubuntu 16.04和nginx 1.10.0.
我已经读过,我可以通过使用一个被调用的函数来揭示变量的类型reveal_type
,但我找不到如何使用它或从哪里导入它.
我有以下课程:
class A {
public:
virtual std::string Serialize();
virtual void Deserialize(std::string);
template <typename T>
T* Clone()
{
std::string s = Serialize();
T* t = new T();
t->Deserialize(s);
return t;
}
};
class B : public A {
public:
std::string Serialize() { ... }
void Deserialize(std::string) { ... }
};
Run Code Online (Sandbox Code Playgroud)
现在,如果我想要克隆B,我会执行以下操作:
B b1;
B* b2 = b1.Clone<B>();
Run Code Online (Sandbox Code Playgroud)
有没有办法删除模板类型而不Clone
在每个派生类中重新实现?
我想要这样的东西:
B b1;
B* b2 = b1.Clone();
Run Code Online (Sandbox Code Playgroud) 我有两个函数:第一个def_a
是异步函数,第二个是def_b
常规函数,调用结果def_a
作为add_done_callback
函数的回调函数.
我的代码看起来像这样:
import asyncio
def def_b(result):
next_number = result.result()
# some work on the next_number
print(next_number + 1)
async def def_a(number):
await some_async_work(number)
return number + 1
loop = asyncio.get_event_loop()
task = asyncio.ensure_future(def_a(1))
task.add_done_callback(def_b)
response = loop.run_until_complete(task)
loop.close()
Run Code Online (Sandbox Code Playgroud)
它的工作非常完美.
当第二个函数def_b
变为异步时,问题就开始了.现在它看起来像这样:
async def def_b(result):
next_number = result.result()
# some asynchronous work on the next_number
print(next_number + 1)
Run Code Online (Sandbox Code Playgroud)
但是现在我无法将它提供给add_done_callback
函数,因为它不是常规函数.
我的问题是 - 它是否可能,如果是异步的,我怎么能提供def_b
给add_done_callback
函数def_b
?
我正在尝试编写使用Coroutine
该类的代码,如输入文档中所述.
它看起来像是可用的python 3.5
,但是当我打字导入它时会抛出一个ImportError
:
In [1]: from typing import Coroutine
Run Code Online (Sandbox Code Playgroud)
ImportError:无法导入名称'Coroutine'
然后,我尝试在Python 3.6中运行代码,它工作正常.这门课没有python 3.5
?如果没有,为什么它出现在文档中(特别是python 3.5)?我试着用它来运行它python 3.5.2
.
我正在编写测试pytest
,我遇到了下一个问题:我有一个测试某个变量的测试,然后我执行一些繁重的计算,然后我想执行另一个测试.
问题是 - 如果第一次assert
失败,整个测试失败,并且pystest
不执行第二次测试.代码:
class TestSomething:
def tests_method(self, some_variables):
# Some actions that take a lot of time!
assert some_var == 1
# Some actions that take a lot of time!
assert some_var == 2
Run Code Online (Sandbox Code Playgroud)
我知道这种测试方法可以分为两种方法,但这里的性能问题至关重要.
有一种方法可以在一种方法中运行2个断言吗?
我正在使用mypy
我的python 3.5代码,我收到了很多看起来像这样的消息:
file:行号:错误:需要变量的类型注释
但我读到了新功能,python 3.6
因为它仅在python 3.6
以下内容中引入了变量注释的语法:
PEP 484引入了函数参数类型注释的标准,即类型提示.这个PEP为Python添加语法,用于注释变量类型,包括类变量和实例变量......
如果我试图在python 3.5
程序中的变量中添加变量类型注释,它会抛出SyntaxError
.
我该怎么办?忽略这条消息?更新到python 3.6
?为什么mypy
编写我的代码就像它写的一样python 3.6
?
我有一个react
看起来像这样的代码:
class UserManagement extends React.Component {
constructor() {
super();
this.state = {
users: undefined,
};
}
componentWillMount() {
// load the users state with a Promise
setTimeout(() => {
this.setState({users: []});
}, 800);
}
render() {
if ( this.state.users === undefined ) {
// until the users state is updated, I want to return an empty element
return null;
}
// real site rendering
return <div>We have users</div>;
}
}
ReactDOM.render(
<UserManagement />,
document.getElementById("root")
);
Run Code Online (Sandbox Code Playgroud)
<div>Will be blank …
Run Code Online (Sandbox Code Playgroud)python ×7
python-3.x ×6
type-hinting ×5
mypy ×2
typing ×2
async-await ×1
c++ ×1
c++11 ×1
coroutine ×1
cors ×1
exception ×1
javascript ×1
jsx ×1
linux ×1
nginx ×1
pytest ×1
python-2.7 ×1
python-3.5 ×1
react-native ×1
reactjs ×1
ubuntu ×1
unit-testing ×1