Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- from pymongo import MongoClient
- from urllib.parse import quote_plus # needed to escape username/pass correctly
- def main():
- # User creatd in Atlas UI
- username = quote_plus('user')
- password = quote_plus('60XlSCNwFB2spygT') # atlas auto generated password
- # connection string given in the Atlas UI.
- connection_str = 'mongodb+srv://{}:{}@cluster0-kesdd.mongodb.net/test?retryWrites=true&authSource=admin'.format(username, password)
- # object that lets you do pretty much anything with your db
- client = MongoClient(connection_str)
- # I created a database called MyDb in the Atlas UI
- db = client.MyDb
- # I created a test collection in the Atlas UI
- collection = db.TestCollection
- # could read from some file or something
- scrabble_words = get_random_junk()
- # actually insert them into the collection
- collection.insert_many(scrabble_words)
- def get_random_junk():
- """function that generates random garbage"""
- import string
- import random
- letters = list(string.ascii_lowercase)
- random.shuffle(letters)
- points = list(range(0, 100))
- random.shuffle(points)
- junk = []
- for _ in range(100):
- word = ''.join(random.sample(letters, random.randint(1,15)))
- score = random.choice(points)
- junk.append({'word' : word, 'score' : score})
- return junk
- if __name__ == '__main__':
- main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement