Răsfoiți Sursa

restored the .format() string ops, so this works with older python boards

Pat Beirne 2 ani în urmă
părinte
comite
9a6fdcdba0
2 a modificat fișierele cu 19 adăugiri și 19 ștergeri
  1. 8 8
      tf.py
  2. 11 11
      tf_extend.py

+ 8 - 8
tf.py

@@ -33,7 +33,7 @@ def cp(src,dest):
 
 def cat(filename, first=1, last=1000000, numbers=False, title=True):
   if title:
-    print(f"===={filename}====")
+    print("===={}====".format(filename))
   transfer(filename,sys.stdout,first,last,numbers=numbers)
 
 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:
     raise ValueError("sed(a/i/d) should have a line number")
-  #print(f"sed command parser of <{op}> returned {cmd} {a.group(1)} {a.group(3)}")
+  #print("sed command parser of <{}> returned {} {} {}".format(op, cmd, a.group(1), a.group(3)))
   if op in "sxX":
     if len(args)<2 or args[0] in "\^$()[]": 
       raise ValueError("invalid sed argument: "+op+args)
@@ -94,7 +94,7 @@ def sed(filename, sed_cmd, bak_ext=".bak"):
           h+=1
           continue   # delete line
         if op=='i' and m:
-          #print(f"insert a line before {i} <{extra}>")
+          #print("insert a line before {} <{}>".format(i,extra))
           g.write(args)
           h+=1
         if op in "aids":
@@ -103,7 +103,7 @@ def sed(filename, sed_cmd, bak_ext=".bak"):
           g.write(lin)
           h+=1
         if op=='a' and m:
-          #print(f"append a line after {i} <{extra}>")
+          #print("append a line after {} <{}>".format(i,extra))
           g.write(args)
           h+=1
       #f.write("--file modifed by sed()--\n")
@@ -112,9 +112,9 @@ def sed(filename, sed_cmd, bak_ext=".bak"):
 def _dir(d='.'):
   for f in os.listdir(d):
     s=os.stat(d+'/'+f)
-    print(f"{'d' if (s[0] & 0x4000) else '-'}rwx all {s[6]:9d} {f}")
+    print("{}rwx all {:9d} {}".format('d' if (s[0] & 0x4000) else '-',s[6],f ))
   s=os.statvfs('/')
-  print(f"disk size:{s[0]*s[2]//1024:8d} KB   disk free: {s[0]*s[3]//1024} KB")
+  print("disk size:{:8d} KB   disk free: {} KB".format(s[0]*s[2]//1024,s[0]*s[3]//1024))
 
 '''-----cut here if you only need the functions-----'''
 def ext_cmd(a):
@@ -132,7 +132,7 @@ def _help():
   print("  cat/list [-n] [-l <n>,<m>] <file>")
   print("  grep <pattern> <file>")
   print("  sed <pattern> <file>")
-  print("      pattern is <line-range><op><extra>   e.g'a/search/replace/', 'x!TODO:!', '43,49d', '8itext'")
+  print("      pattern is <line-range><op><extra>   eg: 's/search/replace/' 'x!TODO:!' '43,49d' '8itext'")
   print("      patterns with spaces require '-quotes\tsed ops are one of s/d/i/a/x/X")
   print("      sed cannot cross line boundaries\t\tsed s/x/X-patterns: non-/ delimiters are ok")
   print("file names must NOT have embedded spaces\toptions must be early on the command line")
@@ -188,7 +188,7 @@ def main():
         else:
           r=sed(p[1],p[0])
           if r:
-            print(f"Lines processed: {r[0]}  Lines modifed: {r[1]}")
+            print("Lines processed: {}  Lines modifed: {}".format(*r))
       except (ValueError, OSError) as e:
         print(e)
       except RuntimeError:

+ 11 - 11
tf_extend.py

@@ -4,11 +4,11 @@ import os,sys,network,socket,time,machine,gc,tf,btree
 # by intercepting the .write()
 class wc():
   def __init__(self):
-    self.words=self.lines=self.bytes_=0
+    self.c = [0,0,0] # lines, words & bytes
   def write(self,text):
-    self.bytes_ += len(text)
-    self.lines += 1
-    self.words += len(text.split())
+    self.c[2] += len(text)
+    self.c[0] += 1
+    self.c[1] += len(text.split())
 
 class lessor():
   def __init__(self,nums=False):
@@ -46,20 +46,20 @@ def cmd(args):
     try:
       tf.transfer(args[1],w)
       if cmd=='wc':
-        print(f"lines: {w.lines}\twords: {w.words}\tbytes: {w.bytes_}")
+        print("lines: {}\twords: {}\tbytes: {}".format(*w.c))
     except:
       print("file not found: "+args[1])
 
   elif cmd in ('ifconfig','ip'):
     ifc=network.WLAN().ifconfig()
-    print(f"IP: {ifc[0]}\tmask: {ifc[1]}\tgateway: {ifc[2]}\tDNS: {ifc[3]}")
+    print("IP: {}\tmask: {}\tgateway: {}\tDNS: {}".format(*ifc))
 
   elif cmd in ('host','nslookup','dig'):
     if len(args)<2:
       print("syntax: host <domain.name>")
     else:
       try:
-        print(f"host <{args[1]}> is at {socket.getaddrinfo(args[1],80)[0][-1][0]}")
+        print("host <{}> is at {}".format(args[1],socket.getaddrinfo(args[1],80)[0][-1][0]))
       except:
         print("network/DNS not available")
 
@@ -81,7 +81,7 @@ def cmd(args):
       print("no AP found")
       return True
     for i in s:
-      print(f"ch: {i[2]}\tRSSI: {i[3]}\t{"open" if i[4]==0 else ""}\tSSID: {i[0]}")
+      print("ch: {}\tRSSI: {}\t{}\tSSID: {}".format(i[2],i[3],"open" if i[4]==0 else "",i[0]))
 
   elif cmd=='freq':
     # identify esp32 or esp8266
@@ -93,7 +93,7 @@ def cmd(args):
     if len(args)==1 or args[1] in freqs:
       if len(args)>1:
         machine.freq(int(args[1])*1000000)
-      print(f"master cpu frequency {machine.freq()//1000000}MHz")
+      print("master cpu frequency {}MHz".format(machine.freq()//1000000))
     else:
       print("syntax: freq [ 160 | 80 | 240 ] ")
 
@@ -104,7 +104,7 @@ def cmd(args):
       print("Key\t\tValue")
       i=0
       for k,v in b.items():
-        print(f"{k:10}\t{v}")
+        print("{:10}\t{}".format(k,v))
         i+=1
         if i%30==0:
           r=input("continue? ")
@@ -118,7 +118,7 @@ def cmd(args):
     except OSError:
       print("file not found")
   elif cmd=='free':
-    print(f"memory used: {gc.mem_alloc()}\tmemory free:{gc.mem_free()}")
+    print("memory used: {}\tmemory free:{}".format(gc.mem_alloc(),gc.mem_free()))
   elif cmd=='help':
     print("==Extended commands")
     print("  connect <essid> <password> \tscan")