[Tutor] capitalize() but only first letter

Erik Price eprice@ptc.com
Wed Feb 5 18:56:20 2003


Alfred Milgrom wrote:
> The simple way to slice a string is to use the string[start:end] construct.
> The start and end parameters are optional (but remember we start at 0), 
> so you can have:
> 
> s = 'everything But The kitchen siNk'
> 
> s[3:7] means slice from 4th to 8th letter (inclusive)
>         'ryth'
> s[:-1] means everything from the beginning except the last letter
>         'everything But The kitchen siN'
> s[1:] means everything except the first letter
>         'verything But The kitchen siNk'
> 
> HTH
> Fred Milgrom

Yes, thanks Fred.  That is exactly what I was looking for.  I had tried 
s[1-] and s[1...] but I had forgotten that it was the colon.

Here is the script I ended up with... I'd like to make some additions to 
it, so that if I specify an overloaded setter method it calls the "true" 
setter method (the one that actually modifies the property), but it's 
been a long day and I'm going to go home now.


Erik


#!/usr/bin/python

# gettersetter.py
# creates getters and setters for a JavaBean
# from property type and name arguments

# Usage: gettersetter.py -b|-s|-g type propertyName
# Example: gettersetter.py -b String userId

def usage():
     """ returns the usage for gettersetter.py """
     usage = "Usage: gettersetter.py -b|-s|-g type propertyName" + "\n" \
             + "Example: gettersetter.py -b String userId\n"
     return usage


def cap_camelcase(target):
     """ capitalizes only first character of a string """
     return target[0].capitalize() + target[1:]


def make_getter(propertytype, propertyname):
     """ returns a getter method definition with javadoc comment """
     pn = cap_camelcase(propertyname)
     methodname = "get" + pn
     comment = "/**\n * returns the <code>" + propertyname \
               + "</code> property\n */\n"
     getter = "public " + propertytype + " " + methodname + "() {\n" \
              + "    return this." + propertyname + ";\n" \
              + "}\n"
     return comment + getter


def make_setter(propertytype, propertyname):
     """ returns a setter method definition with javadoc comment """
     pn = cap_camelcase(propertyname)
     methodname = "set" + pn
     comment = "/**\n * sets the <code>" + propertyname \
               + "</code> property\n */\n"
     setter = "public void " + methodname + "(" \
              + propertytype + " " + propertyname + ") {\n" \
              + "    this." + propertyname + " = " + propertyname \
              + ";\n" + "}\n"
     return comment + setter

if __name__ == "__main__":
     import sys
     if len(sys.argv) < 4:   # 3, includes script name itself
         print usage()
         sys.exit(1)

     opt = sys.argv[1]
     propertytype = sys.argv[2]
     propertyname = sys.argv[3]

     if opt == "-s":
         print make_setter(propertytype, propertyname)
     elif opt == "-g":
         print make_getter(propertytype, propertyname)
     elif opt == "-b":
         print make_getter(propertytype, propertyname)
         print make_setter(propertytype, propertyname)
     else:
         print usage()
         sys.exit(1)