プロジェクトに次のようなファイルがあると想像してください:
# config.yaml database: host: localhost port: 5432 username: postgres password: postgres
このドット yaml 設定ファイルを Python で解析するには、通常次のようにします:
import yaml # pip install PyYAML with open("config.yaml", encoding='utf-8') as fstream: config_yaml = yaml.safe_load(fstream)
結果の辞書から Javascript のようなオブジェクトを作成するには、おそらく次のコードを追加します:
from munch import munchify # pip install munch config = munchify(config_yaml)
pyya は 1 つの関数で自動的にそれを行います:
from pyya import init_config config = init_config( 'config.yaml', 'default.config.yaml', merge_configs = False, convert_keys_to_snake_case = False, add_underscore_prefix_to_keywords = False raise_error_non_identifiers = False) print(config.database) # Output: # Munch({"host": "localhost", "port": 5432, "username": "postgres", "password": "postgres"})
これらの旗はどうでしょうか?
merge_configs が True に設定されている場合、config.yaml とdefault.config.yaml (または指定した名前またはパス) を比較し、後者に存在しないすべてのフィールドを前者に追加します。 False に設定すると、残りのフラグによって行われるすべてのマージとすべての書式設定が無効になります。
convert_keys_to_snake_case は、構成キーを Python っぽく見せるだけなので、一目瞭然です。ただし、このフラグはログ構成などの一部の設定を壊す可能性があります。
add_underscore_prefix_to_keywords 構成キーが Python キーワードでもある場合、その前にアンダースコアが追加されます (class は _class になります)。これにより、属性アクセスがより適切に機能するようになります。
raise_error_non_identifiers 構成キーも無効な Python 識別子である場合、エラーが発生します。
pyya は次のようにインストールできます:
pip install pyya
貢献と提案を歓迎します: https://github.com/shadowy-pycoder/pyya
以上がpyya - Python プロジェクトで YAML 構成を管理する方法の詳細内容です。詳細については、PHP 中国語 Web サイトの他の関連記事を参照してください。

Pythonは解釈された言語ですが、コンパイルプロセスも含まれています。 1)Pythonコードは最初にBytecodeにコンパイルされます。 2)ByteCodeは、Python Virtual Machineによって解釈および実行されます。 3)このハイブリッドメカニズムにより、Pythonは柔軟で効率的になりますが、完全にコンパイルされた言語ほど高速ではありません。

useaforloopwhenteratingoverasequenceor foraspificnumberoftimes; useawhileloopwhentinuninguntinuntilaConditionismet.forloopsareidealforknownownownownownownoptinuptinuptinuptinuptinutionsituations whileoopsuitsituations withinterminedationations。

pythonloopscanleadtoErrorslikeinfiniteloops、ModifiningListsDuringiteration、Off-Oneerrors、Zero-dexingissues、およびNestededLoopinefficiencies.toavoidhese:1)use'i

forloopsareadvastountousforknowterations and sequences、offeringsimplicityandeadability;

pythonusesahybridmodelofcompilation andtertation:1)thepythoninterpretercompilessourcodeodeplatform-indopent bytecode.2)thepythonvirtualmachine(pvm)thenexecuteTesthisbytecode、balancingeaseoputhswithporformance。

pythonisbothintersedand compiled.1)it'scompiledtobytecode forportabalityacrossplatforms.2)bytecodeisthenは解釈され、開発を許可します。

loopsareideal whenyouwhenyouknumberofiterationsinadvance、foreleloopsarebetterforsituationsは、loopsaremoreedilaConditionismetを使用します

henthenumber ofiterationsisknown advanceの場合、dopendonacondition.1)forloopsareideal foriterating over for -for -for -saredaverseversives likelistorarrays.2)whileopsaresupasiable forsaresutable forscenarioswheretheloopcontinupcontinuspificcond


ホットAIツール

Undresser.AI Undress
リアルなヌード写真を作成する AI 搭載アプリ

AI Clothes Remover
写真から衣服を削除するオンライン AI ツール。

Undress AI Tool
脱衣画像を無料で

Clothoff.io
AI衣類リムーバー

Video Face Swap
完全無料の AI 顔交換ツールを使用して、あらゆるビデオの顔を簡単に交換できます。

人気の記事

ホットツール

ZendStudio 13.5.1 Mac
強力な PHP 統合開発環境

SecLists
SecLists は、セキュリティ テスターの究極の相棒です。これは、セキュリティ評価中に頻繁に使用されるさまざまな種類のリストを 1 か所にまとめたものです。 SecLists は、セキュリティ テスターが必要とする可能性のあるすべてのリストを便利に提供することで、セキュリティ テストをより効率的かつ生産的にするのに役立ちます。リストの種類には、ユーザー名、パスワード、URL、ファジング ペイロード、機密データ パターン、Web シェルなどが含まれます。テスターはこのリポジトリを新しいテスト マシンにプルするだけで、必要なあらゆる種類のリストにアクセスできるようになります。

Dreamweaver Mac版
ビジュアル Web 開発ツール

ドリームウィーバー CS6
ビジュアル Web 開発ツール

SublimeText3 中国語版
中国語版、とても使いやすい
