def find_previous(self):
sfiles = os.path.join(self.output_dir, cfg.TRAIN.SNAPSHOT_PREFIX + '_iter_*.ckpt.meta')
sfiles = glob.glob(sfiles)
sfiles.sort(key=os.path.getmtime)
# Get the snapshot name in TensorFlow
redfiles = []
for stepsize in cfg.TRAIN.STEPSIZE:
redfiles.append(os.path.join(self.output_dir,
cfg.TRAIN.SNAPSHOT_PREFIX + '_iter_{:d}.ckpt.meta'.format(stepsize+1)))
sfiles = [ss.replace('.meta', '') for ss in sfiles if ss not in redfiles]
nfiles = os.path.join(self.output_dir, cfg.TRAIN.SNAPSHOT_PREFIX + '_iter_*.pkl')
nfiles = glob.glob(nfiles)
nfiles.sort(key=os.path.getmtime)
redfiles = [redfile.replace('.ckpt.meta', '.pkl') for redfile in redfiles]
nfiles = [nn for nn in nfiles if nn not in redfiles]
lsf = len(sfiles)
assert len(nfiles) == lsf
return lsf, nfiles, sfiles
python类glob()的实例源码
def clean_project_files(path_or_glob, logger) :
"""
Resolve file name references and ensure they are properly deleted
"""
if "*" in path_or_glob :
files_to_clean = glob.glob(path_or_glob)
else :
files_to_clean = [os.path.expanduser(path_or_glob)]
for file_to_clean in files_to_clean :
if not os.path.exists(file_to_clean) :
continue
if os.path.isdir(file_to_clean) :
logger.info("Removing directory {}".format(file_to_clean))
shutil.rmtree(file_to_clean)
else :
logger.info("Removing file {}".format(file_to_clean))
os.remove(file_to_clean)
def get_data(datadir):
#datadir = args.data
# assume each image is 512x256 split to left and right
imgs = glob.glob(os.path.join(datadir, '*.jpg'))
data_X = np.zeros((len(imgs),3,img_cols,img_rows))
data_Y = np.zeros((len(imgs),3,img_cols,img_rows))
i = 0
for file in imgs:
img = cv2.imread(file,cv2.IMREAD_COLOR)
img = cv2.resize(img, (img_cols*2, img_rows))
#print('{} {},{}'.format(i,np.shape(img)[0],np.shape(img)[1]))
img = np.swapaxes(img,0,2)
X, Y = split_input(img)
data_X[i,:,:,:] = X
data_Y[i,:,:,:] = Y
i = i+1
return data_X, data_Y
def find_data_files(source, target, patterns):
"""
Locates the specified data-files and returns the matches
in a data_files compatible format.
source is the root of the source data tree.
Use '' or '.' for current directory.
target is the root of the target data tree.
Use '' or '.' for the distribution directory.
patterns is a sequence of glob-patterns for the
files you want to copy.
"""
if glob.has_magic(source) or glob.has_magic(target):
raise ValueError("Magic not allowed in src, target")
ret = {}
for pattern in patterns:
pattern = os.path.join(source, pattern)
for filename in glob.glob(pattern):
if os.path.isfile(filename):
targetpath = os.path.join(
target, os.path.relpath(filename, source)
)
path = os.path.dirname(targetpath)
ret.setdefault(path, []).append(filename)
return sorted(ret.items())
def load_all(self, config):
"""
Load all existing data.
:param config: Configuration object.
:type config: ``dict``
"""
self.buckets = {}
for path in glob.glob(os.path.join(
config[helper.DATA_ROOT], '%s_buckets-*.pickle' % self.NAME)):
with open(path, 'rb') as inp:
try:
for key, value in pickle.load(inp).items():
if key in self.buckets:
self.buckets[key]['bins'].update(value['bins'])
else:
self.buckets[key] = value
except:
logging.warning('could not load related_%s data', self.NAME)
def execute_recreate(self):
repairedfiles=[]
recreatedfiles=[]
if self.len_verified_actions>0:
for f,retcode in self.verifiedfiles_repairable+self.verifiedfiles_err:
yield 1
pars = glob.glob(glob.escape(f)+'*.par2')
for p in pars:
os.remove(p)
recreatedfiles.append([ f , self.runpar([self.par_cmd,"c","-r"+self.percentage,"-n"+self.nr_parfiles,f]) ])
self.recreate = sorted(recreatedfiles)
self.recreate_err = sorted([f for f,err in recreatedfiles if err !=0])
self.fixes = sorted([f for f,err in repairedfiles if err ==0])
self.fixes_err = sorted([f for f,err in repairedfiles if err !=0])
self.len_all_err = self.len_all_err + len(self.recreate_err) + len(self.fixes_err)
return
pos_tagging_data.py 文件源码
项目:Deep-Learning-with-Keras
作者: PacktPublishing
项目源码
文件源码
阅读 22
收藏 0
点赞 0
评论 0
def stream_reuters_documents(reuters_dir):
""" Iterate over documents of the Reuters dataset.
The Reuters archive will automatically be downloaded and uncompressed if
the `data_path` directory does not exist.
Documents are represented as dictionaries with 'body' (str),
'title' (str), 'topics' (list(str)) keys.
"""
parser = ReutersParser()
for filename in glob(os.path.join(reuters_dir, "*.sgm")):
for doc in parser.parse(open(filename, 'rb')):
yield doc
##################### main ######################
def _admin(self, filename="user.admin.json"):
"""
Expect admin user file; otherwise, search for first system user.
Update access_key, secret_key
"""
filepath = "{}/{}".format(self.pathname, filename)
if os.path.exists(filepath):
user = json.loads(open(filepath).read())
else:
user = None
for user_file in glob.glob("{}/user.*".format(self.pathname)):
user = json.loads(open(user_file).read())
if 'system' in user and user['system'] == "true":
break
user = None
if not user:
# No system user
log.error("No system user for radosgw found")
return
self.credentials['access_key'] = user['keys'][0]['access_key']
self.credentials['secret_key'] = user['keys'][0]['secret_key']
self.credentials['user_id'] = user['keys'][0]['user']
self.credentials['success'] = True
def _parse(line):
"""
Return globbed files constrained by optional slices or regexes.
"""
if " " in line:
parts = re.split('\s+', line)
files = sorted(glob.glob(parts[0]))
for optional in parts[1:]:
filter_type, value = optional.split('=')
if filter_type == "re":
regex = re.compile(value)
files = [m.group(0) for l in files for m in [regex.search(l)] if m]
elif filter_type == "slice":
# pylint: disable=eval-used
files = eval("files{}".format(value))
else:
log.warning("keyword {} unsupported".format(filter_type))
else:
files = glob.glob(line)
return files
def master_minion(self):
"""
Verify that the master minion setting is a minion
"""
data = None
node = None
local = salt.client.LocalClient()
for node in self.data.keys():
data = local.cmd(self.data[node]['master_minion'],
'pillar.get', ['master_minion'], expr_form="glob")
break
if data:
self.passed['master_minion'] = "valid"
else:
if node:
msg = "Could not find minion {}.".format(self.data[node]['master_minion'])
msg += " Check /srv/pillar/ceph/master_minion.sls"
else:
msg = "Missing pillar data"
self.errors['master_minion'] = [msg]
def _parse(self, line):
"""
Return globbed files constrained by optional slices or regexes.
"""
if " " in line:
parts = re.split(r'\s+', line)
files = sorted(glob.glob(parts[0]))
for keyvalue in parts[1:]:
key, value = keyvalue.split('=')
if key == "re":
regex = re.compile(value)
files = [match.group(0) for _file in files
for match in [regex.search(_file)] if match]
elif key == "slice":
# pylint: disable=eval-used
files = eval("files{}".format(value))
else:
log.warning("keyword {} unsupported".format(key))
else:
files = glob.glob(line)
return files
def pairs():
"""
Return an array of devices and paths
"""
_paths = [pathname for pathname in glob.glob("/var/lib/ceph/osd/*")]
_pairs = []
with open('/proc/mounts') as mounts:
for line in mounts:
_partition, path = line.split()[:2]
if path in _paths:
match = re.match(r'^(.+)\d+$', _partition)
device = match.group(1)
if 'nvme' in device:
device = device[:-1]
_pairs.append([device, path])
return _pairs
def do_egg_install(self):
easy_install = self.distribution.get_command_class('easy_install')
cmd = easy_install(
self.distribution, args="x", root=self.root, record=self.record,
)
cmd.ensure_finalized() # finalize before bdist_egg munges install cmd
cmd.always_copy_from = '.' # make sure local-dir eggs get installed
# pick up setup-dir .egg files only: no .egg-info
cmd.package_index.scan(glob.glob('*.egg'))
self.run_command('bdist_egg')
args = [self.distribution.get_command_obj('bdist_egg').egg_output]
if setuptools.bootstrap_install_from:
# Bootstrap self-installation of setuptools
args.insert(0, setuptools.bootstrap_install_from)
cmd.args = args
cmd.run()
setuptools.bootstrap_install_from = None
# XXX Python 3.1 doesn't see _nc if this is inside the class
def run(self):
self.run_command("egg_info")
from glob import glob
for pattern in self.match:
pattern = self.distribution.get_name()+'*'+pattern
files = glob(os.path.join(self.dist_dir,pattern))
files = [(os.path.getmtime(f),f) for f in files]
files.sort()
files.reverse()
log.info("%d file(s) matching %s", len(files), pattern)
files = files[self.keep:]
for (t,f) in files:
log.info("Deleting %s", f)
if not self.dry_run:
os.unlink(f)
def path_hash(path):
"""Generate a hash checksum of all files matching 'path'. Standard
wildcards like '*' and '?' are supported, see documentation for the 'glob'
module for more information.
:return: dict: A { filename: hash } dictionary for all matched files.
Empty if none found.
"""
return {
filename: file_hash(filename)
for filename in glob.iglob(path)
}
def restart_on_change(restart_map, stopstart=False, restart_functions=None):
"""Restart services based on configuration files changing
This function is used a decorator, for example::
@restart_on_change({
'/etc/ceph/ceph.conf': [ 'cinder-api', 'cinder-volume' ]
'/etc/apache/sites-enabled/*': [ 'apache2' ]
})
def config_changed():
pass # your code here
In this example, the cinder-api and cinder-volume services
would be restarted if /etc/ceph/ceph.conf is changed by the
ceph_client_changed function. The apache2 service would be
restarted if any file matching the pattern got changed, created
or removed. Standard wildcards are supported, see documentation
for the 'glob' module for more information.
@param restart_map: {path_file_name: [service_name, ...]
@param stopstart: DEFAULT false; whether to stop, start OR restart
@param restart_functions: nonstandard functions to use to restart services
{svc: func, ...}
@returns result from decorated function
"""
def wrap(f):
@functools.wraps(f)
def wrapped_f(*args, **kwargs):
return restart_on_change_helper(
(lambda: f(*args, **kwargs)), restart_map, stopstart,
restart_functions)
return wrapped_f
return wrap
def is_phy_iface(interface):
"""Returns True if interface is not virtual, otherwise False."""
if interface:
sys_net = '/sys/class/net'
if os.path.isdir(sys_net):
for iface in glob.glob(os.path.join(sys_net, '*')):
if '/virtual/' in os.path.realpath(iface):
continue
if interface == os.path.basename(iface):
return True
return False
def juju_version():
"""Full version string (eg. '1.23.3.1-trusty-amd64')"""
# Per https://bugs.launchpad.net/juju-core/+bug/1455368/comments/1
jujud = glob.glob('/var/lib/juju/tools/machine-*/jujud')[0]
return subprocess.check_output([jujud, 'version'],
universal_newlines=True).strip()
def get_bridges(vnic_dir='/sys/devices/virtual/net'):
"""Return a list of bridges on the system."""
b_regex = "%s/*/bridge" % vnic_dir
return [x.replace(vnic_dir, '').split('/')[1] for x in glob.glob(b_regex)]
def ls(filename):
return sorted(glob(filename))