summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMarius Peter <blendoit@gmail.com>2019-06-12 11:58:00 -0700
committerMarius Peter <blendoit@gmail.com>2019-06-12 11:58:00 -0700
commit976006fbe5f0a141af1041510a640a8e71a79150 (patch)
treebee77a0ee3f50c79c216221c0d49883c70e00654
parentde1ec9a5fafae98fa1168223a78453e819840c0f (diff)
added context guard in save_coord
-rw-r--r--creator.py15
-rw-r--r--main.py8
2 files changed, 13 insertions, 10 deletions
diff --git a/creator.py b/creator.py
index c6cae4e..35784a8 100644
--- a/creator.py
+++ b/creator.py
@@ -85,16 +85,19 @@ class Coordinates:
# print('\n')
return None
- def save_coord(self, save_dir_path):
+ def save_coord(self, save_dir_path, number):
"""
Save all the object's coordinates (must be full path).
"""
- file_name = str(self)
- full_path = os.path.join(save_dir_path, file_name + '.txt')
- file = open(full_path, 'w')
- sys.stdout = file
- self.print_coord(4)
+ file_name = '{}_{}.txt'.format(self, number)
+ full_path = os.path.join(save_dir_path, file_name)
+ # sys.stdout = open(full_path, 'w')
+ # self.print_coord(2)
+ with open(full_path, 'w') as sys.stdout:
+ self.print_coord(2)
+ # It is cleaner to use this context guard to ensure file is closed
+
return None
def pack_coord(self):
diff --git a/main.py b/main.py
index a2dbd94..5d2e90a 100644
--- a/main.py
+++ b/main.py
@@ -21,7 +21,7 @@ import random
import time
start_time = time.time()
-CHORD_LENGTH = 1000
+CHORD_LENGTH = 100
SEMI_SPAN = 200
POP_SIZE = 1
@@ -58,9 +58,9 @@ def main():
creator.plot(af, af.spar, af.stringer)
# Save component coordinates
- af.save_coord(SAVE_PATH)
- af.spar.save_coord(SAVE_PATH)
- af.stringer.save_coord(SAVE_PATH)
+ af.save_coord(SAVE_PATH, _)
+ af.spar.save_coord(SAVE_PATH, _)
+ af.stringer.save_coord(SAVE_PATH, _)
# Print final execution time
print("--- %s seconds ---" % (time.time() - start_time))
Copyright 2019--2024 Marius PETER