Code Challenge 11 - Generators for Fun and Profit

PyBites, Mon 20 March 2017, Challenges

codechallenges, generators, learning, newquote, unix

There is nothing like a challenge to bring out the best in man. - Sean Connery

Hi Pythonistas, a new week, a new 'bite' of Python coding!

After last week's article on generators we will get you to practice a bit more with them.

Inspired by David Beazley's Generator Tricks for Systems Programmers we ask you to turn the following unix pipeline into Python code using generators. To get a bunch of .py files you can use our challenges repo you cloned. Or use a project of your own.

Note that in our experience one subprocess is not necessarily one generator, for example 'sort|uniq|sort' can be easily combined into one, as well as 'grep|sed'. See our template if you need guidance.

# assuming you pulled our challenges master and are in our 11/ subdirectory 
# code this unix pipeline into Python using generators 
$ for i in ../*/*py; do grep ^import $i|sed 's/import //g' ; done | sort | uniq -c | sort -nr
4 unittest
4 sys
3 re
3 csv
2 tweepy
2 random
2 os
2 json
2 itertools
1 time
1 datetime

Not familiar yet with Unix pipeline? It's pretty well explained here.

To follow along with our challenges (UPDATES)

See our INSTALL doc which should contain everything you need to get up and running (any issue, please open a GH Issue).

Update I) Forking

We got some feedback that Forks don't lead to activity on your Github profile. One of our followers was so nice to update our INSTALL (via PR (Pull Request)). Maybe you want to use the workaround under III. if the credit thing is an issue for you. See issue #2 for more details.

Update II) Submit your Solution

We made a new 'community' branch. Another way to get credit is to submit your code via a new PR. We do not merge anything on our master or solutions branches, but if you ping us this way we add your solution to our new community branch (as filename '') and feature it in our review if it taught us something cool.

Code Challenges Archive

See this page for all code challenges so far, if you have an interesting one you'd like to see featured contact us, open a GH Issue.

And last but not least: there is no best solution, only learning more/ better Python. We're looking forward reviewing our and your solutions end of this week. Good luck and have fun!

Keep Calm and Code in Python!

-- Bob and Julian

PyBites Python Tips

Do you want to get 250+ concise and applicable Python tips in an ebook that will cost you less than 10 bucks (future updates included), check it out here.

Get our Python Tips Book

"The discussions are succinct yet thorough enough to give you a solid grasp of the particular problem. I just wish I would have had this book when I started learning Python." - Daniel H

"Bob and Julian are the masters at aggregating these small snippets of code that can really make certain aspects of coding easier." - Jesse B

"This is now my favourite first Python go-to reference." - Anthony L

"Do you ever go on one of those cooking websites for a recipe and have to scroll for what feels like an eternity to get to the ingredients and the 4 steps the recipe actually takes? This is the opposite of that." - Sergio S

Get the book