def sort(artist, single_id, direction): singles = list_for_artist(artist.id) if len(singles) < 2: raise exceptions.InvalidData( message='too few singles to perform a sort') if direction not in ['up', 'down']: raise exceptions.InvalidData('invalid direction') # get index of track being moved track_index = next((i for i, t in enumerate(singles) if t.id == single_id), None) if track_index is None: raise exceptions.InvalidData("invaid single id") # determine track to "bump" (i.e. swap sorts with) if direction == 'up': if track_index == 0: track_to_bump = singles[-1] else: track_to_bump = singles[track_index - 1] else: if track_index == len(singles) - 1: track_to_bump = singles[0] else: track_to_bump = singles[track_index + 1] # swap their sorts track_to_move = singles[track_index] current_sort = track_to_move.sort track_to_move.sort = track_to_bump.sort track_to_bump.sort = current_sort #print('move track {0} from {1} to {2}'.format(track_to_move.title, current_sort, track_to_move.sort)) #print('bump track {0} to {1}'.format(track_to_bump.title, track_to_bump.sort)) # persist changes to both tracks in transaction updates = [ _make_sort_update(track_to_move), _make_sort_update(track_to_bump) ] # print(updates) db.get_client().transact_write_items(TransactItems=updates) # re-sort list singles.sort(key=lambda i: i.sort) return singles
def main(): client = get_client() result = client.query('select location, time, temperature from weather;') print len(result[0]['points']) for item in result[0]['points']: print item break
def get_verse_from_db(chapter_number=None, verse_number=None): try: client = get_client() database = client['gita-db'] gita_collection = database['gita-verses'] if chapter_number: query = dict() query['chapter'] = int(chapter_number) if verse_number: query['verse_number'] = int(verse_number) verse = gita_collection.find_one(query) client.close() if verse: return jsonify(verse), 200 else: return {'error_message': 'Verse {} not found in '\ 'chapter {}'.format(verse_number, chapter_number)}, 404 verses = gita_collection.find(query) client.close() if verses: return jsonify(list(verses)), 200 else: return {'error_message': 'Chapter {} ' \ 'not found'.format(chapter_number)}, 404 return { 'error_message': 'Verse {} not found in chapter {}'.format(verse_number, chapter_number) }, 404 except Exception: return {'error_message': 'Something went wrong!'}, 500
def populate_db(): client = get_client(db_host) database = client['gita-db'] collection = database['gita-verses'] with open('gita.json') as file: verses = json.loads(file.read()) for verse in verses: collection.insert_many(verses)
def signup(credentials): try: client = get_client(db_host) database = client['gita-db'] user_collection = database['users'] if not user_collection.find_one({'username': credentials['username']}): credentials['password'] = md5(credentials['password'].encode()).hexdigest() user_collection.insert_one(credentials) return {} return {'error_message': 'User already exists. Please login'}, 400 except Exception: return {'error_message': 'Something went wrong while signing up'}, 500
def login(credentials): try: client = get_client(db_host) database = client['gita-db'] user_collection = database['users'] credentials['password'] = md5(credentials['password'].encode()).hexdigest() user = user_collection.find_one(credentials) if user: return user, 200 else: return {'error_message': 'Invalid credentials'}, 401 except Exception: return {'error_message': 'Something went wrong while logging in'}, 500
def main(): args = get_args() client = get_client( host=args["host"], port=args["port"], user=args["user"], pwd=args["pwd"], db=args["db"], legacy=args["legacy"], ) if args["action"] == "load" or args["input"] is not None: load(args, client) else: dump(args, client)
def index_user(id): user = database.get_user(id) user_email = user['email'] token = db.create_token(user['dbkey'],user['dbsecret']) ds = database.DataStore() session = db.get_session() client = db.get_client(user,session) blacklisted_paths = ['/1Password.agilekeychain'] ignore_extensions = ['.ppk','.pem','.pgp','.gpg','.ssh'] index_path = get_index_dir(id) ix = None if not os.path.exists(index_path): print 'Creating index dir:',index_path os.mkdir(index_path) ix = create_in(index_path,get_schema()) else: ix = open_dir(index_path) def get_path(p): path_data = ds.get_path_for_user(id,p) if path_data: print 'returning path from db' hash = path_data['metadata']['hash'] try: x = client.metadata(p,hash=hash) except rest.ErrorResponse,e: if e.status == 304: #Not modified - return last stored metadata return path_data else: raise metadata = client.metadata(p) path_data = ds.set_path_for_user(id,p,metadata) return path_data
import calendar import requests import time import ujson from utensils import dictutils from db import get_client ts = calendar.timegm(time.gmtime()) response = requests.get('https://query.yahooapis.com/v1/public/yql?q=select%20item.condition%20from%20weather.forecast%20where%20woeid%20%3D%2012589342&format=json&env=store%3A%2F%2Fdatatables.org%2Falltableswithkeys') obj = ujson.loads(response.content) temp = int(dictutils.get_dotted(obj, 'query.results.channel.item.condition.temp')) client = get_client() json_body = [{ "points": [ ["new york", ts, temp], ], "name": "weather", "columns": ["location", "time", "temperature"] }]