# create a class of object, which in my case # is a multiple system (can be single or have # up to n components) class System_mss(object): """A multiple stellar system (can be single as well as contain multiple components) """ # initialise name def __init__(self, name): self.name = name # now define method. Method takes one argument # 'coord_t' which is just a flag def coords(self, coord_t): """Returns the coordinates of the target. Takes an arguement either 's' or 'd' to signify whether it should return the coordinates in sexagesimal or decimal format """ # assign self.name to a variable to use in the SQL query obj_name = self.name # Get coordinates in either sexigesimal or decimal depending # on the input flag. Below I assign the column names for the # query on this basis if coord_t == "s": ra_col, dec_col = "ra_s", "dec_s" elif coord_t == "d": ra_col, dec_col = "ra_deg", "dec_deg" else: ra_col, dec_col = "ra_s", "dec_s" # columns names for query of database sys_id_col, comp_col, c_id_col = "systemid", "comp", "common_id" # table name tab_1 = "ncomp_tab" # SQL query using python variables. Query is just a string containing # the SQL syntax. c.execute("SELECT %s, %s, %s, %s, %s FROM %s WHERE systemid='%s'" % \ (sys_id_col, comp_col, ra_col, dec_col, c_id_col, tab_1, obj_name)) # c.fetchall() returns all the results from the last query result = c.fetchall() # ********************************************************************** # ********************************************************************** # now print these results out to the terminal in a formatted way for # easy readibility print "\nComp. RA DEC ID" print "---------------------------------------------------------------" for n in result: if coord_t == "d": print "%s %.7f %.7f %s" % (n[1], n[2], n[3], n[4]) else: print "%s %s %s %s" % (n[1], n[2], n[3], n[4])