Skip to content

Commit

Permalink
Merge pull request #7 from karansthr/add_pluralize
Browse files Browse the repository at this point in the history
Pluralize result and filter symbolic links
  • Loading branch information
Karan Suthar authored Aug 26, 2018
2 parents c2cad03 + c1aa095 commit 5e3a856
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 4 deletions.
14 changes: 10 additions & 4 deletions playlist_length/main.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
# -*- coding: utf-8 -*-

import argparse
import glob
import os
import subprocess as sp
import sys
Expand All @@ -12,6 +11,7 @@
from huepy import bold, green, red
from tqdm import tqdm

from .utils import pluralize
from .__version__ import __version__


Expand Down Expand Up @@ -61,7 +61,10 @@ def with_subdir():
)

def without_subdir():
return filter(os.path.isfile, glob.glob(os.path.join(BASE_PATH, '*.*')))
for file in os.listdir(BASE_PATH):
file_path = os.path.join(BASE_PATH, file)
if os.path.isfile(file_path) and not os.path.islink(file_path):
yield file_path

all_files = without_subdir() if no_subdir else with_subdir()
return list(all_files)
Expand Down Expand Up @@ -101,11 +104,14 @@ def calculate_length(BASE_PATH, no_subdir, media_type):
length = round(sum(result))

if length < 60:
result = 'Length of all {} is {} minutes.'.format(media_type, length)
minutes_string = pluralize(length, base='minute', suffix='s')
result = 'Length of all {} is {} minutes.'.format(media_type, minutes_string)
else:
hours, minutes = divmod(length, 60)
hours_string = pluralize(hours, base='hour', suffix='s')
minutes_string = pluralize(minutes, base='minute', suffix='s')
result = 'Length of all {} is {} hours and {} minutes.'.format(
media_type, hours, minutes
media_type, hours_string, minutes_string
)
return bold(green(result))

Expand Down
4 changes: 4 additions & 0 deletions playlist_length/utils.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
def pluralize(number, base, suffix):
if number < 2:
return '{} {}'.format(number, base)
return '{} {}{}'.format(number, base, suffix)

0 comments on commit 5e3a856

Please sign in to comment.