Skip to content

Commit

Permalink
Add a sorting option
Browse files Browse the repository at this point in the history
  • Loading branch information
victordomingos committed Apr 30, 2018
1 parent 3d1348f commit 9384831
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 7 deletions.
9 changes: 7 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,10 @@ that argument empty and it will scan the current working directory.
The optional `-nr` (i.e., "no recursion") switch argument tells the
application not to scan recursively through the subdirectories.

By default, the table will be sorted by the file extension frequency. If you
prefer alphabetically sorted results, you just need to add the `-alpha`
argument.

Similarly, the optional `-nt` (i.e., "no table") switch tells the application
not to show a table listing all the found file extensions and their respective
frequencies, so that it will only display the total number of files.
Expand All @@ -23,6 +27,7 @@ hidden (with names starting with '.', but you can add the `-a` optional
switch argument to make it count all files.



## Examples of usage:

Get a little help about how to use this application:
Expand Down Expand Up @@ -55,9 +60,9 @@ Count all files in a given directory with recursion, but don't display a table,
`countfiles.py -nt ~/Documents`


Count all files in a given directory without recursing through subdirectories:
Count all files in a given directory without recursing through subdirectories, and sort the table alphabetically:

`countfiles.py -nr ~/Documents`
`countfiles.py -nr -alpha ~/Documents`

Count all files in a given directory without recursing through subdirectories, including hidden files, and only displaying the total number of files (no table):

Expand Down
19 changes: 14 additions & 5 deletions countfiles.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
"""
import os
import argparse
import collections


def get_file_extension(file_path: str) -> str:
Expand Down Expand Up @@ -39,12 +40,11 @@ def sort_by_frequency(self):
return sorted_counters

def sort_by_word(self):
sorted_counters = [(word, self.counters[word])
for word in sorted(self.counters,
key=self.counters.get,
reverse=True)]
ordered = collections.OrderedDict(sorted(self.counters.items()))
sorted_counters = ordered.items()
return sorted_counters


def show_2columns(self, data):
if len(data) == 0:
print("Oops! We have no data to show...\n")
Expand Down Expand Up @@ -105,14 +105,20 @@ def show_total(self):
action='store_true',
help="Don't show the table, only the total number of files")

parser.add_argument('-alpha',
action='store_true',
help="Sort the table alphabetically, by file extension.")

parser.add_argument('-a',
action='store_true',
help="Include hidden files and directories (with filenames starting with '.')")


args = parser.parse_args()
recursive = not args.nr
include_hidden = args.a
show_table = not args.nt
sort_alpha = args.alpha


fc = WordCounter()
Expand Down Expand Up @@ -152,6 +158,9 @@ def show_total(self):


if show_table:
fc.show_2columns(fc.sort_by_frequency())
if sort_alpha:
fc.show_2columns(fc.sort_by_word())
else:
fc.show_2columns(fc.sort_by_frequency())
else:
fc.show_total()

0 comments on commit 9384831

Please sign in to comment.