本文整理汇总了Python中Item.genPK方法的典型用法代码示例。如果您正苦于以下问题:Python Item.genPK方法的具体用法?Python Item.genPK怎么用?Python Item.genPK使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类Item
的用法示例。
在下文中一共展示了Item.genPK方法的3个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: __init__
# 需要导入模块: import Item [as 别名]
# 或者: from Item import genPK [as 别名]
def __init__(self):
'''
set up the test data from an external csv file
'''
self._testData = []
for newRec in csv.DictReader(open("TestData.csv", 'rb'), delimiter=','):
newRec['pk'] = Item.genPK(newRec['name'], newRec['category'])
newRec['review'] = listify(newRec['review'])
# convert lat / lon to floats : float('str')
# If already have the PK, append to the existing review entry
if not self._testData:
self._testData.append(newRec)
else:
existingUpdated = False
for existingRec in self._testData:
if (existingRec['pk'] == newRec['pk']) and newRec['review']:
existingRec['review'].append(newRec['review'][0])
existingUpdated = True
if not existingUpdated:
self._testData.append(newRec)
示例2: testValidPK
# 需要导入模块: import Item [as 别名]
# 或者: from Item import genPK [as 别名]
def testValidPK(self):
name = "WhatAGreatPlaceToRockAndRoll"
category = "Recreational"
myuuid = str(uuid.UUID(binascii.hexlify("%d%s" % (0, name[0:15]))))
self.assertEqual(Item.genPK(name, category), myuuid)
示例3: PUT
# 需要导入模块: import Item [as 别名]
# 或者: from Item import genPK [as 别名]
def PUT(self, ignoreMe):
"""
PUT == Create
"""
jsonData = web.data()
item = Item.ThingToDo()
try:
itemData = json.JSONDecoder().decode(jsonData)
except:
logging.error("Item-PUT: unable to decode json %s" % jsonData)
web.badrequest()
return
if isTestMode(): # check env. if in test mode, import dbconn mock
import Shared
dbConn = Shared.dbMock
else:
dbConn = DataAccessLayer.DataAccessLayer()
# if 'lat' in itemData and 'lon' in itemData:
# itemData['latlon'] = Item.LatLon(itemData['lat'], itemData['lon'])
#
# name, category and createdBy are required
if not ("name" in itemData and "category" in itemData and "createdBy" in itemData):
logging.info("Item-PUT: missing required args")
web.badrequest()
return
# One of address or lat/lon pair required
if "address" not in itemData and not ("lat" in itemData and "lon" in itemData):
logging.info("Item-PUT: missing address and lat/lon")
web.badrequest()
return
otherArgs = {}
for attr, val in itemData.iteritems():
if attr == "name" or attr == "category" or attr == "createdBy":
# remove from the dict so that what remains is what setAttrs expects for keyword args
continue
# special handling of lat/lon so we get a textual represenation, rather than a numeric
if attr == "lat":
ll = Item.LatLon(val, 0.0)
otherArgs[attr] = ll._lat
continue
if attr == "lon":
ll = Item.LatLon(0.0, val)
otherArgs[attr] = ll._lon
continue
# everything else just gets added to the keyword args
otherArgs[attr] = val
try:
item.setAttrs(itemData["name"], itemData["category"], itemData["createdBy"], **otherArgs)
except:
logging.error("Item-PUT: unable to set attributes")
web.badrequest()
return
# If the item already exists, that's an error - caller should be using POST, not PUT
# Make the PK and see if it exists. AttributeError if it does
PK = Item.genPK(itemData["name"], itemData["category"])
searchFor = Item.SearchFor()
searchFor.setAttr("pk", PK)
where = searchFor.makeWhereClause()
try:
rtn = dbConn.read(where)
except Exception as ex:
logging.error("Item-PUT: Unexpected exception checking for existing record - %s", ex)
web.badrequest()
return
if rtn != None:
logging.info("Item-PUT: item already exists (%s)", PK)
web.conflict()
return
# now that we have an item that doesn't exist, write it to the dbconn
try:
dbConn.write(item._serialized)
except Exception as ex:
logging.error("Item-PUT: unexpected exception writing item - %s", ex)
web.badrequest()
return
return json.JSONEncoder().encode(item._serialized)