Page List

Search on the blog

2016年6月27日月曜日

便利なPythonライブラリ(7)Cerberus

Pythonでバリデーション処理を書くときは、Cerberusというライブラリが便利そう。

インストール
pipで。
$ pip install cerberus

サンプル
バリデーションの定義をdictで定義して、Validatorのコンストラクタに渡す。
from cerberus import Validator

schema = {
    "url": {'required': True, 'type': 'string'},
    "title": {'required': True, 'type': 'string'},
    "links" : {'type': 'list', 'schema': {'type': 'string'}},
    "imgs": {'type': 'list', 'schema': {'type': 'dict', \
                 'schema': {'tag': {'type': 'string'}, 'url': {'type': 'string'}}}},
    "pv": {'required': True, 'type': 'integer', 'min': 0}
}
validator = Validator(schema)
これで、validatorができた。

上から説明すると、
urlは必須項目。型はstring。
titleは必須項目。型はstring。
linksはオプション項目。型はstringのリスト。
imgsはオプション項目。型はtag, urlというキーをもつdictのリスト。ただし、tag、url共にstring。
pvは必須項目。型はinteger。最小値は0。

データを作ってバリデーションしてみる。

document = {
    "url": "http://example.com/validator/test.html",
    "links": ['http://example.com/test1.html', 'http://example.com/test2.html'],
    "imgs": [{'tag': 'pic1', 'url': 'img/pic1.png'}],
    "pv": 10
}

if validator.validate(document):
    print ("success!")
else:
    print ("fail!")
    print (validator.errors)

実行結果。
fail!
{'title': 'required field'}

という風になかなか直感的で使い易い。 もちろん拡張して自前のバリデーション定義も作成できる。

29 件のコメント:

  1. The actual time and effort took to create this wonderful article were really great and thanks for sharing here please do keep updating us...
    Python Training in Chennai

    返信削除
  2. thanks for sharing,it is very useful to everyone...

    返信削除
  3. What a really awesome post this is. Truly, one of the best posts I've ever witnessed to see in my whole life. Wow, just keep it up.
    Digital Marketing Courses in Hyderabad With Placements

    返信削除
  4. https://www.digitalbrolly.com/digital-marketing-course-in-hyderabad/

    返信削除
  5. https://ravivarma.in/how-to-buy-domains-for-cheap-price/

    返信削除
  6. I feel very grateful that I read this. It is very helpful and very informative and I really learned a lot from it.
    data scientist training in malaysia

    返信削除
  7. If you're interested in getting a job in Python, look at the advanced Python training offered at Hyderabad from AI Patasala. Python Training in Hyderabad by trained experts at AI Patasala training center is the best option.
    Online Python Course

    返信削除
  8. 私はあなたがそれについてのユーチューブのためのビデオを作って、ここから多くのいいねを得ることができると思います https://viplikes.jp

    返信削除
  9. nice blog https://duckcreektraining.com/

    返信削除
  10. You should take part in a contest for one of the best blogs on the web. I will recommend this site!
    카지노
    사설토토

    返信削除
  11. Whats Happening i am new to this, I stumbled upon this I’ve discovered It absolutely helpful and it has aided me out loads. I am hoping to give a contribution & assist other users like its helped me. Great job.
    majortotositepro1
    racesiteinfo1
    oncasinositeinfo1
    totopickpro1

    返信削除
  12. Nice post. I learn something totally new and challenging on sites I stumble upon every day.

    야한동영상
    립카페
    출장안마
    타이마사지
    온라인카지노

    返信削除
  13. I was just examining through the web looking for certain information and ran over your blog.It shows how well you understand this subject. Bookmarked this page, will return for extra.

    返信削除
  14. 360DigiTMG, the top-rated organisation among the most prestigious industries around the world, is an educational destination for those looking to pursue their dreams around the globe. The company is changing careers of many people through constant improvement, 360DigiTMG provides an outstanding learning experience and distinguishes itself from the pack. 360DigiTMG is a prominent global presence by offering world-class training. Its main office is in India and subsidiaries across Malaysia, USA, East Asia, Australia, Uk, Netherlands, and the Middle East.

    返信削除
  15. You understand your tasks stand apart of the group. there might be something exceptional nearly them. It appears to me every one of them are in truth remarkable! FileviewPro Crack

    返信削除
  16. For me, becoming isn’t about arriving somewhere or achieving a certain aim. I see it instead as forward motion, a means of evolving, a way to reach continuously toward a better self. https://wishesquotz.com/helpful-life-quotes/

    返信削除
  17. Good tips for tech, thanks for sharing, keep posting more
    Python full stack Training in KPHB

    返信削除
  18. Nice detailed blog. Thank you for sharing such nice blog.
    We also provide online professional courses.powerbi training in kukatpally hyderabad Do check our website.
    Best IT Training Institute in hyderabad

    返信削除
  19. I feel very grateful that I read this. It is very helpful and very informative and I really learned a lot from it.
    java traning in hyderabad.
    Java Training in KPHB

    返信削除