7c52fec182475ff62a4d30e65db6a3c8e7127a79
[mevemon] / package / src / util.py
1 """Random helpful functions for mevemon """
2 import os
3 import shutil
4
5 def comma(number):
6     """Converts a number in the format 1234567 to 1,234,567
7     """
8     num_string = '%0.2f' % number
9     
10     #a,b = num_string.split('.')
11     decimal_part, fractional_part = num_string.split('.')
12     thousands = []
13     while len(decimal_part) > 3:
14         thousands.insert(0, decimal_part[-3:])
15         decimal_part = decimal_part[0:-3]
16     if decimal_part:
17         thousands.insert(0, decimal_part)
18
19     if type(number) is int:
20         return ','.join(thousands)
21     else:
22         return ','.join(thousands) + '.' + fractional_part
23
24 def clean_dir(path):
25     """ Removes the contents of a directory, but not the directory itself
26     """
27     names = os.listdir(path)
28     for name in names:
29         fullname = os.path.join(path, name)
30         if os.path.isdir(fullname):
31             shutil.rmtree(fullname)
32         else:
33             os.remove(fullname)