小编Ale*_*sen的帖子

为什么"1.real"是语法错误但"1 .real"在Python中有效?

所以我在twitter上看到了这两个 问题.如何1.real语法错误但1 .real不是?

>>> 1.real
  File "<stdin>", line 1
    1.real
         ^
SyntaxError: invalid syntax
>>> 1 .real
1
>>> 1. real
  File "<stdin>", line 1
    1. real
          ^
SyntaxError: invalid syntax
>>> 1 . real
1
>>> 1..real
1.0
>>> 1 ..real
  File "<stdin>", line 1
    1 ..real
       ^
SyntaxError: invalid syntax
>>> 1.. real
1.0
>>> 1 .. real
  File "<stdin>", line 1
    1 .. real
       ^
SyntaxError: invalid syntax
Run Code Online (Sandbox Code Playgroud)

python

31
推荐指数
2
解决办法
1834
查看次数

设置公共Juypter服务器时SSL:WRONG_VERSION_NUMBER

我正在设置一个Juypter服务器来托管我的笔记本电脑.

/home/user/.jupyter/notebook_configuration.py

c.NotebookApp.certfile = u'/home/user/.jupyter/mycert.pem'
c.NotebookApp.keyfile = u'/home/user/.jupyter/mykey.key'
Run Code Online (Sandbox Code Playgroud)

如果我在控制台上跑

jupyter notebook --ip="ip_address" --port=8000 --certfile=mycert.pem --keyfile mykey.key
Run Code Online (Sandbox Code Playgroud)

服务器和证书工作!

但是,当我设置DNS条目并尝试路由到服务器时,我遇到了这个错误

SSL Error on 10 ('ip_address', 63748): [SSL: WRONG_VERSION_NUMBER] wrong version number (_ssl.c:600)
Run Code Online (Sandbox Code Playgroud)

这是怎么回事?

ssl jupyter jupyter-notebook

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

结合两个 Cloudwatch 洞察查询

我有两个 Cloudwatch 见解查询,我希望能够并行运行并比较两者的结果。

stats count(*) as requestIdCount by @requestId 
| filter @message like /START RequestId/
| filter requestIdCount > 1
Run Code Online (Sandbox Code Playgroud)
stats count(*) as requestIdCount by @requestId 
| filter @message like /END RequestId/
| filter requestIdCount > 1
Run Code Online (Sandbox Code Playgroud)

能够做到这一点会很棒

fields (
    stats count(*) as requestIdCount by @requestId 
    | filter @message like /END RequestId/
    | filter requestIdCount > 1) as EndRequestCount,
       (
    stats count(*) as requestIdCount by @requestId 
    | filter @message like /START RequestId/
    | filter requestIdCount > 1) as StartRequestCount 
Run Code Online (Sandbox Code Playgroud)

但是我现在看不到任何方法可以在见解中进行子查询。有没有一种方法可以组合这样的查询?

amazon-cloudwatch

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

如何使对象成为Python2和Python3迭代器?

这篇Stack Overflow帖子是关于使一个对象成为Python中的迭代器.

在Python 2中,这意味着您需要实现一个__iter__()方法和一个next()方法.但是在Python 3中,您需要实现一个不同的方法,而不是next()您需要实现的方法__next__().

如何在Python 2和3中创建一个迭代器对象?

python iterator python-2.7 python-3.x

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

如何获取MyPy的正则表达式模式类型

如果我编译一个正则表达式

>>> type(re.compile(""))
<class '_sre.SRE_Pattern'>
Run Code Online (Sandbox Code Playgroud)

并希望将该正则表达式传递给函数并使用Mypy来键入check

def my_func(compiled_regex: _sre.SRE_Pattern):
Run Code Online (Sandbox Code Playgroud)

我遇到了这个问题

>>> import _sre
>>> from _sre import SRE_Pattern
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
ImportError: cannot import name 'SRE_Pattern'
Run Code Online (Sandbox Code Playgroud)

您似乎可以导入_sre但由于某种原因SRE_Pattern不可导入.

python mypy

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


Apache 网络服务器和 Flask 应用程序

