[Tutor] Question about login=''.join(choice(lc) for j in range(llen))

classic Classic list List threaded Threaded
5 messages Options
Reply | Threaded
Open this post in threaded view
|

[Tutor] Question about login=''.join(choice(lc) for j in range(llen))

Khalid Al-Ghamdi
Hi,

The following code tries to generate some dummy data for regex exercises. My question is in reference the line before last:

    dom="".join(choice(lc) for j in range (dlen))

how does the interpreter know what "j" is supposed to refer to when it was not mentioned prior?


from random import randrange, choice
from string import ascii_lowercase as lc
from sys import maxsize
from time import ctime

tlds = ('com', 'edu', 'net', 'org', 'gov')

for i in range(randrange(5,11)):
    dtint=randrange(maxsize)    #pick a random number to use to generate random date in next line
    dtstr=ctime(dtint)          #date string
    llen=randrange(4,8)         #login is shorter
    login=''.join(choice(lc) for j in range(llen))
    dlen=randrange(llen,13)     #domain is longer
    dom="".join(choice(lc) for j in range (dlen))
    print('{}::{}@{}.{}::{}-{}-{}'.format(dtstr,login,dom,choice(tlds),dtint,llen,dlen))
    


_______________________________________________
Tutor maillist  -  [hidden email]
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor
Reply | Threaded
Open this post in threaded view
|

Re: [Tutor] Question about login=''.join(choice(lc) for j in range(llen))

Emile van Sebille
On 4/3/2012 7:54 AM Khalid Al-Ghamdi said...
> Hi,
>
> The following code tries to generate some dummy data for regex
> exercises. My question is in reference the line before last:
>
>      dom="".join(choice(lc) for j in range (dlen))
>
> how does the interpreter know what "j" is supposed to refer to when it
> was not mentioned prior?


Prior value or not, j is the loop variable that steps over range(dlen)
and refers to the numbers 0 to dlen-1 in turn.

Did you mean to ask something about the code below as well?

Emile


>
>
> from random import randrange, choice
> from string import ascii_lowercase as lc
> from sys import maxsize
> from time import ctime
>
> tlds = ('com', 'edu', 'net', 'org', 'gov')
>
> for i in range(randrange(5,11)):
>      dtint=randrange(maxsize)    #pick a random number to use to
> generate random date in next line
>      dtstr=ctime(dtint)          #date string
>      llen=randrange(4,8)         #login is shorter
>      login=''.join(choice(lc) for j in range(llen))
>      dlen=randrange(llen,13)     #domain is longer
>      dom="".join(choice(lc) for j in range (dlen))
>
> print('{}::{}@{}.{}::{}-{}-{}'.format(dtstr,login,dom,choice(tlds),dtint,llen,dlen))
>
>
>
> _______________________________________________
> Tutor maillist  -  [hidden email]
> To unsubscribe or change subscription options:
> http://mail.python.org/mailman/listinfo/tutor


_______________________________________________
Tutor maillist  -  [hidden email]
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor
Reply | Threaded
Open this post in threaded view
|

Re: [Tutor] Question about login=''.join(choice(lc) for j in range(llen))

Alan Gauld
In reply to this post by Khalid Al-Ghamdi
On 03/04/12 15:54, Khalid Al-Ghamdi wrote:

>      dom="".join(choice(lc) for j in range (dlen))
>
> how does the interpreter know what "j" is supposed to refer to when it
> was not mentioned prior?

In Python variables are defined by using them.

In the code below you have i used in a for loop, even though not
mentioned before. j is similarly being used in the generator expression
for loop:

choice(lc) for j in range (dlen)

unwraps to:

dummy = []
for j in range(dlen):
    dummy.append(choice(lc))

Which effectively creates a list of dlen choice items.


> from random import randrange, choice
> from string import ascii_lowercase as lc
> from sys import maxsize
> from time import ctime
>
> tlds = ('com', 'edu', 'net', 'org', 'gov')
>
> for i in range(randrange(5,11)):
>      dtint=randrange(maxsize)    #pick a random number to use to

HTH
--
Alan G
Author of the Learn to Program web site
http://www.alan-g.me.uk/

_______________________________________________
Tutor maillist  -  [hidden email]
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor
Reply | Threaded
Open this post in threaded view
|

Re: [Tutor] Question about login=''.join(choice(lc) for j in range(llen))

Peter Otten
Alan Gauld wrote:

> On 03/04/12 15:54, Khalid Al-Ghamdi wrote:
>
>>      dom="".join(choice(lc) for j in range (dlen))
>>
>> how does the interpreter know what "j" is supposed to refer to when it
>> was not mentioned prior?
>
> In Python variables are defined by using them.
>
> In the code below you have i used in a for loop, even though not
> mentioned before. j is similarly being used in the generator expression
> for loop:
>
> choice(lc) for j in range (dlen)
>
> unwraps to:
>
> dummy = []
> for j in range(dlen):
>     dummy.append(choice(lc))

An interesting aspect of these "generator expressions" is that they are
"lazy", they deliver only as many items as necessary.

>>> numbers = (n for n in range(1000))
>>> next(numbers)
0
>>> next(numbers)
1
>>> any(n>10 for n in numbers)
True
>>> next(numbers)
12
>>> any(n<10 for n in numbers)
False
>>> next(numbers)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
StopIteration

The StopIteration hints that we have consumed all numbers. We were already
at 13 when we asked for a number < 10; therefore the complete rest from 13
to 999 was scanned in vain.

_______________________________________________
Tutor maillist  -  [hidden email]
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor
Reply | Threaded
Open this post in threaded view
|

Re: [Tutor] Question about login=''.join(choice(lc) for j in range(llen))

wesley chun
On Tue, Apr 3, 2012 at 10:50 AM, Peter Otten <[hidden email]> wrote:
> Alan Gauld wrote:
>> On 03/04/12 15:54, Khalid Al-Ghamdi wrote:
>>
>>>      dom="".join(choice(lc) for j in range (dlen))
>>>
>>> how does the interpreter know what "j" is supposed to refer to when it
>>> was not mentioned prior?


+1 everyone else's replies so far. i'll add the following: you create
variables by assigning things to them. in this example, no prior code
used the variable 'x':

>>> x = 10
>>> print x
10

similarly, when used in a for-loop, it's like you had an "invisible"
assignment at the "top" of the loop. here's an example:

>>> for i in range(5):
...  print i
...
0
1
2
3
4
>>> print i
4

notice that the 'i' variable is still there even after the loop has
ended. it's as if you did the following:

>>> i = 0
>>> print i
0
>>> i = 1
    :
>>> i = 4
>>> print i  # 1st time, part of the "loop"
4
>>> print i  # 2nd time, "outside" of the loop
4

hope this helps!
--wesley
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
"A computer never does what you want... only what you tell it."
    wesley chun : wescpy at gmail : @wescpy/+wescpy
    Python training & consulting : CyberwebConsulting.com
    "Core Python" books : CorePython.com
    Python blog: wescpy.blogspot.com
_______________________________________________
Tutor maillist  -  [hidden email]
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor