我正在尝试做一些可能非常简单的事情,我有一个目录结构,例如:
dir/
subdir1/
subdir2/
file1
file2
subsubdir1/
file3
Run Code Online (Sandbox Code Playgroud)
我想在bash脚本中运行一个命令,它将从dir中递归删除所有文件,但保留所有目录.即:
dir/
subdir1/
subdir2/
subsubdir1
Run Code Online (Sandbox Code Playgroud)
什么是合适的命令?
在调查Ruby时,我遇到了这个问题来创建一个类似Struct的类:
Person = Struct.new(:forname, :surname)
person1 = Person.new('John', 'Doe')
puts person1 #<struct Person forname="John", surname="Doe">
Run Code Online (Sandbox Code Playgroud)
这为我提出了一些Python问题.我在Python中编写了一个[非常]基本的克隆机制:
def Struct(*args):
class NewStruct:
def __init__(self):
for arg in args:
self.__dict__[arg] = None
return NewStruct
>>> Person = Struct('forename', 'surname')
>>> person1 = Person()
>>> person2 = Person()
>>> person1.forename, person1.surname = 'John','Doe'
>>> person2.forename, person2.surname = 'Foo','Bar'
>>> person1.forename
'John'
>>> person2.forename
'Foo'
Run Code Online (Sandbox Code Playgroud)
Python中是否已有类似的机制来处理这个问题?(我通常只使用字典).
我如何获得Struct()创建正确__init__()参数的函数.(在这种情况下,我想尽可能执行person1 = Person('John', 'Doe')命名参数:person1 = Person(surname='Doe', forename='John')
我想,感兴趣的是,即使有更好的Python机制来解决问题2也是如此.
我有以下代码用于在Python中映射嵌套列表以生成具有相同结构的列表.
>>> nested_list = [['Hello', 'World'], ['Goodbye', 'World']]
>>> [map(str.upper, x) for x in nested_list]
[['HELLO', 'WORLD'], ['GOODBYE', 'WORLD']]
Run Code Online (Sandbox Code Playgroud)
这可以单独使用列表理解(不使用map函数)吗?
当我杀死尚未保存的缓冲区时,如何防止Emacs创建我不希望保存的缓冲区的备份副本?
我正在寻找一个python内置函数(或机制)来将列表分段为所需的段长度(不改变输入列表).这是我已有的代码:
>>> def split_list(list, seg_length):
... inlist = list[:]
... outlist = []
...
... while inlist:
... outlist.append(inlist[0:seg_length])
... inlist[0:seg_length] = []
...
... return outlist
...
>>> alist = range(10)
>>> split_list(alist, 3)
[[0, 1, 2], [3, 4, 5], [6, 7, 8], [9]]
Run Code Online (Sandbox Code Playgroud) 我正在使用Haml(Haml/Sass 3.0.9 - Classy Cassidy)独立生成静态HTML.我想创建一个共享布局模板,我的所有其他模板都继承该模板.
Layout.haml
%html
%head
%title Test Template
%body
.Content
Run Code Online (Sandbox Code Playgroud)
Content.haml
SOMEHOW INHERIT Layout.haml
SOMEHOW Change the title of the page "My Content".
%p This is my content
Run Code Online (Sandbox Code Playgroud)
生产:
Content.html
<html>
<head>
<title>My Content</title>
</head>
<body>
<div class="Content">
<p>This is my content</p>
</div>
</body>
</html>
Run Code Online (Sandbox Code Playgroud)
但这似乎不太可能.我已经看到在使用带有Rails的Haml时使用渲染部分,但在使用Haml独立时无法找到任何解决方案.
必须将布局代码放在我的所有模板中都是一个维护噩梦; 所以我的问题是如何避免这样做?有没有一种标准的方法可以解决这个问题?我错过了什么基本的东西?
我发现了一个类似的问题:在Rails的HAMLoutside中渲染HAML部分
Python 2.6.2 (r262:71605, Apr 14 2009, 22:40:02) [MSC v.1500 32 bit (Intel)] on
win32
Type "help", "copyright", "credits" or "license" for more information.
>>> import ctypes
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "C:\Python26\lib\ctypes\__init__.py", line 17, in <module>
from struct import calcsize as _calcsize
ImportError: cannot import name calcsize
>>> from ctypes import *
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "C:\Python26\lib\ctypes\__init__.py", line 17, in <module>
from struct import calcsize …Run Code Online (Sandbox Code Playgroud) 我正在使用Python 2.6并从re.sub()获得[我认为]的意外输出
>>> re.sub('[aeiou]', '-', 'the cat sat on the mat')
'th- c-t s-t -n th- m-t'
>>> re.sub('[aeiou]', '-', 'the cat sat on the mat', re.IGNORECASE)
'th- c-t sat on the mat'
Run Code Online (Sandbox Code Playgroud)
如果这个输出是预期的,它背后的逻辑是什么?
我想为给定的文件列表生成并存储CRC(或类似)值,这些值可以在以后用作比较.编写一个函数来做到这一点很简单,但是在Python库中有更标准的方法吗?
生成的值不需要具有任何特定标准.