Unnamed: 0
int64
0
10k
function
stringlengths
79
138k
label
stringclasses
20 values
info
stringlengths
42
261
1,200
def parse_char(txt): if not txt: raise PreprocError("attempted to parse a null char") if txt[0] != '\\': return ord(txt) c = txt[1] if c == 'x': if len(txt) == 4 and txt[3] in string.hexdigits: return int(txt[2:], 16) return int(txt[2:], 16) elif c.isdigit(): if c == '0' and len(txt)==2: return 0 for i in 3, 2, 1: if len(txt) > i and txt[1:1+i].isdigit(): return (1+i, int(txt[1:1+i], 8)) else: try: return chr_esc[c] except __HOLE__: raise PreprocError("could not parse char literal '%s'" % txt)
KeyError
dataset/ETHPy150Open appcelerator-archive/poc-nodejs-desktop/Resources/nodejs/builds/linux/node/lib/node/wafadmin/Tools/preproc.py/parse_char
1,201
def tokenize(s): """convert a string into a list of tokens (shlex.split does not apply to c/c++/d)""" ret = [] for match in re_clexer.finditer(s): m = match.group for name in tok_types: v = m(name) if v: if name == IDENT: try: v = g_optrans[v]; name = OP except __HOLE__: # c++ specific if v.lower() == "true": v = 1 name = NUM elif v.lower() == "false": v = 0 name = NUM elif name == NUM: if m('oct'): v = int(v, 8) elif m('hex'): v = int(m('hex'), 16) elif m('n0'): v = m('n0') else: v = m('char') if v: v = parse_char(v) else: v = m('n2') or m('n4') elif name == OP: if v == '%:': v = '#' elif v == '%:%:': v = '##' elif name == STR: # remove the quotes around the string v = v[1:-1] ret.append((name, v)) break return ret
KeyError
dataset/ETHPy150Open appcelerator-archive/poc-nodejs-desktop/Resources/nodejs/builds/linux/node/lib/node/wafadmin/Tools/preproc.py/tokenize
1,202
def addlines(self, node): self.currentnode_stack.append(node.parent) filepath = node.abspath(self.env) self.count_files += 1 if self.count_files > recursion_limit: raise PreprocError("recursion limit exceeded") pc = self.parse_cache debug('preproc: reading file %r', filepath) try: lns = pc[filepath] except __HOLE__: pass else: self.lines = lns + self.lines return try: lines = filter_comments(filepath) lines.append((POPFILE, '')) pc[filepath] = lines # cache the lines filtered self.lines = lines + self.lines except IOError: raise PreprocError("could not read the file %s" % filepath) except Exception: if Logs.verbose > 0: error("parsing %s failed" % filepath) traceback.print_exc()
KeyError
dataset/ETHPy150Open appcelerator-archive/poc-nodejs-desktop/Resources/nodejs/builds/linux/node/lib/node/wafadmin/Tools/preproc.py/c_parser.addlines
1,203
def start(self, node, env): debug('preproc: scanning %s (in %s)', node.name, node.parent.name) self.env = env variant = node.variant(env) bld = node.__class__.bld try: self.parse_cache = bld.parse_cache except __HOLE__: bld.parse_cache = {} self.parse_cache = bld.parse_cache self.addlines(node) if env['DEFLINES']: self.lines = [('define', x) for x in env['DEFLINES']] + self.lines while self.lines: (kind, line) = self.lines.pop(0) if kind == POPFILE: self.currentnode_stack.pop() continue try: self.process_line(kind, line) except Exception, e: if Logs.verbose: debug('preproc: line parsing failed (%s): %s %s', e, line, Utils.ex_stack())
AttributeError
dataset/ETHPy150Open appcelerator-archive/poc-nodejs-desktop/Resources/nodejs/builds/linux/node/lib/node/wafadmin/Tools/preproc.py/c_parser.start
1,204
def resolve_name(name): ret = None parts = name.split('.') cursor = len(parts) module_name = parts[:cursor] last_exc = None while cursor > 0: try: ret = __import__('.'.join(module_name)) break except ImportError as exc: last_exc = exc if cursor == 0: raise cursor -= 1 module_name = parts[:cursor] for part in parts[1:]: try: ret = getattr(ret, part) except __HOLE__: if last_exc is not None: raise last_exc raise ImportError(name) if ret is None: if last_exc is not None: raise last_exc raise ImportError(name) return ret
AttributeError
dataset/ETHPy150Open circus-tent/circus/circus/tests/generic.py/resolve_name
1,205
def setup(self): try: global S3Storage from depot.io.awss3 import S3Storage except __HOLE__: raise SkipTest('Boto not installed') env = os.environ access_key_id = env.get('AWS_ACCESS_KEY_ID') secret_access_key = env.get('AWS_SECRET_ACCESS_KEY') if access_key_id is None or secret_access_key is None: raise SkipTest('Amazon S3 credentials not available') PID = os.getpid() NODE = str(uuid.uuid1()).rsplit('-', 1)[-1] # Travis runs multiple tests concurrently self.default_bucket_name = 'filedepot-%s' % (access_key_id.lower(), ) self.cred = (access_key_id, secret_access_key) self.fs = S3Storage(access_key_id, secret_access_key, 'filedepot-testfs-%s-%s-%s' % (access_key_id.lower(), NODE, PID))
ImportError
dataset/ETHPy150Open amol-/depot/tests/test_awss3_storage.py/TestS3FileStorage.setup
1,206
def wait_network_port_idle(port): """Wait network port idle. return after 5 minutes or network port idle. TCP connect close TIME_WAIT max 2MSL(2minutes) :param port: network port """ for i in range(0, 30): # 5 minutes try: sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM) sock.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1) sock.bind(('0.0.0.0', port)) sock.close() # close effect immediately if not data send return except __HOLE__: if i == 0: time.sleep(1) # sometime just need short wait else: log.info('closing port [%d], Please wait a moment.', port) time.sleep(10) # import gc # from werkzeug.serving import BaseWSGIServer # for o in gc.get_referrers(BaseWSGIServer): # if o.__class__ is BaseWSGIServer: # o.server_close()
OSError
dataset/ETHPy150Open remyzane/flask-http-api/http_api/assist/coding.py/wait_network_port_idle
1,207
def test_forbidden(webapp): try: urlopen("%s/test_forbidden" % webapp.server.http.base) except __HOLE__ as e: assert e.code == 403 assert e.msg == "Forbidden" else: assert False
HTTPError
dataset/ETHPy150Open circuits/circuits/tests/web/test_exceptions.py/test_forbidden
1,208
def test_notfound(webapp): try: urlopen("%s/test_notfound" % webapp.server.http.base) except __HOLE__ as e: assert e.code == 404 assert e.msg == "Not Found" else: assert False
HTTPError
dataset/ETHPy150Open circuits/circuits/tests/web/test_exceptions.py/test_notfound
1,209
def test_contenttype(webapp): try: urlopen("%s/test_contenttype" % webapp.server.http.base) except __HOLE__ as e: assert e.code == 500 assert e.msg == "Internal Server Error" assert "text/html" in e.headers.get("Content-Type") else: assert False
HTTPError
dataset/ETHPy150Open circuits/circuits/tests/web/test_exceptions.py/test_contenttype
1,210
def test_contenttype_json(webapp): try: urlopen("%s/test_contenttype_json" % webapp.server.http.base) except __HOLE__ as e: assert "json" in e.headers.get("Content-Type") result = json.loads(e.read().decode("utf-8")) assert result["code"] == 500 assert result["name"] == "Internal Server Error" assert result["description"] == "" assert "raise Exception" in result["traceback"] else: assert False
HTTPError
dataset/ETHPy150Open circuits/circuits/tests/web/test_exceptions.py/test_contenttype_json
1,211
def test_contenttype_json_no_debug(webapp): try: f = urlopen("%s/test_contenttype_json_no_debug" % webapp.server.http.base) except __HOLE__ as e: assert "json" in e.headers.get("Content-Type") result = json.loads(e.read().decode("utf-8")) assert result["code"] == 500 assert result["name"] == "Internal Server Error" assert result["description"] == "" assert "traceback" not in result else: assert False
HTTPError
dataset/ETHPy150Open circuits/circuits/tests/web/test_exceptions.py/test_contenttype_json_no_debug
1,212
def _cross_val(data, est, cv, n_jobs): """Helper to compute cross validation.""" try: from sklearn.model_selection import cross_val_score except __HOLE__: # XXX support sklearn < 0.18 from sklearn.cross_validation import cross_val_score return np.mean(cross_val_score(est, data, cv=cv, n_jobs=n_jobs, scoring=_gaussian_loglik_scorer))
ImportError
dataset/ETHPy150Open mne-tools/mne-python/mne/cov.py/_cross_val
1,213
def _auto_low_rank_model(data, mode, n_jobs, method_params, cv, stop_early=True, verbose=None): """compute latent variable models.""" method_params = cp.deepcopy(method_params) iter_n_components = method_params.pop('iter_n_components') if iter_n_components is None: iter_n_components = np.arange(5, data.shape[1], 5) from sklearn.decomposition import PCA, FactorAnalysis if mode == 'factor_analysis': est = FactorAnalysis elif mode == 'pca': est = PCA else: raise ValueError('Come on, this is not a low rank estimator: %s' % mode) est = est(**method_params) est.n_components = 1 scores = np.empty_like(iter_n_components, dtype=np.float64) scores.fill(np.nan) # make sure we don't empty the thing if it's a generator max_n = max(list(cp.deepcopy(iter_n_components))) if max_n > data.shape[1]: warn('You are trying to estimate %i components on matrix ' 'with %i features.' % (max_n, data.shape[1])) for ii, n in enumerate(iter_n_components): est.n_components = n try: # this may fail depending on rank and split score = _cross_val(data=data, est=est, cv=cv, n_jobs=n_jobs) except __HOLE__: score = np.inf if np.isinf(score) or score > 0: logger.info('... infinite values encountered. stopping estimation') break logger.info('... rank: %i - loglik: %0.3f' % (n, score)) if score != -np.inf: scores[ii] = score if (ii >= 3 and np.all(np.diff(scores[ii - 3:ii]) < 0.) and stop_early is True): # early stop search when loglik has been going down 3 times logger.info('early stopping parameter search.') break # happens if rank is too low right form the beginning if np.isnan(scores).all(): raise RuntimeError('Oh no! Could not estimate covariance because all ' 'scores were NaN. Please contact the MNE-Python ' 'developers.') i_score = np.nanargmax(scores) best = est.n_components = iter_n_components[i_score] logger.info('... best model at rank = %i' % best) runtime_info = {'ranks': np.array(iter_n_components), 'scores': scores, 'best': best, 'cv': cv} return est, runtime_info
ValueError
dataset/ETHPy150Open mne-tools/mne-python/mne/cov.py/_auto_low_rank_model
1,214
def _regularized_covariance(data, reg=None): """Compute a regularized covariance from data using sklearn. Parameters ---------- data : ndarray, shape (n_channels, n_times) Data for covariance estimation. reg : float | str | None (default None) If not None, allow regularization for covariance estimation if float, shrinkage covariance is used (0 <= shrinkage <= 1). if str, optimal shrinkage using Ledoit-Wolf Shrinkage ('ledoit_wolf') or Oracle Approximating Shrinkage ('oas'). Returns ------- cov : ndarray, shape (n_channels, n_channels) The covariance matrix. """ if reg is None: # compute empirical covariance cov = np.cov(data) else: no_sklearn_err = ('the scikit-learn package is missing and ' 'required for covariance regularization.') # use sklearn covariance estimators if isinstance(reg, float): if (reg < 0) or (reg > 1): raise ValueError('0 <= shrinkage <= 1 for ' 'covariance regularization.') try: import sklearn sklearn_version = LooseVersion(sklearn.__version__) from sklearn.covariance import ShrunkCovariance except __HOLE__: raise Exception(no_sklearn_err) if sklearn_version < '0.12': skl_cov = ShrunkCovariance(shrinkage=reg, store_precision=False) else: # init sklearn.covariance.ShrunkCovariance estimator skl_cov = ShrunkCovariance(shrinkage=reg, store_precision=False, assume_centered=True) elif isinstance(reg, string_types): if reg == 'ledoit_wolf': try: from sklearn.covariance import LedoitWolf except ImportError: raise Exception(no_sklearn_err) # init sklearn.covariance.LedoitWolf estimator skl_cov = LedoitWolf(store_precision=False, assume_centered=True) elif reg == 'oas': try: from sklearn.covariance import OAS except ImportError: raise Exception(no_sklearn_err) # init sklearn.covariance.OAS estimator skl_cov = OAS(store_precision=False, assume_centered=True) else: raise ValueError("regularization parameter should be " "'ledoit_wolf' or 'oas'") else: raise ValueError("regularization parameter should be " "of type str or int (got %s)." % type(reg)) # compute regularized covariance using sklearn cov = skl_cov.fit(data.T).covariance_ return cov
ImportError
dataset/ETHPy150Open mne-tools/mne-python/mne/cov.py/_regularized_covariance
1,215
def parse_arg(s): try: return json.loads(s) except __HOLE__: return s
ValueError
dataset/ETHPy150Open aerospike/aerospike-client-python/examples/client/scan_info.py/parse_arg
1,216
def main(argv=None): if not argv: argv = sys.argv parser = E.OptionParser( version="%prog version: $Id: data2histogram.py 2782 2009-09-10 11:40:29Z andreas $") parser.add_option("-r", "--range", dest="range", type="string", help="range to calculate histogram for.") parser.add_option("-b", "--bin-size", dest="bin_size", type="string", help="bin size.") parser.add_option("-i", "--titles", dest="titles", action="store_true", help="use supplied column titles.") parser.add_option("--no-null", dest="nonull", action="store_true", help="do not output null values") parser.add_option("--no-titles", dest="titles", action="store_false", help="no column titles given.") parser.add_option("-c", "--columns", dest="columns", type="string", help="columns to take for calculating histograms.") parser.add_option("--min-data", dest="min_data", type="int", help="minimum amount of data required, if less data, then the histogram will be empty [default=%default].") parser.add_option("--min-value", dest="min_value", type="float", help="minimum value for histogram.") parser.add_option("--max-value", dest="max_value", type="float", help="maximum value for histogram.") parser.add_option("--no-empty-bins", dest="no_empty_bins", action="store_true", help="do not display empty bins.") parser.add_option("--with-empty-bins", dest="no_empty_bins", action="store_false", help="display empty bins.") parser.add_option("--normalize", dest="normalize", action="store_true", help="normalize histogram.") parser.add_option("--cumulative", dest="cumulative", action="store_true", help="calculate cumulative histogram.") parser.add_option("--reverse-cumulative", dest="reverse_cumulative", action="store_true", help="calculate reverse cumulative histogram.") parser.add_option("--header-names", dest="headers", type="string", help="use the following headers.") parser.add_option("--ignore-out-of-range", dest="ignore_out_of_range", action="store_true", help="ignore values that are out of range (as opposed to truncating them to range border.") parser.add_option("--missing-value", dest="missing_value", type="string", help="entry for missing values [%default].") parser.add_option("--use-dynamic-bins", dest="dynamic_bins", action="store_true", help="each value constitutes its own bin.") parser.add_option("--on-the-fly", dest="on_the_fly", action="store_true", help="on the fly computation of histograms. Requires setting of min-value, max-value and bin_size.") parser.set_defaults( bin_size=None, range=None, titles=True, columns="all", append=(), no_empty_bins=True, min_value=None, max_value=None, normalize=False, cumulative=False, reverse_cumulative=False, nonull=None, ignore_out_of_range=False, min_data=1, headers=None, missing_value="na", dynamic_bins=False, on_the_fly=False, bin_format="%.2f", value_format="%6.4f", ) (options, args) = E.Start(parser) if options.columns != "all": options.columns = map(lambda x: int(x) - 1, options.columns.split(",")) if options.range: options.min_value, options.max_value = map( float, options.range.split(",")) if options.headers: options.headers = options.headers.split(",") if options.on_the_fly: if options.min_value is None or options.max_value is None or options.bin_size is None: raise "please supply columns, min-value, max-value and bin-size for on-the-fly computation." # try to glean titles from table: if options.titles: while 1: line = sys.stdin.readline() if not line: break if line[0] == "#": continue data = line[:-1].split("\t") break if options.columns == "all": options.titles = data options.columns = range(len(data)) else: options.titles = [data[x] for x in options.columns] bins = numpy.arange( options.min_value, options.max_value, float(options.bin_size)) hh = Histogram.fillHistograms( sys.stdin, options.columns, [bins for x in range(len(options.columns))]) n = len(hh) titles = ['bin'] if options.headers: titles.append(options.headers[x]) elif options.titles: titles.append(options.titles[x]) else: for x in options.columns: titles.append("col%i" % (x + 1)) if len(titles) > 1: options.stdout.write("\t".join(titles) + "\n") for x in range(len(bins)): v = [] v.append(options.bin_format % bins[x]) for c in range(n): v.append(options.value_format % hh[c][x]) options.stdout.write("\t".join(v) + "\n") else: # in-situ computation of histograms # retrieve data first = True vals = [] # parse data, convert to floats for l in options.stdin: if l[0] == "#": continue data = string.split(l[:-1], "\t") if first: first = False ncols = len(data) if options.columns == "all": options.columns = range(ncols) vals = [[] for x in options.columns] if options.titles: try: options.titles = [data[x] for x in options.columns] except IndexError: raise IndexError, "not all columns %s found in data %s" % ( str(options.columns), str(data)) continue for x in range(len(options.columns)): try: v = string.atof(data[options.columns[x]]) except IndexError: print "# IndexError in line:", l[:-1] continue except __HOLE__: continue vals[x].append(v) lines = None hists = [] titles = [] if not vals: if options.loglevel >= 1: options.stdlog.write("# no data\n") E.Stop() sys.exit(0) for x in range(len(options.columns)): if options.loglevel >= 1: options.stdlog.write( "# column=%i, num_values=%i\n" % (options.columns[x], len(vals[x]))) if len(vals[x]) < options.min_data: continue h = Histogram.Calculate(vals[x], no_empty_bins=options.no_empty_bins, increment=options.bin_size, min_value=options.min_value, max_value=options.max_value, dynamic_bins=options.dynamic_bins, ignore_out_of_range=options.ignore_out_of_range) if options.normalize: h = Histogram.Normalize(h) if options.cumulative: h = Histogram.Cumulate(h) if options.reverse_cumulative: h = Histogram.Cumulate(h, direction=0) hists.append(h) for m in options.append: if m == "normalize": hists.append(Histogram.Normalize(h)) if options.headers: titles.append(options.headers[x]) elif options.titles: titles.append(options.titles[x]) else: titles.append("col%i" % options.columns[x]) if titles: options.stdout.write("bin\t" + "\t".join(titles) + "\n") if len(hists) == 1: Histogram.Print(hists[0], nonull=options.nonull) else: combined_histogram = Histogram.Combine( hists, missing_value=options.missing_value) Histogram.Print(combined_histogram, nonull=options.nonull) E.Stop()
ValueError
dataset/ETHPy150Open CGATOxford/cgat/scripts/data2histogram.py/main
1,217
@signalcommand @custom_transaction def handle(self, *fixture_labels, **options): """ Main method of a Django command """ from django.db.models import get_apps from django.core import serializers from django.conf import settings self.style = no_style() verbosity = int(options.get('verbosity', 1)) show_traceback = options.get('traceback', False) # Keep a count of the installed objects and fixtures fixture_count = 0 object_count = 0 objects_per_fixture = [] models = set() humanize = lambda dirname: dirname and "'%s'" % dirname or 'absolute path' # Get a cursor (even though we don't need one yet). This has # the side effect of initializing the test database (if # it isn't already initialized). cursor = connection.cursor() app_fixtures = [os.path.join(os.path.dirname(app.__file__), 'fixtures') for app in get_apps()] for fixture_label in fixture_labels: parts = fixture_label.split('.') if len(parts) == 1: fixture_name = fixture_label formats = serializers.get_public_serializer_formats() else: fixture_name, format = '.'.join(parts[:-1]), parts[-1] if format in serializers.get_public_serializer_formats(): formats = [format] else: formats = [] if formats: if verbosity > 1: print("Loading '%s' fixtures..." % fixture_name) else: sys.stderr.write(self.style.ERROR("Problem installing fixture '%s': %s is not a known serialization format." % (fixture_name, format))) transaction.rollback() return if os.path.isabs(fixture_name): fixture_dirs = [fixture_name] else: fixture_dirs = app_fixtures + list(settings.FIXTURE_DIRS) + [''] for fixture_dir in fixture_dirs: if verbosity > 1: print("Checking %s for fixtures..." % humanize(fixture_dir)) label_found = False for format in formats: if verbosity > 1: print("Trying %s for %s fixture '%s'..." % (humanize(fixture_dir), format, fixture_name)) try: full_path = os.path.join(fixture_dir, '.'.join([fixture_name, format])) fixture = open(full_path, 'r') if label_found: fixture.close() print(self.style.ERROR("Multiple fixtures named '%s' in %s. Aborting." % (fixture_name, humanize(fixture_dir)))) transaction.rollback() return else: fixture_count += 1 objects_per_fixture.append(0) if verbosity > 0: print("Installing %s fixture '%s' from %s." % (format, fixture_name, humanize(fixture_dir))) try: objects_to_keep = {} objects = serializers.deserialize(format, fixture) for obj in objects: object_count += 1 objects_per_fixture[-1] += 1 class_ = obj.object.__class__ if class_ not in objects_to_keep: objects_to_keep[class_] = set() objects_to_keep[class_].add(obj.object) models.add(class_) obj.save() if options.get('remove'): self.remove_objects_not_in(objects_to_keep, verbosity) label_found = True except (__HOLE__, KeyboardInterrupt): raise except Exception: import traceback fixture.close() transaction.rollback() if show_traceback: traceback.print_exc() else: sys.stderr.write(self.style.ERROR("Problem installing fixture '%s': %s\n" % (full_path, traceback.format_exc()))) return fixture.close() except: if verbosity > 1: print("No %s fixture '%s' in %s." % (format, fixture_name, humanize(fixture_dir))) # If any of the fixtures we loaded contain 0 objects, assume that an # error was encountered during fixture loading. if 0 in objects_per_fixture: sys.stderr.write( self.style.ERROR("No fixture data found for '%s'. (File format may be invalid.)" % fixture_name)) transaction.rollback() return # If we found even one object in a fixture, we need to reset the # database sequences. if object_count > 0: sequence_sql = connection.ops.sequence_reset_sql(self.style, models) if sequence_sql: if verbosity > 1: print("Resetting sequences") for line in sequence_sql: cursor.execute(line) transaction.commit() if object_count == 0: if verbosity > 1: print("No fixtures found.") else: if verbosity > 0: print("Installed %d object(s) from %d fixture(s)" % (object_count, fixture_count)) # Close the DB connection. This is required as a workaround for an # edge case in MySQL: if the same connection is used to # create tables, load data, and query, the query can return # incorrect results. See Django #7572, MySQL #37735. connection.close()
SystemExit
dataset/ETHPy150Open django-extensions/django-extensions/django_extensions/management/commands/syncdata.py/Command.handle
1,218
def test_index_set_conversion(self): try: import AppKit except __HOLE__: from nose import SkipTest raise SkipTest from tinymail.ui_delegates import array_from_index_set data = [ [], [0], [3], [0, 1, 2], [7, 8, 9, 22], ] for value in data: index_set = AppKit.NSMutableIndexSet.indexSet() for i in value: index_set.addIndex_(i) converted = array_from_index_set(index_set) self.assertEqual(value, converted)
ImportError
dataset/ETHPy150Open mgax/tinymail/src/tinymail/tests/test_indexsets.py/IndexSetConversionTest.test_index_set_conversion
1,219
def filenameToModule(fn): """ Given a filename, do whatever possible to return a module object matching that file. If the file in question is a module in Python path, properly import and return that module. Otherwise, load the source manually. @param fn: A filename. @return: A module object. @raise ValueError: If C{fn} does not exist. """ if not os.path.exists(fn): raise ValueError("%r doesn't exist" % (fn,)) try: ret = reflect.namedAny(reflect.filenameToModuleName(fn)) except (__HOLE__, AttributeError): # Couldn't find module. The file 'fn' is not in PYTHONPATH return _importFromFile(fn) # ensure that the loaded module matches the file retFile = os.path.splitext(ret.__file__)[0] + '.py' # not all platforms (e.g. win32) have os.path.samefile same = getattr(os.path, 'samefile', samefile) if os.path.isfile(fn) and not same(fn, retFile): del sys.modules[ret.__name__] ret = _importFromFile(fn) return ret
ValueError
dataset/ETHPy150Open nlloyd/SubliminalCollaborator/libs/twisted/trial/runner.py/filenameToModule
1,220
def name(thing): """ @param thing: an object from modules (instance of PythonModule, PythonAttribute), a TestCase subclass, or an instance of a TestCase. """ if isTestCase(thing): # TestCase subclass theName = reflect.qual(thing) else: # thing from trial, or thing from modules. # this monstrosity exists so that modules' objects do not have to # implement id(). -jml try: theName = thing.id() except __HOLE__: theName = thing.name return theName
AttributeError
dataset/ETHPy150Open nlloyd/SubliminalCollaborator/libs/twisted/trial/runner.py/name
1,221
def isTestCase(obj): """ @return: C{True} if C{obj} is a class that contains test cases, C{False} otherwise. Used to find all the tests in a module. """ try: return issubclass(obj, pyunit.TestCase) except __HOLE__: return False
TypeError
dataset/ETHPy150Open nlloyd/SubliminalCollaborator/libs/twisted/trial/runner.py/isTestCase
1,222
def _getDebugger(self): dbg = pdb.Pdb() try: import readline except ImportError: print "readline module not available" sys.exc_clear() for path in ('.pdbrc', 'pdbrc'): if os.path.exists(path): try: rcFile = file(path, 'r') except __HOLE__: sys.exc_clear() else: dbg.rcLines.extend(rcFile.readlines()) return dbg
IOError
dataset/ETHPy150Open nlloyd/SubliminalCollaborator/libs/twisted/trial/runner.py/TrialRunner._getDebugger
1,223
def parse_feature(obj): """ Given a python object attemp to a GeoJSON-like Feature from it """ # object implementing geo_interface if hasattr(obj, '__geo_interface__'): gi = obj.__geo_interface__ if gi['type'] in geom_types: return wrap_geom(gi) elif gi['type'] == 'Feature': return gi # wkt try: shape = wkt.loads(obj) return wrap_geom(shape.__geo_interface__) except (ReadingError, TypeError, AttributeError): pass # wkb try: shape = wkb.loads(obj) return wrap_geom(shape.__geo_interface__) except (ReadingError, TypeError): pass # geojson-like python mapping try: if obj['type'] in geom_types: return wrap_geom(obj) elif obj['type'] == 'Feature': return obj except (AssertionError, __HOLE__): pass raise ValueError("Can't parse %s as a geojson Feature object" % obj)
TypeError
dataset/ETHPy150Open perrygeo/python-rasterstats/src/rasterstats/io.py/parse_feature
1,224
def read_features(obj, layer=0): features_iter = None if isinstance(obj, string_types): try: # test it as fiona data source with fiona.open(obj, 'r', layer=layer) as src: assert len(src) > 0 def fiona_generator(obj): with fiona.open(obj, 'r', layer=layer) as src: for feature in src: yield feature features_iter = fiona_generator(obj) except (AssertionError, TypeError, IOError, __HOLE__): try: mapping = json.loads(obj) if 'type' in mapping and mapping['type'] == 'FeatureCollection': features_iter = mapping['features'] elif mapping['type'] in geom_types + ['Feature']: features_iter = [parse_feature(mapping)] except ValueError: # Single feature-like string features_iter = [parse_feature(obj)] elif isinstance(obj, Mapping): if 'type' in obj and obj['type'] == 'FeatureCollection': features_iter = obj['features'] else: features_iter = [parse_feature(obj)] elif isinstance(obj, bytes): # Single binary object, probably a wkb features_iter = [parse_feature(obj)] elif hasattr(obj, '__geo_interface__'): mapping = obj.__geo_interface__ if mapping['type'] == 'FeatureCollection': features_iter = mapping['features'] else: features_iter = [parse_feature(mapping)] elif isinstance(obj, Iterable): # Iterable of feature-like objects features_iter = (parse_feature(x) for x in obj) if not features_iter: raise ValueError("Object is not a recognized source of Features") return features_iter
OSError
dataset/ETHPy150Open perrygeo/python-rasterstats/src/rasterstats/io.py/read_features
1,225
def _modify_execution_status_in_database( self, execution, new_status): try: execution_id = execution['id'] except __HOLE__: execution_id = execution.id storage_manager._get_instance().update_execution_status( execution_id, new_status, error='') updated_execution = self.client.executions.get( execution_id=execution_id) self.assertEqual(new_status, updated_execution['status'])
TypeError
dataset/ETHPy150Open cloudify-cosmo/cloudify-manager/rest-service/manager_rest/test/test_executions.py/ExecutionsTestCase._modify_execution_status_in_database
1,226
def get_by_natural_key(self, app_label, model): try: ct = self._cache[self.db][(app_label, model)] except __HOLE__: ct = self.get(app_label=app_label, model=model) self._add_to_cache(self.db, ct) return ct
KeyError
dataset/ETHPy150Open django/django/django/contrib/contenttypes/models.py/ContentTypeManager.get_by_natural_key
1,227
def get_for_model(self, model, for_concrete_model=True): """ Returns the ContentType object for a given model, creating the ContentType if necessary. Lookups are cached so that subsequent lookups for the same model don't hit the database. """ opts = self._get_opts(model, for_concrete_model) try: return self._get_from_cache(opts) except __HOLE__: pass # The ContentType entry was not found in the cache, therefore we # proceed to load or create it. try: try: # We start with get() and not get_or_create() in order to use # the db_for_read (see #20401). ct = self.get(app_label=opts.app_label, model=opts.model_name) except self.model.DoesNotExist: # Not found in the database; we proceed to create it. This time we # use get_or_create to take care of any race conditions. ct, created = self.get_or_create( app_label=opts.app_label, model=opts.model_name, ) except (OperationalError, ProgrammingError, IntegrityError): # It's possible to migrate a single app before contenttypes, # as it's not a required initial dependency (it's contrib!) # Have a nice error for this. raise RuntimeError( "Error creating new content types. Please make sure contenttypes " "is migrated before trying to migrate apps individually." ) self._add_to_cache(self.db, ct) return ct
KeyError
dataset/ETHPy150Open django/django/django/contrib/contenttypes/models.py/ContentTypeManager.get_for_model
1,228
def get_for_models(self, *models, **kwargs): """ Given *models, returns a dictionary mapping {model: content_type}. """ for_concrete_models = kwargs.pop('for_concrete_models', True) # Final results results = {} # models that aren't already in the cache needed_app_labels = set() needed_models = set() needed_opts = set() for model in models: opts = self._get_opts(model, for_concrete_models) try: ct = self._get_from_cache(opts) except __HOLE__: needed_app_labels.add(opts.app_label) needed_models.add(opts.model_name) needed_opts.add(opts) else: results[model] = ct if needed_opts: cts = self.filter( app_label__in=needed_app_labels, model__in=needed_models ) for ct in cts: model = ct.model_class() if model._meta in needed_opts: results[model] = ct needed_opts.remove(model._meta) self._add_to_cache(self.db, ct) for opts in needed_opts: # These weren't in the cache, or the DB, create them. ct = self.create( app_label=opts.app_label, model=opts.model_name, ) self._add_to_cache(self.db, ct) results[ct.model_class()] = ct return results
KeyError
dataset/ETHPy150Open django/django/django/contrib/contenttypes/models.py/ContentTypeManager.get_for_models
1,229
def get_for_id(self, id): """ Lookup a ContentType by ID. Uses the same shared cache as get_for_model (though ContentTypes are obviously not created on-the-fly by get_by_id). """ try: ct = self._cache[self.db][id] except __HOLE__: # This could raise a DoesNotExist; that's correct behavior and will # make sure that only correct ctypes get stored in the cache dict. ct = self.get(pk=id) self._add_to_cache(self.db, ct) return ct
KeyError
dataset/ETHPy150Open django/django/django/contrib/contenttypes/models.py/ContentTypeManager.get_for_id
1,230
def _log_context(self, context): template = Template(self._get_template('context')) keys = [] if isinstance(context, list): for c in context: keys += self._get_context_keys(c) else: keys += self._get_context_keys(context) keys = set(keys) # Skip some keys for discardkey in DISCARD_CONTEXT_KEYS: keys.discard(discardkey) for key in keys: val = force_unicode(context[key]) con = { 'key': key, 'value': val, } con = Context(safe_dict(con)) try: #Avoid memory addy's which will change. if not re.search("0x\w+", val): self.log.info(template.render(con)) except __HOLE__, e: pass
UnicodeDecodeError
dataset/ETHPy150Open ericholscher/django-test-utils/test_utils/testmaker/processors/base.py/Processer._log_context
1,231
def format_citation_details(self, entry, citation_template=None): """Return string.""" if citation_template is None: citation_template = self.citation_template try: type_template = citation_template[entry.entry_type] #:note: recall entry_type was stored as lowercase except __HOLE__: #no template exists for this entry_type -> use default type_template = citation_template['default_type'] shared_logger.warning("Unknown entry type: "+entry.entry_type+". Using default format.") #:note: entry will return None instead of KeyError result = type_template % entry return result
KeyError
dataset/ETHPy150Open dschwilk/bibstuff/bibstuff/bibstyles/shared.py/EntryFormatter.format_citation_details
1,232
def filterbots(fh, ip_ua_re, bots_ua, bots_ips, parser=None, format=None, ip_ua_fields=None, reverse=False, debug=False, **kwargs): """Filter bots from a log stream using ip/useragent blacklists""" bots_ua_dict, bots_ua_prefix_dict, bots_ua_suffix_dict, bots_ua_re = \ parse_bots_ua(bots_ua) bots_ips = dict.fromkeys([l.strip() for l in bots_ips \ if not l.startswith("#")]) is_bot_ua_func = partial(is_bot_ua, bots_ua_dict=bots_ua_dict, bots_ua_prefix_dict=bots_ua_prefix_dict, bots_ua_suffix_dict=bots_ua_suffix_dict, bots_ua_re=bots_ua_re) _is_bot_func=None if not parser: # Regular expression-based matching ua_ip_re = re.compile(ip_ua_re) def _is_bot_func(line): match = ua_ip_re.match(line) if not match: raise ValueError("No match for line: %s" % line) logging.debug("Regular expression matched line: %s", match) matchgroups = match.groupdict() is_bot = False ua = matchgroups.get('ua', None) is_bot = is_bot_ua_func(ua) if not is_bot and matchgroups.get('ip', None) in bots_ips: # IP Is blacklisted is_bot = True return is_bot else: # Custom parser specified, use field-based matching parser = eval(parser, vars(logtools.parsers), {})() try: fields_map = dict([tuple(k.split(':')) for k in ip_ua_fields.split(',')]) except __HOLE__: raise ValueError("Invalid format for --field parameter. Use --help for usage instructions.") is_indices = reduce(and_, (k.isdigit() for k in fields_map.values()), True) if is_indices: # Field index based matching def _is_bot_func(line): parsed_line = parser(line) is_bot = False if 'ua' in fields_map and parsed_line: is_bot = is_bot_ua_func(parsed_line.by_index(fields_map['ua'])) if not is_bot and 'ip' in fields_map: is_bot = parsed_line.by_index(fields_map['ip']) in bots_ips return is_bot else: # Named field based matching def _is_bot_func(line): parsed_line = parser(line) is_bot = False if 'ua' in fields_map and parsed_line: is_bot = is_bot_ua_func(parsed_line[fields_map['ua']]) if not is_bot and 'ip' in fields_map: is_bot = parsed_line[fields_map['ip']] in bots_ips return is_bot num_lines=0 num_filtered=0 num_nomatch=0 for line in imap(lambda x: x.strip(), fh): try: is_bot = _is_bot_func(line) except (KeyError, ValueError): # Parsing error logging.warn("No match for line: %s", line) num_nomatch +=1 continue if is_bot ^ reverse: logging.debug("Filtering line: %s", line) num_filtered+=1 continue num_lines+=1 yield line logging.info("Number of lines after bot filtering: %s", num_lines) logging.info("Number of lines (bots) filtered: %s", num_filtered) if num_nomatch: logging.info("Number of lines could not match on: %s", num_nomatch) return
ValueError
dataset/ETHPy150Open adamhadani/logtools/logtools/_filterbots.py/filterbots
1,233
def __init__(self, path, exclusive=False): self.path = path try: self.fd = open(path, 'r+') except IOError: self.fd = open(path, 'r') try: if exclusive: fcntl.lockf(self.fd, fcntl.LOCK_EX) else: fcntl.lockf(self.fd, fcntl.LOCK_SH) # Python 3.2 raises IOError, Python3.3+ raises OSError except (__HOLE__, OSError): if exclusive: raise self.WriteLockFailed(self.path) else: raise self.ReadLockFailed(self.path) self.is_exclusive = exclusive
IOError
dataset/ETHPy150Open jborg/attic/attic/helpers.py/UpgradableLock.__init__
1,234
def upgrade(self): try: fcntl.lockf(self.fd, fcntl.LOCK_EX) # Python 3.2 raises IOError, Python3.3+ raises OSError except (__HOLE__, OSError): raise self.WriteLockFailed(self.path) self.is_exclusive = True
IOError
dataset/ETHPy150Open jborg/attic/attic/helpers.py/UpgradableLock.upgrade
1,235
def prune_within(archives, within): multiplier = {'H': 1, 'd': 24, 'w': 24*7, 'm': 24*31, 'y': 24*365} try: hours = int(within[:-1]) * multiplier[within[-1]] except (__HOLE__, ValueError): # I don't like how this displays the original exception too: raise argparse.ArgumentTypeError('Unable to parse --within option: "%s"' % within) if hours <= 0: raise argparse.ArgumentTypeError('Number specified using --within option must be positive') target = datetime.now(timezone.utc) - timedelta(seconds=hours*60*60) return [a for a in archives if a.ts > target]
KeyError
dataset/ETHPy150Open jborg/attic/attic/helpers.py/prune_within
1,236
def is_cachedir(path): """Determines whether the specified path is a cache directory (and therefore should potentially be excluded from the backup) according to the CACHEDIR.TAG protocol (http://www.brynosaurus.com/cachedir/spec.html). """ tag_contents = b'Signature: 8a477f597d28d172789f06886806bc55' tag_path = os.path.join(path, 'CACHEDIR.TAG') try: if os.path.exists(tag_path): with open(tag_path, 'rb') as tag_file: tag_data = tag_file.read(len(tag_contents)) if tag_data == tag_contents: return True except __HOLE__: pass return False
OSError
dataset/ETHPy150Open jborg/attic/attic/helpers.py/is_cachedir
1,237
def memoize(function): cache = {} def decorated_function(*args): try: return cache[args] except __HOLE__: val = function(*args) cache[args] = val return val return decorated_function
KeyError
dataset/ETHPy150Open jborg/attic/attic/helpers.py/memoize
1,238
@memoize def uid2user(uid, default=None): try: return pwd.getpwuid(uid).pw_name except __HOLE__: return default
KeyError
dataset/ETHPy150Open jborg/attic/attic/helpers.py/uid2user
1,239
@memoize def user2uid(user, default=None): try: return user and pwd.getpwnam(user).pw_uid except __HOLE__: return default
KeyError
dataset/ETHPy150Open jborg/attic/attic/helpers.py/user2uid
1,240
@memoize def gid2group(gid, default=None): try: return grp.getgrgid(gid).gr_name except __HOLE__: return default
KeyError
dataset/ETHPy150Open jborg/attic/attic/helpers.py/gid2group
1,241
@memoize def group2gid(group, default=None): try: return group and grp.getgrnam(group).gr_gid except __HOLE__: return default
KeyError
dataset/ETHPy150Open jborg/attic/attic/helpers.py/group2gid
1,242
def location_validator(archive=None): def validator(text): try: loc = Location(text) except __HOLE__: raise argparse.ArgumentTypeError('Invalid location format: "%s"' % text) if archive is True and not loc.archive: raise argparse.ArgumentTypeError('"%s": No archive specified' % text) elif archive is False and loc.archive: raise argparse.ArgumentTypeError('"%s" No archive can be specified' % text) return loc return validator
ValueError
dataset/ETHPy150Open jborg/attic/attic/helpers.py/location_validator
1,243
def yield_timed_calls(filename): try: f = open(filename) except __HOLE__, error: print >> sys.stderr, "I/O error while opening file: %s" % error return for line in f: try: labels = line.split(':')[:3] except Exception, error: print >> sys.stderr, "Unhandled Exception:", error, filename return if labels[1:] in [['LOG', 'T']]: if len(line.split(':')) != 5: continue try: call = line.split(':')[3] + ':' if call[:11] == "epoll_wait_": call = "epoll_wait_:" else: call = re.sub(r'_B:|_D:|_E:|_F:|_f:|_K:|_k:|_P:|_p:|_S:|_U:', ':', call) time = line.split(':')[4] msec = int(time.split('.')[0]) * 10 ** 6 \ + int(time.split('.')[1]) except Exception, error: print >> sys.stderr, "Unhandled Exception:", error, filename return # special handling of epoll - it should go to cache later yield call + str(msec) f.close()
IOError
dataset/ETHPy150Open columbia/libtrack/libtrack/parser/scripts/patterns.py/yield_timed_calls
1,244
def visit_Call(self, node): global game if 'id' in dir(node.func): self.stack.store() if len(node.args) > 0: self.generic_visit(node.args) args = self.stack.current() self.stack.wipe() else: args = [] # check this condition, seens strange if node.func.id not in game.bitpaks: obj = getattr(pynes.bitbag, node.func.id, None) if (obj): try: bp = obj(game) game.bitpaks[node.func.id] = bp self.stack(bp(*args)) game += bp.asm() except __HOLE__ as ex: msg = ex.message.replace('__call__', node.func.id, 1) raise(TypeError(msg)) else: raise(NameError("name '%s' is not defined" % node.func.id)) else: bp = game.bitpaks[node.func.id] self.stack(bp(*args)) game += bp.asm() else: self.generic_visit(node) attrib = getattr( self.stack.current()[0], self.stack.current()[1], None) self.stack.wipe() if callable(attrib): attrib()
TypeError
dataset/ETHPy150Open gutomaia/pyNES/pynes/composer.py/PyNesVisitor.visit_Call
1,245
def train(model, dataset, lr=1e-4, momentum=0.9, batch_size=100, n_epochs=100, valid_freq=None, patience_incr=2.0, lr_decr=0.5, epoch_waiting=10, never_stop=False): r"""Train the model with mini-batch Stochastic Gradient Descent(SGD). Parameters ---------- model : NeuralNet The model to be trained. dataset : Dataset The dataset to provide training, validation, and testing data. lr : float or double, optional The initial learning rate. Default is 1e-4. momentum : float or double, optional The coefficient of momentum term. Default is 0.9. batch_size : int, optional The number of samples in each mini-batch. Default is 100. n_epochs : int, optional The number of training epochs. Default is 100. valid_freq : None or int, optional The number of iterations between validations. If None, then it will be set to the size of training batch plus one. Default is None. patience_incr : float or double, optional lr_decr : float or double, optional epoch_waiting : float or double, optional `patience` is utilized to stop training when the model converges. It is initialized as `n_batches` * 20. After each validation process, if `current_valid_error` < `best_valid_error`, then `patience` = `current_iter` * `patience_incr`. Otherwise if there is no improvement in the last `epoch_waiting` epochs, then `lr` = `lr` * `lr_decr`. Default `patience_incr` is 2.0, `lr_decr` is 0.5, and epoch_waiting is 10. never_stop : bool, optional If True, then the training process will never stop until user interrupts, otherwise it will stop when either reaches `n_epochs` or `patience` is consumed. """ n_train = len(dataset.train_ind) n_valid = len(dataset.valid_ind) n_test = len(dataset.test_ind) n_train_batches = n_train // batch_size n_valid_batches = n_valid // batch_size n_test_batches = n_test // batch_size i = T.iscalar() # batch index alpha = T.scalar() # learning rate dummy = T.scalar() # for parameter updates l, r = i * batch_size, (i + 1) * batch_size # Comupute updates grads = T.grad(model.cost, model.parameters, consider_constant=model.consts) incs = [create_empty(p) for p in model.parameters] inc_updates = [] param_updates = [] for p, g, inc in zip(model.parameters, grads, incs): inc_updates.append((inc, momentum * inc - alpha * g)) param_updates.append((p, p + inc)) # Build functions inc_updates_func = theano.function( inputs=[i, alpha], outputs=model.cost, updates=inc_updates, givens=_bind_data(model, dataset.train, [l, r]), on_unused_input='ignore') param_updates_func = theano.function( inputs=[dummy], outputs=dummy, updates=param_updates, on_unused_input='ignore') valid_func = theano.function( inputs=[i], outputs=[model.cost, model.error], givens=_bind_data(model, dataset.valid, [l, r]), on_unused_input='ignore') test_func = theano.function( inputs=[i], outputs=[model.cost, model.error], givens=_bind_data(model, dataset.test, [l, r]), on_unused_input='ignore') # Start training best_valid_error = np.inf test_error = np.inf patience = n_train_batches * 20 last_improve_epoch = 0 if valid_freq is None: valid_freq = n_train_batches + 1 print "Start training ..." begin_time = time.clock() try: for epoch in xrange(n_epochs): for j in xrange(n_train_batches): cur_iter = epoch * n_train_batches + j # training dataset.train.prepare([j * batch_size, (j + 1) * batch_size]) batch_cost = inc_updates_func(j, lr) param_updates_func(0) print "[train] epoch {0} batch {1}/{2}, iter {3}, cost {4}".format( epoch, j + 1, n_train_batches, cur_iter, batch_cost) if (cur_iter + 1) % valid_freq == 0: # validation valid_cost, valid_error = [], [] for j in xrange(n_valid_batches): dataset.valid.prepare([j * batch_size, (j + 1) * batch_size]) cost, error = valid_func(j) valid_cost.append(cost) valid_error.append(error) valid_cost = np.mean(valid_cost) valid_error = np.mean(valid_error) print "[valid] cost {0}, error {1}".format(valid_cost, valid_error) # testing if valid_error < best_valid_error: best_valid_error = valid_error last_improve_epoch = epoch patience = max(patience, cur_iter * patience_incr) print "Update patience {0}".format(patience) test_cost, test_error = [], [] for j in xrange(n_test_batches): dataset.test.prepare([j * batch_size, (j + 1) * batch_size]) cost, error = test_func(j) test_cost.append(cost) test_error.append(error) test_cost = np.mean(test_cost) test_error = np.mean(test_error) print "[test] cost {0}, error {1}".format(test_cost, test_error) elif epoch >= last_improve_epoch + epoch_waiting: # lr decreasing lr *= lr_decr last_improve_epoch = epoch print "Update lr {0}".format(lr) # early stopping if cur_iter > patience and not never_stop: break except __HOLE__: print "Keyboard interrupt. Stop training" print "Training complete, time {0}".format(time.clock() - begin_time) print "Best validation error {0}, test error {1}".format(best_valid_error, test_error)
KeyboardInterrupt
dataset/ETHPy150Open Cysu/dlearn/dlearn/optimization/sgd.py/train
1,246
def train_dp(model, data_provider, lr=1e-4, momentum=0.9, n_epochs=100, valid_freq=None, patience_incr=2.0, lr_decr=0.1, epoch_waiting=10, never_stop=False): r"""Train the model with mini-batch Stochastic Gradient Descent(SGD). Parameters ---------- model : NeuralNet The model to be trained. dataset : Dataset The dataset to provide training, validation, and testing data. lr : float or double, optional The initial learning rate. Default is 1e-4. momentum : float or double, optional The coefficient of momentum term. Default is 0.9. batch_size : int, optional The number of samples in each mini-batch. Default is 100. n_epochs : int, optional The number of training epochs. Default is 100. valid_freq : None or int, optional The number of iterations between validations. If None, then it will be set to the size of training batch plus one. Default is None. patience_incr : float or double, optional lr_decr : float or double, optional epoch_waiting : float or double, optional `patience` is utilized to stop training when the model converges. It is initialized as `n_batches` * 20. After each validation process, if `current_valid_error` < `best_valid_error`, then `patience` = `current_iter` * `patience_incr`. Otherwise if there is no improvement in the last `epoch_waiting` epochs, then `lr` = `lr` * `lr_decr`. Default `patience_incr` is 2.0, `lr_decr` is 0.5, and epoch_waiting is 10. never_stop : bool, optional If True, then the training process will never stop until user interrupts, otherwise it will stop when either reaches `n_epochs` or `patience` is consumed. """ n_train_batches = data_provider.train.n_batches n_valid_batches = data_provider.valid.n_batches n_test_batches = data_provider.test.n_batches alpha = T.scalar() # learning rate dummy = T.scalar() # for parameter updates # Comupute updates grads = T.grad(model.cost, model.parameters, consider_constant=model.consts) incs = [create_empty(p) for p in model.parameters] inc_updates = [] param_updates = [] for p, g, inc in zip(model.parameters, grads, incs): inc_updates.append((inc, momentum * inc - alpha * g)) param_updates.append((p, p + inc)) # Build functions inc_updates_func = theano.function( inputs=[alpha], outputs=model.cost, updates=inc_updates, givens=_bind_data(model, data_provider.train), on_unused_input='ignore') param_updates_func = theano.function( inputs=[dummy], outputs=dummy, updates=param_updates, on_unused_input='ignore') valid_func = theano.function( inputs=[], outputs=[model.cost, model.error], givens=_bind_data(model, data_provider.valid), on_unused_input='ignore') test_func = theano.function( inputs=[], outputs=[model.cost, model.error], givens=_bind_data(model, data_provider.test), on_unused_input='ignore') # Start training best_valid_cost = np.inf last_improve_epoch = 0 train_costs = [0] * n_train_batches if valid_freq is None: valid_freq = n_train_batches + 1 print "Start training ..." begin_time = time.clock() try: for epoch in xrange(n_epochs): need_stop = False for j in xrange(n_train_batches): cur_iter = epoch * n_train_batches + j # training data_provider.train.prepare(j) batch_cost = inc_updates_func(lr) param_updates_func(0) train_costs[j] = batch_cost print "[train] epoch {0} batch {1}/{2}, iter {3}, cost {4}".format( epoch, j + 1, n_train_batches, cur_iter, batch_cost) if (cur_iter + 1) % valid_freq == 0: # validation valid_cost, valid_error = [], [] for j in xrange(n_valid_batches): data_provider.valid.prepare(j) cost, error = valid_func() valid_cost.append(cost) valid_error.append(error) valid_cost = np.mean(valid_cost) valid_error = np.mean(valid_error) print "[valid] cost {0}, error {1}".format(valid_cost, valid_error) # testing if valid_cost < best_valid_cost: best_valid_cost = valid_cost best_valid_error = valid_error last_improve_epoch = epoch test_cost, test_error = [], [] for j in xrange(n_test_batches): data_provider.test.prepare(j) cost, error = test_func() test_cost.append(cost) test_error.append(error) test_cost = np.mean(test_cost) test_error = np.mean(test_error) print "[test] cost {0}, error {1}".format(test_cost, test_error) elif epoch >= last_improve_epoch + epoch_waiting: # check overfitting if np.mean(train_costs) < valid_cost * 0.85: need_stop = True break # lr decreasing if lr >= 1e-5: lr *= lr_decr print "Update lr {0}".format(lr) last_improve_epoch = epoch # early stopping if not never_stop and need_stop: break except __HOLE__: print "Keyboard interrupt. Stop training" print "Training complete, time {0}".format(time.clock() - begin_time) print "Best validation cost {0}, valid error {1}".format(best_valid_cost, best_valid_error) print "Test cost {0}, test error {1}".format(test_cost, test_error)
KeyboardInterrupt
dataset/ETHPy150Open Cysu/dlearn/dlearn/optimization/sgd.py/train_dp
1,247
@staticmethod def _parse(lines): def coalesce_lines(): line_iter = iter(lines) try: buffer = '' while True: line = next(line_iter) if line.strip().endswith('\\'): # Continuation. buffer += line.strip()[:-1] else: if buffer: # Continuation join, preserve left hand ws (could be a kv separator) buffer += line.rstrip() else: # Plain old line buffer = line.strip() try: yield buffer finally: buffer = '' except __HOLE__: pass def normalize(atom): return re.sub(r'\\([:=\s])', r'\1', atom.strip()) def parse_line(line): if line and not (line.startswith('#') or line.startswith('!')): match = Properties._EXPLICIT_KV_SEP.search(line) if match: return normalize(line[:match.start()]), normalize(line[match.end():]) else: space_sep = line.find(' ') if space_sep == -1: return normalize(line), '' else: return normalize(line[:space_sep]), normalize(line[space_sep:]) props = OrderedDict() for line in coalesce_lines(): kv_pair = parse_line(line) if kv_pair: key, value = kv_pair props[key] = value return props
StopIteration
dataset/ETHPy150Open pantsbuild/pants/src/python/pants/backend/jvm/tasks/properties.py/Properties._parse
1,248
def load_caffe(model_desc, model_file): """ return a dict of params """ param_dict = {} param_processors = get_processor() with change_env('GLOG_minloglevel', '2'): import caffe caffe.set_mode_cpu() net = caffe.Net(model_desc, model_file, caffe.TEST) layer_names = net._layer_names blob_names = net.blobs.keys() for layername, layer in zip(layer_names, net.layers): try: prev_blob_name = blob_names[blob_names.index(layername)-1] prev_data_shape = net.blobs[prev_blob_name].data.shape[1:] except __HOLE__: prev_data_shape = None if layer.type in param_processors: param_dict.update(param_processors[layer.type]( layername, layer.blobs, prev_data_shape)) else: assert len(layer.blobs) == 0, len(layer.blobs) logger.info("Model loaded from caffe. Params: " + \ " ".join(sorted(param_dict.keys()))) return param_dict
ValueError
dataset/ETHPy150Open ppwwyyxx/tensorpack/tensorpack/utils/loadcaffe.py/load_caffe
1,249
def get(self, key): try: expires, value = self._cache[key] if expires == 0 or expires > time(): return pickle.loads(value) except (__HOLE__, pickle.PickleError): return None
KeyError
dataset/ETHPy150Open GoogleCloudPlatform/appengine-flask-skeleton/lib/werkzeug/contrib/cache.py/SimpleCache.get
1,250
def has(self, key): try: expires, value = self._cache[key] return expires == 0 or expires > time() except __HOLE__: return False
KeyError
dataset/ETHPy150Open GoogleCloudPlatform/appengine-flask-skeleton/lib/werkzeug/contrib/cache.py/SimpleCache.has
1,251
def import_preferred_memcache_lib(self, servers): """Returns an initialized memcache client. Used by the constructor.""" try: import pylibmc except ImportError: pass else: return pylibmc.Client(servers) try: from google.appengine.api import memcache except __HOLE__: pass else: return memcache.Client() try: import memcache except ImportError: pass else: return memcache.Client(servers) # backwards compatibility
ImportError
dataset/ETHPy150Open GoogleCloudPlatform/appengine-flask-skeleton/lib/werkzeug/contrib/cache.py/MemcachedCache.import_preferred_memcache_lib
1,252
def __init__(self, host='localhost', port=6379, password=None, db=0, default_timeout=300, key_prefix=None, **kwargs): BaseCache.__init__(self, default_timeout) if isinstance(host, string_types): try: import redis except __HOLE__: raise RuntimeError('no redis module found') if kwargs.get('decode_responses', None): raise ValueError('decode_responses is not supported by ' 'RedisCache.') self._client = redis.Redis(host=host, port=port, password=password, db=db, **kwargs) else: self._client = host self.key_prefix = key_prefix or ''
ImportError
dataset/ETHPy150Open GoogleCloudPlatform/appengine-flask-skeleton/lib/werkzeug/contrib/cache.py/RedisCache.__init__
1,253
def load_object(self, value): """The reversal of :meth:`dump_object`. This might be called with None. """ if value is None: return None if value.startswith(b'!'): try: return pickle.loads(value[1:]) except pickle.PickleError: return None try: return int(value) except __HOLE__: # before 0.8 we did not have serialization. Still support that. return value
ValueError
dataset/ETHPy150Open GoogleCloudPlatform/appengine-flask-skeleton/lib/werkzeug/contrib/cache.py/RedisCache.load_object
1,254
def __init__(self, cache_dir, threshold=500, default_timeout=300, mode=0o600): BaseCache.__init__(self, default_timeout) self._path = cache_dir self._threshold = threshold self._mode = mode try: os.makedirs(self._path) except __HOLE__ as ex: if ex.errno != errno.EEXIST: raise
OSError
dataset/ETHPy150Open GoogleCloudPlatform/appengine-flask-skeleton/lib/werkzeug/contrib/cache.py/FileSystemCache.__init__
1,255
def _prune(self): entries = self._list_dir() if len(entries) > self._threshold: now = time() try: for idx, fname in enumerate(entries): remove = False with open(fname, 'rb') as f: expires = pickle.load(f) remove = (expires != 0 and expires <= now) or idx % 3 == 0 if remove: os.remove(fname) except (IOError, __HOLE__): pass
OSError
dataset/ETHPy150Open GoogleCloudPlatform/appengine-flask-skeleton/lib/werkzeug/contrib/cache.py/FileSystemCache._prune
1,256
def clear(self): for fname in self._list_dir(): try: os.remove(fname) except (__HOLE__, OSError): return False return True
IOError
dataset/ETHPy150Open GoogleCloudPlatform/appengine-flask-skeleton/lib/werkzeug/contrib/cache.py/FileSystemCache.clear
1,257
def get(self, key): filename = self._get_filename(key) try: with open(filename, 'rb') as f: pickle_time = pickle.load(f) if pickle_time == 0 or pickle_time >= time(): return pickle.load(f) else: os.remove(filename) return None except (IOError, __HOLE__, pickle.PickleError): return None
OSError
dataset/ETHPy150Open GoogleCloudPlatform/appengine-flask-skeleton/lib/werkzeug/contrib/cache.py/FileSystemCache.get
1,258
def set(self, key, value, timeout=None): if timeout is None: timeout = int(time() + self.default_timeout) elif timeout != 0: timeout = int(time() + timeout) filename = self._get_filename(key) self._prune() try: fd, tmp = tempfile.mkstemp(suffix=self._fs_transaction_suffix, dir=self._path) with os.fdopen(fd, 'wb') as f: pickle.dump(timeout, f, 1) pickle.dump(value, f, pickle.HIGHEST_PROTOCOL) rename(tmp, filename) os.chmod(filename, self._mode) except (__HOLE__, OSError): return False else: return True
IOError
dataset/ETHPy150Open GoogleCloudPlatform/appengine-flask-skeleton/lib/werkzeug/contrib/cache.py/FileSystemCache.set
1,259
def delete(self, key): try: os.remove(self._get_filename(key)) except (__HOLE__, OSError): return False else: return True
IOError
dataset/ETHPy150Open GoogleCloudPlatform/appengine-flask-skeleton/lib/werkzeug/contrib/cache.py/FileSystemCache.delete
1,260
def has(self, key): filename = self._get_filename(key) try: with open(filename, 'rb') as f: pickle_time = pickle.load(f) if pickle_time == 0 or pickle_time >= time(): return True else: os.remove(filename) return False except (__HOLE__, OSError, pickle.PickleError): return False
IOError
dataset/ETHPy150Open GoogleCloudPlatform/appengine-flask-skeleton/lib/werkzeug/contrib/cache.py/FileSystemCache.has
1,261
def consume_batch_async(self, batch): op = self.operation() op_mutate = op in ['set', 'add'] stdout = sys.stdout msg_visitor = None opts_etc = getattr(self.opts, "etc", None) if opts_etc: stdout = opts_etc.get("stdout", sys.stdout) msg_visitor = opts_etc.get("msg_visitor", None) for msg in batch.msgs: if msg_visitor: msg = msg_visitor(msg) cmd, vbucket_id, key, flg, exp, cas, meta, val = msg if self.skip(key, vbucket_id): continue try: if cmd == couchbaseConstants.CMD_TAP_MUTATION: if op_mutate: # <op> <key> <flags> <exptime> <bytes> [noreply]\r\n stdout.write("%s %s %s %s %s\r\n" % (op, key, flg, exp, len(val))) stdout.write(val) stdout.write("\r\n") elif op == 'get': stdout.write("get %s\r\n" % (key)) elif cmd == couchbaseConstants.CMD_TAP_DELETE: if op_mutate: stdout.write("delete %s\r\n" % (key)) elif cmd == couchbaseConstants.CMD_GET: stdout.write("get %s\r\n" % (key)) else: return "error: StdOutSink - unknown cmd: " + str(cmd), None except __HOLE__: return "error: could not write to stdout", None future = SinkBatchFuture(self, batch) self.future_done(future, 0) return 0, future # --------------------------------------------------
IOError
dataset/ETHPy150Open membase/membase-cli/pump.py/StdOutSink.consume_batch_async
1,262
def rest_request_json(host, port, user, pswd, path, reason=''): err, conn, rest_json = rest_request(host, port, user, pswd, path, reason=reason) if err: return err, None, None if conn: conn.close() try: return None, rest_json, json.loads(rest_json) except __HOLE__, e: return ("error: could not decode JSON from REST API: %s:%s%s" + "; exception: %s" + "; please check URL, username (-u) and password (-p)") % \ (host, port, path, e), None, None
ValueError
dataset/ETHPy150Open membase/membase-cli/pump.py/rest_request_json
1,263
def parse(self, timestr, default=None, ignoretz=False, settings=None, **kwargs): # timestr needs to be a buffer as required by _parse if isinstance(timestr, binary_type): timestr = timestr.decode() # Parse timestr # handle dateutil>=2.5 tuple result first try: res, _ = self._parse(timestr, **kwargs) except __HOLE__: res = self._parse(timestr, **kwargs) if res is None: raise ValueError("unknown string format") # Fill in missing date new_date = self._populate(res, default, settings=settings) # Clean hour and minutes, etc in case not defined for e in ['hour', 'minute', 'second', 'microsecond']: if not getattr(res, e): new_date = new_date.replace(**{e: 0}) return new_date, self.get_period(res)
TypeError
dataset/ETHPy150Open scrapinghub/dateparser/dateparser/date_parser.py/new_parser.parse
1,264
def dateutil_parse(date_string, settings=None, **kwargs): """Wrapper function around dateutil.parser.parse """ today = datetime.utcnow() kwargs.update(default=today) date_string = re.sub(r'\b(year|month|week|day)\b', '', date_string, re.I) # XXX: this is needed because of a bug in dateutil.parser # that raises TypeError for an invalid string # https://bugs.launchpad.net/dateutil/+bug/1042851 try: return new_parser().parse(date_string, settings=settings, **kwargs) except __HOLE__ as e: raise ValueError(e, "Invalid date: %s" % date_string)
TypeError
dataset/ETHPy150Open scrapinghub/dateparser/dateparser/date_parser.py/dateutil_parse
1,265
def _format_external_gateway_info(info): try: return json.dumps(info) except (__HOLE__, KeyError): return ''
TypeError
dataset/ETHPy150Open dtroyer/python-openstackclient/openstackclient/network/v2/router.py/_format_external_gateway_info
1,266
def main(): class MyParser(argparse.ArgumentParser): def error(self, message): sys.stderr.write('error: {0}\n'.format(message)) self.print_help() sys.exit(2) class AppendDictAction(argparse.Action): def __call__(self, parser, namespace, values, option_string=None): items = getattr(namespace, self.dest, {}) try: vals = values.split(',') for t in vals: k, v = t.split('=', 1) items[k] = v except ValueError: parser.error("invalid argument {}".format(self.dest)) setattr(namespace, self.dest, items) class CheckAction(argparse.Action): def _check_command(self, command, package="", why=""): if _util.command_exists(command): print(" INSTALLED {}".format(command)) elif why and package: print(" MISSING {:<20}needed for {}, part of the {} package".format(command, why, package)) elif why: print(" MISSING {:<20}needed for {}".format(command, why)) elif package: print(" MISSING {:<20}part of the {} package".format(command, package)) else: print(" MISSING {}".format(command)) def _check_module(self, module, pip_name="", why=""): if not pip_name: pip_name = module if module == "magic" and _util.module_exists(module): import magic if hasattr(magic, 'from_file'): print(" INSTALLED {:<20}(Python package)".format(pip_name)) elif hasattr(magic, 'open'): print(" INSTALLED {:<20}(system package)".format(pip_name)) else: print(" ERROR {:<20}expecting {}, found other module named magic".format(pip_name, pip_name)) elif module != "magic" and _util.module_exists(module): print(" INSTALLED {}".format(pip_name)) elif why: print(" MISSING {:<20}needed for {}, install using pip".format(pip_name, why)) else: print(" MISSING {:<20}install using pip".format(pip_name, why)) # noinspection PyShadowingNames def __call__(self, parser, namespace, values, option_string=None): print("The following commands are used by imagemounter internally. Without most commands, imagemounter " "works perfectly fine, but may lack some detection or mounting capabilities.") print("-- Mounting base disk images (at least one required, first three recommended) --") self._check_command("xmount", "xmount", "several types of disk images") self._check_command("ewfmount", "ewf-tools", "EWF images (partially covered by xmount)") self._check_command("affuse", "afflib-tools", "AFF images (partially covered by xmount)") self._check_command("vmware-mount", why="VMWare disks") print("-- Detecting volumes and volume types (at least one required) --") self._check_command("mmls", "sleuthkit") self._check_module("pytsk3") self._check_command("parted", "parted") print("-- Detecting volume types (all recommended, first two highly recommended) --") self._check_command("fsstat", "sleuthkit") self._check_command("file", "libmagic1") self._check_module("magic", "python-magic") self._check_command("disktype", "disktype") print("-- Enhanced mounting and detecting disks (install when needed) --") self._check_command("mdadm", "mdadm", "RAID disks") self._check_command("cryptsetup", "cryptsetup", "LUKS containers") self._check_command("mountavfs", "avfs", "compressed disk images") print("-- Mounting volumes (install when needed) --") self._check_command("mount.xfs", "xfsprogs", "XFS volumes") self._check_command("mount.ntfs", "ntfs-3g", "NTFS volumes") self._check_command("lvm", "lvm2", "LVM volumes") self._check_command("vmfs-fuse", "vmfs-tools", "VMFS volumes") self._check_command("mount.jffs2", "mtd-tools", "JFFS2 volumes") self._check_command("mount.squashfs", "squashfs-tools", "SquashFS volumes") self._check_command("bdemount", "libbde-utils", "Bitlocker Drive Encryption volumes") parser.exit() parser = MyParser(description='Utility to mount volumes in Encase and dd images locally.') parser.add_argument('images', nargs='*', help='path(s) to the image(s) that you want to mount; generally just the first file (e.g. ' 'the .E01 or .001 file) or the folder containing the files is enough in the case of ' 'split files') # Special options parser.add_argument('--version', action='version', version=__version__, help='display version and exit') parser.add_argument('--check', action=CheckAction, nargs=0, help='do a system check and list which tools are installed') # Utility specific parser.add_argument('-u', '--unmount', action='store_true', default=False, help='try to unmount left-overs of previous imount runs; may occasionally not be able to ' 'detect all mountpoints or detect too much mountpoints; use --casename to limit ' 'the unmount options') parser.add_argument('-w', '--wait', action='store_true', default=False, help='pause on some additional warnings') parser.add_argument('-k', '--keep', action='store_true', default=False, help='keep volumes mounted after program exits') parser.add_argument('--no-interaction', action='store_true', default=False, help="do not ask for any user input, implies --keep") parser.add_argument('-o', '--only-mount', default=None, help="specify which volume(s) you want to mount, comma-separated") parser.add_argument('-v', '--verbose', action='count', default=False, help='enable verbose output') parser.add_argument('-c', '--color', action='store_true', default=False, help='force colorizing the output') parser.add_argument('--no-color', action='store_true', default=False, help='prevent colorizing the output') # Additional options parser.add_argument('-r', '--reconstruct', action='store_true', default=False, help='attempt to reconstruct the full filesystem tree; implies -s and mounts all partitions ' 'at once') parser.add_argument('--carve', action='store_true', default=False, help='automatically carve the free space of a mounted volume for deleted files') # Specify options to the subsystem parser.add_argument('-md', '--mountdir', default=None, help='specify other directory for volume mountpoints') parser.add_argument('-p', '--pretty', action='store_true', default=False, help='use pretty names for mount points; useful in combination with --mountdir') parser.add_argument('-cn', '--casename', default=None, help='name to add to the --mountdir, often used in conjunction with --pretty') parser.add_argument('-rw', '--read-write', action='store_true', default=False, help='mount image read-write by creating a local write-cache file in a temp directory; ' 'implies --method=xmount') parser.add_argument('-m', '--method', choices=['xmount', 'affuse', 'ewfmount', 'vmware-mount', 'avfs', 'auto', 'dummy'], default='auto', help='use other tool to mount the initial images; results may vary between methods and if ' 'something doesn\'t work, try another method; dummy can be used when base should not be ' 'mounted (default: auto)') parser.add_argument('-d', '--detection', choices=['pytsk3', 'mmls', 'parted', 'auto'], default='auto', help='use other volume detection method; pytsk3 and mmls should provide identical results, ' 'though pytsk3 is using the direct C API of mmls, but requires pytsk3 to be installed; ' 'auto distinguishes between pytsk3 and mmls only ' '(default: auto)') parser.add_argument('--vstype', choices=VOLUME_SYSTEM_TYPES, default="detect", help='specify type of volume system (partition table); if you don\'t know, ' 'use "detect" to try to detect (default: detect)') parser.add_argument('--fstypes', action=AppendDictAction, default={'?': 'unknown'}, help="allows the specification of the file system type per volume number; format: 0.1=lvm,...; " "use volume number ? for all undetected file system types and * for all file systems; " "accepted file systems types are {}".format(", ".join(FILE_SYSTEM_TYPES)) + ", and none only for the ? volume (defaults to unknown)") parser.add_argument('--keys', action=AppendDictAction, default={}, help="allows the specification of key material per volume number; format: 0.1=p:pass,...; " "exact format depends on volume type") # Toggles for default settings you may perhaps want to override toggroup = parser.add_argument_group('toggles') toggroup.add_argument('--stats', action='store_true', default=False, help='show limited information from fsstat, which will slow down mounting and may cause ' 'random issues such as partitions being unreadable (default)') toggroup.add_argument('--no-stats', action='store_true', default=False, help='do not show limited information from fsstat') toggroup.add_argument('--disktype', action='store_true', default=False, help='use the disktype command to get even more information about the volumes (default)') toggroup.add_argument('--no-disktype', action='store_true', default=False, help='do not use disktype to get more information') toggroup.add_argument('--raid', action='store_true', default=False, help="try to detect whether the volume is part of a RAID array (default)") toggroup.add_argument('--no-raid', action='store_true', default=False, help="prevent trying to mount the volume in a RAID array") toggroup.add_argument('--single', action='store_true', default=False, help="do not try to find a volume system, but assume the image contains a single volume") toggroup.add_argument('--no-single', action='store_true', default=False, help="prevent trying to mount the image as a single volume if no volume system was found") args = parser.parse_args() # Colorize the output by default if the terminal supports it if not args.color and args.no_color: args.color = False elif args.color: args.color = True else: args.color = _util.terminal_supports_color() if not args.color: # noinspection PyUnusedLocal,PyShadowingNames def col(s, *args, **kwargs): return s else: from termcolor import colored col = colored class ImageMounterFormatter(logging.Formatter): def format(self, record): msg = record.getMessage() if args.verbose >= 4 and record.exc_info: if not record.exc_text: record.exc_text = self.formatException(record.exc_info) if msg[-1:] != "\n": msg += "\n" msg += record.exc_text if record.levelno >= logging.WARNING: return col("[-] " + msg, 'cyan') elif record.levelno == logging.INFO: return col("[+] " + msg, 'cyan') elif msg.startswith('$'): return col(" " + msg, 'cyan') else: return col(" " + msg, 'cyan') # Set logging level for internal Python handler = logging.StreamHandler() handler.setFormatter(ImageMounterFormatter()) logger = logging.getLogger("imagemounter") logger.setLevel({0: logging.CRITICAL, 1: logging.WARNING, 2: logging.INFO}.get(args.verbose, logging.DEBUG)) logger.addHandler(handler) # Check some prerequisites if os.geteuid(): # Not run as root print(col('[!] Not running as root!', 'yellow')) if 'a' in __version__ or 'b' in __version__: print(col("Development release v{0}. Please report any bugs you encounter.".format(__version__), attrs=['dark'])) print(col("Bug reports: use -vvvv to get maximum verbosity and include imount --check output in your report", attrs=['dark'])) print(col("Critical bug? Use git tag to list all versions and use git checkout <version>", attrs=['dark'])) # Always assume stats, except when --no-stats is present, and --stats is not. if not args.stats and args.no_stats: args.stats = False else: args.stats = True # Make args.disktype default to True explicit_disktype = False if not args.disktype and args.no_disktype: args.disktype = False else: if args.disktype: explicit_disktype = True args.disktype = True # Make args.raid default to True explicit_raid = False if not args.raid and args.no_raid: args.raid = False else: if args.raid: explicit_raid = True args.raid = True # Make args.single default to None if args.single == args.no_single: args.single = None elif args.single: args.single = True elif args.no_single: args.single = False # If --no-interaction is specified, imply --keep and not --wait if args.no_interaction: args.keep = True if args.wait: print(col("[!] --no-interaction can't be used in conjunction with --wait", 'yellow')) args.wait = False # Check if mount method supports rw if args.method not in ('xmount', 'auto') and args.read_write: print(col("[!] {0} does not support mounting read-write! Will mount read-only.".format(args.method), 'yellow')) args.read_write = False # Check if mount method is available mount_command = 'avfsd' if args.method == 'avfs' else args.method if args.method not in ('auto', 'dummy') and not _util.command_exists(mount_command): print(col("[-] {0} is not installed!".format(args.method), 'red')) sys.exit(1) elif args.method == 'auto' and not any(map(_util.command_exists, ('xmount', 'affuse', 'ewfmount', 'vmware-mount', 'avfsd'))): print(col("[-] No tools installed to mount the image base! Please install xmount, affuse (afflib-tools), " "ewfmount (ewf-tools), vmware-mount or avfs first.", 'red')) sys.exit(1) # Check if detection method is available if args.detection == 'pytsk3' and not _util.module_exists('pytsk3'): print(col("[-] pytsk3 module does not exist!", 'red')) sys.exit(1) elif args.detection in ('mmls', 'parted') and not _util.command_exists(args.detection): print(col("[-] {0} is not installed!".format(args.detection), 'red')) sys.exit(1) elif args.detection == 'auto' and not any((_util.module_exists('pytsk3'), _util.command_exists('mmls'), _util.command_exists('parted'))): print(col("[-] No tools installed to detect volumes! Please install mmls (sleuthkit), pytsk3 or parted first.", 'red')) sys.exit(1) # Check if raid is available if args.raid and not _util.command_exists('mdadm'): if explicit_raid: print(col("[!] RAID mount requires the mdadm command.", 'yellow')) args.raid = False if args.reconstruct and not args.stats: # Reconstruct implies use of fsstat print("[!] You explicitly disabled stats, but --reconstruct implies the use of stats. Stats are re-enabled.") args.stats = True # Check if raid is available if args.disktype and not _util.command_exists('disktype'): if explicit_disktype: print(col("[-] The disktype command can not be used in this session, as it is not installed.", 'yellow')) args.disktype = False if args.stats and not _util.command_exists('fsstat'): print(col("[-] The fsstat command (part of sleuthkit package) is required to obtain stats, but is not " "installed. Stats can not be obtained during this session.", 'yellow')) args.stats = False if args.reconstruct: print(col("[-] Reconstruction requires stats to be obtained, but stats can not be enabled.", 'red')) sys.exit(1) if args.fstypes: for k, v in args.fstypes.items(): if v.strip() not in FILE_SYSTEM_TYPES and not (k == '?' and v.strip().lower() == 'none'): print("[!] Error while parsing --fstypes: {} is invalid".format(v)) sys.exit(1) if '*' in args.fstypes: print("[!] You are forcing the file system type to {0}. This may cause unexpected results." .format(args.fstypes['*'])) elif '?' in args.fstypes and args.fstypes['?'] not in ('unknown', 'none'): print("[!] You are using the file system type {0} as fallback. This may cause unexpected results." .format(args.fstypes['?'])) if args.only_mount: args.only_mount = args.only_mount.split(',') if args.vstype != 'detect' and args.single: print("[!] There's no point in using --single in combination with --vstype.") if args.carve and not _util.command_exists('photorec'): print(col("[-] The photorec command (part of testdisk package) is required to carve, but is not " "installed. Carving will be disabled.", 'yellow')) args.carve = False if not args.images and not args.unmount: print(col("[-] You must specify at least one path to a disk image", 'red')) sys.exit(1) if args.unmount: unmounter = Unmounter(**vars(args)) commands = unmounter.preview_unmount() if not commands: print("[+] Nothing to do") parser.exit() print("[!] --unmount will rigorously clean anything that looks like a mount or volume group originating " "from this utility. You may regret using this if you have other mounts or volume groups that are " "similarly named. The following commands will be executed:") for c in commands: print(" {0}".format(c)) try: input(">>> Press [enter] to continue or ^C to cancel... ") unmounter.unmount() except KeyboardInterrupt: print("\n[-] Aborted.") sys.exit(0) # Enumerate over all images in the CLI images = [] for num, image in enumerate(args.images): # If is a directory, find a E01 file in the directory if os.path.isdir(image): for f in glob.glob(os.path.join(image, '*.[E0]01')): images.append(f) break else: print(col("[-] {0} is a directory not containing a .001 or .E01 file, aborting!".format(image), "red")) break continue elif not os.path.exists(image): print(col("[-] Image {0} does not exist, aborting!".format(image), "red")) break images.append(image) else: p = None try: p = ImageParser(images, **vars(args)) num = 0 found_raid = False # Mount all disks. We could use .init, but where's the fun in that? for disk in p.disks: num += 1 print('[+] Mounting image {0} using {1}...'.format(p.paths[0], disk.method)) # Mount the base image using the preferred method if not disk.mount(): print(col("[-] Failed mounting base image. Perhaps try another mount method than {0}?" .format(disk.method), "red")) return if args.raid: if disk.add_to_raid(): found_raid = True if args.read_write: print('[+] Created read-write cache at {0}'.format(disk.rwpath)) if args.disktype: disk.volumes.load_disktype_data() print('[+] Mounted raw image [{num}/{total}]'.format(num=num, total=len(args.images))) sys.stdout.write("[+] Mounting volume...\r") sys.stdout.flush() has_left_mounted = False for volume in p.mount_volumes(args.single, args.only_mount): try: # something failed? if not volume.mountpoint and not volume.loopback: if volume.exception and volume.size is not None and volume.size <= 1048576: print(col('[-] Exception while mounting small volume {0}'.format(volume.get_description()), 'yellow')) if args.wait: input(col('>>> Press [enter] to continue... ', attrs=['dark'])) elif volume.exception: print(col('[-] Exception while mounting {0}'.format(volume.get_description()), 'red')) if not args.no_interaction: input(col('>>> Press [enter] to continue... ', attrs=['dark'])) elif volume.flag != 'alloc': if args.wait or args.verbose: # do not show skipped messages by default print(col('[-] Skipped {0} {1} volume' .format(volume.get_description(), volume.flag), 'yellow')) if args.wait: input(col('>>> Press [enter] to continue... ', attrs=['dark'])) elif not volume._should_mount(args.only_mount): print(col('[-] Skipped {0}'.format(volume.get_description()), 'yellow')) else: print(col('[-] Could not mount volume {0}'.format(volume.get_description()), 'yellow')) if args.wait: input(col('>>> Press [enter] to continue... ', attrs=['dark'])) if args.carve and volume.flag in ('alloc', 'unalloc'): sys.stdout.write("[+] Carving volume...\r") sys.stdout.flush() if volume.carve(freespace=False): print('[+] Carved data is available at {0}.'.format(col(volume.carvepoint, 'green', attrs=['bold']))) else: print(col('[-] Carving failed.', 'red')) else: continue # we do not need the unmounting sequence else: # it all was ok if volume.mountpoint: print('[+] Mounted volume {0} on {1}.'.format(col(volume.get_description(), attrs=['bold']), col(volume.mountpoint, 'green', attrs=['bold']))) elif volume.loopback: # fallback, generally indicates error. print('[+] Mounted volume {0} as loopback on {1}.'.format(col(volume.get_description(), attrs=['bold']), col(volume.loopback, 'green', attrs=['bold']))) print(col('[-] Could not detect further volumes in the loopback device.', 'red')) if args.carve: sys.stdout.write("[+] Carving volume...\r") sys.stdout.flush() if volume.carve(): print('[+] Carved data is available at {0}.'.format(col(volume.carvepoint, 'green', attrs=['bold']))) else: print(col('[-] Carving failed.', 'red')) # Do not offer unmount when reconstructing if args.reconstruct or args.keep: has_left_mounted = True continue input(col('>>> Press [enter] to unmount the volume, or ^C to keep mounted... ', attrs=['dark'])) # Case where image should be unmounted, but has failed to do so. Keep asking whether the user wants # to unmount. while True: if volume.unmount(): break else: try: print(col("[-] Error unmounting volume. Perhaps files are still open?", "red")) input(col('>>> Press [enter] to retry unmounting, or ^C to skip... ', attrs=['dark'])) except KeyboardInterrupt: has_left_mounted = True print("") break except KeyboardInterrupt: has_left_mounted = True print("") sys.stdout.write("[+] Mounting volume...\r") sys.stdout.flush() for disk in p.disks: if [x for x in disk.volumes if x.was_mounted] == 0: if args.vstype != 'detect': print(col('[?] Could not determine volume information of {0}. Image may be empty, ' 'or volume system type {0} was incorrect.'.format(args.vstype.upper()), 'yellow')) elif found_raid: print(col('[?] Could not determine volume information. Image may be empty, or volume system ' 'type could not be detected. Try explicitly providing the volume system type with ' '--vstype, or providing more volumes to complete the RAID array.', 'yellow')) elif not args.raid or args.single is False: print(col('[?] Could not determine volume information. Image may be empty, or volume system ' 'type could not be detected. Try explicitly providing the volume system type with ' '--vstype, mounting as RAID with --raid and/or mounting as a single volume with ' '--single', 'yellow')) else: print(col('[?] Could not determine volume information. Image may be empty, or volume system ' 'type could not be detected. Try explicitly providing the volume system type with ' '--vstype.', 'yellow')) if args.wait: input(col('>>> Press [enter] to continue... ', attrs=['dark'])) print('[+] Parsed all volumes!') # Perform reconstruct if required if args.reconstruct: # Reverse order so '/' gets unmounted last print("[+] Performing reconstruct... ") root = p.reconstruct() if not root: print(col("[-] Failed reconstructing filesystem: could not find root directory.", 'red')) else: failed = [] for disk in p.disks: failed.extend([x for x in disk.volumes if not x.bindmountpoint and x.mountpoint and x != root]) if failed: print("[+] Parts of the filesystem are reconstructed in {0}.".format(col(root.mountpoint, "green", attrs=["bold"]))) for m in failed: print(" {0} was not reconstructed".format(m.mountpoint)) else: print("[+] The entire filesystem is reconstructed in {0}.".format(col(root.mountpoint, "green", attrs=["bold"]))) if not args.keep: input(col(">>> Press [enter] to unmount all volumes... ", attrs=['dark'])) elif has_left_mounted and not args.keep: input(col(">>> Some volumes were left mounted. Press [enter] to unmount all... ", attrs=['dark'])) except KeyboardInterrupt: print('\n[+] User pressed ^C, aborting...') return except Exception as e: import traceback traceback.print_exc() print(col("[-] {0}".format(e), 'red')) if not args.no_interaction: input(col(">>> Press [enter] to continue.", attrs=['dark'])) finally: if args.keep: print("[+] Analysis complete.") elif not p: print("[-] Crashed before creating parser") else: print('[+] Analysis complete, unmounting...') # All done with this image, unmount it try: remove_rw = p.rw_active() and 'y' in input('>>> Delete the rw cache file? [y/N] ').lower() except KeyboardInterrupt: remove_rw = False while True: if p.clean(remove_rw): break else: try: print(col("[-] Error unmounting base image. Perhaps volumes are still open?", 'red')) input(col('>>> Press [enter] to retry unmounting, or ^C to cancel... ', attrs=['dark'])) except __HOLE__: print("") # ^C does not print \n break print("[+] All cleaned up")
KeyboardInterrupt
dataset/ETHPy150Open ralphje/imagemounter/imagemounter/imount.py/main
1,267
def lookup_id(self, id): try: return self.Identities[id] except __HOLE__: if self.fallback: return self.fallback.lookup_id(id) else: return None
KeyError
dataset/ETHPy150Open antong/ldaptor/ldaptor/protocols/pureber.py/BERDecoderContext.lookup_id
1,268
def test_is_length_failure(self): try: assert_that('foo').is_length(4) fail('should have raised error') except __HOLE__ as ex: assert_that(str(ex)).is_equal_to('Expected <foo> to be of length <4>, but was <3>.')
AssertionError
dataset/ETHPy150Open ActivisionGameScience/assertpy/tests/test_string.py/TestString.test_is_length_failure
1,269
def test_contains_single_item_failure(self): try: assert_that('foo').contains('x') fail('should have raised error') except __HOLE__ as ex: assert_that(str(ex)).is_equal_to('Expected <foo> to contain item <x>, but did not.')
AssertionError
dataset/ETHPy150Open ActivisionGameScience/assertpy/tests/test_string.py/TestString.test_contains_single_item_failure
1,270
def test_contains_multi_item_failure(self): try: assert_that('foo').contains('f','x','z') fail('should have raised error') except __HOLE__ as ex: assert_that(str(ex)).is_equal_to("Expected <foo> to contain items ('f', 'x', 'z'), but did not contain <x>.")
AssertionError
dataset/ETHPy150Open ActivisionGameScience/assertpy/tests/test_string.py/TestString.test_contains_multi_item_failure
1,271
def test_contains_ignoring_case_type_failure(self): try: assert_that(123).contains_ignoring_case('f') fail('should have raised error') except __HOLE__ as ex: assert_that(str(ex)).is_equal_to('val is not a string')
TypeError
dataset/ETHPy150Open ActivisionGameScience/assertpy/tests/test_string.py/TestString.test_contains_ignoring_case_type_failure
1,272
def test_contains_ignoring_case_missinge_item_failure(self): try: assert_that('foo').contains_ignoring_case() fail('should have raised error') except __HOLE__ as ex: assert_that(str(ex)).is_equal_to('one or more args must be given')
ValueError
dataset/ETHPy150Open ActivisionGameScience/assertpy/tests/test_string.py/TestString.test_contains_ignoring_case_missinge_item_failure
1,273
def test_contains_ignoring_case_single_item_failure(self): try: assert_that('foo').contains_ignoring_case('X') fail('should have raised error') except __HOLE__ as ex: assert_that(str(ex)).is_equal_to('Expected <foo> to case-insensitive contain item <X>, but did not.')
AssertionError
dataset/ETHPy150Open ActivisionGameScience/assertpy/tests/test_string.py/TestString.test_contains_ignoring_case_single_item_failure
1,274
def test_contains_ignoring_case_single_item_type_failure(self): try: assert_that('foo').contains_ignoring_case(12) fail('should have raised error') except __HOLE__ as ex: assert_that(str(ex)).is_equal_to('given arg must be a string')
TypeError
dataset/ETHPy150Open ActivisionGameScience/assertpy/tests/test_string.py/TestString.test_contains_ignoring_case_single_item_type_failure
1,275
def test_contains_ignoring_case_multi_item_failure(self): try: assert_that('foo').contains_ignoring_case('F','X','Z') fail('should have raised error') except __HOLE__ as ex: assert_that(str(ex)).is_equal_to("Expected <foo> to case-insensitive contain items ('F', 'X', 'Z'), but did not contain <X>.")
AssertionError
dataset/ETHPy150Open ActivisionGameScience/assertpy/tests/test_string.py/TestString.test_contains_ignoring_case_multi_item_failure
1,276
def test_contains_ignoring_case_multi_item_type_failure(self): try: assert_that('foo').contains_ignoring_case('F', 12) fail('should have raised error') except __HOLE__ as ex: assert_that(str(ex)).is_equal_to('given args must all be strings')
TypeError
dataset/ETHPy150Open ActivisionGameScience/assertpy/tests/test_string.py/TestString.test_contains_ignoring_case_multi_item_type_failure
1,277
def test_does_not_contain_single_item_failure(self): try: assert_that('foo').does_not_contain('f') fail('should have raised error') except __HOLE__ as ex: assert_that(str(ex)).is_equal_to('Expected <foo> to not contain item <f>, but did.')
AssertionError
dataset/ETHPy150Open ActivisionGameScience/assertpy/tests/test_string.py/TestString.test_does_not_contain_single_item_failure
1,278
def test_does_not_contain_list_item_failure(self): try: assert_that('foo').does_not_contain('x','y','f') fail('should have raised error') except __HOLE__ as ex: assert_that(str(ex)).is_equal_to("Expected <foo> to not contain items ('x', 'y', 'f'), but did contain <f>.")
AssertionError
dataset/ETHPy150Open ActivisionGameScience/assertpy/tests/test_string.py/TestString.test_does_not_contain_list_item_failure
1,279
def test_is_empty_failure(self): try: assert_that('foo').is_empty() fail('should have raised error') except __HOLE__ as ex: assert_that(str(ex)).is_equal_to('Expected <foo> to be empty string, but was not.')
AssertionError
dataset/ETHPy150Open ActivisionGameScience/assertpy/tests/test_string.py/TestString.test_is_empty_failure
1,280
def test_is_not_empty_failure(self): try: assert_that('').is_not_empty() fail('should have raised error') except __HOLE__ as ex: assert_that(str(ex)).is_equal_to('Expected not empty string, but was empty.')
AssertionError
dataset/ETHPy150Open ActivisionGameScience/assertpy/tests/test_string.py/TestString.test_is_not_empty_failure
1,281
def test_is_equal_ignoring_case_failure(self): try: assert_that('foo').is_equal_to_ignoring_case('bar') fail('should have raised error') except __HOLE__ as ex: assert_that(str(ex)).is_equal_to('Expected <foo> to be case-insensitive equal to <bar>, but was not.')
AssertionError
dataset/ETHPy150Open ActivisionGameScience/assertpy/tests/test_string.py/TestString.test_is_equal_ignoring_case_failure
1,282
def test_is_equal_ignoring_case_bad_value_type_failure(self): try: assert_that(123).is_equal_to_ignoring_case(12) fail('should have raised error') except __HOLE__ as ex: assert_that(str(ex)).is_equal_to('val is not a string')
TypeError
dataset/ETHPy150Open ActivisionGameScience/assertpy/tests/test_string.py/TestString.test_is_equal_ignoring_case_bad_value_type_failure
1,283
def test_is_equal_ignoring_case_bad_arg_type_failure(self): try: assert_that('fred').is_equal_to_ignoring_case(12) fail('should have raised error') except __HOLE__ as ex: assert_that(str(ex)).is_equal_to('given arg must be a string')
TypeError
dataset/ETHPy150Open ActivisionGameScience/assertpy/tests/test_string.py/TestString.test_is_equal_ignoring_case_bad_arg_type_failure
1,284
def test_starts_with_failure(self): try: assert_that('fred').starts_with('bar') fail('should have raised error') except __HOLE__ as ex: assert_that(str(ex)).is_equal_to('Expected <fred> to start with <bar>, but did not.')
AssertionError
dataset/ETHPy150Open ActivisionGameScience/assertpy/tests/test_string.py/TestString.test_starts_with_failure
1,285
def test_starts_with_bad_value_type_failure(self): try: assert_that(123).starts_with(12) fail('should have raised error') except __HOLE__ as ex: assert_that(str(ex)).is_equal_to('val is not a string or iterable')
TypeError
dataset/ETHPy150Open ActivisionGameScience/assertpy/tests/test_string.py/TestString.test_starts_with_bad_value_type_failure
1,286
def test_starts_with_bad_arg_none_failure(self): try: assert_that('fred').starts_with(None) fail('should have raised error') except __HOLE__ as ex: assert_that(str(ex)).is_equal_to('given prefix arg must not be none')
TypeError
dataset/ETHPy150Open ActivisionGameScience/assertpy/tests/test_string.py/TestString.test_starts_with_bad_arg_none_failure
1,287
def test_starts_with_bad_arg_type_failure(self): try: assert_that('fred').starts_with(123) fail('should have raised error') except __HOLE__ as ex: assert_that(str(ex)).is_equal_to('given prefix arg must be a string')
TypeError
dataset/ETHPy150Open ActivisionGameScience/assertpy/tests/test_string.py/TestString.test_starts_with_bad_arg_type_failure
1,288
def test_starts_with_bad_arg_empty_failure(self): try: assert_that('fred').starts_with('') fail('should have raised error') except __HOLE__ as ex: assert_that(str(ex)).is_equal_to('given prefix arg must not be empty')
ValueError
dataset/ETHPy150Open ActivisionGameScience/assertpy/tests/test_string.py/TestString.test_starts_with_bad_arg_empty_failure
1,289
def test_ends_with_failure(self): try: assert_that('fred').ends_with('bar') fail('should have raised error') except __HOLE__ as ex: assert_that(str(ex)).is_equal_to('Expected <fred> to end with <bar>, but did not.')
AssertionError
dataset/ETHPy150Open ActivisionGameScience/assertpy/tests/test_string.py/TestString.test_ends_with_failure
1,290
def test_ends_with_bad_value_type_failure(self): try: assert_that(123).ends_with(12) fail('should have raised error') except __HOLE__ as ex: assert_that(str(ex)).is_equal_to('val is not a string or iterable')
TypeError
dataset/ETHPy150Open ActivisionGameScience/assertpy/tests/test_string.py/TestString.test_ends_with_bad_value_type_failure
1,291
def test_ends_with_bad_arg_none_failure(self): try: assert_that('fred').ends_with(None) fail('should have raised error') except __HOLE__ as ex: assert_that(str(ex)).is_equal_to('given suffix arg must not be none')
TypeError
dataset/ETHPy150Open ActivisionGameScience/assertpy/tests/test_string.py/TestString.test_ends_with_bad_arg_none_failure
1,292
def test_ends_with_bad_arg_type_failure(self): try: assert_that('fred').ends_with(123) fail('should have raised error') except __HOLE__ as ex: assert_that(str(ex)).is_equal_to('given suffix arg must be a string')
TypeError
dataset/ETHPy150Open ActivisionGameScience/assertpy/tests/test_string.py/TestString.test_ends_with_bad_arg_type_failure
1,293
def test_ends_with_bad_arg_empty_failure(self): try: assert_that('fred').ends_with('') fail('should have raised error') except __HOLE__ as ex: assert_that(str(ex)).is_equal_to('given suffix arg must not be empty')
ValueError
dataset/ETHPy150Open ActivisionGameScience/assertpy/tests/test_string.py/TestString.test_ends_with_bad_arg_empty_failure
1,294
def test_matches_failure(self): try: assert_that('fred').matches('\d+') fail('should have raised error') except __HOLE__ as ex: assert_that(str(ex)).is_equal_to('Expected <fred> to match pattern <\d+>, but did not.')
AssertionError
dataset/ETHPy150Open ActivisionGameScience/assertpy/tests/test_string.py/TestString.test_matches_failure
1,295
def test_matches_bad_value_type_failure(self): try: assert_that(123).matches(12) fail('should have raised error') except __HOLE__ as ex: assert_that(str(ex)).is_equal_to('val is not a string')
TypeError
dataset/ETHPy150Open ActivisionGameScience/assertpy/tests/test_string.py/TestString.test_matches_bad_value_type_failure
1,296
def test_matches_bad_arg_type_failure(self): try: assert_that('fred').matches(123) fail('should have raised error') except __HOLE__ as ex: assert_that(str(ex)).is_equal_to('given pattern arg must be a string')
TypeError
dataset/ETHPy150Open ActivisionGameScience/assertpy/tests/test_string.py/TestString.test_matches_bad_arg_type_failure
1,297
def test_matches_bad_arg_empty_failure(self): try: assert_that('fred').matches('') fail('should have raised error') except __HOLE__ as ex: assert_that(str(ex)).is_equal_to('given pattern arg must not be empty')
ValueError
dataset/ETHPy150Open ActivisionGameScience/assertpy/tests/test_string.py/TestString.test_matches_bad_arg_empty_failure
1,298
def test_does_not_match_failure(self): try: assert_that('fred').does_not_match('\w+') fail('should have raised error') except __HOLE__ as ex: assert_that(str(ex)).is_equal_to('Expected <fred> to not match pattern <\w+>, but did.')
AssertionError
dataset/ETHPy150Open ActivisionGameScience/assertpy/tests/test_string.py/TestString.test_does_not_match_failure
1,299
def test_does_not_match_bad_value_type_failure(self): try: assert_that(123).does_not_match(12) fail('should have raised error') except __HOLE__ as ex: assert_that(str(ex)).is_equal_to('val is not a string')
TypeError
dataset/ETHPy150Open ActivisionGameScience/assertpy/tests/test_string.py/TestString.test_does_not_match_bad_value_type_failure