generator

Why won't yield return from within a `.map` callback?

强颜欢笑 提交于 2019-12-18 11:47:55
问题 Learn Generators - 4 » CATCH ERROR! The solution uses a for loop but I just couldn't find anything in MDN - Iteration Protocols that refers to yield within callbacks. I'm going to guess the answer is just don't do that but thanks in advance if anyone has the time or inclination to provide an explanation! Code: function *upper (items) { items.map(function (item) { try { yield item.toUpperCase() } catch (e) { yield 'null' } } } var badItems = ['a', 'B', 1, 'c'] for (var item of upper(badItems))

How does C#'s random number generator work?

蓝咒 提交于 2019-12-18 11:44:17
问题 I was just wondering how the random number generator in C# works. I was also curious how I could make a program that generates random WHOLE INTEGER numbers from 1-100. 回答1: I was just wondering how the random number generator in C# works. That's implementation-specific, but the wikipedia entry for pseudo-random number generators should give you some ideas. I was also curious how I could make a program that generates random WHOLE INTEGER numbers from 1-100. You can use Random.Next(int, int):

Will python SystemRandom / os.urandom always have enough entropy for good crypto

风格不统一 提交于 2019-12-18 11:06:06
问题 I have a password generator: import random, string def gen_pass(): foo = random.SystemRandom() length = 64 chars = string.letters + string.digits return ''.join(foo.choice(chars) for _ in xrange(length)) According to the docs, SystemRandom uses os.urandom which uses /dev/urandom to throw out random cryto bits. In Linux you can get random bits from /dev/urandom or /dev/random, they both use whatever entropy the kernel can get its hands on. The amount of entropy available can be checked with

Will python SystemRandom / os.urandom always have enough entropy for good crypto

家住魔仙堡 提交于 2019-12-18 11:05:25
问题 I have a password generator: import random, string def gen_pass(): foo = random.SystemRandom() length = 64 chars = string.letters + string.digits return ''.join(foo.choice(chars) for _ in xrange(length)) According to the docs, SystemRandom uses os.urandom which uses /dev/urandom to throw out random cryto bits. In Linux you can get random bits from /dev/urandom or /dev/random, they both use whatever entropy the kernel can get its hands on. The amount of entropy available can be checked with

Python multi-loop failed when with generator and iterator

南楼画角 提交于 2019-12-18 09:34:12
问题 I need a 2D loop of which the first loop uses an iterator and the second uses a generator, but this simple function failed to work, can anyone help to check? def alphabet(begin, end): for number in xrange(ord(begin), ord(end)+1): yield chr(number) def test(a, b): for i in a: for j in b: print i, j test(xrange(8, 10), alphabet('A', 'C')) The result shows: >>> 8 A >>> 8 B >>> 8 c don't know why? thanks in advance if any one can help. 回答1: Since you've asked for clarification, I'll say a bit

Python “all” function with conditional generator expression returning True. Why?

一笑奈何 提交于 2019-12-18 08:43:11
问题 Can anyone help me understand why the following Python script returns True ? x = '' y = all(i == ' ' for i in x) print(y) I imagine it's something to do with x being a zero-length entity, but cannot fully comprehend. 回答1: all() always returns True unless there is an element in the sequence that is False . Your loop produces 0 items, so True is returned. This is documented: Return True if all elements of the iterable are true ( or if the iterable is empty ). Emphasis mine. Similarly, any()

PowerShell - Password Generator - How to always include number in string?

梦想与她 提交于 2019-12-18 05:58:29
问题 I have the following PowerShell script that creates a random string of 15 digits, for use as an Active Directory password. The trouble is, this works great most of the time, but on some occasions it doesn't use a number or symbol. I just get 15 letters. This is then not usable as an Active Directory password, as it must have at least one number or symbol in it. $punc = 46..46 $digits = 48..57 $letters = 65..90 + 97..122 $YouShallNotPass = get-random -count 15 ` -input ($punc + $digits +

PowerShell - Password Generator - How to always include number in string?

落爺英雄遲暮 提交于 2019-12-18 05:58:10
问题 I have the following PowerShell script that creates a random string of 15 digits, for use as an Active Directory password. The trouble is, this works great most of the time, but on some occasions it doesn't use a number or symbol. I just get 15 letters. This is then not usable as an Active Directory password, as it must have at least one number or symbol in it. $punc = 46..46 $digits = 48..57 $letters = 65..90 + 97..122 $YouShallNotPass = get-random -count 15 ` -input ($punc + $digits +

Variable Scope In Generators In Classes

我的未来我决定 提交于 2019-12-18 04:44:12
问题 I think that I know how variables and generators work in Python well. However, the following code makes me confused. from __future__ import print_function class A(object): x = 4 gen = (x for _ in range(3)) a = A() print(list(a.gen)) When run the code (Python 2), it says: Traceback (most recent call last): File "Untitled 8.py", line 10, in <module> print(list(a.gen)) File "Untitled 8.py", line 6, in <genexpr> gen = (x for _ in range(3)) NameError: global name 'x' is not defined In Python 3, it

Getting a promise's value via yield & co

烈酒焚心 提交于 2019-12-18 04:42:22
问题 I'm trying to figure out how to get the value of a promise via yield , possibly with "co": function *(){ var someVar = yield functionThatReturnsAPromise(); } The called function is not a generator, just a normal function. With the above, someVar == Promise , but I want the resolved value. Does co or some other library have a way of doing this? 回答1: Yes, co can do that. You'll have to wrap parent function inside co call: co(function *(){ var someVar = yield functionThatReturnsAPromise(); })()