From e28328eaf772123c97d394d349fb0f11b8d03cdf Mon Sep 17 00:00:00 2001 From: facat Date: Sun, 29 Dec 2019 20:31:24 +0800 Subject: [PATCH] =?UTF-8?q?1.=E4=BF=AE=E5=A4=8D=E4=BA=86=E4=B8=80=E4=B8=AA?= =?UTF-8?q?=E7=94=9F=E6=88=90=E5=A4=87=E4=BB=BD=E6=96=87=E4=BB=B6=E6=97=B6?= =?UTF-8?q?=E9=97=B4=E7=9A=84bug=202.=E4=BF=AE=E5=A4=8D=E4=BA=86=E5=86=99?= =?UTF-8?q?=E5=85=A5S=E6=96=87=E4=BB=B6=E7=9A=84bug=E3=80=82?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- file_object.py | 85 +++++++++++++++++++++++++++++++++++--------------- main.py | 2 +- 2 files changed, 60 insertions(+), 27 deletions(-) diff --git a/file_object.py b/file_object.py index 5a6396d..d2480ea 100644 --- a/file_object.py +++ b/file_object.py @@ -4,9 +4,10 @@ import shutil import os.path import re + def file_backup_time(): now = datetime.datetime.now() - return now.strftime("%Y%I%d%H%M%S") + return now.strftime("%Y%m%d%H%M%S") def get_directory(file_path): @@ -23,7 +24,7 @@ def get_file_name_with_extention(file_path): class SFileObject: def __init__(self, file_path): self._content = [] - self._file_path = file_path + self.file_path = file_path self._read(file_path) pass @@ -38,11 +39,11 @@ class SFileObject: self._content.append(sep) def _save_backup(self, back_file_path): - shutil.copy(self._file_path, back_file_path) + shutil.copy(self.file_path, back_file_path) def write(self, target_file_path): - (file_name, ext) = get_file_name_with_extention(self._file_path) - src_file_dir = get_directory(self._file_path) + (file_name, ext) = get_file_name_with_extention(self.file_path) + src_file_dir = get_directory(self.file_path) self._save_backup( "{dir}/{file_name}-{time}.{ext}".format( dir=src_file_dir, file_name=file_name, time=file_backup_time(), ext=ext @@ -50,7 +51,8 @@ class SFileObject: ) with open(target_file_path, "wt") as file: for content in self._content: - file.write("{content}\n".format(content=" ".join(content))) + _content=[str(x) for x in content] + file.write("{content}\n".format(content=" ".join(_content))) def has(self, tower_number): # 塔位号是否存在S文件中。 for index, content in enumerate(self._content): @@ -66,6 +68,33 @@ class SFileObject: content[index][7] = new_height +# 把所有S文件当做一个来操作 +class SFileAsSingle: + def __init__(self, s_files): + self._s_files = s_files + + # 考虑不同位置处出现塔号的情况 + def has(self, tower_number) -> [(SFileObject, int)]: + s_file = self._s_files + ret = [] + for s in s_file: + d_file_obj = SFileObject(s) + index = d_file_obj.has(tower_number) + if index > 0: + ret.append((d_file_obj, index)) + return ret + + def update_height(self, result: [(SFileObject, int)], new_height): + s_changed_record = {} + for s_file_obj, index in result: + s_file_obj.update_height(index, new_height) + s_changed_record[s_file_obj] = 1 + for s_file_obj in s_changed_record: + s_output_file_path = s_file_obj.file_path + s_file_obj.write(s_output_file_path) + print("update S file {Ss}".format(Ss=s_output_file_path)) + + class TaFileObject: def __init__(self, file_path, SFile): # self._content = [] @@ -97,7 +126,7 @@ class TaFileObject: tower_height_record[tower_number] = tower_height return tower_height_record - def sync_tower_height(self, new_contents, old_tower_height_record): + def sync_tower_height_to_S(self, new_contents, old_tower_height_record): updated = False for content in new_contents: sep = content @@ -112,31 +141,34 @@ class TaFileObject: ) ) SFile = self._SFile - norm_tower_number = tower_number.replace("G", "") - for Ss in SFile: - d_file_obj = SFileObject(Ss) - index = d_file_obj.has(norm_tower_number) - if index > 0: - d_file_obj.update_height(index, str(new_tower_height)) - d_file_obj.write(Ss) - print("update S file {Ss}".format(Ss=Ss)) - break + s_as_single = SFileAsSingle(SFile) + result = s_as_single.has(tower_number) + if len(result) > 0: + s_as_single.update_height(result, new_tower_height) updated = True return updated + # 将S文件中的塔高同步到TA文件中。 + def sync_tower_height_from_S(self): + pass + def start(self): file_path = self._file_path - while True: + while True: # 检测文件是否有变化的循环 new_contents = self._read(file_path) # 每一次循环都会重新读一遍TA文件 if self._old_tower_height_record is None: self._old_tower_height_record = self._make_tower_height_record( new_contents ) - if self.sync_tower_height(new_contents, self._old_tower_height_record): + if self.sync_tower_height_to_S(new_contents, self._old_tower_height_record): self._old_tower_height_record = self._make_tower_height_record( new_contents ) - input("press enter to continue\n") + while True: # 接受输入的循环 + key = input("press Y to synchronize TA to S\n") + if key == "y" or key == "Y": + break + print("key invalid.") class CordinationObject: @@ -148,23 +180,24 @@ class CordinationObject: if line.strip() == "": continue sep = line.split(",") - J_or_Z=sep[0][0] - pile=re.findall(r'[JZ]0*([0-9]+)',sep[0])[0] - JZ_pile=J_or_Z+pile + J_or_Z = sep[0][0] + pile = re.findall(r"[JZ]0*([0-9]+)", sep[0])[0] + JZ_pile = J_or_Z + pile # pile = sep[0][1:len(sep[0])] altitude = sep[1] mileage = sep[2] content.append((JZ_pile, altitude, mileage)) index = len(content) - 1 - cord_dic[pile] = index#字典是不带J或者Z的 + cord_dic[pile] = index # 字典是不带J或者Z的 self._content = content self._cord_dic = cord_dic - def get_altitude(self,pile): - cord_dic=self._cord_dic + def get_altitude(self, pile): + cord_dic = self._cord_dic if pile in cord_dic: - index=cord_dic[pile] + index = cord_dic[pile] return self._content[index][1] return None + def content(self): return self._content diff --git a/main.py b/main.py index 0bcb2f7..ae87449 100644 --- a/main.py +++ b/main.py @@ -9,7 +9,7 @@ if __name__ == "__main__": r"d:\工程\灵州-青山\排位\68-72\S068.DAT", r"d:\工程\灵州-青山\排位\72-169\S072.DAT", ] - ta_object = TaFileObject(r"d:\工程\灵州-青山\排位\道亨\现场排位\现场排位.TA", SFile) + ta_object = TaFileObject(r"d:\工程\灵州-青山\排位\道亨\最终排位\最终排位.TA", SFile) ta_object.start() # generate_ta_from_csv.generate_ta(r"d:\工程\灵州-青山\排位\道亨\最终排位\最终排位.TA",SFile,r'd:\工程\灵州-青山\排位\道亨\成果表.csv') print("Finished.")