|
From: Michael F. <fuz...@vo...> - 2010-03-10 15:36:36
|
On 10/03/2010 15:03, Tom Vaughan wrote: > On Wed, Mar 10, 2010 at 03:33, Michael Foord<fuz...@vo...> wrote: > >> On 09/03/2010 14:33, Tom Vaughan wrote: >> >> I mean how do I make sure a hex string is written to the .ini file, not the >> decimal equivalent? >> >> When it is set in the ConfigObj how do you know if it is a hex string rather >> than decimal? >> > Because it starts with "0x". This is my own convention used in just my > own apps. I dictate what goes into the .ini's. > > Right, that is pretty much the standard convention, it is used by Python as well. >> If you want to allow decimal values (integer type) to be set, but write them >> out as hex strings then you will need to 'wrap' the configobj instance in >> something that converts the integers to hex strings when they are set, and >> does the reverse when they are fetched. >> > This is what my "Preferences" object does below. At least the first > part. It converts the hex string to an int. Another way I could have > done this is with "traverse". > > What do you mean by "fetched"? I suppose I could overload the write > method. But how could I tell if a value has been converted using > "conv_hex" (or any other method for that matter) via "validate"? > > What I'm suggesting is that instead of getting and setting values directly with the configobj instance you have a wrapper class or functions that do the translation. The other way would be to use the 'walk' method to do your traversal and transform (for example) all integers to hex strings prior to writing. Another approach would be to use validation and have two validators - one that converts hex strings to integers after you have read the configobj and another to convert back again prior to writing. All the best, Michael Foord > Thanks. > > -Tom > > > >> All the best, >> >> Michael >> >> >> Sent from my iPhone >> On Mar 9, 2010, at 6:23, David Hostetler<neg...@gm...> wrote: >> >> you mean... how do you print a hex value as a string? >> >> '%#x' % (val) >> >> Omit the '#' if you don't want the leading '0x'. >> >> >> http://docs.python.org/library/string.html#format-specification-mini-language >> >> >> -David >> >> >> On Mon, Mar 8, 2010 at 15:36, Tom Vaughan<to...@so...> wrote: >> >>> Hi, >>> >>> I know how to convert a hex string to an int. But how would I write >>> out the hex string? Thanks. >>> >>> -Tom >>> >>> >>> class Preferences(ConfigObj): >>> >>> def conv_hex(value): >>> return int(value, 16) >>> >>> fdict = { >>> 'hex': conv_hex, >>> } >>> >>> def __init__(self, prefsname): >>> specsname = prefsname + '.spec' >>> if os.path.exists(specsname): >>> ConfigObj.__init__(self, prefsname, >>> configspec=specsname) >>> self.validate(Validator(self.fdict)) >>> else: >>> ConfigObj.__init__(self, prefsname) >>> >>> >>> ------------------------------------------------------------------------------ >>> Download Intel® Parallel Studio Eval >>> Try the new software tools for yourself. Speed compiling, find bugs >>> proactively, and fine-tune applications for parallel performance. >>> See why Intel Parallel Studio got high marks during beta. >>> http://p.sf.net/sfu/intel-sw-dev >>> _______________________________________________ >>> Configobj-develop mailing list >>> Con...@li... >>> https://lists.sourceforge.net/lists/listinfo/configobj-develop >>> >> ------------------------------------------------------------------------------ >> Download Intel® Parallel Studio Eval >> Try the new software tools for yourself. Speed compiling, find bugs >> proactively, and fine-tune applications for parallel performance. >> See why Intel Parallel Studio got high marks during beta. >> http://p.sf.net/sfu/intel-sw-dev >> >> _______________________________________________ >> Configobj-develop mailing list >> Con...@li... >> https://lists.sourceforge.net/lists/listinfo/configobj-develop >> >> ------------------------------------------------------------------------------ >> Download Intel® Parallel Studio Eval >> Try the new software tools for yourself. Speed compiling, find bugs >> proactively, and fine-tune applications for parallel performance. >> See why Intel Parallel Studio got high marks during beta. >> http://p.sf.net/sfu/intel-sw-dev >> >> _______________________________________________ >> Configobj-develop mailing list >> Con...@li... >> https://lists.sourceforge.net/lists/listinfo/configobj-develop >> >> >> -- >> http://www.ironpythoninaction.com/ >> http://www.voidspace.org.uk/blog >> >> READ CAREFULLY. By accepting and reading this email you agree, on behalf of >> your employer, to release me from all obligations and waivers arising from >> any and all NON-NEGOTIATED agreements, licenses, terms-of-service, >> shrinkwrap, clickwrap, browsewrap, confidentiality, non-disclosure, >> non-compete and acceptable use policies (”BOGUS AGREEMENTS”) that I have >> entered into with your employer, its partners, licensors, agents and >> assigns, in perpetuity, without prejudice to my ongoing rights and >> privileges. You further represent that you have the authority to release me >> from any BOGUS AGREEMENTS on behalf of your employer. >> >> >> > > > -- http://www.ironpythoninaction.com/ |