# Socks!

## Purpose
In this lab we obtained data using a web API, used Python classes to organize information, and used Python for data analysis.


## Answering the Questions

### 1. Discuss how you used the API to obtain the dataset.
</br>


```import requests
import csv
 
BASE_URL = "https://hm-cs.herokuapp.com"
ENDPOINT = "/socks"
API_KEY = "ArtOfDataKEY123"
idx=0 
payload = {'key': API_KEY, 'idx': 0}
response = requests.get(BASE_URL + ENDPOINT , params=payload)
data = response.text
while response.ok:
    payload = {'key': API_KEY, 'idx': idx}
    response = requests.get(BASE_URL + ENDPOINT , params=payload)
    data = response.text
    idx += 1 
if not response.ok:
 print(response.status_code, response.text)
```

### My Answer: 
I used the API to obtain the dataset by using the API Key, Base URL, and Endpoint, and then used the ged function to get the data!

### Creating a Sock Class

```
class Socksie:

    def __init__(self, name, color1, color2, variation):
        self.name = name
        self.variation = variation
        self.color1 = color1
        self.color2 = color2
    def __str__(self) -> str:
        return f"{self.name},{self.variation},{self.color1},{self.color2}"
```


### Creating The Table

```
with open("datasets/socksie.csv", encoding="utf-8-sig") as f:
    data = list(csv.DictReader(f)) 
    table = {} 
    colors={}

    for socksie in data:
        color1 = socksie["Color 1"] 
        color2 = socksie["Color 2"]
        variation= socksie["Variation"]
        name = socksie["Name"]
        if name not in table:
            table[name] = []
        table[name].append({"variation": variation, "color1": color1, "color2": color2})

```
### 2. Which sock has the most variations? If there is more than one answer, then list all of them.


In order to do this I created a list of variations and stored the names of the variations with an idx that was equal to 8 in the list. I used 8 as I noticed that it is the maximum number of variations.
```
variations=[]
for name in table:
        idx = 0
        for variation in table[name]:
            idx+=1
            if idx == 8: 
                variations.append(name)
                
print(variations)
```
### My Answer: 
My answer was:  **['argyle crew socks', 'color-blocked socks', 'frilly knee-high socks', 'holey tights', 'kiddie socks', 'mixed-tweed socks', 'no-show socks', 'semi-opaque socks', 'semi-opaque tights', 'sequin leggings', 'simple-accent socks', 'striped socks', 'striped tights', 'tube socks', 'ultra no-show socks', 'vivid leggings', 'vivid socks', 'vivid tights']**
### 3. How many socks of each color are there? If a sock has two different colors, it should be counted in both. However, if a sock has the same Color1 and Color2, make sure you don’t double count it!

In order to figure out the answer to this question, I created a dictionary called colors with keys of color 1 and color 2, and added one to the values of each, and made sure that if color 1 = color 2, I subtracted one from color 1. Then, I printed the colors dictionary. 


```
if color1 not in colors.keys():
            colors[color1]= 1
        else: 
            colors[color1]+=1
        if color2 not in colors.keys():
            colors[color2]= 1
        else: 
            colors[color2]+=1  
        if color1 == color2 :
            colors[color1] -=1      
        
    print(colors)
```
### My Answer: 
When I stated *print(colors)*,my answer was: **{'Pink': 41, 'Red': 39, 'Green': 50, 'Light blue': 33, 'Orange': 27, 'Purple': 37, 'Blue': 47, 'Yellow': 33, 'Beige': 15, 'White': 85, 'Black': 59, 'Brown': 11, 'Gray': 31, 'Colorful': 14}**.

## Process

The first thing I did in this lab was importing the API and converting the csv. This step was pretty straightforward! Then, we had to use the data and answer the questions. In order to do so, I first drew out visual diagrams to help me see all of the elements I had to consider when answering the questions. After doing so, it basically flowed! All of the code that I used is included in the other parts of this lab!

## Sources
I met with with Mr. Lee for this lab! 
