我们从Python开源项目中,提取了以下2个代码示例,用于说明如何使用sqlalchemy.func.abs()。
def acquisitions_by_mapgrid_and_date(date, mapgrid_target, day_buffer): from sqlalchemy import Integer, func, Date from sqlalchemy.sql.expression import cast session = SESSION_MAKER() images_paths = session.query(RawProduct.product_path, RapidEyeFootPrintsMexicoOld.code, RapidEyeFootPrintsMexicoOld.mapgrid2).distinct().join(RawProduct.information).filter(RawProduct.satellite_id == 1, RapidEyeFootPrintsMexicoOld.mapgrid2 == mapgrid_target, cast(RapidEyeFootPrintsMexicoOld.code, Integer) == cast(Information.grid_id, Integer), func.abs(cast(RawProduct.acquisition_date, Date) - date) < day_buffer).all() #RawProduct.sensor_id return images_paths
def transferred_amount(from_account, to_account, begin_date=None, end_date=None): """ Determine how much has been transferred from one account to another in a given interval. A negative value indicates that more has been transferred from to_account to from_account than the other way round. The interval boundaries may be None, which indicates no lower and upper bound respectively. :param FinanceAccount from_account: :param FinanceAccount to_account: :param date|None begin_date: since when (inclusive) :param date|None end_date: till when (inclusive) :rtype: int """ split1 = aliased(Split) split2 = aliased(Split) query = session.session.query( cast(func.sum( sign(split2.amount) * least(func.abs(split1.amount), func.abs(split2.amount)) ), Integer) ).select_from( split1 ).join( (split2, split1.transaction_id == split2.transaction_id) ).join( Transaction, split2.transaction_id == Transaction.id ).filter( split1.account == from_account, split2.account == to_account, sign(split1.amount) != sign(split2.amount) ) if begin_date is not None and end_date is not None: query = query.filter( between(Transaction.valid_date, begin_date, end_date) ) elif begin_date is not None: query = query.filter(Transaction.valid_date >= begin_date) elif end_date is not None: query = query.filter(Transaction.valid_date <= end_date) return query.scalar()