Issue
For the password entry field of an email client that I am making, I thought that it would be cool to have the password show random characters. Since I don't want to write down a dictionary of all ascii characters, I was wondering if there is a module which I could import to get it. My idea of the code looks like this:
import random
import char_set #All ascii characters
def random_char():
char_select = random.randrange(len(char_set))
char_choice = char_set[char_select]
return char_choice
NOTE: This must be cross-platform as I run on Mac OSX, Windows, and Debian (Raspberry Pi).
Solution
The whole ASCII set:
In [22]: "".join(chr(x) for x in range(128))
Out[22]: '\x00\x01\x02\x03\x04\x05\x06\x07\x08\t\n\x0b\x0c\r\x0e\x0f\x10\x11\x12\x13\x14\x15\x16\x17\x18\x19\x1a\x1b\x1c\x1d\x1e\x1f !"#$%&\'()*+,-./0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\\]^_`abcdefghijklmnopqrstuvwxyz{|}~\x7f'
If you want the printable ascii characters:
In [9]: "".join(chr(x) for x in range(32,127))
Out[9]: ' !"#$%&\'()*+,-./0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\\]^_`abcdefghijklmnopqrstuvwxyz{|}~'
or if you only want the alphabets:
In [10]: import string
In [11]: string.ascii_letters
Out[11]: 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ'
string.printable
is also an option, it contains 5 extra charcters that are not in the range(32,127):
In [39]: s1=set(x for x in string.printable)
In [40]: s2=set(chr(x) for x in range(32,127))
In [41]: s1-s2
Out[41]: set(['\t', '\x0b', '\n', '\r', '\x0c'])
Answered By - Ashwini Chaudhary
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.