[Tutor] Changing the Attribute of a Variable

Marc Tompkins marc.tompkins at gmail.com
Tue Feb 17 19:11:00 CET 2009


On Tue, Feb 17, 2009 at 4:44 AM, Wayne Watson
<sierra_mtnview at sbcglobal.net>wrote:

> Note that the diagnostic output in the image shows attributeError: 'str'
> object has no attribute 'strftime'.
>
> Let me see if I clarify what's really going on by including some of the
> code.
>

Everything in Python - both variables and code - is an object.  Objects have
attributes - data, basically - and methods - functions - associated with
them.  (As Wesley pointed out, since pieces of code are also objects,
methods are attributes too.)

However, not all objects have the same attributes or methods associated with
them!  datetime.time objects have a "strftime" method, which, when called,
returns a string representation of the time.  String objects do not have any
such method or data attribute, hence the error.

You're showing us both too much code and too little -
      print "wtw self.stop_time", self.stop_time, type(self.stop_time)
      # set in GUI as datetime.time(6,0,0)
      # HEY wtw self.stop_time.strftime("%H:%M:%S")
      set_loc_dict[ "stop_time" ] = self.stop_time.strftime("%H:%M:%S")
<<----problem, see image in first post

I'd like to see the output of that "print" - I'm pretty sure that "type"
will return "str", not "time".

On another front, have you considered ConfigObj (
http://www.voidspace.org.uk/python/modules.shtml#configobj)?  When I first
came to Python, I wrote myself a config-file handler (serializing to XML,
actually) and it worked OK - but everything was so much work!  I discovered
ConfigObj and life is good.  Here's a quick example from one of my early
programs I refactored to use ConfigObj (NOT as a shining example of code,
but just to show the power):

from configobj import ConfigObj
> from validate import Validator

...

>     cfgFileName = os.getcwd() + os.sep + 'fsr_1500.ini'
>     tmpStr = """
>     npiXMLFile = string(default="npiMap.XML")
>     UCFformLength = integer(min=50, max=80, default=66)
>     FIformLength = integer(min=50, max=80, default=64)
>     OutformLength = integer(min=50, max=80, default=64)
>     IncludeLegacy = boolean(default=False)
>     TopLeft = int_list(min=2, max=2)
>     BottomRight = int_list(min=2, max=2)
>     FIHist = string_list(default=None)
>     UCFHist = string_list(default=None)
>     OutHist = string_list(default=None)
>     LastRunUCF = boolean(default=True)
>     LastRunPrinter = boolean(default=False)
>     detailLeft = integer(min=0, max=80, default=0)
>     detailTo = integer(min=0, max=80, default=9)
>     detailPOS = integer(min=0, max=80, default=19)
>     detailCode = integer(min=0, max=80, default=25)
>     detailMods = integer(min=0, max=80, default=32)
>     detailDiags = integer(min=0, max=80, default=44)
>     detailCharge = integer(min=0, max=80, default=49)
>     detailUnits = integer(min=0, max=80, default=58)
>     detailEPSDT = integer(min=0, max=80, default=62)
>     detailEMG = integer(min=0, max=80, default=22)
>     detailID = integer(min=0, max=80, default=67)
>     bodyLeftBlock = integer(min=0, max=80, default=0)
>     bodyMidBlock = integer(min=0, max=80, default=24)
>     bodyRightBlock = integer(min=0, max=80, default=49)
>     bodyLabelEdge = integer(min=0, max=80, default=40)
>     ConfirmSuccess = boolean(default=True)
>     """
>     cfgSpec = StringIO.StringIO(tmpStr)
>     cfgFile = ConfigObj(cfgFileName,
>                 configspec=cfgSpec, raise_errors=True,
> write_empty_values=True,
>                 create_empty=True, indent_type='    ', list_values=True)
>     vtor = Validator()

...

>     cfgFile['TopLeft'] = data.GetMarginTopLeft()  # writing a couple of
> values
>     cfgFile['BottomRight'] = data.GetMarginBottomRight()
>
 ...

>     test = cfgFile.validate(Global.vtor, copy=True)
>     cfgFile.write()
>

Looking at that, I see a few things I want to clean up.  That's the danger
(and advantage) of exposing your own code to public scrutiny...

-- 
www.fsrtechnologies.com
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/tutor/attachments/20090217/f27104b7/attachment.htm>


More information about the Tutor mailing list