|
| 1 | +#!/usr/bin/env python3 |
| 2 | + |
| 3 | +# Copyright 2014 Brett Slatkin, Pearson Education Inc. |
| 4 | +# |
| 5 | +# Licensed under the Apache License, Version 2.0 (the "License"); |
| 6 | +# you may not use this file except in compliance with the License. |
| 7 | +# You may obtain a copy of the License at |
| 8 | +# |
| 9 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | +# |
| 11 | +# Unless required by applicable law or agreed to in writing, software |
| 12 | +# distributed under the License is distributed on an "AS IS" BASIS, |
| 13 | +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | +# See the License for the specific language governing permissions and |
| 15 | +# limitations under the License. |
| 16 | + |
| 17 | +# Preamble to mimick book environment |
| 18 | +import logging |
| 19 | +from pprint import pprint |
| 20 | +from sys import stdout as STDOUT |
| 21 | + |
| 22 | + |
| 23 | +# Example 1 |
| 24 | +from urllib.parse import parse_qs |
| 25 | +my_values = parse_qs('red=5&blue=0&green=', |
| 26 | + keep_blank_values=True) |
| 27 | +print(repr(my_values)) |
| 28 | + |
| 29 | + |
| 30 | +# Example 2 |
| 31 | +print('Red: ', my_values.get('red')) |
| 32 | +print('Green: ', my_values.get('green')) |
| 33 | +print('Opacity: ', my_values.get('opacity')) |
| 34 | + |
| 35 | + |
| 36 | +# Example 3 |
| 37 | +# For query string 'red=5&blue=0&green=' |
| 38 | +red = my_values.get('red', [''])[0] or 0 |
| 39 | +green = my_values.get('green', [''])[0] or 0 |
| 40 | +opacity = my_values.get('opacity', [''])[0] or 0 |
| 41 | +print('Red: %r' % red) |
| 42 | +print('Green: %r' % green) |
| 43 | +print('Opacity: %r' % opacity) |
| 44 | + |
| 45 | + |
| 46 | +# Example 4 |
| 47 | +red = int(my_values.get('red', [''])[0] or 0) |
| 48 | +green = int(my_values.get('green', [''])[0] or 0) |
| 49 | +opacity = int(my_values.get('opacity', [''])[0] or 0) |
| 50 | +print('Red: %r' % red) |
| 51 | +print('Green: %r' % green) |
| 52 | +print('Opacity: %r' % opacity) |
| 53 | + |
| 54 | + |
| 55 | +# Example 5 |
| 56 | +red = my_values.get('red', ['']) |
| 57 | +red = int(red[0]) if red[0] else 0 |
| 58 | +green = my_values.get('green', ['']) |
| 59 | +green = int(green[0]) if green[0] else 0 |
| 60 | +opacity = my_values.get('opacity', ['']) |
| 61 | +opacity = int(opacity[0]) if opacity[0] else 0 |
| 62 | +print('Red: %r' % red) |
| 63 | +print('Green: %r' % green) |
| 64 | +print('Opacity: %r' % opacity) |
| 65 | + |
| 66 | + |
| 67 | +# Example 6 |
| 68 | +green = my_values.get('green', ['']) |
| 69 | +if green[0]: |
| 70 | + green = int(green[0]) |
| 71 | +else: |
| 72 | + green = 0 |
| 73 | +print('Green: %r' % green) |
| 74 | + |
| 75 | + |
| 76 | +# Example 7 |
| 77 | +def get_first_int(values, key, default=0): |
| 78 | + found = values.get(key, ['']) |
| 79 | + if found[0]: |
| 80 | + found = int(found[0]) |
| 81 | + else: |
| 82 | + found = default |
| 83 | + return found |
| 84 | + |
| 85 | + |
| 86 | +# Example 8 |
| 87 | +green = get_first_int(my_values, 'green') |
| 88 | +print('Green: %r' % green) |
0 commit comments