小编Lau*_*RTE的帖子

typescript-eslint-parser 未正式支持的 TypeScript 版本

我继承了一个旧的AngularJs应用程序,它使用遗留工具:Bowergrunt

\n

当我运行时grunt serve --reload,我收到以下警告消息:

\n
WARNING: You are currently running a version of TypeScript which is not officially supported by typescript-eslint-parser.\n\nYou may find that it works just fine, or you may not.\n\nSUPPORTED TYPESCRIPT VERSIONS: ~2.3.2\n\nYOUR TYPESCRIPT VERSION: 2.2.2\n\nPlease only submit bug reports when using the officially supported version.\n
Run Code Online (Sandbox Code Playgroud)\n

这很奇怪,因为我使用最新版本的 Typescript:

\n
WARNING: You are currently running a version of TypeScript which is not officially supported by typescript-eslint-parser.\n\nYou may find that …
Run Code Online (Sandbox Code Playgroud)

gruntjs typescript eslint

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

免费XSD到DTD转换实用程序?

我有一个XSD(XML Schema),我需要转换为DTD.有没有免费的实用工具或简单的方法来实现这一目标?以XSLT为例?Python脚本也是受欢迎的.

注意:这是Free DTD到XSD转换实用程序问题的相反方向

我知道转换可能会丢失信息......

我尝试过XMLSpy或oXygenXML,但它不是免费的,我没有定制转换的可能性.

xsd dtd converters

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

为什么使用__eq__运算符多次评估NotImplemented

不要混淆苹果和橘子

问题

我正在玩__eq__操作员和NotImplemented价值.

我试图了解obj1.__eq__(obj2)返回时会发生什么NotImplemented,obj2.__eq__(obj1)也会返回NotImplemented.

根据为什么返回NotImplemented而不是引发NotImplementedError的答案,以及详细的文章如何在"LiveJournal"博客中覆盖Python中的比较运算符,运行时应该回归到内置行为(基于身份==!=).

代码示例

但是,尝试下面的示例,似乎我__eq__对每对对象都有多个调用.

class Apple(object):
    def __init__(self, color):
        self.color = color

    def __repr__(self):
        return "<Apple color='{color}'>".format(color=self.color)

    def __eq__(self, other):
        if isinstance(other, Apple):
            print("{self} == {other} -> OK".format(self=self, other=other))
            return self.color == other.color
        print("{self} == {other} -> NotImplemented".format(self=self, other=other))
        return NotImplemented


class Orange(object):
    def __init__(self, usage):
        self.usage = usage

    def __repr__(self): …
Run Code Online (Sandbox Code Playgroud)

python operators python-2.7

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

使用PyCharm阴影内置名称"function"和"module"

我有以下Python代码:

function = "Developer"
module = "something"
print(function + " on " + module)
Run Code Online (Sandbox Code Playgroud)

使用PyCharm 2017,我有一个泡泡,上面写着"Shadows内置名称"功能"/"模块"与PyCharm".

我很惊讶,因为"函数"和"模块"不是内置名称.它们也不是关键字:

import __builtin__
import keyword

assert "function" not in dir(__builtin__)  # -> OK
assert "module" not in dir(__builtin__)    # -> OK
assert "function" not in keyword.kwlist    # -> OK
assert "module" not in keyword.kwlist      # -> OK
Run Code Online (Sandbox Code Playgroud)

怎么了?

我使用的是CPython 2.7,但3.5和3.6也有同样的问题.

编辑:

__builtin__现在builtins是Python 3.

python pycharm

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

