首页 文章 精选 留言 我的

精选列表

搜索[类],共10004篇文章
优秀的个人博客,低调大师

Python的类方法

1 down vote I used to work with PHP and recently I was asking myself, whats going on with this classmethod? Python manual is very technical and very short in words so it wont help with understanding that feature. I was googling and googling and I found answer ->http://code.anjanesh.net/2007/12/python-classmethods.html. If you are lazy to click it. My explanation is shorter and below. :) in PHP (maybe not all of you know PHP, but this language is so straight forward that everybody should understand what I'm talking about) we have static variables like this: class A{ static protected $inner_var = null; static public function echoInnerVar() { echo self::$inner_var."\n"; } static public function setInnerVar($v) { self::$inner_var = $v; }}class B extends A{} A::setInnerVar(10); B::setInnerVar(20); A::echoInnerVar(); B::echoInnerVar(); The output will be in both cases 20. However in python we can add @classmethod decorator and thus it is possible to have output 10 and 20 respectively. Example: class A(object): inner_var = 0 @classmethod def setInnerVar(cls, value): cls.inner_var = value @classmethod def echoInnerVar(cls): print cls.inner_varclass B(A): pass A.setInnerVar(10) B.setInnerVar(20) A.echoInnerVar() B.echoInnerVar() Smart, ain't? 欢迎加群互相学习,共同进步。QQ群:iOS: 58099570 | Android: 572064792 | Nodejs:329118122 做人要厚道,转载请注明出处! 本文转自张昺华-sky博客园博客,原文链接:http://www.cnblogs.com/sunshine-anycall/archive/2011/07/04/2097646.html ,如需转载请自行联系原作者

优秀的个人博客,低调大师

HDFS中PathFilter类

在单个操作中处理一批文件,这是很常见的需求。比如说处理日志的MapReduce作业可能需要分析一个月内包含在大量目录中的日志文件。在一 个表达式中使用通配符在匹配多个文件时比较方便的,无需列举每个文件和目录来指定输入。hadoop为执行通配提供了两个FIleSystem方法: 1 public FileStatus[] globStatus(Path pathPattern) throw IOException 2 public FileStatus[] globStatus(Path pathPattern, PathFilter filter) throw IOException globStatus()方法返回与路径想匹配的所有文件的FileStatus对象数组,并按路径排序。hadoop所支持的通配符与Unix bash相同。 第二个方法传了一个PathFilter对象作为参数,PathFilter可以进一步对匹配进行限制。PathFilter是一个接口,里面只有一个方法accept(Path path)。 下面看一个例子演示PathFilter的作用: RegexExcludePathFilter.java:该类实现了PathFilter接口,重写了accept方法 1 class RegexExcludePathFilter implements PathFilter{ 2 private final String regex; 3 public RegexExcludePathFilter(String regex) { 4 this.regex = regex; 5 } 6 @Override 7 public boolean accept(Path path) { 8 return !path.toString().matches(regex); 9 } 10 11 } 该方法就是打印符合通配的路径: 1 //通配符的使用 2 public static void list() throws IOException{ 3 Configuration conf = new Configuration(); 4 FileSystem fs = FileSystem.get(conf); 5 //PathFilter是过滤布符合置顶表达式的路径,下列就是把以txt结尾的过滤掉 6 FileStatus[] status = fs.globStatus(new Path(“hdfs://master:9000/user/hadoop/test/“),new RegexExcludePathFilter(“.txt”)); 7 //FileStatus[] status = fs.globStatus(new Path(“hdfs://master:9000/user/hadoop/test/*”)); 8 Path[] listedPaths = FileUtil.stat2Paths(status); 9 for (Path p : listedPaths) { 10 System.out.println(p); 11 } 12 } 如果注释第6行,取消第7行的注释,则输出结果如下: hdfs://master:9000/user/hadoop/test/a.txt hdfs://master:9000/user/hadoop/test/b.txt hdfs://master:9000/user/hadoop/test/c.aaa hdfs://master:9000/user/hadoop/test/c.txt hdfs://master:9000/user/hadoop/test/cc.aaa 如果注释第7行,取消第6行的注释,则输出结果如下: hdfs://master:9000/user/hadoop/test/c.aaa hdfs://master:9000/user/hadoop/test/cc.aaa 由此可见,PathFilter就是在匹配前面条件之后再加以限制,将匹配PathFilter的路径去除掉。其实由accept方法里面的 return !path.toString().matches(regex);可以看出来,就是将匹配的全部去除掉,如果改为return path.toString().matches(regex);就是将匹配regex的Path输出,将不匹配的去除。

资源下载

更多资源
Mario

Mario

马里奥是站在游戏界顶峰的超人气多面角色。马里奥靠吃蘑菇成长,特征是大鼻子、头戴帽子、身穿背带裤,还留着胡子。与他的双胞胎兄弟路易基一起,长年担任任天堂的招牌角色。

腾讯云软件源

腾讯云软件源

为解决软件依赖安装时官方源访问速度慢的问题,腾讯云为一些软件搭建了缓存服务。您可以通过使用腾讯云软件源站来提升依赖包的安装速度。为了方便用户自由搭建服务架构,目前腾讯云软件源站支持公网访问和内网访问。

Rocky Linux

Rocky Linux

Rocky Linux(中文名:洛基)是由Gregory Kurtzer于2020年12月发起的企业级Linux发行版,作为CentOS稳定版停止维护后与RHEL(Red Hat Enterprise Linux)完全兼容的开源替代方案,由社区拥有并管理,支持x86_64、aarch64等架构。其通过重新编译RHEL源代码提供长期稳定性,采用模块化包装和SELinux安全架构,默认包含GNOME桌面环境及XFS文件系统,支持十年生命周期更新。

Sublime Text

Sublime Text

Sublime Text具有漂亮的用户界面和强大的功能,例如代码缩略图,Python的插件,代码段等。还可自定义键绑定,菜单和工具栏。Sublime Text 的主要功能包括:拼写检查,书签,完整的 Python API , Goto 功能,即时项目切换,多选择,多窗口等等。Sublime Text 是一个跨平台的编辑器,同时支持Windows、Linux、Mac OS X等操作系统。

用户登录
用户注册