Skip to content

Instantly share code, notes, and snippets.

@kracekumar
Created February 15, 2022 10:37
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kracekumar/afde5c73bd60254ea364a8b62a74cd59 to your computer and use it in GitHub Desktop.
Save kracekumar/afde5c73bd60254ea364a8b62a74cd59 to your computer and use it in GitHub Desktop.
"""
Datastructures
1. Tuple
2. List
3. Set
4. Dictionary
5. Class
"""
import datetime
def tuple_example():
# constants = ("Python", 3.10, "Rust", 1)
# print(constants[0])
# This will error out
# constants[0] = "Java"
# Access the tuple using index in reverse order
# print(constants[-1])
# print(constants[:])
def list_example():
# List is mutable datastructure
# langs = ["Python", 3.10, "Rust", 1, "JS", 6]
# print(langs[0])
# langs[0] = "Java"
# print(langs[0])
# print(langs[-1])
# print("Python" in langs)
# print("Rust index: ", langs.index("Rust"))
def set_example():
# words = set()
# for word in ("welcome", "greeting", "welcome", "hello"):
# words.add(word)
# print(words, len(words))
# common_words = set(["a", "an", "the"])
# print(common_words)
# print(common_words.intersection(words))
# print(common_words.union(words))
# print(common_words.difference(words))
def dictionary_example():
# sentence = "Welcome to the workshop. In this workshop you will learn Python datastructures."
# word_count = {}
# for word in sentence.split():
# word = word.lower()
# word = word.replace(".", "")
# if word in word_count:
# word_count[word] += 1
# else:
# word_count[word] = 1
#print("Welcome Count:", word_count["welcome"])
#print("Greeting Count:", word_count.get("greeting"))
#word_count["missing"] = -1
#print(word_count.get("missing"))
def class_example():
#class Person:
# def __init__(self, name, dob, gender):
# self.name = name
# self.dob = dob
# self.gender = gender
# def age(self):
# return (datetime.datetime.today().date().year - self.dob.year)
#person = Person(name='John', dob=datetime.date(1960, 5, 1), gender='M')
#print(person.age())
def main():
# 1. Tuple - immutable datastructure, accessed using index.
tuple_example()
# 2. List - mutable datastructure, accessed using index.
list_example()
# 3. Set - mutable datastructure, removes duplicate items.
set_example()
# 4. Dictionary - mutable datastructure, accessed using key.
dictionary_example()
# 5. Class
class_example()
if __name__ == "__main__":
main()
import requests
import argparse
import rich
from rich.progress import Progress
import time
def download_url(url, output_file):
with Progress() as progress:
rich.print(f'Downloading URL {url}')
task = progress.add_task(f"Progress downloading URL", total=3)
progress.advance(task)
time.sleep(1)
r = requests.get(url)
time.sleep(1)
progress.advance(task)
if output_file:
with open(output_file, "w") as f:
f.write(r.text)
time.sleep(1)
progress.advance(task)
def get_output_file(url):
return url.split("/")[-1]
def handle_download_from_url(args):
output = args.output or get_output_file(args.input)
download_url(args.input, output)
def handle_download_from_file(args):
with open(args.input, "r") as f:
urls = f.readlines()
for url in urls:
output = get_output_file(url)
download_url(url, output)
def create_parser():
parser = argparse.ArgumentParser(description="Wget program arguments")
parser.add_argument("input", help="Input file or URL to download")
parser.add_argument("-o", "--output", help="Output file name")
return parser
def main():
parser = create_parser()
args = parser.parse_args()
if args.input.startswith(("https", "wwww")):
handle_download_from_url(args)
else:
handle_download_from_file(args)
if __name__ == "__main__":
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment