小编Way*_*ner的帖子

这是使用isinstance pythonic /"good"吗?

这个问题的另一个副作用是我引导了这篇文章,其中指出:

无论何时使用实例,控制流动叉; 一种类型的对象沿着一条代码路径向下移动,而其他类型的对象沿着另一条对象向下移动 - 即使它们实现了相同的接口!

并暗示这是一件坏事.

但是,我之前使用过这样的代码,我认为这是一种OO方式.类似于以下内容:

class MyTime(object):
    def __init__(self, h=0, m=0, s=0):
        self.h = 0
        self.m = 0
        self.s = 0
    def __iadd__(self, other):
        if isinstance(other, MyTime):
            self.h += other.h
            self.m += other.m
            self.s += other.s
        elif isinstance(other, int):
            self.h += other/3600
            other %= 3600
            self.m += other/60
            other %= 60
            self.s += other
        else:
            raise TypeError('Addition not supported for ' + type(other).__name__)
Run Code Online (Sandbox Code Playgroud)

所以我的问题:

这是isinstance"pythonic"和"好"OOP的用法吗?

python oop

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

在python中使用IFF

有没有办法在python中编写iff语句(即if和only if).我想像使用它一样使用它

for i in range(x)
    iff x%2==0 and x%i==0:
Run Code Online (Sandbox Code Playgroud)

但是,iffPython中没有声明.维基百科为iff定义了真值表,如下所示:

 a |  b  |  iff a and b
-----------------------
 T |  T  |      T
 T |  F  |      F
 F |  T  |      F
 F |  F  |      T
Run Code Online (Sandbox Code Playgroud)

我如何在Python中实现这一目标?

python python-2.7

9
推荐指数
2
解决办法
7659
查看次数

在Python中用verbally格式化数字

pythonistas如何将数字作为单词打印,就像Common Lisp代码的等价物一样:

[3]> (format t "~r" 1e25)
nine septillion, nine hundred and ninety-nine sextillion, nine hundred and ninety-nine quintillion, seven hundred and seventy-eight quadrillion, one hundred and ninety-six trillion, three hundred and eight billion, three hundred and sixty-one million, two hundred and sixteen thousand
Run Code Online (Sandbox Code Playgroud)

python formatting nlp language-comparisons

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

在"文本"小部件中设置光标位置

是否可以在Tkinter Text小部件中设置光标位置?我找不到任何非常有用的东西.

我能做的最好的是在某个xy坐标处发出一个<Button-1><ButtonRelease-1>事件,但这是一个像素数量,而不是字母数量.

python tkinter cursor-position

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

WTForms创建自定义小部件

WTForms文档非常不合适,它们甚至没有向您显示自定义窗口小部件的一个示例,该窗口小部件不是从另一个窗口小部件派生的.

我想创建一个按钮类型,这不是<input>在HTML中:

submit = InlineButton(name='submit', type='submit', title='Save this page', textWithinSpan='Save')
Run Code Online (Sandbox Code Playgroud)

这就是我正在尝试的:

from flask.ext.wtf import Required, Length, EqualTo, Field, TextInput, html_params
from flask import Markup

class InlineButtonWidget(object):
  text = ''
  html_params = staticmethod(html_params)

  def __init__(self, input_type='submit', **kwargs):
    self.input_type = input_type

  def __call__(self, field, **kwargs):
    kwargs.setdefault('id', field.id)
    kwargs.setdefault('type', self.input_type)
    if 'value' not in kwargs:
        kwargs['value'] = field._value()
    return Markup('<button type="submit" %s><span>%s</span></button>' % (self.html_params(name=field.name, **kwargs), kwargs['textWithinSpan']))


