为什么我会收到NameError?

时间:2020-12-08 22:50:28

I have the following code:

我有以下代码:

from crypt import crypt
from itertools import product
from string import ascii_letters, digits

def decrypt(all_hashes, salt, charset=ascii_letters + digits + "-"):
     products = (product(charset, repeat=r) for r in range(8))
     chain = itertools.chain.from_iterable(products)
     for candidate in chain:
         hash = crypt(candidate, salt)
         if hash in all_hashes:
              yield candidate, hash
              all_hashes.remove(hash)
              if not all_hashes:
                 return

all_hashes = ['aaRrt6qwqR7xk', 'aaacT.VSMxhms' , 'aaWIa93yJI9kU',
'aakf8kFpfzD5E', 'aaMOPiDnXYTPE', 'aaz71s8a0SSbU', 'aa6SXFxZJrI7E'
'aa9hi/efJu5P.', 'aaBWpr07X4LDE', 'aaqwyFUsGMNrQ', 'aa.lUgfbPGANY'
'aaHgyDUxJGPl6', 'aaTuBoxlxtjeg', 'aaluQSsvEIrDs', 'aajuaeRAx9C9g'
'aat0FraNnWA4g', 'aaya6nAGIGcYo', 'aaya6nAGIGcYo', 'aawmOHEectP/g'
'aazpGZ/jXGDhw', 'aadc1hd1Uxlz.', 'aabx55R4tiWwQ', 'aaOhLry1KgN3.'
'aaGO0MNkEn0JA', 'aaGxcBxfr5rgM', 'aa2voaxqfsKQA', 'aahdDVXRTugPc'
'aaaLf47tEydKM', 'aawZuilJMRO.w', 'aayxG5tSZJJHc', 'aaPXxZDcwBKgo'
'aaZroUk7y0Nao', 'aaZo046pM1vmY', 'aa5Be/kKhzh.o', 'aa0lJMaclo592'
'aaY5SpAiLEJj6', 'aa..CW12pQtCE', 'aamVYXdd9MlOI', 'aajCM.48K40M.'
'aa1iXl.B1Zjb2', 'aapG.//419wZU']


all_hashes = set(all_hashes)
salt = 'aa'
for candidate, hash in decrypt(all_hashes, salt):
     print 'Found', hash, '! The original string was', candidate

And when I go to run it i get the following traceback error:

当我去运行它时,我得到以下回溯错误:

Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "<stdin>", line 3, in decrypt
NameError: global name 'itertools' is not defined

And can not figure out why it is happening.

并且无法弄清楚它为什么会发生。

Someone please shed some light, thanks in advance

有人请提前了解一下

3 个解决方案

#1


13  

It doesn't look like you imported itertools...

它看起来不像你导入的itertools ......

from itertools import product

doesn't count as that will only pull product directly into your module's namespace (your module still doesn't know anything about the rest of itertools. Just add:

不计算只会将产品直接拉入模块的命名空间(你的模块仍然不知道其余的itertools。只需添加:

import itertools

at the top of your script and that error should go away because now you've pulled the itertools namespace into your module's namespace under the name itertools. In other words, to have access to the chain function, you'd use itertools.chain (as you have in your script above).

在脚本的顶部,该错误应该消失,因为现在您已经将itertools命名空间拉到名称为itertools的模块命名空间中。换句话说,要访问链函数,您可以使用itertools.chain(就像上面的脚本中一样)。

#2


1  

You want either:

你想要:

from itertools import chain, product

and use chain and product, or:

并使用链和产品,或:

import itertools

and use itertools.chain and itertools.product.

并使用itertools.chain和itertools.product。

#3


0  

import itertools

from itertools import izip_longest

This helped me use itertools and then be able to use izip_longest for iterating through uneven length of arrays.

这有助于我使用itertools,然后能够使用izip_longest迭代不均匀的数组长度。

#1


13  

It doesn't look like you imported itertools...

它看起来不像你导入的itertools ......

from itertools import product

doesn't count as that will only pull product directly into your module's namespace (your module still doesn't know anything about the rest of itertools. Just add:

不计算只会将产品直接拉入模块的命名空间(你的模块仍然不知道其余的itertools。只需添加:

import itertools

at the top of your script and that error should go away because now you've pulled the itertools namespace into your module's namespace under the name itertools. In other words, to have access to the chain function, you'd use itertools.chain (as you have in your script above).

在脚本的顶部,该错误应该消失,因为现在您已经将itertools命名空间拉到名称为itertools的模块命名空间中。换句话说,要访问链函数,您可以使用itertools.chain(就像上面的脚本中一样)。

#2


1  

You want either:

你想要:

from itertools import chain, product

and use chain and product, or:

并使用链和产品,或:

import itertools

and use itertools.chain and itertools.product.

并使用itertools.chain和itertools.product。

#3


0  

import itertools

from itertools import izip_longest

This helped me use itertools and then be able to use izip_longest for iterating through uneven length of arrays.

这有助于我使用itertools,然后能够使用izip_longest迭代不均匀的数组长度。