def raise_cannot_open(path): pieces = path.split('/') for i in xrange(1, len(pieces) + 1): so_far = '/'.join(pieces[0:i]) if not os.path.exists(so_far): if i == 1: if so_far == '': continue raise IOError('Cannot open ' + path + ' (' + so_far + ' does not exist)') parent = '/'.join(pieces[0:i - 1]) bad = pieces[i - 1] if not os.path.isdir(parent): raise IOError("Cannot open " + path + " because " + parent + " is not a directory.") candidates = os.listdir(parent) if len(candidates) == 0: raise IOError("Cannot open " + path + " because " + parent + " is empty.") if len(candidates) > 100: # Don't attempt to guess the right name if the directory is huge raise IOError("Cannot open " + path + " but can open " + parent + ".") raise IOError("Cannot open " + path + " but can open " + parent + ". Did you mean " + match(bad, candidates) + " instead of " + bad + "?") # end if # end for assert False
def raise_cannot_open(path): """ Raise an exception saying we can't open `path`. Parameters ---------- path : str The path we cannot open """ pieces = path.split('/') for i in xrange(1, len(pieces) + 1): so_far = '/'.join(pieces[0:i]) if not os.path.exists(so_far): if i == 1: if so_far == '': continue reraise_as( IOError('Cannot open ' + path + ' (' + so_far + ' does not exist)')) parent = '/'.join(pieces[0:i - 1]) bad = pieces[i - 1] if not os.path.isdir(parent): reraise_as( IOError("Cannot open " + path + " because " + parent + " is not a directory.")) candidates = os.listdir(parent) if len(candidates) == 0: reraise_as( IOError("Cannot open " + path + " because " + parent + " is empty.")) if len(candidates) > 100: # Don't attempt to guess the right name if the directory is # huge reraise_as( IOError("Cannot open " + path + " but can open " + parent + ".")) if os.path.islink(path): reraise_as( IOError(path + " appears to be a symlink to a " "non-existent file")) reraise_as( IOError("Cannot open " + path + " but can open " + parent + ". Did you mean " + match(bad, candidates) + " instead of " + bad + "?")) # end if # end for assert False
def raise_cannot_open(path): """ Raise an exception saying we can't open `path`. Parameters ---------- path : str The path we cannot open """ pieces = path.split('/') for i in xrange(1, len(pieces) + 1): so_far = '/'.join(pieces[0:i]) if not os.path.exists(so_far): if i == 1: if so_far == '': continue reraise_as(IOError('Cannot open ' + path + ' (' + so_far + ' does not exist)')) parent = '/'.join(pieces[0:i - 1]) bad = pieces[i - 1] if not os.path.isdir(parent): reraise_as(IOError("Cannot open " + path + " because " + parent + " is not a directory.")) candidates = os.listdir(parent) if len(candidates) == 0: reraise_as(IOError("Cannot open " + path + " because " + parent + " is empty.")) if len(candidates) > 100: # Don't attempt to guess the right name if the directory is # huge reraise_as(IOError("Cannot open " + path + " but can open " + parent + ".")) if os.path.islink(path): reraise_as(IOError(path + " appears to be a symlink to a " "non-existent file")) reraise_as(IOError("Cannot open " + path + " but can open " + parent + ". Did you mean " + match(bad, candidates) + " instead of " + bad + "?")) # end if # end for assert False
def raise_cannot_open(path): """ .. todo:: WRITEME """ pieces = path.split("/") for i in xrange(1, len(pieces) + 1): so_far = "/".join(pieces[0:i]) if not os.path.exists(so_far): if i == 1: if so_far == "": continue raise IOError("Cannot open " + path + " (" + so_far + " does not exist)") parent = "/".join(pieces[0 : i - 1]) bad = pieces[i - 1] if not os.path.isdir(parent): raise IOError("Cannot open " + path + " because " + parent + " is not a directory.") candidates = os.listdir(parent) if len(candidates) == 0: raise IOError("Cannot open " + path + " because " + parent + " is empty.") if len(candidates) > 100: # Don't attempt to guess the right name if the directory is huge raise IOError("Cannot open " + path + " but can open " + parent + ".") if os.path.islink(path): raise IOError(path + " appears to be a symlink to a non-existent file") raise IOError( "Cannot open " + path + " but can open " + parent + ". Did you mean " + match(bad, candidates) + " instead of " + bad + "?" ) # end if # end for assert False
def raise_cannot_open(path): pieces = path.split('/') for i in xrange(1,len(pieces)+1): so_far = '/'.join(pieces[0:i]) if not os.path.exists(so_far): if i == 1: if so_far == '': continue raise IOError('Cannot open '+path+' ('+so_far+' does not exist)') parent = '/'.join(pieces[0:i-1]) bad = pieces[i-1] if not os.path.isdir(parent): raise IOError("Cannot open "+path+" because "+parent+" is not a directory.") candidates = os.listdir(parent) if len(candidates) == 0: raise IOError("Cannot open "+path+" because "+parent+" is empty.") if len(candidates) > 100: # Don't attempt to guess the right name if the directory is huge raise IOError("Cannot open "+path+" but can open "+parent+".") raise IOError("Cannot open "+path+" but can open "+parent+". Did you mean "+match(bad,candidates)+" instead of "+bad+"?") # end if # end for assert False
str(e)) j += 1 try: obj = eval(tag_suffix) except AttributeError, e: try: # Try to figure out what the wrong field name was # If we fail to do it, just fall back to giving the usual # attribute error pieces = tag_suffix.split('.') module = '.'.join(pieces[:-1]) field = pieces[-1] candidates = dir(eval(module)) msg = ('Could not evaluate %s. ' % tag_suffix + 'Did you mean ' + match(field, candidates) + '? ' + 'Original error was ' + str(e)) except: warnings.warn("Attempt to decipher AttributeError failed") raise AttributeError('Could not evaluate %s. ' % tag_suffix + 'Original error was ' + str(e)) raise AttributeError(msg) return obj def initialize(): """ Initialize the configuration system by installing YAML handlers. Automatically done on first call to load() specified in this file. """
+ str(e))) j += 1 try: obj = eval(tag_suffix) except AttributeError, e: try: # Try to figure out what the wrong field name was # If we fail to do it, just fall back to giving the usual # attribute error pieces = tag_suffix.split('.') module = '.'.join(pieces[:-1]) field = pieces[-1] candidates = dir(eval(module)) msg = ('Could not evaluate %s. ' % tag_suffix + 'Did you mean ' + match(field, candidates) + '? ' + 'Original error was ' + str(e)) except Exception: warnings.warn("Attempt to decipher AttributeError failed") reraise_as(AttributeError('Could not evaluate %s. ' % tag_suffix + 'Original error was ' + str(e))) reraise_as(AttributeError(msg)) return obj def initialize(): """ Initialize the configuration system by installing YAML handlers. Automatically done on first call to load() specified in this file. """
+ str(e)) j += 1 try: obj = eval(tag_suffix) except AttributeError, e: try: # Try to figure out what the wrong field name was # If we fail to do it, just fall back to giving the usual # attribute error pieces = tag_suffix.split('.') module = '.'.join(pieces[:-1]) field = pieces[-1] candidates = dir(eval(module)) msg = ('Could not evaluate %s. ' % tag_suffix + 'Did you mean ' + match(field, candidates) + '? ' + 'Original error was ' + str(e)) except: warnings.warn("Attempt to decipher AttributeError failed") raise AttributeError('Could not evaluate %s. ' % tag_suffix + 'Original error was ' + str(e)) raise AttributeError(msg) return obj def initialize(): """ Initialize the configuration system by installing YAML handlers. Automatically done on first call to load() specified in this file. """
def try_to_import(tag_suffix): """ .. todo:: WRITEME """ components = tag_suffix.split('.') modulename = '.'.join(components[:-1]) try: exec('import %s' % modulename) except ImportError as e: # We know it's an ImportError, but is it an ImportError related to # this path, # or did the module we're importing have an unrelated ImportError? # and yes, this test can still have false positives, feel free to # improve it pieces = modulename.split('.') str_e = str(e) found = True in [piece.find(str(e)) != -1 for piece in pieces] if found: # The yaml file is probably to blame. # Report the problem with the full module path from the YAML # file reraise_as(ImportError("Could not import %s; ImportError was %s" % (modulename, str_e))) else: pcomponents = components[:-1] assert len(pcomponents) >= 1 j = 1 while j <= len(pcomponents): modulename = '.'.join(pcomponents[:j]) try: exec('import %s' % modulename) except Exception: base_msg = 'Could not import %s' % modulename if j > 1: modulename = '.'.join(pcomponents[:j - 1]) base_msg += ' but could import %s' % modulename reraise_as(ImportError(base_msg + '. Original exception: ' + str(e))) j += 1 try: obj = eval(tag_suffix) except AttributeError as e: try: # Try to figure out what the wrong field name was # If we fail to do it, just fall back to giving the usual # attribute error pieces = tag_suffix.split('.') module = '.'.join(pieces[:-1]) field = pieces[-1] candidates = dir(eval(module)) msg = ('Could not evaluate %s. ' % tag_suffix + 'Did you mean ' + match(field, candidates) + '? ' + 'Original error was ' + str(e)) except Exception: warnings.warn("Attempt to decipher AttributeError failed") reraise_as(AttributeError('Could not evaluate %s. ' % tag_suffix + 'Original error was ' + str(e))) reraise_as(AttributeError(msg)) return obj
def check_call_arguments(to_call, kwargs): """ Check the call signature against a dictionary of proposed arguments, raising an informative exception in the case of mismatch. Parameters ---------- to_call : class or callable Function or class to examine (in the case of classes, the constructor call signature is analyzed) kwargs : dict Dictionary mapping parameter names (including positional arguments) to proposed values. """ if 'self' in kwargs.keys(): raise TypeError("Your dictionary includes an entry for 'self', " "which is just asking for trouble") orig_to_call = getattr(to_call, '__name__', str(to_call)) if not isinstance(to_call, types.FunctionType): if hasattr(to_call, '__init__'): to_call = to_call.__init__ elif hasattr(to_call, '__call__'): to_call = to_call.__call__ args, varargs, keywords, defaults = inspect.getargspec(to_call) if any(not isinstance(arg, str) for arg in args): raise TypeError('%s uses argument unpacking, which is deprecated and ' 'unsupported by this pylearn2' % orig_to_call) if varargs is not None: raise TypeError('%s has a variable length argument list, but ' 'this is not supported by config resolution' % orig_to_call) if keywords is None: bad_keywords = [arg_name for arg_name in kwargs.keys() if arg_name not in args] if len(bad_keywords) > 0: bad = ', '.join(bad_keywords) args = [ arg for arg in args if arg != 'self' ] if len(args) == 0: matched_str = '(It does not support any keywords, actually)' else: matched = [ match(keyword, args) for keyword in bad_keywords ] matched_str = 'Did you mean %s?' % (', '.join(matched)) raise TypeError('%s does not support the following ' 'keywords: %s. %s' % (orig_to_call, bad, matched_str)) if defaults is None: num_defaults = 0 else: num_defaults = len(defaults) required = args[:len(args) - num_defaults] missing = [arg for arg in required if arg not in kwargs] if len(missing) > 0: #iff the im_self (or __self__) field is present, this is a # bound method, which has 'self' listed as an argument, but # which should not be supplied by kwargs is_bound = hasattr(to_call, 'im_self') or hasattr(to_call, '__self__') if len(missing) > 1 or missing[0] != 'self' or not is_bound: if 'self' in missing: missing.remove('self') missing = ', '.join([str(m) for m in missing]) raise TypeError('%s did not get these expected ' 'arguments: %s' % (orig_to_call, missing))
def try_to_import(tag_suffix): """ .. todo:: WRITEME """ components = tag_suffix.split('.') modulename = '.'.join(components[:-1]) try: exec('import %s' % modulename) except ImportError as e: # We know it's an ImportError, but is it an ImportError related to # this path, # or did the module we're importing have an unrelated ImportError? # and yes, this test can still have false positives, feel free to # improve it pieces = modulename.split('.') str_e = str(e) found = True in [piece.find(str(e)) != -1 for piece in pieces] if found: # The yaml file is probably to blame. # Report the problem with the full module path from the YAML # file reraise_as( ImportError("Could not import %s; ImportError was %s" % (modulename, str_e))) else: pcomponents = components[:-1] assert len(pcomponents) >= 1 j = 1 while j <= len(pcomponents): modulename = '.'.join(pcomponents[:j]) try: exec('import %s' % modulename) except Exception: base_msg = 'Could not import %s' % modulename if j > 1: modulename = '.'.join(pcomponents[:j - 1]) base_msg += ' but could import %s' % modulename reraise_as( ImportError(base_msg + '. Original exception: ' + str(e))) j += 1 try: obj = eval(tag_suffix) except AttributeError as e: try: # Try to figure out what the wrong field name was # If we fail to do it, just fall back to giving the usual # attribute error pieces = tag_suffix.split('.') module = '.'.join(pieces[:-1]) field = pieces[-1] candidates = dir(eval(module)) msg = ('Could not evaluate %s. ' % tag_suffix + 'Did you mean ' + match(field, candidates) + '? ' + 'Original error was ' + str(e)) except Exception: warnings.warn("Attempt to decipher AttributeError failed") reraise_as( AttributeError('Could not evaluate %s. ' % tag_suffix + 'Original error was ' + str(e))) reraise_as(AttributeError(msg)) return obj
try: obj = eval(tag_suffix) except AttributeError, e: try: # Try to figure out what the wrong field name was # If we fail to do it, just fall back to giving the usual # attribute error pieces = tag_suffix.split(".") module = ".".join(pieces[:-1]) field = pieces[-1] candidates = dir(eval(module)) msg = ( "Could not evaluate %s. " % tag_suffix + "Did you mean " + match(field, candidates) + "? " + "Original error was " + str(e) ) except: warnings.warn("Attempt to decipher AttributeError failed") raise AttributeError("Could not evaluate %s. " % tag_suffix + "Original error was " + str(e)) raise AttributeError(msg) return obj def initialize(): """ Initialize the configuration system by installing YAML handlers.
def check_call_arguments(to_call, kwargs): """ Check the call signature against a dictionary of proposed arguments, raising an informative exception in the case of mismatch. Parameters ---------- to_call : class or callable Function or class to examine (in the case of classes, the constructor call signature is analyzed). kwargs : dict Dictionary mapping parameter names (including positional arguments) to proposed values. """ if 'self' in kwargs.keys(): raise TypeError("Your dictionary includes an entry for 'self', " "which is just asking for trouble") orig_to_call = getattr(to_call, '__name__', str(to_call)) if not isinstance(to_call, types.FunctionType): if hasattr(to_call, '__init__'): to_call = to_call.__init__ elif hasattr(to_call, '__call__'): to_call = to_call.__call__ args, varargs, keywords, defaults = inspect.getargspec(to_call) if any(not isinstance(arg, str) for arg in args): raise TypeError('%s uses argument unpacking, which is deprecated and ' 'unsupported by this pylearn2' % orig_to_call) if varargs is not None: raise TypeError('%s has a variable length argument list, but ' 'this is not supported by config resolution' % orig_to_call) if keywords is None: bad_keywords = [ arg_name for arg_name in kwargs.keys() if arg_name not in args ] if len(bad_keywords) > 0: bad = ', '.join(bad_keywords) args = [arg for arg in args if arg != 'self'] if len(args) == 0: matched_str = '(It does not support any keywords, actually)' else: matched = [match(keyword, args) for keyword in bad_keywords] matched_str = 'Did you mean %s?' % (', '.join(matched)) raise TypeError('%s does not support the following ' 'keywords: %s. %s' % (orig_to_call, bad, matched_str)) if defaults is None: num_defaults = 0 else: num_defaults = len(defaults) required = args[:len(args) - num_defaults] missing = [arg for arg in required if arg not in kwargs] if len(missing) > 0: #iff the im_self (or __self__) field is present, this is a # bound method, which has 'self' listed as an argument, but # which should not be supplied by kwargs is_bound = hasattr(to_call, 'im_self') or hasattr(to_call, '__self__') if len(missing) > 1 or missing[0] != 'self' or not is_bound: if 'self' in missing: missing.remove('self') missing = ', '.join([str(m) for m in missing]) raise TypeError('%s did not get these expected ' 'arguments: %s' % (orig_to_call, missing))