def run(args): assert len(args) == 0 exercise_forward_compatibility() exercise_misc() assert utils.sequence_index_dict(["a", "b"]) == {"a": 0, "b": 1} assert utils.flat_list(0) == [0] assert utils.flat_list([1, 2, 3]) == [1, 2, 3] assert utils.flat_list([1, [2, 3, 4], 3]) == [1, 2, 3, 4, 3] assert utils.flat_list([1, [2, 3, 4], [[3, 4], [5, 6]]]) == [1, 2, 3, 4, 3, 4, 5, 6] try: raise RuntimeError("Trial") except KeyboardInterrupt: raise except Exception: assert utils.format_exception() == "RuntimeError: Trial" else: raise Exception_expected try: assert 1 == 2 except KeyboardInterrupt: raise except Exception: s = utils.format_exception() assert s.startswith("AssertionError: ") assert s.find("tst_utils.py line ") >= 0 else: raise Exception_expected exercise_indented_display() exercise_approx_equal() exercise_file_utils() exercise_dir_utils() print utils.format_cpu_times()
def run(args): assert len(args) == 0 if '--exercise-retrieve-unless-exists' in args: exercise_retrieve_unless_exists() else: print('Skipping exercise_retrieve_unless_exists') exercise_misc() assert utils.sequence_index_dict(["a", "b"]) == {"a": 0, "b": 1} assert utils.flat_list(0) == [0] assert utils.flat_list([1,2,3]) == [1,2,3] assert utils.flat_list([1,[2,3,4],3]) == [1,2,3,4,3] assert utils.flat_list([1,[2,3,4],[[3,4],[5,6]]]) == [1,2,3,4,3,4,5,6] try: raise RuntimeError("Trial") except KeyboardInterrupt: raise except Exception: assert utils.format_exception() == "RuntimeError: Trial" else: raise Exception_expected try: assert 1 == 2 except KeyboardInterrupt: raise except Exception: s = utils.format_exception() assert s.startswith("AssertionError: ") assert s.find("tst_utils.py line ") >= 0 else: raise Exception_expected exercise_indented_display() exercise_approx_equal() exercise_file_utils() exercise_dir_utils() exercise_group_args() exercise_round2() print(utils.format_cpu_times())
def run(args): assert len(args) == 0 if '--exercise-retrieve-unless-exists' in args: exercise_retrieve_unless_exists() else: print 'Skipping exercise_retrieve_unless_exists' exercise_forward_compatibility() exercise_misc() assert utils.sequence_index_dict(["a", "b"]) == {"a": 0, "b": 1} assert utils.flat_list(0) == [0] assert utils.flat_list([1,2,3]) == [1,2,3] assert utils.flat_list([1,[2,3,4],3]) == [1,2,3,4,3] assert utils.flat_list([1,[2,3,4],[[3,4],[5,6]]]) == [1,2,3,4,3,4,5,6] try: raise RuntimeError("Trial") except KeyboardInterrupt: raise except Exception: assert utils.format_exception() == "RuntimeError: Trial" else: raise Exception_expected try: assert 1 == 2 except KeyboardInterrupt: raise except Exception: s = utils.format_exception() assert s.startswith("AssertionError: ") assert s.find("tst_utils.py line ") >= 0 else: raise Exception_expected exercise_indented_display() exercise_approx_equal() exercise_file_utils() exercise_dir_utils() print utils.format_cpu_times()
def process_cif(self, file_name): try: cif_object = read_cif(file_name=file_name) except KeyboardInterrupt: raise except Exception: raise Sorry( "Error reading CIF file:\n" " %s\n" " (%s)" % (show_string(file_name), format_exception())) self.process_cif_object(cif_object=cif_object, file_name=file_name)
def process_cif(self, file_name, cache=True): try: cif_object = read_cif(file_name=file_name) except KeyboardInterrupt: raise except Exception: raise Sorry( "Error reading CIF file:\n" " %s\n" " (%s)" % (show_string(file_name), format_exception())) self.process_cif_object(cif_object=cif_object, file_name=file_name, cache=cache, )
def process_cif_object(self, cif_object, file_name=None): if (file_name is None): source_info = None else: source_info = "file: "+file_name try: self.convert_all(source_info=source_info, cif_object=cif_object) except KeyboardInterrupt: raise except Exception: if (file_name is None): file_name = "(file name not available)" raise Sorry( "Error processing CIF file:\n" " %s\n" " (%s)" % (show_string(file_name), format_exception()))
def process_cif_object(self, cif_object, file_name=None, cache=True): if (file_name is None): source_info = None else: source_info = "file: "+file_name try: self.convert_all(source_info=source_info, cif_object=cif_object, cache=cache, ) except KeyboardInterrupt: raise except Exception: if (file_name is None): file_name = "(file name not available)" raise Sorry( "Error processing CIF file:\n" " %s\n" " (%s)" % (show_string(file_name), format_exception()))
def selection(self, string, optional=True, contiguous_word_characters=None, callback=None): """ Given a selection string, return the corresponding flex.bool selection of the same size as root.atoms(). """ try: return self.selection_parser( word_iterator=self.selection_tokenizer( string=string, contiguous_word_characters=contiguous_word_characters), optional=optional, callback=callback) except (AtomSelectionError, KeyboardInterrupt): raise except Exception: msg = format_exception().splitlines() msg.extend(["Atom selection string leading to error:\n %s" % string]) raise AtomSelectionError("\n".join(msg))
def process_arg(self, arg): try: params = libtbx.phil.parse( input_string=arg, source_info=self.argument_description+"argument") except RuntimeError: raise Sorry(( 'Error interpreting %sargument as parameter definition:\n' ' "%s"\n %s') % (self.argument_description, arg, format_exception())) if (self.target_paths is None): self.target_paths = [object_locator.path for object_locator in self.master_phil.all_definitions()] source_definitions = params.all_definitions() complete_definitions = "" for object_locator in source_definitions: object = object_locator.object scores = [self.get_path_score(object_locator.path, target_path) for target_path in self.target_paths] max_score = max(scores) if (max_score == 0): raise Sorry("Unknown %sparameter definition: %s" % ( self.argument_description, object.as_str().strip())) if (scores.count(max_score) > 1): error = ["Ambiguous parameter definition: %s" % object.as_str().strip()] error.append("Best matches:") for target_path,score in zip(self.target_paths, scores): if (score == max_score): error.append(" " + target_path) raise Sorry("\n".join(error)) complete_definitions += object.customized_copy( name=self.target_paths[scores.index(max_score)]).as_str() if (complete_definitions == ""): raise Sorry(('%sparameter definition has no effect: "%s"' % ( self.argument_description, arg)).capitalize()) return libtbx.phil.parse( input_string=complete_definitions, source_info=self.argument_description+"argument")
def process_arg(self, arg): try: params = libtbx.phil.parse(input_string=arg, source_info=self.argument_description + "argument") except RuntimeError: raise Sorry( ('Error interpreting %sargument as parameter definition:\n' ' "%s"\n %s') % (self.argument_description, arg, format_exception())) if (self.target_paths is None): self.target_paths = [ object_locator.path for object_locator in self.master_phil.all_definitions() ] def recursive_expert_level(phil_obj): if hasattr(phil_obj.object, 'expert_level' ) and phil_obj.object.expert_level is not None: return phil_obj.object.expert_level if not hasattr(phil_obj, 'parent') or not phil_obj.parent: return 0 def parent_expert_level(obj): if hasattr(obj, 'expert_level') and obj.expert_level is not None: return obj.expert_level if hasattr( obj, 'primary_parent_scope') and obj.primary_parent_scope: return parent_expert_level(obj.primary_parent_scope) return 0 return parent_expert_level(phil_obj.parent) expert_level = [ recursive_expert_level(object_locator) for object_locator in self.master_phil.all_definitions() ] source_definitions = params.all_definitions() complete_definitions = "" for object_locator in source_definitions: object = object_locator.object scores = [ self.get_path_score(object_locator.path, target_path) for target_path in self.target_paths ] max_score = max(scores) if (max_score == 0): raise Sorry( "Unknown %sparameter definition: %s" % (self.argument_description, object.as_str().strip())) if (scores.count(max_score) > 1): error = [ "Ambiguous parameter definition: %s" % object.as_str().strip() ] error.append("Best matches:") for target_path, score in zip(self.target_paths, scores): if (score == max_score): error.append(" " + target_path) # Calculate and apply tie-breaker value depending on expert level. # Arguments with lower expert level are preferentially # chosen if otherwise they would be ambiguous. scores = [ score - (exp_lvl / 100) for score, exp_lvl in zip(scores, expert_level) ] max_score = max(scores) if (scores.count(max_score) > 1): raise Sorry("\n".join(error)) print("Warning: " + "\n".join(error) + "\nAssuming %s was intended." % self.target_paths[scores.index(max_score)]) complete_definitions += object.customized_copy( name=self.target_paths[scores.index(max_score)]).as_str() if (complete_definitions == ""): raise Sorry(('%sparameter definition has no effect: "%s"' % (self.argument_description, arg)).capitalize()) return libtbx.phil.parse(input_string=complete_definitions, source_info=self.argument_description + "argument")