什么是瓶颈静音分析器的日期时间格式? [英] What is the datetime format for flask-restful parser?

查看:210
本文介绍了什么是瓶颈静音分析器的日期时间格式?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

  from flask.ext.restful import reqparse 

parser = reqparse.RequestParser()
parser.add_argument('when',type = datetime,help ='Input wasn'not valid!')

然后我想用curl来测试所说的get方法...

  curl --datawhen = [我该写些什么?]localhost:5000 / myGet 

所以问题是,我该如何调用get方法?我已经尝试了很多不同的格式,试图读取rfc228标准等,但我不能找出正确的格式。 解决方案

有点迟了,但是我刚刚遇到了同样的问题,试图用RequestParser解析日期时间,可悲的是这个文档对于这个场景并不是那么有用,所以在看到和测试RequestParser和Argument代码后,我发现问题:
$ b

add_argument 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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