<commit_before># Welcome to the Datastore Demo! (hit enter)
# We're going to walk through some of the basics...
# Don't worry though. You don't need to do anything, just keep hitting enter...

#	Copyright 2015, Google, Inc.
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#    http:#www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

#Import libraries
from gcloud import datastore
import os
import time

#The next few lines will set up your environment variables
#Replace "YOUR_RPOEJCT_ID_HERE" with the correct value in code.py
os.environ["GOOGLE_APPLICATION_CREDENTIALS"] = "key.json"

projectID = "YOUR_RPOEJCT_ID_HERE"

os.environ["GCLOUD_TESTS_PROJECT_ID"] = projectID
os.environ["GCLOUD_TESTS_DATASET_ID"] = projectID
datastore.set_default_dataset_id(projectID)

#Let us build a message board / news website

#First, create a fake email for our fake user
email = "<EMAIL>"

#Now, create a 'key' for that user using the email
user_key = datastore.Key('User', email)
print( user_key )

#Now create a entity using that key
new_user = datastore.Entity( key=user_key )
print( new_user )

#Add some fields to the entity

new_user["name"] = unicode("<NAME>")
new_user["email"] = unicode(email)
print( new_user )

#Push entity to the Cloud Datastore
datastore.put( new_user )

#Get the user from datastore and print
print( datastore.get(user_key) )

#Make a new incomplete key for the story
#datastore will make a unique id automatically
story_key = datastore.Key( "Story" )
new_story = datastore.Entity( key = story_key )

#Add some fields
new_story["url"] = unicode("cloud.google.com")
new_story["title"] = unicode("Google is an awesome cloud provider")
new_story["text"] = unicode("Check out this cool website I found for Cloud Stuff!")
new_story['timestamp'] = unicode(time.strftime("%s", time.gmtime()))
print( new_story )

#Push entity to the Cloud Datastore
datastore.put( new_story )

#Query for the stories
#This query is eventually consistant
query = datastore.Query(kind='Story', order=["-timestamp"])
results = list( query.fetch() )
print( results )

#Make another story
new_story = datastore.Entity( key = datastore.Key("Story") )
new_story["url"] = unicode("blog.sandeepdinesh.com")
new_story["title"] = unicode("Interesting Blog")
new_story["text"] = unicode("This is a pretty cool blog I write. Check it out!")
new_story['timestamp'] = unicode(time.strftime("%s", time.gmtime()))
datastore.put( new_story )

#Query for the stories
results = list(query.fetch())
print( results )

#Lets add a comment to the first story

#Get the id from the first story
story = results[0]
story_id = story.key.id_or_name
print( story_id )

#Create the comment
#The ancestor (parent) is the user
comment_key = datastore.Key('Comment', parent=user_key)
new_comment = datastore.Entity( key=comment_key )
new_comment['text'] = unicode("Cool story bro")
new_comment['storyid'] = unicode(story_id)
new_comment['timestamp'] = unicode(time.strftime("%s", time.gmtime()))
print( new_comment )

#Push comment to datastore
datastore.put( new_comment )

#Get the id from the second story
story = results[1]
story_id = story.key.id_or_name
print( story_id )

#Create another comment
new_comment = datastore.Entity( key=datastore.Key('Comment', parent=user_key) )
new_comment['text'] = unicode("Wow. Much Awesome. So Rad.")
new_comment['storyid'] = unicode(story_id)
new_comment['timestamp'] = unicode(time.strftime("%s", time.gmtime()))
print( new_comment )

#Push comment to datastore
datastore.put( new_comment )

#Query for all comments
query = datastore.Query( kind='Comment', order=["timestamp"] )
results = list( query.fetch() )
print( results )

#Query for comments on a single story
query = datastore.Query( kind='Comment', filters=[( 'storyid', '=', unicode(story_id) )], order=["timestamp"] )

#Query will fail without a joint index on storyid and timestamp!

#Run Query
results = list( query.fetch() )
print( results )

#Let's make a new user and comment on the same story
email = "<EMAIL>"
second_user = datastore.Entity( key=datastore.Key('User', email) )
second_user["name"] = unicode("<NAME>")
second_user["email"] = unicode(email)
datastore.put( second_user )

new_comment = datastore.Entity( key=datastore.Key('Comment', parent=datastore.Key('User', email) ) )
new_comment['text'] = unicode("Boring story :( ")
new_comment['storyid'] = unicode(story_id)
new_comment['timestamp'] = unicode(time.strftime("%s", time.gmtime()))
datastore.put( new_comment )

#Query again
query = datastore.Query( kind='Comment', filters=[( 'storyid', '=', unicode(story_id) )], order=["timestamp"] )
results = list( query.fetch() )
print( results )

#Perform strongly consistant query
#Use the user_key as the ancestor to get the users comments
query = datastore.Query( kind='Comment', ancestor=user_key )
results = list( query.fetch() )
print( results )

#Thats it!<commit_msg>Updated Datastore example to work with latest gcloud-python, and using datetime instead of strings for timestamp<commit_after># Welcome to the Datastore Demo! (hit enter)
# We're going to walk through some of the basics...
# Don't worry though. You don't need to do anything, just keep hitting enter...

#	Copyright 2015, Google, Inc.
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#    http:#www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

#Import libraries
from gcloud import datastore
import os
import time
import datetime

#The next few lines will set up your environment variables
#Replace "YOUR_RPOEJCT_ID_HERE" with the correct value in code.py
projectID = "smart-spark-93622"

client = datastore.Client.from_service_account_json( json_credentials_path="key.json", dataset_id=projectID )

#Let us build a message board / news website

#First, create a fake email for our fake user
email = "<EMAIL>"

#Now, create a 'key' for that user using the email
user_key = client.key('User', email)
print( user_key )

#Now create a entity using that key
new_user = datastore.Entity( key=user_key )
print( new_user )

#Add some fields to the entity

new_user["name"] = unicode("Iam Fake")
new_user["email"] = unicode(email)
print( new_user )

#Push entity to the Cloud Datastore
client.put( new_user )

#Get the user from datastore and print
print( client.get(user_key) )

#Make a new incomplete key for the story
#datastore will make a unique id automatically
story_key = client.key( "Story" )
new_story = datastore.Entity( key = story_key )

#Add some fields
new_story["url"] = unicode("cloud.google.com")
new_story["title"] = unicode("Google is an awesome cloud provider")
new_story["text"] = unicode("Check out this cool website I found for Cloud Stuff!")
new_story['timestamp'] = datetime.datetime.now()
print( new_story )

#Push entity to the Cloud Datastore
client.put( new_story )

#Query for the stories
#This query is eventually consistant
query = datastore.Query( client=client, kind='Story', order=["-timestamp"])
results = list( query.fetch() )
print( results )

#Make another story
new_story = datastore.Entity( key = client.key("Story") )
new_story["url"] = unicode("blog.sandeepdinesh.com")
new_story["title"] = unicode("Interesting Blog")
new_story["text"] = unicode("This is a pretty cool blog I write. Check it out!")
new_story['timestamp'] = datetime.datetime.now()
client.put( new_story )

#Query for the stories
results = list(query.fetch())
print( results )

#Lets add a comment to the first story

#Get the id from the first story
story = results[0]
story_id = story.key.id_or_name
print( story_id )

#Create the comment
#The ancestor (parent) is the user
comment_key = client.key('Comment', parent=user_key)
new_comment = datastore.Entity( key=comment_key )
new_comment['text'] = unicode("Cool story bro")
new_comment['storyid'] = unicode(story_id)
new_comment['timestamp'] = datetime.datetime.now()
print( new_comment )

#Push comment to datastore
client.put( new_comment )

#Get the id from the second story
story = results[1]
story_id = story.key.id_or_name
print( story_id )

#Create another comment
new_comment = datastore.Entity( key=client.key('Comment', parent=user_key) )
new_comment['text'] = unicode("Wow. Much Awesome. So Rad.")
new_comment['storyid'] = unicode(story_id)
new_comment['timestamp'] = datetime.datetime.now()
print( new_comment )

#Push comment to datastore
client.put( new_comment )

#Query for all comments
query = datastore.Query(client=client, kind='Comment', order=["timestamp"] )
results = list( query.fetch() )
print( results )

#Query for comments on a single story
query = datastore.Query(client=client, kind='Comment', filters=[( 'storyid', '=', unicode(story_id) )], order=["timestamp"] )

#Query will fail without a joint index on storyid and timestamp!

#Run Query
results = list( query.fetch() )
print( results )

#Let's make a new user and comment on the same story
email = "<EMAIL>"
second_user = datastore.Entity( key=client.key('User', email) )
second_user["name"] = unicode("<NAME>")
second_user["email"] = unicode(email)
client.put( second_user )

new_comment = datastore.Entity( key=client.key('Comment', parent=client.key('User', email) ) )
new_comment['text'] = unicode("Boring story :( ")
new_comment['storyid'] = unicode(story_id)
new_comment['timestamp'] = datetime.datetime.now()
client.put( new_comment )

#Query again
query = datastore.Query(client=client, kind='Comment', filters=[( 'storyid', '=', unicode(story_id) )], order=["timestamp"] )
results = list( query.fetch() )
print( results )

#Perform strongly consistant query
#Use the user_key as the ancestor to get the users comments
query = datastore.Query(client=client, kind='Comment', ancestor=user_key )
results = list( query.fetch() )
print( results )

#Thats it!
{'fraction_non_alphanumeric': 0.12130711503752256, 'fraction_numerical': 0.0023748456350337226, 'mean_word_length': 5.654867256637168, 'pattern_counts': {'":': 0, '<': 10, '<?xml version=': 0, '>': 10, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2972060', 'n_tokens_mistral': 3352, 'n_tokens_neox': 3258, 'n_words': 1113}