@@ -1465,13 +1465,14 @@ Argparse
1465
1465
--------
1466
1466
``` python
1467
1467
from argparse import ArgumentParser
1468
- parser = ArgumentParser(description = ' calculate X to the power of Y' )
1469
- group = parser.add_mutually_exclusive_group()
1468
+ desc = ' calculate X to the power of Y'
1469
+ parser = ArgumentParser(description = desc)
1470
+ group = parser.add_mutually_exclusive_group()
1470
1471
group.add_argument(' -v' , ' --verbose' , action = ' store_true' )
1471
1472
group.add_argument(' -q' , ' --quiet' , action = ' store_true' )
1472
1473
parser.add_argument(' x' , type = int , help = ' the base' )
1473
1474
parser.add_argument(' y' , type = int , help = ' the exponent' )
1474
- args = parser.parse_args()
1475
+ args = parser.parse_args()
1475
1476
answer = args.x ** args.y
1476
1477
1477
1478
if args.quiet:
@@ -1490,8 +1491,8 @@ Table
1490
1491
# $ pip3 install tabulate
1491
1492
from csv import reader
1492
1493
from tabulate import tabulate
1493
- with open (< filename> , encoding = ' utf-8' , newline = ' ' ) as csv_file :
1494
- reader = reader(csv_file , delimiter = ' ;' )
1494
+ with open (< filename> , encoding = ' utf-8' , newline = ' ' ) as file :
1495
+ reader = reader(file , delimiter = ' ;' )
1495
1496
headers = [a.title() for a in next (reader)]
1496
1497
print (tabulate(reader, headers))
1497
1498
```
@@ -1527,8 +1528,10 @@ Image
1527
1528
# $ pip3 install pillow
1528
1529
from PIL import Image
1529
1530
width, height = 100 , 100
1530
- img = Image.new(' L' , (width, height), ' white' )
1531
- img.putdata([255 * a/ (width* height) for a in range (width* height)])
1531
+ img = Image.new(' L' , (width, height), ' white' )
1532
+ size = width * height
1533
+ pixels = [255 * a/ size for a in range (size)]
1534
+ img.putdata(pixels)
1532
1535
img.save(' out.png' )
1533
1536
```
1534
1537
@@ -1639,9 +1642,10 @@ def odds_handler(sport):
1639
1642
``` python
1640
1643
# $ pip3 install requests
1641
1644
>> > import requests
1642
- >> > url = ' http://localhost:8080/odds/football'
1643
- >> > r = requests.post(url, data = {' team' : ' arsenal f.c.' })
1644
- >> > r.json()
1645
+ >> > url = ' http://localhost:8080/odds/football'
1646
+ >> > data = {' team' : ' arsenal f.c.' }
1647
+ >> > response = requests.post(url, data = data)
1648
+ >> > response.json()
1645
1649
[' arsenal f.c.' , 2.44 , 3.29 ]
1646
1650
```
1647
1651
@@ -1698,11 +1702,11 @@ Line # Hits Time Per Hit % Time Line Contents
1698
1702
``` python
1699
1703
# $ pip3 install pycallgraph
1700
1704
from pycallgraph import output, PyCallGraph
1701
- from datetime import datetime
1702
- graph = output.GraphvizOutput()
1705
+ from datetime import datetime
1703
1706
time_str = datetime.now().strftime(' %Y%m%d %H%M%S' )
1704
- graph.output_file = f ' profile- { time_str} .png '
1705
- with PyCallGraph(output = graph):
1707
+ filename = f ' profile- { time_str} .png '
1708
+ drawer = output.GraphvizOutput(output_file = filename)
1709
+ with PyCallGraph(output = drawer):
1706
1710
< code_to_be_profiled>
1707
1711
```
1708
1712
0 commit comments