我已经写了一些抽象Crypto.Cipher.AES.encrypt/decrypt
,我想为它编写单元测试.编写单元测试decrypt
很容易,但有没有合理的方法来测试加密本身的发生?例如,有没有办法查看字节串并知道是,那是一大堆AES加密数据?如果没有(可能会提供太多信息),我可以选择测试我的encrypt
功能是否具有合理的输出?
def encrypt(plaintext):
"""Return 'plaintext' AES encrypted."""
initialization_vector = Random.new().read(AES.block_size)
cipher = AES.new(settings.secret, AES.MODE_CFB, initialization_vector)
return initialization_vector + cipher.encrypt(bytes(plaintext.encode('utf-8')))
def decrypt(crypt):
"""Return 'crypt' AES decrypted."""
initialization_vector, crypt = crypt[:AES.block_size], crypt[AES.block_size:]
cipher = AES.new(settings.secret, AES.MODE_CFB, initialization_vector)
return cipher.decrypt(crypt)
# … this is a method of a unittest.TestCase subclass:
def test_decrypt(self):
"""Test that a message can be decrypted."""
crypt = utils.encrypt("abcdefg")
decrypt = utils.decrypt(crypt)
self.assertEqual("abcdefg", decrypt)
def test_encrypt(self):
"""Test that a message can be …
Run Code Online (Sandbox Code Playgroud) 我试图了解sed命令和循环.我需要参考文本的一部分(20行)并将其附加到文件名的csv.这是我的代码
for i in ~/mspl/*.doc
do
catdoc "$i" > auto.txt
sed -e '1,20!d' auto.txt > auto1.txt
sed -e '1s/^/"$i" ;/' auto1.txt > auto2.txt
sed -e '20s/$/~/' auto2.txt > auto3.txt
cat auto3.txt >> lines.csv
done
Run Code Online (Sandbox Code Playgroud)
问题是我的第二个"i"参数没有转换为csv中的文件名.
在线
sed -e '1s/^/"$i" ;/' auto1.txt > auto2.txt
Run Code Online (Sandbox Code Playgroud)
请告诉我这里的错误是什么?
我正在练习这个练习:
编写一个函数is_member(),它接受一个值(即一个数字,字符串等)x和一个值列表a,如果x是一个成员,则返回True,否则返回False.(注意,这正是in运算符所做的,但是为了练习,你应该假装Python没有这个运算符.)
我写了这个函数:
def isMember(value, list):
for element in list:
if(element == value):
return True
else:
return False
myList = ["a","b","c",1,2,3]
print(isMember("a",myList)) #Returns True; correct
print(isMember(3,myList)) #Returns False; why the heck?
Run Code Online (Sandbox Code Playgroud) 我在目录中有一组文件.我想从ls输出中提取没有绝对路径和修改时间戳的文件名.
/apps/dir/file1.txt
/apps/dir/file2.txt
Run Code Online (Sandbox Code Playgroud)
现在从ls输出我提取出文件名和时间戳的字段
ls -ltr /apps/dir | awk '{print $6 $7 $8 $9}'
Sep 25 2013 /apps/dir/file1.txt
Dec 20 2013 /apps/dir/file2.txt
Dec 20 2013 /apps/dir/file3.txt
Run Code Online (Sandbox Code Playgroud)
而我希望它像
Sep 25 2013 file1
Dec 20 2013 file2
Dec 20 2013 file3
Run Code Online (Sandbox Code Playgroud)
一个解决方案可以是cd到那个目录并从那里运行命令,但有没有CD的解决方案?我也使用substr(),但由于文件名的长度不相等,所以将常量值传递给substr()函数并没有成功.
对于我正在研究的项目,我正在处理许多深度嵌套的dict-and-list数据结构.通常我发现自己正在进行查找,我希望返回一个包含单个成员的列表.查找可能会彻底失败,或者只返回零结果,因此我可以编写如下代码:
try:
value_I_need = lookup_results[0]
except IndexError:
# handle lookup failure, keep going
Run Code Online (Sandbox Code Playgroud)
但是最近我一直想知道编写一个循环是否更具有前瞻性,最初假设它只发生零次或一次:
value_I_need = None
for value_I_need in lookup_results:
break
if value_I_need is None:
# handle lookup failure, keep going
Run Code Online (Sandbox Code Playgroud)
(我说"面向未来",因为我可能想重新编写或推广这些代码来处理其中包含多个结果的查找.)这两种方法中是否存在任何固有的untythonic,错误或较慢的内容?
为什么以下功能update()
不更新?这个小提琴的结构是否正确?
小提琴代码:
HTML:
<body>
<div id="toupdate">
<script type="text/javascript" charset="utf-8" src="http://static.polldaddy.com/p/6343621.js"></script>
</div>
</body>?
Run Code Online (Sandbox Code Playgroud)
JavaScript的:
$(document).ready(function() {
update();
});
function update(){
$('#toupdate').remove();
alert('removed');
$('body').append('<div id="toupdate">
<script type="text/javascript" charset="utf-8" src="http://static.polldaddy.com/p/6343621.js"></script>
</div>');
}
Run Code Online (Sandbox Code Playgroud) 我有一个下面定义的函数打印列表中的每个整数,它完美地工作.我想要做的是创建第二个函数,该int_list()
函数将调用或重用函数以显示已生成的列表的总和.
我不确定这本身是否由代码本身执行 - 我对Python语法很新.
integer_list = [5, 10, 15, 20, 25, 30, 35, 40, 45]
def int_list(self):
for n in integer_list
index = 0
index += n
print index
Run Code Online (Sandbox Code Playgroud) 我有一个数据集组织成一个列表字典,如:
{ UUID: [3, 3, 5, 3, 0, 0, 3, 3, 2, 3, 2, 1, 1, 0, 2, 0, 5, 0, 0, 0, 0, 3, 4, 1, 2],
UUID: [1, 2, 3, 1, 0, 0, 2] }
Run Code Online (Sandbox Code Playgroud)
我想检测连续相同值(特别是0)的情况,特别是检测n个连续相同值的实例.
例如,如果n为3且值为0,我会将第一个键:值对的UUID附加到符合条件的UUID列表,而不是第二个.
以这种方式检测连续相同值的最有效方法是什么?
我正在为计费程序项目编写条件语句。我知道这对于初学者来说有点高级,但我欢迎挑战。无论如何,我计划在启动程序时询问用户名和密码。这是我对该程序的第一次编码。
print ("Hello and welcome to Billing Pro, please enter your username and password to access the database.")
username = input ("Enter username:")
if username == "cking" or "doneal" or "mcook":
print ("Valid username.")
else:
print ("Invalid username. Please try again.")
password = input ("Enter password:")
if password == "rammstein1" or "theory1" or "tupac1":
print ("Valid password. User has been verified.")
else:
print ("Invalid password. Access denied.")
Run Code Online (Sandbox Code Playgroud)
现在,一开始当我运行这段代码时,如果我输入了用户名的三个选项以外的任何内容,Python 就会打印出“无效用户名”行。由于某种原因,它现在打印出“有效用户名”,然后继续显示密码提示。此外,如果我输入密码选择以外的任何内容,它总是会读出“有效密码”提示。
另外,当用户输入三个选项以外的内容时,如何循环用户名提示?我应该使用 while 语句而不是 if-else 还是可以在 if-else 语句末尾放置 while 语句以再次触发提示?
哦,我知道你看不出来,因为我在问题中的格式很糟糕,但我确实在脚本本身上使用了正确的缩进。
例如,我有一个参考编号a = 15
和b= 3
.
x=2
,f(a,b,x) = 1
因为如果将15分成3部分,则数字2在第一部分中.x=7
,f(a,b,x) = 2
因为如果将15分成3部分,则数字7在第二部分中.x=15
,f(a,b,x) = 3
因为如果将15分成3部分,则数字15在第3部分中.有这样的内置功能吗?
我正在尝试编写看起来像这样的东西:
#!/bin/bash
echo "type your separated words"
read *all variables*
echo *all variables*
Run Code Online (Sandbox Code Playgroud)
那可能吗?
您好我的Python函数有问题.我试图创建一个简单的函数来返回一个变量然后打印它.相反,它返回功能信息在0x7f8442e9d398而不是字符串...任何想法?
def info():
message = '********************************\n' \
'*******INSTRUCTIONS*************\n' \
'********************************\n' \
'* This is an info message *\n' \
'********************************'
return message
print info
Run Code Online (Sandbox Code Playgroud) 我的作业需要做的是将正数的数字从低到高排序.
我可以用
我不能用
python ×8
bash ×3
linux ×2
list ×2
loops ×2
shell ×2
arrays ×1
awk ×1
dictionary ×1
encryption ×1
function ×1
if-statement ×1
javascript ×1
jquery ×1
python-3.x ×1
sed ×1
sorting ×1
unit-testing ×1
while-loop ×1