def rename_sample_key(config,mockdb,orig_sample_key,new_sample_key): collection_clses = child_classes(Sample,config=config) + [Sample] for cls in collection_clses: collection = mockdb[cls.__name__] for object_key, object in collection.objects.iteritems(): if object.key == orig_sample_key: object.key = new_sample_key return 1
def test_child_classes(): import re class A(object): pass class B(A): pass class C: pass class D(B,C): pass class E(C): pass clses = [A,B,C,D,E] a_chd = set(child_classes(A,clses)) c_chd = set(child_classes(C,clses)) e_chd = set(child_classes(E,clses)) if e_chd != set([]): print 'child_classes failed empty child test.' print "\t" + str(e_chd) + ' should be empty' if a_chd != set([B,D]): print 'child_classes failed grand-parent test.' print "\t" + str(a_chd) + ' should be ' + str([B,D]) if c_chd != set([D,E]): print 'child_classes failed sibling test.' print "\t" + str(c_chd) + ' should be ' + str([D,E])
def __load__(self,config,no_children = True, base_dir = None, key=None): if base_dir == None: base_dir = config.get('Common_directories','mockdb') first_clses = progenitor_classes(self.cls) if len(first_clses) > 1: raise TypeError("The class {0} cannot be made into a SetOf class.\n".format(cls.__name__)) #Some keyed classes might have children classes. Load these as well unless no_children is flagged if no_children is True: clses = [self.cls] else: clses = [self.cls] + child_classes(self.cls,config=config) for c in clses: db_fname = db_file_name(first_clses[0],self.cls,base_dir) #sys.stderr.write("%s\n" % db_fname) if os.path.isfile(db_fname): keys = extract_db_keys(db_fname) key_index=keys.index('key') with open(db_fname,'r') as f: f.readline() #Gets past header to contents. for line in f: vals = line.rstrip().split(',') if len(vals) != len(keys): raise FormattingError("The formatting in {0} is incorrect. The number of keys and the number of values are different in the following line:\n{1}\n".format(db_fname,str(line))) instance = self.cls(config) for i in range(0,len(vals)): if vals[i] == 'None': setattr(instance,keys[i],None) continue if vals[i] == 'True': setattr(instance,keys[i],True) continue if vals[i] == 'False': setattr(instance,keys[i],False) continue if keys[i] == 'sample_key' or self.cls.__name__ == 'Sample': setattr(instance,keys[i],vals[i]) continue try: int_val = int(vals[i]) setattr(instance,keys[i],int_val) except ValueError: setattr(instance,keys[i],vals[i]) if key != None: if instance.key not in [key]: continue #Load all keys if key is none. Elsewise, just that key. self.objects[instance.key] = instance