def create(cls, from_unit, to_unit, factor): with scoped_session() as session: obj0 = cls(from_unit=from_unit, to_unit=to_unit, factor=factor) obj1 = cls(from_unit=to_unit, to_unit=from_unit, factor=1/factor) session.add(obj0) session.add(obj1) reconfigure_units()
def _getitem(cls, key): if isinstance(key, tuple): f = cls.aliases[key[0].lower()] if key[0].lower() in cls.aliases else key[0] t = cls.aliases[key[1].lower()] if key[1].lower() in cls.aliases else key[1] with scoped_session() as session: return session.query(cls).filter(and_(cls.from_unit==f, cls.to_unit==t)).one().factor else: raise KeyError('Usage requires syntax Dimension["from_unit", "to_unit"]')
def test_unit_creation(self): """Test :func:`~exa.cms.unit.Dimension.create`.""" Length.create("xyz", "zyx", 1) self.assertEqual(unit.units[('xyz', 'zyx')], 1) self.assertEqual(unit.units[('zyx', 'xyz')], 1) with scoped_session() as session: session.query(Length).filter(Length.from_unit=="xyz").delete() session.query(Length).filter(Length.from_unit=="zyx").delete()
def get_by_symbol(cls, symbol): """Get the value of a constant with the given symbol.""" if symbol.lower() in cls.aliases: symbol = cls.aliases[symbol.lower()] with scoped_session() as session: return session.query(cls).filter(cls.symbol == symbol).one().value
def init_tutorial(): """Create the tutorial notebook.""" fp = os.path.join(config['paths']['notebooks'], 'tutorial.ipynb') with scoped_session() as session: tutorial = File.from_path(fp) session.add(tutorial)