小编Pet*_*ore的帖子

为什么不是True == True:

样式指南的最后一点http://www.python.org/dev/peps/pep-0008

读......

不要使用==将布尔值与True或False进行比较.

为什么?

编辑只是为了清楚地说明我在问什么(并且它表明了问题本身),当你写作时

if something:
    print "something is true"
Run Code Online (Sandbox Code Playgroud)

您正在进行一个隐式转换为布尔值,这可能会或可能不会取决于真正的含义.恕我直言,这种形式的编程是不鼓励的,因为它可能导致副作用.

numberOfApples = -1
if numberOfApples:
    print "you have apples" # is not what is intended.

if numberOfApples == True:
    print "you have apples" # is also not what is intended.

iHaveApples = numberOfApples > 0
if iHaveApples is True:    # Edit:  corrected this.. the "is" is better than the ==
  print  "you have apples" # is correct.
Run Code Online (Sandbox Code Playgroud)

隐式转换掩盖了逻辑错误.那么为什么风格指南鼓励这个呢?

python

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

如何使用clickhouse选择表?

我是 Clickhouse 的新手,但我记得 SQL 有选择功能。

SELECT column1, column2, column3, ...
INTO newtable 
FROM oldtable
WHERE condition;
Run Code Online (Sandbox Code Playgroud)

Clickhouse 的方法是什么?

clickhouse

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

如何在不添加参数的情况下对基于时间的功能进

我创建了一个函数,它返回剩下的秒数直到下一次出现,但是我遇到了为它编写单元测试的问题.人们如何测试这种有调用功能的功能datetime.now()

添加另一个参数(current_time)似乎是错误的,只是为了测试它,因为它改变了函数的初始要求.

功能测试是.

from datetime import datetime, time, timedelta

def get_time_left(target_time):
    '''return float of number of seconds left until the target_time'''

    if not isinstance( target_time, time ):
        raise TypeError("target_time must be datetime.time")

    curr_time = datetime.now()
    target_datetime = datetime.combine( datetime.today(), target_time )
    if curr_time > target_datetime:
        target_datetime = curr_time + timedelta(1)

    seconds_left = (curr_time - target_datetime).total_seconds()

    return seconds_left
Run Code Online (Sandbox Code Playgroud)

对它的测试是.

class TestDTime(unittest.TestCase):

    def test_time_left(self):
        dt_now = datetime.now()
        tm_5sec_future = ( dt_now + timedelta(0,5) ).time()
        self.assertEqual( dtime.get_time_left(tm_5sec_future), 5.0)
Run Code Online (Sandbox Code Playgroud)

结果是. …

python datetime unit-testing assert mocking

3
推荐指数
1
解决办法
58
查看次数

lambda或其他方法可以用作python中的默认参数吗

我想使用一个默认参数,该默认参数在您调用该函数时进行了初始化,因此每次调用它时,它都是一个不同的默认参数。为了说明我要问的内容,如果没有提供第二时间,则您需要一个默认为当前时间的时间差函数:

通常你会做这样的事情。

def tdiff(target_time, curr_time=None):
   if curr_time == None:
      curr_time = datetime.datetime.now()
   return  curr_time - target_time
Run Code Online (Sandbox Code Playgroud)

但是摆脱if语句会更好。可以用lambda完成吗?(这样的事情)

def tdiff(target_time,  curr_time= lambda : datetime.now() ): 
    return curr_time - target_time
Run Code Online (Sandbox Code Playgroud)

编辑:我知道lambda将不起作用。还有其他方法可行吗?

python lambda

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

为什么在Python中允许其他

我不明白为什么在python中允许这样做?

>>> for i in []:
...   print(i)
... else:
...   print('here')
...
here
Run Code Online (Sandbox Code Playgroud)

如果没有语法错误,应该没有其他吗?else每次也会运行(如果for进行了迭代),因此未连接。

>>> for i in 1,2,3:
...   print(i)
... else:
...   print('here')
...
1
2
3
here
Run Code Online (Sandbox Code Playgroud)

python-2.7 python-3.x

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

clickhouse中如何判断当前选择的数据库

如何找到最后使用use <database>clickhouse 中的命令选择的数据表(即您当前的数据库名称)?

command-line-interface clickhouse

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