|
| 1 | +#!/usr/bin/env python3 |
| 2 | + |
| 3 | +'''Item 4 from Effective Python''' |
| 4 | + |
| 5 | + |
| 6 | +# Example 1 |
| 7 | +from urllib.parse import parse_qs |
| 8 | +MY_VALUES = parse_qs('red=5&blue=0&green=', keep_blank_values=True) |
| 9 | +print('Example 1:\n==========') |
| 10 | +print(repr(MY_VALUES)) |
| 11 | + |
| 12 | + |
| 13 | +# Example 2 |
| 14 | +print('\nExample 2:\n==========') |
| 15 | +print('Red: ', MY_VALUES.get('red')) |
| 16 | +print('Green: ', MY_VALUES.get('green')) |
| 17 | +print('Opacity: ', MY_VALUES.get('opactity')) |
| 18 | + |
| 19 | + |
| 20 | +# Example 3 |
| 21 | +# For query string 'red=5&blue=0&green=' |
| 22 | +RED = MY_VALUES.get('red', [''])[0] or 0 |
| 23 | +GREEN = MY_VALUES.get('green', [''])[0] or 0 |
| 24 | +OPACITY = MY_VALUES.get('opacity', [''])[0] or 0 |
| 25 | +print('\nExample 3:\n==========') |
| 26 | +print('Red: %r' % RED) |
| 27 | +print('Green: %r' % GREEN) |
| 28 | +print('Opacity: %r' % OPACITY) |
| 29 | + |
| 30 | + |
| 31 | +# Example 4 |
| 32 | +RED = int(MY_VALUES.get('red', [''])[0] or 0) |
| 33 | +GREEN = int(MY_VALUES.get('green', [''])[0] or 0) |
| 34 | +OPACITY = int(MY_VALUES.get('opacity', [''])[0] or 0) |
| 35 | +print('\nExample 4:\n==========') |
| 36 | +print('Red: %r' % RED) |
| 37 | +print('Green: %r' % GREEN) |
| 38 | +print('Opacity: %r' % OPACITY) |
| 39 | + |
| 40 | + |
| 41 | +# Example 5 |
| 42 | +RED = MY_VALUES.get('red', ['']) |
| 43 | +RED = int(RED[0]) if RED[0] else 0 |
| 44 | +GREEN = MY_VALUES.get('green', ['']) |
| 45 | +GREEN = int(GREEN[0]) if GREEN[0] else 0 |
| 46 | +OPACITY = MY_VALUES.get('opacity', ['']) |
| 47 | +OPACITY = int(OPACITY[0]) if OPACITY[0] else 0 |
| 48 | +print('\nExample 5:\n==========') |
| 49 | +print('Red: %r' % RED) |
| 50 | +print('Green: %r' % GREEN) |
| 51 | +print('Opacity: %r' % OPACITY) |
| 52 | + |
| 53 | + |
| 54 | +# Example 6 |
| 55 | +RED = MY_VALUES.get('red', ['']) |
| 56 | +if RED[0]: |
| 57 | + RED = int(RED[0]) |
| 58 | +else: |
| 59 | + RED = 0 |
| 60 | + |
| 61 | +GREEN = MY_VALUES.get('green', ['']) |
| 62 | +if GREEN[0]: |
| 63 | + GREEN = int(GREEN[0]) |
| 64 | +else: |
| 65 | + GREEN = 0 |
| 66 | + |
| 67 | +OPACITY = MY_VALUES.get('opacity', ['']) |
| 68 | +if OPACITY[0]: |
| 69 | + OPACITY = int(OPACITY[0]) |
| 70 | +else: |
| 71 | + OPACITY = 0 |
| 72 | + |
| 73 | +print('\nExample 6:\n==========') |
| 74 | +print('Red: %r' % RED) |
| 75 | +print('Green: %r' % GREEN) |
| 76 | +print('Opacity: %r' % OPACITY) |
| 77 | + |
| 78 | + |
| 79 | +# Example 7 |
| 80 | +def get_first_int(values, key, default=0): |
| 81 | + '''Writing a helper function is the way to go, especially if you need to |
| 82 | + use this logic repeatedly.''' |
| 83 | + found = values.get(key, ['']) |
| 84 | + if found[0]: |
| 85 | + found = int(found[0]) |
| 86 | + else: |
| 87 | + found = default |
| 88 | + return found |
| 89 | + |
| 90 | + |
| 91 | +# Example 8 |
| 92 | +RED = get_first_int(MY_VALUES, 'red') |
| 93 | +GREEN = get_first_int(MY_VALUES, 'green') |
| 94 | +OPACITY = get_first_int(MY_VALUES, 'opacity') |
| 95 | +print('\nExample 8:\n==========') |
| 96 | +print('Red: %r' % RED) |
| 97 | +print('Green: %r' % GREEN) |
| 98 | +print('Opacity: %r' % OPACITY) |
0 commit comments