Exemplo n.º 1
0
def populate_element_name_table(engine, purge=False):
    if not setup_table(engine, ElementNameProperty, purge):
        return False

    with session_scope(engine) as session:
        ref = require_reference(session, 'wikipedia2016',
                                author='Wikipedia',
                                year=2016)

        for z, name_en in enumerate(NAMES_EN, 1):
            element = require_element(session, z)

            prop = ElementNameProperty(element=element, name=name_en,
                                       language_code='en', reference=ref)
            session.add(prop)

            for lang, name in _find_wikipedia_names(name_en).items():
                prop = ElementNameProperty(element=element, name=name,
                                           language_code=lang, reference=ref)
                session.add(prop)

            logger.debug('Processed {0}'.format(name_en))

        session.commit()

    return True
Exemplo n.º 2
0
def populate_notation_type_table(engine, purge=False):
    if not setup_table(engine, NotationType, purge):
        return False

    with session_scope(engine) as session:
        ref_unattributed = require_reference(session, 'unattributed')

        n = NotationType(name='Siegbahn', reference=ref_unattributed)
        session.add(n)

        n = NotationType(name='Atomic', reference=ref_unattributed)
        session.add(n)

        ref_iupac = \
            require_reference(session, 'jenkins1991',
                              author='Jenkins, R. and Manne, R. and Robin, R. and Senemaud, C.',
                              year='1991',
                              pages='149--155',
                              journal='X-Ray Spectrometry',
                              title='{IUPAC} --- nomenclature system for x-ray spectroscopy',
                              volume='20')
        n = NotationType(name='IUPAC', reference=ref_iupac)
        session.add(n)

        session.commit()

    return True
Exemplo n.º 3
0
    def _query_with_references(self, q, exception, reference=None):
        q = q.add_columns(Reference.bibtexkey)
        q = q.join(Reference)
        q = q.order_by(Reference.id)

        with session_scope(self.engine) as session:
            q = q.with_session(session)

            results = OrderedDict((k, v) for * v, k in q.all())

            # No results from query
            if not results:
                raise exception

            # Check result from specified reference
            if reference is not None:
                if reference in results:
                    return one_or_list(results[reference]), reference
                else:
                    raise exception

            # Check result from preferred references
            for reference in self.reference_priority:
                if reference in results:
                    return one_or_list(results[reference]), reference

            # Return first result
            reference, value = results.popitem(last=False)
            return one_or_list(value), reference
Exemplo n.º 4
0
 def _query_one(self, q, exception):
     with session_scope(self.engine) as session:
         q = q.with_session(session)
         try:
             return one_or_list(q.one())
         except NoResultFound:
             raise exception
Exemplo n.º 5
0
def create_mock_database():
    engine = create_engine('sqlite:///:memory:')

    Base.metadata.create_all(engine)  #@UndefinedVariable

    elements = {}
    data = [(26, 'Fe'), (8, 'O')]
    with session_scope(engine) as session:
        for z, symbol in data:
            e = Element(z=z, symbol=symbol)
            elements[z] = e
            session.add(e)
        session.commit()

    ref1 = Reference(bibtexkey='ref1')
    ref2 = Reference(bibtexkey='ref2')
    data = [(26, 'Iron', 'Eisen', 55.845, 7874.0, ref1),
            (26, 'Iron', 'Eisen', 58.0, 9000.0, ref2),
            (8, 'Oxygen', 'Sauerstoff', 15.9994, 1.429, ref1)]

    with session_scope(engine) as session:
        for z, name_en, name_de, aw, rho, ref in data:
            e = elements[z]

            p = ElementNameProperty(element=e,
                                    language_code='en',
                                    name=name_en,
                                    reference=ref)
            session.add(p)

            p = ElementNameProperty(element=e,
                                    language_code='de',
                                    name=name_de,
                                    reference=ref)
            session.add(p)

            p = ElementAtomicWeightProperty(element=e, value=aw, reference=ref)
            session.add(p)

            p = ElementMassDensityProperty(element=e,
                                           value_kg_per_m3=rho,
                                           reference=ref)
            session.add(p)

        session.commit()

    return engine
Exemplo n.º 6
0
def populate_reference_table(engine, purge=False):
    if not setup_table(engine, Reference, purge):
        return False

    with session_scope(engine) as session:
        ref = Reference(bibtexkey='unattributed')
        session.add(ref)
        session.commit()

    return True
