Skip to content

Instantly share code, notes, and snippets.

View rongpenl's full-sized avatar
🏠
Working from home

Ron Li rongpenl

🏠
Working from home
View GitHub Profile
@rongpenl
rongpenl / copying_python_list.py
Last active May 21, 2023 19:49
Copying a Python list is tricky
"""
hollyandprosper.com
YouTube Channel: https://www.youtube.com/@hollyandprosper
Link to the original YouTube Short:
part 1: https://youtu.be/642mFCjpHxA
part 2: https://youtu.be/VFGaapH0gaQ
part 3: https://youtu.be/k0ekMw6u5wU
"""
@rongpenl
rongpenl / python_101_01.py
Created May 21, 2023 00:30
Don't say this is a context manager to a beginner, just don't say it.
with open('example.txt', 'r') as file:
# do something
@rongpenl
rongpenl / immutable_integers.py
Created May 22, 2023 17:36
Python integers are immutable.
"""
hollyandprosper.com
YouTube Channel: https://www.youtube.com/@hollyandprosper
Link to the original YouTube Short: https://youtu.be/Oo2g93efAeQ
"""
alpha = 'alpha'
# alpha[0] = 'A'
"""
hollyandprosper.com
YouTube Channel: https://www.youtube.com/@hollyandprosper
Link to the original YouTube Short: https://youtu.be/rnAl4Rk5bu8
"""
from immutabledict import immutabledict
immu_dict_a = immutabledict({
'alpha': 1,
'beta': 2
@rongpenl
rongpenl / default_mutable.py
Created May 23, 2023 05:55
Don't pass mutable object to function as default value
"""
hollyandprosper.com
YouTube Channel: https://www.youtube.com/@hollyandprosper
Link to the original YouTube Short: https://youtu.be/lATMth5wb1E
"""
# wrong way
def add_tourist(tourist, tourist_list=[]):
tourist_list.append(tourist)
@rongpenl
rongpenl / dummy_coach.py
Created May 29, 2023 04:18
Cracking Intermediate Python Episode 1: Context Manager
class Coach:
def warm_up_players(self):
print("The coach is warming up the players.")
def host_retro_meeting(self):
print("The coach is having a retro meeting after the game.")
def coach_game(self):
print("The game is in progress.")
@rongpenl
rongpenl / routine_refactoring.py
Created May 29, 2023 04:21
Cracking Intermediate Python Episode 1: Context Manager
from typing import Callable
class Coach:
def warm_up_players(self):
print("The coach is warming up the players.")
def host_retro_meeting(self):
print("The coach is having a retro meeting after the game.")
def coach_game(self):
@rongpenl
rongpenl / decorator_context_manager.py
Created May 29, 2023 04:22
Cracking Intermediate Python Episode 1: Context Manager
from contextlib import contextmanager
from coach import Coach
@contextmanager
def activity_management(coach: Coach):
try:
coach.warm_up_players()
yield coach
finally:
coach.host_retro_meeting()
@rongpenl
rongpenl / open_file.py
Created May 29, 2023 04:23
Cracking Intermediate Python Episode 1: Context Manager
from contextlib import contextmanager
@contextmanager
def open_file(path, mode):
try:
file = open(path, mode)
yield file
finally:
file.close()
@rongpenl
rongpenl / class_context_manager.py
Created May 29, 2023 04:25
Cracking Intermediate Python Episode 1: Context Manager
from coach import Coach
class ActivityManagedCoach(Coach):
def __enter__(self):
self.warm_up_players()
return self
def __exit__(self, exc_type, exc_val, exc_tb):
self.host_retro_meeting()
print(f"Exception type: {exc_type}")