forked from realpython/materials
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
28 changed files
with
701 additions
and
42 deletions.
There are no files selected for viewing
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,24 +1,24 @@ | ||
import json | ||
import os | ||
from pathlib import Path | ||
|
||
import openai | ||
from openai import OpenAI | ||
|
||
client = OpenAI() | ||
|
||
PROMPT = "An eco-friendly computer from the 90s in the style of vaporwave" | ||
DATA_DIR = Path.cwd() / "responses" | ||
|
||
DATA_DIR.mkdir(exist_ok=True) | ||
|
||
openai.api_key = os.getenv("OPENAI_API_KEY") | ||
|
||
response = openai.Image.create( | ||
response = client.images.generate( | ||
model="dall-e-2", | ||
prompt=PROMPT, | ||
n=1, | ||
size="256x256", | ||
response_format="b64_json", | ||
) | ||
|
||
file_name = DATA_DIR / f"{PROMPT[:5]}-{response['created']}.json" | ||
file_name = DATA_DIR / f"{PROMPT[:5]}-{response.created}.json" | ||
|
||
with open(file_name, mode="w", encoding="utf-8") as file: | ||
json.dump(response, file) | ||
json.dump(response.to_dict(), file) |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
from openai import OpenAI | ||
|
||
client = OpenAI() | ||
|
||
PROMPT = "A vaporwave computer" | ||
|
||
|
||
response = client.images.generate( | ||
model="dall-e-3", | ||
prompt=PROMPT, | ||
) | ||
|
||
print(response.data[0].url) | ||
print(response.data[0].revised_prompt) |
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,16 +1,15 @@ | ||
certifi==2022.9.24 | ||
charset-normalizer==2.1.1 | ||
et-xmlfile==1.1.0 | ||
idna==3.4 | ||
numpy==1.23.4 | ||
openai==0.25.0 | ||
openpyxl==3.0.10 | ||
pandas==1.5.1 | ||
pandas-stubs==1.2.0.62 | ||
python-dateutil==2.8.2 | ||
pytz==2022.6 | ||
requests==2.28.1 | ||
six==1.16.0 | ||
tqdm==4.64.1 | ||
typing_extensions==4.4.0 | ||
urllib3==1.26.12 | ||
annotated-types==0.7.0 | ||
anyio==4.4.0 | ||
certifi==2024.7.4 | ||
distro==1.9.0 | ||
h11==0.14.0 | ||
httpcore==1.0.5 | ||
httpx==0.27.0 | ||
idna==3.7 | ||
jiter==0.5.0 | ||
openai==1.40.6 | ||
pydantic==2.8.2 | ||
pydantic_core==2.20.1 | ||
sniffio==1.3.1 | ||
tqdm==4.66.5 | ||
typing_extensions==4.12.2 |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,27 +1,26 @@ | ||
import json | ||
import os | ||
from base64 import b64decode | ||
from pathlib import Path | ||
|
||
import openai | ||
from openai import OpenAI | ||
|
||
client = OpenAI() | ||
|
||
DATA_DIR = Path.cwd() / "responses" | ||
SOURCE_FILE = DATA_DIR / "An ec-1667994848.json" | ||
|
||
openai.api_key = os.getenv("OPENAI_API_KEY") | ||
|
||
with open(SOURCE_FILE, mode="r", encoding="utf-8") as json_file: | ||
saved_response = json.load(json_file) | ||
image_data = b64decode(saved_response["data"][0]["b64_json"]) | ||
|
||
response = openai.Image.create_variation( | ||
response = client.images.create_variation( | ||
image=image_data, | ||
n=3, | ||
size="256x256", | ||
response_format="b64_json", | ||
) | ||
|
||
new_file_name = f"vary-{SOURCE_FILE.stem[:5]}-{response['created']}.json" | ||
new_file_name = f"vary-{SOURCE_FILE.stem[:5]}-{response.created}.json" | ||
|
||
with open(DATA_DIR / new_file_name, mode="w", encoding="utf-8") as file: | ||
json.dump(response, file) | ||
json.dump(response.to_dict(), file) |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
# Lists vs Tuples in Python | ||
|
||
This folder provides the code examples for the Real Python tutorial [Lists vs Tuples in Python](https://realpython.com/python-lists-tuples/). |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
colors = ["red", "green", "blue", "yellow"] | ||
print(colors) | ||
|
||
person = ("Jane Doe", 25, "Python Developer", "Canada") | ||
print(person) | ||
|
||
digits = list(range(10)) | ||
print(digits) | ||
|
||
even_digits = [number for number in range(1, 10) if not number % 2] | ||
print(even_digits) | ||
|
||
print(["Pythonista", 7, False, 3.14159]) | ||
print(("Pythonista", 7, False, 3.14159)) | ||
|
||
print(list(range(1_000_000))) | ||
print(tuple(range(1_000_000))) |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
numbers = [2, 7, 5, 4, 8] | ||
print(len(numbers)) | ||
print(min(numbers)) | ||
print(max(numbers)) | ||
print(sum(numbers)) |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
a = ["a", "b"] | ||
a.append("c") | ||
print(a) | ||
|
||
a = ["a", "c"] | ||
a.insert(1, "b") | ||
print(a) | ||
|
||
a = ["a", "b", "c", "d", "e"] | ||
a.remove("b") | ||
print(a) | ||
a.remove("c") | ||
print(a) | ||
|
||
a = ["a", "b", "c", "d", "e"] | ||
a.pop() | ||
print(a) | ||
a.pop() | ||
print(a) |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
x = ["a", ["bb", ["ccc", "ddd"], "ee", "ff"], "g", ["hh", "ii"], "j"] | ||
|
||
print(x[0], x[2], x[4]) | ||
|
||
print(x[1]) | ||
print(x[3]) | ||
|
||
print(x[1][0]) | ||
print(x[1][1]) | ||
print(x[1][2]) | ||
print(x[1][3]) |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
words = ["foo", "bar", "baz", "qux", "quux", "corge"] | ||
print("qux" in words) | ||
print("py" in words) | ||
print("thud" not in words) | ||
|
||
print(words + ["grault", "garply"]) | ||
print(words * 2) |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
fruits = ["apple", "orange", "mango", "grape"] | ||
del fruits[0] # Remove apple | ||
|
||
print(fruits) | ||
|
||
person = ("John Doe", 35, "Web Dev") | ||
# del person[1] # Try to remove the age value |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
numbers = [1, 2, 3, 7] | ||
numbers[3:4] = [4, 5, 6, 7] | ||
print(numbers) | ||
|
||
numbers = [1, 2, 3, 7] | ||
numbers[3:3] = [4, 5, 6] | ||
print(numbers) |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
t = ("foo", "bar", "baz", "qux") | ||
|
||
s1, s2, s3, s4 = t | ||
print(s1) | ||
print(s2) | ||
print(s3) | ||
print(s4) | ||
|
||
a = "foo" | ||
b = "bar" | ||
# Using a temporary variable | ||
temp = a | ||
a = b | ||
b = temp | ||
print(a, b) | ||
|
||
a = "foo" | ||
b = "bar" | ||
# Using unpacking | ||
a, b = b, a | ||
print(a, b) |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
# Web Scraping With Scrapy and MongoDB | ||
|
||
[Web Scraping With Scrapy and MongoDB](https://realpython.com/web-scraping-with-scrapy-and-mongodb/) is an example project for building a robust web scraper for static sites leveraging Scrapy and MongoDB. | ||
|
||
## Installation and Setup | ||
|
||
1. Create a Python virtual environment | ||
|
||
```sh | ||
$ python -m venv ./venv | ||
$ source venv/bin/activate | ||
(venv) $ | ||
``` | ||
|
||
2. Install the requirements | ||
|
||
```sh | ||
(venv) $ pip install -r requirements.txt | ||
``` | ||
|
||
You'll also need to [set up a MongoDB collection](https://realpython.com/web-scraping-with-scrapy-and-mongodb/#set-up-a-mongodb-collection-on-your-computer) like described in the tutorial. | ||
|
||
## Run the Scraper | ||
|
||
Navigate into the `books/` project directory. | ||
|
||
Then you can start crawling the site: | ||
|
||
```sh | ||
(venv) $ scrapy crawl book | ||
``` | ||
|
||
If set up correctly, this will populate your MongoDB collection with the book information scraped from the example site. | ||
|
||
## About the Author | ||
|
||
Martin Breuss - Email: [email protected] | ||
|
||
## License | ||
|
||
Distributed under the MIT license. See ``LICENSE`` for more information. |
Empty file.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
import scrapy | ||
|
||
|
||
class BooksItem(scrapy.Item): | ||
_id = scrapy.Field() | ||
url = scrapy.Field() | ||
title = scrapy.Field() | ||
price = scrapy.Field() |
Oops, something went wrong.