Lunarpedia talk:Autostub2
Database
James Rogers is working on improving the data set. -- Strangelv 10:51, 2 February 2007 (PST)
Source Code
Revision as of Test 2, with successful generation of an XML file for importation. Please note that the <PRE> tag fails to stop the ampersand markup from being converted into symbols that the importer will choke on if the code is run as is. -- Strangelv 10:51, 2 February 2007 (PST)
#%#%#%#%#%#%#%#%#%#%#%#%#%#%#%# # # # Element Autostub Generator # # # # Public Domain # # # #%#%#%#%#%#%#%#%#%#%#%#%#%#%#%# def linebrk(listish): newlist = [] for q in listish: newlist += [q+"\n"] return newlist def dostub(thisentry): """Create stub article from database """ # # Set up variables to generate # the element article stub # # database: 00 Atomic number # 01 Symbol # 02 Name # 03 Availability # 04 Importance # 05 Group # 06 Period # 07 Series # 08 Normal Phase # 09 Mass # 10 Density # 11 Melt Point # 12 Boil Point # 13 Above Prev # 14 Above # 15 Above Next # 16 Prev # 17 Next # 18 Below Prev # 19 Below # 20 Below Next # 21 Atomic Radius (pm) # 22 Bohr Atomic Radius (pm) # 23 Covalent Radius (pm) # 24 Van der Waals Radius (pm) # 25 Ionic Radius (pm) # 26 1st Ion Potential (energy required to remove one electron, forming a cation, measured in eV # 27 Electron Shell Configuration # 28 Electrons per Shell # 29 Electronegativity # 30 Electron Affinity # 31 Oxidation States # 32 Magnetism # 33 Crystal Structure # #%#%#%#%#%#%#%#%#%#%#%#%#%#%%#%#%#%#%# # name = thisentry[ 2] # name of element (Iron) symbol = thisentry[ 1] # element symbol (Fe) number = thisentry[ 0] # atomic number (26) mass = thisentry[ 9] # atomic mass (55.845) gprev = thisentry[14] # up one (none) gnext = thisentry[19] # down one (Ruthenium) imp = thisentry[ 4] # importance (important) avail = thisentry[ 3] # availability (ubiquitous) group = thisentry[ 5] # in this group (8) # phase = thisentry[ 8] # normal phase density = thisentry[10] # density melt = thisentry[11] # melt point boil = thisentry[12] # boil point period = thisentry[ 6] # period series = thisentry[ 7] # series arad = thisentry[21] # atomic radius brad = thisentry[22] # Bohr atomic radius crad = thisentry[23] # covalant radius vrad = thisentry[24] # Van der Waals radius irad = thisentry[25] # Ionic Radius fionp = thisentry[26] # First Ion Potential elesh = thisentry[27] # electron shell config epersh = thisentry[28] # electrons per shell eneg = thisentry[29] # electronegativity eaff = thisentry[30] # electron affinity oxid = thisentry[31] # oxidation states mag = thisentry[32] # magnetism cry = thisentry[33] # crystalline structure # #%#%#%#%#%#%#%#%#%#%#%#%#%#%%#%#%#%#%# # # start generating the article here: # to_out = [] to_out += ["{{Test Element "] to_out += ["name=" + name] to_out += ["symbol=" + symbol] to_out += ["available=" + avail] to_out += ["need=" + imp] to_out += ["number=" + number] if (mass[0] == "["): to_out += ["mass=" + mass] else: to_out += ["mass=" + str(float(mass))] to_out += ["group=" + group] to_out += ["period=" + period] to_out += ["phase=" + phase] to_out += ["series=" + series] to_out += ["density=" + density] to_out += ["melts=" + melt] to_out += ["boils=" + boil] #to_out+= ["isotopes="] to_out += ["prior="+get_element(number,-1)] to_out += ["next="+get_element(number,1)] to_out += ["above="+get_element(gprev,0)] to_out += ["aprior="+get_element(gprev,-1)] to_out += ["anext="+get_element(gprev,1)] to_out += ["below="+get_element(gnext,0)] to_out += ["bprior="+get_element(gnext,-1)] to_out += ["bnext="+get_element(gnext,1)] to_out += ["radius=" + arad] to_out += ["bohr=" + brad] to_out += ["covalent=" + crad] to_out += ["vdwr=" + vrad] to_out += ["irad=" + irad] to_out += ["ipot=" + fionp] to_out += ["econfig=" + elesh] to_out += ["eshell=" + epersh] to_out += ["enega=" + eneg] to_out += ["eaffin=" + eaff] to_out += ["oxstat=" + oxid] to_out += ["magn=" + mag] to_out += ["cryst=" + cry] # Tidily install pipes for the table to_out = tidypipes(to_out) to_out += ["}}"] to_out +=["{{Script Test}}"] if group != "19": to_out += ["'''"+name+"''' is a "+series+" in group "+group+"."] else: to_out += ["'''"+name+"''' is a "+series+" metal."] if cry: to_out += ["It has a "+cry+" crystalline structure."] to_out += ["<BR/><BR/>\n\n"] to_out += [""] to_out += [""] to_out += [""] to_out += ["{"+"{Test Autostub}"+"}"] # to_out += docategories(typecat) #["[[Category:"+typecat+"]]"] to_out += ['<DIV ID="catlinks"><P CLASS="catlinks">[[Special:Categories&article=Lunarpedia%3AAutostub2_test_2|Categories]]: [[:Category:Stubs|Stubs]] | [[:Category:Elements|Elements]]'] to_out += ["</P></DIV><!-- fake category box -- see commented out text above for actual script output -->"] to_out += ["<!-- Generated by a too-early version of Autostub2 -->"] to_out = linebrk(to_out) return to_out # def main(): # # load database # didxml = templatetop() # for q in database: didxml += dostub(q) + templatemid(); print ".", # didxml += templatefin() # # save didxml def tidypipes(table): """takes a list oy strings, finds the longest one, and tidily adds pipes to the right of each line. maxlen: maximum length table: input list of strings table2: output list of strings """ maxlen = 0 for q in table: if len(q)>maxlen: maxlen = len(q) table2 = [] for q in table: qq = q while (len(qq)<maxlen): qq += " " table2 += [qq+" |"] return table2 def replacer(stringie, old, new): """ """ name2="" for q in stringie: if q == old: name2 += new else: name2 += q return name2 def refbreak(ref): """ """ return replacer(ref,";",";<BR/>") def get_element(number,shift): """if a number is a number, return the element symbol corresponding to number+shift """ global DB #if number = "N/A": return "N/A" try: number = int(number) except: return "<SMALL>N/A</SMALL>" return "[[Mediawiki:Sandbox|"+str(DB[number+int(shift)][1])+"]]" ##def (): ## """ ## """ ## return "() not implemented" def StartXML(): out = [['<mediawiki xmlns="http://www.mediawiki.org/xml/export-0.3/" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.mediawiki.org/xml/export-0.3/ http://www.mediawiki.org/xml/export-0.3.xsd" version="0.3" xml:lang="en">\n']] out += [' <siteinfo>\n'] out += [' <sitename>Lunarpedia</sitename>\n'] out += [' </siteinfo>\n'] return out def EndXML(): out = [['</mediawiki>\n']] return out def ArtXML(title, contrib, date, text): '''XML markup for article in file title -- title of article contrib -- name of script (ie Autostub2) text -- the article ''' out = [[' <page>\n']] out += [' <title>'+title+'</title>\n'] out += [' <revision>\n'] out += [' <timestamp>'+date+'</timestamp>'] out += [' <contributor>\n'] out += [' <username>'+contrib+'</username>\n'] out += [' </contributor>\n'] #out += [' <text xml:space="preserve">'+text+'</text>'] out += [' <text xml:space="preserve">'] out += text out += ['</text>\n'] out += [' </revision>\n'] out += [' </page>\n'] return out # TSV sorter # Public Domain def TSVinput(filename): """tab separated database parser """ # Open filename tsv=open(filename) # Convert to list ## get list of lines biglist = tsv.readlines() #for q in tsv: # biglist += q tsv.close() ## parse lines #for q in biglist: print q newlist = [] for q in biglist: newlist += [q.split("\t")] # print max(max(newlist)) # return list return newlist #Item = ["28","Ni","Nickel","58.6934000000","N/A","46","","","10"] #Item = ["26","Fe","Iron","55.8450000000","N/A","44","important","ubiquietous","8"] #Atomic number, symbol, name, atomic mass, previous in group, next in group, importance, availahbility, group number #What=dostub(Item) DB=TSVinput("/home/Luna/Elements_H206.csv") gotit = StartXML() #print DB[26][2] #print DB[6][2] gotit += ArtXML("Lunarpedia:Autostub2 test 3a","Autostub2","2007-02-08T12:00:00Z",dostub(DB[ 10])) gotit += ArtXML("Lunarpedia:Autostub2 test 3b","Autostub2","2007-02-08T12:00:00Z",dostub(DB[ 17])) gotit += ArtXML("Lunarpedia:Autostub2 test 3c","Autostub2","2007-02-08T12:00:00Z",dostub(DB[ 34])) gotit += ArtXML("Lunarpedia:Autostub2 test 3d","Autostub2","2007-02-08T12:00:00Z",dostub(DB[ 51])) gotit += ArtXML("Lunarpedia:Autostub2 test 3e","Autostub2","2007-02-08T12:00:00Z",dostub(DB[ 82])) gotit += ArtXML("Lunarpedia:Autostub2 test 3f","Autostub2","2007-02-08T12:00:00Z",dostub(DB[113])) gotit += ArtXML("Lunarpedia:Autostub2 test 3g","Autostub2","2007-02-08T12:00:00Z",dostub(DB[ 61])) gotit += ArtXML("Lunarpedia:Autostub2 test 3h","Autostub2","2007-02-08T12:00:00Z",dostub(DB[ 92])) gotit += EndXML() def Stringify(listish): stringish = "" for q in listish: if type(q) == type('str'): stringish += q #print "str" elif type(q) == type([]): stringish += Stringify(q) #print "list" else: print type(q) print q raise TypeError, "non-string non-list!!!" return stringish outdone = Stringify(gotit) do_xml=open('/home/Luna/autostub2test3.xml', 'w') do_xml.write(outdone) do_xml.close()