Last active
September 16, 2020 15:22
-
-
Save ahmad-ali14/8740961112f6bff99cf3ccb9554fc912 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
""" | |
Part 1 | |
rite a function called print_volume (r) that takes an argument for the radius of the sphere, and prints the volume of the sphere. | |
Call your print_volume function three times with different values for radius. | |
""" | |
# define a constant variable PI | |
PI = 3.141592653589793 | |
# calculate the volume | |
def print_volume(radius): | |
print((4/3) * PI * (radius**3)) | |
""" | |
Part 2 | |
Write your own function that illustrates a feature that you learned in this unit. The function must take at least one argument. | |
""" | |
# importing regular expression library | |
import re | |
""" | |
googbox is a function that transforms a google drive image shared url or dropbox shared url into direct url that can be used directly as an image source in html file | |
goobox takes url <string> as an argument and do the following: | |
1- check if the url is already a valid direct url, if so returns an error. | |
2- check if it's a google drive shared url, and transform it to a direct url, returns the new direct url. | |
3- check if it's a dropbox shared url, return a new direct url | |
4- otherwise, if the url is not valid, returns an error | |
I own this function, more info here: https://github.com/ahmad-ali14/dg-url | |
""" | |
def goobox(url): | |
# regular expressions, for shared google drive and dropbox url | |
dropbox_regex = "(http(s)*:\/\/)*(www\.)*(dropbox.com)" | |
drive_regex = "(http(s)*:\/\/)*(www\.)*(drive.google.com\/file\/d\/)" | |
# regular expressions for direct ggogle drive and dropbox url | |
drive_direct_url_regex = "^(https:\/\/drive.google.com\/uc\?id=)" | |
dropbox_direct_url_regex = "^(https:\/\/dl.dropbox.com)" | |
# if the url is already a valid direct url, returns an error | |
if re.search(drive_direct_url_regex, url) or re.search(dropbox_direct_url_regex, url): | |
return "your URL is already a direct url, please use it as it is" | |
# if it is a shared dropbox url | |
if re.search(dropbox_regex, url): | |
# return direct dropox url, by replacing "www." with "dl." | |
return re.sub("(http(s)*:\/\/)*(www\.)*", "https://dl.", url, 1) | |
# if it is a shared google drive url | |
elif re.search(drive_regex, url): | |
# prefix for direct gdrive urls | |
url_starter = "https://drive.google.com/uc?id=" | |
# extract the file id from the url itself | |
_file_id = re.sub(drive_regex, "", url) | |
file_id = re.split("/",_file_id) | |
# return the direct url by compining the prefix with the file Id | |
return url_starter + file_id[0] | |
# if it is not a valid gdrive/dropbox url, return an Error | |
else: | |
return "Error, Wrong URL, not a vlid drobox or google drive url" | |
# our progrram entry point | |
if __name__ == "__main__": | |
# calling print_volume() three times | |
print_volume(4) # 201.06192983 | |
print_volume(1) # 3.14159265359 | |
print_volume(10) # 3141.59265359 | |
# calling goobox with differnt cases | |
print(goobox("https://drive.google.com/uc?id=1VctOazcyDrm8nxoWFAWM_t5bxlpyLI9s")) # your URL is already a direct url, please use it as it is | |
print(goobox("https://ahmad-ali.co.uk/")) # Error, Wrong URL, not a vlid drobox or google drive url | |
print(goobox("https://drive.google.com/file/d/1VctOazcyDrm8nxoWFAWM_t5bxlpyLI9s/view?usp=sharing")) # https://drive.google.com/uc?id=1VctOazcyDrm8nxoWFAWM_t5bxlpyLI9s | |
print(goobox("https://www.dropbox.com/s/degru93hceo26m6/30sec.png?dl=0")) #https://dl.dropbox.com/s/degru93hceo26m6/30sec.png?dl=0 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment