Skip to content

Instantly share code, notes, and snippets.

@junetech
Last active September 9, 2021 04:12
Show Gist options
  • Save junetech/cbaa86ed0e397e5f950b798fca68adb8 to your computer and use it in GitHub Desktop.
Save junetech/cbaa86ed0e397e5f950b798fca68adb8 to your computer and use it in GitHub Desktop.
Python snippets
{
// Snippets for Python
"Private Member Getter": {
"prefix": [
"pmg",
"ㅔㅡㅎ"
],
"description": "Create private class member with getter",
"body": [
"__${1:Name}: ${2:Type}",
"",
"@property",
"def $1(self) -> $2:",
" return self.__$1",
"$0\"$1\""
]
},
"Private Member Getter Setter": {
"prefix": [
"pmgs",
"ㅔㅡㅎㄴ"
],
"description": "Create private class member with getter & setter",
"body": [
"__${1:Name}: ${2:Type}",
"",
"@property",
"def $1(self) -> $2:",
" return self.__$1",
"",
"@$1.setter",
"def $1(self, val: $2):",
" self.__$1 = val",
"$0\"$1\""
]
},
"JSON class initializer": {
"prefix": "jcinit",
"description": "Create a format of class connected with JSON",
"body": [
"import json",
"from typing import Any, Dict",
"",
"",
"class $1:",
" def __init__(self, filename: str, encoding: str):",
" self.fill_from_json(filename, encoding)",
"",
" def fill_from_json(self, filename: str, encoding: str):",
" with open(filename, encoding=encoding) as _file:",
" input_dict: Dict[str, Any] = json.load(_file)",
" for key in input_dict.keys():",
" class_key = f\"_{self.__class__.__name__}__{key}\"",
" if class_key in self.__class__.__annotations__:",
" self.__dict__[class_key] = input_dict[key]",
" else:",
" base_key = f\"_{self.__class__.__base__.__name__}__{key}\"",
" self.__dict__[base_key] = input_dict[key]",
"",
" $0"
]
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment