Page 1 of 1

snippet of formatting code

Posted: Tue Mar 11, 2014 5:06 pm
by lilzz

Code: Select all

M_JSON  = "application/json"
if result != None:
            if hasattr(func, "contentType"):
                contentType = func.contentType
                if contentType == M_JSON:
                    response = types.jsonDumps(result)
                else:
                    response = func.format % result
            else:
                response = result
I understand the first one is displaying result in json but what's fund.format % result?

Code: Select all

 
_MAPPING = [[], [], []]
_MAPPING[1] = ["V33", "V50", 0, "V50", 1, "GND", 4, 14, "GND", 15, 17, 18, 21, "GND", 22, 23, "V33", 24, 10, "GND", 9, 25, 11, 8, "GND", 7]
_MAPPING[2] = ["V33", "V50", 2, "V50", 3, "GND", 4, 14, "GND", 15, 17, 18, 27, "GND", 22, 23, "V33", 24, 10, "GND", 9, 25, 11, 8, "GND", 7]

MAPPING = _MAPPING[BOARD_REVISION]


elif relativePath == "map":
            json = "%s" % MAPPING
            json = json.replace("'", '"')
            return (200, json, M_JSON)
what's json="%s" % MAPPING? example for above?


finally

Code: Select all

import re
    rc = re.compile("Revision\s*:\s(.*)\n")
        
does re stands for revision, and this compile function gets the revision of os?

Re: snippet of formatting code

Posted: Tue Mar 11, 2014 5:35 pm
by elParaguayo
"%" is used in string formatting. See: http://docs.python.org/2/library/stdtyp ... operations

My guess would be that "func.format" is a string that contains a formatting symbol (e.g. "%s") and this symbol is replaced by the value of "result"

"json="%s" % MAPPING" sets the value of "json" to be the string representation of "MAPPING". It's the same as "json=str(MAPPING)"

re stands for regular expression - the compile function builds a regular expression from the string in the brackets.