def ajax_dashboard_per_hour():
arg_year = request.args.get('year', 'all')
arg_month = request.args.get('month', 'all')
arg_artist = request.args.get('artist', '')
count = func.count(Scrobble.id).label('count')
time = Scrobble.played_at
hour = func.extract('hour', time).label('hour')
weekday = func.extract('isodow', time).label('weekday')
year = func.extract('year', time).label('year')
month = func.extract('month', time).label('month')
year_filter = True if arg_year == 'all' else (year == arg_year)
month_filter = True if arg_month == 'all' else (month == arg_month)
artist_filter = True if arg_artist == '' else (Scrobble.artist == arg_artist)
per_hour = (
db.session.query(weekday, hour, count)
.filter(Scrobble.user_id == current_user.id)
.filter(year_filter, month_filter, artist_filter)
.group_by('weekday', 'hour').all()
)
per_hour = [(d, h + 1, v) for d, h, v in per_hour]
return dumps(per_hour)
python类extract()的实例源码
def __init__(self, field, expr, **kwargs):
"""Return a :class:`.Extract` construct.
This is typically available as :func:`.extract`
as well as ``func.extract`` from the
:data:`.func` namespace.
"""
self.type = type_api.INTEGERTYPE
self.field = field
self.expr = _literal_as_binds(expr, None)
def dashboard(period=None):
period, days = PERIODS.get(period, PERIODS['1w'])
col_year = func.extract('year', Scrobble.played_at)
year_from, year_to = db.session.query(func.min(col_year), func.max(col_year)).first()
year_from, year_to = int(year_from), int(year_to)
return render_template(
'dashboard.html',
period=period,
year_min=year_from,
year_max=year_to,
)
def __init__(self, field, expr, **kwargs):
"""Return a :class:`.Extract` construct.
This is typically available as :func:`.extract`
as well as ``func.extract`` from the
:data:`.func` namespace.
"""
self.type = type_api.INTEGERTYPE
self.field = field
self.expr = _literal_as_binds(expr, None)
def __init__(self, field, expr, **kwargs):
"""Return a :class:`.Extract` construct.
This is typically available as :func:`.extract`
as well as ``func.extract`` from the
:data:`.func` namespace.
"""
self.type = type_api.INTEGERTYPE
self.field = field
self.expr = _literal_as_binds(expr, None)
def __init__(self, field, expr, **kwargs):
"""Return a :class:`.Extract` construct.
This is typically available as :func:`.extract`
as well as ``func.extract`` from the
:data:`.func` namespace.
"""
self.type = type_api.INTEGERTYPE
self.field = field
self.expr = _literal_as_binds(expr, None)
def __init__(self, field, expr, **kwargs):
"""Return a :class:`.Extract` construct.
This is typically available as :func:`.extract`
as well as ``func.extract`` from the
:data:`.func` namespace.
"""
self.type = type_api.INTEGERTYPE
self.field = field
self.expr = _literal_as_binds(expr, None)
def __init__(self, field, expr, **kwargs):
"""Return a :class:`.Extract` construct.
This is typically available as :func:`.extract`
as well as ``func.extract`` from the
:data:`.func` namespace.
"""
self.type = type_api.INTEGERTYPE
self.field = field
self.expr = _literal_as_binds(expr, None)
def __init__(self, field, expr, **kwargs):
"""Return a :class:`.Extract` construct.
This is typically available as :func:`.extract`
as well as ``func.extract`` from the
:data:`.func` namespace.
"""
self.type = type_api.INTEGERTYPE
self.field = field
self.expr = _literal_as_binds(expr, None)
def __init__(self, field, expr, **kwargs):
"""Return a :class:`.Extract` construct.
This is typically available as :func:`.extract`
as well as ``func.extract`` from the
:data:`.func` namespace.
"""
self.type = type_api.INTEGERTYPE
self.field = field
self.expr = _literal_as_binds(expr, None)
def __init__(self, field, expr, **kwargs):
"""Return a :class:`.Extract` construct.
This is typically available as :func:`.extract`
as well as ``func.extract`` from the
:data:`.func` namespace.
"""
self.type = type_api.INTEGERTYPE
self.field = field
self.expr = _literal_as_binds(expr, None)
def __init__(self, field, expr, **kwargs):
"""Return a :class:`.Extract` construct.
This is typically available as :func:`.extract`
as well as ``func.extract`` from the
:data:`.func` namespace.
"""
self.type = type_api.INTEGERTYPE
self.field = field
self.expr = _literal_as_binds(expr, None)
def __init__(self, field, expr, **kwargs):
"""Return a :class:`.Extract` construct.
This is typically available as :func:`.extract`
as well as ``func.extract`` from the
:data:`.func` namespace.
"""
self.type = type_api.INTEGERTYPE
self.field = field
self.expr = _literal_as_binds(expr, None)
def __init__(self, field, expr, **kwargs):
"""Return a :class:`.Extract` construct.
This is typically available as :func:`.extract`
as well as ``func.extract`` from the
:data:`.func` namespace.
"""
self.type = type_api.INTEGERTYPE
self.field = field
self.expr = _literal_as_binds(expr, None)
def __init__(self, field, expr, **kwargs):
"""Return a :class:`.Extract` construct.
This is typically available as :func:`.extract`
as well as ``func.extract`` from the
:data:`.func` namespace.
"""
self.type = type_api.INTEGERTYPE
self.field = field
self.expr = _literal_as_binds(expr, None)
def __init__(self, field, expr, **kwargs):
"""Return a :class:`.Extract` construct.
This is typically available as :func:`.extract`
as well as ``func.extract`` from the
:data:`.func` namespace.
"""
self.type = type_api.INTEGERTYPE
self.field = field
self.expr = _literal_as_binds(expr, None)
def __init__(self, field, expr, **kwargs):
"""Return a :class:`.Extract` construct.
This is typically available as :func:`.extract`
as well as ``func.extract`` from the
:data:`.func` namespace.
"""
self.type = type_api.INTEGERTYPE
self.field = field
self.expr = _literal_as_binds(expr, None)
def __init__(self, field, expr, **kwargs):
"""Return a :class:`.Extract` construct.
This is typically available as :func:`.extract`
as well as ``func.extract`` from the
:data:`.func` namespace.
"""
self.type = type_api.INTEGERTYPE
self.field = field
self.expr = _literal_as_binds(expr, None)
def __init__(self, field, expr, **kwargs):
"""Return a :class:`.Extract` construct.
This is typically available as :func:`.extract`
as well as ``func.extract`` from the
:data:`.func` namespace.
"""
self.type = type_api.INTEGERTYPE
self.field = field
self.expr = _literal_as_binds(expr, None)
def __init__(self, field, expr, **kwargs):
"""Return a :class:`.Extract` construct.
This is typically available as :func:`.extract`
as well as ``func.extract`` from the
:data:`.func` namespace.
"""
self.type = type_api.INTEGERTYPE
self.field = field
self.expr = _literal_as_binds(expr, None)
def __init__(self, field, expr, **kwargs):
"""Return a :class:`.Extract` construct.
This is typically available as :func:`.extract`
as well as ``func.extract`` from the
:data:`.func` namespace.
"""
self.type = type_api.INTEGERTYPE
self.field = field
self.expr = _literal_as_binds(expr, None)
def __init__(self, field, expr, **kwargs):
"""Return a :class:`.Extract` construct.
This is typically available as :func:`.extract`
as well as ``func.extract`` from the
:data:`.func` namespace.
"""
self.type = type_api.INTEGERTYPE
self.field = field
self.expr = _literal_as_binds(expr, None)
def __init__(self, field, expr, **kwargs):
"""Return a :class:`.Extract` construct.
This is typically available as :func:`.extract`
as well as ``func.extract`` from the
:data:`.func` namespace.
"""
self.type = type_api.INTEGERTYPE
self.field = field
self.expr = _literal_as_binds(expr, None)
def query_links_by_date(year, month, day, session):
links = session.query(models.Link).join(models.Link.post).filter(
func.extract('year', models.Post.created_at) == year,
func.extract('month', models.Post.created_at) == month,
func.extract('day', models.Post.created_at) == day) \
.all()
return links
def driver():
s = Session()
links = query_links_by_date(2017, 8, 29, s)
for link in links:
tld = tldextract.extract(link.url)
if tld.domain == 'youtube' or tld.domain == 'youtu':
youtube_id = youtube_id_from_url(link.url)
if youtube_id:
save_youtube_id(youtube_id, link)
def __init__(self, field, expr, **kwargs):
"""Return a :class:`.Extract` construct.
This is typically available as :func:`.extract`
as well as ``func.extract`` from the
:data:`.func` namespace.
"""
self.type = type_api.INTEGERTYPE
self.field = field
self.expr = _literal_as_binds(expr, None)
def __init__(self, field, expr, **kwargs):
"""Return a :class:`.Extract` construct.
This is typically available as :func:`.extract`
as well as ``func.extract`` from the
:data:`.func` namespace.
"""
self.type = type_api.INTEGERTYPE
self.field = field
self.expr = _literal_as_binds(expr, None)
def __init__(self, field, expr, **kwargs):
"""Return a :class:`.Extract` construct.
This is typically available as :func:`.extract`
as well as ``func.extract`` from the
:data:`.func` namespace.
"""
self.type = type_api.INTEGERTYPE
self.field = field
self.expr = _literal_as_binds(expr, None)
def __init__(self, field, expr, **kwargs):
"""Return a :class:`.Extract` construct.
This is typically available as :func:`.extract`
as well as ``func.extract`` from the
:data:`.func` namespace.
"""
self.type = type_api.INTEGERTYPE
self.field = field
self.expr = _literal_as_binds(expr, None)
def top_yearly_artists():
scrobbles = func.count(Scrobble.artist).label('count')
charts = {}
col_year = func.extract('year', Scrobble.played_at)
year_from, year_to = db.session.query(func.min(col_year), func.max(col_year)).first()
year_from, year_to = int(year_from), int(year_to)
stat_count = 1000
show_count = 100
for year in range(year_from, year_to + 1):
time_from = datetime.datetime(year, 1, 1)
time_to = datetime.datetime(year, 12, 31, 23, 59, 59, 999999)
charts[year] = (
db.session.query(Scrobble.artist, scrobbles)
.filter(Scrobble.user_id == current_user.id)
.filter(Scrobble.played_at >= time_from, Scrobble.played_at <= time_to)
.group_by(Scrobble.artist)
.order_by(scrobbles.desc())
.limit(stat_count)
.all()
)
position_changes = {}
for year in range(year_from + 1, year_to + 1):
chart = {artist: position for position, (artist, scrobbles) in enumerate(charts[year], 1)}
prev_chart = {
artist: position for position, (artist, scrobbles) in enumerate(charts[year - 1], 1)
}
prev_charts = (chart for chart_year, chart in charts.items() if chart_year < year)
prev_artists = {artist for chart in prev_charts for (artist, scrobbles) in chart}
if year not in position_changes:
position_changes[year] = {}
for artist, data in chart.items():
if artist in prev_chart:
position_changes[year][artist] = prev_chart[artist] - chart[artist]
elif artist not in prev_artists:
position_changes[year][artist] = 'new'
charts = sorted(charts.items())
return render_template(
'charts/top_yearly_artists.html',
charts=charts,
position_changes=position_changes,
show_count=show_count
)