1+ import sys
2+
13import urllib .request , urllib .error , urllib .parse , urllib .request , urllib .parse , urllib .error
24import difflib
35import os .path
911from amuse .units import derivedsi
1012
1113NIST_URL = "http://132.229.222.6:9000/nistdata"
12- MESSAGE = \
13- """
14- #This is an auto generated file, do not change manually. Instead if you want to add constants
15- #or change them, change the nist.txt file and run nist.py
14+ MESSAGE = """\" \" \"
15+ Physical constants
16+ \" \" \"
17+ # This is an auto generated file, do not change manually. Instead if you want
18+ # to add constants or change them, change the nist.txt file and run nist.py
1619
1720import numpy
18- from amuse.units.si import *
19- from amuse.units.derivedsi import *
21+ from amuse.units.si import m, kg, s, A, K, mol, none
22+ from amuse.units.derivedsi import Hz, MHz, sr, N, Pa, J, W, F, C, V, T, ohm, S, Wb
2023
2124"""
22- ADDITIONAL_DERIVED_CONSTANTS = \
23- """
25+ ADDITIONAL_DERIVED_CONSTANTS = """
2426pi = numpy.pi
2527hbar = h / (2.0 * numpy.pi)
2628four_pi_stefan_boltzmann = 4.0 * numpy.pi * Stefan_hyphen_Boltzmann_constant
3133eps = numpy.finfo(numpy.double).eps
3234precision = int(numpy.log10(2/eps))
3335"""
34- class GetConstantsFromFiles (object ):
35-
36- def __init__ (self ):
36+
37+
38+ class GetConstantsFromFiles :
39+
40+ def __init__ (self , filename = "nist.txt" ):
41+ self .nist_filename = filename
3742 self .nist_table = ""
3843 self .local_table = ""
3944 self .translator_table = []
@@ -45,157 +50,213 @@ def get_table_from_url(self):
4550 f .close ()
4651
4752 def save_table_as (self , filename ):
48- f = open (os .path .join (self .directory , 'nist.txt' ), 'w' )
53+ f = open (os .path .join (self .directory , self . nist_filename ), "w" )
4954 f .write (self .nist_table )
5055 f .close ()
51-
56+
5257 def get_table_from_file (self ):
53- f = open (os .path .join (self .directory , 'nist.txt' ), 'r' ) # CODATA 2006, for CODATA 2010 use 'nist2010.txt'
54- self .nist_table = f .read ()
55- f .close ()
58+ f = open (
59+ os .path .join (self .directory , self .nist_filename ), "r"
60+ ) # CODATA 2006, for CODATA 2010 use 'nist2010.txt'
61+ self .nist_table = f .read ()
62+ f .close ()
5663
5764 def check_current_file_with_table (self ):
58- md5sum_local = md5 ()
59- md5sum_local .update (self .local_table )
60- md5sum_local_val = md5sum_local .hexdigest ()
61- md5sum_wgot = md5 ()
62- md5sum_wgot .update (self .nist_table )
63- md5sum_wgot_val = md5sum_wgot .hexdigest ()
64- return md5sum_local_val == md5sum_wgot_val
65+ md5sum_local = md5 ()
66+ md5sum_local .update (self .local_table )
67+ md5sum_local_val = md5sum_local .hexdigest ()
68+ md5sum_wgot = md5 ()
69+ md5sum_wgot .update (self .nist_table )
70+ md5sum_wgot_val = md5sum_wgot .hexdigest ()
71+ return md5sum_local_val == md5sum_wgot_val
6572
6673 def compare_char_by_char (self ):
6774
68- self .nist_table .lstrip ('\n ' )
69- mydiff = difflib .unified_diff (self .nist_table .splitlines (1 ), self .local_table .splitlines (1 ))
75+ self .nist_table .lstrip ("\n " )
76+ mydiff = difflib .unified_diff (
77+ self .nist_table .splitlines (1 ), self .local_table .splitlines (1 )
78+ )
7079 for i in list (mydiff ):
7180 print (i )
72-
81+
7382 def get_translator (self ):
74- f = open (os .path .join (self .directory , ' translator.txt' ), 'r' )
83+ f = open (os .path .join (self .directory , " translator.txt" ), "r" )
7584 lines = f .readlines ()
7685
7786 for i , s in enumerate (lines ):
78- cols = s .split (',' )
87+ cols = s .split ("," )
7988 self .translator_table .append (cols )
8089
81- f .close ()
90+ f .close ()
91+
8292
83- class Constants ( object ) :
84- def __init__ (self ):
85- self .I = GetConstantsFromFiles ()
86- #I.get_table_from_url()
93+ class Constants :
94+ def __init__ (self , filename = "nist.txt" ):
95+ self .I = GetConstantsFromFiles (filename = filename )
96+ # I.get_table_from_url()
8797 self .I .get_table_from_file ()
8898 self .table = self .I .nist_table
8999 self .I .get_translator ()
90100 self .translator = self .I .translator_table
91101 self .nistfile = MESSAGE
92-
102+
93103 self .nisttable = []
94104 self .nisttablederivedunits = []
95105 self .nisttablenoneunits = []
96106 self .nisttablebaseunits = []
97107 self .nisttabledependingunits = []
98108
99- self .siunits = dir (si )+ dir (derivedsi )
100-
109+ self .siunits = dir (si ) + dir (derivedsi )
110+
101111 def test_regexp (self , regexp ):
102- lines = self .table .splitlines (1 )
103- for i ,line in enumerate (lines ):
104- if i > 80 :
112+ lines = self .table .splitlines (1 )
113+ for i , line in enumerate (lines ):
114+ if i > 80 :
105115 break
106116 print (re .findall (regexp , line ))
107117
108118 def translate (self , to_translate ):
109119 list = [s [1 ] for s in self .translator if to_translate == s [0 ]]
110120 if list == []:
111- return to_translate .lstrip (' ' )
121+ return to_translate .lstrip (" " )
112122 else :
113- return list [0 ].strip (' \n ' )
123+ return list [0 ].strip (" \n " )
114124
115125 def list_constants (self ):
116- error = []
126+ error = []
117127 value = []
118128 name = []
119129 unit = []
120130
121- lines = self .table .splitlines (1 )
131+ lines = self .table .splitlines (1 )
122132 for n , line in enumerate (lines ):
123133 if "----------------------" in line :
124134 number_of_header_lines = n + 1
125135 break
126136 firstline = lines [number_of_header_lines ]
127- namestr_length = len (firstline ) - len (firstline [firstline .find (" " ):].lstrip ())
128- column_index_of_uncertainty = len (firstline ) - len (firstline [namestr_length + 21 + firstline [namestr_length + 21 :].find (" " ):].lstrip ())
129- column_index_of_unit = len (firstline ) - len (firstline [column_index_of_uncertainty + 21 + firstline [column_index_of_uncertainty + 21 :].find (" " ):].lstrip ())
137+ print ()
138+ print (firstline )
139+ namestr_length = len (firstline ) - len (
140+ firstline [firstline .find (" " ) :].lstrip ()
141+ )
142+ column_index_of_uncertainty = len (firstline ) - len (
143+ firstline [
144+ namestr_length + 21 + firstline [namestr_length + 21 :].find (" " ) :
145+ ].lstrip ()
146+ )
147+ column_index_of_unit = len (firstline ) - len (
148+ firstline [
149+ column_index_of_uncertainty
150+ + 21
151+ + firstline [column_index_of_uncertainty + 21 :].find (" " ) :
152+ ].lstrip ()
153+ )
130154 for i in lines [number_of_header_lines :]:
131155 namestr = i [0 :namestr_length ]
132156
133157 marker1 = column_index_of_uncertainty
134158 marker2 = column_index_of_unit
135159
136160 while 1 :
137- if i [marker1 - 1 ] == ' \x20 ' :
161+ if i [marker1 - 1 ] == " \x20 " :
138162 break
139163 else :
140- marker1 += 1
164+ marker1 += 1
141165 while 1 :
142- if i [marker2 - 1 ]== '\x20 ' :
166+ try :
167+ if i [marker2 - 1 ] == "\x20 " :
168+ break
169+ else :
170+ marker2 += 1
171+ except :
143172 break
144- else :
145- marker2 += 1
173+ # print(f"i (length {len(i)}): {i}")
174+ # print(f"marker2: {marker2}")
175+ # sys.exit()
146176
147- nrs = []
177+ nrs = []
148178 nrs .append (i [namestr_length :marker1 ])
149179 nrs .append (i [marker1 :marker2 ])
150180 unitstr = i [marker2 :]
151-
152- unitstr = unitstr .strip ().replace (' ' ,'*' ).replace ('^' ,'**' )
153181
154- new_name = self .translate (namestr .rstrip (' ' ).replace (' ' ,'_' ).replace ('.' ,'' ).replace ('{' ,'X' ).replace ('}' ,'X' ).replace ('(' ,'X' ).replace (')' ,'X' ).replace ('-' ,'_hyphen_' ).replace (',' ,'_and_' ).replace ('/' ,'_div_' ))
155- error .append (nrs [1 ].replace (' ' ,'' ))
156- if len (unitstr )== 1 :
182+ unitstr = unitstr .strip ().replace (" " , " * " ).replace ("^" , "**" )
183+
184+ new_name = self .translate (
185+ namestr .rstrip (" " )
186+ .replace (" " , "_" )
187+ .replace ("." , "" )
188+ .replace ("{" , "X" )
189+ .replace ("}" , "X" )
190+ .replace ("(" , "X" )
191+ .replace (")" , "X" )
192+ .replace ("-" , "_hyphen_" )
193+ .replace ("," , "_and_" )
194+ .replace ("/" , "_div_" )
195+ )
196+ error .append (nrs [1 ].replace (" " , "" ))
197+ if len (unitstr ) == 1 :
157198 this_unit = "none\n "
158199 else :
159200 this_unit = unitstr
160201
161- self .nisttable .append ([new_name , float (i [namestr_length :marker1 ].replace (' ' ,'' ).replace ('...' ,'' )), unitstr ])
202+ self .nisttable .append (
203+ [
204+ new_name ,
205+ float (
206+ i [namestr_length :marker1 ].replace (" " , "" ).replace ("..." , "" )
207+ ),
208+ unitstr ,
209+ ]
210+ )
162211
163212 def sort_units (self ):
164213 for entry in self .nisttable :
165214 if entry [2 ] in self .siunits :
166215 self .nisttablebaseunits .append (entry )
167- elif entry [2 ] == '' :
216+ elif entry [2 ] == "" :
168217 self .nisttablenoneunits .append (entry )
169- elif set (re .split ('[*/^]' ,re .sub ('\*\*-?[0-9.]*' ,'' ,entry [2 ]))).issubset (set (self .siunits )):
218+ elif set (re .split ("[*/^]" , re .sub ("\*\*-?[0-9.]*" , "" , entry [2 ]))).issubset (
219+ set (self .siunits )
220+ ):
170221 self .nisttablederivedunits .append (entry )
171222 else :
172223 self .nisttabledependingunits .append (entry )
173-
224+
174225 def print_list_of_units (self , unitlist ):
175226 for name , value , unit in unitlist :
176- self .nistfile += ( "{0} = {1} | {2}\n " .format (name , value , unit or "none" ) )
227+ self .nistfile += "{0} = {1} | {2}\n " .format (name , value , unit or "none" )
177228
178229 def generate_constants (self ):
179230 self .list_constants ()
180231 self .sort_units ()
181- self .nistfile += "# BASE UNITS***********************************************\n "
232+ self .nistfile += "# BASE UNITS***********************************************\n "
182233 self .print_list_of_units (self .nisttablebaseunits )
183- self .nistfile += "#DERIVED UNITS***********************************************\n "
234+ self .nistfile += (
235+ "# DERIVED UNITS***********************************************\n "
236+ )
184237 self .print_list_of_units (self .nisttablederivedunits )
185- self .nistfile += "#RATIOS ***********************************************\n "
238+ self .nistfile += "# RATIOS ***********************************************\n "
186239 self .print_list_of_units (self .nisttablenoneunits )
187- self .nistfile += "#DERIVED CONSTANTS***********************************************"
240+ self .nistfile += (
241+ "# DERIVED CONSTANTS***********************************************"
242+ )
188243 self .nistfile += ADDITIONAL_DERIVED_CONSTANTS
189- self .nistfile += '#DROPPED UNITS***********************************************\n """'
244+ self .nistfile += (
245+ '# DROPPED UNITS***********************************************\n """'
246+ )
190247 self .print_list_of_units (self .nisttabledependingunits )
191- self .nistfile += '"""\n '
248+ self .nistfile += '"""\n '
192249
193-
194- f = open (os .path .join (self .I .directory , 'constants.py' ), 'w' )
250+ f = open (os .path .join (self .I .directory , "constants.py" ), "w" )
195251 f .write (self .nistfile )
196252 f .close ()
197253
198- if __name__ == "__main__" :
199- print ("Generating constants.py..." , end = ' ' )
200- Constants ().generate_constants ()
254+
255+ if __name__ == "__main__" :
256+ if len (sys .argv ) > 1 :
257+ filename = sys .argv [1 ]
258+ else :
259+ filename = "nist.txt"
260+ print ("Generating constants.py..." , end = " " )
261+ Constants (filename = filename ).generate_constants ()
201262 print (" done!" )
0 commit comments