Advertisement
Guest User

Untitled

a guest
Oct 14th, 2018
110
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.40 KB | None | 0 0
  1. from pymongo import MongoClient
  2. from urllib.parse import quote_plus  # needed to escape username/pass correctly
  3.  
  4. def main():
  5.     # User creatd in Atlas UI
  6.     username = quote_plus('user')
  7.     password = quote_plus('60XlSCNwFB2spygT') # atlas auto generated password
  8.  
  9.     # connection string given in the Atlas UI.
  10.     connection_str = 'mongodb+srv://{}:{}@cluster0-kesdd.mongodb.net/test?retryWrites=true&authSource=admin'.format(username, password)
  11.  
  12.     # object that lets you do pretty much anything with your db
  13.     client = MongoClient(connection_str)
  14.  
  15.     # I created a database called MyDb in the Atlas UI
  16.     db = client.MyDb
  17.  
  18.     # I created a test collection in the Atlas UI
  19.     collection = db.TestCollection
  20.  
  21.     # could read from some file or something
  22.     scrabble_words = get_random_junk()
  23.  
  24.     # actually insert them into the collection
  25.     collection.insert_many(scrabble_words)
  26.  
  27.  
  28. def get_random_junk():
  29.     """function that generates random garbage"""
  30.     import string
  31.     import random
  32.     letters = list(string.ascii_lowercase)
  33.     random.shuffle(letters)
  34.     points = list(range(0, 100))
  35.     random.shuffle(points)
  36.     junk = []
  37.     for _ in range(100):
  38.         word = ''.join(random.sample(letters, random.randint(1,15)))
  39.         score = random.choice(points)
  40.         junk.append({'word' : word, 'score' : score})
  41.     return junk
  42.  
  43. if __name__ == '__main__':
  44.     main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement