forked from data-mining-the-city/week-2
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathpyorientAssignment.py
More file actions
62 lines (43 loc) · 1.86 KB
/
pyorientAssignment.py
File metadata and controls
62 lines (43 loc) · 1.86 KB
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
import pyorient
import sys
client = pyorient.OrientDB("localhost", 2424)
session_id = client.connect("root", "C036DE6CC47CA21FBD847B71C788D1F0D2A1ADADFC0283428E784C2514622E8E")
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.
maxPrice = float("inf") * (-1)
minPrice = float("inf")
avgSum = 0
for record in records:
print record.price
avgSum = avgSum + record.price
if record.price>maxPrice:
maxPrice = record.price
if record.price<minPrice:
minPrice=record.price
avgPrice=avgSum/numListings
# [PRINT OUT THE RESULTING VALUES BY CONCATENATING THEM TO THESE LINES TO CHECK YOUR WORK]
print 'min price: ' + str(minPrice)
print 'max price: ' + str(maxPrice)
print 'average price: '+ str(avgPrice)
client.db_close()