Skip to content

Instantly share code, notes, and snippets.

import math
def mySqrt(x):
if type(x) not in [int, float] or x < 0:
raise ExceptionGroup("twice",
[ValueError(999),
TypeError("Work with Numbers Only")])
else:
return math.sqrt(x)
print(mySqrt('-10'))
## With asyncio.TaskGroup
async def run_errands():
async with asyncio.TaskGroup() as tg:
for errand, (start_time, time_to_finish) in errandsDict.items():
tg.create_task(errands_log(errand,
start_time,
time_to_finish))
(base) C:\Desktop>python async.py
(Pick Up Kids) starting at 6am
(Pick Up Kids) done at 7am
======;
(Return Packages) starting at 9am
(Return Packages) done at 10am
======;
(Grocery Shopping) starting at 11am
(Grocery Shopping) done at 13am
======;
from typing import Any, Type, TypeVar
from dataclasses import dataclass
curType = TypeVar('curType', bound='Rectangle')
class Rectangle:
def __init__(self, length: float) -> None:
self.length = length
@classmethod
from bs4 import BeautifulSoup
from urllib.request import urlopen, Request
from collections import defaultdict
tagInfo = defaultdict(list)
for tag in tags:
startLink = "https://medium.com/tag/"+tag
response = requests.get(startLink, allow_redirects=True)
page = response.content
--- However, COALESCE() NOT WORK for Empty or NA string, instead, use CASE WHEN
SELECT
ID_VAR,
EMPTY_STR_VAR,
COALESCE(EMPTY_STR_VAR, 'MISSING') AS COALESCE_EMPTY_STR_VAR,
CASE WHEN EMPTY_STR_VAR = ' ' THEN 'EMPTY_MISSING' END AS CASEWHEN_EMPTY_STR_VAR,
NA_STR_VAR,
CASE WHEN NA_STR_VAR = 'NA' THEN 'NA_MISSING' END AS CASEWHEN_NA_STR_VAR
FROM
### Install: !pip install textstate
import textstat
# curText = doc_set[27310]
# (1) Flesch readability score
print(textstat.flesch_reading_ease(curText))
68.94 ## indicating Standard
# (2) Reading time, assuming 15 ms/character
print(textstat.reading_time(curText, ms_per_char=15))
3.8 ## 3.8s to read
# (3) Grade level: Intended for text written for children up to grade four
## Install: !pip install -U layer
import layer
df = layer.get_dataset('layer/wikitext/datasets/wikitext-103-train').to_pandas()
doc_set = [i for i in df.sentence.str.lower()]
# Install: !pip install sentence_transformers
from sentence_transformers import SentenceTransformer
model = SentenceTransformer('distilbert-base-nli-mean-tokens')
# sentences = [doc_set[9234], doc_set[9239], doc_set[1131966]]
sentence_embeddings = model.encode(sentences)
# Calculate cosine distance of the embeddings
from scipy.spatial import distance
print(1 - distance.cosine(sentence_embeddings[0], sentence_embeddings[1]))
##### Generate the wordcloud #####
my_freq_grams = freq_grams
curMask = np.array(Image.open(pathToYourPic))
wc = WordCloud(background_color='white',
stopwords=stopwords,
width=800,
height=600,
relative_scaling=.6,
max_font_size=60,