def read_from_file(self, data_file, append_flag = False, filter = None, lagramge_flag = False): from distutils.text_file import TextFile; from string import split, atof; f = TextFile(filename = data_file); read_vars_flag = not append_flag or (self.length == 0); line = f.readline(); if read_vars_flag: if lagramge_flag: self.vars = line[-1].split(" "); else: self.vars = line.split(" "); for v in self.vars: self.data[v] = []; line_index = -1; for line in f.readlines(): line_index = line_index + 1; if (filter <> None) and (line_index not in filter): continue; if lagramge_flag: vals = line[:-1].split(" "); else: vals = line.split(" "); for i in range(len(vals)): if vals[i] <> "?": self.data[self.vars[i]].append(atof(vals[i])); else: self.data[self.vars[i]].append(vals[i]); self.length = self.length + 1; f.close();
def build_manifest(cls, manifest, path=None): infile = StringIO() infile.write('\n'.join(manifest)) infile.seek(0) template = TextFile(file=infile, strip_comments=1, skip_blanks=1, join_lines=1, lstrip_ws=1, rstrip_ws=1, collapse_join=1) lines = template.readlines() filelist = FileList() try: if path is not None and not path == os.getcwd(): oldpath = os.getcwd() os.chdir(path) else: oldpath = None for line in lines: filelist.process_template_line(line) finally: if oldpath is not None: os.chdir(oldpath) return set(filelist.files)
def _get_included_files(package_masks): old_threshold = set_threshold(ERROR) file_list = FileList() file_list.extend(_iter_package_modules(package_masks)) manifest = TextFile('MANIFEST.in', strip_comments=1, skip_blanks=1, join_lines=1, lstrip_ws=1, rstrip_ws=1, collapse_join=1) for line in manifest.readlines(): file_list.process_template_line(line) set_threshold(old_threshold) return file_list.files
def read_dependencies(requirements=missing): if requirements is None: return [] if requirements is missing: requirements = 'requirements.txt' if not os.path.isfile(requirements): return [] text = TextFile(requirements, lstrip_ws=True) try: return text.readlines() finally: text.close()
def scan_file(self, f, regexs): # Use TextFile since it has a nice function to print a warning with the # offending line's number. text_file = TextFile(f) # Thanks to http://stackoverflow.com/a/17502838/6124862 contents = '\n'.join(text_file.readlines()) for r in regexs: regex = re.compile(r, flags=re.MULTILINE | re.DOTALL) for match in regex.finditer(contents): lineno = contents.count('\n', 0, match.start()) text_file.warn("Found '%s' match" % r, lineno) text_file.close()
def get_urlimage_dictionary(self, path): '''method to retrieve the image/url mappings from the imageurl.txt file ''' urlimage_dict = dict() image_fh = TextFile(filename=os.path.join(path, self.URLMAPPING), lstrip_ws=True) for line in image_fh.readlines(): if '=' in line: filename, sep, url = line.partition('=') filename = os.path.join(path, filename) urlimage_dict[filename] = url return urlimage_dict
def simulate(self, data_sets, init_state = None, window = 0, output = sys.stdout): from distutils.text_file import TextFile; from string import split, atof; if window == 0: self.fit_params(data_sets, init_state = init_state, n_tf_restarts = 0, n_fs_restarts = 0, output = output); return; first_flag = True; for data_set in data_sets: for i in range(data_set.length - window): ptime = [data_set.time[i + window]]; if i == 0: ptime = ptime + [data_set.time[0]]; if first_flag: ptime = ptime + [-1]; first_flag = False; ds_window = data_set.subset(range(i, i + window + 1), file_suffix = "w"); ds_window.write_to_file(); f = open("model.out", "w"); self.simulate([ds_window], output = f); f.close(); f = TextFile(filename = "model.out"); simulation_flag = False; for line in f.readlines(): if line[-4:] == "_sim": simulation_flag = True; if (-1 in ptime): print >> output, line; continue; if line[:5] == "SSE, ": simulation_flag = False; continue; if simulation_flag: fields = line.split(" "); if atof(fields[0]) in ptime: print >> output, line; f.close();
def read_requirements_txt(path): """ read only entries from requirements.txt in the form:: pkg pkg==0.1.0 pkg<=0.1.0 pkg>=0.1.0 this parser reads any non-comment, non -* line """ requirements = [] _path = os.path.join(SETUPPY_PATH, path) try: tf = TextFile(_path) requirements_txt = (x.lstrip() for x in tf.readlines()) for line in requirements_txt: if not line.startswith('-'): requirements.append(line) finally: tf and tf.close() return requirements
def read_requirements_txt(path): """ read only entries from requirements.txt in the form:: pkg pkg==0.1.0 pkg<=0.1.0 pkg>=0.1.0 this parser reads any non-comment, non -* line """ requirements = [] _path = os.path.join(SETUPPY_PATH, path) try: tf = TextFile(_path) requirements_txt = (x.lstrip() for x in tf.readlines()) for line in requirements_txt: if not line.startswith("-"): requirements.append(line) finally: tf and tf.close() return requirements
def _parse_template_file(filename, path=None): template = TextFile(filename, strip_comments=1, skip_blanks=1, join_lines=1, lstrip_ws=1, rstrip_ws=1, collapse_join=1) lines = template.readlines() filelist = FileList() try: if path is not None and not path == os.getcwd(): oldpath = os.getcwd() os.chdir(path) else: oldpath = None for line in lines: filelist.process_template_line(line) finally: if oldpath is not None: os.chdir(oldpath) return filelist.files