convert strings to f-strings; clean up btree code; wrap network stuff in try-except
This commit is contained in:
16
tf.py
Executable file → Normal file
16
tf.py
Executable file → Normal file
@@ -33,7 +33,7 @@ def cp(src,dest):
|
|||||||
|
|
||||||
def cat(filename, first=1, last=1000000, numbers=False, title=True):
|
def cat(filename, first=1, last=1000000, numbers=False, title=True):
|
||||||
if title:
|
if title:
|
||||||
print("===={}====".format(filename))
|
print(f"===={filename}====")
|
||||||
transfer(filename,sys.stdout,first,last,numbers=numbers)
|
transfer(filename,sys.stdout,first,last,numbers=numbers)
|
||||||
|
|
||||||
def grep(filename, pattern, numbers=False):
|
def grep(filename, pattern, numbers=False):
|
||||||
@@ -56,7 +56,7 @@ def sed(filename, sed_cmd, bak_ext=".bak"):
|
|||||||
|
|
||||||
if op in "aid" and e-s==1000000:
|
if op in "aid" and e-s==1000000:
|
||||||
raise ValueError("sed(a/i/d) should have a line number")
|
raise ValueError("sed(a/i/d) should have a line number")
|
||||||
#print("sed command parser of <{}> returned {} {} {}".format(op,cmd,a.group(1),a.group(3)))
|
#print(f"sed command parser of <{op}> returned {cmd} {a.group(1)} {a.group(3)}")
|
||||||
if op in "sxX":
|
if op in "sxX":
|
||||||
if len(args)<2 or args[0] in "\^$()[]":
|
if len(args)<2 or args[0] in "\^$()[]":
|
||||||
raise ValueError("invalid sed argument: "+op+args)
|
raise ValueError("invalid sed argument: "+op+args)
|
||||||
@@ -94,7 +94,7 @@ def sed(filename, sed_cmd, bak_ext=".bak"):
|
|||||||
h+=1
|
h+=1
|
||||||
continue # delete line
|
continue # delete line
|
||||||
if op=='i' and m:
|
if op=='i' and m:
|
||||||
#print("insert a line before {} <{}>".format(i,extra))
|
#print(f"insert a line before {i} <{extra}>")
|
||||||
g.write(args)
|
g.write(args)
|
||||||
h+=1
|
h+=1
|
||||||
if op in "aids":
|
if op in "aids":
|
||||||
@@ -103,7 +103,7 @@ def sed(filename, sed_cmd, bak_ext=".bak"):
|
|||||||
g.write(lin)
|
g.write(lin)
|
||||||
h+=1
|
h+=1
|
||||||
if op=='a' and m:
|
if op=='a' and m:
|
||||||
#print("append a line after {} <{}>".format(i,extra))
|
#print(f"append a line after {i} <{extra}>")
|
||||||
g.write(args)
|
g.write(args)
|
||||||
h+=1
|
h+=1
|
||||||
#f.write("--file modifed by sed()--\n")
|
#f.write("--file modifed by sed()--\n")
|
||||||
@@ -112,9 +112,9 @@ def sed(filename, sed_cmd, bak_ext=".bak"):
|
|||||||
def _dir(d='.'):
|
def _dir(d='.'):
|
||||||
for f in os.listdir(d):
|
for f in os.listdir(d):
|
||||||
s=os.stat(d+'/'+f)
|
s=os.stat(d+'/'+f)
|
||||||
print("{}rwx all {:9d} {}".format('d' if (s[0] & 0x4000) else '-',s[6],f))
|
print(f"{'d' if (s[0] & 0x4000) else '-'}rwx all {s[6]:9d} {f}")
|
||||||
s=os.statvfs('/')
|
s=os.statvfs('/')
|
||||||
print("disk size:{:8d} KB disk free: {} KB".format(s[0]*s[2]//1024,s[0]*s[3]//1024))
|
print(f"disk size:{s[0]*s[2]//1024:8d} KB disk free: {s[0]*s[3]//1024} KB")
|
||||||
|
|
||||||
'''-----cut here if you only need the functions-----'''
|
'''-----cut here if you only need the functions-----'''
|
||||||
def ext_cmd(a):
|
def ext_cmd(a):
|
||||||
@@ -188,7 +188,7 @@ def main():
|
|||||||
else:
|
else:
|
||||||
r=sed(p[1],p[0])
|
r=sed(p[1],p[0])
|
||||||
if r:
|
if r:
|
||||||
print("Lines processed: {} Lines modifed: {}".format(*r))
|
print(f"Lines processed: {r[0]} Lines modifed: {r[1]}")
|
||||||
except (ValueError, OSError) as e:
|
except (ValueError, OSError) as e:
|
||||||
print(e)
|
print(e)
|
||||||
except RuntimeError:
|
except RuntimeError:
|
||||||
@@ -223,5 +223,3 @@ def main():
|
|||||||
if __name__=="tf":
|
if __name__=="tf":
|
||||||
print("tf module loaded; members cp(), cat(), _dir(), grep() and sed()")
|
print("tf module loaded; members cp(), cat(), _dir(), grep() and sed()")
|
||||||
main()
|
main()
|
||||||
|
|
||||||
# grep 12.*\) dmesg fails
|
|
||||||
|
|||||||
23
tf_extend.py
23
tf_extend.py
@@ -46,19 +46,22 @@ def cmd(args):
|
|||||||
try:
|
try:
|
||||||
tf.transfer(args[1],w)
|
tf.transfer(args[1],w)
|
||||||
if cmd=='wc':
|
if cmd=='wc':
|
||||||
print("lines: {}\twords: {}\tbytes: {}".format(w.lines, w.words, w.bytes_))
|
print(f"lines: {w.lines}\twords: {w.words}\tbytes: {w.bytes_}")
|
||||||
except:
|
except:
|
||||||
print("file not found: "+args[1])
|
print("file not found: "+args[1])
|
||||||
|
|
||||||
elif cmd in ('ifconfig','ip'):
|
elif cmd in ('ifconfig','ip'):
|
||||||
ifc=network.WLAN().ifconfig()
|
ifc=network.WLAN().ifconfig()
|
||||||
print("IP: {}\tmask: {}\tgateway: {}\tDNS: {}".format(*ifc))
|
print(f"IP: {ifc[0]}\tmask: {ifc[1]}\tgateway: {ifc[2]}\tDNS: {ifc[3]}")
|
||||||
|
|
||||||
elif cmd in ('host','nslookup','dig'):
|
elif cmd in ('host','nslookup','dig'):
|
||||||
if len(args)<2:
|
if len(args)<2:
|
||||||
print("syntax: host <domain.name>")
|
print("syntax: host <domain.name>")
|
||||||
else:
|
else:
|
||||||
print("host <{}> is at {}".format(args[1],socket.getaddrinfo(args[1],80)[0][-1][0]))
|
try:
|
||||||
|
print(f"host <{args[1]}> is at {socket.getaddrinfo(args[1],80)[0][-1][0]}")
|
||||||
|
except:
|
||||||
|
print("network/DNS not available")
|
||||||
|
|
||||||
elif cmd=='connect':
|
elif cmd=='connect':
|
||||||
if len(args)<3:
|
if len(args)<3:
|
||||||
@@ -78,7 +81,7 @@ def cmd(args):
|
|||||||
print("no AP found")
|
print("no AP found")
|
||||||
return True
|
return True
|
||||||
for i in s:
|
for i in s:
|
||||||
print("ch: {}\tRSSI: {}\t{}\tSSID: {}".format(i[2],i[3],"open" if i[4]==0 else "",i[0]))
|
print(f"ch: {i[2]}\tRSSI: {i[3]}\t{"open" if i[4]==0 else ""}\tSSID: {i[0]}")
|
||||||
|
|
||||||
elif cmd=='freq':
|
elif cmd=='freq':
|
||||||
# identify esp32 or esp8266
|
# identify esp32 or esp8266
|
||||||
@@ -90,7 +93,7 @@ def cmd(args):
|
|||||||
if len(args)==1 or args[1] in freqs:
|
if len(args)==1 or args[1] in freqs:
|
||||||
if len(args)>1:
|
if len(args)>1:
|
||||||
machine.freq(int(args[1])*1000000)
|
machine.freq(int(args[1])*1000000)
|
||||||
print("master cpu frequency {}MHz".format(machine.freq()//1000000))
|
print(f"master cpu frequency {machine.freq()//1000000}MHz")
|
||||||
else:
|
else:
|
||||||
print("syntax: freq [ 160 | 80 | 240 ] ")
|
print("syntax: freq [ 160 | 80 | 240 ] ")
|
||||||
|
|
||||||
@@ -100,8 +103,8 @@ def cmd(args):
|
|||||||
b=btree.open(f)
|
b=btree.open(f)
|
||||||
print("Key\t\tValue")
|
print("Key\t\tValue")
|
||||||
i=0
|
i=0
|
||||||
for w in b:
|
for k,v in b.items():
|
||||||
print("{}\t{}".format(w,b[w]))
|
print(f"{k:10}\t{v}")
|
||||||
i+=1
|
i+=1
|
||||||
if i%30==0:
|
if i%30==0:
|
||||||
r=input("continue? ")
|
r=input("continue? ")
|
||||||
@@ -115,15 +118,15 @@ def cmd(args):
|
|||||||
except OSError:
|
except OSError:
|
||||||
print("file not found")
|
print("file not found")
|
||||||
elif cmd=='free':
|
elif cmd=='free':
|
||||||
print("memory used: {}\tmemory free:{}".format(gc.mem_alloc(),gc.mem_free()))
|
print(f"memory used: {gc.mem_alloc()}\tmemory free:{gc.mem_free()}")
|
||||||
elif cmd=='help':
|
elif cmd=='help':
|
||||||
print("==Extended commands")
|
print("==Extended commands")
|
||||||
print(" connect <essid> <password> \tscan")
|
print(" connect <essid> <password> \tscan")
|
||||||
print(" ifconfig/ip \t\thost/dig/nslookup <domain.name>")
|
print(" ifconfig/ip \t\thost/dig/nslookup <domain.name>")
|
||||||
print(" freq [160 | 80 | 240]\texec <python-filename>")
|
print(" freq [160 | 80 | 240]\t\texec <python-filename>")
|
||||||
print(" free \t\t\twc <filename>")
|
print(" free \t\t\twc <filename>")
|
||||||
print(" less/more [-n] <filename>")
|
print(" less/more [-n] <filename>")
|
||||||
print(" bt-list <filename>")
|
print(" btree <filename>")
|
||||||
else: # command not found
|
else: # command not found
|
||||||
return False
|
return False
|
||||||
return True
|
return True
|
||||||
|
|||||||
Reference in New Issue
Block a user