991# -------------------------------------------------------------------
2# Import libraries
3# -------------------------------------------------------------------
4import math
5
6# -------------------------------------------------------------------
7# Constants
8# -------------------------------------------------------------------
9
10# -------------------------------------------------------------------
11# Global variables
12# -------------------------------------------------------------------
13fileSize = ""
14totalBits = 0
15networkSpeed = ""
16bitsPerSecond = 0.0
17downloadTime = 0.0
18
19# -------------------------------------------------------------------
20# Subprograms
21# -------------------------------------------------------------------
22
23# convert a string like "1.7 MiB" to the number of bits (e.g 14260634)
24def convertSizeToBits(pFileSize):
25parts = pFileSize.split(" ")
26quantity = float(parts[0])
27unit = parts[1]
28
29if unit == "B":
30quantity *= 8
31elif unit == "KiB":
32quantity *= 8 * 1024
33elif unit == "MiB":
34quantity *= 8 * (1024 ** 2)
35elif unit == "GiB":
36quantity *= 8 * (1024 ** 3)
37elif unit == "TiB":
38quantity *= 8 * (1024 ** 4)
39else:
40print("Unknown unit, assuming you entered the number of bits")
41
42return math.ceil(quantity)
43
44# convert a string like "10 Mbps" to the number of bits per second (e.g. 10000000)
45def convertSpeedToBitsPerSecond(pSpeed):
46parts = pSpeed.split(" ")
47speed = float(parts[0])
48unit = parts[1]
49
50if unit == "Kbps":
51speed *= 1000
52elif unit == "Mbps":
53speed *= (1000 ** 2)
54elif unit == "Gbps":
55speed *= (1000 ** 3)
56elif unit == "Tbps":
57speed *= (1000 ** 4)
58else:
59print("Unknown unit, assuming you entered the number of bits per second")
60
61return speed
62
63# convert a time in seconds like 0.3 to a human readable string (e.g "300 ms")
64def convertTime(pTimeInSeconds):
65unit = "s"
66duration = pTimeInSeconds
67
68if duration < 1:
69unit = "ms"
70duration *= 1000
71elif duration > 60:
72unit = "mins"
73duration /= 60
74
75return "{:.1f} {}".format(duration, unit)
76
77# -------------------------------------------------------------------
78# Main program
79# -------------------------------------------------------------------
80
81print("Download speed calculator")
82
83# find the size of the file in bits
84fileSize = input("Enter file size (e.g. 1.7 MiB)")
85totalBits = convertSizeToBits(fileSize)
86print("Total number of bits: ", totalBits)
87
88# find the network speed in bits per second
89networkSpeed = input("Enter network speed (e.g. 55 Mbps)")
90bitsPerSecond = convertSpeedToBitsPerSecond(networkSpeed)
91print("Total bits per second: ", bitsPerSecond)
92
93downloadTime = totalBits / bitsPerSecond
94print("Download time:", convertTime(downloadTime))
95
96# Challenges
97# 1) Replace every 1024 with a constant called BYTES_IN_KIBIBYTE
98# 2) Replace every 1000 with a constant called BYTES_IN_KILOBYTE
99# 3) Adjust convertTime to show very long times in hours rather than minutes