class InlineButton(Field):
  widget = InlineButtonWidget()

  def __init__(self, label='', **kwargs):
    self.widget = InlineButtonWidget('submit', label)
  def __call__(self, …
Run Code Online (Sandbox Code Playgroud)

python flask wtforms flask-wtforms

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

是什么导致这个sqlite外键不匹配?

我已经检查了这个问题,并且认为我有答案 - 但那时我看起来并不合适.

我有以下简化示例:

CREATE TABLE pipelines (                                                        
        name VARCHAR NOT NULL,                                                  
        owner VARCHAR NOT NULL,                                                 
        description VARCHAR,                                                    
        PRIMARY KEY (name, owner),                                              
        FOREIGN KEY(owner) REFERENCES user (id)                                 
);                                                                              
CREATE TABLE tasks (                                                            
        id INTEGER NOT NULL,                                                    
        title VARCHAR,                                                          
        pipeline VARCHAR,                                                       
        owner VARCHAR,                                                          
        PRIMARY KEY (id),                                                       
        FOREIGN KEY(pipeline) REFERENCES pipelines (name),                      
        FOREIGN KEY(owner) REFERENCES pipelines (owner)                         
);                                                                              
CREATE TABLE user (                                                           
        id VARCHAR NOT NULL,                                                    
        name VARCHAR,                                                           
        password VARCHAR,                                                       
        PRIMARY KEY (id)                                                        
);                                                                              
pragma foreign_keys=on;                                                         

insert into user values …
Run Code Online (Sandbox Code Playgroud)

sqlite foreign-keys

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

这是使用 dateutil 设置时区的正确方法吗?

>>> import dateutil.parser, dateutil.tz as tz
>>> dateutil.parser.parse('2017-08-09 10:45 am').replace(tzinfo=tz.gettz('America/New_York'))
datetime.datetime(2017, 8, 9, 10, 45, tzinfo=tzfile('/usr/share/zoneinfo/America/New_York'))
Run Code Online (Sandbox Code Playgroud)

真的是我们应该为解析设置默认时区的方式吗?我已经阅读了解析器示例的文档,但似乎找不到任何说“这是为 dateutil.parser.parse 设置默认时区的方法”,甚至是类似内容的内容。

因为虽然这有效,但如果提供了区域,在某些情况下它会做错事。这是否意味着我们应该这样做?

>>> d = dateutil.parser.parse('2017-08-09 10:45 am +06:00')
>>> d = d.replace(tzinfo=d.tzinfo or tz.gettz('America/Chicago'))
Run Code Online (Sandbox Code Playgroud)

因为它也很笨重。

解析时设置默认时区的推荐方法是什么?

python timezone datetime python-dateutil

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

如何使Windows 10 Terminal启动WSL?

我正在使用新的Windows终端,并试图使其启动我的WSL终端。这是我要使用的设置:

        {
            "acrylicOpacity" : 0.75,
            "closeOnExit" : true,
            "colorScheme" : "Campbell",
            "commandline" : "%LOCALAPPDATA%/wsltty/bin/mintty.exe --WSL= --configdir='%APPDATA%/wsltty' -~ ",
            "cursorColor" : "#FFFFFF",
            "cursorShape" : "bar",
            "fontFace" : "Consolas",
            "fontSize" : 10,
            "guid" : "{0caa0dad-35be-5f56-a8ff-afceeeaa6101}",
            "historySize" : 9001,
            "icon" : "ms-appx:///ProfileIcons/{0caa0dad-35be-5f56-a8ff-afceeeaa6101}.png",
            "name" : "wsl",
            "padding" : "0, 0, 0, 0",
            "snapOnInput" : true,
            "startingDirectory" : "%USERPROFILE%",
            "useAcrylic" : true
        }
Run Code Online (Sandbox Code Playgroud)

但是,它所做的只是打开某种CMD。

什么是运行WSL终端的正确命令

编辑:

我确实注意到GUID与常规CMD是相同的,所以我更改了它。然后它没有启动一个外部的外壳。

windows-subsystem-for-linux windows-terminal

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

为什么 git blame --ignore-rev/--ignore-revs-file 对我不起作用?

git blame --ignore-revs-file显然是现代 Git 中存在的一个选项。

只有一个问题。它不起作用。

或者至少,它对我不起作用:

您可以将其添加到 shell 脚本中:

mkdir -p /tmp/blarp
cd /tmp/blarp
git init
cat << EOF > file.txt
one
two
three
EOF
git add file.txt
git commit --author "One <one@example.com>" -m 'one commit'
cat << EOF > file.txt
one
awesome
three
EOF
git add file.txt
git commit --author "Two <two@example.com>" -m 'two commits'
cat << EOF > file.txt
one
awesome
sauce
EOF
git add file.txt
git commit --author "One <one@example.com>" -m 'three commits'
git rev-parse …
Run Code Online (Sandbox Code Playgroud)

git ignore revision-history

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

Lisp数据安全/验证

在这一点上,这对我来说真的只是一个概念性的问题.

在Lisp中,程序是数据,数据是程序.REPL正是这样做 - 读取然后进行评估.

那么如何以安全的方式从用户那里获得输入呢?显然它是可能的 - 我的意思是viaweb - 现在雅虎!商店非常安全,所以它是如何完成的?

lisp security validation

7
推荐指数
2
解决办法
1076
查看次数