def _format_optdict(optdict, script=False, ignore=None): opts = [] for opt, value in optdict.iteritems(): if not ignore or opt not in ignore: opts.append('-%s' % opt) if value is not None: opts.append(_format_optvalue(value, script)) return _flatten(opts)
def _list_from_statespec(stuple): nval = [] for val in stuple: typename = getattr(val, 'typename', None) if typename is None: nval.append(val) val = str(val) if typename == 'StateSpec': val = val.split() nval.append(val) it = iter(nval) return [ _flatten(spec) for spec in zip(it, it) ]
def dragged(event): global point_draw point_draw = False if selected_corner == None: return global triangle triangle[selected_corner] = [event.x, event.y] canvas.coords(triangle_item, _flatten(triangle)) for p in points: color = 'red' oval_coords = canvas.coords(p) point = array([oval_coords[0]+point_radius, oval_coords[1]+point_radius]) if is_inside(point, array(triangle)): color = 'green' canvas.itemconfig(p, fill=color)
def update_clock(self): # render the particles points = [0] * 6 for p in self.particles: points[0] = p.location[0] points[1] = p.location[1] r = p.velocity[1] + (math.pi * 5.0) / 12.0 points[2] = points[0] - (int) (math.cos(r) * PARTICLE_SIZE) points[3] = points[1] - (int) (math.sin(r) * PARTICLE_SIZE) r2 = p.velocity[1] + (math.pi * 7.0) / 12.0 points[4] = points[0] - (int) (math.cos(r2) * PARTICLE_SIZE) points[5] = points[1] - (int) (math.sin(r2) * PARTICLE_SIZE) self.c.coords(p.poly,_flatten(points)) # move the particle dx = math.cos(r) dy = math.sin(r) p.location[0] = p.location[0] + (dx * p.velocity[0]) p.location[1] = p.location[1] + (dy * p.velocity[0]) # handle wraps if p.location[0] < 0: p.location[0] = CANVAS_WIDTH if p.location[1] < 0: p.location[1] = CANVAS_HEIGHT if p.location[0] > CANVAS_WIDTH: p.location[0] = 0 if p.location[1] > CANVAS_HEIGHT: p.location[1] = 0 self.flock() # Next frame. self.root.after(100, self.update_clock)
def update_clock(self): # render the particles points = [0] * 6 for p in self.particles: points[0] = p.location[0] points[1] = p.location[1] r = p.velocity[1] + (math.pi * 5.0) / 12.0 points[2] = points[0] - (int)(math.cos(r) * PARTICLE_SIZE) points[3] = points[1] - (int)(math.sin(r) * PARTICLE_SIZE) r2 = p.velocity[1] + (math.pi * 7.0) / 12.0 points[4] = points[0] - (int)(math.cos(r2) * PARTICLE_SIZE) points[5] = points[1] - (int)(math.sin(r2) * PARTICLE_SIZE) self.c.coords(p.poly, _flatten(points)) # move the particle dx = math.cos(r) dy = math.sin(r) p.location[0] = p.location[0] + (dx * p.velocity[0]) p.location[1] = p.location[1] + (dy * p.velocity[0]) # handle wraps if p.location[0] < 0: p.location[0] = CANVAS_WIDTH if p.location[1] < 0: p.location[1] = CANVAS_HEIGHT if p.location[0] > CANVAS_WIDTH: p.location[0] = 0 if p.location[1] > CANVAS_HEIGHT: p.location[1] = 0 self.flock() # Next frame. self.root.after(100, self.update_clock)
def main(): login() titles = check_msg() #已完成列表 done = DB.done() all = DB.all() for title in titles: print title if title not in done: craw = fetch_single_page(title) if cf.get('sheet', 'done_sign') in _flatten(craw): isdone = '1' else: isdone = '0' #项目在数据库中则更新,不存在则添加 if title in all: DB.update(title, unicode(craw), isdone) else: DB.insert(title, unicode(craw), isdone) #特喵的操控IE就和伺候大爷一样啊 browser.quit()
def main(): login() titles = check_msg() #已完成列表 done = DB.done() all = DB.all() for title in titles: print title if title not in done: craw = fetch_single_page(title) if cf.get('sheet','done_sign') in _flatten(craw): isdone = '1' else: isdone = '0' #项目在数据库中则更新,不存在则添加 if title in all: DB.update(title,unicode(craw),isdone) else: DB.insert(title,unicode(craw),isdone) #特喵的操控IE就和伺候大爷一样啊 browser.quit()
def _do(self, cmd, *args): return self.canvas._do(cmd, (self.tag,) + _flatten(args))
def _do(self, cmd, *args): return self.canvas._do(cmd, (self.tag, ) + _flatten(args))
def compute_common_degree_in_network(network_type, is_directed, node_tuple_list, connection_tuple_list): node_tuple_list = filter(lambda (network_type_,\ is_directed_,\ node,\ degree_str,\ in_degree_str,\ out_degree_str): network_type_ == network_type and is_directed_ == is_directed,\ node_tuple_list\ ) edge_tuple_list = filter(lambda (network_type_,\ is_directed_,\ node1,\ node2): network_type_ == network_type and is_directed_ == is_directed,\ connection_tuple_list\ ) edge_tuple_list_length = len(edge_tuple_list) success_compute = 0 failure_compute = 0 common_degree_str_list_in_network = [] common_degree_num_list_in_network = [] common_edge_tuple_list_in_network = [] common_degree_rate_list_in_network = [] for edge_idx in xrange(len(edge_tuple_list)): edge_tuple = edge_tuple_list[edge_idx] node1 = edge_tuple[2] node2 = edge_tuple[3] if (edge_idx % 1000 == 0 and edge_idx > 998) or (edge_idx == edge_tuple_list_length-1): logging.info("============== Computer common node of {edge_idx}th edge in {network_type}.{is_directed} network ==============".format(edge_idx = edge_idx, network_type = network_type, is_directed = is_directed)) logging.info("edge_index:{idx}, finish rate:{rate}".format(idx = edge_idx, rate = float(edge_idx+1)/edge_tuple_list_length)) logging.info("success_rate:{success_rate}".format(success_rate = success_compute / float(success_compute + failure_compute + 0.0001))) logging.info("success_update:{success}, failure_update:{failure}".format(success = success_compute, failure = failure_compute)) try: node1_and_degree_str_list = filter(lambda (network_type,\ is_directed,\ node,\ degree_str,\ in_degree_str,\ out_degree_str): node == node1,\ node_tuple_list)[0] except Exception as e: failure_compute = failure_compute + 1 logging.error(e) continue if node1_and_degree_str_list[3] == "": node1_degree_list = [] else: node1_degree_list = node1_and_degree_str_list[3].split("///") try: node2_and_degree_str_list = filter(lambda (network_type,\ is_directed,\ node,\ degree_str,\ in_degree_str,\ out_degree_str): node == node2,\ node_tuple_list)[0] except Exception as e: failure_compute = failure_compute + 1 logging.error(e) continue if node2_and_degree_str_list[3] == "": node2_degree_list = [] else: node2_degree_list = node2_and_degree_str_list[3].split("///") # Merge current result node1_degree_list = _flatten([node1_degree_list]) node2_degree_list = _flatten([node2_degree_list]) if len(node2_degree_list) <= len(node1_degree_list): common_degree_list = filter(lambda node: node in node1_degree_list, node2_degree_list) else: common_degree_list = filter(lambda node: node in node2_degree_list, node1_degree_list) common_degree_str_list_in_network.append("///".join(map(str, common_degree_list))) common_degree_num_list_in_network.append(len(common_degree_list)) common_edge_tuple_list_in_network.append((node1, node2)) common_degree_rate_list_in_network.append(len(common_degree_list)/float(len(node1_degree_list)+len(node2_degree_list))) success_compute = success_compute + 1 # Merge all results degree_data_tuple_list_in_network = map(lambda edge_tuple, degree_str, degree_num, degree_rate:\ (edge_tuple, degree_str, degree_num, degree_rate),\ common_edge_tuple_list_in_network,\ common_degree_str_list_in_network,\ common_degree_num_list_in_network,\ common_degree_rate_list_in_network\ ) logging.info("len(degree_data_tuple_list_in_network):{0}".format(len(degree_data_tuple_list_in_network))) logging.info("degree_data_tuple_list_in_network[:3]:{0}".format(degree_data_tuple_list_in_network[:3])) return degree_data_tuple_list_in_network
def Generator_sheet(raw_data): cf = ConfigParser.ConfigParser() cf.readfp(codecs.open('conf.ini','r','utf-8')) #竖条宽度 width = 0.8 #加载中文字体让正常显示 font = FontProperties(fname="simsun.ttc", size=14) colors ='brcmyw' #流程完成标志 done_sign = cf.get('sheet','done_sign') project = [i[0] for i in raw_data] #将每个project字符插入换行符 n=5 project = ['\n'.join(re.findall(r'.{%d}|.{1,%d}$'%(n,max(1,len(i)%n)),i)) for i in project] name_and_times = [i[1] for i in raw_data] names = [[y[0] for y in x] for x in name_and_times] #压平names name = _flatten(names) times = [[y[1][2:].replace('-','.') for y in x] for x in name_and_times] times = _flatten(times) number = [[1]*len(i[1]) for i in raw_data] #为设置超时提醒做准备 time_out = [len(i[1]) for i in raw_data] time_out_detection = [] Initial = 0 for i in time_out: Initial+=i time_out_detection.append(Initial) #是否符合添加需求(True表示需要添加提醒) vaild = [done_sign not in _flatten(i) for i in names] #需要提醒的位置 need_alert = [vaild[x]*y for x,y in enumerate(time_out_detection)] #计算2个时间之间的差值,例如符合14.04.10离今天的时间 def distance(timev): timev = '20'+timev ymd = [int(i) for i in timev.split('.')] d_now = datetime.datetime.now() d_2 = datetime.datetime(ymd[0],ymd[1],ymd[2]) return (d_now-d_2).days def singlebar(left,height): global patch_handles bottom = 0 #初始化出现标记位置 sign = 10000 for i,j in enumerate(height): if raw_data[-1][-1][i][0] == done_sign: patch_handles.append(plt.bar(left,j,width=width,align='center',color='g',bottom=bottom)) #记住该位置 sign = i elif i>sign: patch_handles.append(plt.bar(left,j,width=width,align='center',color='g',bottom=bottom)) else: patch_handles.append(plt.bar(left,j,width=width,align='center',color=colors[i%len(colors)],bottom=bottom)) bottom+=j #画图 for x in xrange(len(number)): singlebar(x,number[x]) #添加文字 for j in xrange(len(patch_handles)): for i,patch in enumerate(patch_handles[j].get_children()): bl = patch.get_xy() x = 0.5*patch.get_width() + bl[0] y = 0.5*patch.get_height() + bl[1] if j+1 in need_alert and j>1: plt.text(x,y+0.1, "%s" % (name[j]), ha='center',fontproperties=font ) plt.text(x,y-0.2, "%s" % (times[j]), ha='center',fontproperties=font ) #添加提醒文字 plt.text(x,y+0.6, u'已过%d天'%(distance(times[j])), ha='center',fontproperties=font,color ='r' ) else: plt.text(x,y+0.1, "%s" % (name[j]), ha='center',fontproperties=font ) plt.text(x,y-0.2, "%s" % (times[j]), ha='center',fontproperties=font ) plt.xticks(range(len(number)),project,fontproperties=font) plt.title(cf.get('sheet','title'),fontproperties=font,size=30) plt.axis([-1,14,0,9]) fig = plt.gcf() fig.set_size_inches(18.5,10.5) fig.subplots_adjust(left=0.02,bottom=0.17,right=0.99,top=0.93) fig.savefig('{}.png'.format(str(datetime.date.today())),dpi=600)
def compute_common_degree_in_network(network_type, is_directed, node_tuple_list, connection_tuple_list): node_tuple_list = filter(lambda (network_type_,\ is_directed_,\ node,\ degree_str,\ in_degree_str,\ out_degree_str): network_type_ == network_type and is_directed_ == is_directed,\ node_tuple_list\ ) edge_tuple_list = filter(lambda (network_type_,\ is_directed_,\ node1,\ node2): network_type_ == network_type and is_directed_ == is_directed,\ connection_tuple_list\ ) edge_tuple_list_length = len(edge_tuple_list) success_compute = 0 failure_compute = 0 common_degree_str_list_in_network = [] common_degree_num_list_in_network = [] common_edge_tuple_list_in_network = [] common_degree_rate_list_in_network = [] for edge_idx in xrange(len(edge_tuple_list)): edge_tuple = edge_tuple_list[edge_idx] node1 = edge_tuple[2] node2 = edge_tuple[3] if (edge_idx % 1000 == 0 and edge_idx > 998) or (edge_idx == edge_tuple_list_length - 1): logging.info( "============== Computer common node of {edge_idx}th edge in {network_type}.{is_directed} network ==============" .format(edge_idx=edge_idx, network_type=network_type, is_directed=is_directed)) logging.info("edge_index:{idx}, finish rate:{rate}".format( idx=edge_idx, rate=float(edge_idx + 1) / edge_tuple_list_length)) logging.info("success_rate:{success_rate}".format( success_rate=success_compute / float(success_compute + failure_compute + 0.0001))) logging.info( "success_update:{success}, failure_update:{failure}". format(success=success_compute, failure=failure_compute)) try: node1_and_degree_str_list = filter(lambda (network_type,\ is_directed,\ node,\ degree_str,\ in_degree_str,\ out_degree_str): node == node1,\ node_tuple_list)[0] except Exception as e: failure_compute = failure_compute + 1 logging.error(e) continue if node1_and_degree_str_list[3] == "": node1_degree_list = [] else: node1_degree_list = node1_and_degree_str_list[3].split( "///") try: node2_and_degree_str_list = filter(lambda (network_type,\ is_directed,\ node,\ degree_str,\ in_degree_str,\ out_degree_str): node == node2,\ node_tuple_list)[0] except Exception as e: failure_compute = failure_compute + 1 logging.error(e) continue if node2_and_degree_str_list[3] == "": node2_degree_list = [] else: node2_degree_list = node2_and_degree_str_list[3].split( "///") # Merge current result node1_degree_list = _flatten([node1_degree_list]) node2_degree_list = _flatten([node2_degree_list]) if len(node2_degree_list) <= len(node1_degree_list): common_degree_list = filter( lambda node: node in node1_degree_list, node2_degree_list) else: common_degree_list = filter( lambda node: node in node2_degree_list, node1_degree_list) common_degree_str_list_in_network.append("///".join( map(str, common_degree_list))) common_degree_num_list_in_network.append( len(common_degree_list)) common_edge_tuple_list_in_network.append((node1, node2)) common_degree_rate_list_in_network.append( len(common_degree_list) / float(len(node1_degree_list) + len(node2_degree_list))) success_compute = success_compute + 1 # Merge all results degree_data_tuple_list_in_network = map(lambda edge_tuple, degree_str, degree_num, degree_rate:\ (edge_tuple, degree_str, degree_num, degree_rate),\ common_edge_tuple_list_in_network,\ common_degree_str_list_in_network,\ common_degree_num_list_in_network,\ common_degree_rate_list_in_network\ ) logging.info("len(degree_data_tuple_list_in_network):{0}".format( len(degree_data_tuple_list_in_network))) logging.info("degree_data_tuple_list_in_network[:3]:{0}".format( degree_data_tuple_list_in_network[:3])) return degree_data_tuple_list_in_network
# This module exports classes for the various canvas item types
def Generator_sheet(raw_data): cf = ConfigParser.ConfigParser() cf.readfp(codecs.open('conf.ini', 'r', 'utf-8')) #竖条宽度 width = 0.8 #加载中文字体让正常显示 font = FontProperties(fname="simsun.ttc", size=14) colors = 'brcmyw' #流程完成标志 done_sign = cf.get('sheet', 'done_sign') project = [i[0] for i in raw_data] #将每个project字符插入换行符 n = 5 project = [ '\n'.join(re.findall(r'.{%d}|.{1,%d}$' % (n, max(1, len(i) % n)), i)) for i in project ] name_and_times = [i[1] for i in raw_data] names = [[y[0] for y in x] for x in name_and_times] #压平names name = _flatten(names) times = [[y[1][2:].replace('-', '.') for y in x] for x in name_and_times] times = _flatten(times) number = [[1] * len(i[1]) for i in raw_data] #为设置超时提醒做准备 time_out = [len(i[1]) for i in raw_data] time_out_detection = [] Initial = 0 for i in time_out: Initial += i time_out_detection.append(Initial) #是否符合添加需求(True表示需要添加提醒) vaild = [done_sign not in _flatten(i) for i in names] #需要提醒的位置 need_alert = [vaild[x] * y for x, y in enumerate(time_out_detection)] #计算2个时间之间的差值,例如符合14.04.10离今天的时间 def distance(timev): timev = '20' + timev ymd = [int(i) for i in timev.split('.')] d_now = datetime.datetime.now() d_2 = datetime.datetime(ymd[0], ymd[1], ymd[2]) return (d_now - d_2).days def singlebar(left, height): global patch_handles bottom = 0 #初始化出现标记位置 sign = 10000 for i, j in enumerate(height): if raw_data[-1][-1][i][0] == done_sign: patch_handles.append( plt.bar(left, j, width=width, align='center', color='g', bottom=bottom)) #记住该位置 sign = i elif i > sign: patch_handles.append( plt.bar(left, j, width=width, align='center', color='g', bottom=bottom)) else: patch_handles.append( plt.bar(left, j, width=width, align='center', color=colors[i % len(colors)], bottom=bottom)) bottom += j #画图 for x in xrange(len(number)): singlebar(x, number[x]) #添加文字 for j in xrange(len(patch_handles)): for i, patch in enumerate(patch_handles[j].get_children()): bl = patch.get_xy() x = 0.5 * patch.get_width() + bl[0] y = 0.5 * patch.get_height() + bl[1] if j + 1 in need_alert and j > 1: plt.text(x, y + 0.1, "%s" % (name[j]), ha='center', fontproperties=font) plt.text(x, y - 0.2, "%s" % (times[j]), ha='center', fontproperties=font) #添加提醒文字 plt.text(x, y + 0.6, u'已过%d天' % (distance(times[j])), ha='center', fontproperties=font, color='r') else: plt.text(x, y + 0.1, "%s" % (name[j]), ha='center', fontproperties=font) plt.text(x, y - 0.2, "%s" % (times[j]), ha='center', fontproperties=font) plt.xticks(range(len(number)), project, fontproperties=font) plt.title(cf.get('sheet', 'title'), fontproperties=font, size=30) plt.axis([-1, 14, 0, 9]) fig = plt.gcf() fig.set_size_inches(18.5, 10.5) fig.subplots_adjust(left=0.02, bottom=0.17, right=0.99, top=0.93) fig.savefig('{}.png'.format(str(datetime.date.today())), dpi=600)
def _format_mapdict(mapdict, script=False): opts = [] for opt, value in mapdict.iteritems(): opts.extend(('-%s' % opt, _format_optvalue(_mapdict_values(value), script))) return _flatten(opts)