re.sub(".*",","(替换)","text")在Python 3.7上加倍替换

在Python 3.7(在Windows 64位上测试),使用RegEx替换字符串.*会使输入字符串重复两次!

在Python 3.7.2上:

>>> import re
>>> re.sub(".*", "(replacement)", "sample text")
'(replacement)(replacement)'
Run Code Online (Sandbox Code Playgroud)

在Python 3.6.4上:

>>> import re
>>> re.sub(".*", "(replacement)", "sample text")
'(replacement)'
Run Code Online (Sandbox Code Playgroud)

在Python 2.7.5(32位)上:

>>> import re
>>> re.sub(".*", "(replacement)", "sample text")
'(replacement)'
Run Code Online (Sandbox Code Playgroud)

怎么了?如何解决?

python python-regex

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

从另一个virtualenv创建virtualenv

我们可以从现有的virtualenv 创建virtualenv以继承已安装的库吗?

详细地:

我首先创建一个"引用"virtualenv,并添加库(修复版本):

virtualenv ref
source ref/bin/activate
pip install -U pip==8.1.1     # <- I want to fix the version number
pip install -U wheel==0.29.0  # <- I want to fix the version number
Run Code Online (Sandbox Code Playgroud)

然后:

virtualenv -p ref/bin/python myapp
source myapp/bin/activate
pip list
Run Code Online (Sandbox Code Playgroud)

我明白了:

pip (1.4.1)
setuptools (0.9.8)
wsgiref (0.1.2)
Run Code Online (Sandbox Code Playgroud)

如何获取我安装的库?

类似的问题

我看到了一个类似的问题:virtualenv可以继承另一个吗?.

但我想要一个孤立的virtualenv,它不使用引用的virtualenv,除了库安装.因此,将指定的目录添加到当前活动的virtualenv的Python路径中,不是解决方案.

为什么这样做?

好吧,我们有一个集成服务器来构建应用程序(用于发布和持续集成),我们希望保持对库版本的控制并使构建更快.

创建一个可重定位的virtualenv

我想我可以使用一个可重定位的virtualenv,就这样:

  1. 创建ref virtualenv
  2. 让它可重新定位:``virtualenv --relocatable ref```

对于"myapp":

  • ref复制到myapp

您如何看待这个解决方案?对于可分发的版本是否可靠?

python virtualenv

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

为什么ast.literal_eval('5*7')会失败?

为什么文字评估5 * 7失败,而5 + 7不是?

import ast

print(ast.literal_eval('5 + 7'))
# -> 12

print(ast.literal_eval('5 * 7'))
# -> 
Traceback (most recent call last):
  ...
ValueError: malformed node or string: <_ast.BinOp object at ...>
Run Code Online (Sandbox Code Playgroud)

文件没有解释这一点.

在SO上回答这个问题之后我发现了这个问题:得到一个字符串的结果.

python eval abstract-syntax-tree

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

setup.py-配置私人/商业项目

我可以在setup.py项目配置文件中放些什么,以告诉开发人员该项目是私有/商业应用程序/库。

目前我设定:

setup(
    name='MyProject',
    version='0.1.0',
    license='(c) My Company',
    ...
)
Run Code Online (Sandbox Code Playgroud)

有最佳做法吗?

注意:

如今,大多数项目都是开源的,并且遵循许可证模型。但是,当您从事该行业时,软件是私有的。我的公司与离岸公司合作,这些公司可能不知道软件可以是私有的。因此,我想通过在setup.py文件中指定它来引起他们的注意。这就是为什么我在寻找最佳实践的原因。

结论/解决方案

对于私人/专有应用程序,我将遵循rth的建议

  • 将许可证属性设置为“专有”,
  • 添加分类器“许可证::其他/专有许可证”,
  • 并可能添加一个LICENSE文件。

模板将是这样的:

setup(
    name='MyProject',
    version='0.1.0',
    license="Proprietary",
    classifiers=[
        'License :: Other/Proprietary License',
        ...
    ],
    ...
)
Run Code Online (Sandbox Code Playgroud)

一种替代方法是设置“非开源”,如cookiecutter-pypackage模板中定义的那样

python python-packaging

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

在finally子句中返回值是否错误?

如果尝试以下代码,则会看到未返回正常的块返回值,但最终的块返回值为:

>>> def f():
...     try:
...         return "normal"
...     finally:
...         return "finally"
... 
>>> f()
'finally'
Run Code Online (Sandbox Code Playgroud)

一个更高级的示例是在每个return语句中调用一个函数:

在这种情况下,我可以看到:

  • 在普通块中,对show函数进行求值(但不返回),
  • 在finally块中,对show函数进行求值并返回:
>>> def show(x):
...     print(x)
...     return x
... 
>>> def g():
...     try:
...         return show("normal")
...     finally:
...         return show("finally")
... 
>>> g()
normal
finally
'finally'
Run Code Online (Sandbox Code Playgroud)

在finally子句中有return语句是一种好习惯吗?还是它是一个潜在的错误(应通过代码分析工具或代码审查来检测)?

编辑

另一个有例外的示例:

>>> def f():
...     try:
...         raise ValueError("bad")
...     finally:
...         return "good"
... 
>>> f()
'good'
Run Code Online (Sandbox Code Playgroud)

奇怪的!

python try-finally python-3.x

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

在不覆盖现有文件的情况下解压缩存档

如何在不覆盖现有文件的情况下解压缩存档?

ZipFile.extractall功能是在提取 ZIP 文件的同时覆盖现有文件。

所以,我写了自己的函数:

import os
import zipfile


def unzip(src_path, dst_dir, pwd=None):
    with zipfile.ZipFile(src_path) as zf:
        members = zf.namelist()
        for member in members:
            arch_info = zf.getinfo(member)
            arch_name = arch_info.filename.replace('/', os.path.sep)
            dst_path = os.path.join(dst_dir, arch_name)
            dst_path = os.path.normpath(dst_path)
            if not os.path.exists(dst_path):
                zf.extract(arch_info, dst_dir, pwd)
Run Code Online (Sandbox Code Playgroud)

但是,完整的实现可能需要重新实现该extract方法。

有没有办法解压缩存档而忽略现有文件?相当于unzip -n arch.zip?

python unzip

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