Skip to content

Instantly share code, notes, and snippets.

@sohang3112
Last active August 4, 2022 05:00
Show Gist options
  • Save sohang3112/a1d648eb0b0c06948d823d491431ee20 to your computer and use it in GitHub Desktop.
Save sohang3112/a1d648eb0b0c06948d823d491431ee20 to your computer and use it in GitHub Desktop.
My Custom Code Snippets in VS Code
{
// Place your global snippets here. Each snippet is defined under a snippet name and has a scope, prefix, body and
// description. Add comma separated ids of the languages where the snippet is applicable in the scope field. If scope
// is left empty or omitted, the snippet gets applied to all languages. 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": {
// "scope": "javascript,typescript",
// "prefix": "log",
// "body": [
// "console.log('$1');",
// "$2"
// ],
// "description": "Log output to console"
// }
// Source: https://mattferderer.com/create-a-snippet-or-shortcut-in-vs-code-to-insert-the-current-date-time
"Current Date (IST)": {
"scope": "", // applicable anywhere
"prefix": "date",
"body": [
"$CURRENT_YEAR-$CURRENT_MONTH-${CURRENT_DATE}T$CURRENT_HOUR:$CURRENT_MINUTE:$CURRENT_SECOND.000+05:30"
],
"description": "Add Current date & time"
},
//////////////////////////// AWS ///////////////////////////////
"AWS SSO Login (Python)": {
"scope": "python",
"prefix": "boto3",
"body": [
"!aws sso login --profile sohang # Shell Command - Comment this out and run in terminal if not using Jupyter Notebook",
"",
"import boto3 ",
"from pprint import pprint",
"",
"boto3.setup_default_session(profile_name='sohang')",
"my_profile = boto3.client('sts').get_caller_identity() # Throws if invalid credentials",
"pprint(my_profile)",
"",
"s3 = boto3.client('s3')"
],
"description": "AWS SSO Login, then validate credentials in Python using boto3"
},
//////////////////////////// Python ////////////////////////////
"Common Python 3 Imports": {
"scope": "python",
"prefix": "import",
"body": [
"from typing import *",
"from functools import *",
"from itertools import *",
"import json",
"import logging as log",
"",
"from bs4 import BeautifulSoup",
"from requests import RequestException",
"import requests",
"import pandas as pd",
"import numpy as np",
"import datetime as dt",
],
"description": "Import Common Modules"
},
"IPython Imports": {
"scope": "python",
"prefix": "ipython",
"body": [
"from IPython.display import display, HTML, IFrame, Image",
"from ipython_secrets import get_secret"
],
"description": "Import Common IPython Modules"
},
//////////////////////////// Web Scraping //////////////////////////
"User Agent": {
"scope": "",
"prefix": "user-agent",
"body": [
"Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/74.0.3729.169 Safari/537.36"
],
"description": "User Agent"
},
"View HTML": {
"scope": "python",
"prefix": "view-html",
"body": [
"import webbrowser",
"from tempfile import NamedTemporaryFile",
"def view_html(html: bytes, **kwargs) -> bool:",
" '''",
" Open HTML input string as a webpage in default browser",
" @return - True if page opened successfully, False otherwise",
" '''",
" with NamedTemporaryFile(delete=False, **kwargs) as f:",
" f.write(html)",
" return webbrowser.open(f.name)",
],
},
// make Selenium (Firefox) also?
"Selenium (Chrome)": {
"scope": "python",
"prefix": "selenium",
"body": [
"from webdriver_manager.chrome import ChromeDriverManager",
"from selenium.common.exceptions import *",
"from selenium.webdriver import Chrome, ChromeOptions",
"from selenium.webdriver.common.by import By",
"from selenium.webdriver.remote.webelement import WebElement",
"from selenium.webdriver.support.ui import WebDriverWait",
"from selenium.webdriver.support import expected_conditions as EC",
"",
"# chrome_options = ChromeOptions()",
"driver = Chrome(ChromeDriverManager().install())",
],
"description": "Start Chrome Selenium Driver (required web driver installed automatically by Web Driver Manager if not found)"
},
"Dummy HTTP Request": {
"scope": "python",
"prefix": "requests",
"body": [
"import requests",
"# from bs4 import BeautifulSoup",
"",
"url = '$1'",
"",
"headers = {",
" 'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/103.0.0.0 Safari/537.36'",
"}",
"",
"cookies = {",
" ${2:# cookies here}",
"}",
"",
"params = {",
" ${3:# url params here}",
"}",
"",
"payload = {",
" ${4:# JSON payload here}",
"}",
"",
"res = requests.$5(url, params=params, headers=headers, cookies=cookies, json=payload)",
"res.raise_for_status()",
"# data = res.json()",
"# soup = BeautifulSoup(res.content, 'html-parser')"
],
"description": "Dummy HTTP Request using requests library"
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment