def getDBDtls(): if request.method == 'POST' and request.form['dbdtls'] == 'False': dbusername = request.form['username'] dbpassword = request.form['password'] dburl = request.form['url'] global db_user global db_pass global db_url db_user = dbusername db_pass = dbpassword db_url = dburl client = Cloudant(db_user, db_pass, url=db_url) client.connect() try: myDatabase = client.create_database("user_db") global db_svc db_svc = {"dbsvc": myDatabase} except CloudantException as err: print err client = Cloudant(db_user, db_pass, url=db_url) client.connect() client.delete_database("user_db") myDatabase = client.create_database("user_db") global db_svc db_svc = {"dbsvc": myDatabase} return render_template('index.html', getUsrDtls=True)
def main(dict): user = dict.get("username", "<username from credentials>") passwd = dict.get("password", "<password from credentials>") host = dict.get("host", "<host from credentials>") db_name = "test_cloud_functions_python_3_ibm_runtime" client = Cloudant(user, passwd, url='https://{0}'.format(host), connect=True) try: client.delete_database(db_name) except CloudantClientException as ex: if ex.status_code != 404: raise CloudantClientException(ex.status_code, db_name) my_database = client.create_database('my_database') if my_database.exists(): print('SUCCESS DB exists!!') data = {"_id": "p3", "firstname": "Suzzy", "lastname": "Queue"} my_document = my_database.create_document(data) if my_document.exists(): print('SUCCESS DOC exist!!') my_document_read = my_database['p3'] return my_document_read
def main(dict): user = dict.get("username", "<username from credentials>") passwd = dict.get("password", "<password from credentials>") host = dict.get("host", "<host from credentials>") db_name = "test_cloud_functions_python_3_ibm_runtime" client = Cloudant(user, passwd, url='https://{0}'.format(host), connect=True) try: client.delete_database(db_name) except CloudantClientException as ex: if ex.status_code != 404: raise CloudantClientException(ex.status_code, db_name) my_database = client.create_database(db_name) if my_database.exists(): print('SUCCESS DB exists!!') """Sample document""" _id = str(uuid.uuid4()) data = {"agentId": "Suzzy", "type": "User"} update_data = {"lastname": "Queue"} """Run tests""" test_create_doc(my_database, _id, data) test_update_doc(my_database, _id, update_data) test_get_doc_by_id(my_database, _id) doc = test_fetch_doc_by_id(my_database, _id) test_delete_doc_by_id(my_database, doc['_id']) return doc
def looping(): while True: time.sleep(5) ser = serial.Serial("/dev/ttyACM0", 9600) ser.baudrate = 9600 temp1 = ser.readline() # temp1 = 20 ser = serial.Serial("/dev/ttyACM1", 9600) ser.baudrate = 9600 temp2 = ser.readline() # temp2 = 20 print temp1, temp2 if int(temp1[0:2]) < 35: # if temp1 < 35: l1['color'] = "#2ecc71" l1['Safe'] = "true" else: l1['color'] = "#c0392b" l1['Safe'] = "false" # if temp2 < 35: if int(temp2[0:2]) < 35: l2['color'] = "#2ecc71" l2['Safe'] = "true" else: l2['color'] = "#c0392b" l2['Safe'] = "false" serviceUsername = "******" servicePassword = "******" serviceURL = "https://*****:*****@ac99bfe8-c64e-48a5-9f62-e3b7c44ba586-bluemix.cloudant.com" client = Cloudant(serviceUsername, servicePassword, url=serviceURL) client.connect() databaseName = "sensordbs" try: client.delete_database(databaseName) except CloudantException: print "There was a problem deleting '{0}'.\n".format(databaseName) else: print "'{0}' successfully deleted.\n".format(databaseName) myDatabaseDemo = client.create_database(databaseName) if myDatabaseDemo.exists(): print "'{0}' successfully created.\n".format(databaseName) newDocument = myDatabaseDemo.create_document(l1) newDocument = myDatabaseDemo.create_document(l2) # Check that the document exists in the database. if newDocument.exists(): print "Document"
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
def add_signal(status, load): client = Cloudant( "39a4348e-3ce1-40cd-b016-1f85569d409e-bluemix", "48e26645f504209f85b4c44d74a4cb14bc0d059a22b361534b78f406a513f8ff", url= "https://*****:*****@39a4348e-3ce1-40cd-b016-1f85569d409e-bluemix.cloudant.com" ) client.connect() databaseName = load myDatabase = client.create_database(databaseName) client.delete_database(databaseName) # if myDatabase.exists(): # print("'{0}' successfully created.\n".format(databaseName)) # client.delete_database(databaseName) myDatabase = client.create_database(databaseName) sampleData = [[status, "kitchen"]] # Create documents using the sample data. # Go through each row in the array for document in sampleData: # Retrieve the fields in each row. number = document[0] name = document[1] # Create a JSON document that represents # all the data in the row. jsonDocument = {"status": number, "circuit": name} # Create a document using the Database API. newDocument = myDatabase.create_document(jsonDocument) # Check that the document exists in the database. if newDocument.exists(): print("Document '{0}' successfully created.".format(number)) client.disconnect()
traceback.print_exc() json.dumps(cred) # クラウダントへの接続 client = Cloudant(cred['services'][label][i]['credentials']['username'], cred['services'][label][i]['credentials']['password'], url=cred['services'][label][i]['credentials']['url']) client.connect() # DBが存在していたら削除 print "既存データベース ", database_name ," の削除" try: db = client[database_name] if db.exists(): client.delete_database(database_name) except: pass # DBを新規作成 print "新規データベース ", database_name ," の作成" try: db = client.create_database(database_name) print "データベース作成成功" except: print "データベース作成失敗" sys.exit()
cred = json.load(f) f.close() print "クラウダントへの接続" client = Cloudant( cred['services']['cloudantNoSQLDB'][0]['credentials']['username'], cred['services']['cloudantNoSQLDB'][0]['credentials']['password'], url=cred['services']['cloudantNoSQLDB'][0]['credentials']['url']) client.connect() # DBが存在していたら削除 print "既存データベース ", output_db, " の作成" try: db = client[output_db] if db.exists(): client.delete_database(output_db) except: pass # DBを新規作成 print "新規データベース ", output_db, " の作成" try: db = client.create_database(output_db) print "データベース作成成功" except: print "データベース作成失敗" sys.exit() # 文字コード変換など前処理結果を中間ファイルへ出力 fin = codecs.open(input_file, "r", "shift_jis") fout = codecs.open(output_file, "w", "utf-8")
"descriptionField": description, "temperatureField": temperature } # Create a document using the Database API. newDocument = myDatabase.create_document(jsonDocument) # Check that the document exists in the database. if newDocument.exists(): print("Document '{0}' successfully created.".format(number)) result_collection = Result(myDatabase.all_docs) print("Retrieved minimal document:\n{0}\n".format(result_collection[0])) result_collection = Result(myDatabase.all_docs, include_docs=True) print("Retrieved full document:\n{0}\n".format(result_collection[0])) end_point = '{0}/{1}'.format("<url>", databaseName + "/_all_docs") params = {'include_docs': 'true'} response = client.r_session.get(end_point, params=params) print("{0}\n".format(response.json())) try: client.delete_database(databaseName) except CloudantException: print("There was a problem deleting '{0}'.\n".format(databaseName)) else: print("'{0}' successfully deleted.\n".format(databaseName)) client.disconnect()
class UnitTestDbBase(unittest.TestCase): """ The base class for all unit tests targeting a database """ @classmethod def setUpClass(cls): """ If targeting CouchDB, Set up a CouchDB instance otherwise do nothing. """ if os.environ.get('RUN_CLOUDANT_TESTS') is None: if os.environ.get('DB_URL') is None: os.environ['DB_URL'] = 'http://127.0.0.1:5984' if (os.environ.get('ADMIN_PARTY') and os.environ.get('ADMIN_PARTY') == 'true'): if os.environ.get('DB_USER'): del os.environ['DB_USER'] if os.environ.get('DB_PASSWORD'): del os.environ['DB_PASSWORD'] return if os.environ.get('DB_USER') is None: # Get couchdb docker node name if os.environ.get('COUCHDB_VERSION') == '2.1.1': os.environ['NODENAME'] = requests.get( '{0}/_membership'.format(os.environ['DB_URL'])).json()['all_nodes'][0] os.environ['DB_USER_CREATED'] = '1' os.environ['DB_USER'] = '******'.format( unicode_(uuid.uuid4()) ) os.environ['DB_PASSWORD'] = '******' if os.environ.get('COUCHDB_VERSION') == '2.1.1': resp = requests.put( '{0}/_node/{1}/_config/admins/{2}'.format( os.environ['DB_URL'], os.environ['NODENAME'], os.environ['DB_USER'] ), data='"{0}"'.format(os.environ['DB_PASSWORD']) ) else: resp = requests.put( '{0}/_config/admins/{1}'.format( os.environ['DB_URL'], os.environ['DB_USER'] ), data='"{0}"'.format(os.environ['DB_PASSWORD']) ) resp.raise_for_status() @classmethod def tearDownClass(cls): """ If necessary, clean up CouchDB instance once all tests are complete. """ if (os.environ.get('RUN_CLOUDANT_TESTS') is None and os.environ.get('DB_USER_CREATED') is not None): if os.environ.get('COUCHDB_VERSION') == '2.1.1': resp = requests.delete( '{0}://{1}:{2}@{3}/_node/{4}/_config/admins/{5}'.format( os.environ['DB_URL'].split('://', 1)[0], os.environ['DB_USER'], os.environ['DB_PASSWORD'], os.environ['DB_URL'].split('://', 1)[1], os.environ['NODENAME'], os.environ['DB_USER'] ) ) else: resp = requests.delete( '{0}://{1}:{2}@{3}/_config/admins/{4}'.format( os.environ['DB_URL'].split('://', 1)[0], os.environ['DB_USER'], os.environ['DB_PASSWORD'], os.environ['DB_URL'].split('://', 1)[1], os.environ['DB_USER'] ) ) del os.environ['DB_USER_CREATED'] del os.environ['DB_USER'] resp.raise_for_status() def setUp(self): """ Set up test attributes for unit tests targeting a database """ self.set_up_client() def set_up_client(self, auto_connect=False, auto_renew=False, encoder=None, timeout=(30,300)): self.user = os.environ.get('DB_USER', None) self.pwd = os.environ.get('DB_PASSWORD', None) self.use_cookie_auth = True self.iam_api_key = os.environ.get('IAM_API_KEY', None) if os.environ.get('RUN_CLOUDANT_TESTS') is None: self.url = os.environ['DB_URL'] admin_party = False if os.environ.get('ADMIN_PARTY') == 'true': admin_party = True self.use_cookie_auth = False # construct Cloudant client (using admin party mode) self.client = CouchDB( self.user, self.pwd, admin_party, url=self.url, connect=auto_connect, auto_renew=auto_renew, encoder=encoder, timeout=timeout ) else: self.account = os.environ.get('CLOUDANT_ACCOUNT') self.url = os.environ.get( 'DB_URL', 'https://{0}.cloudant.com'.format(self.account)) if os.environ.get('RUN_BASIC_AUTH_TESTS'): self.use_cookie_auth = False # construct Cloudant client (using basic access authentication) self.client = Cloudant( self.user, self.pwd, url=self.url, x_cloudant_user=self.account, connect=auto_connect, auto_renew=auto_renew, encoder=encoder, timeout=timeout, use_basic_auth=True, ) elif self.iam_api_key: self.use_cookie_auth = False # construct Cloudant client (using IAM authentication) self.client = Cloudant( None, # username is not required self.iam_api_key, url=self.url, x_cloudant_user=self.account, connect=auto_connect, auto_renew=auto_renew, encoder=encoder, timeout=timeout, use_iam=True, ) else: # construct Cloudant client (using cookie authentication) self.client = Cloudant( self.user, self.pwd, url=self.url, x_cloudant_user=self.account, connect=auto_connect, auto_renew=auto_renew, encoder=encoder, timeout=timeout ) def tearDown(self): """ Ensure the client is new for each test """ del self.client def db_set_up(self): """ Set up test attributes for Database tests """ self.client.connect() self.test_dbname = self.dbname() self.db = self.client._DATABASE_CLASS(self.client, self.test_dbname) self.db.create() def db_tear_down(self): """ Reset test attributes for each test """ self.db.delete() self.client.disconnect() del self.test_dbname del self.db def dbname(self, database_name='db'): return '{0}-{1}-{2}'.format(database_name, self._testMethodName, unicode_(uuid.uuid4())) def populate_db_with_documents(self, doc_count=100, **kwargs): off_set = kwargs.get('off_set', 0) docs = [ {'_id': 'julia{0:03d}'.format(i), 'name': 'julia', 'age': i} for i in range(off_set, off_set + doc_count) ] return self.db.bulk_docs(docs) def create_views(self): """ Create a design document with views for use with tests. """ self.ddoc = DesignDocument(self.db, 'ddoc001') self.ddoc.add_view( 'view001', 'function (doc) {\n emit(doc._id, 1);\n}' ) self.ddoc.add_view( 'view002', 'function (doc) {\n emit(doc._id, 1);\n}', '_count' ) self.ddoc.add_view( 'view003', 'function (doc) {\n emit(Math.floor(doc.age / 2), 1);\n}' ) self.ddoc.add_view( 'view004', 'function (doc) {\n emit(Math.floor(doc.age / 2), 1);\n}', '_count' ) self.ddoc.add_view( 'view005', 'function (doc) {\n emit([doc.name, doc.age], 1);\n}' ) self.ddoc.add_view( 'view006', 'function (doc) {\n emit([doc.name, doc.age], 1);\n}', '_count' ) self.ddoc.add_view( 'view007', 'function (doc) {\n emit(1, doc.name);\n}' ) self.ddoc.save() self.view001 = self.ddoc.get_view('view001') self.view002 = self.ddoc.get_view('view002') self.view003 = self.ddoc.get_view('view003') self.view004 = self.ddoc.get_view('view004') self.view005 = self.ddoc.get_view('view005') self.view006 = self.ddoc.get_view('view006') self.view007 = self.ddoc.get_view('view007') def create_search_index(self): """ Create a design document with search indexes for use with search query tests. """ self.search_ddoc = DesignDocument(self.db, 'searchddoc001') self.search_ddoc['indexes'] = {'searchindex001': { 'index': 'function (doc) {\n index("default", doc._id); \n ' 'if (doc.name) {\n index("name", doc.name, {"store": true}); \n} ' 'if (doc.age) {\n index("age", doc.age, {"facet": true}); \n} \n} ' } } self.search_ddoc.save() def load_security_document_data(self): """ Create a security document in the specified database and assign attributes to be used during unit tests """ self.sdoc = { 'admins': {'names': ['foo'], 'roles': ['admins']}, 'members': {'names': ['foo1', 'foo2'], 'roles': ['developers']} } self.mod_sdoc = { 'admins': {'names': ['bar'], 'roles': ['admins']}, 'members': {'names': ['bar1', 'bar2'], 'roles': ['developers']} } if os.environ.get('RUN_CLOUDANT_TESTS') is not None: self.sdoc = { 'cloudant': { 'foo1': ['_reader', '_writer'], 'foo2': ['_reader'] } } self.mod_sdoc = { 'cloudant': { 'bar1': ['_reader', '_writer'], 'bar2': ['_reader'] } } resp = self.client.r_session.put( '/'.join([self.db.database_url, '_security']), data=json.dumps(self.sdoc), headers={'Content-Type': 'application/json'} ) self.assertEqual(resp.status_code, 200) def create_db_updates(self): """ Create '_global_changes' system database required for testing against _db_updates """ self.DB_UPDATES = '_global_changes' try: self.client.create_database(self.DB_UPDATES, throw_on_exists=True) except CloudantClientException: self.delete_db_updates() self.create_db_updates() def delete_db_updates(self): """ Delete '_global_changes' system database used for _db_updates testing """ try: self.client.delete_database(self.DB_UPDATES) except CloudantClientException: pass def is_couchdb_1x_version(self): if os.environ.get('COUCHDB_VERSION') and os.environ.get('COUCHDB_VERSION').startswith('1'): return True else: # Get version from server info couchdb_info = json.loads(self.client.r_session.get(self.client.server_url).text) if couchdb_info and couchdb_info['version'].startswith('1'): return True else: return False
if my_document.exists(): print('SUCCESS!!') # In[38]: my_document = my_database['12345'] # Update the document content # This can be done as you would any other dictionary my_document['name'] = "G D Abhishek" my_document['age'] = 23 # You must save the document in order to update it on the database my_document.save() # In[39]: my_document = my_database['12345'] # Display the document print(my_document) # In[40]: # Delete a database using an initialized client client.delete_database('my_database') # In[41]: 1 + 2
# データベース名の取得 f = open('database_name.json', 'r') dbn = json.load(f) f.close() print dbn['name'] client = Cloudant(cred['credentials']['username'], cred['credentials']['password'], url=cred['credentials']['url']) # Connect to the server client.connect() # DB選択 db = client[dbn['name']] # Create a database using an initialized client # The result is a new CloudantDatabase or CouchDatabase based on the client client.delete_database(dbn['name']) # You can check that the database exists if db.exists(): print 'FAILED(^_^;;' else: print 'SUCCESS!!' # Disconnect from the server client.disconnect()
def getURLs(userIn): #replaced by server #userIn = input('Enter Search Parameter:') f = open('/Users/[email protected]/Desktop/output.txt', 'w') #From Lynda n = 0 url = 'https://www.lynda.com/search?q=' + userIn req = urllib2.Request(url, headers={'User-Agent': "Magic Browser"}) url = urllib2.urlopen(req).read() soup = BeautifulSoup(url, "html.parser") for line in soup.find_all('a'): if line.get('href').startswith('https://www.lynda.com/'): if n > 0: f.write(line.get('href')) f.write('\n') n = n + 1 #print(line.get('href')) p = 0 url = 'https://www.ted.com/search?q=' + userIn req = urllib2.Request(url, headers={'User-Agent': "Magic Browser"}) url = urllib2.urlopen(req).read() soup = BeautifulSoup(url, "html.parser") for line in soup.find_all('a'): if line.get('href').startswith('/read/') or line.get( 'href').startswith('/talks/') or line.get('href').startswith( '/read/'): if p > 0: f.write('https://www.ted.com' + line.get('href')) f.write('\n') p = p + 1 #print(line.get('href')) url = 'https://www.coursera.org/courses?languages=en&query=' + userIn + "&userQuery=" + userIn req = urllib2.Request(url, headers={'User-Agent': "Magic Browser"}) url = urllib2.urlopen(req).read() soup = BeautifulSoup(url, "html.parser") for line in soup.find_all('a'): if line.get('href').startswith('/learn/'): #if p > 0: f.write('https://www.coursera.org' + line.get('href')) f.write('\n') #print(line.get('href')) #p = p + 1 #print(line.get('href')) f.close() ##DB STUFF from cloudant.client import Cloudant from cloudant.error import CloudantException from cloudant.result import Result, ResultByKey client = Cloudant( "ae11f357-074c-42ec-88c0-d46fd1dbc8c8-bluemix", "1d9351f791650b12e456f49ed45005b03944e40e4be6066057450e616cb8382c", url= "https://*****:*****@ae11f357-074c-42ec-88c0-d46fd1dbc8c8-bluemix.cloudant.com" ) client.connect() databaseName = "urls" try: client.delete_database(databaseName) except CloudantException: print "There was a problem deleting '{0}'.\n".format(databaseName) else: print "'{0}' successfully deleted.\n".format(databaseName) databaseName = "urls" myDatabase = client.create_database(databaseName) if myDatabase.exists(): print "'{0}' successfully created.\n".format(databaseName) #Read file and put into sample data with open("/Users/[email protected]/Desktop/output.txt", "r") as ins: sampleData = [] for line in ins: sampleData.append(line) #sampleData = [ # [1, "one", "boiling", 100], # [2, "two", "hot", 40], # [3, "three", "warm", 20], # [4, "four", "cold", 10], # [5, "five", "freezing", 0] #] # Create documents using the sample data. # Go through each row in the array for document in sampleData: # Retrieve the fields in each row. url = document # Create a JSON document that represents # all the data in the row. jsonDocument = { "url": url, #"name" : website, } # Create a document using the Database API. newDocument = myDatabase.create_document(jsonDocument) # Check that the document exists in the database. if newDocument.exists(): print "Document '{0}' successfully created.".format(url) f = open('/Users/[email protected]/Desktop/output.json', 'w') result_collection = Result(myDatabase.all_docs, include_docs=True) # "Retrieved full document:\n{0}\n".format(result_collection[0]) f.write("[") for result in result_collection: f.write('"' + str(result) + '"') f.write(",\n") f.write('""') f.write("]") #end_point = '{0}/{1}'.format("<url>", databaseName + "/_all_docs") #params = {'include_docs': 'true'} #response = client.r_session.get(end_point, params=params) #print "{0}\n".format(response.json()) f.close() f = open('/Users/[email protected]/Desktop/output.json', 'r') jsonURLs = f.read() f.close() try: client.delete_database(databaseName) except CloudantException: print "There was a problem deleting '{0}'.\n".format(databaseName) else: print "'{0}' successfully deleted.\n".format(databaseName) client.disconnect() return jsonURLs
f.close() print "クラウダントへの接続" dbname = cnf['database'] print dbname client = Cloudant(cred['credentials']['username'], cred['credentials']['password'], url=cred['credentials']['url']) client.connect() # DBが存在していたら削除 print "既存データベースの削除" try: db = client[dbname] if db.exists(): client.delete_database(dbname) except: print "新規データベースの作成" # DBを新規作成 db = client.create_database(dbname) # 失敗したら終了 if db.exists(): print 'SUCCESS!! Database creation on cloudant' else: sys.exit() # エクセルシートからのCSVデータを読んで # NLCトレーニングデータを作成する