Getting started with python 06 - grocery store (generator / iterator / pickle)

Posted by lingo5 on Fri, 31 Dec 2021 22:53:04 +0100

iterator

Iteration is one of Python's most powerful features and a way to access collection elements.

An iterator is an object that remembers where to traverse.
The iterator object is accessed from the first element of the collection until all the elements are accessed. Iterators can only move forward, not backward.
Iterators have two basic methods: iter() and next().

Iterator objects can be traversed using the regular for statement:

list = [1, 2, 3, 4]
it = iter(list)  # Create iterator object
for i in it:  # Iterator objects can be traversed with regular for statements
    print(i, end=" ")
## The result is 1 2 3 4

You can also use the next() function:

import sys  # Import sys module
list = [1, 2, 3, 4]
it = iter(list)  # Create iterator object
while True:
    try:
        print(next(it))  # Keep outputting list elements
    except StopIteration:  # The StopIteration exception is used to identify the completion of the iteration
        sys.exit()
## The result is 1
##      2
##      3
##      4

Create an iterator:
To use a class as an iterator, you need to implement two methods iter() and next() in the class.
The iter() method returns a special iterator object that implements the next() method and identifies the completion of the iteration through the StopIteration exception.
The next() method (next() in Python 2) returns the next iterator object.

# Create an iterator that returns a number, with an initial value of 1 and gradually increasing by 1:
class MyNumbers:
  def __iter__(self):
    self.a = 1
    return self
 
  def __next__(self):
    x = self.a
    self.a += 1
    return x
 
myclass = MyNumbers()
myiter = iter(myclass)  # Use a class as an iterator
 
print(next(myiter))
print(next(myiter))
print(next(myiter))
print(next(myiter))
## The result is 1
##      2
##      3
##      4

StopIteration exception
The StopIteration exception is used to identify the completion of the iteration and prevent infinite loops. In the next() method, we can set the StopIteration exception to be triggered after completing the specified number of cycles to end the iteration.

# Stop execution after 5 iterations:
class MyNumbers:
    def __iter__(self):
        self.a = 1
        return self

    def __next__(self):
        if self.a <= 5:
            x = self.a
            self.a += 1
            return x
        else:
            raise StopIteration  # Stop after 5 cycles


myclass = MyNumbers()
myiter = iter(myclass)

for x in myiter:
    print(x)
## The result is 1
##      2
##      3
##      4
##      5

generator

In Python, functions that use yield are called generator s.

Different from ordinary functions, a generator is a function that returns an iterator and can only be used for iterative operations. It is easier to understand that a generator is an iterator.
In the process of calling the generator to run, each time a yield is encountered, the function will pause and save all the current running information, return the value of yield, and continue to run from the current position the next time the next() method is executed.
Call a generator function and return an iterator object.

The following example uses yield to implement Fibonacci sequence:

def fibonacci(n):  # Generator function - Fibonacci
    a, b, counter = 0, 1, 0
    while True:
        if (counter > n):
            return
        yield a  # Pause saving all current operation information
        a, b = b, a + b
        counter += 1
# a=0,b=1,counter=0 [save:a=0] a=1,b=1,counter=1
# [save:a=1] a=1,b=2,counter=2
# [save:a=1] a=2,b=3,counter=3
# [save:a=2] a=3,b=5,counter=4 exit

f = fibonacci(3)  # f is an iterator that is returned by the generator

while True:
    try:
        print(next(f), end=" ")
    except StopIteration:
        sys.exit()
## The result is 0 1 2

An example helps understand yield:

def myYield_1():
    a, i = 'yield', 0
    while True:
        print('before #%d' % i, end=", ")
        yield a, i
        print('after #%d' % i, end=", ")
        i += 1

it1 = iter(myYield_1())

for i in range(10):
    print("next #%d" % i, end=": ")
    print(next(it1))

The output result of this example is:

pickle module

The pickle module implements binary serialization and deserialization of a Python object structure.

The pickle module provides the following function pairs:

  1. dumps(object) returns a string containing an object in pickle format;
  2. loads(string) returns the object contained in the pickle string;
  3. dump(object, file) writes an object to a file. This file can be an actual physical file, but it can also be any object similar to a file. This object has a write() method and can accept a single string parameter;
  4. load(file) returns the objects contained in the pickle file.
import pickle

Define a list type and store the data in the list into my_list.pkl file.

my_list = [123, 3.14, 'zhang', ['another list']]
pickle_file = open('my_list.pkl', 'wb')  # b indicates byte type
pickle.dump(my_list, pickle_file)  # pickle_file write to file my_list
pickle_file.close()  # Similar to a normal file, it needs to be closed before writing

