Python sqlalchemy.Interval() Examples
The following are 10
code examples of sqlalchemy.Interval().
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example.
You may also want to check out all available functions/classes of the module
sqlalchemy
, or try the search function
.
Example #1
Source File: helpers.py From planespotter with MIT License | 6 votes |
def is_interval_field(model, fieldname): """Returns ``True`` if and only if the field of `model` with the specified name corresponds to a :class:`datetime.timedelta` object. """ fieldtype = get_field_type(model, fieldname) return isinstance(fieldtype, Interval)
Example #2
Source File: helpers.py From planespotter with MIT License | 6 votes |
def strings_to_dates(model, dictionary): """Returns a new dictionary with all the mappings of `dictionary` but with date strings and intervals mapped to :class:`datetime.datetime` or :class:`datetime.timedelta` objects. The keys of `dictionary` are names of fields in the model specified in the constructor of this class. The values are values to set on these fields. If a field name corresponds to a field in the model which is a :class:`sqlalchemy.types.Date`, :class:`sqlalchemy.types.DateTime`, or :class:`sqlalchemy.Interval`, then the returned dictionary will have the corresponding :class:`datetime.datetime` or :class:`datetime.timedelta` Python object as the value of that mapping in place of the string. This function outputs a new dictionary; it does not modify the argument. """ result = {} for fieldname, value in dictionary.items(): if is_date_field(model, fieldname) and value is not None: if value.strip() == '': result[fieldname] = None elif value in CURRENT_TIME_MARKERS: result[fieldname] = getattr(func, value.lower())() else: value_as_datetime = parse_datetime(value) result[fieldname] = value_as_datetime # If the attribute on the model needs to be a Date object as # opposed to a DateTime object, just get the date component of # the datetime. fieldtype = get_field_type(model, fieldname) if isinstance(fieldtype, Date): result[fieldname] = value_as_datetime.date() elif (is_interval_field(model, fieldname) and value is not None and isinstance(value, int)): result[fieldname] = datetime.timedelta(seconds=value) else: result[fieldname] = value return result
Example #3
Source File: test_types.py From sqlalchemy with MIT License | 6 votes |
def test_date_coercion(self): expr = column("bar", types.NULLTYPE) - column("foo", types.TIMESTAMP) eq_(expr.type._type_affinity, types.NullType) expr = func.sysdate() - column("foo", types.TIMESTAMP) eq_(expr.type._type_affinity, types.Interval) expr = func.current_date() - column("foo", types.TIMESTAMP) eq_(expr.type._type_affinity, types.Interval)
Example #4
Source File: test_types.py From sqlalchemy with MIT License | 6 votes |
def test_interval_coercion(self): expr = column("bar", types.Interval) + column("foo", types.Date) eq_(expr.type._type_affinity, types.DateTime) expr = column("bar", types.Interval) * column("foo", types.Numeric) eq_(expr.type._type_affinity, types.Interval)
Example #5
Source File: test_types.py From sqlalchemy with MIT License | 6 votes |
def setup_class(cls): global interval_table, metadata metadata = MetaData(testing.db) interval_table = Table( "intervaltable", metadata, Column( "id", Integer, primary_key=True, test_needs_autoincrement=True ), Column("native_interval", Interval()), Column( "native_interval_args", Interval(day_precision=3, second_precision=6), ), Column("non_native_interval", Interval(native=False)), ) metadata.create_all()
Example #6
Source File: test_types.py From sqlalchemy with MIT License | 6 votes |
def test_non_native_adapt(self): interval = Interval(native=False) adapted = interval.dialect_impl(testing.db.dialect) assert isinstance(adapted, Interval) assert adapted.native is False eq_(str(adapted), "DATETIME")
Example #7
Source File: eventserver.py From lrrbot with Apache License 2.0 | 5 votes |
def get_last_events(self, request): try: last_event_id = int(request.headers.get('Last-Event-Id', request.query.get('last-event-id'))) except (ValueError, TypeError): last_event_id = None interval = request.query.get('interval') if interval is not None and last_event_id is None: last_event_id = 0 if last_event_id is not None: events = self.metadata.tables['events'] query = sqlalchemy.select([ events.c.id, events.c.event, events.c.data, events.c.time ]) query = query.where(events.c.id > last_event_id) if interval is not None: query = query.where(events.c.time > sqlalchemy.func.current_timestamp() - sqlalchemy.cast(interval, sqlalchemy.Interval)) query = query.order_by(events.c.id) try: with self.engine.begin() as conn: return [ {'id': id, 'event': event, 'data': dict(data, time=time.isoformat())} for id, event, data, time in conn.execute(query) ] except sqlalchemy.exc.DataError as e: raise aiohttp.web.HTTPBadRequest from e return []
Example #8
Source File: test_postgresql.py From alembic with MIT License | 5 votes |
def test_compare_interval_str(self): # this form shouldn't be used but testing here # for compatibility self._compare_default_roundtrip(Interval, "14 days")
Example #9
Source File: test_postgresql.py From alembic with MIT License | 5 votes |
def test_compare_interval_text(self): self._compare_default_roundtrip(Interval, text("'14 days'"))
Example #10
Source File: test_types.py From sqlalchemy with MIT License | 5 votes |
def test_python_type(self): eq_(types.Integer().python_type, int) eq_(types.Numeric().python_type, decimal.Decimal) eq_(types.Numeric(asdecimal=False).python_type, float) eq_(types.LargeBinary().python_type, util.binary_type) eq_(types.Float().python_type, float) eq_(types.Interval().python_type, datetime.timedelta) eq_(types.Date().python_type, datetime.date) eq_(types.DateTime().python_type, datetime.datetime) eq_(types.String().python_type, str) eq_(types.Unicode().python_type, util.text_type) eq_(types.Enum("one", "two", "three").python_type, str) assert_raises( NotImplementedError, lambda: types.TypeEngine().python_type )