go back  11 - Generators for Fun and Profit




This challenge write-up first appeared on PyBites.

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 'topic-GHuser.py') 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