def write(self, a=None, attr=None, name=None, *args, **kwargs): ''' Identical to the function in class Molecule of same name ... could move this to Object_Files, but a bit tricky this is supposed to be a versitile tool for writing to the Molecule's data file. Whether the added intricacy will be worth the lines of code it saves, I can't tell yet. Writes in one of the following ways: 1. If the name of an attribute is given, that attribute is written. 2. If a is a string, simply write a to the molecule's datafile. 3. If a is a function, then it's a function that writes what you want to the data file given in the first argument 4. If a is a dictionary, list, or tuple, convert it to lines according to the system encoded in Object_Files. 5. If a is not given but keyword arguments are, write **kwargs to the data file according to the system encoded in Object_Files. ''' if name is None: name = self.name if attr is not None: a = (attr, getattr(self, attr)) elif a is None: if len(kwargs) == 0: print('nothing to write.') return else: a = kwargs cwd = os.getcwd() os.chdir(data_directory) file_name = name + '.txt' with open(file_name, 'a') as f: if callable(a): a(f, *args, **kwargs) elif type(a) is str: if a[-1] != '\n': a += '\n' f.write(a) elif type(a) in (list, dict): if 'key' in kwargs.keys(): lines = structure_to_lines(a, preamble=kwargs['key']) else: lines = structure_to_lines(a) f.writelines(lines) elif type(a) is tuple: lines = structure_to_lines(a[1], preamble=a[0]) f.writelines(lines) else: print('Couldn' 't write ' + str(a)) os.chdir(cwd)
def write_calibration(self, f, calibration): ''' Writes a calibration dictionary to the molecule's data file (pre-opened as 'f') in a way readable by read_calibration. Typically called by add_calibration. There's a much cleverer way to write this type of function. ''' print('\nWriting calibration for ' + self.name + '\n') title_line = 'calibration_' + calibration['title'] lines = structure_to_lines(calibration, preamble=title_line) f.writelines(lines) print('wrote calibration ' + calibration['title'] + ' for ' + self.name)
f = open("color_table.txt", "w") f.write(header) for (mass, color) in ourcolors_0.items(): print(mass + " : " + color) m = int(mass[1:]) ax1.barh(m, 1, align="center", color=color) rgb = ColorConverter.to_rgb(color) # why do I have to do this? html = colors.rgb2hex(rgb) ourcolors[mass] = (color, rgb, html) rgb = [np.round(a, 3) for a in rgb] f.write(line_0.format(mass, color, rgb[0], rgb[1], rgb[2], str(html))) f.close() plt.savefig("colors_for_cinfdata.png") makeECMSfile = True if makeECMSfile: import os cwd = os.getcwd() os.chdir("..") from Object_Files import structure_to_lines os.chdir(cwd) lines = structure_to_lines(ourcolors_0, preamble="standard colors") g = open("standard_colors.txt", "w") g.writelines(lines) g.close()