changeset 67:13182e98a1ab

use sorted insertion into tuple list for properties
author Henry S. Thompson <ht@markup.co.uk>
date Thu, 04 Jun 2020 17:58:10 +0000
parents b04870ab3035
children 1f04bce6ead7
files master/src/wecu/sac_schemes.py
diffstat 1 files changed, 21 insertions(+), 5 deletions(-) [+]
line wrap: on
line diff
--- a/master/src/wecu/sac_schemes.py	Thu Jun 04 16:10:55 2020 +0000
+++ b/master/src/wecu/sac_schemes.py	Thu Jun 04 17:58:10 2020 +0000
@@ -10,6 +10,8 @@
 
 import sys, json, regex
 
+print(sys.argv,file=sys.stderr)
+
 if len(sys.argv)>1 and sys.argv[1]=='-d':
   sys.argv.pop(1)
   dictRes=True
@@ -29,6 +31,20 @@
 
 D={}
 
+def insert(e,tbt):
+  '''insert something into a trivial pair-impl of a list,
+  not balanced!'''
+  if isinstance(tbt,tuple):
+    assert not isinstance(tbt[0],tuple)
+    if e<=tbt[0]:
+      return (e,tbt)
+    else:
+      return (tbt[0],insert(e,tbt[1]))
+  elif e<=tbt:
+    return (e,tbt)
+  else:
+    return (tbt,e)
+
 def walk(o,f,r,path=None):
   '''Apply f to every key+leaf of a json object reached via p in region r'''
   if isinstance(o,dict):
@@ -53,7 +69,7 @@
           #print(v,D,kk,file=sys.stderr)
           if v in D:
             (rr,pp,jj,ss)=D[v]
-            D[v]=(rr,pp,(jj,k),ss)
+            D[v]=(rr,pp,insert(k,jj),ss)
           else:
             D[v]=kk
     if D:
@@ -152,8 +168,8 @@
     else:
       print(DOT,p,sep=EMPTY,end='\t')
     while isinstance(k,tuple):
-      print(k[1],end='&')
-      k=k[0]
+      print(k[0],end='&')
+      k=k[1]
     print(k,end='\t')
     print(s,c,sep='\t')
 
@@ -170,13 +186,13 @@
     print(k,end='\t')
     print(s,c,sep='\t')
 
-if len(sys.argv)==2:
+if len(sys.argv)>=2:
   res=dict()
   if sys.argv[1]=='1':
     print('using tuple',file=sys.stderr)
     pp=pp_tuple 
     dump=dump_tuple
-  else:
+  elif sys.argv[1]=='2':
     print('using concat',file=sys.stderr)
     pp=pp_concat
     dump=dump_concat