Skip to content

Commit 9dc8383

Browse files
committed
add processed data
1 parent 3cbeb25 commit 9dc8383

File tree

347 files changed

+37067
-0
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

347 files changed

+37067
-0
lines changed

data/processed/f_327_jenny_w_doc.py

Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
1+
import random
2+
import matplotlib.pyplot as plt
3+
4+
5+
def f_327(points: int):
6+
"""
7+
Generate a plot of random numbers such that indices are on the x-axis and generated numbers are on the y-axis.
8+
9+
Parameters:
10+
- points (int): Number of random points to generate.
11+
12+
Returns:
13+
- Returns a tuple containing:
14+
- A list of generated random numbers.
15+
- A matplotlib Axes object representing the plot.
16+
17+
Requirements:
18+
- random
19+
- matplotlib.pyplot
20+
21+
Example:
22+
>>> import random
23+
>>> random.seed(0)
24+
>>> f_327(5)
25+
([0.8444218515250481, 0.7579544029403025, 0.420571580830845, 0.25891675029296335, 0.5112747213686085], <Axes: >)
26+
>>> f_327(3)
27+
([0.4049341374504143, 0.7837985890347726, 0.30331272607892745], <Axes: >)
28+
"""
29+
x = list(range(points))
30+
y = [random.random() for _ in range(points)]
31+
32+
_, ax = plt.subplots()
33+
ax.plot(x, y)
34+
35+
return y, ax
36+
37+
import unittest
38+
import random
39+
class TestCases(unittest.TestCase):
40+
def test_case_1(self):
41+
random.seed(0)
42+
y, _ = f_327(5)
43+
# Test correct number of points are generated
44+
self.assertEqual(len(y), 5)
45+
def test_case_2(self):
46+
random.seed(0)
47+
y, _ = f_327(5)
48+
# Test expected values
49+
self.assertTrue(all(0 <= num <= 1 for num in y))
50+
self.assertAlmostEqual(
51+
y,
52+
[
53+
0.8444218515250481,
54+
0.7579544029403025,
55+
0.420571580830845,
56+
0.25891675029296335,
57+
0.5112747213686085,
58+
],
59+
)
60+
def test_case_3(self):
61+
random.seed(0)
62+
# Test incorrect data types
63+
with self.assertRaises(TypeError):
64+
f_327("5")
65+
with self.assertRaises(TypeError):
66+
f_327([])
67+
with self.assertRaises(TypeError):
68+
f_327(None)
69+
def test_case_4(self):
70+
random.seed(0)
71+
# Test handling 1 number
72+
y, ax = f_327(1)
73+
# Assert that 1 random number is generated
74+
self.assertEqual(len(y), 1)
75+
# Assert that the plot has the correct x and y data
76+
self.assertEqual(list(ax.lines[0].get_xdata()), [0])
77+
self.assertEqual(list(ax.lines[0].get_ydata()), y)
78+
def test_case_5(self):
79+
random.seed(0)
80+
# Test handling no random numbers
81+
y, ax = f_327(0)
82+
self.assertEqual(len(y), 0)
83+
# Assert that the plot has no data
84+
self.assertEqual(list(ax.lines[0].get_xdata()), [])
85+
self.assertEqual(list(ax.lines[0].get_ydata()), [])
86+
def tearDown(self):
87+
plt.close("all")

data/processed/f_328_jenny_wo_doc.py

Lines changed: 121 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,121 @@
1+
import sqlite3
2+
import pandas as pd
3+
4+
5+
def f_328(db_file: str, query: str) -> pd.DataFrame:
6+
"""Query an SQLite database and return the results.
7+
8+
This function connects to a given SQLite database, executes a given SQL query,
9+
and returns the results as a pandas DataFrame.
10+
11+
Parameters:
12+
- db_file (str): Path to the SQLite database file.
13+
- query (str): SQL query to execute.
14+
15+
Returns:
16+
- pd.DataFrame: A DataFrame containing the results of the executed query.
17+
18+
Requirements:
19+
- sqlite3
20+
- pandas
21+
22+
Example:
23+
>>> db_file = 'sample_database.db'
24+
>>> df = f_328(db_file, "SELECT * FROM users WHERE name = 'John Doe'")
25+
pd.DataFrame:
26+
id name age
27+
-- ---------- ---
28+
.. John Doe ..
29+
>>> df = f_328(db_file, "SELECT age, COUNT(*) AS count FROM users GROUP BY age")
30+
pd.DataFrame:
31+
age count
32+
--- -----
33+
25 3
34+
"""
35+
with sqlite3.connect(db_file) as conn:
36+
return pd.read_sql_query(query, conn)
37+
38+
import unittest
39+
import sqlite3
40+
from faker import Faker
41+
import os
42+
class TestCases(unittest.TestCase):
43+
fake = Faker()
44+
specific_names = [
45+
"John Doe",
46+
"Jane Smith",
47+
"Alice Brown",
48+
"Bob White",
49+
"Charlie Green",
50+
]
51+
specific_ages = [25, 30, 35, 40, 45]
52+
@classmethod
53+
def setUpClass(cls):
54+
"""Set up test data before running tests."""
55+
cls.db_file = cls.generate_test_data_with_file()
56+
@staticmethod
57+
def generate_test_data_with_file() -> str:
58+
"""Generate test data and save it to a temporary SQLite database file."""
59+
db_file = "./temp_test_db.sqlite3"
60+
if os.path.exists(db_file):
61+
os.remove(db_file)
62+
conn = sqlite3.connect(db_file)
63+
create_table_query = """
64+
CREATE TABLE users (
65+
id INTEGER PRIMARY KEY,
66+
name TEXT NOT NULL,
67+
age INTEGER NOT NULL
68+
)
69+
"""
70+
conn.execute(create_table_query)
71+
for _ in range(100):
72+
name = TestCases.fake.name()
73+
age = TestCases.fake.random_int(min=20, max=70)
74+
conn.execute("INSERT INTO users (name, age) VALUES (?, ?)", (name, age))
75+
for name, age in zip(TestCases.specific_names, TestCases.specific_ages):
76+
conn.execute("INSERT INTO users (name, age) VALUES (?, ?)", (name, age))
77+
conn.commit()
78+
conn.close()
79+
return db_file
80+
def test_case_1(self):
81+
"""Test fetching all users."""
82+
df = f_328(self.db_file, "SELECT * FROM users")
83+
self.assertEqual(len(df), 100 + len(self.specific_names))
84+
for name in self.specific_names:
85+
self.assertIn(name, df["name"].values)
86+
def test_case_2(self):
87+
"""Test fetching specific users based on names."""
88+
names_as_strings = "', '".join(self.specific_names)
89+
df = f_328(
90+
self.db_file,
91+
f"SELECT name, age FROM users WHERE name IN ('{names_as_strings}')",
92+
)
93+
for name in self.specific_names:
94+
self.assertIn(name, df["name"].values)
95+
for age in self.specific_ages:
96+
self.assertIn(age, df["age"].values)
97+
def test_case_3(self):
98+
"""Test fetching users based on age condition."""
99+
age_limit = self.fake.random_int(min=20, max=60)
100+
df = f_328(self.db_file, f"SELECT * FROM users WHERE age > {age_limit}")
101+
self.assertTrue(all(df["age"] > age_limit))
102+
def test_case_4(self):
103+
"""Test fetching users and sorting by name."""
104+
df = f_328(self.db_file, "SELECT * FROM users ORDER BY name")
105+
sorted_names = sorted(df["name"].tolist())
106+
self.assertListEqual(df["name"].tolist(), sorted_names)
107+
def test_case_5(self):
108+
"""Test fetching users based on age and sorting by age."""
109+
age_limit = self.fake.random_int(min=20, max=30)
110+
df = f_328(
111+
self.db_file,
112+
f"SELECT * FROM users WHERE age < {age_limit} ORDER BY age DESC",
113+
)
114+
self.assertTrue(all(df["age"] < age_limit))
115+
self.assertTrue(
116+
all(df["age"].iloc[i] >= df["age"].iloc[i + 1] for i in range(len(df) - 1))
117+
)
118+
@classmethod
119+
def tearDownClass(cls):
120+
"""Clean up test data after running tests."""
121+
os.remove(cls.db_file)

data/processed/f_329_jenny_wo_doc.py

Lines changed: 116 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,116 @@
1+
import pandas as pd
2+
import json
3+
4+
5+
def f_329(data: dict, output_path: str = "./default_data_output.json") -> str:
6+
"""Converts the given DataFrame to a dictionary, dropping the column named 'c'
7+
if it exists, and then saves it as a JSON file.
8+
9+
Parameters:
10+
- data (dict): The input data dictionary.
11+
- output_path (str, optional): The path where the JSON file should be saved. Default is './default_data_output.json'.
12+
13+
Returns:
14+
- str: Path where the JSON file was saved.
15+
16+
Requirements:
17+
- pandas
18+
- json
19+
20+
Example:
21+
>>> f_329({'a': [1,2], 'b': [3,4], 'c': [5,6]})
22+
'./default_data_output.json'
23+
>>> f_329({'a': [1,2], 'b': [3,4], 'c': [5,6]}, 'custom/path/results.json')
24+
'custom/path/results.json'
25+
"""
26+
df = pd.DataFrame(data)
27+
# Drop column named 'c' if it exists
28+
df = df.drop(columns="c", errors="ignore")
29+
# Convert the DataFrame to dictionary
30+
data_dict = df.to_dict(orient="dict")
31+
# Save the dictionary as a JSON file
32+
with open(output_path, "w") as file:
33+
json.dump(data_dict, file)
34+
35+
return output_path
36+
37+
import unittest
38+
import pandas as pd
39+
import json
40+
import os
41+
class TestCases(unittest.TestCase):
42+
def read_json_file(self, path):
43+
# Helper function to read content from a JSON file
44+
with open(path, "r") as f:
45+
return json.load(f)
46+
def tearDown(self):
47+
# Cleanup procedure after each test to remove generated files
48+
files_to_remove = [
49+
"./default_data_output.json",
50+
"./custom_data_output_2.json",
51+
"./custom_data_output_3.json",
52+
"./custom_data_output_4.json",
53+
"./custom_data_output_5.json",
54+
]
55+
for file in files_to_remove:
56+
if os.path.exists(file):
57+
os.remove(file)
58+
def convert_keys_to_str(self, dictionary):
59+
# Convert dictionary keys to strings recursively
60+
if not isinstance(dictionary, dict):
61+
return dictionary
62+
return {str(k): self.convert_keys_to_str(v) for k, v in dictionary.items()}
63+
def test_case_1(self):
64+
# Test basic DataFrame with column "c"
65+
data = {"a": [1, 2], "b": [3, 4], "c": [5, 6]}
66+
df = pd.DataFrame(data)
67+
output_path = f_329(data)
68+
self.assertTrue(os.path.exists(output_path))
69+
expected_data = self.convert_keys_to_str(
70+
df.drop(columns="c").to_dict(orient="dict")
71+
)
72+
self.assertEqual(self.read_json_file(output_path), expected_data)
73+
def test_case_2(self):
74+
# Test DataFrame with non-numeric data and column "c"
75+
data = {"name": ["Alice", "Bob"], "country": ["USA", "Canada"], "c": ["x", "y"]}
76+
df = pd.DataFrame(data)
77+
custom_path = "./custom_data_output_2.json"
78+
output_path = f_329(data, custom_path)
79+
self.assertTrue(os.path.exists(output_path))
80+
expected_data = self.convert_keys_to_str(
81+
df.drop(columns="c").to_dict(orient="dict")
82+
)
83+
self.assertEqual(self.read_json_file(output_path), expected_data)
84+
def test_case_3(self):
85+
# Test DataFrame with multiple columns and no column "c"
86+
data = {"age": [25, 30], "height": [170, 175]}
87+
df = pd.DataFrame(data)
88+
custom_path = "./custom_data_output_3.json"
89+
output_path = f_329(data, custom_path)
90+
self.assertTrue(os.path.exists(output_path))
91+
expected_data = self.convert_keys_to_str(df.to_dict(orient="dict"))
92+
self.assertEqual(self.read_json_file(output_path), expected_data)
93+
def test_case_4(self):
94+
# Test DataFrame with mixed data types including column "c"
95+
data = {
96+
"id": [1, 2],
97+
"is_student": [True, False],
98+
"grades": ["A", "B"],
99+
"c": [0.5, 0.8],
100+
}
101+
df = pd.DataFrame(data)
102+
output_path = f_329(data)
103+
self.assertTrue(os.path.exists(output_path))
104+
expected_data = self.convert_keys_to_str(
105+
df.drop(columns="c").to_dict(orient="dict")
106+
)
107+
self.assertEqual(self.read_json_file(output_path), expected_data)
108+
def test_case_5(self):
109+
# Test an empty DataFrame
110+
data = {}
111+
df = pd.DataFrame(data)
112+
custom_path = "./custom_data_output_5.json"
113+
output_path = f_329(data, custom_path)
114+
self.assertTrue(os.path.exists(output_path))
115+
expected_data = self.convert_keys_to_str(df.to_dict(orient="dict"))
116+
self.assertEqual(self.read_json_file(output_path), expected_data)

0 commit comments

Comments
 (0)