Obscure Python Libraries I picked 4 Python libraries that you probably haven't heard of that I find useful. I'll show you how to use them and then we'll make a fun little program with them. Great Scott Marty! Introducing Delorean You mean Delorean is more than a time-traveling car? Yes, random Internet Stranger, it is. Datetime objects in Python can be a little difficult to work with. Delorean is a library that makes dealing with Datetime objects easier. According to the , Delorean is a library for clearing up the inconvenient truths that arise dealing with datetimes in Python. Understanding that timing is delicate enough of a problem Delorean hopes to provide a cleaner less troublesome solution to shifting, manipulating, generating datetimes. documentation Fire Up The Flux Capacitor Since the docs for Delorean are pretty good, so I won't go into a lot of detail here. Below are a few examples of using it. (You can run these yourself in your Python interpreter or IDE.) before you start. Buy your Flux Capacitor delorean Delorean d = Delorean() d d = d.shift( ) d d.datetime d.date from import # create delorean obj # set obj to central timezone "US/Central" # print datetime # print date In the example above, we create a Delorean object, set it to , and then print its datetime and date values. Central timezone d = Delorean() d d.next_tuesday() d.last_tuesday( ).midnight # create obj # print next tuesday's date # print datetime two tuesdays ago at midnight 2 In our second example, we print the datetime of next Tuesday based on the current date set by Delorean. Finally, we count back to 2 previous Tuesdays and get the datetime at midnight. That would've been painful to do manually. Bears and Fuzzy Pattern Matching. Introducing FuzzyWuzzy Our next library, FuzzyWuzzy, gives us a way to easily fuzzy match strings. Fuzzy matching involves comparing the input to a variety of values and computing their amount of "sameness". The gives more information. FuzzyWuzzy documentation fuzzywuzzy fuzz fuzzywuzzy process fuzz.ratio( , ) fuzz.partial_ratio( , ) fuzz.token_sort_ratio( , ) choices = [ , , , ] process.extract( , choices, limit= ) process.extractOne( , choices) from import from import "this is a test" "this is a test!" "this is a test" "this is a test!" "fuzzy was a bear" "fuzzy fuzzy was a bear" "Atlanta Falcons" "New York Jets" "New York Giants" "Dallas Cowboys" "new york jets" 2 "cowboys" With the , we compare "this is a test" to "this is a test!" which gives us a score of 97 (97% the same). , scoring on the first string existing in the second, gives us 100 because 100% of the first string exists in the second. , using "tokens" to compare the first set of words compared to the second, gives us a score of 84. fuzz.ratio fuzz.partial_ratio fuzz.token_sort_ratio The second section of the code compares the first string to matches in a list of strings. returns a list of tuples containing the matched string from the list and the score. (limit sets the number of matches returned.) does the same thing but only returns one match which is always the (highest score). process.extract process.extractOne best match Wink, Smile, Thumbs Up. Introducing Emoji Emoji is a Python library that unsurprisingly allows us to display emojis. is easy to follow but it doesn't give much detail. Emoji's documentation emoji print(emoji.emojize( )) print(emoji.emojize( , use_aliases= )) print(emoji.emojize( )) print(emoji.emojize( ,variant= )) import 'Python is :thumbs_up:' 'Python is :thumbsup:' True "Python is fun :red_heart:" "Python is fun :red_heart:" "emoji_type" There's not much to Emoji but it does its job well. replaces any emoji code with the actual emoji symbol. allows you to use alternate names for the emoji (:thumbs_up: vs :thumbsup:). seems to force your computer to load an actual emoji in the terminal instead of an ascii code character. (That's just a guess based on my experimenting with it but it doesn't say anything in the docs.) emoji.emojize use_aliases=True variant The Last Stop On Our Magical Mystery Tour. Introducing Inflect Inflect is a bit harder to explain because it does a lot. It's a toolbox that helps us build messages/sentences to display to the user that is "smart". If you give Inflect a verb, nouns or adjectives, it's smart enough to return the correct version of the word. For example, the plural form of "dog" is "dogs". Inflect also allows you to switch numbers (1, 100, 1000) to words (one, one hundred, one thousand). See the for more information. Inflect documentation inflect inflector = inflect.engine() num_words = inflector.number_to_words( ) num_words num_turkeys = print( , num_turkeys, inflector.plural(word, num_turkeys)) num_turkeys = print( , num_turkeys, inflector.plural(word, num_turkeys)) n1 = n2 = print( inflector.plural_noun( , n1), inflector.plural_verb( , n1), inflector.plural_adj( , n2), inflector.plural_noun( , n2) ) import 1632976 1 "I saw" 17 "I saw" 1 2 "I" "saw" "my" "saw" Let's Build A Project! Now that we've seen how to use these libraries, let's put them all together to build a mini-project. Our project is going to be a very small version of MadLibs. I'll show you the code and then give a tiny bit of explanation. random datetime timedelta delorean emoji inflect delorean Delorean fuzzywuzzy process verbs = [ , , , , , , , , , , , ] nouns = [ , , , , , , , , , , , ] descriptors = [ , , , , , , , , , , , ] places = [ , , , , , , , , , , , ] subjects = { : [ , , , , , , , , , , , ], : [ , , , , , , , , , , , ] } adjectives = [ , , , , , , , ] prepositions = [ , , , , , , ] intervals = [ , , , ] feelings = { : , : , : , : , : , : , : , : , : } lunch_stuff = [ , , , ] num_stories = self.inflector = inflect.engine() self.num_stories = num_stories process.extractOne(noun, self.lunch_stuff)[ ] >= : emotion = random.choice( list(self.feelings) ) ( emotion, self.emoji_by_emotion(emotion) ) selected_emoji = self.feelings.get(emotion) emoji.emojize(selected_emoji, use_aliases= ) num = random.randint( , ) num % == : num = random.randint( , ) num % == : num self.inflector.number_to_words(num) random.choice(self.prepositions) random.choice(self.descriptors) random.choice(self.places) self.inflector.plural_noun( random.choice(self.subjects.get(idx)), num ) adjective = random.choice(self.adjectives) num == : adjective self.inflector.plural_adj(adjective) noun = random.choice(self.nouns) num == : noun self.inflector.plural(noun) self.inflector.plural_verb( random.choice(self.verbs), num ) num = self.single_or_plural() [ self.random_adjective(num), self.random_subject(subject_idx, num), self.random_verb(num) ] num = self.single_or_plural() subject = self.random_subject(subject_idx, num) num == : subject += : subject += [ self.random_adjective(num), subject, self.random_place() ] num = self.random_number() [ self.random_adjective(num), self.number_to_words(num), self.random_descriptor(), self.random_noun(num) ] self.delorean = Delorean() self.delorean = self.delorean.shift( ) interval = random.choice(self.intervals) num = random.randint( , ) self.delorean += timedelta( **{interval: num} ) out = out sentences = orig.split( ) sentences2 = [sentence[ ].capitalize() + sentence[ :] sentence sentences] .join(sentences2) part1 = self.adjective_subject_verb( ) part2 = self.adjective_num_descriptor_noun() part3 = self.adjective_subject_place( ) preposition = self.random_preposition() time = self.timeframe() (emotion, feeling) = self.random_emotion() lunch = out = self.capitalize_sentences( ) print(out) __name__ == : story_maker = Story( int(input( )) ) i range(story_maker.num_stories): story_maker.create_story() import from import import import import from import from import : class Story 'runs' 'swims' 'walks' 'flies' 'paints' 'sleeps' 'eats' 'drives' 'spells' 'bakes' 'slices' 'waters' 'taco' 'elephant' 'cheeseburger' 'shoe' 'ogre' 'broccoli' 'lightbulb' 'cloud' 'coffee' 'mailbox' 'slime' 'hat' 'green' 'tired' 'huge' 'sleepy' 'delicious' 'stinky' 'funky' 'hairy' 'moldy' 'expensive' 'wet' 'droopy' 'house' 'stadium' 'theater' 'store' 'bakery' 'amusement park' 'canyon' 'moon' 'valley' 'circus tent' 'forest' 'mountain' 0 'boy' 'girl' 'rabbit' 'platypus' 'astronaut' 'horse' 'alligator' 'clown' 'dentist' 'beekeeper' 'mushroom' 'monster' 1 'candle' 'toe' 'headache' 'spoon' 'cat' 'toaster' 'racecar' 'baseball' 'toothbrush' 'towel' 'sandbox' 'grampa' 'this' 'that' 'a' 'my' 'your' 'his' 'her' 'the' 'at' 'in' 'under' 'beside' 'behind' 'above' 'from' 'minutes' 'hours' 'days' 'weeks' 'sad' ':frowning:' 'happy' ':smile:' 'tired' ':tired_face:' 'sleepy' ':sleepy:' 'angry' ':angry:' 'worried' ':worried:' 'confused' ':confused:' 'disappointed' ':disappointed:' 'amazed' ':open_mouth:' 'Chicken Tacos' 'Cheeseburgers' 'Steamed Broccoli' 'Black Coffee' 1 : def __init__ (self, num_stories = ) 1 : def about_lunch (self, noun) if 1 75 return 'is' return 'isn\'t' : def random_emotion (self) return : def emoji_by_emotion (self, emotion) return True : def single_or_plural (self) 1 10000 if 2 0 return 1 return 2 : def random_number (self) 1 10000 if 3 0 return 1 return : def number_to_words (self,num) return : def random_preposition (self) return : def random_descriptor (self) return : def random_place (self) return : def random_subject (self, idx, num) return : def random_adjective (self, num) if 1 return return : def random_noun (self, num) if 1 return return : def random_verb (self, num) return : def adjective_subject_verb (self, subject_idx) return : def adjective_subject_place (self, subject_idx) if 2 '\'' else '\'s' return : def adjective_num_descriptor_noun (self) return : def timeframe (self) "US/Central" 1 10 f'In , it will be ' {num} {interval} {self.delorean.datetime.strftime( )} "%a %b %d, %Y %I:%M %p" return : def capitalize_sentences (self, orig) ". " 0 1 for in return '. ' : def create_story (self) 0 1 f'The first sentence about lunch.' {self.about_lunch(part2[ ])} 3 f" .\n .\nI\'m .\n \n\n" { .join(part1)} ' ' { .join(part2)} ' ' {preposition} { .join(part3)} ' ' {time} {feeling} {emotion} {lunch} if '__main__' 'How many stories: ' for in Basic Overview Ask the user how many "stories" he/she wants to create. Input: Select whether a noun is singular or plural and randomly get an adjective, subject, and verb with the correct choice of singular or plural. Random adjective, subject, verb: Randomly select an adjective, number, descriptor, and noun. Get more random words: You know the drill by now. Pick another random adjective, subject, and place: Randomly choose a preposition from our list. Preposition: Set the timezone and randomly set a time in the future. Use our Delorean object: Pick a random feeling and an associated emoji. Express yourself: Fuzzy matches the first sentence against our list of food words. Does the first sentence contain a food word? Concatenate all of the sentences together and capitalize the first word of each one. Capitalization: Print out the result. Display: Continue the process for each other story we need to create. Repeat: Support Me By Buying Me A Coffee! If you find this tutorial helpful, please consider . Thanks! buying me a coffee Sample Output Previously published at https://www.iceorfire.com/post/four-useful-python-libraries-you-dont-know-about