Demo of Python "Map Reduce Filter"
Here I share with you a demo for python map, reduce and filter functional programming thatowned by me(Xiaoqiang).
I assume there are two DB tables, that `file_logs` and `expanded_attrs` which records more columns to expand table `file_logs`. For demonstration, we assume that there are more than one file logs for a same tuple of (platform_id, client_id). We need to feture
 out which is the one lasted updated for (platform_id=1, client_id=1) tuple.
Here is the thoughts:
1. Filter out all file logs for tuple (platform_id=1, client_id=1) from original file logs,
2. Merge expand table attributes into file_logs table in memory, like union selection.
3. Reduce the full version of file_logs for figuring out which is latest updated.
Demo codes shows here (use Python 2.6+, 2.7+):
BTW, you are welcome if you feature out a more effective way of working or any issues you found. Thanks. :)
#!/usr/bin/env python """
Requirement:
known platform_id=1, client_id=1 as pid and cid.
exists file_logs and expanded_attrs which are array of objects, expanded_attrs is a table of columns expand table file_logs
as file_logs contains more than one for pid=1,cid=1, we need to find out which is the one latest updated.
""" file_logs = [
{ 'file_log_id': '1', 'platform_id': '1', 'client_id': '1', 'file': 'path/to/platform/client/j-1/stdout' },
{ 'file_log_id': '2', 'platform_id': '1', 'client_id': '1', 'file': 'path/to/platform/client/j-2/stdout' },
{ 'file_log_id': '3', 'platform_id': '2', 'client_id': '3', 'file': 'path/to/platform/client/j-3/stdout' },
] expanded_attrs = [
{ 'file_log_id': '1', 'attr_name': 'CLICK', 'attr_value': '100' },
{ 'file_log_id': '1', 'attr_name': 'SUPPRESSION', 'attr_value': '100' },
{ 'file_log_id': '1', 'attr_name': 'last_updated', 'attr_value': '2014-07-14' },
{ 'file_log_id': '2', 'attr_name': 'CLICK', 'attr_value': '200' },
{ 'file_log_id': '2', 'attr_name': 'SUPPRESSION', 'attr_value': '200' },
{ 'file_log_id': '2', 'attr_name': 'last_updated', 'attr_value': '2014-07-15' },
{ 'file_log_id': '3', 'attr_name': 'CLICK', 'attr_value': '300' },
{ 'file_log_id': '3', 'attr_name': 'SUPPRESSION', 'attr_value': '300' },
{ 'file_log_id': '3', 'attr_name': 'last_updated', 'attr_value': '2014-07-15' },
] platform_id = '1'
client_id = '1' target_scope_filelogs = filter(lambda x: x['platform_id'] == platform_id and x['client_id'] == client_id, file_logs) map(
lambda x:
x.update(reduce(
lambda xx, xy: xx.update({ xy['attr_name']: xy['attr_value'] }) is None and xx,
filter(lambda xx: xx['file_log_id'] == x['file_log_id'], expanded_attrs),
dict()
)),
target_scope_filelogs
) print reduce(lambda x, y: x['last_updated'] > y['last_updated'] and x or y, target_scope_filelogs)
#> {'file_log_id': '2', 'platform_id': '1', 'last_updated': '2014-07-15', 'SUPPRESSION': '200', 'file': 'path/to/platform/client/j-2/stdout', 'client_id': '1', 'CLICK': '200'}
版权声明:本文博客原创文章,博客,未经同意,不得转载。
Demo of Python "Map Reduce Filter"的更多相关文章
- Python基础-map/reduce/filter
		一.map Python内置函数,用法及说明如下: class map(object): """ map(func, *iterables) --> map obj ... 
- Python: lambda, map, reduce, filter
		在学习python的过程中,lambda的语法时常会使人感到困惑,lambda是什么,为什么要使用lambda,是不是必须使用lambda? 下面就上面的问题进行一下解答. 1.lambda是什么? ... 
- python基础===map, reduce, filter的用法
		filter的用法: 这还是一个操作表list的内嵌函数'filter' 需要一个函数与一个list它用这个函数来决定哪个项应该被放入过滤结果队列中遍历list中的每一个值,输入到这个函数中如果这个函 ... 
- python之map、filter、reduce、lambda函数  转
		python之map.filter.reduce.lambda函数 转 http://www.cnblogs.com/kaituorensheng/p/5300340.html 阅读目录 map ... 
- [python基础知识]python内置函数map/reduce/filter
		python内置函数map/reduce/filter 这三个函数用的顺手了,很cool. filter()函数:filter函数相当于过滤,调用一个bool_func(只返回bool类型数据的方法) ... 
- Python学习:函数式编程(lambda, map() ,reduce() ,filter())
		1. lambda: Python 支持用lambda对简单的功能定义“行内函数” 2.map() : 3.reduce() : 4.filter() : map() ,reduce() , filt ... 
- python 函数式编程之lambda( ), map( ), reduce( ), filter( )
		lambda( ), map( ), reduce( ), filter( ) 1. lambda( )主要用于“行内函数”: f = lambda x : x + 2 #定义函数f(x)=x+2 g ... 
- Python map/reduce/filter/sorted函数以及匿名函数
		1. map() 函数的功能: map(f, [x1,x2,x3]) = [f(x1), f(x2), f(x3)] def f(x): return x*x a = map(f, [1, 2, 3, ... 
- python--函数式编程 (高阶函数(map , reduce ,filter,sorted),匿名函数(lambda))
		1.1函数式编程 面向过程编程:我们通过把大段代码拆成函数,通过一层一层的函数,可以把复杂的任务分解成简单的任务,这种一步一步的分解可以称之为面向过程的程序设计.函数就是面向过程的程序设计的基本单元. ... 
随机推荐
- windows服务的创建、安装和调试
			1.创建 windows服务 项目 文件 -> 新建项目 -> 已安装的模板 -> Visual C# -> windows ,在右侧窗口选择"windows 服 ... 
- linux LNMP自动安装脚本
			#!/bin/bashsoft_dir="/home/soft"config_dir="/home/config"httpd="httpd-2.0.5 ... 
- ZooKeeper场景实践:(2)集中式配置管理
			1. 基本介绍 在分布式的环境中,可能会有多个对等的程序读取相同的配置文件,程序能够部署在多台机器上,假设配置採用文件的话,则须要为部署该程序的机器也部署一个配置文件,一旦要改动配置的时候就会很麻烦, ... 
- Linux内核——进程管理与调度
			进程的管理与调度 进程管理 进程描写叙述符及任务结构 进程存放在叫做任务队列(tasklist)的双向循环链表中.链表中的每一项包括一个详细进程的全部信息,类型为task_struct,称为进程描写叙 ... 
- Navicat工具破解
			Navicat提供多达 7 种语言供客户选择,被公认为全球最受欢迎的数据库前端用户介面工具.它可以用来对本机或远程的 MySQL.SQL Server.SQLite.Oracle 及 Post ... 
- WPF遮蔽层的实现
			在一些项目中,难免会有耗时的加载,如果加载时没有提示,给人一种假死的感觉,很不友好,那么现在福利来啦,WPF版的模态窗体,先上效果图 实际效果指针是转动的,话不多说,一大批干货来袭 XMAL的代码 W ... 
- 【ArcGIS 10.2新特性】ArcGIS 10.2 for Desktop 新特性(二)
			4 三维 4.1 共享三维场景 用户能够将ArcScene文档导出为3D web场景,能够被加载到ArcGIS Online.Portal或本地Web服务器上并进行分享.这样,用户可以 ... 
- 树形dp专辑
			hdu 2196 http://acm.hdu.edu.cn/showproblem.php?pid=2196 input 5//5个结点 1 1//表示结点2到结点1有一条权值为1的边 2 1//表 ... 
- 初识google多语言通信框架gRPC系列(二)编译gRPC
			目录 一.概述 二.编译gRPC 三.C#中使用gRPC 四.C++中使用gRPC 无论通过哪种语言调用gRPC,都必须要编译gRPC,因为生成proto访问类时,除了产生标准的数据定义类之外,还需要 ... 
- Red Gate系列之三 SQL Server 开发利器 SQL Prompt 5.3.4.1 Edition T-SQL智能感知分析器 完全破解+使用教程
			原文:Red Gate系列之三 SQL Server 开发利器 SQL Prompt 5.3.4.1 Edition T-SQL智能感知分析器 完全破解+使用教程 Red Gate系列之三 SQL S ... 
