python: Open files using "with"
This commit is contained in:
@@ -12,11 +12,11 @@ import os
|
||||
import codecs
|
||||
|
||||
def dump_pickles(out, dirname, filename, path):
|
||||
f = open(os.path.join(dirname, filename), 'r', encoding='utf-8')
|
||||
data = pickle.load(f)
|
||||
fragment_file = codecs.open(data['current_page_name'] + '.frag', mode='w', encoding='utf-8')
|
||||
fragment_file.write(data['body'])
|
||||
fragment_file.close()
|
||||
with open(os.path.join(dirname, filename), 'r', encoding='utf-8') as f:
|
||||
data = pickle.load(f)
|
||||
with codecs.open(data['current_page_name'] + '.frag', mode='w', encoding='utf-8') as fragment_file:
|
||||
fragment_file.write(data['body'])
|
||||
|
||||
out.write(' <page url="%s">\n' % path)
|
||||
out.write(' <fragment>%s.frag</fragment>\n' % data['current_page_name'])
|
||||
if data['prev'] is not None:
|
||||
@@ -28,7 +28,7 @@ def dump_pickles(out, dirname, filename, path):
|
||||
(os.path.normpath(os.path.join(path, data['next']['link'])),
|
||||
data['next']['title']))
|
||||
out.write(' </page>\n')
|
||||
f.close()
|
||||
|
||||
if data['next'] is not None:
|
||||
next_path = os.path.normpath(os.path.join(path, data['next']['link']))
|
||||
next_filename = os.path.basename(next_path) + '.fpickle'
|
||||
|
||||
@@ -12,11 +12,11 @@ import os
|
||||
import codecs
|
||||
|
||||
def dump_pickles(out, dirname, filename, path):
|
||||
f = open(os.path.join(dirname, filename), 'r', encoding='utf-8')
|
||||
data = pickle.load(f)
|
||||
fragment_file = codecs.open(data['current_page_name'] + '.frag', mode='w', encoding='utf-8')
|
||||
fragment_file.write(data['body'])
|
||||
fragment_file.close()
|
||||
with open(os.path.join(dirname, filename), 'r', encoding='utf-8') as f:
|
||||
data = pickle.load(f)
|
||||
with codecs.open(data['current_page_name'] + '.frag', mode='w', encoding='utf-8') as fragment_file:
|
||||
fragment_file.write(data['body'])
|
||||
|
||||
out.write(' <page url="%s">\n' % path)
|
||||
out.write(' <fragment>%s.frag</fragment>\n' % data['current_page_name'])
|
||||
if data['prev'] is not None:
|
||||
@@ -28,7 +28,7 @@ def dump_pickles(out, dirname, filename, path):
|
||||
(os.path.normpath(os.path.join(path, data['next']['link'])),
|
||||
data['next']['title']))
|
||||
out.write(' </page>\n')
|
||||
f.close()
|
||||
|
||||
if data['next'] is not None:
|
||||
next_path = os.path.normpath(os.path.join(path, data['next']['link']))
|
||||
next_filename = os.path.basename(next_path) + '.fpickle'
|
||||
|
||||
@@ -179,23 +179,23 @@ class Simulation(object):
|
||||
|
||||
|
||||
def main(argv):
|
||||
file_obj = open(argv[1], encoding="utf-8")
|
||||
print("Reading XML file ", end=" ")
|
||||
with open(argv[1], encoding="utf-8") as file_obj:
|
||||
print("Reading XML file ", end=" ")
|
||||
|
||||
sys.stdout.flush()
|
||||
level = 0
|
||||
sim_list = []
|
||||
for event, elem in ElementTree.iterparse(file_obj, events=("start", "end")):
|
||||
if event == "start":
|
||||
level += 1
|
||||
if event == "end":
|
||||
level -= 1
|
||||
if level == 0 and elem.tag == 'FlowMonitor':
|
||||
sim = Simulation(elem)
|
||||
sim_list.append(sim)
|
||||
elem.clear() # won't need this any more
|
||||
sys.stdout.write(".")
|
||||
sys.stdout.flush()
|
||||
sys.stdout.flush()
|
||||
level = 0
|
||||
sim_list = []
|
||||
for event, elem in ElementTree.iterparse(file_obj, events=("start", "end")):
|
||||
if event == "start":
|
||||
level += 1
|
||||
if event == "end":
|
||||
level -= 1
|
||||
if level == 0 and elem.tag == 'FlowMonitor':
|
||||
sim = Simulation(elem)
|
||||
sim_list.append(sim)
|
||||
elem.clear() # won't need this any more
|
||||
sys.stdout.write(".")
|
||||
sys.stdout.flush()
|
||||
print(" done.")
|
||||
|
||||
|
||||
|
||||
@@ -438,9 +438,8 @@ def print_cplusplus_map_from_fit_results(fit: pd.DataFrame, out_fname: str):
|
||||
out_str = out_str[0:-2]
|
||||
out_str += '}\n'
|
||||
|
||||
f = open(out_fname, "w", encoding="utf-8")
|
||||
f.write(out_str)
|
||||
f.close()
|
||||
with open(out_fname, "w", encoding="utf-8") as f:
|
||||
f.write(out_str)
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
@@ -499,12 +498,10 @@ if __name__ == '__main__':
|
||||
res = joblib.Parallel(n_jobs=10)(
|
||||
joblib.delayed(fit_ftr_to_reference)(df, params_comb, num_search_grid_params, num_refinements) for params_comb in product(scenarios, is_los, frequencies))
|
||||
|
||||
f = open(fit_out_fname, "w", encoding="utf-8")
|
||||
f.write("scen\tcond\tfc\tsigma\tk\tdelta\tm\n")
|
||||
for line in res:
|
||||
f.write(line)
|
||||
|
||||
f.close()
|
||||
with open(fit_out_fname, "w", encoding="utf-8") as f:
|
||||
f.write("scen\tcond\tfc\tsigma\tk\tdelta\tm\n")
|
||||
for line in res:
|
||||
f.write(line)
|
||||
|
||||
if output_ns3_table:
|
||||
|
||||
|
||||
@@ -129,41 +129,39 @@ ack_rates_160MHz = [6e6, 12e6, 12e6, 24e6, 24e6, 24e6, 24e6, 24e6, 24e6, 24e6, 2
|
||||
k = 1
|
||||
|
||||
difs = 1
|
||||
fo = open("bianchi_11ax_difs.txt", "w", encoding="utf-8")
|
||||
for i in range(len(data_rates_20MHz)):
|
||||
bianchi_result = bianchi_ax(data_rates_20MHz[i], ack_rates_20MHz[i], k, difs)
|
||||
str_s = str_result(bianchi_result, i, 20)
|
||||
fo.write(str_s)
|
||||
for i in range(len(data_rates_40MHz)):
|
||||
bianchi_result = bianchi_ax(data_rates_40MHz[i], ack_rates_40MHz[i], k, difs)
|
||||
str_s = str_result(bianchi_result, i, 40)
|
||||
fo.write(str_s)
|
||||
for i in range(len(data_rates_80MHz)):
|
||||
bianchi_result = bianchi_ax(data_rates_80MHz[i], ack_rates_80MHz[i], k, difs)
|
||||
str_s = str_result(bianchi_result, i, 80)
|
||||
fo.write(str_s)
|
||||
for i in range(len(data_rates_160MHz)):
|
||||
bianchi_result = bianchi_ax(data_rates_160MHz[i], ack_rates_160MHz[i], k, difs)
|
||||
str_s = str_result(bianchi_result, i, 160)
|
||||
fo.write(str_s)
|
||||
fo.close()
|
||||
with open("bianchi_11ax_difs.txt", "w", encoding="utf-8") as fo:
|
||||
for i in range(len(data_rates_20MHz)):
|
||||
bianchi_result = bianchi_ax(data_rates_20MHz[i], ack_rates_20MHz[i], k, difs)
|
||||
str_s = str_result(bianchi_result, i, 20)
|
||||
fo.write(str_s)
|
||||
for i in range(len(data_rates_40MHz)):
|
||||
bianchi_result = bianchi_ax(data_rates_40MHz[i], ack_rates_40MHz[i], k, difs)
|
||||
str_s = str_result(bianchi_result, i, 40)
|
||||
fo.write(str_s)
|
||||
for i in range(len(data_rates_80MHz)):
|
||||
bianchi_result = bianchi_ax(data_rates_80MHz[i], ack_rates_80MHz[i], k, difs)
|
||||
str_s = str_result(bianchi_result, i, 80)
|
||||
fo.write(str_s)
|
||||
for i in range(len(data_rates_160MHz)):
|
||||
bianchi_result = bianchi_ax(data_rates_160MHz[i], ack_rates_160MHz[i], k, difs)
|
||||
str_s = str_result(bianchi_result, i, 160)
|
||||
fo.write(str_s)
|
||||
|
||||
difs = 0
|
||||
fo = open("bianchi_11ax_eifs.txt", "w", encoding="utf-8")
|
||||
for i in range(len(data_rates_20MHz)):
|
||||
bianchi_result = bianchi_ax(data_rates_20MHz[i], ack_rates_20MHz[i], k, difs)
|
||||
str_s = str_result(bianchi_result, i, 20)
|
||||
fo.write(str_s)
|
||||
for i in range(len(data_rates_40MHz)):
|
||||
bianchi_result = bianchi_ax(data_rates_40MHz[i], ack_rates_40MHz[i], k, difs)
|
||||
str_s = str_result(bianchi_result, i, 40)
|
||||
fo.write(str_s)
|
||||
for i in range(len(data_rates_80MHz)):
|
||||
bianchi_result = bianchi_ax(data_rates_80MHz[i], ack_rates_80MHz[i], k, difs)
|
||||
str_s = str_result(bianchi_result, i, 80)
|
||||
fo.write(str_s)
|
||||
for i in range(len(data_rates_160MHz)):
|
||||
bianchi_result = bianchi_ax(data_rates_160MHz[i], ack_rates_160MHz[i], k, difs)
|
||||
str_s = str_result(bianchi_result, i, 160)
|
||||
fo.write(str_s)
|
||||
fo.close()
|
||||
with open("bianchi_11ax_eifs.txt", "w", encoding="utf-8") as fo:
|
||||
for i in range(len(data_rates_20MHz)):
|
||||
bianchi_result = bianchi_ax(data_rates_20MHz[i], ack_rates_20MHz[i], k, difs)
|
||||
str_s = str_result(bianchi_result, i, 20)
|
||||
fo.write(str_s)
|
||||
for i in range(len(data_rates_40MHz)):
|
||||
bianchi_result = bianchi_ax(data_rates_40MHz[i], ack_rates_40MHz[i], k, difs)
|
||||
str_s = str_result(bianchi_result, i, 40)
|
||||
fo.write(str_s)
|
||||
for i in range(len(data_rates_80MHz)):
|
||||
bianchi_result = bianchi_ax(data_rates_80MHz[i], ack_rates_80MHz[i], k, difs)
|
||||
str_s = str_result(bianchi_result, i, 80)
|
||||
fo.write(str_s)
|
||||
for i in range(len(data_rates_160MHz)):
|
||||
bianchi_result = bianchi_ax(data_rates_160MHz[i], ack_rates_160MHz[i], k, difs)
|
||||
str_s = str_result(bianchi_result, i, 160)
|
||||
fo.write(str_s)
|
||||
|
||||
22
test.py
22
test.py
@@ -593,24 +593,22 @@ def sigint_hook(signal, frame):
|
||||
#
|
||||
def read_ns3_config():
|
||||
lock_filename = ".lock-ns3_%s_build" % sys.platform
|
||||
f = None
|
||||
|
||||
try:
|
||||
# sys.platform reports linux2 for python2 and linux for python3
|
||||
f = open(lock_filename, "rt", encoding="utf-8")
|
||||
with open(lock_filename, "rt", encoding="utf-8") as f:
|
||||
for line in f:
|
||||
if line.startswith("top_dir ="):
|
||||
key, val = line.split('=')
|
||||
top_dir = eval(val.strip())
|
||||
if line.startswith("out_dir ="):
|
||||
key, val = line.split('=')
|
||||
out_dir = eval(val.strip())
|
||||
|
||||
except FileNotFoundError:
|
||||
print('The .lock-ns3 file was not found. You must configure before running test.py.', file=sys.stderr)
|
||||
sys.exit(2)
|
||||
|
||||
for line in f:
|
||||
if line.startswith("top_dir ="):
|
||||
key, val = line.split('=')
|
||||
top_dir = eval(val.strip())
|
||||
if line.startswith("out_dir ="):
|
||||
key, val = line.split('=')
|
||||
out_dir = eval(val.strip())
|
||||
|
||||
f.close()
|
||||
|
||||
global NS3_BASEDIR
|
||||
NS3_BASEDIR = top_dir
|
||||
global NS3_BUILDDIR
|
||||
|
||||
Reference in New Issue
Block a user