GVKun编程网logo

Ruby相当于Python的os.walk是什么?(ruby和python性能比较)

24

在这里,我们将给大家分享关于Ruby相当于Python的os.walk是什么?的知识,让您更了解ruby和python性能比较的本质,同时也会涉及到如何更有效地bash相当于Python的os.pat

在这里,我们将给大家分享关于Ruby相当于Python的os.walk是什么?的知识,让您更了解ruby和python性能比较的本质,同时也会涉及到如何更有效地bash相当于Python的os.path.normpath?、Groovy相当于Ruby的Object#tap、Javascript相当于python的.format()、os.path相当于python中的网址?的内容。

本文目录一览:

Ruby相当于Python的os.walk是什么?(ruby和python性能比较)

Ruby相当于Python的os.walk是什么?(ruby和python性能比较)

有人知道Ruby内部是否有一个遍历文件系统目录和文件的模块/函数吗?我正在寻找类似于Python的东西os.walk。我找到的最接近的模块是,Find但需要一些额外的工作才能进行遍历。

Python代码如下所示:

for root,dirs,files in os.walk('.'):
    for name in files:
        print name
    for name in dirs:
        print name

bash相当于Python的os.path.normpath?

bash相当于Python的os.path.normpath?

什么是相当于 os.path.normpath的bash?具体来说,我有兴趣删除执行find时给出的前导./.

matt@stanley:~/src/libtelnet-0.20/test$find
.
./Makefile
./Makefile.in
./Makefile.am
...

解决方法

那么,为此,你可以简单地通过sed管道输出,你不必规范化整个路径:

your_command_goes_here | sed 's?^\./??'

这将在一行开头摆脱所有./序列.

以下成绩单显示了这一点:

pax$find -name 'qq*sh'
./qq.ksh
./qq.sh
./qq.zsh
./qq2.sh
./qq2.zsh
./qqq/qq.sh

pax$find -name 'qq*sh' | sed 's?^./??'
qq.ksh
qq.sh
qq.zsh
qq2.sh
qq2.zsh
qqq/qq.sh

正如您所看到的,我对我的临时shell脚本有一个相当直观的命名标准:-)

Groovy相当于Ruby的Object#tap

Groovy相当于Ruby的Object#tap

在Groovy中是否有任何等价的 Ruby的 Object#tap方法将对象传递给一个闭包,在该闭包中对象变为self然后返回对象?我知道DefaultGroovyMethods.with,但这需要您显式返回对象,以便能够链接或分配它.如果没有,有没有什么方法可以自己实现它并让它可用于所有对象,如DefaultGroovyMethods中的其他方法?使用DefaultGroovyMethods.with的实现很容易并且总是返回对象而不是闭包的返回值,但它是否可以用于所有对象?根据 this post,没有办法扩展DefaultGroovyMethods,但有没有其他方法可以做到这一点?

解决方法

在groovy中我没有类似的方法,但你应该能够做到:

Object.MetaClass.tap = { Closure c ->
    delegate.with c
    delegate
}

(1..10)                         .tap { println "original ${it}" }
       .findAll { it % 2 == 0 } .tap { println "evens    ${it}" }
       .collect { it * it }     .tap { println "squares  ${it}" }

打印:

original [1,2,3,4,5,6,7,8,9,10]
evens    [2,10]
squares  [4,16,36,64,100]

Javascript相当于python的.format()

Javascript相当于python的.format()

我想要一个模仿 python .format()函数的 javascript函数
.format(*args,**kwargs)

前一个问题为’.format(* args)提供了一个可能的(但不是完整的)解决方案

JavaScript equivalent to printf/string.format

我希望能够做到

"hello {} and {}".format("you","bob"
==> hello you and bob

"hello {0} and {1}".format("you","bob")
==> hello you and bob

"hello {0} and {1} and {a}".format("you","bob",a="mary")
==> hello you and bob and mary

"hello {0} and {1} and {a} and {2}".format("you","jill",a="mary")
==> hello you and bob and mary and jill

我意识到这是一个很高的订单,但也许某个地方有一个包含关键字参数的完整(或至少部分)解决方案.

哦,我听说AJAX和JQuery可能有这方面的方法,但我希望能够在没有这些开销的情况下完成它.

特别是,我希望能够将其与google doc的脚本一起使用.

谢谢

解决方法

更新:如果您使用的是ES6,则模板字符串的工作方式与String.format: https://developers.google.com/web/updates/2015/01/ES6-Template-Strings非常相似

如果没有,下面的代码适用于上面的所有情况,其语法与python的String.format方法非常相似.以下测试用例.

String.prototype.format = function() {
  var args = arguments;
  this.unkeyed_index = 0;
  return this.replace(/\{(\w*)\}/g,function(match,key) { 
    if (key === '') {
      key = this.unkeyed_index;
      this.unkeyed_index++
    }
    if (key == +key) {
      return args[key] !== 'undefined'
      ? args[key]
      : match;
    } else {
      for (var i = 0; i < args.length; i++) {
        if (typeof args[i] === 'object' && typeof args[i][key] !== 'undefined') {
          return args[i][key];
        }
      }
      return match;
    }
  }.bind(this));
};

// Run some tests
$('#tests')
  .append(
    "hello {} and {}<br />".format("you","bob")
  )
  .append(
    "hello {0} and {1}<br />".format("you","bob")
  )
  .append(
    "hello {0} and {1} and {a}<br />".format("you",{a:"mary"})
  )
  .append(
    "hello {0} and {1} and {a} and {2}<br />".format("you",{a:"mary"})
  );
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div id="tests"></div>

os.path相当于python中的网址?

os.path相当于python中的网址?

对于加入,分割和处理文件和目录路径,python的os.path模块很棒.网站网址是否相当?

解决方法

urlparse – 将URL解析成组件,是您的模块

实际抓取网址和数据,你需要urllib2.

关于Ruby相当于Python的os.walk是什么?ruby和python性能比较的问题我们已经讲解完毕,感谢您的阅读,如果还想了解更多关于bash相当于Python的os.path.normpath?、Groovy相当于Ruby的Object#tap、Javascript相当于python的.format()、os.path相当于python中的网址?等相关内容,可以在本站寻找。

本文标签: