Ejemplo n.º 1
0
    def connection_to_database(self):
        if os.path.isfile('../resources/vcap-local.txt'):
            with open('../resources/vcap-local.txt') as f:
                vcap = json.load(f)
                print('Found local VCAP_SERVICES')
                creds = vcap['services']['cloudantNoSQLDB'][0]['credentials']
                user = creds['username']
                password = creds['password']
                url = 'https://' + creds['host']
                client = Cloudant(user,
                                  password,
                                  url=url,
                                  connect=True,
                                  auto_renew=True,
                                  adapter=Replay429Adapter(
                                      retries=10, initialBackoff=0.01))

        session = client.session()
        print('Username: {0}'.format(session['userCtx']['name']))
        print('Databases: {0}'.format(client.all_dbs()))

        my_database = client['batya_db']
        self.db = my_database
        my_document = my_database.client['batya_db']
        # crops4years= self.get4Years(self, 2018, self.db)
        return my_database
Ejemplo n.º 2
0
def update_all_to_db():
    adapter = Replay429Adapter(200, 0.25)
    client = Cloudant(api_user, api_pass, url=api_url, adapter=adapter)
    # or using url
    # client = Cloudant(USERNAME, PASSWORD, url='https://acct.cloudant.com')

    # Connect to the server
    client.connect()

    # Perform client tasks...
    session = client.session()
    print('Username: {0}'.format(session['userCtx']['name']))
    print('Databases: {0}'.format(client.all_dbs()))

    courses_db = client['purdue_courses']

    file_path = "CourseInfo.json"
    f = open(file_path, 'r')
    text = f.read()
    f.close()
    deep_caches = json.loads(text)

    cache = dict()
    for cache_type, cache in deep_caches.items():
        cache.update(cache)

    bulk_update(cache, courses_db)

    # Disconnect from the server
    client.disconnect()
    def insertDbUrl(self, action, link, intent):
        if action == 'url':
            data = {
                'url': link,
                'bot': 'None',
                'intent': intent
            }
        elif action == 'bot':
            data = {
                'url': 'None',
                'bot': link,
                'intent': intent
            }
        client = Cloudant(COUCHDB_USER, COUCHDB_PASSWORD, url=COUCHDB_URL)

        # Connect to the server
        client.connect()

        # Perform client tasks...
        session = client.session()

        actionsDB = client['actions']
        my_document = actionsDB.create_document(data)

        # Check that the document exists in the database
        if my_document.exists():
            print 'SUCCESS!!'
Ejemplo n.º 4
0
def getConnection():
    """
    Creates and returns a Cloudant connection.
    """
    my_client = Cloudant(CLOUDANT_USERNAME, CLOUDANT_PASSWORD, account=ACCOUNT_NAME)
    my_client.connect()
    my_session = my_client.session()
    if my_session is None:
        print "Connection unsuccessful"
    return my_client
Ejemplo n.º 5
0
class DBCloudant:
    def __init__(self):
        self.client = ""
        self.session = ""
        self.dataBase = ""
        self.document = ""
        self.query = ""

    def connect(self):
        self.client = Cloudant(conf.username,
                               conf.password,
                               url=conf.url,
                               connect=True)
        self.session = self.client.session()
        if self.session['userCtx']['name']:
            print("Coneccion realizada correctamente")
        else:
            print("Error al conectar con la BD")

    def disconnect(self):
        self.client.disconnect()

    def createDB(self, name):
        try:
            self.dataBase = self.client.create_database(name)
            if self.dataBase.exists():
                print("DB creada correctamente")
        except Exception as error:
            print("DB ya existente")

    def getDataBases(self):
        return self.client.all_dbs()

    def deleteDataBase(self, name):
        return self.client.delete_database(name)

    def openDB(self, name):
        self.dataBase = self.client[name]

    def createDocument(self, data):
        self.document = self.dataBase.create_document(data)
        if self.document.exists():
            print("Se creo el documento correctamente")

    def getDocument(self, name):
        self.document = self.dataBase[name]
        return self.document

    def getAllDocuments(self, name):
        return self.dataBase

    def getDocumentBySensor(self, sensor):
        self.query = cl.query.Query(self.dataBase)
        with self.query.custom_result(selector={"sensor": sensor}) as rst:
            return rst
Ejemplo n.º 6
0
def conn():
    vcap = json.loads(os.getenv("VCAP_SERVICES"))['cloudantNoSQLDB']
    cl_username = vcap[0]['credentials']['username']
    cl_password = vcap[0]['credentials']['password']
    url = vcap[0]['credentials']['url']
    # print(cl_username + ' ' + cl_password)
    client = Cloudant(cl_username, cl_password, url=url)
    client.connect()
    session = client.session()
    # print 'Username: {0}'.format(session['userCtx']['name'])
    # print 'Databases: {0}'.format(client.all_dbs())
    return client
Ejemplo n.º 7
0
def consultarCloudant():
    # Authenticate using an IAM API key
    client = Cloudant(CLOUDANT_USERNAME, CLOUDANT_PASSWORD, url=CLOUDANT_URL, connect=True)

    session = client.session()
    # print('Username: {0}'.format(session['userCtx']['name']))
    # print('Databases: {0}'.format(client.all_dbs()))

    result_collection = Result(client["prueba"].all_docs, include_docs=True)
    print("Retrieved minimal document:\n{0}\n".format(result_collection[0]))
    # Disconnect from the server
    client.disconnect()

    return result_collection
def ListFiles():
	client = Cloudant("accesS_key","secret key", 	url='url')

	client.connect()
	session = client.session()
	print 'Username: {0}'.format(session['userCtx']['name'])
	print 'Databases: {0}'.format(client.all_dbs())
	my_database = client['test']
	## Get all of the documents from my_database
	t = HTML.Table(header_row=['File Name', 'File Version', 'Last Modified Date'])
	for document in my_database:
		t.rows.append([document['name'],document['version'],document['Last_Modified_Date']])
	htmlcode = str(t)
    	return htmlcode
def delfile():
	fileName=request.form['fileName']
	fileVersion=request.form['fileVersion']

	client = Cloudant("accesS_key","secret key", 	url='url')
	client.connect()
    	session = client.session()
    	my_database = client['test']

	selector = {'name': {'$eq': fileName} }
	resp =my_database.get_query_result(selector, raw_result=True, limit=100)
	if resp['docs']:
		for doc in resp['docs']:
			if doc in resp['docs']:
				#selector1 = {'version': {'$eq': fileVersion}}
				#resp1 = my_database.get_query_result(selector1, raw_result=True, limit=100)
				#print doc['version']
				#print fileVersion
				if(doc['version']==int(fileVersion)):
					#print doc['_id']
					dId=doc['_id']
					my_document = my_database[dId]
					# Delete the document
					my_document.delete()
					#doc['name'].delete()
					html = '''<html>
					<title>Attributes</title>
					%s
					</html>''' %('Document Deleted')    
					client.disconnect()   
					return html
				else:
					html = '''<html>
					<title>Attributes</title>
					File with entered version not found.
					</html>''' #%(doc['content'])    
					return html
	else:
		html = '''<html>
		<title>Attributes</title>
		File not found.
		</html>''' #%(doc['content'])    
		return html
Ejemplo n.º 10
0
def update_lookup_tables():
    adapter = Replay429Adapter(200, 0.25)
    client = Cloudant(api_user, api_pass, url=api_url, adapter=adapter)
    # or using url
    # client = Cloudant(USERNAME, PASSWORD, url='https://acct.cloudant.com')

    # Connect to the server
    client.connect()

    # Perform client tasks...
    session = client.session()
    print('Username: {0}'.format(session['userCtx']['name']))
    print('Databases: {0}'.format(client.all_dbs()))

    file_path = "CourseInfo.json"
    f = open(file_path, 'r')
    text = f.read()
    f.close()
    deep_caches = json.loads(text)

    query_table_db = client['query_table']
    query_table = __make_query_table__(deep_caches['Subjects'],
                                       deep_caches['Courses'])
    bulk_update(query_table, query_table_db)

    api_class_lookup_table_db = client['api_class_lookup_table']
    api_class_lookup_table = \
        __make_lookup_table__(deep_caches['Classes'], 'CourseId')
    bulk_update(api_class_lookup_table, api_class_lookup_table_db)

    section_lookup_table_db = client['section_lookup_table']
    section_lookup_table = \
        __make_lookup_table__(deep_caches['Sections'], 'ClassId')
    bulk_update(section_lookup_table, section_lookup_table_db)

    meeting_lookup_table_db = client['meeting_lookup_table']
    meeting_lookup_table = \
        __make_lookup_table__(deep_caches['Meetings'], 'SectionId')
    bulk_update(meeting_lookup_table, meeting_lookup_table_db)

    # Disconnect from the server
    client.disconnect()
Ejemplo n.º 11
0
def query_from_db(odata_id):
    client = Cloudant(api_user, api_pass, url=api_url)
    print("Quering all meeting times")

    # Connect to the server
    client.connect()

    # Perform client tasks...
    session = client.session()
    print('Username: {0}'.format(session['userCtx']['name']))
    print('Databases: {0}'.format(client.all_dbs()))

    courses_db = client['testing_db']

    if odata_id not in courses_db:
        out = None
    else:
        out = courses_db[odata_id]

    client.disconnect()
    return out
Ejemplo n.º 12
0
def downloadfile():
	fileName=request.form['fileName']
	fileVersion=request.form['fileVersion']
	#print 'here: '+fileName
	#return redirect('/')

	client = Cloudant("accesS_key","secret key", 	url='url')

	client.connect()
    	session = client.session()
    	my_database = client['test']

	selector = {'name': {'$eq': fileName} }
	resp =my_database.get_query_result(selector, raw_result=True, limit=100)
	if resp['docs']:
		for doc in resp['docs']:
			if doc in resp['docs']:
				if(doc['version']==int(fileVersion)):
					html = '''<html>
					<title>Attributes</title>
					%s
					</html>''' %(doc['content'])   
					with io.FileIO(doc['name'], "w") as file:
					    file.write(doc['content']) 
					client.disconnect()   
					return html
				else:
					html = '''<html>
					<title>Attributes</title>
					File with entered version not found.
					</html>'''     
					return html
	else:
		html = '''<html>
		<title>Attributes</title>
		File not found.
		</html>'''    
		return html
Ejemplo n.º 13
0
def commonCreation(key):
    return masterdict[key]


client = Cloudant(
    '06867603-146b-43d1-b8d9-454f272fdc02-bluemix',
    'cacca3cc252cb10645410c6e42a7ebb0c4a6382bb0dfa940b029d1de3285fa64',
    url=
    'https://*****:*****@06867603-146b-43d1-b8d9-454f272fdc02-bluemix.cloudant.com'
)

# Connect to the server
client.connect()

# Perform client tasks...
session = client.session()


def update(key):
    if 'parking' in client.all_dbs():
        my_database = client['parking']
        selector = {'zipcode': zipcode, 'pno': rpino, '_id': id}
        docs = my_database.get_query_result(selector,
                                            raw_result=True,
                                            limit=100)
        # print len(docs['docs'])
        if len(docs['docs']) == 0:
            print "no docs"
            data = commonCreation(key)
            # Create a document using the Database API
            my_document = my_database.create_document(data)
Ejemplo n.º 14
0
def main(dict):
    # CREDENTIALS #
    # THESE ARE ADMIN CREDENTIALS WITH ALL PERMISSIONS ENABLED.....NO PRESSURE #

    USERNAME = "******"
    PASSWORD = "******"
    ACCOUNT_NAME = "account_name"

    # Use Cloudant to create a Cloudant client using account #

    client = Cloudant(USERNAME,
                      PASSWORD,
                      account=ACCOUNT_NAME,
                      connect=True,
                      auto_renew=True)

    # Perform client tasks by establishing a session #

    session = client.session()
    database = client['master_dataset_v1']
    result_collection = Result(database.all_docs, include_docs=True)
    rand = random.randint(0, 5804)
    db = {}
    phrases = result_collection[rand:rand + 5]
    for i in range(5):
        db[str(i + 1)] = phrases[i]
    print(db)

    #creating information about the phrases
    phrE1 = db["1"]['doc']['Original_Text']
    phrF1 = db["1"]['doc']['Translation']
    phrase1 = "Our phrase is " + phrF1 + " , it means " + phrE1 + " in English. Would you like to see the part of speech of each word or the definition of each word? You can also move on to the next phrase"
    aud1 = db["1"]['doc']['Translation']
    #print(phrF1)
    pos1 = make_pos_text(db["1"])

    phrE2 = db["2"]['doc']['Original_Text']
    phrF2 = db["2"]['doc']['Translation']
    phrase2 = "Our phrase is " + phrF2 + " , it means " + phrE2 + " in English. Would you like to see the part of speech of each word or the definition of each word? You can also continue to the next phrase"
    aud2 = db["2"]['doc']['Translation']
    pos2 = make_pos_text(db["2"])

    phrE3 = db["3"]['doc']['Original_Text']
    phrF3 = db["3"]['doc']['Translation']
    phrase3 = "Our phrase is " + phrF3 + " , it means " + phrE3 + " in English. Would you like to see the part of speech of each word or the definition of each word? You can also type continue for the next phrase"
    aud3 = db["3"]['doc']['Translation']
    pos3 = make_pos_text(db["3"])

    phrE4 = db["4"]['doc']['Original_Text']
    phrF4 = db["4"]['doc']['Translation']
    phrase4 = "Our phrase is " + phrF4 + " , it means " + phrE4 + " in English. Would you like to see the part of speech of each word or the definition of each word? Type continue or next to move on"
    aud4 = db["4"]['doc']['Translation']
    pos4 = make_pos_text(db["4"])

    phrE5 = db["5"]['doc']['Original_Text']
    phrF5 = db["5"]['doc']['Translation']
    phrase5 = "Our phrase is " + phrF5 + " , it means " + phrE5 + " in English. Would you like to see the part of speech of each word or the definition of each word?"
    aud5 = db["5"]['doc']['Translation']
    pos5 = make_pos_text(db["5"])

    #creating the first question
    #Question 1: what does -insert phrase- mean in English?
    #Multiple Choice: each is an English phrase from the database
    question1 = "Question 1: what does " + phrF1 + " mean in English?"
    q1cPhrase = phrE1
    q1c, q1choice = randMultipleChoice(q1cPhrase, rand, result_collection)
    q1o1val = setVal(0, q1c)
    q1o2val = setVal(1, q1c)
    q1o3val = setVal(2, q1c)
    q1o4val = setVal(3, q1c)
    q1oArr = [q1o1val, q1o2val, q1o3val, q1o4val]

    #creating the second question
    #Question 2: what part of speech is this word, -insert word-?
    #Multiple Choice: Noun Verb
    #print(db["2"]['doc'])
    #print("DEBUG!")
    # print(db["2"]['doc']['Nouns'])
    secondstrN = db["2"]['doc']['Nouns']
    secondstrV = db["2"]['doc']['Verbs']

    nounlist = secondstrN.split(",")
    verblist = secondstrV.split(",")
    #print("DEBUG!")
    #print(nounlist[0])
    choosePos = random.randint(0, 1)
    #0 : nounlist
    #1 : verblist
    #2 : neither
    if choosePos == 0 and nounlist == []:
        choosePos = 1
    elif choosePos == 1 and verblist == []:
        choosePos = 0
    elif nounlist == [] and verblist == []:
        #should not go into here
        choosePos = 2

    if choosePos == 0:
        word = random.choice(nounlist)
        q2cPhrase = "Noun"
    elif choosePos == 1:
        word = random.choice(verblist)
        q2cPhrase = "Verb"
    else:
        word = "aimer"
        choosePos = 1
        q2cPhrase = "Verb"

    question2 = "Question 2: Is " + word + " a Noun or a Verb?"

    q2choice = ["Noun", "Verb"]

    q2valNoun = setVal(0, choosePos)
    q2valVerb = setVal(1, choosePos)
    q2oArr = [q2valNoun, q2valVerb]

    #creating the third question
    #Question 3: what is -insert french word- in English?
    #Multiple Choice: each is an English word (either all noun or all adj)
    thirdstrN = db["3"]['doc']['Nouns']
    thirdstrV = db["3"]['doc']['Verbs']

    nounlist3 = thirdstrN.split(",")
    verblist3 = thirdstrV.split(",")
    #print("DEBUG!")
    #print(nounlist[0])
    choosePos3 = random.randint(0, 1)
    #0 : nounlist
    #1 : verblist
    #2 : neither
    if choosePos3 == 0 and nounlist3 == []:
        choosePos3 = 1
    elif choosePos3 == 1 and verblist3 == []:
        choosePos3 = 0
    elif nounlist3 == [] and verblist3 == []:
        #should not go into here
        choosePos3 = 2

    if choosePos3 == 0:
        word3 = random.choice(nounlist3)
        q3cPhrase = "Noun"
    elif choosePos3 == 1:
        word3 = random.choice(verblist3)
        q3cPhrase = "Verb"
    else:
        word3 = "aimer"
        choosePo3s = 1
        q3cPhrase = "Verb"

    question3 = "Question 3: Is " + word3 + " a Noun or a Verb?"

    q3choice = ["Noun", "Verb"]

    q3valNoun = setVal(0, choosePos3)
    q3valVerb = setVal(1, choosePos3)
    q3oArr = [q3valNoun, q3valVerb]

    #creating the fourth question
    #Question 4: fill in the blank: -insert french phrase with one word removed-
    #Multiple Choice: one option is the word removed, other options are random
    q4nouns = (db["4"]['doc']['Nouns']).split(",")
    q4verbs = (db["4"]['doc']['Verbs']).split(",")
    q4allwords = q4nouns + q4verbs
    grabWord = random.choice(q4allwords)
    manipulate4 = phrF4
    fillinblank4 = manipulate4.replace(grabWord, "_______")
    #print(fillinblank4)

    q4choices = []
    q4choices.append(grabWord)

    q1_nouns = (db["1"]['doc']['Nouns']).split(",")
    q1_verbs = (db["1"]['doc']['Verbs']).split(",")
    q2_nouns = (db["2"]['doc']['Nouns']).split(",")
    q2_verbs = (db["2"]['doc']['Verbs']).split(",")
    q3_nouns = (db["3"]['doc']['Nouns']).split(",")
    q3_verbs = (db["3"]['doc']['Verbs']).split(",")
    q5_nouns = (db["5"]['doc']['Nouns']).split(",")
    q5_verbs = (db["5"]['doc']['Verbs']).split(",")
    rand_words = q1_nouns + q1_verbs + q2_nouns + q2_verbs + q3_nouns + q3_verbs + q5_nouns + q5_verbs

    for i in range(3):
        f = True
        while (f):
            potWord = random.choice(rand_words)
            if potWord not in q4choices:
                q4choices.append(potWord)
                f = False

    random.shuffle(q4choices)
    q4c = q4choices.index(grabWord)

    q4o1val = setVal(0, q4c)
    q4o2val = setVal(1, q4c)
    q4o3val = setVal(2, q4c)
    q4o4val = setVal(3, q4c)
    q4oArr = [q4o1val, q4o2val, q4o3val, q4o4val]
    q4cPhrase = grabWord
    question4 = "Question 4: fill in the blank: " + fillinblank4

    #creating the fifth question
    #Question 5: True or False : This phrase "-insert french phrase-"" is in the
    #category, "-insert intent-"
    #True/False
    category = db["5"]['doc']['Category']
    other1 = db["1"]['doc']['Category']
    other2 = db["2"]['doc']['Category']
    other3 = db["3"]['doc']['Category']
    other4 = db["4"]['doc']['Category']

    otherOptions = []
    if other1 != category: otherOptions.append(other1)
    if other2 != category: otherOptions.append(other2)
    if other3 != category: otherOptions.append(other3)
    if other4 != category: otherOptions.append(other4)

    chooseCat = random.randint(0, 1)
    #0 : False
    #1 : True
    if chooseCat == 0 and otherOptions == []:
        chooseCat = 1

    if chooseCat == 0:
        catchosen = random.choice(otherOptions)
    else:
        catchosen = category

    question5 = "Question 5: True or False : This phrase " + phrF5 + " is in the category, " + catchosen + "."

    q5choice = ["False", "True"]

    q5valFalse = setVal(0, chooseCat)
    q5valTrue = setVal(1, chooseCat)
    q5oArr = [q5valFalse, q5valTrue]

    RET = {}

    #populating the dictionary with phrases for the lesson
    RET["phrase1"] = phrase1
    RET["aud1"] = aud1
    RET["pos1"] = pos1

    RET["phrase2"] = phrase2
    RET["aud2"] = aud2
    RET["pos2"] = pos2

    RET["phrase3"] = phrase3
    RET["aud3"] = aud3
    RET["pos3"] = pos3

    RET["phrase4"] = phrase4
    RET["aud4"] = aud4
    RET["pos4"] = pos4

    RET["phrase5"] = phrase5
    RET["aud5"] = aud5
    RET["pos5"] = pos5

    #populating the dictionary with the first question
    RET["question1"] = question1
    RET["q1c"] = q1c
    RET['q1cPhrase'] = q1cPhrase
    RET['q1choice'] = q1choice
    RET['q1oArr'] = q1oArr

    #populating the dictionary with the second question
    RET["question2"] = question2
    RET["q2c"] = choosePos
    RET["q2cPhrase"] = q2cPhrase
    RET["q2choice"] = q2choice
    RET["q2oArr"] = q2oArr

    #populating the dictionary with the third question
    RET["question3"] = question3
    RET["q3c"] = choosePos3
    RET["q3cPhrase"] = q3cPhrase
    RET["q3choice"] = q3choice
    RET["q3oArr"] = q3oArr

    #populating the dictionary with the fourth question
    RET["question4"] = question4
    RET["q4c"] = q4c
    RET['q4cPhrase'] = q4cPhrase
    RET['q4choice'] = q4choices
    RET['q4oArr'] = q4oArr

    #populating the dictionary with the second question
    RET["question5"] = question5
    RET["q5c"] = chooseCat
    RET["q5cPhrase"] = phrF5
    RET["q5choice"] = q5choice
    RET["q5oArr"] = q5oArr

    #print(RET)

    return RET
Ejemplo n.º 15
0
def saveFile():	
	import os, time
	file = request.files['file']
        filename =  file.filename
	fContent = file.read()
	BLOCKSIZE = 65536
	(mode, ino, dev, nlink, uid, gid, size, atime, mtime, ctime) = os.stat(filename)
	last_modified_date = time.ctime(mtime)
	hasher = hashlib.sha1()
	with open(filename, 'rb') as afile:
	    buf = afile.read(BLOCKSIZE)
	    while len(buf) > 0:
        	hasher.update(buf)
	        buf = afile.read(BLOCKSIZE)
 	client = Cloudant("accesS_key","secret key", 	url='url')

	client.connect()
	i=1
	session = client.session()
	my_database = client['test']
	temp = list()
	selector = {'name': {'$eq': filename}}
	resp = my_database.get_query_result(selector, raw_result=True, limit=100)
	if resp['docs']:
		for doc in resp['docs']:
			if(doc['name']==filename):
				# file exists, update version
				if (doc['hash']!=hasher.hexdigest()):
					print 'just update version. Content Edited'
					i=doc['version']
					i=int(i+1)
					data = {
					    'name': filename,
					    'content': fContent,
					    'Last_Modified_Date': 'some date',
					    'version': i,
					    'hash': hasher.hexdigest()
					    }	
					my_document = my_database.create_document(data)
					return render_template('file-upload.htm')

				# else check if document exists and file content is same.	
				else:
					if(doc['hash']==hasher.hexdigest()):
						print 'File has NOT been modified'
						html = '''<html>
						<title>Attributes</title>
						%s
						</html>''' %('File already exists!')    
						return html

	# file is brand new.
	else:
		data = {
		    'name': filename,
		    'content': fContent,
		    'Last_Modified_Date': last_modified_date,
		    'version': 1,
		    'hash': hasher.hexdigest()
		    }	
		my_document = my_database.create_document(data)
		return render_template('file-upload.htm')
Ejemplo n.º 16
0
def main():
    USERNAME = "******"
    PASSWORD = "******"
    cloudant_url = "https://*****:*****@ba62d85b-fc08-4706-8a9e-ba91bfbf6174-bluemix.cloudant.com"

    my_database_name_in_cloudant = 'abc'

    #client = Cloudant(USERNAME, PASSWORD, account=ACCOUNT_NAME)
    client = Cloudant(USERNAME, PASSWORD, url=cloudant_url)
    # or using url
    # client = Cloudant(USERNAME, PASSWORD, url='https://acct.cloudant.com')

    # Connect to the server
    client.connect()

    # Perform client tasks...
    session = client.session()
    print('Username: {0}'.format(session['userCtx']['name']))
    print('Databases: {0}'.format(client.all_dbs()))

    # Connection Established....Open My Database Now
    my_database = client[my_database_name_in_cloudant]
    # my_document = my_database['0ee5818002b8302e815f619a1521679f']
    # print(my_document)
    '''
    # Send Data document in Database by reading the input CSV file using my_database object
    csvfile = open('file1.csv', 'r')

    fieldnames = ("member_id", "product id", "date", "number of helpful feedbacks", "number of feedbacks", "rating", "title", "body")
    reader = csv.DictReader(csvfile, fieldnames, delimiter='\t')  # Create a dictionary object using the field names

    for row in reader:  # Take each row from dictionary and convert as needed and prepare another dictionary object
        my_data_doc = {'member_id': row['member_id'], 'product id': row['product id'], 'date': row['date'],
                'number of helpful feedbacks': int(row['number of helpful feedbacks']),
                'number of feedbacks': int(row['number of feedbacks']), 'rating': round(float(row['rating']), 1),'title': row['title'],
                'body': row['body']}
        time.sleep(0.7) #  Otherwise server raises Too many requests..10 per second can accept
        my_database.create_document(my_data_doc)  # Send the data to database


    #Use Get Method
    # Define the end point and parameters
    retrieve_target = '_all_docs'
    end_point = '{0}/{1}/{2}'.format(cloudant_url, my_database_name_in_cloudant, retrieve_target)
    #print(end_point)
    params = {'include_docs': 'true'}

    # Issue the request
    #response = client.r_session.get(end_point, params=params)

    # Display the response content
    #print(response.json())
'''

    # Create Index
    database_handler = cloudant.database.CloudantDatabase(
        client, my_database_name_in_cloudant)
    # index_response = database_handler.create_query_index(design_document_id='py_ddoc', index_name='py_index', index_type='json', fields=['member id'])
    # print(index_response)
    # print(database_handler.get_query_indexes(raw_result=True))
    # Disconnect from the server
    '''
    # Start Query :: Select Query # Assignment Query: 1
    selector = {'member_id': {'$eq': 'A1004AX2J2HXGL'}}
    docs_collection = database_handler.get_query_result(selector)
    # for each_doc in docs_collection:
    #    print(each_doc)

    # JSON output
    docs_collection = database_handler.get_query_result(selector, raw_result=True)
    # for each_doc in docs_collection['docs']:
    #   print(each_doc)


    # Start Query :: Select Query # Assignment Query: 3
    selector = {'rating': {'$lt': 3}}
    fields = ['product id']
    docs_collection = database_handler.get_query_result(selector, fields)
    for each_doc in docs_collection:
        print(each_doc)
'''

    # Search and Group Query:: Assignment Query 2/3
    # resp = database_handler.get_search_result(ddoc_id='py_ddoc', index_name='py_index', query='member_id:A*', group_field='member_id', counts=["member_id"])
    resp = database_handler.get_search_result(ddoc_id='py_ddoc',
                                              index_name='py_index',
                                              query='*:*',
                                              counts=["member_id"])
    print(resp['counts'])

    client.disconnect()