After execution, a file named my will appear under the same file path as the current python file_ list. Pkl file.

We can read the data saved in this file:

pickle_file = open('my_list.pkl', 'rb')  # Open the byte file as read-only
my_list2 = pickle.load(pickle_file)  # Return included in pickle_ Objects in file
print(my_list2)  # Output object
# The output result is [123, 3.14, 'Zhang', ['Other list ']]

[example] save the city data in the query city weather script with pickle.
weather_ search. The PY script is as follows:

import urllib.request
import json

# Building a city dictionary
city = {
    'Beijing': '101010100',
    'Haidian': '101010200',
    'Sunrise': '101010300',
    'Shunyi': '101010400',
    'Huairou': '101010500',
    'Tongzhou': '101010600',
    'Changping': '101010700',
    'Yanqing': '101010800',
    'Fengtai': '101010900',
    'Shijingshan': '1010101100',
    'Daxing': '10101011200',
    'Miyun': '10101011300',
    'Mentougou': '10101011400',
    'Pinggu': '10101011500',
    'badaling section of the great wall': '10101011600',
    'Foyeding': '10101011700',
    'Tanghekou': '10101011800',
    'Miyun Shangdianzi': '10101011900',
    'Vegetarian Canteen': '10101012000',
    'Xiayunling': '10101012100',
}
password = input('Please enter city:')
name1 = city[password]
File1 = urllib.request.urlopen('http://m.weather.com.cn/data/' + name1 + '.html')
weatherHTML = File1.read().decode('utf-8')  # Read in the open url
weatherJSON = json.JSONDecoder().decode(weatherHTML)  # Create json
weatherInfo = weatherJSON[' weatherinfo']
# Print information
print('city: ', weatherInfo['city'])
print('time: ', weatherInfo['date_ y'])
print('24 Hourly weather: ')
print('temperature: ', weatherInfo['temp1'])
print('weather: ', weatherInfo['weather1'])
print('wind speed: ', weatherInfo['wind1'])
print('ultraviolet rays: ', weatherInfo['index_ _uv'])
print('Dressing index: ', weatherInfo['index_ _d'])
print('48 Hourly weather: ')
print('temperature:', weatherInfo['temp2'])
print('weather: ', weatherInfo['weather2'])
print('wind speed: ', weatherInfo['wind2'])
print('ultraviolet rays: ', weatherInfo['index48_ _uv'])
print('Dressing index: ', weatherInfo[' index48_ _d'])
print('72 Hourly weather: ')
print('temperature: ', weatherInfo[' temp3'])
print('weather: ', weatherInfo['weather3'])
print('wind speed: ', weatherInfo[' wind3'])
input('Press any key to exit')

Save city information in city to pickle:

import pickle

pickle_file = open('city_data.pkl', 'wb')
pickle.dump(city, pickle_file)  # Write data objects in city to pickle_ In file
pickle_file.close()

The original script can be clearer and more readable. The script code after pickle is as follows:

import urllib.request
import json
import pickle

#Read the original city data by opening the pkl file
pickle_file = open('city_data.pkl', 'rb')
city = pickle.load(pickle_file)

password = input('Please enter city:')
name1 = city[password]
File1 = urllib.request.urlopen('http://m.weather.com.cn/data/' + name1 + '.html')
weatherHTML = File1.read().decode('utf-8')  # Read in the open url
weatherJSON = json.JSONDecoder().decode(weatherHTML)  # Create json
weatherInfo = weatherJSON[' weatherinfo']
# Print information
print('city: ', weatherInfo['city'])
print('time: ', weatherInfo['date_ y'])
print('24 Hourly weather: ')
print('temperature: ', weatherInfo['temp1'])
print('weather: ', weatherInfo['weather1'])
print('wind speed: ', weatherInfo['wind1'])
print('ultraviolet rays: ', weatherInfo['index_ _uv'])
print('Dressing index: ', weatherInfo['index_ _d'])
print('48 Hourly weather: ')
print('temperature:', weatherInfo['temp2'])
print('weather: ', weatherInfo['weather2'])
print('wind speed: ', weatherInfo['wind2'])
print('ultraviolet rays: ', weatherInfo['index48_ _uv'])
print('Dressing index: ', weatherInfo[' index48_ _d'])
print('72 Hourly weather: ')
print('temperature: ', weatherInfo[' temp3'])
print('weather: ', weatherInfo['weather3'])
print('wind speed: ', weatherInfo[' wind3'])
input('Press any key to exit')

Topics: Python