我有在 Ubuntu 14.04 上运行的 apache2 Web 服务器。默认情况下,当我启动 apache Web 服务器时,http://localhost我可以看到从 /var/www/html/index.html 文件启动的“Apache2 Ubuntu 默认页面”。

我在 /home/ubuntu/myprojects/ 位置有一个烧瓶应用程序。Flask 应用程序在 virtualenv 上运行,并具有适当的文件夹结构来呈现 html 文件。下面是文件夹结构

/home/ubuntu/myprojects/hello/hello.py(flask 应用程序渲染 html)/home/ubuntu/myprojects/hello/templates/hello.html

Flask 应用程序代码如下:

from flask import Flask
from flask import render_template
from flask import request

app = Flask(__name__)

@app.route('/')
def my_form():
        return render_template("hello.html")

if __name__ == '__main__':
        app.debug = True
        app.run(host='0.0.0.0')
Run Code Online (Sandbox Code Playgroud)

当我运行http://localhost:5000hello.html 时呈现。我想在http://localhost没有指定任何端口号的情况下调用hello.py flask 应用程序时呈现 hello.html 。为此,我添加了以下代码:

app.run(host='0.0.0.0', port=80)
Run Code Online (Sandbox Code Playgroud)

但是,当我运行 Flask 应用程序时,存在错误:

 * Running on http://0.0.0.0:80/ (Press CTRL+C to quit) …
Run Code Online (Sandbox Code Playgroud)

python apache flask ubuntu-14.04

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

修补导入的模块时模拟返回 ImportError

我在模拟函数时遇到了一些麻烦。所述函数被导入并用于run_parsers.py,我得到

ImportError: 'No module named run_parsers'
Run Code Online (Sandbox Code Playgroud)

当我尝试mock.patch run_parsers.py.

这是我的测试代码 test_run_parsers.py

from .. import run_parsers # Used in all my other tests.

def test_node_data_parser_throws_exception(self):
    def parser():
        return NotImplementedError()

    with mock.patch("run_parsers.get_node_paths") as node_paths:
        node_paths.return_value = "node_1"
        run_parsers.get_node_data(parser, "/a/path")
Run Code Online (Sandbox Code Playgroud)

这是我的存储库结构

control_scripts
??? __init__.py
??? README.md
??? run_all_parsers.py
??? run_parsers.py
??? tests
    ??? __init__.py
    ??? test_run_parsers.py
Run Code Online (Sandbox Code Playgroud)

根据本教程,我应该模拟导入函数的位置。这就是为什么我试图模拟调用模块而不是定义 get_node_paths 的模块

python unit-testing mocking

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

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

为什么 'é' 和 'é' 编码为不同的字节?

为什么在我的代码库的不同部分将相同的字符编码为不同的字节?

语境

我有一个单元测试,它生成一个临时文件树,然后检查以确保我的扫描确实找到了有问题的文件。

def test_unicode_file_name():
    test_regex = "é"
    file_tree = {"files": ["é"]} # File created with python.open()
    with TempTree(file_tree) as tmp_tree:
        import pdb; pdb.set_trace()
        result = tasks.find_files(test_regex, root_path=tmp_tree.root_path)
        expected = [os.path.join(tmp_tree.root_path, "é")]
        assert result == expected
Run Code Online (Sandbox Code Playgroud)

失败的功能

for dir_entry in scandir(current_path):
    if dir_entry.is_dir():
        dirs_to_search.append(dir_entry.path)

    if dir_entry.is_file():
        testing = dir_entry.name
        if filename_regex.match(testing):
            results.append(dir_entry.path)
Run Code Online (Sandbox Code Playgroud)

PDB 会话

当我开始深入研究时,我发现测试字符(从我的单元测试中复制)和dir_entry.name编码为不同字节的字符。

(Pdb) testing
'é'
(Pdb) 'é'
'é'
(Pdb) testing == 'é'
False
(Pdb) testing in 'é'
False
(Pdb) type(testing)
<class 'str'>
(Pdb) type('é') …
Run Code Online (Sandbox Code Playgroud)

python unicode normalization python-3.x

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