Skip to content

Commit 00d0f88

Browse files
committed
oop added, requests changed
1 parent 579d99a commit 00d0f88

File tree

7 files changed

+145
-25
lines changed

7 files changed

+145
-25
lines changed

OOP/dog.py

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
### Exercises "The Oldest Dog"
2+
# Using the same Dog class, instantiate three new dogs, each with a different age.
3+
# Then write a function called, get_biggest_number(), that takes any number of ages (*args) and
4+
# returns the oldest one. Then output the age of the oldest dog like so:
5+
# The oldest dog is 7 years old.
6+
7+
8+
9+
class Dog:
10+
11+
# Class Attribute
12+
species = 'mammal'
13+
14+
# Initializer / Instance Attributes
15+
def __init__(self, name, age):
16+
self.name = name
17+
self.age = age
18+
19+
20+
# Instantiate the Dog object
21+
philo = Dog("Philo", 5)
22+
mikey = Dog("Mikey", 6)
23+
24+
# Access the instance attributes
25+
print(f"{philo.name} is {philo.age} and {mikey.name} is {mikey.age}.")
26+
27+
# Is Philo a mammal?
28+
if philo.species == "mammal":
29+
print(f"{philo.name} is a {philo.species}!")

OOP/dog_inheritance.py

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
# Parent class
2+
class Dog:
3+
4+
# Class attribute
5+
species = 'mammal'
6+
7+
# Initializer / Instance attributes
8+
def __init__(self, name, age):
9+
self.name = name
10+
self.age = age
11+
12+
# instance method
13+
def description(self):
14+
return "{} is {} years old".format(self.name, self.age)
15+
16+
# instance method
17+
def speak(self, sound):
18+
return "{} says {}".format(self.name, sound)
19+
20+
21+
# Child class (inherits from Dog class)
22+
class RussellTerrier(Dog):
23+
def run(self, speed):
24+
return "{} runs {}".format(self.name, speed)
25+
26+
27+
# Child class (inherits from Dog class)
28+
class Bulldog(Dog):
29+
def run(self, speed):
30+
return "{} runs {}".format(self.name, speed)
31+
32+
33+
# Child classes inherit attributes and
34+
# behaviors from the parent class
35+
jim = Bulldog("Jim", 12)
36+
print(jim.description())
37+
38+
# Child classes have specific attributes
39+
# and behaviors as well
40+
print(jim.run("slowly"))

OOP/dog_sol.py

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
## Solution to Exercise "The Oldest Dog"
2+
3+
class Dog:
4+
5+
# Class Attribute
6+
species = 'mammal'
7+
8+
# Initializer / Instance Attributes
9+
def __init__(self, name, age):
10+
self.name = name
11+
self.age = age
12+
13+
14+
# Instantiate the Dog object
15+
jake = Dog("Jake", 7)
16+
doug = Dog("Doug", 4)
17+
william = Dog("William", 5)
18+
19+
20+
# Determine the oldest dog
21+
def get_biggest_number(*args):
22+
return max(args)
23+
24+
25+
# Output
26+
print("The oldest dog is {} years old.".format(
27+
get_biggest_number(jake.age, doug.age, william.age)))

requests/dog.py

Lines changed: 0 additions & 13 deletions
This file was deleted.

requests/git_api_exampe.py

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -62,11 +62,14 @@ def repos_with_most_stars(languages, sort="stars", order="desc"):
6262

6363
if __name__ == "__main__":
6464
languages = ["python", "javascript", "ruby", "go"]
65-
results = repos_with_most_stars(languages)
65+
try:
66+
results = repos_with_most_stars(languages)
6667

67-
for result in results:
68-
language = result["language"]
69-
stars = result["stargazers_count"]
70-
name = result["name"]
68+
for result in results:
69+
language = result["language"]
70+
stars = result["stargazers_count"]
71+
name = result["name"]
72+
print(f"-> {name} is a {language} repo with {stars} stars.")
7173

72-
print(f"-> {name} is a {language} repo with {stars} stars.")
74+
except Exception as e:
75+
print(e)

requests/iss-now.py

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
# Requests: HTTP for Humans
2+
# http://docs.python-requests.org/en/master/
3+
####
4+
# HTTP in nutshell:
5+
# Two commonly used methods for a request-response between a client and server are: GET and POST.
6+
# GET - Requests data from a specified resource
7+
# POST - Submits data to be processed to a specified resource
8+
# https://www.w3schools.com/tags/ref_httpmethods.asp
9+
# List of HTTP status codes : https://www.w3schools.com/tags/ref_httpmessages.asp
10+
# Let's example with some NASA open API and requests python module
11+
# http://api.open-notify.org example for API , we will see only GET request examples. For POST continue explore by yourself.
12+
import sys
13+
import requests
14+
import json
15+
from datetime import datetime
16+
17+
try:
18+
# Make a get request to get the latest position of the international space station from the opennotify api.
19+
response_now = requests.get("http://api.open-notify.org/iss-now.json")
20+
response_astros = requests.get("http://api.open-notify.org/astros.json")
21+
22+
print(response_now.status_code)
23+
print(response_now.text)
24+
print(response_astros.status_code)
25+
print(response_astros.text)
26+
data = response_now.json()
27+
time_s = datetime.fromtimestamp(int(data['timestamp']))
28+
location = data['iss_position']
29+
print(f"Timestamp:\t\t{time_s}")
30+
print(f"Position:")
31+
print(f"\tlatitude:\t {location['latitude']}")
32+
print(f"\tlongitude:\t {location['longitude']}")
33+
print(50*"=")
34+
data = response_astros.json()
35+
print("People in Space Right Now:")
36+
for name in data['people']:
37+
print(f"\t{name['name']}")
38+
39+
except Exception as e:
40+
print(e)

requests/simple_example.py

Lines changed: 0 additions & 6 deletions
This file was deleted.

0 commit comments

Comments
 (0)