草庐IT

format_time

全部标签

python - 内部类 : How can I get the outer-class object at construction time?

考虑以下Python(在2.x或3.x中运行):classOuter(object):passclassInner(object):def__init__(self):print("Inner.self",self)o=Outer()i=o.Inner()我想在Inner.__init__()中接触到o。但是:我不希望o成为Inner的显式参数。我希望O.Inner和o.Inner成为一个类对象,而不是像闭包那样奇怪的东西。你能建议我如何实现这一目标吗?目前我最好的想法是使用线程本地存储。在我的用例中,每当我构造一个o.Inner()时,我已经在某个地方的o上的方法中,这没什么大不了的

python - 计算time-1到time-2之间的时间?

entertime-1//eg01:12entertime-2//eg18:59calculate:time-1totime-2/12//i.etimebetween01:12to18:59dividedby12如何在Python中完成。我是初学者,所以我真的不知道从哪里开始。编辑添加:我不想要计时器。time-1和time-2都是用户手动输入的。预先感谢您的帮助。 最佳答案 您需要内置datetime模块中的datetime和timedelta类。fromdatetimeimportdatetime#Parsethetimestr

python - 如何将 tuple1 if ... else tuple2 传递给 str.format?

简单来说,为什么会出现以下错误?>>>yes=True>>>'no[{0}]yes[{1}]'.format(("","x")ifyeselse("x",""))Traceback(mostrecentcalllast):File"",line1,inIndexError:tupleindexoutofrange我使用的是python2.6。 最佳答案 ☞索引选项:在格式字符串中访问参数项时,应该使用索引来调用值:yes=Trueprint'no[{0[0]}]yes[{0[1]}]'.format(("","x")ifyesels

python - psycopg2 类型错误 : not all arguments converted during string formatting

我正在尝试执行一个简单的查询,但无论我如何传递参数都会出现此错误。这是查询(我正在使用Trac数据库对象连接到数据库):cursor.execute("""SELECTnameFROM"%s".customerWHEREfirm_id='%s'"""%(schema,each['id']))schema和each['id']都是简单的字符串print("""SELECTnameFROM"%s".customerWHEREfirm_id='%s'"""%(schema,each['id']))结果:SELECTnameFROM"Planing".customerWHEREfirm_id=

python - py.test : format failed assert AND print custom message

py.testassertdocs说...ifyouspecifyamessagewiththeassertionlikethis:asserta%2==0,"valuewasodd,shouldbeeven"thennoassertionintrospectiontakesplacesatallandthemessagewillbesimplyshowninthetraceback.Python的内置unittest模块也执行此操作,除非您的TestCase设置longMessage=True.拥有漂亮的断言格式对测试开发人员友好,而自定义消息对业务需求/人性化更友好。当您不在测试上

python - 正则表达式速度 : Python x6 times faster than C++11 under VS2013?

难道是python的Cregex实现快了6倍还是我遗漏了什么?Python版本:importrer=re.compile(r'(HELLO).+?(\d+)',re.I)s=r"prefixdfadfadfadfadfadfadfheasdfdHelloRegex123"%timeitr.search(s)1000000loops,bestof3:1.3µsperloop(769,000persec)C++11版本:#includeintmain(intargc,char*argv[]){std::strings="prefixdfadfadfadfadfadfadfheasdfdHe

python - 将带@times 的 bsxfun 转换为 numpy

这是我在Octave中的代码:sum(bsxfun(@times,X*Y,X),2)代码的bsxfun部分产生逐元素乘法,所以我认为numpy.multiply(X*Y,X)可以解决问题,但我遇到了一个异常(exception)。当我做了一些研究时,我发现按元素乘法不适用于Python数组(特别是如果X和Y的类型为“numpy.ndarray”)。所以我想知道是否有人可以对此进行更多解释——也就是说,将类型转换为不同类型的对象会起作用吗?Octave代码有效,所以我知道我没有线性代数错误。我假设bsxfun和numpy.multiply实际上并不等同,但我不确定为什么这样任何解释都会很

python - Django : RunTimeWarning : DateTimeField received a naive datetime while time zone support is active

我正在尝试基于djangocart测试购物车创建但是当我尝试创建购物车时出现此错误:RunTimeWarning:DateTimeFieldreceivedanaivedatetimewhiletimezonesupportisactive我做了一些研究,但无法解决datetime.datetime.now()的问题test_views.py在我的测试目录中:fromdjango.testimportTestCase,Client,RequestFactoryimportunittestfromdjango.contrib.auth.modelsimportUser,Anonymous

python - 错误 : Unsupported format, 或损坏的文件:预期的 BOF 记录

我正在尝试打开一个xlsx文件并打印其中的内容。我一直遇到这个错误:importxlrdbook=xlrd.open_workbook("file.xlsx")print"Thenumberofworksheetsis",book.nsheetsprint"Worksheetname(s):",book.sheet_names()printsh=book.sheet_by_index(0)printsh.name,sh.nrows,sh.ncolsprintprint"CellD30is",sh.cell_value(rowx=29,colx=3)printforrxinrange(5

python - 绘制 datetime.time python/matplotlib 的直方图

我正在尝试绘制datetime.time值的直方图。这些值被离散化为五分钟的片段。数据在列表中如下所示:['17:15:00','18:20:00','17:15:00','13:10:00','17:45:00','18:20:00']我想绘制直方图或某种形式的分布图,以便可以轻松检查每次出现的次数。注意。给定每个时间然后离散化。直方图中的最大bin数为288=(60/5*24)我看过matplotlib.pyplot.hist。但是需要某种连续标量 最佳答案 我按照DavidZwicker所说的做了,用了几秒钟,然后改变了x轴。