Difference between revisions of "Lunarpedia talk:Autostub2"

From Lunarpedia
Jump to: navigation, search
(source code for script)
 
(replaced source code with version that produced test 3)
Line 33: Line 33:
 
     #                                     
 
     #                                     
 
     # database: 00 Atomic number           
 
     # database: 00 Atomic number           
     #          01 symbol                  
+
     #          01 Symbol                  
     #          02 name                    
+
     #          02 Name                    
     #          03 atomic mass           
+
     #          03 Availability         
     #          04 previous in group     
+
     #          04 Importance           
     #          05 next in group         
+
     #          05 Group                 
     #          06 importance              
+
     #          06 Period               
     #          07 availability            
+
    #          07 Series               
     #          08 group number            
+
    #          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     
 
     #                                     
 
     #                                     
     #          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[ 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: 
 
     #                                     
 
     #                                     
    #%#%#%#%#%#%#%#%#%#%#%#%#%#%%#%#%#%#%#
 
                                        #
 
    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  = []
 
     to_out += ["{{Test Element      "]
 
     to_out += ["{{Test Element      "]
Line 68: Line 110:
 
     to_out += ["need=" + imp]
 
     to_out += ["need=" + imp]
 
     to_out += ["number=" + number]
 
     to_out += ["number=" + number]
     to_out += ["mass=" + str(float(mass))]
+
     if (mass[0] == "["):
 +
      to_out += ["mass=" + mass]
 +
    else:
 +
        to_out += ["mass=" + str(float(mass))]
 
     to_out += ["group=" + group]
 
     to_out += ["group=" + group]
     to_out += ["period="]
+
     to_out += ["period=" + period]
     to_out += ["phase="]
+
     to_out += ["phase=" + phase]
     to_out += ["series="]
+
     to_out += ["series=" + series]
     to_out += ["density="]
+
     to_out += ["density=" + density]
     to_out += ["melts="]
+
     to_out += ["melts=" + melt]
     to_out += ["boils="]
+
     to_out += ["boils=" + boil]
     to_out += ["isotopes="]
+
     #to_out+= ["isotopes="]
 
     to_out += ["prior="+get_element(number,-1)]
 
     to_out += ["prior="+get_element(number,-1)]
 
     to_out += ["next="+get_element(number,1)]
 
     to_out += ["next="+get_element(number,1)]
Line 85: Line 130:
 
     to_out += ["bprior="+get_element(gnext,-1)]
 
     to_out += ["bprior="+get_element(gnext,-1)]
 
     to_out += ["bnext="+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  
 
     # Tidily install pipes for the table  
Line 92: Line 153:
  
 
     to_out +=["{{Script Test}}"]     
 
     to_out +=["{{Script Test}}"]     
      
+
     if group != "19":   
    to_out += ["'''"+name+"''' is a "+"''(type of element)''"+" in the "+"''(series)''"+" series."]
+
      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 += ["<BR/><BR/>\n\n"]
 
     to_out += [""]
 
     to_out += [""]
Line 160: Line 225:
 
     try:
 
     try:
 
         number = int(number)
 
         number = int(number)
     except: return "N/A"
+
     except: return "<SMALL>N/A</SMALL>"
 
     return "[[Mediawiki:Sandbox|"+str(DB[number+int(shift)][1])+"]]"
 
     return "[[Mediawiki:Sandbox|"+str(DB[number+int(shift)][1])+"]]"
  
Line 180: Line 245:
 
     return out
 
     return out
  
def ArtXML(title, contrib, text):
+
def ArtXML(title, contrib, date, text):
 
     '''XML markup for article in file
 
     '''XML markup for article in file
 
     title --  title of article
 
     title --  title of article
Line 189: Line 254:
 
     out +=  ['        <title>'+title+'</title>\n']
 
     out +=  ['        <title>'+title+'</title>\n']
 
     out +=  ['        <revision>\n']
 
     out +=  ['        <revision>\n']
 +
    out +=  ['            <timestamp>'+date+'</timestamp>']
 
     out +=  ['            <contributor>\n']
 
     out +=  ['            <contributor>\n']
 
     out +=  ['                <username>'+contrib+'</username>\n']
 
     out +=  ['                <username>'+contrib+'</username>\n']
 
     out +=  ['            </contributor>\n']
 
     out +=  ['            </contributor>\n']
     # out += ['            <text xml:space="preserve">'+text+'</text>']
+
     #out += ['            <text xml:space="preserve">'+text+'</text>']
 
     out +=  ['            <text xml:space="preserve">']
 
     out +=  ['            <text xml:space="preserve">']
 
     out +=  text
 
     out +=  text
Line 231: Line 297:
 
#What=dostub(Item)
 
#What=dostub(Item)
  
DB=TSVinput("/home/Luna/Elements_H125a.csv")
+
DB=TSVinput("/home/Luna/Elements_H206.csv")
  
 
gotit =  StartXML()
 
gotit =  StartXML()
 
#print DB[26][2]
 
#print DB[26][2]
 
#print DB[6][2]
 
#print DB[6][2]
gotit += ArtXML("Lunarpedia:Autostub2 test 2a","Autostub2",dostub(DB[26]))
+
gotit += ArtXML("Lunarpedia:Autostub2 test 3a","Autostub2","2007-02-08T12:00:00Z",dostub(DB[ 10]))
gotit += ArtXML("Lunarpedia:Autostub2 test 2b","Autostub2",dostub(DB[6]))
+
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()
 
gotit += EndXML()
  
Line 259: Line 331:
 
outdone = Stringify(gotit)
 
outdone = Stringify(gotit)
  
do_xml=open('/home/Luna/autostub2test2_3.xml', 'w')
+
do_xml=open('/home/Luna/autostub2test3.xml', 'w')
 
do_xml.write(outdone)
 
do_xml.write(outdone)
 
do_xml.close()
 
do_xml.close()
 +
  
  
  
 
</PRE></TT>
 
</PRE></TT>

Revision as of 15:17, 8 February 2007

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()