3

Code adapted from here:

#from 'foo_bar' to 'Foo.Bar'
def lower_case_underscore_to_camel_case(self, string):
  print string
  class_ = string.__class__
  return class_.join('.', map(class_.capitalize, string.split('_')))

Output:

client_area
TypeError: descriptor 'join' requires a 'unicode' object but received a 'str'

Especially disappointing since the source code states:

"""Convert string or unicode from lower-case underscore to camel-case"""

How to fix this?


Easy fix:

return str.join('.', map(class_.capitalize, string.split('_')))

Could anyone explain me the overall process?

apneadiving
  • 114,565
  • 26
  • 219
  • 213
  • The "easy fix" you have just intruduced is not actually a fix. What part of the process you do not understand? – Tadeck Nov 27 '12 at 01:21

1 Answers1

3

The code seems to introduce unnecessary complexity, but you can do it like that:

#from 'foo_bar' to 'FooBar'
def lower_case_underscore_to_camel_case(self, string):
  print string
  class_ = string.__class__
  return class_.join(class_('.'), map(class_.capitalize, string.split('_')))

And you could actually shorten the last line to be:

return class_('.').join(map(class_.capitalize, string.split('_')))

Also, judging from the code, you will receive something like "Foo.Bar" (notice a dot) from "foo_bar".

Tadeck
  • 132,510
  • 28
  • 152
  • 198