This was a pretty easy one, yet showing a powerful way to start thinking about generators as pipelines that can be plugged into each other.
Our solution is here. A couple of notes:
Here you see the 'plugged into each other' part:
if __name__ == "__main__": files = gen_files('../*/*.py') lines = gen_lines(files) modules = gen_grep(lines, re.compile(r'^import (\w+)')) ...
iglob returns an iterator which yields the paths matching a pathname pattern, glob returns a list, also fine because we are not dealing with a lot of directories. You can also use os.walk but when you have a clear pattern (i)glob is less code.
We use yield from (>= 3.3) which saves a for loop (shorter).
As seen before use collections.Counter, it's hard to beat short- and conciseness:
def gen_count(modules): yield from Counter(modules).most_common()
We use grouping to capture the match as discussed in our regex article:
def gen_grep(lines, pattern): for line in lines: m = pattern.match(line.rstrip()) if m: yield m.group(1) # retrieves what's matched in parenthesis modules = gen_grep(lines, re.compile(r'^import (\w+)'))
We got our first solution PR which we merged onto our community branch. This is a nice way to get credit and help our community learn more.
The solution was pretty similar to ours. One nice addition was to sort manually on both values and keys, most_common only sorts by values. Also the regex part was slightly different stripping off import with re.sub. It is nice to see different solutions to the same problem, you learn more.
Next week we do another game, so we expect some more complexity and variety in solutions. Stay tuned ...
We hope you are enjoying this. If we can do anything better or you have ideas please open a new issue or reach out to us. See you next week ...
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.
"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