-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
95 lines (77 loc) · 3.35 KB
/
main.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
from pymongo import MongoClient, ASCENDING
import csv
from pprint import pprint
from datetime import datetime
from urllib.parse import quote_plus
def mongo_connect(login='', password='', server='localhost', port=27017, db=''):
if login:
url = "mongodb://%s:%s@%s:%s/%s" % (quote_plus(login), quote_plus(password),
quote_plus(server), port, quote_plus(db))
else:
url = "mongodb://%s:%s/%s" % (quote_plus(server), port, quote_plus(db))
try:
client = MongoClient(url)
client.admin.command('ismaster')
except Exception as err:
print("Could not connect to MongoDB. Detailed: ", err)
else:
print("Connected successfully!!!")
return client
def read_data(path_to_file, db_object):
with open(path_to_file, encoding='utf8') as file:
row = csv.DictReader(file)
db_object.insert_many(row)
def format_data(db_object, column_price='Цена', column_date='Дата'):
for doc in db_object.find():
db_object.find_one_and_update({'_id': doc['_id'], column_price: {'$type': 'string'}},
{'$set': {column_price: int(doc[column_price])}})
if isinstance(doc[column_date], str):
raw_data = doc[column_date]
list_data = [i if (len(i) == 2) else f'0{i}' for i in raw_data.split('.')]
str_data = raw_data if len(raw_data) == 5 else list_data
try:
data_ = datetime.fromisoformat('2019-{0[1]}-{0[0]}'.format(str_data))
db_object.find_one_and_update({'_id': doc['_id'], column_date: {'$type': 'string'}},
{'$set': {column_date: data_}})
except ValueError as err:
print('Error in value date. Detailed: ', err)
except Exception as err:
print(err)
def find_cheapest(db_object, column_find='Цена'):
sort = db_object.find().sort(column_find, ASCENDING)
pprint(list(sort))
def find_by_name(db_object, name_find, column_find, column_sort='Цена'):
artist = db_object.find({column_find: {'$regex': '.*%s.*' % name_find,
'$options': 'i'}}).sort(column_sort, ASCENDING)
pprint(list(artist))
def find_by_date(db_object, start_date, end_date, column_find='Дата'):
try:
start = datetime.fromisoformat(start_date)
end = datetime.fromisoformat(end_date)
data_ = db_object.find({column_find: {'$gte': start, '$lte': end}}
).sort(column_find, ASCENDING)
pprint(list(data_))
except ValueError as err:
print('Error in value date. Detailed: ', err)
except Exception as err:
print(err)
if __name__ == '__main__':
# input your credentials
conn = mongo_connect()
concerts = conn['netology_db']['concerts']
read_data('artists.csv', concerts)
format_data(concerts)
# sort by price
find_cheapest(concerts)
print(f'{"=" * 90}')
# search by artist
find_by_name(concerts,'on', 'Исполнитель')
print(f'{"=" * 90}')
# search by location
find_by_name(concerts, 'st', 'Место')
print(f'{"=" * 90}')
# sort by date for a specific period
find_by_date(concerts, '2019-07-01', '2019-07-30')
conn.close()