python筆記49-yaml文件中變量的使用(錨點& 與 引用*)


前言

在yaml文件中如何引用變量?當我們在一個yaml文件中寫很多測試數據時候,比如一些配置信息像用戶名,郵箱,數據庫配置等很多地方都會重復用到。
重復的數據,如果不設置變量,后續維護起來就很困難。
yaml文件里面也可以設置變量(錨點&),其它地方重復用到的話,可以用*引用

錨點&和引用*

對於重復的數據,可以單獨寫到yaml文件的開頭位置,其它的地方用到的可以用*引用

# 作者-上海悠悠 QQ交流群:717225969
# blog地址 https://www.cnblogs.com/yoyoketang/

userinfo: &userinfo
    user: yoyo
    email: 283340479@qq.com

test1:
   name: testcase 1
   data:
       <<: *userinfo
       tel: 12345678901

test2:
    name: testcase 2
    data:
        <<: *userinfo
        tel: 15201234023

等同於下面的數據

userinfo:
    user: yoyo
    email: 283340479@qq.com

test1:
   name: testcase 1
   data:
       user: yoyo
       email: 283340479@qq.com
       tel: 12345678901

test2:
    name: testcase 2
    data:
        user: yoyo
        email: 283340479@qq.com
        tel: 15201234023

&用來建立錨點(userinfo),<<表示合並到當前數據,*用來引用錨點。

*引用value值

上面的例子是對userinfo整體的數據,引用到其它地方了,有時候我們只想引用其中的一個值,如email的值,如何實現呢?

# 作者-上海悠悠 QQ交流群:717225969
# blog地址 https://www.cnblogs.com/yoyoketang/

userinfo: &userinfo
    user: yoyo
    email: &email 283340479@qq.com

test3:
   name: testcase 3
   data:
       user: admin
       email: *email
       tel: 12345678901

test4:
   name: testcase 5
   data:
       user: test123
       email: *email
       tel: 12345678902

等同於下面的數據

userinfo:
    user: yoyo
    email: 283340479@qq.com

test3:
   name: testcase 3
   data:
       user: admin
       email: 283340479@qq.com
       tel: 12345678901

test4:
   name: testcase 5
   data:
       user: test123
       email: 283340479@qq.com
       tel: 12345678902

這樣就可以把重復的數據,單獨寫到一個配置,其它地方*引用就可以了


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM