Python| Random Password Generator using Tkinter With growing technology, everything has relied on data and securing these data is the main concern. Passwords are meant to keep the data safe that we upload on the Internet. Generating a random string. # I wont be going into depth because the python # documentation provides ample information. # so lets generate a random string with 32 characters. >>> random = '.join ( [random.choice (string.ascii_letters + string.digits) for n in xrange (32)]) >>> print random '9fAgMmc9hl6VXIsFu3ddb5MJ2U86qEad' >>> print len (random) 32.
Previous Chapter: List Comprehension
Next Chapter: Exception Handling
Next Chapter: Exception Handling
Generators
Introduction
Generators are a simple and powerful possibility to create or to generate iterators. On the surfacethey look like functions, but there is both a syntactical and a semantic difference.Instead of return statements you will find inside of the body of a generator only yield statements,i.e. one or more yield statements.
Another important feature of generators is that the local variables and the execution start is automatically saved between calls. This is necessary, because unlike an ordinary function successive calls to a generator function don't start execution at the beginning of the function. Instead, the new call to a generator function will resume execution rightafter the yield statement in the code, where the last call exited. In other words: When the Pythoninterpreter finds a yield statement inside of an iterator generated by a generator, it records the position of this statement and the local variables, and returns from the iterator. The next time this iterator is called, it will resume execution at the line following the previous yield statement. There may be more than one yield statement in the code of a generator or the yield statement might be inside the body of a loop. If there is a return statement in the code of a generator, the execution will stop with a StopIteration exception error if this code is executed by the Python interpreter.
Everything what can be done with a generator can be implemented with a class based iterator as well.But the crucial advantage of generators consists in automatically creating the methods __iter__() and next().
Generators provide a very neat way of producing data which is huge or infinite.
The following is a simple example of a generator, which is capable of producing four city names:It's possible to create an iterator with this generator, which generates one after the other the four cities Konstanz, Zurich, Schaffhausen and Stuttgart.
As we can see, we have generated an iterator x in the interactive shell. Every call of the method next() returns another city. After the last city, i.e. Stuttgart, has been created, another call ofx.next() raises an error, saying that the iteration has stopped, i.e. 'StopIteration'.
Can we send a reset to an iterator is a frequently asked question, so that it can start the iterationall over again. There is no reset, but it's possible to create another generator. This can be done e.g. by having the statement 'x = city_generator()' again.
Thought at first sight the yield statement looks like the return statement of a function, we can seein this example that there is a big difference. If we had a return statement instead of a yield in theprevious example, it would be a function. But this function would always return 'Konstanz' and never any of the other cities, i.e. Zurich, Schaffhausen or Stuttgart.
Another important feature of generators is that the local variables and the execution start is automatically saved between calls. This is necessary, because unlike an ordinary function successive calls to a generator function don't start execution at the beginning of the function. Instead, the new call to a generator function will resume execution rightafter the yield statement in the code, where the last call exited. In other words: When the Pythoninterpreter finds a yield statement inside of an iterator generated by a generator, it records the position of this statement and the local variables, and returns from the iterator. The next time this iterator is called, it will resume execution at the line following the previous yield statement. There may be more than one yield statement in the code of a generator or the yield statement might be inside the body of a loop. If there is a return statement in the code of a generator, the execution will stop with a StopIteration exception error if this code is executed by the Python interpreter.
Everything what can be done with a generator can be implemented with a class based iterator as well.But the crucial advantage of generators consists in automatically creating the methods __iter__() and next().
Generators provide a very neat way of producing data which is huge or infinite.
The following is a simple example of a generator, which is capable of producing four city names:It's possible to create an iterator with this generator, which generates one after the other the four cities Konstanz, Zurich, Schaffhausen and Stuttgart.
As we can see, we have generated an iterator x in the interactive shell. Every call of the method next() returns another city. After the last city, i.e. Stuttgart, has been created, another call ofx.next() raises an error, saying that the iteration has stopped, i.e. 'StopIteration'.
Can we send a reset to an iterator is a frequently asked question, so that it can start the iterationall over again. There is no reset, but it's possible to create another generator. This can be done e.g. by having the statement 'x = city_generator()' again.
Thought at first sight the yield statement looks like the return statement of a function, we can seein this example that there is a big difference. If we had a return statement instead of a yield in theprevious example, it would be a function. But this function would always return 'Konstanz' and never any of the other cities, i.e. Zurich, Schaffhausen or Stuttgart.
Method of Operation
As we have elaborated in the introduction of this chapter, the generators offer a comfortablemethod to generate iterators, and that's why they are called generators.
Method of working:
Method of working:
- A generator is called like a function. It's return value is an iterator object. The code of the generator will not be executed in this stage.
- The iterator can be used by calling the next method. The first time the execution starts like a function, i.e. the first line of code within the body of the iterator. The code is executed until a yield statement is reached.
- yield returns the value of the expression, which is following the keyword yield. This is like a function, but Python keeps track of the position of this yield and the state of the local variables is stored for the next call. At the next call, the execution continues with the statement following the yield statement and the variables have the same values as they had in the previous call.
- The iterator is finished, if the generator body is completely worked through or if the program flow encounters a return statement without a value.
The Fibonacci sequence is named after Leonardo of Pisa, who was known as Fibonacci (a contraction of filius Bonacci, 'son of Bonaccio'). In his textbook Liber Abaci, which appeared in the year 1202) he had an exercise about the rabbits and their breeding: It starts with a newly-born pair of rabbits, i.e. a maleand a female animal. It takes one month until they can mate. At the end of the second monththe female gives birth to a new pair of rabbits. Now let's suppose that every female rabbitwill bring forth another pair of rabbits every month after the end of the first month.We have to mention that Fibonacci's rabbits never die. They question is how large thepopulation will be after a certain period of time.
This produces a sequence of numbers: 0,1,1,2,3,5,8,13
This sequence can be defined in mathematical terms like this:
Fn = Fn - 1 + Fn - 2
with the seed values:
F0 = 0 and F1 = 1The generator above can be used to create the first n Fibonacci numbers, or better(n+1) numbers because the 0th number is also included.
In the next example we show you a version which is capable of returning an endlessiterator. We have to take care when we use this iterator that a termination criteriumis used:
Recursive Generators
Like functions generators can be recursively programmed. The following example is a generator to create all the permutations of a given list of items.
For those who don't know what permutations are, we have a short introduction:
Formal Definition:
A permutation is a rearrangement of the elements of an ordered list. In other words: Every arrangement of n elements is called a permutation.
In the following lines we show you all the permutations of the letter a, b and c:
a b c
a c b
b a c
b c a
c a b
c b a
The number of permutations on a set of n elements is given by n!
For those who don't know what permutations are, we have a short introduction:
Formal Definition:
A permutation is a rearrangement of the elements of an ordered list. In other words: Every arrangement of n elements is called a permutation.
In the following lines we show you all the permutations of the letter a, b and c:
a b c
a c b
b a c
b c a
c a b
c b a
The number of permutations on a set of n elements is given by n!
A Generator of Generators
The second generator of our Fibonacci sequence example generates an iterator, whichcan theoretically produce all the Fibonacci numbers, i.e. an infinite number.But you shouldn't try to produce all these numbers, as we would do in the followingexample: This will show you very fast the limits of your computer.
In most practical applications, we only need the first n elements of an 'endless' iterator.We can use another generator, in our example firstn, to create the first n elements ofa generator g: The following script returns the first 10 elements of the Fibonacci sequence:
In most practical applications, we only need the first n elements of an 'endless' iterator.We can use another generator, in our example firstn, to create the first n elements ofa generator g: The following script returns the first 10 elements of the Fibonacci sequence:
Previous Chapter: List Comprehension
Next Chapter: Exception Handling
Next Chapter: Exception Handling
Let’s see how to generate QR code in Python using
pyqrcode
module.pyqrcode
module is a QR code generator. The module automates most of the building process for creating QR codes. This module attempts to follow the QR code standard as closely as possible. The terminology and the encodings used in pyqrcode
come directly from the standard.Installation
pyqrcode.create(content, error='H', version=None, mode=None, encoding=None)
: When creating a QR code only the content to be encoded is required, all the other properties of the code will be guessed based on the contents given. This function will return a QRCode
object.One can specify all the properties of required QR code through the optional parameters of the
pyqrcode.create()
function. Below are some properties:error: This parameter sets the error correction level of the code.
version: This parameter specifies the size and data capacity of the code.
mode: This parameter sets how the contents will be encoded.
version: This parameter specifies the size and data capacity of the code.
mode: This parameter sets how the contents will be encoded.
Below is the code:
import pyqrcode # String which represent the QR code url = pyqrcode.create(s) # Create and save the png file naming 'myqr.png' |
Output:
Recommended Posts:
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to [email protected]. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the 'Improve Article' button below.