Skip to content

Instantly share code, notes, and snippets.

View buddha
//
// _oo0oo_
// o8888888o
// 88" . "88
// (| -_- |)
// 0\ = /0
// ___/`---'\___
// .' \\| |// '.
// / \\||| : |||// \
// / _||||| -:- |||||- \
View upgrade.py
!sudo apt-get update -y
!sudo apt-get install python3.11
!sudo update-alternatives --install /usr/bin/python3 python3 /usr/bin/python3.7 1
!sudo update-alternatives --install /usr/bin/python3 python3 /usr/bin/python3.11 2
View exptNote.py
from datetime import datetime
try:
1/0
except Exception as myError:
myError.add_note(f'Hey, there is an error; \
Raised at {datetime.now()}')
raise
Output:
Traceback (most recent call last):
View excptGrp.py
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'))
View taskGroup.py
## 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))
View asyncOut.sh
(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
======;
View asyncOld.py
## Errands: [start_time, duration]
errandsDict = {
'Grocery Shopping': [11, 2],
'Return Packages': [9, 1],
'Pick Up Kids': [6, 1],
}
async def errands_log(task, start_time, time_to_finish):
await asyncio.sleep(start_time)
print(f"({task}) starting at {start_time}am")
View selfOld.py
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
View scrapTags.py
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
View layerWikiText.py
## 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()]