# In[14]: # マーケット一覧から先物を除外:product_code最後の9文字で判断する。 r = re.compile('[0-9]{2}[A-Z]{3}[0-9]{2}') markets = list( filter(lambda item: not r.match(item['product_code'][-9:]), markets_all[1])) # In[15]: # Mongo DB mongo_db_name = 'bf' mongo_dataset_name = '_ticker_board01' dbc = MongoClient() # Open DB db = dbc[mongo_db_name] # Dataset ds = db[mongo_dataset_name] # #ds.create_index('_timestamp') # In[16]: max_age_of_data = 21600 # 6 hours. # In[ ]: # とりあえず 500 とってみよう。500xproducts
#MongoDB Example import pymongo import MongoClient import datetime #Open connection with localhost and port 27017 client = MongoClient("localhost", 27017) #Access test Database db = client.test_database #Get the test collection collection = db.test_collection #Use Python Dictionary to create document post = {"author": "Mike", "text": "This is a test", "tags": ["mongodb", "python", "pymongo"], "date": datetime.datetime.utcnow() } #Insert a Document posts = db.posts post_id = posts.insert_one(post).inserted_id post_id
import pandas as pd import GooglePlace as gp import MongoClient as mongo_client train = pd.read_csv("<Train CSV file location>") coordinates = train.ix[1:len(train), ['X', 'Y']] for index, coordinate in coordinates.iterrows(): latitude = str(coordinate['Y']) longitude = str(coordinate['X']) radius = 3000 authKey = "<Google API Key>" # Refer below link for set of valid place types supported by google places API # https://developers.google.com/places/supported_types types = "police|post_office|bus_station|train_station|taxi_stand|airport|fire_station|bank|gas_station|school|university|night_club|casino|shopping_mall|convenience_store|lodging" #types="cafe" mongo_collection = mongo_client.mongodb_connections( "sfocrimelocs", "neighbourhood") if mongo_client.isRecordsExists(mongo_collection, latitude, longitude) == False: json_data = gp.GooglePlace(latitude, longitude, radius, types, authKey) # Get MongoDB connection and collection to insert json_data['latitude'] = latitude json_data['longitude'] = longitude mongo_collection.insert(json_data) print "inserted" else: print "records already exists"
from werkzeug.security import generate_password_hash, check_password_hash from flask_bootstrap import Bootstrap from flask_wtf import FlaskForm from werkzeug.urls import url_parse import re, os import pymongo import MongoClient app = Flask(__name__) UPLOAD_FOLDER = "uploads" ALLOWED_EXTENSIONS = {'txt', 'pdf', 'png', 'jpg', 'jpeg', 'gif'} basedir = os.path.abspath(os.path.dirname(__file__)) #client = pymongo.MongoClient("mongodb+srv://wad:[email protected]/test") #db = client["wad"] #users = db["users"] client = MongoClient('mongodb', 27017) db = client.wad users = db.users users.create_index("username") app.secret_key = "super secret key" login_manager = LoginManager(app) login_manager.login_view = 'login' app.config['UPLOAD_FOLDER']= UPLOAD_FOLDER bootstrap = Bootstrap(app) @app.route('/favicon.ico') def favicon(): return send_from_directory(os.path.join(app.root_path, 'static'), 'favicon.ico', mimetype='favicon.ico')
ffrom pymongo import MongoClient MONGO_URI = 'mongodb://localhost' client = MongoClient(MONGO_URI) db = client['musicalInstrumentsShop'] collectionCategory = db['category'] collectionCategory.insert_many([ { "nombre": "Piano Digital", }; { "nombre": "Guitarra Electrica" }; { "nombre":"Bateria Completa" }; { "nombre":"Flauta" }; { "nombre":"Violin" };
In [8]: y = x**2 In [9]: print(x) [ 0. 1. 2. 3. 4. 5. 6. 7. 8. 9. 10.] In [10]: print(y) [ 0. 1. 4. 9. 16. 25. 36. 49. 64. 81. 100.] In [11]: figure, axis = plt.subplots(1, 1) In [12]: axis.plot(x, y) Out[12]: [<matplotlib.lines.Line2D at 0x7f53c6a1c6d8>] In [13]: figure.savefig("x_squared.svg") In [14]: db = MongoClient("mongodb://mongo0.scitecha.com,mongo1.scitecha.com,mongo2.mrocha.org/mrocha?replicaSet=rs0").mrocha In [15]: user_data = list(db.users.find({}, {"profile.TracyProgress": 1})) In [16]: tprogress = [user["profile"]["TracyProgress"] for user in user_data] In [17]: figure, axis = plt.subplots(1, 1) In [18]: axis.hist(tprogress, bins=10) Out[18]: (array([3., 1., 5., 7., 2., 3., 5., 3., 0., 1.]), array([ 3. , 11.8, 20.6, 29.4, 38.2, 47. , 55.8, 64.6, 73.4, 82.2, 91. ]), <a list of 10 Patch objects>) In [19]: figure.savefig('TracyProgress_histogram.svg') In [21]: figure.savefig('TracyProgress_histogram.svg'
# Setting up environment to use existing models import os, django os.environ.setdefault("DJANGO_SETTINGS_MODULE", "uber_challenge.settings django.setup() # Import models from film.models import # Setup MongoDB from pymongo import MongoClient import json import requests # Connect to database client = MongoClient() db = client.uber_challenge coll = db.film # Remove old data before getting new data coll.remove() # Dump the data from provided API endpoint r = requests.get('https://data.sfgov.org/resource/yitu-d5am.json') data = json.loads(r.text) for i in range(len(data)): curr = data[i] # @TODO: Create Models According to Data and Save to the DataBase
train = pd.read_csv("<Train CSV file location>") coordinates = train.ix[1:len(train), ['X', 'Y']] for index, coordinate in coordinates.iterrows(): latitude = str(coordinate['Y']) longitude = str(coordinate['X']) radius = 3000; authKey = "<Google API Key>" # Refer below link for set of valid place types supported by google places API # https://developers.google.com/places/supported_types types = "police|post_office|bus_station|train_station|taxi_stand|airport|fire_station|bank|gas_station|school|university|night_club|casino|shopping_mall|convenience_store|lodging" #types="cafe" mongo_collection = mongo_client.mongodb_connections("sfocrimelocs","neighbourhood") if mongo_client.isRecordsExists(mongo_collection, latitude, longitude) == False: json_data = gp.GooglePlace(latitude, longitude, radius, types, authKey) # Get MongoDB connection and collection to insert json_data['latitude']=latitude json_data['longitude']=longitude mongo_collection.insert(json_data); print "inserted" else: print "records already exists"
> db.user.find() { "_id" : ObjectId("59a8034564e0acb13483d514"), "user" : "jin", "age" : 31, "email" : "*****@*****.**", "addr" : [ "GZ", "SZ" ] } { "_id" : ObjectId("59a8034564e0acb13483d515"), "user" : "nan", "age" : 26, "email" : "*****@*****.**", "addr" : [ "NJ", "AH" ] } 上面的命令成功的删除所有地址包含 "CD" 的用户,共删除了两条记录。 MongoDB 基本操作视频: Play Video Python 操作 MongoDB 在 Python 中访问 MongoDB 数据库,主要通过 PyMongo 软件包。该软件包含一个 MongoClient 对象,可以用于建立 MongoDB 客户端。在 IPython 中输入下面的示例代码,创建客户端: In [2]: from pymongo import MongoClient In [3]: client = MongoClient('127.0.0.1', 27017) In [4]: db = client.shiyanlou 前文中,已经知道 MongoDB 默认监听在 127.0.0.1:27017 地址上,所以在创建 client 时,使用了该地址。客户端创建成功后,我们通过 client.shiyanlou 方式选择了 shiyanlou 数据库。接着就可以查询所有的文档了: In [13]: for user in db.user.find(): ...: print(user) ...: {'_id': ObjectId('59a8034564e0acb13483d514'), 'user': '******', 'age': 31.0, 'email': '*****@*****.**', 'addr': ['GZ', 'SZ']} {'_id': ObjectId('59a8034564e0acb13483d515'), 'user': '******', 'age': 26.0, 'email': '*****@*****.**', 'addr': ['NJ', 'AH']} 通过 PyMongo 插入数据也非常简单,直接通过 insert_one 方法: In [14]: user = {'name': 'Aiden', 'age': 30, 'addr': ['CD', 'SH', 'BJ']} In [15]: db.user.insert_one(user) Out[15]: <pymongo.results.InsertOneResult at 0x10730aa08>