您的位置:首页 > 其它

程序扩展性的一个实例(下)

2012-11-16 16:25 274 查看
上代码,主要是整个流程。细节忽略

#! /usr/bin/env python
# -*- coding: utf-8 -*-
"""
@author: zhoujiebing
@contact: zhoujiebing@maimiaotech.com
@date: 2012-11-16 11:30
@version: 1.0.0
@license: Copyright maimiaotech.com
@copyright: Copyright maimiaotech.com

"""
import sys
sys.path.append('http://www.cnblogs.com/')
from shop_info_db import ShopInfoDB
from database_operator import DBOpsCampaignSelect, DBOpsItemsSelect, DBOpsKeyword, DBOpsActionLog, DBOpsShopInfo
MONGODB_HOST = 'localhost'
MONGODB_PORT = 2006
from pymongo import Connection
mongoConn = Connection(host = MONGODB_HOST, port = MONGODB_PORT)

class Rule:
"""
Base Rule Class
"""
def __init__(self):
self.child_rule = []

def judge_rule(self, shop_id, result):
return False

class AccessTokenRule(Rule):
"""
judge AccessToken
"""
def __init__(self, shop_status_dict):
Rule.__init__(self)
self.shop_status_dict = shop_status_dict

def judge_rule(self, shop_id, result):
shop_status = self.shop_status_dict[shop_id]
if shop_status['session_expired']:
resultinfo = ['Invalid AccessToken', shop_id]
result.append(resultinfo)
return True
if shop_status['insuff_level']:
resultinfo = ['R1 AccessToken', shop_id]
result.append(resultinfo)
return True
return False

class CampaignRule(Rule):
"""
judge campaign self
"""
def __init__(self):
Rule.__init__(self)

def judge_rule(self, shop_id, result):
campaign_select_db_ops = DBOpsCampaignSelect(mongoConn, str(shop_id))
campaigns = campaign_select_db_ops.get_all()
if len(campaigns) != 1:
resultinfo = ['No Campaign', shop_id]
result.append(resultinfo)
return True

if not campaigns[0]['init_success']:
resultinfo = ['Campaign init Fail', shop_id]
result.append(resultinfo)
return True
return False

class ItemRule(Rule):
"""
judge items
"""
def __init__(self):
Rule.__init__(self)

def judge_rule(self, shop_id, result):
item_select_db_ops = DBOpsItemsSelect(mongoConn, str(shop_id))
items_num = len(item_select_db_ops.get_active_items_all())
if items_num <= 10:
resultinfo = ['Item few', shop_id, str(items_num)]
result.append(resultinfo)
return True
return False

class KeywordRule(Rule):
"""
judge keyword
"""
def __init__(self):
Rule.__init__(self)

def judge_rule(self, shop_id, result):
keyword_db_ops = DBOpsKeyword(mongoConn, str(shop_id))
keywords_num = keyword_db_ops.coll.find().count()
if keywords_num <= 100:
resultinfo = ['Keyword few', shop_id, str(keywords_num)]
result.append(resultinfo)
return True
return False

class ActionRule(Rule):
"""
judge action
"""
def __init__(self):
Rule.__init__(self)

def judge_rule(self, shop_id, result):
action_db_ops = DBOpsActionLog(mongoConn, str(shop_id))
accumulate = action_db_ops.get_last_week_action()
action_num = accumulate['add_words'] + accumulate['delete_words'] + accumulate['change_price']
if action_num <= 100:
resultinfo = ['Action few', shop_id, str(action_num)]
return True
return False

def judge_rule(shop_id, unnormal_result, rule_relation):
"""
judge the shop
"""
while 1:
if rule_relation.judge_rule(shop_id, unnormal_result):
break
if not rule_relation.child_rule:
break
if len(rule_relation.child_rule) == 1:
rule_relation = rule_relation.child_rule[0]
else:
for rule_relation in rule_relation.child_rule:
if judge_rule(shop_id, unnormal_result, rule_relation):
return

if __name__ == '__main__':
file_name = 'unnormal_data.csv'
file_obj = file(file_name, 'w')

shop_info = ShopInfoDB(mongoConn)
shop_id_list = shop_info.get_all_shop_id_list()
shop_status_dict = {}
for shop in shop_info.shop_status.find():
shop_status_dict[int(shop['_id'])] = shop
#rule init
rule_relation = Rule()
access_token_rule = AccessTokenRule(shop_status_dict)
campaign_rule = CampaignRule()
item_rule = ItemRule()
keyword_rule = KeywordRule()
action_rule = ActionRule()

#rule relation init
rule_relation.child_rule.append(access_token_rule)
access_token_rule.child_rule.append(campaign_rule)
campaign_rule.child_rule.append(item_rule)
campaign_rule.child_rule.append(keyword_rule)
campaign_rule.child_rule.append(action_rule)

unnormal_result = []
for shop_id in shop_id_list:
judge_rule(shop_id, unnormal_result, rule_relation)
unnormal_result.sort(key = lambda x:x[0])
for info in unnormal_result:
shop_id = info[1]
info[1] = str(shop_id)
nick = shop_status_dict[shop_id]['nick']
info.insert(2, nick)
file_obj.write(','.join(info))
file_obj.write('\n')
file_obj.close()
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: