def __init__(self, i_in, i_out, rate): i_in = list(iterable(i_in)) i_out = list(iterable(i_out)) for j,i in enumerate(i_in): if isinstance(i, str): i_in[j] = ion(i) for j,i in enumerate(i_out): if isinstance(i, str): i_out[j] = ion(i) self.i_in = i_in self.i_out = i_out self.rate = rate
def load_rate_data(self, filename = ( '/home/alex/kepler/fission/netsu_nfis_Roberts2010rates', '/home/alex/kepler/fission/netsu_sfis_Roberts2010rates', ), mode = 'reaclib2', ): self.setup_logger(silent = False) rates = [] filename = utils.iterable(filename) assert mode in ('reaclib', 'reaclib2') for fn in filename: with open(fn, 'rt') as f: self.logger.info(f'loading {fn} ...') if mode == 'reaclib': chapter = int(f.readline()) f.readline() f.readline() else: chapter = None while True: try: rate = ReacLibRecord(f, chapter) if rate is not None: rates.append(rate) except Exception as e: if isinstance(e, EmptyRecord): break else: raise self.rates = rates self.close_logger(r'loaded {} reactions in '.format(len(rates)))
def load_rate_data( self, filename=( '/home/alex/kepler/fission/netsu_nfis_Roberts2010rates', '/home/alex/kepler/fission/netsu_sfis_Roberts2010rates', ), mode='reaclib2', ): self.setup_logger(silent=False) rates = [] filename = utils.iterable(filename) assert mode in ('reaclib', 'reaclib2') for fn in filename: with open(fn, 'rt') as f: self.logger.info(f'loading {fn} ...') if mode == 'reaclib': chapter = int(f.readline()) f.readline() f.readline() else: chapter = None while True: try: rate = ReacLibRecord(f, chapter) if rate is not None: rates.append(rate) except Exception as e: if isinstance(e, EmptyRecord): break else: raise self.rates = rates self.close_logger(r'loaded {} reactions in '.format(len(rates)))
def compress_file(filename, compressor = 'xz', flags = None, nice = 19, ): """ Compress file in background """ _flags = {'xz' : ('-e'), 'gzip' : ('-9'), 'bzip2': ('--best'), } assert compressor in _flags, 'unknown compressor' filename = os.path.expanduser(os.path.expandvars(filename)) assert os.path.exists(filename), 'file does not exists' filepath = os.path.dirname(filename) assert os.access(filepath, os.W_OK), 'directory is not writable' if flags is None: flags = _flags[compressor] args = [shutil.which(compressor)] args += list(iterable(args)) args += [filename] p = psutil.Popen(args, shell = False, cwd = filepath, stdin = subprocess.DEVNULL, stdout = subprocess.DEVNULL, stderr = subprocess.DEVNULL, start_new_session = True) if (nice is not None) and (nice > 0): p.nice(nice)
def insert(self, *items): if len(items) == 1: items = items[0] if iterable(items): self.connection.executemany( self.INSERT_INTO.format(self.TABLE_NAME, self.ENTRY_TYPE.ITEMS_PLACEHOLDER), (i.tolist() for i in items)) elif isinstance(items, self.ENTRY_TYPE): self.connection.execute( self.INSERT_INTO.format(self.TABLE_NAME, self.ENTRY_TYPE.ITEMS_PLACEHOLDER), items.tolist()) return self
def find_local(filename, ignore = ('.git',), extensions = ('.py', '.f', '.f90', '.inc', '')): for dirpath, dirs, files in os.walk(source): for f in files: for e in extensions: if e != '' and not filename.endswith(e): fn = filename + e else: fn = filename if f == fn: return os.path.join(dirpath, f) for d in iterable(ignore): try: dirs.remove(d) except: pass
def find_local(filename, ignore=('.git', ), extensions=('.py', '.f', '.f90', '.inc', '')): for dirpath, dirs, files in os.walk(source): for f in files: for e in extensions: if e != '' and not filename.endswith(e): fn = filename + e else: fn = filename if f == fn: return os.path.join(dirpath, f) for d in iterable(ignore): try: dirs.remove(d) except: pass
def save(self, entity): if self._data is None: return if not entity.exists: entity._persist() # first save it, we need it's key! if self._data is None: #no data to be saved go along about our day return True assert iterable( self._data ) #must be iterable, many remote models to one local entity, not the other way around for remote in self._data: setattr( remote, self.foreign_key, entity.key ) #set the remote attribute to {table_name}_id = this entities key remote.save()
def addItem(menu, key, id=wx.ID_ANY, kind=wx.ITEM_NORMAL, checked=False, index=None, table=None, handlerobj=None, binder=None): accelerators = app.config['accelerators'] if app.config.has_section( 'accelerators') else {} if id == wx.ID_NONE: return elif id == wx.ID_SEPARATOR: if index is None: return menu.AppendSeparator() else: return menu.InsertSeparator(index) elif utils.iterable(id): return addSubMenu(menu, key, id, index, table, handlerobj, binder) if binder is None: binder = win if handlerobj is None: handlerobj = binder handler = getkey(handlerobj, key) if not handler and callable(key): handler = key if not handler and callable(handlerobj) and not isinstance( handlerobj, type): handler = handlerobj trArgs = None if isinstance(key, tuple): key, *trArgs = key if not isinstance(key, str): key = key.__name__ label = translate(key) if trArgs: label = label.format(*trArgs) help = translate(key + 'Help', None) if help and trArgs: help = help.format(*trArgs) shortcut = accelerators.get(key, None) if index is None: item = menu.Append(id, label, help, kind) else: item = menu.Insert(index, id, label, help, kind) if kind != wx.ITEM_NORMAL and checked: item.Check(checked) if handler and binder: binder.Bind(wx.EVT_MENU, handler, id=item.GetId()) if shortcut and table is not None: entry = wx.AcceleratorEntry(cmd=item.GetId(), item=item) if entry.FromString(shortcut): item.SetItemLabel(item.GetItemLabel() + '\t' + entry.ToString()) table.append(entry) return item
def _find_in_db(self, id=None): def quote(v): return "`" + v + "`" fields = list(self.fields.keys()) if self.key_name not in fields: fields.insert(0, self.key_name) fields = map(quote, fields) query = "SELECT {} from {} ".format( ", ".join(fields), self.table_name) #TODO don't select *...... db = self.db cursor = db.cursor(dictionary=True) if iterable(id): ids = list(filter(lambda x: bool(x), id)) logging.debug("SEARCHING for ids: %s", ids) query += "WHERE {} in ({})".format(self.key_name, ", ".join(ids)) logging.debug("QUERY: %s", query) cursor.execute(query) result = cursor.fetchall() db.commit() logging.debug("FOUND: %s", result) return map(lambda x: self.build(**x), result) elif id is None: # no id no problem cursor.execute(query) result = cursor.fetchall() db.commit() logging.debug("FOUND: %s", result) return list(map(lambda x: self.build(**x), result)) else: # find single instance with the specified id query += "WHERE {} = {}".format(self.key_name, id) logging.debug("QUERY: %s", query) cursor.execute(query) result = cursor.fetchone() db.commit() logging.debug("FOUND: %s", result) if cursor.rowcount <= 0: return None return self.build(**result)
def compress_file( filename, compressor='xz', flags=None, nice=19, ): """ Compress file in background """ _flags = { 'xz': ('-e'), 'gzip': ('-9'), 'bzip2': ('--best'), } assert compressor in _flags, 'unknown compressor' filename = os.path.expanduser(os.path.expandvars(filename)) assert os.path.exists(filename), 'file does not exists' filepath = os.path.dirname(filename) assert os.access(filepath, os.W_OK), 'directory is not writable' if flags is None: flags = _flags[compressor] args = [shutil.which(compressor)] args += list(iterable(args)) args += [filename] p = psutil.Popen(args, shell=False, cwd=filepath, stdin=subprocess.DEVNULL, stdout=subprocess.DEVNULL, stderr=subprocess.DEVNULL, start_new_session=True) if (nice is not None) and (nice > 0): p.nice(nice)
def __str__(self): if isinstance(self.permission, str): return self._get_grant(self.permission) elif iterable(self.permission): return '\n'.join((self._get_grant(p) for p in self.permission))
def add_atoms(self, atoms): for atom in iterable(atoms): self.add({atom: "some %s" % atom})
def __str__(self): if isinstance(self.permission, str): return self._get_grant(self.permission) elif iterable(self.permission): return '\n'.join((self._get_grant(p) for p in self.permission))