def groupby_iterator( dict_iterable: Iterable[Dict], key: Hashable, default_value: Any = None) -> Iterator[Tuple[Hashable, Iterable]]: """ Group an iterable of dicts by a dict attribute :param dict_iterable: An iterable of dicts :param key: A attribute that the dicts contains :param default_value: The default value of the attribute if the dict does not contain it :return: Iterator for a tuple(key, iterable) where the key is the group common value and the iterable iterates over the dicts that had this value """ return itertools.groupby(sorted_dicts(dict_iterable, key), lambda x: x.get(key, default_value))
def broadcast_order_data(self, new_order): notifications = [] vals = {} pos_order = self.search([('lines.state', 'not in', ['cancel', 'done']), ('amount_total', '>', 0.00)]) manager_id = self.env['res.users'].search( [('kitchen_screen_user', '=', 'manager')], limit=1) screen_table_data = [] for order in pos_order: order_line_list = [] for line in order.lines: order_line = { 'id': line.id, 'name': line.product_id.display_name, 'qty': line.qty, 'table': line.order_id.table_id.name, 'floor': line.order_id.table_id.floor_id.name, 'time': self.get_session_date(line), 'state': line.state, 'note': line.order_line_note, 'categ_id': line.product_id.product_tmpl_id.pos_categ_id.id, 'order': line.order_id.id, 'pos_cid': line.pos_cid, 'user': line.create_uid.id, 'route_id': line.product_id.product_tmpl_id.route_ids.active, } order_line_list.append(order_line) order_dict = { 'order_id': order.id, 'order_name': order.name, 'order_time': self.get_order_date(order), 'table': order.table_id.name, 'floor': order.table_id.floor_id.name, 'customer': order.partner_id.name, 'order_lines': order_line_list, 'total': order.amount_total, 'note': order.note, 'user_id': order.user_id.id, } screen_table_data.append(order_dict) kitchen_group_data = {} sort_group = sorted(screen_table_data, key=itemgetter('user_id')) for key, value in itertools.groupby(sort_group, key=itemgetter('user_id')): if key not in kitchen_group_data: kitchen_group_data.update({key: [x for x in value]}) else: kitchen_group_data[key] = [x for x in value] if kitchen_group_data: for user_id in kitchen_group_data: user = self.env['res.users'].browse(user_id) if user and user.cook_user_ids: for cook_user_id in user.cook_user_ids: if len(vals) > 0: d1 = kitchen_group_data[user_id] for each_order in d1: vals['orders'].append(each_order) else: vals = { "orders": kitchen_group_data[user_id], } if new_order: vals['new_order'] = new_order notifications.append( ((self._cr.dbname, 'pos.order.line', cook_user_id.id), { 'screen_display_data': vals })) if user and user.kitchen_screen_user != 'cook': notifications.append( ((self._cr.dbname, 'pos.order.line', manager_id.id), { 'screen_display_data': vals })) else: notifications.append( ((self._cr.dbname, 'pos.order.line', manager_id.id), { 'screen_display_data': vals })) cook_user_ids = self.env['res.users'].search([ ('kitchen_screen_user', '=', 'cook') ]) if cook_user_ids: for each_cook_id in cook_user_ids: notifications.append(((self._cr.dbname, 'pos.order.line', each_cook_id.id), { 'screen_display_data': vals })) if notifications: self.env['bus.bus'].sendmany(notifications) return True
def compress(data): return ((len(list(group)), name) for name, group in itertools.groupby(data))
def compress_ex(data): for name, group in itertools.groupby(data): print name, list(group)
def groupAnagrams(self, strs): return [sorted(g) for _, g in itertools.groupby(sorted(strs, key=sorted), sorted)]
def groupAnagrams(self, strs): groups=itertools.groupby(sorted(strs, key=sorted), sorted) return [sorted(members) for _, members in groups]
def broadcast_order_data(self, new_order): print('in prodcast') print('self', self) print('new_order', new_order) notifications = [] vals = {} pos_order = self.search([('lines.state', 'not in', ['cancel', 'done']), ('amount_total', '>', 0.00)]) print('pos_order', pos_order) manager_id = self.env['res.users'].search( [('kitchen_screen_user', '=', 'manager')], limit=1) xx_cookers_id = self.env['res.users'].search( [('kitchen_screen_user', '=', 'cook')], limit=1) screen_table_data = [] for order in pos_order: print('order', order) order_line_list = [] for line in order.lines: print('line', line) order_line = { 'id': line.id, 'name': line.product_id.display_name, 'qty': line.qty, 'table': line.order_id.table_id.name, 'floor': line.order_id.table_id.floor_id.name, 'time': self.get_session_date(line), 'state': line.state, 'note': line.order_line_note, 'categ_id': line.product_id.product_tmpl_id.pos_categ_id.id, 'order': line.order_id.id, 'pos_cid': line.pos_cid, 'user': line.create_uid.id, 'route_id': line.product_id.product_tmpl_id.route_ids.active, } order_line_list.append(order_line) print('order_line_list', order_line_list) order_dict = { 'order_id': order.id, 'order_name': order.name, 'order_time': self.get_order_date(order), 'table': order.table_id.name, 'floor': order.table_id.floor_id.name, 'customer': order.partner_id.name, 'order_lines': order_line_list, 'total': order.amount_total, 'note': order.note, 'user_id': order.user_id.id, } print('order_dict', order_dict) screen_table_data.append(order_dict) kitchen_group_data = {} #print('kitchen_group_data',kitchen_group_data) sort_group = sorted(screen_table_data, key=itemgetter('user_id')) for key, value in itertools.groupby(sort_group, key=itemgetter('user_id')): if key not in kitchen_group_data: print('not in ') kitchen_group_data.update({key: [x for x in value]}) else: print('in') kitchen_group_data[key] = [x for x in value] if kitchen_group_data: print('kitchen_group_data', kitchen_group_data) for user_id in kitchen_group_data: print('in kitchen_group_data for ', user_id) user = self.env['res.users'].browse(user_id) if user and user.cook_user_ids: print('user.cook_user_ids ', user.cook_user_ids) print('xx_cookers_id ', xx_cookers_id) for cook_user_id in xx_cookers_id: print('len(vals)', len(vals)) if len(vals) > 0: d1 = kitchen_group_data[user_id] for each_order in d1: print('vals[orders]', vals['orders']) print('d1[0]', d1[0]) print('d1[0]', d1[0]['order_id']) # updated by Falak / Shireen for order in vals['orders']: print(order) print('d1[0]', d1[0]['order_id']) if (d1[0]['order_id'] == order['order_id'] ): print('exits ') else: print('Not Exists ') vals['orders'].append(d1[0]) # print('vals[orders]',vals['orders']) # # print('vals[orders]',vals['orders'][0]['order_id']) else: vals = { "orders": kitchen_group_data[user_id], } print('vals', vals) if new_order: print('in new orders') vals['new_order'] = new_order notifications.append( ((self._cr.dbname, 'pos.order.line', 999), { 'screen_display_data': vals }) ) # cook_user_id.id / updated by Falak/Ahmad 20190718 # notifications.append( # ((self._cr.dbname, 'pos.order.line', 999), {'screen_display_data': vals})) # manager_id.id / updated by Falak/Ahmad 20190718 # if user and user.kitchen_screen_user != 'cook': # notifications.append( # ((self._cr.dbname, 'pos.order.line', 999 ), {'screen_display_data': vals})) # manager_id.id / updated by Falak/Ahmad 20190718 if user and user.kitchen_screen_user != 'cook': notifications.append( ((self._cr.dbname, 'pos.order.line', 999), { 'screen_display_data': vals })) #/ updated by Falak else: print('In Else', new_order) # if new_order: # pos_order = self.env['pos.order'].search([('id', '=', new_order)]) # # print(pos_order) # notifications.append( # ((self._cr.dbname, 'pos.order.line', 999), {'screen_display_data': pos_order})) #/ updated by Falak # cook_user_ids = self.env['res.users'].search([ ('kitchen_screen_user', '=', 'cook') ]) if cook_user_ids: for each_cook_id in cook_user_ids: notifications.append( ((self._cr.dbname, 'pos.order.line', 999), { 'screen_display_data': vals })) #/ updated by Falak if notifications: print('in broad notifications', notifications) self.env['bus.bus'].sendmany(notifications) return True
return merge(left, right) if __name__ == "__main__": START_DIRECTORY = "/Users/gimjiwon/Desktop/dev-3/Algo/homework/brown/" All = [] merged_file = open('brown_dic.txt', 'w') for root, dirs, files in os.walk(START_DIRECTORY): full_files = (os.path.join(root, f) for f in files) for f in full_files: if f.find('.pos') > 0: fList = process_file(f) All = All + fList #All=[1,6,7,9,8,4,5,3,2] #start_idx = 0 #end_idx = len(All)-1 dest = merge_sort(All) #print(dest) #https://python.flowdas.com/library/itertools.html for word, k in itertools.groupby(dest): #print(word,len(list(k))) freq_num = len(list(k)) merged_file.writelines(word + "\t" + str(freq_num) + '\n')
def routing_debug_waypoints(*waypoint_data, _connection=None): obj = get_optional_target(None, _connection=_connection) if obj is None: return False routing_component = obj.get_component(ROUTING_COMPONENT) if routing_component is None: return False object_manager = services.object_manager() waypoints = [] for (is_float, data_points) in itertools.groupby(waypoint_data, lambda d: '.' in d): while True: try: if is_float: position = Vector3(float(next(data_points)), float(next(data_points)), float(next(data_points))) routing_surface = routing.SurfaceIdentifier( services.current_zone_id(), 0, routing.SurfaceType.SURFACETYPE_WORLD) location = routing.Location( position, routing_surface=routing_surface) else: o = object_manager.get(int(next(data_points))) if o is None: continue routing_surface = o.provided_routing_surface if routing_surface is None: continue location = routing.Location( o.position, routing_surface=routing_surface) waypoints.append((routing.Goal(location), )) except StopIteration: break def _do_route_gen(timeline): routing_context = obj.get_routing_context() route = routing.Route(obj.routing_location, waypoints[-1], waypoints=waypoints[:-1], routing_context=routing_context) plan_primitive = PlanRoute(route, obj) result = yield from element_utils.run_child(timeline, plan_primitive) if not result: return result yield nodes = plan_primitive.path.nodes if not (nodes and nodes.plan_success): return False yield else: follow_path_element = FollowPath(obj, plan_primitive.path) result = yield from element_utils.run_child( timeline, follow_path_element) if not result: return result yield return True yield timeline = services.time_service().sim_timeline timeline.schedule(GeneratorElement(_do_route_gen)) return True