def organise_users(self, store): expr = re.compile("user([0-9]+)$") for param in store.kw: match = expr.match(param) if match and int(match.group(1)) < 21: store.users.add(store.kw[param].strip()) store.users = {ucfirst(u.strip()) for u in store.users} store.users = {u for u in store.users if u} store.users = tuple(store.users)
def tcard(): stat = {} for content_item in db.query(ContentItem).filter(ContentItem.type == "tcard_trip", ContentItem.created_at >= start, ContentItem.created_at <= end): if content_item.data["RouteType"] == u"метро": key = u"Метро «" + { "MARKS" : u"Площадь Маркса", "STUD" : u"Студенческая", "RECHV" : u"Речной вокзал", "OKT" : u"Октябрьская", "LENIN" : u"Площадь Ленина", "KR-PR" : u"Красный проспект", "GAGAR" : u"Гагаринская", "ZAELC" : u"Заельцовская", "G-M" : u"Площадь Гарина-Михайловского", "POKR" : u"Маршала Покрышкина", "BEREZ" : u"Берёзовая роща", "NIVA" : u"Золотая Нива", }[content_item.data["RouteNum"]] + u"»" else: key = ucfirst(content_item.data["RouteType"].split()[1]) + u" " + content_item.data["RouteNum"] if key not in stat: stat[key] = [] stat[key].append(content_item.data["Summa"] / 100) if stat: text = u"Потратил на проезд %s (%s). Топ транспорта:\n" % ( pytils.numeral.get_plural(sum([sum(l) for l in stat.values()]), (u"рубль", u"рубля", u"рублей")), pytils.numeral.get_plural(sum([len(l) for l in stat.values()]), (u"поездка", u"поездки", u"поездок")), ) text += u"<ul>\n" for transport, amounts in sorted(stat.items(), key=lambda kv: -len(kv[1]))[:5]: text += u"<li>%s (%s)</li>\n" % (transport, pytils.numeral.get_plural(len(amounts), (u"поездка", u"поездки", u"поездок"))) text += u"</ul>\n" return text else: return None
def set_constants(self): super().set_constants() store = self.store store.page = ucfirst(store.page) store.titleparts = self.titleparts(store.page) try: store.startdate = int(store.startdate) * 1000000 except: store.startdate = 0 try: store.enddate = int(store.enddate) * 1000000 except: store.enddate = 0 if isinstance(store.users, list): store.users = set(store.users) elif isinstance(store.users, str): store.users = {store.users} else: store.users = set() self.organise_users(store)
def time_at_home(): min_out_threshold = timedelta(minutes=5) outs = get_outs(start, end) time_out = sum([came - left for left, came in outs], timedelta()) time_in = (end - start) - time_out looser_days = 0 total_days = 0 day = start while day < end: found_out = False for left, came in outs: if came - left > min_out_threshold and day.date() in [left.date(), came.date()]: found_out = True break if not found_out: entire_day_out = False for left, came in outs: if left < day and day < came: entire_day_out = True break if not entire_day_out: looser_days += 1 day += timedelta(days=1) total_days += 1 out_times = sum([1 if came - left > min_out_threshold else 0 for left, came in outs]) return u"%(percent_time_at_home)d%% времени дома. Выходил на улицу %(out_times)s. %(looser_days)s из %(total_days)s имел «счастье» никуда не ходить вообще." % { "percent_time_at_home" : time_in.total_seconds() / (end - start).total_seconds() * 100, "out_times" : pytils.numeral.get_plural(out_times, (u"раз", u"раза", u"раз")), "looser_days" : ucfirst(pytils.numeral.sum_string(looser_days, 1, (u"день", u"дня", u"дней"))), "total_days" : u" ".join([pymorphy2.MorphAnalyzer().parse(word)[0].inflect({"gent"}).word for word in pytils.numeral.in_words(total_days).split()]), }
def typeParser(name) : name = utils.ucfirst(name)+"Type" return getattr(types,name)
text += u"<li>%s (%s)</li>\n" % (transport, pytils.numeral.get_plural(len(amounts), (u"поездка", u"поездки", u"поездок"))) text += u"</ul>\n" return text else: return None report = u"<p>Вот как прошёл у меня этот месяц:<cut></p>\n<ul>\n" for f in report_items: item = f() if item: report += u"<li>" + item + u"</li>\n" report += u"</ul>\n" if sys.argv[2] == "post": title = ucfirst(pytils.dt.ru_strftime(u"%B %Y", date=start)) now_playing = all_social_services["last.fm"].network.get_user(all_social_services["last.fm"].username).get_now_playing() if now_playing: music = u"%s – %s" % (now_playing.get_artist().get_name(), now_playing.get_title()) else: music = "" post = ContentItem() post.type = "blog_post" post.type_key = pytils.translit.slugify(title) post.created_at = datetime.now() post.permissions = ContentItem.permissions_PUBLIC post.data = { "title" : title, "title_html" : "",