Compare commits
9
Commits
main
...
9fc24236d5
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
9fc24236d5 | ||
|
|
7d8416f920 | ||
|
|
44c2dd845a | ||
|
|
9257ba26a6 | ||
|
|
129b2a31de | ||
|
|
1b2bef6591 | ||
|
|
51ccc92cd0 | ||
|
|
609f367689 | ||
|
|
6353bab6bd |
Generated
+10
@@ -0,0 +1,10 @@
|
|||||||
|
# Default ignored files
|
||||||
|
/shelf/
|
||||||
|
/workspace.xml
|
||||||
|
# Ignored default folder with query files
|
||||||
|
/queries/
|
||||||
|
# Datasource local storage ignored files
|
||||||
|
/dataSources/
|
||||||
|
/dataSources.local.xml
|
||||||
|
# Editor-based HTTP Client requests
|
||||||
|
/httpRequests/
|
||||||
Generated
+8
@@ -0,0 +1,8 @@
|
|||||||
|
<?xml version="1.0" encoding="UTF-8"?>
|
||||||
|
<module type="PYTHON_MODULE" version="4">
|
||||||
|
<component name="NewModuleRootManager">
|
||||||
|
<content url="file://$MODULE_DIR$" />
|
||||||
|
<orderEntry type="jdk" jdkName="Python 3.14" jdkType="Python SDK" />
|
||||||
|
<orderEntry type="sourceFolder" forTests="false" />
|
||||||
|
</component>
|
||||||
|
</module>
|
||||||
+6
@@ -0,0 +1,6 @@
|
|||||||
|
<component name="InspectionProjectProfileManager">
|
||||||
|
<settings>
|
||||||
|
<option name="USE_PROJECT_PROFILE" value="false" />
|
||||||
|
<version value="1.0" />
|
||||||
|
</settings>
|
||||||
|
</component>
|
||||||
Generated
+7
@@ -0,0 +1,7 @@
|
|||||||
|
<?xml version="1.0" encoding="UTF-8"?>
|
||||||
|
<project version="4">
|
||||||
|
<component name="Black">
|
||||||
|
<option name="sdkName" value="Python 3.14" />
|
||||||
|
</component>
|
||||||
|
<component name="ProjectRootManager" version="2" project-jdk-name="Python 3.14" project-jdk-type="Python SDK" />
|
||||||
|
</project>
|
||||||
Generated
+8
@@ -0,0 +1,8 @@
|
|||||||
|
<?xml version="1.0" encoding="UTF-8"?>
|
||||||
|
<project version="4">
|
||||||
|
<component name="ProjectModuleManager">
|
||||||
|
<modules>
|
||||||
|
<module fileurl="file://$PROJECT_DIR$/.idea/MyPractice.iml" filepath="$PROJECT_DIR$/.idea/MyPractice.iml" />
|
||||||
|
</modules>
|
||||||
|
</component>
|
||||||
|
</project>
|
||||||
Generated
+6
@@ -0,0 +1,6 @@
|
|||||||
|
<?xml version="1.0" encoding="UTF-8"?>
|
||||||
|
<project version="4">
|
||||||
|
<component name="VcsDirectoryMappings">
|
||||||
|
<mapping directory="" vcs="Git" />
|
||||||
|
</component>
|
||||||
|
</project>
|
||||||
@@ -0,0 +1,108 @@
|
|||||||
|
import csv
|
||||||
|
from collections import defaultdict
|
||||||
|
|
||||||
|
|
||||||
|
def load_data(filepath: str) -> list[dict]:
|
||||||
|
records = []
|
||||||
|
with open(filepath, mode='r', encoding='utf-8') as f:
|
||||||
|
reader = csv.reader(f)
|
||||||
|
for row in reader:
|
||||||
|
if not row: continue
|
||||||
|
name, category, price, quantity, date = row
|
||||||
|
records.append({
|
||||||
|
"name": name.strip(),
|
||||||
|
"category": category.strip(),
|
||||||
|
"price": float(price),
|
||||||
|
"quantity": int(quantity),
|
||||||
|
"date": date.strip()
|
||||||
|
})
|
||||||
|
return records
|
||||||
|
|
||||||
|
|
||||||
|
def clean_data(records: list[dict]) -> list[dict]:
|
||||||
|
cleaned = []
|
||||||
|
for r in records:
|
||||||
|
if r['price'] >= 0 and r['quantity'] >= 0:
|
||||||
|
r['category'] = r['category'].lower()
|
||||||
|
cleaned.append(r)
|
||||||
|
return cleaned
|
||||||
|
|
||||||
|
|
||||||
|
def filter_by_category(records: list[dict], category: str) -> list[dict]:
|
||||||
|
return [r for r in records if r['category'] == category.lower()]
|
||||||
|
|
||||||
|
|
||||||
|
def filter_by_price_range(records: list[dict], min_price: float, max_price: float) -> list[dict]:
|
||||||
|
return [r for r in records if min_price <= r['price'] <= max_price]
|
||||||
|
|
||||||
|
|
||||||
|
def total_revenue(records: list[dict]) -> float:
|
||||||
|
return sum(r['price'] * r['quantity'] for r in records)
|
||||||
|
|
||||||
|
|
||||||
|
def category_revenue(records: list[dict]) -> dict[str, float]:
|
||||||
|
rev_map = defaultdict(float)
|
||||||
|
for r in records:
|
||||||
|
rev_map[r['category']] += r['price'] * r['quantity']
|
||||||
|
return dict(rev_map)
|
||||||
|
|
||||||
|
|
||||||
|
def top_n_items(records: list[dict], n: int) -> list[tuple[str, int]]:
|
||||||
|
counts = defaultdict(int)
|
||||||
|
for r in records:
|
||||||
|
counts[r['name']] += r['quantity']
|
||||||
|
sorted_items = sorted(counts.items(), key=lambda x: x[1], reverse=True)
|
||||||
|
return sorted_items[:n]
|
||||||
|
|
||||||
|
|
||||||
|
def monthly_sales(records: list[dict]) -> dict[str, float]:
|
||||||
|
monthly_rev = defaultdict(float)
|
||||||
|
for r in records:
|
||||||
|
month = r['date'][:7] # Извлекает YYYY-MM
|
||||||
|
monthly_rev[month] += r['price'] * r['quantity']
|
||||||
|
return dict(sorted(monthly_rev.items()))
|
||||||
|
|
||||||
|
|
||||||
|
def best_selling_category(records: list[dict]) -> str:
|
||||||
|
cat_rev = category_revenue(records)
|
||||||
|
if not cat_rev: return ""
|
||||||
|
return max(cat_rev, key=cat_rev.get)
|
||||||
|
|
||||||
|
|
||||||
|
def export_summary(records: list[dict], output_path: str) -> None:
|
||||||
|
rev = total_revenue(records)
|
||||||
|
best_cat = best_selling_category(records)
|
||||||
|
top3 = top_n_items(records, 3)
|
||||||
|
monthly = monthly_sales(records)
|
||||||
|
|
||||||
|
with open(output_path, 'w', encoding='utf-8') as f:
|
||||||
|
f.write("СВОДНЫЙ ОТЧЕТ ПО ПРОДАЖАМ\n")
|
||||||
|
f.write("=" * 30 + "\n")
|
||||||
|
f.write(f"Общая выручка: {rev:.2f}\n")
|
||||||
|
f.write(f"Лучшая категория: {best_cat}\n\n")
|
||||||
|
f.write("Топ-3 товара (по количеству):\n")
|
||||||
|
for name, qty in top3:
|
||||||
|
f.write(f"- {name}: {qty} шт.\n")
|
||||||
|
f.write("\nВыручка по месяцам:\n")
|
||||||
|
for month, m_rev in monthly.items():
|
||||||
|
f.write(f"- {month}: {m_rev:.2f}\n")
|
||||||
|
|
||||||
|
|
||||||
|
def main() -> None:
|
||||||
|
data = load_data("data/sales.csv")
|
||||||
|
data = clean_data(data)
|
||||||
|
filtered = filter_by_price_range(data, 10, 500)
|
||||||
|
rev = total_revenue(filtered)
|
||||||
|
category_revenue(filtered)
|
||||||
|
best_cat = best_selling_category(filtered)
|
||||||
|
top3 = top_n_items(filtered, 3)
|
||||||
|
monthly = monthly_sales(filtered)
|
||||||
|
print(f"--- Результаты анализа (фильтр: 10-500 руб) ---")
|
||||||
|
print(f"Общая выручка: {rev:.2f}")
|
||||||
|
print(f"Лучшая категория: {best_cat.capitalize()}")
|
||||||
|
print(f"Топ-3 товара: {top3}")
|
||||||
|
print(f"Выручка по месяцам: {monthly}")
|
||||||
|
export_summary(filtered, "report.txt")
|
||||||
|
print(f"\nОтчет успешно сохранен в report.txt")
|
||||||
|
if __name__ == "__main__":
|
||||||
|
main()
|
||||||
Reference in New Issue
Block a user