import bisect
solar_system = ['Earth', 'Jupiter', 'Mercury', 'Saturn', 'Venus']
name = 'Mars'
# Find the location where to insert the element to keep the list sorted and insert the element
bisect.insort(solar_system, name)
print(solar_system)
print(sorted(solar_system))
import sys
import os
def traverse(path):
if os.path.isfile(path):
print(path)
return
if os.path.isdir(path):
for item in os.listdir(path):
traverse(os.path.join(path, item))
return
# other unhandled things
if len(sys.argv) < 2:
exit(f"Usage: {sys.argv[0]} DIR|FILE")
traverse(sys.argv[1])
import sys
import os
def traverse(path, func):
response = {}
if os.path.isfile(path):
func(path)
return response
if os.path.isdir(path):
for item in os.listdir(path):
traverse(os.path.join(path, item), func)
return response
# other unhandled things
if len(sys.argv) < 2:
exit(f"Usage: {sys.argv[0]} DIR|FILE")
#traverse(sys.argv[1], print)
#traverse(sys.argv[1], lambda path: print(f"{os.path.getsize(path):>6} {path}"))
import sys
import os
def traverse(path, func):
if os.path.isfile(path):
func(path)
return
if os.path.isdir(path):
for item in os.listdir(path):
traverse(os.path.join(path, item), func)
return
# other unhandled things
if len(sys.argv) < 2:
exit(f"Usage: {sys.argv[0]} DIR|FILE")
#traverse(sys.argv[1], print)
#traverse(sys.argv[1], lambda path: print(f"{os.path.getsize(path):>6} {path}"))
#from inspect import getmembers, isfunction
import inspect
def change(sub):
def new(*args, **kw):
print("before")
res = sub(*args, **kw)
print("after")
return res
return new
def add(x, y):
return x+y
#print(add(2, 3))
fixed = change(add)
#print(fixed(3, 4))
def replace(subname):
def new(*args, **kw):
print("before")
res = locals()[subname](*args, **kw)
print("after")
return res
locals()[subname] = new
replace('add')
add(1, 7)
def say():
print("hello")
#print(dir())
#getattr('say')