YAML 在Python中的配置应用
yaml是一種比xml和json更輕的文件格式,也更簡單更強大,它可以通過縮進(jìn)來表示結(jié)構(gòu),聽著就和Python很配對不對?
yaml的介紹不在這里贅述,感興趣可以自行百度下,先說下它的基本語法,還是配合著PyYaml來:
1. PyYaml
PyYaml是Python的一個專門針對yaml文件操作的模塊,使用起來非常簡單。
或者到這里下載相應(yīng)版本的包,人工安裝,http://pyyaml.org/wiki/PyYAML 點擊打開鏈接。
使用起來非常簡單,就像json、pickle一樣,load、dump就足夠我們使用了。
- load()
結(jié)果:{'job': 'Tester', 'age': 0, 'name': u'\u7070\u84dd'}
- dump()
輸出結(jié)果:
age: 0 job: Tester name: "\u7070\u84DD"上面只是簡單的兩個應(yīng)用,還有?load_all()、dump_all()?等,我們一般用這四個足夠了,另外兩個下面會講到,其他方法可以自己看API,我也沒怎么折騰過。
2. yaml語法
https://www.ibm.com/developerworks/cn/xml/x-cn-yamlintro/點擊打開鏈接
有了上面的基礎(chǔ),接下來我們看看yaml的語法,首先我們準(zhǔn)備下測試語法的環(huán)境:
創(chuàng)建?test.yaml?文件,我們練習(xí)語法就在這里
創(chuàng)建?testyaml.py?文件,用來查看Python執(zhí)行后的效果,其中內(nèi)容如下:
好了,接下來我們就來看語法吧:
1. 基本規(guī)則
- 大小寫敏感
- 使用縮進(jìn)表示層級關(guān)系
- 縮進(jìn)時不允許使用Tab,只允許使用空格
- 縮進(jìn)的空格數(shù)目不重要,只要相同層級的元素左對齊即可
- #?表示注釋,從它開始到行尾都被忽略
2. yaml轉(zhuǎn)字典
yaml中支持映射或字典的表示,如下:
#test.yaml #下面格式讀到Python里會是個dict name: 灰藍(lán) age: 0 job: Tester #coding:utf-8import yaml import os #open('test.yaml','w') #如果沒有創(chuàng)建test.yaml,先使用open在當(dāng)前目錄下創(chuàng)建文件#方法一 output = yaml.load(file("test.yaml")) print output#方法二 #filename = os.path.join(os.path.dirname(__file__), 'test.yaml').replace("\\", "/") #output = yaml.load(open(filename)) #print output輸出:{'job': 'Tester', 'age': 0, 'name': u'\u7070\u84dd'}
3. yaml轉(zhuǎn)列表
yaml中支持列表或數(shù)組的表示,注意裂變中的每一個元素前面用符號?-?如下:
#test.yaml #下面格式讀到Python里會是個list- 灰藍(lán) - 0 - Tester在test.ymal文件中:
輸出:[u'\u7070\u84dd', 0, 'Tester']
4. 復(fù)合結(jié)構(gòu)
字典和列表可以復(fù)合起來使用,注意這里使用?-? 如下:
# test.yaml #下面格式讀到Python里是個list里包含dict - name: 灰藍(lán)age: 0job: Tester - name: Jamesage: 30輸出:[{'job': 'Tester', 'age': 0, 'name': u'\u7070\u84dd'}, {'age': 30, 'name': 'James'}]
5. 基本類型
yaml中有以下基本類型:
- 字符串
- 整型
- 浮點型
- 布爾型
- null
- 時間
- 日期
我們寫個例子來看下:
# test.yaml #這個例子輸出一個字典,其中value包括所有基本類型 str: "Hello World!" int: 110 float: 3.141 boolean: true # or false None: null # 也可以用 ~ 號來表示 null time: 2016-09-22t11:43:30.20+08:00 # ISO8601,寫法百度 date: 2016-09-22 # 同樣ISO8601輸出:
{'date': datetime.date(2016, 9, 22), 'None': None, 'boolean': True, 'str': 'Hello World!', 'time': datetime.datetime(2016, 9, 22, 3, 43, 30, 200000), 'int': 110, 'float': 3.141}如果字符串沒有空格或特殊字符,不需要加引號,但如果其中有空格或特殊字符,則需要加引號了
#test.yaml str: 灰藍(lán) str1: "Hello World" str2: "Hello\nWorld"輸出結(jié)果:{'str2': 'Hello\nWorld', 'str1': 'Hello World', 'str': u'\u7070\u84dd'}
這里要注意單引號和雙引號的區(qū)別,單引號中的特殊字符轉(zhuǎn)到Python會被轉(zhuǎn)義,也就是到最后是原樣輸出了,雙引號不會被Python轉(zhuǎn)義,到最后是輸出了特殊字符;可能比較拗口,來個例子理解下:
#test.yaml str1: 'Hello\nWorld' str2: "Hello\nWorld" # -*- coding: utf-8 -*- import yamly = yaml.load(file('test.yaml', 'r')) print y['str1'] print y['str2']輸出:
Hello\nWorld Hello World可以看到,單引號中的’\n’最后是輸出了,雙引號中的’\n’最后是轉(zhuǎn)義成了回車
字符串處理中寫成多行、’|’、’>’、’+’、’-‘的意義這里就不講了。
6. 引用
&?和?*?用于引用
#test.yaml name: &name 灰藍(lán) tester: *name這個相當(dāng)于一下腳本:
#test.yaml name: 灰藍(lán) tester: 灰藍(lán)輸出:{'name': u'\u7070\u84dd', 'tester': u'\u7070\u84dd'}
7. 強制轉(zhuǎn)換
yaml是可以進(jìn)行強制轉(zhuǎn)換的,用?!!?實現(xiàn),如下:
#test.yaml str: !!str 3.14 int: !!int "123"輸出:{'int': 123, 'str': '3.14'}
明顯能夠看出123被強轉(zhuǎn)成了int類型,而float型的3.14則被強轉(zhuǎn)成了str型。另外PyYaml還支持轉(zhuǎn)換成Python/object類型,這個我們下面再討論。
8. 分段
在同一個yaml文件中,可以用?---?來分段,這樣可以將多個文檔寫在一個文件中
#test.yaml --- name: James age: 20 --- name: Lily age: 19這時候我們就得用到我們的?load_all()?方法出場了,load_all()?方法會生成一個迭代器,可以用for輸出出來:
# -*- coding: utf-8 -*- import yamlys = yaml.load_all(file('test.yaml', 'r')) for y in ys:print y輸出:
{'age': 20, 'name': 'James'} {'age': 19, 'name': 'Lily'}對應(yīng)的也有?dump_all()?方法,一個意思,就是將多個段輸出到一個文件中,如:
# -*- coding: utf-8 -*- import yamlobj1 = {"name": "James", "age": 20} obj2 = ["Lily", 19]with open('test.yaml', 'w') as f:yaml.dump_all([obj1, obj2], f)打開test.yaml看看:
{age: 20, name: James} --- [Lily, 19]dump()?和?dump_all()?方法可以傳入列表,也可以傳入一個可序列化生成器,如?range(10), 如下:
# -*- coding: utf-8 -*- import yamly = yaml.dump(range(10)) print y輸出:
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]在?dump?和?dump_all()?的時候還可以配一堆參數(shù),不一一講解了(其實博主也不全了解。。)
3. 構(gòu)造器(constructors)、表示器(representers)、解析器(resolvers )
這幾個東西可以把Python的對象和yaml互轉(zhuǎn),很強大。這個翻譯是博主自個兒翻譯的,表達(dá)的不準(zhǔn)確勿怪。
1.?yaml.YAMLObject
yaml.YAMLObject用元類來注冊一個構(gòu)造器(也就是代碼里的?__init__()?方法),讓你把yaml節(jié)點轉(zhuǎn)為Python對象實例,用表示器(也就是代碼里的?__repr__()?函數(shù))來讓你把Python對象轉(zhuǎn)為yaml節(jié)點,看代碼:
# -*- coding: utf-8 -*- import yamlclass Person(yaml.YAMLObject):yaml_tag = '!person'def __init__(self, name, age):self.name = nameself.age = agedef __repr__(self):return '%s(name=%s, age=%d)' % (self.__class__.__name__, self.name, self.age)james = Person('James', 20)print yaml.dump(james) # Python對象實例轉(zhuǎn)為yamllily = yaml.load('!person {name: Lily, age: 19}')print lily # yaml轉(zhuǎn)為Python對象實例輸出:
!person {age: 20, name: James}Person(name=Lily, age=19)2.?yaml.add_constructor?和?yaml.add_representer
你可能在使用過程中并不想通過上面這種元類的方式,而是想定義正常的類,那么,可以用這兩種方法
# -*- coding: utf-8 -*- import yamlclass Person(object):def __init__(self, name, age):self.name = nameself.age = agedef __repr__(self):return 'Person(%s, %s)' % (self.name, self.age)james = Person('James', 20) print yaml.dump(james) # 沒加表示器之前def person_repr(dumper, data):return dumper.represent_mapping(u'!person', {"name": data.name, "age": data.age}) # mapping表示器,用于dictyaml.add_representer(Person, person_repr) # 用add_representer方法為對象添加表示器 print yaml.dump(james) # 加了表示器之后def person_cons(loader, node):value = loader.construct_mapping(node) # mapping構(gòu)造器,用于dictname = value['name']age = value['age']return Person(name, age)yaml.add_constructor(u'!person', person_cons) # 用add_constructor方法為指定yaml標(biāo)簽添加構(gòu)造器 lily = yaml.load('!person {name: Lily, age: 19}') print lily輸出:
!!python/object:__main__.Person {age: 20, name: James}!person {age: 20, name: James}Person(Lily, 19)第一行是沒加表示器之前,多丑!中間那行是加了表示器之后,變成了規(guī)范的格式,下面添加了構(gòu)造器,能夠把?!person標(biāo)簽轉(zhuǎn)化為Person對象。
這里用了?construct_mapping?,還有其他好多?construct_document,?construct_object,construct_scalar,construct_sequence,construct_pairs,具體怎么用,可以自己研究下,看看API,看看源碼學(xué)習(xí)下。
對應(yīng)的?representer?也一樣,有很多,這里只用了?represent_mapping,其他的不示例講解了。
3.?add_implicit_resolver
如果你不想每次都寫標(biāo)簽,也可以用?add_implicit_resolver?方法添加解析器,然后它就能夠把指定樣式的沒有標(biāo)簽的基本元素解析成對應(yīng)的Python對象。這個就不詳細(xì)分析給示例了。感興趣的同學(xué)自己看文檔學(xué)習(xí)吧。
4. 結(jié)語
yaml是一種很清晰、簡潔的格式,而且跟Python非常合拍,非常容易操作,我們在搭建自動化測試框架的時候,可以采用yaml作為配置文件,或者用例文件,下面給出一個用例的示例,這個示例來自于Python restful接口框架?pyresttest:
# Test using included Django test app # First install python-django # Then launch the app in another terminal by doing # cd testapp # python manage.py testserver test_data.json # Once launched, tests can be executed via: # python resttest.py http://localhost:8000 miniapp-test.yaml --- - config:- testset: "Tests using test app"- test: # create entity- name: "Basic get"- url: "/api/person/" - test: # create entity- name: "Get single person"- url: "/api/person/1/" - test: # create entity- name: "Get single person"- url: "/api/person/1/"- method: 'DELETE' - test: # create entity by PUT- name: "Create/update person"- url: "/api/person/1/"- method: "PUT"- body: '{"first_name": "Gaius","id": 1,"last_name": "Baltar","login": "gbaltar"}'- headers: {'Content-Type': 'application/json'} - test: # create entity by POST- name: "Create person"- url: "/api/person/"- method: "POST"- body: '{"first_name": "Willim","last_name": "Adama","login": "theadmiral"}'- headers: {Content-Type: application/json}怎么樣,趕緊用起來吧!有什么問題歡迎跟博主交流了溝通!
轉(zhuǎn)載:https://blog.csdn.net/huilan_same/article/details/52625230 轉(zhuǎn)載出處總結(jié)
以上是生活随笔為你收集整理的YAML 在Python中的配置应用的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Python之单元测试
- 下一篇: 简单分享一个轻量级自动化测试框架目录结构