小编Yuv*_*uss的帖子

Python类型提示有异常

我有一个看起来像这样的函数:

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)

我的问题是:我应该在提示中提及错误吗?

python exception type-hinting python-3.x

32
推荐指数
3
解决办法
5389
查看次数

枚举值的Python类型注释

我有这段代码:

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

python typing type-hinting python-3.x type-annotation

20
推荐指数
3
解决办法
5283
查看次数

更改X-Frame-Options以允许所有域

我试图使用我的一些网站作为我的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.

linux ubuntu nginx cors x-frame-options

18
推荐指数
2
解决办法
3万
查看次数

如何在mypy中使用reveal_type

我已经读过,我可以通过使用一个被调用的函数来揭示变量的类型reveal_type,但我找不到如何使用它或从哪里导入它.

python type-hinting mypy

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

使用模板返回值从函数中删除类型

我有以下课程:

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)

c++ c++11

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

python asyncio add_done_callback with async def

我有两个函数:第一个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_badd_done_callback函数def_b

python coroutine python-3.x async-await python-asyncio

12
推荐指数
2
解决办法
8053
查看次数

python输入模块缺少python 3.5中的Coroutine类

我正在尝试编写使用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.

python typing type-hinting python-3.x python-3.5

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

我可以在一个Python pytest方法中处理多个断言吗?

我正在编写测试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个断言吗?

python unit-testing pytest python-2.7 python-3.x

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

MyPy:在python 3.5代码中需要变量的类型注释

我正在使用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

python type-hinting python-3.x mypy

8
推荐指数
2
解决办法
6763
查看次数

如何从反应中的渲染函数返回一个空的 jsx 元素?

我有一个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)

javascript jsx reactjs react-native

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