Browse Source

Libraries

pull/10/head
Jure Šorn 6 years ago
parent
commit
7f31af5603
1 changed files with 18 additions and 14 deletions
  1. 32
      README.md

32
README.md

@ -1465,13 +1465,14 @@ Argparse
-------- --------
```python ```python
from argparse import ArgumentParser from argparse import ArgumentParser
parser = ArgumentParser(description='calculate X to the power of Y') desc = 'calculate X to the power of Y'
group = parser.add_mutually_exclusive_group() parser = ArgumentParser(description=desc)
group = parser.add_mutually_exclusive_group()
group.add_argument('-v', '--verbose', action='store_true') group.add_argument('-v', '--verbose', action='store_true')
group.add_argument('-q', '--quiet', action='store_true') group.add_argument('-q', '--quiet', action='store_true')
parser.add_argument('x', type=int, help='the base') parser.add_argument('x', type=int, help='the base')
parser.add_argument('y', type=int, help='the exponent') parser.add_argument('y', type=int, help='the exponent')
args = parser.parse_args() args = parser.parse_args()
answer = args.x ** args.y answer = args.x ** args.y
if args.quiet: if args.quiet:
@ -1490,8 +1491,8 @@ Table
# $ pip3 install tabulate # $ pip3 install tabulate
from csv import reader from csv import reader
from tabulate import tabulate from tabulate import tabulate
with open(<filename>, encoding='utf-8', newline='') as csv_file: with open(<filename>, encoding='utf-8', newline='') as file:
reader = reader(csv_file, delimiter=';') reader = reader(file, delimiter=';')
headers = [a.title() for a in next(reader)] headers = [a.title() for a in next(reader)]
print(tabulate(reader, headers)) print(tabulate(reader, headers))
``` ```
@ -1527,8 +1528,10 @@ Image
# $ pip3 install pillow # $ pip3 install pillow
from PIL import Image from PIL import Image
width, height = 100, 100 width, height = 100, 100
img = Image.new('L', (width, height), 'white') img = Image.new('L', (width, height), 'white')
img.putdata([255 * a/(width*height) for a in range(width*height)]) size = width * height
pixels = [255 * a/size for a in range(size)]
img.putdata(pixels)
img.save('out.png') img.save('out.png')
``` ```
@ -1639,9 +1642,10 @@ def odds_handler(sport):
```python ```python
# $ pip3 install requests # $ pip3 install requests
>>> import requests >>> import requests
>>> url = 'http://localhost:8080/odds/football' >>> url = 'http://localhost:8080/odds/football'
>>> r = requests.post(url, data={'team': 'arsenal f.c.'}) >>> data = {'team': 'arsenal f.c.'}
>>> r.json() >>> response = requests.post(url, data=data)
>>> response.json()
['arsenal f.c.', 2.44, 3.29] ['arsenal f.c.', 2.44, 3.29]
``` ```
@ -1698,11 +1702,11 @@ Line # Hits Time Per Hit % Time Line Contents
```python ```python
# $ pip3 install pycallgraph # $ pip3 install pycallgraph
from pycallgraph import output, PyCallGraph from pycallgraph import output, PyCallGraph
from datetime import datetime from datetime import datetime
graph = output.GraphvizOutput()
time_str = datetime.now().strftime('%Y%m%d%H%M%S') time_str = datetime.now().strftime('%Y%m%d%H%M%S')
graph.output_file = f'profile-{time_str}.png' filename = f'profile-{time_str}.png'
with PyCallGraph(output=graph): drawer = output.GraphvizOutput(output_file=filename)
with PyCallGraph(output=drawer):
<code_to_be_profiled> <code_to_be_profiled>
``` ```

|||||||
100:0
Loading…
Cancel
Save