Last active
February 26, 2016 17:47
-
-
Save paulozullu/18cb17625e6e6e31f184 to your computer and use it in GitHub Desktop.
Using pyneo, get users whose birthday is this week
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#-*- coding: utf-8 -*- | |
''' | |
Created on 25 de fev de 2016 | |
''' | |
from py2neo import Graph, authenticate | |
import datetime | |
now = datetime.datetime.now() | |
year = datetime.date.today().year | |
this_day = datetime.date.today().day | |
this_month = datetime.date.today().month | |
authenticate("localhost:7474", "", "") | |
graph = Graph() | |
query = "MATCH (all:User) WITH count(all) AS total " | |
query += "MATCH (u:User) RETURN u, total" | |
user_dob = graph.cypher.execute(query) | |
users = [] | |
for user in user_dob: | |
dob_splitted = str(user[0]["dob"]).split("/") | |
day = int(dob_splitted[0]) | |
month = int(dob_splitted[1]) | |
user_week = datetime.date(year, month, day).isocalendar()[1] | |
this_week = datetime.date(year, this_month, this_day).isocalendar()[1] | |
if this_week == user_week: | |
users.append(user[0]["username"]) | |
for user in users: | |
print user + "'s birthday is this week." | |
if len(users) <= 0: | |
print "\nMatching query has no results" | |
elif len(users) == 1: | |
print "\nThere is one user from a total of %d whose birthday is this week. The user represents %f%s of the users" %(user_dob[0]["total"], 1 / float(user_dob[0]["total"]) * 100, "%") | |
else: | |
print "\nThere are %d users from a total of %d whose birthday is this week. They represent %f%s of the users" %(len(users), user_dob[0]["total"], float(len(users)) / float(user_dob[0]["total"]) * 100, "%") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment