程序扩展性的一个实例(下)
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()
相关文章推荐
- 程序扩展性的一个实例(上)
- 如何让一个程序只有一个运行实例
- 让程序只运行一个实例(Delphi篇)
- Java实例 定义任务的一个发射小程序 实现Runnable接口并编写run()方法
- C# WinForm 只运行一个程序实例
- C#.NET禁止一个程序启动多个实例
- 在VS2010中设计一个程序中打开另一个应用程序的实例
- C#实现让程序只能打开一个实例(总结3方法)
- Python中使用wxPython开发的一个简易笔记本程序实例
- 利用互斥控制程序只有一个运行实例
- c# 程序只能运行一次(多次运行只能打开同一个程序) 并激活第一个实例,使其获得焦点,并在最前端显示.
- 5.11 一个用Spring AOP实现异常处理和记录程序执行时间的实例
- 确保程序只运行一个实例
- 一个简单的linux下网络程序实例-网络编程入门 收藏
- 实例--post请求,完成一个抽奖程序概率的测试
- IOS错误异常一个未知的实例在运行,出现程序崩溃
- 只能运行程序的一个实例
- 利用HttpModuler实现WEB程序同一时间只让一个用户实例登陆
- c# 程序只能运行一次(多次运行只能打开同一个程序) 并激活第一个实例,使其获得焦点,并在最前端显示.
- Linux Socket编程实例(一个Hello World程序)