स्निपेट·मुफ़्त
Python स्निपेट
स्क्रिप्ट और टेस्ट के लिए 20 Python स्निपेट: argparse एंट्री पॉइंट, dataclass, pathlib और JSON से फ़ाइलें, logging सेटअप, डेकोरेटर, कॉन्टेक्स्ट मैनेजर, asyncio.gather और pytest फ़िक्स्चर। किसी .py फ़ाइल में argmain या dcls जैसा प्रीफ़िक्स लिखें।
मुफ़्त में इंस्टॉल करें।
अगर आपके Android डिवाइस पर PocketCode इंस्टॉल है, तो यह पेज ऐप में खुलेगा।
प्रीव्यू
20 में से 5
Script with argparseargmain
Command-line script with a positional argument and a --verbose flag
import argparse
def main() -> None:
parser = argparse.ArgumentParser(description="${1:What the script does}")
parser.add_argument("${2:path}", help="${3:Input file}")
parser.add_argument("-v", "--verbose", action="store_true")
args = parser.parse_args()
$0
if __name__ == "__main__":
main()Dataclassdcls
Dataclass with a typed field and a list default
from dataclasses import dataclass, field
@dataclass
class ${1:Item}:
${2:name}: ${3:str}
${4:tags}: list[str] = field(default_factory=list)Enumenumc
Enum with string values
from enum import Enum
class ${1:Color}(Enum):
${2:RED} = "${3:red}"
${4:GREEN} = "${5:green}"Read a text file with pathlibreadp
Read a whole file as UTF-8 text
from pathlib import Path
${1:text} = Path("${2:data.txt}").read_text(encoding="utf-8")Load JSON from a filejload
Parse a JSON file into Python objects
import json
with open("${1:data.json}", encoding="utf-8") as f:
${2:data} = json.load(f)1.0.0 में बदलाव
पहला संस्करण।