forked from data-mining-the-city/week-2
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpyorientAssignment.py
63 lines (46 loc) · 1.72 KB
/
pyorientAssignment.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
import pyorient
import sys
client = pyorient.OrientDB("localhost", 2424)
session_id = client.connect("root", "admin")
db_name = "soufun"
db_username = "admin"
db_password = "admin"
if client.db_exists( db_name, pyorient.STORAGE_TYPE_MEMORY ):
client.db_open( db_name, db_username, db_password )
print db_name + " opened successfully"
else:
print "database [" + db_name + "] does not exist! session ending..."
sys.exit()
lat1 = 22.532498
lat2 = 22.552317
lng1 = 114.044329
lng2 = 114.076644
query = 'SELECT FROM Listing WHERE latitude BETWEEN {} AND {} AND longitude BETWEEN {} AND {}'
records = client.command(query.format(lat1, lat2, lng1, lng2))
numListings = len(records)
print 'received ' + str(numListings) + ' records'
# [ANALYZE THE RETURNED RECORDS TO DETERMINE THE MINIMUM, MAXIMUM, AND AVERAGE PRICE OF THE LISTINGS]
# Hint: the loop that you need to look into each record is already provided below.
# To find the average price, add up all the prices and divide by the number of results
# To find the minimum price, create a variable and initialize it to a very large number,
# then test each price to see if it is smaller than the current minimum. If it is, update
# the minimum variable with that price. You can do something similar to find the maximum.
min_p = 20001
max_p = 0
sum_p = 0
for record in records:
sum_p += record.price
if record.price<min_p:
min_p = record.price
else:
min_p = min_p
if record.price>max_p:
max_p=record.price
else:
max_p = max_p
avg_p = sum_p/len(records)
# [PRINT OUT THE RESULTING VALUES BY CONCATENATING THEM TO THESE LINES TO CHECK YOUR WORK]
print 'min price: ' + str(min_p)
print 'max price: ' + str(max_p)
print 'average price: ' + str(avg_p)
client.db_close()