Last active
June 25, 2023 12:35
-
-
Save carlinmack/3d54ecaf0f808ef91267bc4da1dc4cda to your computer and use it in GitHub Desktop.
Python snippets for vs code
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ | |
// Place your snippets for python here. Each snippet is defined under a snippet name and has a prefix, body and | |
// description. The prefix is what is used to trigger the snippet and the body will be expanded and inserted. Possible variables are: | |
// $1, $2 for tab stops, $0 for the final cursor position, and ${1:label}, ${2:another} for placeholders. Placeholders with the | |
// same ids are connected. | |
// Example: | |
// "Print to console": { | |
// "prefix": "log", | |
// "body": [ | |
// "console.log('$1');", | |
// "$2" | |
// ], | |
// "description": "Log output to console" | |
// } | |
"Default file": { | |
"prefix": "python", | |
"body": [ | |
"def main():", | |
" pass", | |
"", | |
"if __name__ == '__main__':", | |
" main()", | |
], | |
}, | |
"Time execution": { | |
"prefix": "time", | |
"body": [ | |
"import time", | |
"tick = time.time()", | |
"", | |
"print(f'Elapsed time: {time.time() - tick:.4f} seconds')", | |
], | |
}, | |
"Page through list": { | |
"prefix": "page", | |
"body": [ | |
"step = 4", | |
"for i in range(0, len(mylist), step):", | |
" page = mylist[i : i + step]", | |
" print(page)", | |
], | |
}, | |
"write": { | |
"prefix": "write", | |
"body": [ | |
"with open('file.txt', 'w') as w:", | |
" w.write(f\"\\n\")" | |
], | |
}, | |
"Read lines": { | |
"prefix": "fromlines", | |
"body": [ | |
"with open('file.txt', 'r') as r:", | |
" data = r.read().splitlines()", | |
], | |
"description": "Read from lines" | |
}, | |
"Save to lines": { | |
"prefix": "tolines", | |
"body": [ | |
"with open('file.txt', 'w') as w:", | |
" for d in data:", | |
" w.write(d + '\\n')", | |
], | |
"description": "Read from lines" | |
}, | |
"Read from json": { | |
"prefix": "fromjson", | |
"body": [ | |
"import json", | |
"with open('file.json', 'r') as r:", | |
" data = json.load(r)", | |
], | |
"description": "Read from json file" | |
}, | |
"Save to json": { | |
"prefix": "tojson", | |
"body": [ | |
"import json", | |
"with open('file.json', 'w', encoding='utf-8') as w:", | |
" json.dump(data, w, ensure_ascii=False)", | |
], | |
"description": "Save to json file" | |
}, | |
"Read from pickle": { | |
"prefix": "frompickle", | |
"body": [ | |
"import pickle", | |
"with open('file.pickle', 'rb') as r:", | |
" data = pickle.load(r)", | |
], | |
"description": "Read from pickle file" | |
}, | |
"Save to pickle": { | |
"prefix": "topickle", | |
"body": [ | |
"import pickle", | |
"with open('file.pickle', 'wb') as w:", | |
" pickle.dump(data, w)", | |
], | |
"description": "Save to pickle file" | |
}, | |
"Read from csv": { | |
"prefix": "fromcsv", | |
"body": [ | |
"import csv", | |
"with open('some.csv', newline='') as f:", | |
" reader = csv.reader(f)", | |
" for row in reader:", | |
" print(row)", | |
], | |
"description": "Read from csv file" | |
}, | |
"Save to csv": { | |
"prefix": "tocsv", | |
"body": [ | |
"import csv", | |
"with open('some.csv', 'w', newline='') as w:", | |
" writer = csv.writer(w)", | |
" writer.writerows(someiterable)", | |
], | |
"description": "Save to csv file" | |
}, | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment