Advertisement
Guest User

Untitled

a guest
Jan 26th, 2015
187
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.66 KB | None | 0 0
  1. #!/usr/bin/env python
  2.  
  3. """case.py
  4.  
  5. A utility for converting strings to different cases.
  6.  
  7. Usage info is autogenerated by the argparse module and can be accessed
  8. by running the program with the '-h' option.
  9. """
  10.  
  11. import sys
  12. import argparse
  13. import string
  14.  
  15. def get_args():
  16. """Obtain the arguments entered for our program on the command line."""
  17. # Define all of our arguments and their help messages
  18. description = ("case - A utility for converting strings to different"
  19. " cases.")
  20. epilog = ("Note: Leading, trailing, and multiple whitespace is not"
  21. " preserved for -C/-t.")
  22. parser = argparse.ArgumentParser(add_help=False, description=description,
  23. epilog=epilog)
  24.  
  25. # If we're running interactively, require a string
  26. if sys.stdin.isatty():
  27. parser.add_argument("string", help="The string to convert.")
  28.  
  29. cases = parser.add_mutually_exclusive_group(required=True)
  30. cases.add_argument("-c", "--capitalize", action="store_true",
  31. help="Capitalize the first letter of the string.")
  32. cases.add_argument("-C", "--capwords", action="store_true",
  33. help="Capitalize every word.")
  34. cases.add_argument("-t", "--titlecase", action="store_true",
  35. help="Capitalize words according to titlecase"
  36. " rules.")
  37. cases.add_argument("-u", "--uppercase", action="store_true",
  38. help="Make every letter uppercase.")
  39. cases.add_argument("-l", "--lowercase", action="store_true",
  40. help="Make every letter lowercase.")
  41. cases.add_argument("-s", "--swapcase", action="store_true",
  42. help="Make lowercase letters uppercase and"
  43. " vice-versa.")
  44.  
  45. parser.add_argument("-h", "--help", action="help",
  46. help="Print this help message.")
  47.  
  48. # Display a help message and exit if the program is run without arguments
  49. if len(sys.argv) == 1:
  50. parser.print_help()
  51. sys.exit(1)
  52.  
  53. args = parser.parse_args()
  54.  
  55. return args
  56.  
  57. def case_to_title(input_string):
  58. """Capitalize principle words."""
  59. # Build a *simplified* list of non-principles
  60. articles = ["a", "an", "the"]
  61. conjunctions = ["and", "but", "or"]
  62. prepositions = ["at", "by", "of"]
  63.  
  64. excludes = articles + conjunctions + prepositions
  65. output_string = []
  66.  
  67. for index, word in enumerate(input_string.split()):
  68. # Always capitalize the first word
  69. if index == 0:
  70. output_string.append(word.capitalize())
  71. else:
  72. lowercase = word.lower()
  73. if lowercase in excludes:
  74. output_string.append(lowercase)
  75. else:
  76. output_string.append(word.capitalize())
  77.  
  78. return " ".join(output_string)
  79.  
  80. def process_string(args):
  81. """Call the function corresponding to the chosen case and print the
  82. processed string.
  83. """
  84. if sys.stdin.isatty():
  85. input_string = args.string
  86. else:
  87. input_string = sys.stdin.read()
  88.  
  89. if args.capitalize:
  90. output_string = input_string.capitalize()
  91. elif args.capwords:
  92. output_string = string.capwords(input_string)
  93. elif args.titlecase:
  94. output_string = case_to_title(input_string)
  95. elif args.uppercase:
  96. output_string = input_string.upper()
  97. elif args.lowercase:
  98. output_string = input_string.lower()
  99. elif args.swapcase:
  100. output_string = input_string.swapcase()
  101.  
  102. print output_string
  103. sys.exit(0)
  104.  
  105. def main():
  106. """Get program arguments and process them."""
  107. args = get_args()
  108. process_string(args)
  109.  
  110. if __name__ == "__main__":
  111. main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement