This repository has been archived by the owner on Jun 5, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Agregado Script de Python para actualizar toda la collection de Units…
… en Mongo
- Loading branch information
Showing
1 changed file
with
69 additions
and
0 deletions.
There are no files selected for viewing
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
import json | ||
from pymongo import MongoClient | ||
|
||
|
||
################################ | ||
# Decorators | ||
################################################################### | ||
|
||
def catch_exceptions(func): | ||
def exceptions_inner(*args, **kwargs): | ||
try: | ||
return func(*args, **kwargs) | ||
except Exception: | ||
close_connection() | ||
raise | ||
return exceptions_inner | ||
|
||
|
||
################################ | ||
# DB Operations | ||
################################################################### | ||
|
||
def create_connection(): | ||
print "Mongo DB - Connection Created" | ||
return MongoClient('localhost', 27017) | ||
|
||
|
||
def close_connection(db_client): | ||
print "Mongo DB - Connection Closed" | ||
db_client.close() | ||
|
||
def update_units(db_client): | ||
print "Mongo DB - Updating Units Collection..." | ||
db = db_client.ffbe | ||
units_collection = db.units | ||
units_collection.remove() | ||
|
||
units = get_units() | ||
result = units_collection.insert_many(units) | ||
|
||
|
||
################################ | ||
# Data Processing | ||
################################################################### | ||
|
||
def format_data(data): | ||
def with_id(properties, item_id): | ||
properties['id'] = item_id | ||
return properties | ||
|
||
return [with_id(properties, item_id) for item_id, properties in data.items()] | ||
|
||
@catch_exceptions | ||
def get_units(): | ||
with open('./node_modules/data/units.json') as units_json: | ||
return format_data(json.load(units_json)) | ||
|
||
|
||
################################ | ||
# Main | ||
################################################################### | ||
|
||
if __name__ == "__main__": | ||
db_client = create_connection() | ||
|
||
update_units(db_client) | ||
|
||
close_connection(db_client) | ||
|