Last active
February 26, 2016 17:48
-
-
Save paulozullu/8a66ee93dffaf648654e to your computer and use it in GitHub Desktop.
Using pyneo, get users that have more than 21 years old
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 | |
from dateutil.relativedelta import relativedelta | |
now = datetime.datetime.now() | |
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 = datetime.datetime.strptime(str(user[0]["dob"]), '%d/%m/%Y') | |
difference_in_years = relativedelta(now, dob).years | |
if difference_in_years > 21: | |
data = {} | |
data["username"] = user[0]["username"] | |
data["age"] = str(difference_in_years) | |
users.append(data) | |
for user in users: | |
print user["username"] + " has " + user["age"] + " years." | |
if len(users) <= 0: | |
print "\nMatching query has no results" | |
elif len(users) == 1: | |
print "\nThere is one user from a total of %d with more than 21 years old. 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 with more than 21 years old. 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