Tech C**P
15 subscribers
161 photos
9 videos
59 files
304 links
مدرس و برنامه نویس پایتون و لینوکس @alirezastack
Download Telegram
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 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:

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