Package konval :: Module impl
[hide private]
[frames] | no frames]

Source Code for Module konval.impl

  1  #! /usr/bin/env python 
  2  # -*- coding: utf-8 -*- 
  3  """ 
  4  Internal implementation utilities and details. 
  5   
  6  This module contains various odds and ends to make development easier. None of 
  7  code within should be relied upon as it is subject to change at a whim. 
  8  """ 
  9   
 10  __docformat__ = 'restructuredtext en' 
 11   
 12   
 13  ### IMPORTS ### 
 14   
 15  import types 
 16   
 17  import defs 
 18   
 19  __all__ = [ 
 20          'make_list', 
 21          'make_canonical', 
 22  ] 
 23   
 24   
 25  ### CONSTANTS & DEFINES ### 
 26   
 27  ### IMPLEMENTATION ###   
 28   
29 -def make_list (x):
30 """ 31 If this isn't a list, make it one. 32 33 :Parameters: 34 x : list, tuple, other 35 a sequence, or a single element to be placed in a sequence 36 37 :Returns: 38 Either the original a parameter if a sequence, or the parameter placed in 39 a list. 40 41 Syntactic sugar for allowing method calls to be single elements or lists of 42 elements. 43 44 For example:: 45 46 >>> make_list (1) 47 [1] 48 >>> make_list ('1') 49 ['1'] 50 >>> make_list ([1, 2]) 51 [1, 2] 52 >>> make_list ((1, 2)) 53 (1, 2) 54 55 """ 56 # TODO: should be a more general way of doing this 57 if (type (x) not in (types.ListType, types.TupleType)): 58 x = [x] 59 return x
60 61
62 -def make_canonical (value):
63 """ 64 Clean-up minor string variants to a single form. 65 66 :Parameters: 67 value : string 68 the string to be sanitized 69 70 :Returns: 71 The parameter cleaned up 72 73 This is syntactic sugar for mapping minor string variants (mostly 74 whitespace and punctuation flourishes, as you expect in user input or free 75 text) to a single canonical from. This consists of trimming flanking spaces, 76 making the string uppercase, and collapsing all internal spaces / hyphens / 77 underscores to a single underscore. 78 79 For example:: 80 81 >>> make_canonical ('abc') 82 'ABC' 83 >>> make_canonical (' CD-EF ') 84 'CD_EF' 85 >>> make_canonical ('H-IJ- _K') 86 'H_IJ_K' 87 88 """ 89 # TODO: all internal punctuation 90 # TODO: trim flanking punctuation 91 new_val = value.strip().upper() 92 new_val = defs.CANON_SPACE_RE.sub ('_', new_val) 93 return new_val
94 95 96 97 ## DEBUG & TEST ### 98 99 if __name__ == "__main__": 100 import doctest 101 doctest.testmod() 102 103 104 105 ### END ####################################################################### 106