[Python practice 0] modify the Python code uploaded to PyPI

Source: Internet
Author: User

In the previous article, we have uploaded a wukong module to the PyPI website, which contains a print_lol function, which can be used to output the list, but this function has a problem, that is, the nested list output is not indented, as shown below:

>>> import wukong>>> movies = ["The Holy Grail",1975,"Terry Jones & Terry Gilliam",91,                    ["Graham chapman",["Michael Palin","John cleese","Terry Gilliam","Eric Idle & Terry Jones"]],         "The Life of Brain",1979,"Terry Jones",94,            ["Graham chapman",["Michael Palin","John cleese","Terry Gilliam","Eric Idle & Terry Jones"]]]>>> wukong.print_lol(movies)The Holy Grail1975Terry Jones & Terry Gilliam91Graham chapmanMichael PalinJohn cleeseTerry GilliamEric Idle & Terry JonesThe Life of Brain1979Terry Jones94Graham chapmanMichael PalinJohn cleeseTerry GilliamEric Idle & Terry Jones>>> 

As shown above, we can see that all output results are not indented, and sometimes we need to indent the nested list, so we have encountered some problems at this time, we can re-compile a print method to deal with indentation, but this increases the amount of code after all. Another method is to modify the existing code, in this way, we do not need to do repetitive work.

 

But how can we control the current print_lol method to implement nested indentation of the list? One way is to use additional parameters to control the print behavior, but what kind of parameters are added? In Python, we provide a built-in function: range ()

 

The fixed number of iterations using range () can be used to specify the number of iterations, and can also be used to generate a list of numbers ranging from 0 to (but not included), as shown below:
>>> for num in range(4):print(num)0123>>>
Okay. Now we can modify the print_lol method to indent it. The modification is as follows:
"Here is the wukong module. The print_lol function is used to output a list." adds a level parameter, the output "def print_lol (movies, level): for item_1 in movies: if isinstance (item_1, list): print_lol (item_1) else: for tab_stop in range (level): print ("\ t", end = '') print (item_1)
Okay. After the modification, press F5 to restart the Python Shell and run the Code:
>>> ================================ RESTART ================================>>> >>> movies = ["The Holy Grail",1975,"Terry Jones & Terry Gilliam",91,                    ["Graham chapman",["Michael Palin","John cleese","Terry Gilliam","Eric Idle & Terry Jones"]],         "The Life of Brain",1979,"Terry Jones",94,            ["Graham chapman",["Michael Palin","John cleese","Terry Gilliam","Eric Idle & Terry Jones"]]]>>> print_lol(movies,0)The Holy Grail1975Terry Jones & Terry Gilliam91Traceback (most recent call last):  File "
 
  ", line 1, in 
  
       print_lol(movies,0)  File "D:\python\wukong\wukong.py", line 6, in print_lol    print_lol(item_1)TypeError: print_lol() missing 1 required positional argument: 'level'>>> 
  
 
A TypeError occurs when we run the program halfway. It is estimated that the print_lol function is incorrect. We looked back at our function and found that the new print_lol function was not used in recursive calls, instead, use the previous: print_lol (item_1). Here we modify it to print_lol (item_1, level). But now let's take another look at it. Is it true that the level is passed in simply? Every time we nest a deep layer, our indentation should be increased. So here we change it to print_lol (item_1, level + 1). The modified code is as follows:
"Here is the wukong module. The print_lol function is used to output a list." adds a level parameter, the output "def print_lol (movies, level): for item_1 in movies: if isinstance (item_1, list): print_lol (item_1, level + 1) else: for tab_stop in range (level): print ("\ t", end = '') print (item_1)
Run the command again and the result is as follows:
>>> ================================ RESTART ================================>>> >>> movies = ["The Holy Grail",1975,"Terry Jones & Terry Gilliam",91,                    ["Graham chapman",["Michael Palin","John cleese","Terry Gilliam","Eric Idle & Terry Jones"]],         "The Life of Brain",1979,"Terry Jones",94,            ["Graham chapman",["Michael Palin","John cleese","Terry Gilliam","Eric Idle & Terry Jones"]]]>>> print_lol(movies,0)The Holy Grail1975Terry Jones & Terry Gilliam91Graham chapmanMichael PalinJohn cleeseTerry GilliamEric Idle & Terry JonesThe Life of Brain1979Terry Jones94Graham chapmanMichael PalinJohn cleeseTerry GilliamEric Idle & Terry Jones>>> 
We can see that all Nesting is indented. Now we can update the code on PyPI.
Update the code on PyPI. After modifying the code locally, We need to update the code on PyPI. Before updating the Code, we must first modify setup. py, change the version to 1.1.0 (previously 1.0.0), as follows:
"Import setup function from python publishing tool" from distutils. core import setup "sets the parameters of the setup function. py_modules is the module to be released. Here is wukong. py, so the value is wukong "" setup (name = 'wukong ', version = '1. 1.0 ', py_modules = ['wukong'], author = 'wukongcode', author_email = 'bjwangzhen @ pku.edu.cn ', url = 'HTTP: // blog.csdn.net/wukongcode ', description = 'a simple printer of nested lists ',)
After that, run python setup. py sdist upload again to update the code on PyPI:


When we use variable parameters to update the code on the PyPI, others will see the code and then download and update their local code, but this causes a problem: previously, print_lol was used to print the list. Because print_lol in the new module requires two parameters, and only one parameter is required in the previous version. Well, I know I am in trouble. What should I do? We can use variable parameters to control the second parameter, that is, you can pass in only one parameter as in the previous version without passing in the second parameter. Does it sound cool?
But how do I use variable parameters? It is very simple. You only need to program the parameter level = 0. If you do not enter the level value, the level will be 0 by default. The modified code is as follows:
"Here is the wukong module. The print_lol function is used to output a list." adds a level parameter, the output "def print_lol (movies, level = 0): for item_1 in movies: if isinstance (item_1, list): print_lol (item_1, level + 1) else: for tab_stop in range (level): print ("\ t", end = '') print (item_1)
Then let's try again:
>>> ================================ RESTART ================================>>> >>> movies = ["The Holy Grail",1975,"Terry Jones & Terry Gilliam",91,                    ["Graham chapman",["Michael Palin","John cleese","Terry Gilliam","Eric Idle & Terry Jones"]],         "The Life of Brain",1979,"Terry Jones",94,            ["Graham chapman",["Michael Palin","John cleese","Terry Gilliam","Eric Idle & Terry Jones"]]]>>> print_lol(movies,0)The Holy Grail1975Terry Jones & Terry Gilliam91Graham chapmanMichael PalinJohn cleeseTerry GilliamEric Idle & Terry JonesThe Life of Brain1979Terry Jones94Graham chapmanMichael PalinJohn cleeseTerry GilliamEric Idle & Terry Jones>>> print_lol(movies)The Holy Grail1975Terry Jones & Terry Gilliam91Graham chapmanMichael PalinJohn cleeseTerry GilliamEric Idle & Terry JonesThe Life of Brain1979Terry Jones94Graham chapmanMichael PalinJohn cleeseTerry GilliamEric Idle & Terry Jones>>> 
We can see that the effects of print_lol (movies, 0) and print_lol (movies) are the same, which solves the problem just now.
The Code on PyPI has been updated here. Now we can't wait to share this function with our friends. Let's continue to modify the setup. py file and change the version to 1.2.0, as shown below:
"Import setup function from python publishing tool" from distutils. core import setup "sets the parameters of the setup function. py_modules is the module to be released. Here is wukong. py, so the value is wukong "" setup (name = 'wukong ', version = '1. 2.0 ', py_modules = ['wukong'], author = 'wukongcode', author_email = 'bjwangzhen @ pku.edu.cn ', url = 'HTTP: // blog.csdn.net/wukongcode ', description = 'a simple printer of nested lists ',)
Then upload:

Controls whether to indent now we have compiled the print_lol code perfectly and can support indentation perfectly. However, someone said: Can I control whether to indent by myself? Sometimes I do not need to indent it. Well, how can we solve another problem? Of course, we can add another parameter to control whether indentation is required. This time we learned to be smart and define this parameter as variable. The Code is as follows:
"Here is the wukong module. The print_lol function is used to output a list." adds a level parameter, the output "def print_lol (movies, indent = False, level = 0): for item_1 in movies: if isinstance (item_1, list ): print_lol (item_1, indent, level + 1) else: if indent: for tab_stop in range (level): print ("\ t", end = '') print (item_1)
Now perform the following test:
>>> ================================ RESTART ================================>>> >>> movies = ["The Holy Grail",1975,"Terry Jones & Terry Gilliam",91,                    ["Graham chapman",["Michael Palin","John cleese","Terry Gilliam","Eric Idle & Terry Jones"]],         "The Life of Brain",1979,"Terry Jones",94,            ["Graham chapman",["Michael Palin","John cleese","Terry Gilliam","Eric Idle & Terry Jones"]]]>>> print_lol(movies)The Holy Grail1975Terry Jones & Terry Gilliam91Graham chapmanMichael PalinJohn cleeseTerry GilliamEric Idle & Terry JonesThe Life of Brain1979Terry Jones94Graham chapmanMichael PalinJohn cleeseTerry GilliamEric Idle & Terry Jones>>> print_lol(movies,True)The Holy Grail1975Terry Jones & Terry Gilliam91Graham chapmanMichael PalinJohn cleeseTerry GilliamEric Idle & Terry JonesThe Life of Brain1979Terry Jones94Graham chapmanMichael PalinJohn cleeseTerry GilliamEric Idle & Terry Jones>>> 
It perfectly achieves what we want. Now, upload it to PyPI again.
The last time you upload the code to PyPI, the current code is perfect, so let's start uploading the code. We also need to first modify the version in setup. py to 1.3.0, as shown below:
"Import setup function from python publishing tool" from distutils. core import setup "sets the parameters of the setup function. py_modules is the module to be released. Here is wukong. py, so the value is wukong "" setup (name = 'wukong ', version = '1. 3.0 ', py_modules = ['wukong'], author = 'wukongcode', author_email = 'bjwangzhen @ pku.edu.cn ', url = 'HTTP: // blog.csdn.net/wukongcode ', description = 'a simple printer of nested lists ',)
Then upload:

In this way, we have compiled a module perfectly and uploaded it to PyPI.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.