什么是瓶颈静音分析器的日期时间格式? [英] What is the datetime format for flask-restful parser?
问题描述
from flask.ext.restful import reqparse
parser = reqparse.RequestParser()
parser.add_argument('when',type = datetime,help ='Input wasn'not valid!')
$ c $
然后我想用curl来测试所说的get方法...
curl --datawhen = [我该写些什么?]localhost:5000 / myGet
所以问题是,我该如何调用get方法?我已经尝试了很多不同的格式,试图读取rfc228标准等,但我不能找出正确的格式。 解决方案
有点迟了,但是我刚刚遇到了同样的问题,试图用RequestParser解析日期时间,可悲的是这个文档对于这个场景并不是那么有用,所以在看到和测试RequestParser和Argument代码后,我发现问题:
$ b
在 在我的例子中,我想解析一个字符串格式为 add_argument $ c中使用
type = datetime
时$ c>方法,它只是调用 datetime
与arg,如下所示: datetime(arg)
,所以如果你的参数是这样的字符串: 2016-07-12T23:13:3
,错误将是需要一个整数
%Y-%m-%dT%H: %M:%S
到日期时间对象中,所以我想用 type = datetime.strptime
之类的东西,但是如你所知,这个方法需要一个格式参数所以我终于使用这个解决方法:
$ b prearser.add_argument('date',type = lambda x:datetime.strptime(x,' %y-%m-%dT%H:%M:%S'))
正如你所看到的,使用任何你想要的日期时间格式。您也可以使用 partial
functool而不是lambda获得相同的结果或命名函数。
UPDATE
这个解决方法是非常有效的,因为它在文档上(我刚刚意识到): http://flask-restful.readthedocs.io/en/latest/extending.html#inputs
问候。
Let's say that I have following parser inside my get method:
from flask.ext.restful import reqparse
parser = reqparse.RequestParser()
parser.add_argument('when', type=datetime, help='Input wasn\'t valid!')
And then I want to test the said get method with curl...
curl --data "when=[WHAT SHOULD I WRITE HERE?]" localhost:5000/myGet
So the question is, how I should call the get method? I've tried numerous different formats, tried to read rfc228 standard, etc. but I can't figure out the right format.
Kinda late, but I've just been in the same problem, trying to parse a datetime with RequestParser, and sadly the docs are not so helpful for this scenario, so after seeing and testing RequestParser and Argument code, I think I found the problem:
When you use type=datetime
in the add_argument
method, under the hood it just calls datetime
with the arg, like this: datetime(arg)
, so if your param is a string like this: 2016-07-12T23:13:3
, the error will be an integer is required
.
In my case, I wanted to parse a string with this format %Y-%m-%dT%H:%M:%S
into a datetime object, so I thought to use something like type=datetime.strptime
but as you know this method needs a format parameter, so I finally used this workaround:
parser.add_argument('date', type=lambda x: datetime.strptime(x,'%Y-%m-%dT%H:%M:%S'))
As you can see in this way you can use whatever format datetime you want. Also you can use partial
functool instead of lambda to get the same result or a named function.
UPDATE
This workaround is pretty valid since it's on the docs (I've just realized): http://flask-restful.readthedocs.io/en/latest/extending.html#inputs
Greetings.
这篇关于什么是瓶颈静音分析器的日期时间格式?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!