def remove(self, name): with open(self.path, 'w') as fp: for line in fileiter(self.path, strip=True): if line == name: continue else: fp.write('{}\n'.format(line))
def remove(self, name): with open(self.path, 'w') as wfile: for line in fileiter(self.path, strip=True): if line == name: continue else: wfile.write('{}\n'.format(line))
def setUp(self): p = '../data/hop.txt' with open(p, 'r') as rfile: # lines=filetolist(fp) # lines = fileiter(fp) hops = [eval(li) for li in fileiter(rfile)] # hops = [eval(line) for line in fp if line.strip() and not line.strip().startswith('#')] self.gen = parse_hops(hops)
def add(self, name): with open(self.path, 'r') as fp: exists = next((line for line in fileiter(fp, strip=True) if line == name), None) if not exists: with open(self.path, 'a') as fp: fp.write('{}\n'.format(name))
def add(self, name): with open(self.path, 'r') as rfile: exists = next( (line for line in fileiter(rfile, strip=True) if line == name), None) if not exists: with open(self.path, 'a') as wfile: wfile.write('{}\n'.format(name))
def _open_file(self, p): if not os.path.isfile(p): self.debug('{} does not exist'.format(p)) return if not p.endswith('.py'): self.debug('{} is not a valid pyscript file'.format(p)) return self._path = p valve_re = re.compile(r'''(?P<action>(open|close)+)\((name *= *)*['"]{1}(?P<name>[\w\d])+['"]{1}\,*[\w\s]*\)''') sleep_re = re.compile(r'''(?P<action>(sleep\())+((?P<value>(\d+.*\d*)))\)''') info_re = re.compile(r'''(info\()+['"]{1}(?P<value>[\w\s\d'"]*)['"]{1}\)''') actions = [] with open(p, 'r') as rfile: _docstring_started = False has__docstring = False ds = [] for li in fileiter(rfile): if not has__docstring and (li.startswith('"""') or li.startswith("'''")): if _docstring_started: has__docstring = True _docstring_started = False else: _docstring_started = True continue if _docstring_started: ds.append(li) continue sli = li.strip() m = valve_re.match(sli) if m: action = m.group('action') name = m.group('name') actions.append(ValveAction(name=action, value=name)) continue m = sleep_re.match(sli) if m: actions.append(SleepAction(value=float(m.group('value')))) continue m = info_re.match(sli) if m: actions.append(InfoAction(value=m.group('value'))) continue if li.startswith('def main'): continue actions.append(BaseAction(value=sli)) self._docstring = '\n'.join(ds) self.actions = actions
def load_hops(self, p, *args, **kw): """ load the hops definition from a file :param p: path. absolute or relative to this scripts root :return: hops :rtype: list of tuples """ if not os.path.isfile(p): p = os.path.join(self.root, p) if os.path.isfile(p): with open(p, 'r') as rfile: head, ext = os.path.splitext(p) if ext in ('.yaml', '.yml'): hops = yaml.load(rfile) elif ext in ('.txt', ): def hop_factory(l): pairs, counts, settle = eval(l) # isos, dets = zip(*(p.split(':') for p in pairs.split(','))) # items = (p.split(':') for p in pairs.split(',')) items = [] for p in pairs.split(','): args = p.split(':') defl = args[2] if len(args) == 3 else None items.append((args[0], args[1], defl)) # n = len(isos) cc = [{ 'isotope': i, 'detector': d, 'active': True, 'deflection': de, 'is_baseline': False, 'protect': False } for i, d, de in items] h = { 'counts': counts, 'settle': settle, 'cup_configuration': cc, 'positioning': { 'detector': cc[0]['detector'], 'isotope': cc[0]['isotope'] } } return h hops = [hop_factory(li) for li in fileiter(rfile)] return hops else: self.warning_dialog('No such file {}'.format(p))
def _load(self, p): with open(p, 'r') as fp: hops = [eval(l) for l in fileiter(fp)] self.hop_sequence = hs = HopSequence() for i, (hopstr, cnt, settle) in enumerate(hops): h = Hop(name=str(i + 1), counts=cnt, settle=settle, detectors=self.detectors) h.parse_hopstr(hopstr) hs.hops.append(h) hs.label_hops() self.selected = hs.hops[0] with open(p, 'r') as fp: self.text = fp.read()
def update_gitignore(self, *args): p = os.path.join(self.path, '.gitignore') # mode = 'a' if os.path.isfile(p) else 'w' args = list(args) if os.path.isfile(p): with open(p, 'r') as rfile: for line in fileiter(rfile, strip=True): for i, ai in enumerate(args): if line == ai: args.pop(i) if args: with open(p, 'a') as wfile: for ai in args: wfile.write('{}\n'.format(ai)) self._add_to_repo(p, msg='updated .gitignore')
def _load(self, p): self.hop_sequence = hs = HopSequence() if p.endswith('.txt'): self.use_yaml = False with open(p, 'r') as rfile: hops = [eval(l) for l in fileiter(rfile)] for i, (hopstr, cnt, settle) in enumerate(hops): h = Hop(name=str(i + 1), counts=cnt, settle=settle, detectors=self.detectors) h.parse_hopstr(hopstr) hs.hops.append(h) hs.label_hops() self.selected = hs.hops[0] with open(p, 'r') as rfile: self.text = rfile.read() else: self.use_yaml = True with open(p, 'r') as rfile: self.text = rfile.read() rfile.seek(0) try: for i, hop in enumerate(yaml.load(rfile)): h = Hop(name=str(i + 1), counts=hop.get('counts', 0), settle=hop.get('settle', 0), detectors=self.detectors) for p in hop.get('cup_configurations'): pos = Position(detector=p.get('detector', ''), isotope=p.get('isotope', ''), active=p.get('active', True), is_baseline=p.get( 'is_baseline', False), protect=p.get('protect', False), deflection=p.get('deflection', '')) h.positions.append(pos) hs.hops.append(h) hs.label_hops() return True except yaml.YAMLError: pass
def _load(self, p): with open(p, 'r') as rfile: hops = [eval(l) for l in fileiter(rfile)] self.hop_sequence = hs = HopSequence() for i, (hopstr, cnt, settle) in enumerate(hops): h = Hop(name=str(i + 1), counts=cnt, settle=settle, detectors=self.detectors) h.parse_hopstr(hopstr) hs.hops.append(h) hs.label_hops() self.selected = hs.hops[0] with open(p, 'r') as rfile: self.text = rfile.read()
def load_hops(self, p, *args, **kw): """ load the hops definition from a file :param p: path. absolute or relative to this scripts root :return: hops :rtype: list of tuples """ if not os.path.isfile(p): p = os.path.join(self.root, p) if os.path.isfile(p): with open(p, 'r') as rfile: hops = [eval(li) for li in fileiter(rfile)] return hops else: self.warning_dialog('No such file {}'.format(p))
def activated(self): p = os.path.join(paths.spectrometer_scans_dir, 'scan-005.txt') g = self.graph with open(p, 'r') as rfile: fi = fileiter(rfile, strip=True) fi.next().split(',') plot = g.new_plot(padding=[60, 5, 5, 50]) g.set_y_title('Intensity (fA)') data = [line.split(',') for line in fi] data = array(data, dtype=float).T xs = data[0] for ys in data[1:]: g.new_series(x=xs, y=ys) plot.value_scale = 'log'
def _load(self, p): self.hop_sequence = hs = HopSequence() if p.endswith('.txt'): self.use_yaml = False with open(p, 'r') as rfile: hops = [eval(l) for l in fileiter(rfile)] for i, (hopstr, cnt, settle) in enumerate(hops): h = Hop(name=str(i + 1), counts=cnt, settle=settle, detectors=self.detectors) h.parse_hopstr(hopstr) hs.hops.append(h) hs.label_hops() self.selected = hs.hops[0] with open(p, 'r') as rfile: self.text = rfile.read() else: self.use_yaml = True with open(p, 'r') as rfile: self.text = rfile.read() rfile.seek(0) try: for i, hop in enumerate(yaml.load(rfile)): h = Hop(name=str(i + 1), counts=hop.get('counts', 0), settle=hop.get('settle', 0), detectors=self.detectors) for p in hop.get('cup_configurations'): pos = Position(detector=p.get('detector', ''), isotope=p.get('isotope', ''), active=p.get('active', True), is_baseline=p.get('is_baseline', False), protect=p.get('protect', False), deflection=p.get('deflection', '')) h.positions.append(pos) hs.hops.append(h) hs.label_hops() return True except yaml.YAMLError: pass
def load_hops(self, p, *args, **kw): """ load the hops definition from a file :param p: path. absolute or relative to this scripts root :return: hops :rtype: list of tuples """ if not os.path.isfile(p): p = os.path.join(self.root, p) if os.path.isfile(p): with open(p, 'r') as rfile: head, ext = os.path.splitext(p) if ext in ('.yaml', '.yml'): hops = yaml.load(rfile) elif ext in ('.txt',): def hop_factory(l): pairs, counts, settle = eval(l) # isos, dets = zip(*(p.split(':') for p in pairs.split(','))) # items = (p.split(':') for p in pairs.split(',')) items = [] for p in pairs.split(','): args = p.split(':') defl = args[2] if len(args) == 3 else None items.append((args[0], args[1], defl)) # n = len(isos) cc = [{'isotope': i, 'detector': d, 'active': True, 'deflection': de, 'is_baseline': False, 'protect': False} for i, d, de in items] h = {'counts': counts, 'settle': settle, 'cup_configuration': cc, 'positioning': {'detector': cc[0]['detector'], 'isotope': cc[0]['isotope']}} return h hops = [hop_factory(li) for li in fileiter(rfile)] return hops else: self.warning_dialog('No such file {}'.format(p))
def names(self): with open(self.path, 'r') as fp: return list(fileiter(fp, strip=True))
def names(self): with open(self.path, 'r') as rfile: return list(fileiter(rfile, strip=True))