From 1bea27095c52939c1080456247c7109dd85457f4 Mon Sep 17 00:00:00 2001
From: Yngve Inntjore Levinsen <Yngve.Levinsen@esss.se>
Date: Fri, 9 Sep 2016 11:15:27 +0200
Subject: [PATCH] exit -> sys.exit made all print statements python3 safe
 absolute import

---
 ess/lib_tw.py      | 34 +++++++++++++++++++---------------
 ess/lib_tw_elem.py |  8 ++++++--
 2 files changed, 25 insertions(+), 17 deletions(-)

diff --git a/ess/lib_tw.py b/ess/lib_tw.py
index 63a8c8a..b5b9486 100644
--- a/ess/lib_tw.py
+++ b/ess/lib_tw.py
@@ -7,6 +7,9 @@
         correction is established with new classes.
 '''
 
+from __future__ import print_function
+from __future__ import absolute_import
+
 #---- Lib
 
 from numpy        import *
@@ -18,9 +21,9 @@ from itertools import chain
 from subprocess import check_output
 from os         import system
 from os.path    import isdir,isfile
-from sys        import exit
+import sys
 
-from lib_tw_elem import *
+from .lib_tw_elem import *
 
 
 
@@ -867,7 +870,7 @@ def loss_elem2den(s,loss,file_name_dt='',dlt_dt=5e-6):
                 L[i]=L_dt[dL.index(dL_min)]; Ndt+=1
         # Check
         if Ndt!=len(L_dt):
-            print 'drift-tube # unmatched, in file: '+str(len(L_dt))+', matched: '+str(Ndt)
+            print('drift-tube # unmatched, in file: {}, matched: {}'.format(len(L_dt),Ndt))
     except:
         pass
 
@@ -875,7 +878,7 @@ def loss_elem2den(s,loss,file_name_dt='',dlt_dt=5e-6):
     loss_den=loss[::]
     for i in range(len(loss_den)):
         if loss_den[i]!=0.0 and L[i]==0.0:
-            print 'Caution: inf loss density at elem # ',i,'!!!!'
+            print('Caution: inf loss density at elem # {} !!!!'.format(i))
             for k in range(i)[::-1]:
                 if L[k]!=0.0: loss_den[k]+=loss_den[i]; loss_den[i]=0.0; break
 
@@ -885,7 +888,7 @@ def loss_elem2den(s,loss,file_name_dt='',dlt_dt=5e-6):
 
     return loss_den
 
-    exit()
+    sys.exit()
 
     # Define L
     l=[s[i+1]-s[i] for i in range(len(s)-1)]; l.insert(0,0.0)
@@ -904,7 +907,7 @@ def loss_elem2den(s,loss,file_name_dt='',dlt_dt=5e-6):
                 l[i]=l_dt[dl.index(dl_min)]; Ndt+=1
         # Check
         if Ndt!=len(l_cell):
-            print 'drift-tube # unmatched, in file: '+str(len(l_cell))+', matched: '+str(Ndt)
+            print('drift-tube # unmatched, in file: {}, matched: {}'.format(len(l_cell),Ndt))
     except:
         pass
 
@@ -912,12 +915,13 @@ def loss_elem2den(s,loss,file_name_dt='',dlt_dt=5e-6):
     for i in range(len(l)):
         if l[i]==0.0 and loss[i]!=0.0:
             if i==1:
-                print 'The 1st elem has 0 length and yet has a loss, exiting...'; exit()
+                print('The 1st elem has 0 length and yet has a loss, exiting...')
+                sys.exit()
             else:
                 k=1
                 while l[i-k]==0.0: k+=1
                 loss[i-k]+=loss[i]; loss[i]=0.0
-                print 'Caution: Inf loss density at elem #',i,'!!!!'
+                print('Caution: Inf loss density at elem # {} !!!!'.format(i))
 
     # Finally, convert to loss density
     loss_den=[]
@@ -952,13 +956,13 @@ def loss_elem2den(s,loss,file_name_dt='',dlt_dt=5e-6):
 ##     # Check the replacement.
 ##     if Ndt!=len(l_cell):
 ##         print 'drift-tube # unmatched, in file: '+str(len(l_cell))+', from matching: '+str(Ndt)
-##         print 'review the threshold, exiting...'; exit()
+##         print 'review the threshold, exiting...'; sys.exit()
 
 ##     # Treat inf loss den.
 ##     for i in range(len(l)):
 ##         if l[i]==0.0 and loss[i]!=0.0:
 ##             if i==1:
-##                 print 'The 1st elem has 0 length and yet has a loss, exiting...'; exit()
+##                 print 'The 1st elem has 0 length and yet has a loss, exiting...'; sys.exit()
 ##             else:
 ##                 k=1
 ##                 while l[i-k]==0.0: k+=1
@@ -1361,23 +1365,23 @@ def _update_field_map_dict(dictionary,folder_path):
 ##         if path_cal[-1]!='/': path_cal+='/'
 
 ##         # Check the calc dir exists.
-##         if isdir(path_cal)==False: print 'Calc dir does not exist !!!! Exiting ...'; exit()
+##         if isdir(path_cal)==False: print 'Calc dir does not exist !!!! Exiting ...'; sys.exit()
 
 ##         # Check "tracelx64" is in the calc dir.
 ##         if isfile(path_cal+'tracelx64')==False:
 ##             try   : system('cp /opt/cea/tracelx64 '+path_cal)
-##             except: print 'tracelx64 not in calc dir !!!! Exiting ...'; exit()
+##             except: print 'tracelx64 not in calc dir !!!! Exiting ...'; sys.exit()
 
 ##         # Check "tracewin.key" is in the calc dir.
 ##         if isfile(path_cal+'tracewin.key')==False:
 ##             try   : system('cp /opt/cea/tracewin.key '+path_cal)
-##             except: print 'tracewin.key not in calc dir !!!! Exiting ...'; exit()
+##             except: print 'tracewin.key not in calc dir !!!! Exiting ...'; sys.exit()
 
 ##         # Check the project/lattice file is in the calc dir.
 ##         if '/' not in file_name_proj: file_name_proj=path_cal+file_name_proj
 ##         if '/' not in file_name_lat : file_name_lat =path_cal+file_name_lat
-##         if isfile(file_name_proj)==False: print 'project file not in calc dir !!!! Exiting ...'; exit()
-##         if isfile(file_name_lat) ==False: print 'lattice file not in calc dir !!!! Exiting ...'; exit()
+##         if isfile(file_name_proj)==False: print 'project file not in calc dir !!!! Exiting ...'; sys.exit()
+##         if isfile(file_name_lat) ==False: print 'lattice file not in calc dir !!!! Exiting ...'; sys.exit()
 
 ##         # Instances (Add more options as needed.)
 ##         self.path_cal      =path_cal
diff --git a/ess/lib_tw_elem.py b/ess/lib_tw_elem.py
index aebc37e..e8b2086 100644
--- a/ess/lib_tw_elem.py
+++ b/ess/lib_tw_elem.py
@@ -79,10 +79,12 @@
         - Asymmetric aperture of DRIFT implemented.
 '''
 
+from __future__ import print_function
+
 #---- Libs
 
 from numpy import *
-from sys   import exit
+import sys
 
 #---- Constants
 
@@ -210,7 +212,9 @@ class FIELD_MAP(ELEM):
     def update_gamma(self):
 
         # Temp error message for field map types
-        if self.typ_fmap!='100': print 'Gamma calc only supported for 1D, exiting ...', exit()
+        if self.typ_fmap!='100':
+            print('Gamma calc only supported for 1D, exiting ...')
+            sys.exit()
 
         # Update gamma (simple ver, closer to TW)
         dL=self.data.dL; V=self.Enom*array(self.data.field)*dL; phs=self.phs_rf; C=0.0; S=0.0
-- 
GitLab