过去一小时我一直坚持这一点.我已经插入登录到我的tkinter gui,但无法让它工作.然后我开始删除部分,直到我在非常python文档中的裸骨示例,它将无法正常工作.在这一点上,我没有别的东西要删除.
代码如下:
import logging
LOG_FILENAME = r'logging_example.out'
logging.basicConfig(filename=LOG_FILENAME ,level=logging.DEBUG)
logging.debug('This message should go to the log file')
logging.info('So should this')
logging.warning('And this, too')
f = open(LOG_FILENAME, 'rt')
try:
body = f.read()
finally:
f.close()
print('FILE:')
print (body)
Run Code Online (Sandbox Code Playgroud)
警告将打印到stdout,但不会生成该文件.我在Windows 7上运行python 3.4,x64.它是一个anacondas发行版,所以这是在Spyder里面的Ipython中运行的.
我想这应该有效
我想在Python上使用xlrd导入包含文本,数字和日期的文件.
我尝试过类似的东西:
if "/" in worksheet.cell_value:
do_this
else:
do_that
Run Code Online (Sandbox Code Playgroud)
但这没用,因为后者发现日期存储为浮点数,而不是字符串.要将它们转换为datetime类型我做了:
try:
get_row = str(datetime.datetime(*xlrd.xldate_as_tuple(worksheet.cell_value(i, col - 1), workbook.datemode)))
except:
get_row = unicode(worksheet.cell_value(i, col - 1))
Run Code Online (Sandbox Code Playgroud)
当单元格包含文本时,我有一个例外.现在我想将数字作为数字和日期作为日期,因为现在所有数字都转换为日期.
有任何想法吗?
我有以下代码用于在python 3.5中延迟计算值。我也尝试过用@cached_property
装饰器获得相同的结果,因此为了简单起见,我将使用它。
class Foo:
@property
def expensive_object(self):
if not hasattr(self, "_expensive_object"):
print("Lengthy initialization routine")
self._expensive_object = 2
return self._expensive_object
Run Code Online (Sandbox Code Playgroud)
问题在于,即使我最终没有在内部使用它,当我将其作为函数的参数传递给它时,它也会被评估,如以下示例所示:
def bar(some_parameter, another_parameter):
if some_parameter != 10:
print(some_parameter)
else:
print(another_parameter)
Run Code Online (Sandbox Code Playgroud)
从下面的输出中,我们看到它只是通过传递而得到评估的,但是由于代码没有尝试使用它,因此它不是严格必需的。
In [23]: foo1 = Foo()
...: bar(3, foo1.expensive_object)
Lengthy initialization routine
3
In [24]: bar(3, foo1.expensive_object)
3
Run Code Online (Sandbox Code Playgroud)
在某些情况下,我的脚本可以运行而无需评估,但由于这种情况,最终还是要这样做。排除参数也是不实际的。我还在__init__
组合成员对象中使用它。
如果可能的话,我想使该属性更加懒惰,因为它仅在实际读取时才进行评估。