This commit is contained in:
Stephen Deaton 2024-06-26 14:24:56 -04:00
commit e374bd45db
5 changed files with 101 additions and 1 deletions

1
.gitignore vendored
View File

@ -4,3 +4,4 @@ __pycache__
desktop.ini
dist/
build/
test_*

64
Python/045_filedata.py Normal file
View File

@ -0,0 +1,64 @@
# File Data Input/Output
import os
from dataclasses import dataclass
from pathlib import Path
# We will start combining much of what we've learned so far
# as we begin talking about more complex subjects. The first
# example will be working with a CSV (Comma Separated Value)
# file used to store user data.
# We create a data class named User()
@dataclass
class User():
email: str = ""
id: int = 0
first_name: str = ""
last_name: str = ""
# Build up the path to the data file
data_file = os.path.join("Data", "users.csv")
# The pathlib module provides streamlined methods of getting
# the entire contents of a file in one command.
# This is a fairly complex statement.
# 1) Create a Path() object to the data_file
# 2) Use the read_text() method to read in the entire file as a string
# 3) Use the split() method on the string data to split it into lines
# 4) Use a slice ([1:]) to remove the first line (the header)
csv_contents = Path(data_file).read_text().split("\n")[1:]
# The list which will contain User() objects
users = []
# Step through the lines one at a time
for line in csv_contents:
# Split the line on commas after stripping leading/trailing white space
parts = line.strip().split(",")
# If we don't get four elements from the split then go to the next line
if len(parts) != 4:
continue
# Create a new User() object from the split parts of the line
users.append(User(parts[0], parts[1], parts[2], parts[3]))
# Print them out
for user in users:
print(user)
# Now we'll write the data we have back to a new file.
# In this repository any file beginning with "test_" will
# be ignored so we'll use a file names of "test_userdata.csv"
csv_filename = os.path.join("Data", "test_userdata.csv")
# We open the file. If the open fails it just quietly continues
with open(csv_filename, "w") as f:
# Let's write a header to the CSV
line = "EMail Address,User ID,First Name,Last Name\n"
f.write(line)
# Now we walk through the user data and write it all to the file
user: User
for user in users:
line = user.email + ","
line += user.id + ","
line += user.first_name + ","
line += user.last_name + "\n"
f.write(line)
print(f"User data written to file: {csv_filename}")

20
Python/050_apidata.py Normal file
View File

@ -0,0 +1,20 @@
# Retrieving data from a Web/REST API
# Import the library for web requests
import requests
# The URL of our API call
URL = "https://jackpot.jaj.com/weather/"
# Make the request for data
response = requests.get(URL)
# response.json() will contain the json data returned from the request.
# This data can be accessed just like a dictionary/dict.
# Load the URL above in Firefox or Edge to see the available data fields
print("Current Weather in Beavercreek, OH")
print(f" Conditions: {response.json()['current']['weather'][0]['main']}")
print(f" Temperature: {response.json()['current']['temp']}° F (feels like {response.json()['current']['feels_like']}° F)")
print(f" Humidity: {response.json()['current']['humidity']}%")
print(f" Wind: {response.json()['current']['wind_speed']} MPH")
print(f" Prediction: {response.json()['daily'][0]['summary']}")

5
Python/Data/users.csv Normal file
View File

@ -0,0 +1,5 @@
Login email,Identifier,First name,Last name
laura@example.com,2070,Laura,Grey
craig@example.com,4081,Craig,Johnson
mary@example.com,9346,Mary,Jenkins
jamie@example.com,5079,Jamie,Smith
1 Login email Identifier First name Last name
2 laura@example.com 2070 Laura Grey
3 craig@example.com 4081 Craig Johnson
4 mary@example.com 9346 Mary Jenkins
5 jamie@example.com 5079 Jamie Smith

View File

@ -2,6 +2,16 @@
This repository is primarily a collection of resources and learning aides for Steve's classroom work. This can include coding examples in various languages, project examples, documentation, instructional guides, and just about anything else.
## Prerequesits
* The requests module provides web/REST API access
* The colorama module provides access to color and cursor positioning in a terminal
Inside a terminal:
```bash
pip install requests colorama
```
## [Python Basics](Python/)
This is the home of many examples of basic Python syntax and logic. An attempt has been made to name them in such a way that they increase in complexity and build on each other as you go down the list of files.