本文整理汇总了Python中models.City类的典型用法代码示例。如果您正苦于以下问题:Python City类的具体用法?Python City怎么用?Python City使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
在下文中一共展示了City类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: test_parse_city
def test_parse_city(self):
response = '''
{"response":[
{"cid":1,"title":"Москва","region":"Regione Abruzzo область"},
{"cid":1074996,"title":"Москва","area":"Порховский район","region":"Псковская область"},
{"cid":1102561,"title":"Москва","area":"Пеновский район","region":"Тверская область"},
{"cid":1130701,"title":"Москва","area":"Верхошижемский район","region":"Кировская область"}
]}
'''
country = CountryFactory.create(remote_id=1)
instance = City(country=country)
instance.parse(json.loads(response)['response'][0])
instance.save()
self.assertEqual(instance.remote_id, 1)
self.assertEqual(instance.name, u'Москва')
instance = City(country=country)
instance.parse(json.loads(response)['response'][1])
instance.save()
self.assertEqual(instance.remote_id, 1074996)
self.assertEqual(instance.name, u'Москва')
self.assertEqual(instance.area, u"Порховский район")
self.assertEqual(instance.region, u"Псковская область")
示例2: test01_setup
def test01_setup(self):
"Setting up for related model tests."
for name, state, lon, lat in cities:
loc = Location(point=Point(lon, lat))
loc.save()
c = City(name=name, state=state, location=loc)
c.save()
示例3: create_tables
def create_tables():
from models import User, Message, Note, Relationship, City, Pinche
# User.create_table()
Relationship.create_table()
Note.create_table()
Message.create_table()
City.create_table()
Pinche.create_table()
示例4: postCity
def postCity():
try:
city = City()
if "name" in request.form: city.name = request.form["name"]
if "maxLat" in request.form: city.pack = request.form["maxLat"]
if "maxLon" in request.form: city.pack = request.form["maxLon"]
if "minLat" in request.form: city.pack = request.form["minLat"]
if "minLon" in request.form: city.pack = request.form["minLon"]
db.session.add(city)
db.session.commit()
return make_response(jsonify( { 'ok': 'city created' } ), 200)
except:
return make_response(jsonify( { 'error': 'Server Error' } ), 500)
示例5: test11_lookup_insert_transform
def test11_lookup_insert_transform(self):
"Testing automatic transform for lookups and inserts."
if DISABLE:
return
# San Antonio in 'WGS84' (SRID 4326)
sa_4326 = "POINT (-98.493183 29.424170)"
wgs_pnt = fromstr(sa_4326, srid=4326) # Our reference point in WGS84
# Oracle doesn't have SRID 3084, using 41157.
if SpatialBackend.oracle:
# San Antonio in 'Texas 4205, Southern Zone (1983, meters)' (SRID 41157)
# Used the following Oracle SQL to get this value:
# SELECT SDO_UTIL.TO_WKTGEOMETRY(SDO_CS.TRANSFORM(SDO_GEOMETRY('POINT (-98.493183 29.424170)', 4326), 41157)) FROM DUAL;
nad_wkt = "POINT (300662.034646583 5416427.45974934)"
nad_srid = 41157
else:
# San Antonio in 'NAD83(HARN) / Texas Centric Lambert Conformal' (SRID 3084)
nad_wkt = (
"POINT (1645978.362408288754523 6276356.025927528738976)"
) # Used ogr.py in gdal 1.4.1 for this transform
nad_srid = 3084
# Constructing & querying with a point from a different SRID. Oracle
# `SDO_OVERLAPBDYINTERSECT` operates differently from
# `ST_Intersects`, so contains is used instead.
nad_pnt = fromstr(nad_wkt, srid=nad_srid)
if SpatialBackend.oracle:
tx = Country.objects.get(mpoly__contains=nad_pnt)
else:
tx = Country.objects.get(mpoly__intersects=nad_pnt)
self.assertEqual("Texas", tx.name)
# Creating San Antonio. Remember the Alamo.
sa = City(name="San Antonio", point=nad_pnt)
sa.save()
# Now verifying that San Antonio was transformed correctly
sa = City.objects.get(name="San Antonio")
self.assertAlmostEqual(wgs_pnt.x, sa.point.x, 6)
self.assertAlmostEqual(wgs_pnt.y, sa.point.y, 6)
# If the GeometryField SRID is -1, then we shouldn't perform any
# transformation if the SRID of the input geometry is different.
# SpatiaLite does not support missing SRID values.
if not SpatialBackend.spatialite:
m1 = MinusOneSRID(geom=Point(17, 23, srid=4326))
m1.save()
self.assertEqual(-1, m1.geom.srid)
示例6: load2mysql
def load2mysql(self):
with open(self.path) as f:
provinces = json.load(f)
for province in provinces:
# print province["name"]
p = Province(name=province["name"])
p.save()
for city in province["city"]:
# print " %s" % city["name"]
c = City(name=city["name"], province=p)
c.save()
for area in city["area"]:
# print " %s" % area
cou = County(name=area, city=c)
cou.save()
示例7: view_city
def view_city(environ, id_region=None):
if not id_region:
return ''
city_list = City.filter(region_id=int(id_region[0]))
response = u'<option value="">---</option>'
for city in city_list:
response += u'<option value="{0}">{1}</option>'.format(city.id, city.name)
return HttpResponse(response)
示例8: homepage
def homepage():
query = request.args.get("postal_code", "84510")
results = []
for city in City.select().where(City.postal_code == query):
results.append({"insee": city.insee, "name": city.name, "postal_code": city.postal_code, "label": city.label})
return jsonify(results=results)
示例9: seed_cities
def seed_cities(self, number):
"""
Crée des enregistrements aléatoires dans la table des villes
:param number: nombre de villes
:return: void
"""
for i in range(0, number):
city = City()
city.name = self.fake.city()
city.country = self.fake.country()
city.is_capital = self.fake.pybool()
self.city_service.save(city)
capitals = self.city_service.all_capitals()
non_capitals = self.city_service.all_non_capitals()
for city in non_capitals:
city.capital_id = random.choice(capitals).id
self.city_service.save(city)
示例10: create_or_getCity
def create_or_getCity(cityName, state, lat,lon):
try:
c = City.objects.get(name=cityName)
except City.DoesNotExist:
r = requests.get("http://photon.komoot.de/api/?q={},{}&limit=1&lat={}&lon={}".format(cityName, state.name, lat,lon)).json()
lastCity = City.objects.latest('created_on')
c = City()
c.id = lastCity.id +1
c.name=cityName
c.stateID=state.id
c.lat= r['features'][0]['geometry']['coordinates'][1]
c.lon= r['features'][0]['geometry']['coordinates'][0]
c.live="true"
c.save()
return c
示例11: main
def main():
cities = City.select()
city_dict = {}
for city in cities:
if city.parent is None:
city_dict[city.id] = []
city_dict[city.id].append({'id': city.id, 'name': city.name})
else:
city_dict[city.parent.id].append({'id': city.id, 'name': city.name})
print(json.dumps(city_dict, ensure_ascii=False))
示例12: save_to_db
def save_to_db(self, dic):
assert all(map(dic.has_key, ['title', 'original_price', 'price', 'detail', 'url'])),\
"Information incomplete."
url = dic['url']
original_price = dic['original_price'].text.encode('utf8')
price = dic['price'].text.encode('utf8')
title = dic['title'].text # title is unicode
detail = dic['detail'].renderContents(encoding='utf8')
detail = utils.clean_detail(detail, self.home_url)
# Data formatting & validation.
try:
original_price, price = map(lambda s: int(re.search(r'(\d+)', s).group()),
[original_price, price])
except TypeError:
logging.error("Price conversion failed. Detailed info: %s", [original_price, price])
return
except AttributeError:
logging.error("Regex failed on %s", [original_price, price])
return
if len(title) > 500 or len(title) < 10:
logging.error("Title length too short or too long : %s", title)
return
if len(detail) < 20:
logging.error("Detail too short. %s", detail)
return
# Save to db.
try:
site = Site.select(Site.q.url == self.home_url)
assert(site.count() == 1), "%s not found or dups." % self.home_url
title = utils.lstrip(title, [s.decode('utf8') for s in ('今日团购', '今日精选', ':')])
title = title.strip()
title='[%s] %s' % (site[0].name, title)
city_name = self.index_urls[url]
city = City.select(City.q.name == city_name.decode('utf8'))
assert city.count() == 1, "%s not found or dups." % city_name
cityID = city[0].id
if Deal.select(AND(Deal.q.title == title, Deal.q.cityID == cityID)).count() > 0:
logging.info("Title dups %s" % title)
return
deal = Deal(url=url, title=title, price=price, originalPrice=original_price,
detail=detail.decode('utf8'),cityID=cityID, siteID=site[0].id)
logging.info('%s OK', url)
except:
# Simple handling for the moment.
logging.error("Error occured while saving data : %s", sys.exc_info())
示例13: test11_lookup_insert_transform
def test11_lookup_insert_transform(self):
"Testing automatic transform for lookups and inserts."
if DISABLE: return
# San Antonio in 'WGS84' (SRID 4326)
sa_4326 = 'POINT (-98.493183 29.424170)'
wgs_pnt = fromstr(sa_4326, srid=4326) # Our reference point in WGS84
# Oracle doesn't have SRID 3084, using 41157.
if oracle:
# San Antonio in 'Texas 4205, Southern Zone (1983, meters)' (SRID 41157)
# Used the following Oracle SQL to get this value:
# SELECT SDO_UTIL.TO_WKTGEOMETRY(SDO_CS.TRANSFORM(SDO_GEOMETRY('POINT (-98.493183 29.424170)', 4326), 41157)) FROM DUAL;
nad_wkt = 'POINT (300662.034646583 5416427.45974934)'
nad_srid = 41157
else:
# San Antonio in 'NAD83(HARN) / Texas Centric Lambert Conformal' (SRID 3084)
nad_wkt = 'POINT (1645978.362408288754523 6276356.025927528738976)' # Used ogr.py in gdal 1.4.1 for this transform
nad_srid = 3084
# Constructing & querying with a point from a different SRID. Oracle
# `SDO_OVERLAPBDYINTERSECT` operates differently from
# `ST_Intersects`, so contains is used instead.
nad_pnt = fromstr(nad_wkt, srid=nad_srid)
if oracle:
tx = Country.objects.get(mpoly__contains=nad_pnt)
else:
tx = Country.objects.get(mpoly__intersects=nad_pnt)
self.assertEqual('Texas', tx.name)
# Creating San Antonio. Remember the Alamo.
sa = City(name='San Antonio', point=nad_pnt)
sa.save()
# Now verifying that San Antonio was transformed correctly
sa = City.objects.get(name='San Antonio')
self.assertAlmostEqual(wgs_pnt.x, sa.point.x, 6)
self.assertAlmostEqual(wgs_pnt.y, sa.point.y, 6)
示例14: print
import csv
import os
import time
from models import City
from app import db
City.create_table(True)
i = 0
data_source = []
print(City.delete().execute())
with open("cp.csv", "r", encoding="utf-8") as csvfile:
spamreader = csv.reader(csvfile, delimiter=";")
for row in spamreader:
data_source.append({"insee": row[0], "name": row[1], "postal_code": row[2], "label": row[3]})
i += 1
if len(data_source) > 100:
print("+1")
with db.atomic():
City.insert_many(data_source).execute()
data_source = []
time.sleep(0.02)
示例15: test02_proxy
def test02_proxy(self):
"Testing Lazy-Geometry support (using the GeometryProxy)."
#### Testing on a Point
pnt = Point(0, 0)
nullcity = City(name='NullCity', point=pnt)
nullcity.save()
# Making sure TypeError is thrown when trying to set with an
# incompatible type.
for bad in [5, 2.0, LineString((0, 0), (1, 1))]:
try:
nullcity.point = bad
except TypeError:
pass
else:
self.fail('Should throw a TypeError')
# Now setting with a compatible GEOS Geometry, saving, and ensuring
# the save took, notice no SRID is explicitly set.
new = Point(5, 23)
nullcity.point = new
# Ensuring that the SRID is automatically set to that of the
# field after assignment, but before saving.
self.assertEqual(4326, nullcity.point.srid)
nullcity.save()
# Ensuring the point was saved correctly after saving
self.assertEqual(new, City.objects.get(name='NullCity').point)
# Setting the X and Y of the Point
nullcity.point.x = 23
nullcity.point.y = 5
# Checking assignments pre & post-save.
self.assertNotEqual(Point(23, 5), City.objects.get(name='NullCity').point)
nullcity.save()
self.assertEqual(Point(23, 5), City.objects.get(name='NullCity').point)
nullcity.delete()
#### Testing on a Polygon
shell = LinearRing((0, 0), (0, 100), (100, 100), (100, 0), (0, 0))
inner = LinearRing((40, 40), (40, 60), (60, 60), (60, 40), (40, 40))
# Creating a State object using a built Polygon
ply = Polygon(shell, inner)
nullstate = State(name='NullState', poly=ply)
self.assertEqual(4326, nullstate.poly.srid) # SRID auto-set from None
nullstate.save()
ns = State.objects.get(name='NullState')
self.assertEqual(ply, ns.poly)
# Testing the `ogr` and `srs` lazy-geometry properties.
if gdal.HAS_GDAL:
self.assertEqual(True, isinstance(ns.poly.ogr, gdal.OGRGeometry))
self.assertEqual(ns.poly.wkb, ns.poly.ogr.wkb)
self.assertEqual(True, isinstance(ns.poly.srs, gdal.SpatialReference))
self.assertEqual('WGS 84', ns.poly.srs.name)
# Changing the interior ring on the poly attribute.
new_inner = LinearRing((30, 30), (30, 70), (70, 70), (70, 30), (30, 30))
ns.poly[1] = new_inner
ply[1] = new_inner
self.assertEqual(4326, ns.poly.srid)
ns.save()
self.assertEqual(ply, State.objects.get(name='NullState').poly)
ns.delete()