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 atomic mass # 04 previous in group # 05 next in group # 06 importance # 07 availability # 08 group number # # ungrouped by IUPAC are # given group 19 # #%#%#%#%#%#%#%#%#%#%#%#%#%#%%#%#%#%#%# # name = thisentry[2] # name of element (Iron) symbol = thisentry[1] # element symbol (Fe) number = thisentry[0] # atomic number (26) mass = thisentry[3] # atomic mass (55.845) gprev = thisentry[4] # up one (none) gnext = thisentry[5] # down one (Ruthenium) imp = thisentry[6] # importance (important) avail = thisentry[7] # availability (ubiquitous) group = thisentry[8] # in this group (8) # #%#%#%#%#%#%#%#%#%#%#%#%#%#%%#%#%#%#%# # # 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] to_out += ["mass=" + str(float(mass))] to_out += ["group=" + group] to_out += ["period="] to_out += ["phase="] to_out += ["series="] to_out += ["density="] to_out += ["melts="] to_out += ["boils="] 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)] # Tidily install pipes for the table to_out = tidypipes(to_out) to_out += ["}}"] to_out +=["{{Script Test}}"] to_out += ["'''"+name+"''' is a "+"''(type of element)''"+" in the "+"''(series)''"+" series."] 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 "N/A" 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, 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 += [' <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_H125a.csv") gotit = StartXML() #print DB[26][2] #print DB[6][2] gotit += ArtXML("Lunarpedia:Autostub2 test 2a","Autostub2",dostub(DB[26])) gotit += ArtXML("Lunarpedia:Autostub2 test 2b","Autostub2",dostub(DB[6])) 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/autostub2test2_3.xml', 'w') do_xml.write(outdone) do_xml.close()