Exemplo n.º 7
0
def create_mock_database():
    engine = create_engine('sqlite:///:memory:')

    Base.metadata.create_all(engine) #@UndefinedVariable

    elements = {}
    data = [(26, 'Fe'), (8, 'O')]
    with session_scope(engine) as session:
        for z, symbol in data:
            e = Element(z=z, symbol=symbol)
            elements[z] = e
            session.add(e)
        session.commit()

    ref1 = Reference(bibtexkey='ref1')
    ref2 = Reference(bibtexkey='ref2')
    data = [(26, 'Iron', 'Eisen', 55.845, 7874.0, ref1),
            (26, 'Iron', 'Eisen', 58.0, 9000.0, ref2),
            (8, 'Oxygen', 'Sauerstoff', 15.9994, 1.429, ref1)]

    with session_scope(engine) as session:
        for z, name_en, name_de, aw, rho, ref in data:
            e = elements[z]

            p = ElementNameProperty(element=e, language_code='en',
                                    name=name_en, reference=ref)
            session.add(p)

            p = ElementNameProperty(element=e, language_code='de',
                                    name=name_de, reference=ref)
            session.add(p)

            p = ElementAtomicWeightProperty(element=e, value=aw, reference=ref)
            session.add(p)

            p = ElementMassDensityProperty(element=e, value_kg_per_m3=rho,
                                           reference=ref)
            session.add(p)

        session.commit()

    return engine
Exemplo n.º 8
0
def populate_atomic_shell_table(engine, purge=False):
    if not setup_table(engine, AtomicShell, purge):
        return False

    with session_scope(engine) as session:
        for n in range(1, 7 + 1):
            s = AtomicShell(principal_quantum_number=n)
            session.add(s)
        session.commit()

    return True
Exemplo n.º 9
0
def populate_element_table(engine, purge=False):
    if not setup_table(engine, Element, purge):
        return False

    with session_scope(engine) as session:
        for z, symbol in enumerate(SYMBOLS, 1):
            element = Element(z=z, symbol=symbol)
            session.add(element)

        session.commit()

    return True
Exemplo n.º 10
0
def populate_atomic_shell_notation_table(engine, purge=False):
    if not setup_table(engine, AtomicShellNotationProperty, purge):
        return False

    with session_scope(engine) as session:
        notation_types = [require_notation(session, 'iupac'),
                          require_notation(session, 'siegbahn'), ]

        for n, notation in enumerate(SHELL_NOTATION, 1):
            shell = require_shell(session, n)

            for notation_type in notation_types:
                p = AtomicShellNotationProperty(atomic_shell=shell,
                                                value=notation,
                                                value_html=notation,
                                                value_latex=notation,
                                                notation_type=notation_type)
                session.add(p)

        session.commit()

    return True
Exemplo n.º 11
0
def populate_element_atomic_weight_table(engine, purge=False):
	if not setup_table(engine, ElementAtomicWeightProperty, purge):
		return False

	with session_scope(engine) as session:
		r = requests.get('http://physics.nist.gov/cgi-bin/Compositions/stand_alone.pl?ele=Li&all=all&ascii=ascii2&isotype=some')
		rdata = r.text
		data = rdata.split('\n')
		current_z = "1"
		value = 0
			
		for l in data:
			z = re.search(r"Atomic\sNumber\s=\s([0-9]*)", l)
			relative_mass = re.search(r"Relative\sAtomic\sMass\s=\s([0-9]*.{0,1}[0-9]*)", l)
			composition = re.search(r"Isotopic\sComposition\s=\s([0-9]*.{0,1}[0-9]*)", l)
			
			if z!=None:
				if current_z != z.group(1):
					current_z = z.group(1)
					if value = 0:
						value = None
					atomicweight = ElementAtomicWeightProperty(value = value)
					session.add(atomicweight)
					value = 0

			elif relative_mass!=None:
				if relative_mass.group(1) == '':
					r_m = 0.0
				else:
					r_m = float(relative_mass.group(1))
							
			elif composition!=None:
				if composition.group(1) == '':
					c = 0.0
				else:
					c = float(composition.group(1))	
				value = value + r_m*c