Tech C**P
Some useful string methods, variables: string.ascii_letters: in case you want to use alpha characters a-zA-Z. The output is: abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ string.ascii_lowercase: abcdefghijklmnopqrstuvwxyz string.ascii_uppercase:…
Generate a sample string (bonus coupon) from
This function returns a random string from
GET THE IDEA!
#python #string #ascii_uppercase #digits #random #random_choice #range
string functions
and removing misguiding characters from the final list:def generate_coupon(coupon_code_length=10):
alphanum = list(string.ascii_uppercase + string.digits)
alphanum.remove('L')
alphanum.remove('I')
alphanum.remove('1')
alphanum.remove('0')
alphanum.remove('O')
return ''.join(random.choice(alphanum) for i in range(coupon_code_length))
This function returns a random string from
ascii_uppercase
and string.digits
. This is one usages out of very many usages of string module public variables. :)GET THE IDEA!
#python #string #ascii_uppercase #digits #random #random_choice #range
In order to get a random document from MongoDB collection you can use aggregate framework:
Read more here: https://www.mongodb.com/blog/post/how-to-perform-random-queries-on-mongodb
This method is the fastest and most efficient way of getting random data from a huge database like 100 M records.
#mongodb #mongo #aggregate #sample #random
db.users.aggregate( [ { $sample: { size: 1 } } ] )
NOTE:
MongoDB 3.2 introduced $sample
to the aggregation pipeline.Read more here: https://www.mongodb.com/blog/post/how-to-perform-random-queries-on-mongodb
This method is the fastest and most efficient way of getting random data from a huge database like 100 M records.
#mongodb #mongo #aggregate #sample #random
MongoDB
How to Perform Random Queries on MongoDB | MongoDB Blog