Python

Useful Modules & Recipes

44 flashcards · answers and spaced-repetition review in the KnowCard app

You want to open a help page in the user's actual default browser from a script, with no GUI dependency. What does the standard library give you?
Your CLI prompts for a password with input(). A reviewer flags it as a security bug. Why, and what's the fix?
What's the difference in what gets shown on screen between getpass.getpass() and input() while the user types?
Which function returns the login name of the current OS user, without you parsing environment variables yourself?
What kind of object is a generator that uses value = (yield) rather than yield expr?
What does this print? ``` def printer(): while True: v = (yield) print(v) p = printer() p.send(87) ```
Before you can p.send(87) into a consuming generator, what one step must come first and why?
What's a clean way to avoid manually priming every consuming generator with next() before send()?
When you write a consuming generator, why must yield be wrapped in parentheses in lines like a = (yield) + 10?
How do you raise an exception inside a paused generator from the calling code, and where does it surface?
How do you build a processing pipeline out of consuming generators, and which end do you create first?
What does this pipeline print on the first f.send(1)? ``` p = output() # prints what it gets f = filter_lift(10,p) # adds 10 f = filter_mean(2,f) # mean of last 2 f.send(1) ```
You did pip install PIL and your import fails. What's actually going on with Pillow's naming?
In Pillow, how do you decide the output file format when saving an image — is there a format argument?
In Pillow, how do you read and change a single pixel's color, and what does indexing actually operate on?
What are the three core attributes a Pillow Image exposes after Image.open(), and what do they tell you?
In Pillow, what four numbers does crop() take, and what region do they define?
Pillow has both transpose() and rotate(). When do you reach for which?
In Pillow, you want a quick per-pixel threshold/brightness tweak with your own function. Which method, and what's its limitation?
getpass.getpass() takes an optional stream argument for its prompt. What's the cross-platform catch?
Using the cmd module, how do you add a command named 'date' to an interactive console, set its prompt, and start the loop?
In a cmd.Cmd command method, what determines whether the console keeps accepting commands or quits?
In a cmd.Cmd console, how do you provide per-command help text that appears for 'help date' or '? date'?
You have lst = [1, [2, 3]], do c = copy.copy(lst), then c[1].append(9). Does lst's inner list change? Why?
Are there objects that copy.copy and copy.deepcopy won't actually duplicate?
In Pillow, how do you build a blank canvas of a chosen size and composite an existing Image onto it?
Pillow's img.rotate(30) leaves black wedges in the corners. Why, and how do you keep the whole rotated image? Also, what does resize return?
In Pillow, what's the difference between applying ImageFilter.GaussianBlur and ImageFilter.FIND_EDGES?
You want to cut an image's contrast to half in Pillow. Which module, and what does the enhance factor mean?
In Pillow, how do you draw a rectangle and write text in a specific TrueType font onto an existing image?
You've edited an image with Pillow and want to show it in a Tkinter or PyQt GUI. What does Pillow give you?
You binary-read 8 bytes from a file and need two 32-bit integers out of them. What turns those raw bytes into Python numbers?
In a struct format string, what's the difference between h/H, i/I, and q/Q — and which case is signed?
Beyond unpack, the struct module has pack, pack_into, unpack_from, and calcsize. What does each do?
A third-party library floods your run with DeprecationWarnings. How do you globally silence them — or instead make them stop the program so you can catch them?
Why can't you recover a password from its stored hash, and what does it mean for a hash to be 'avalanche-like'?
Which hash algorithms are guaranteed available in hashlib, and which two should you avoid in security-relevant code?
You do m = hashlib.md5(b'Hello world'). What's the difference between m.digest() and m.hexdigest()?
You need an algorithm like md4 that isn't one of hashlib's built-in classes. How do you instantiate it, and how do you check what's available?
You must hash a huge file without loading it all into RAM. Which hashlib method lets you feed data in chunks, and does chunking change the digest?
Storing a password as a plain md5 hash is still weak. What two techniques harden it, and which hashlib function bundles them?
gettext localizes your program's text. What does it deliberately NOT handle, and what file format stores the translations?
In gettext, how does the _() wrapper get into your namespace, and why must you call _('...{}').format(x) rather than formatting first?
Where must a compiled gettext translation live on disk, and which tools turn source strings into that file?

Start learning today

Free to start — download the app or use it in your browser.

Get it on App StoreGet it on Google Play
Useful Modules & Recipes (Python) · KnowCard