Regex loop question

Peter Otten __peter__ at web.de
Mon Mar 3 06:49:48 EST 2008


Mike P wrote:

> Hi Experts,
> 
> I've written a peice of code that works fine and fits, and passes
> values into a peice of SPSS code, the problem is that it is not
> dynamic, and so i though about how i can make it dynamic, (other code
> may not have upto 10 some may have more) and came up with regex for an
> idea, but i can't seem to make it work, can anyone offer any advice?
> Below is current working code
> 
> time_variables = {"ActivityTime": "Activity_Time",
> "ExposureTime_Last":"Exposure_Time_1",
> "ExposureTime_Last2":"Exposure_Time_2",
> "ExposureTime_Last3":"Exposure_Time_3",
> "ExposureTime_Last4":"Exposure_Time_4",
> "ExposureTime_Last5":"Exposure_Time_5",
> "ExposureTime_Last6":"Exposure_Time_6",
> "ExposureTime_Last7":"Exposure_Time_7",
> "ExposureTime_Last8":"Exposure_Time_8",
> "ExposureTime_Last9":"Exposure_Time_9",
> "ExposureTime_Last10":"Exposure_Time_10"}
> 
> for Var in time_variables.keys():
> time_manips = ("""COMPUTE %s = SUBSTR(%s,(INDEX(%s,'T'))+1) .
> COMPUTE %s = number(%s, TIME8).
> VARIABLE LABEL %s.
> VARIABLE LEVEL %s (SCALE).
> FORMATS %s (TIME8).
> VARIABLE WIDTH %s (8).
> EXECUTE.""") %(Var, Var,
> Var,time_variables[Var],Var,time_variables[Var],
> time_variables[Var],time_variables[Var],time_variables[Var]) 
> spss.Submit(time_manips)
> 
> 
> Now to make it dynamic i've gone for the following...
> 
> reg_time = re.compile("^ExposureTime_Last([0-9]*)$")
> reg_Activity = re.compile("^ActivityTime")
> for Var in time_variables.keys():
>     if reg_time.match(Var):
>         match = reg_time.match(Var)
>         E_time = "Exposure_Time_%s" % match.groups()[0]
>     else:
>         match = reg_time.match(Var)
>         match.groups()[0] = "Activity_Time"
> 
> 
> time_manips = ("""COMPUTE %s = SUBSTR(%s,(INDEX(%s,'T'))+1) .
> COMPUTE %s = number(%s, TIME8).
> VARIABLE LABEL %s.
> VARIABLE LEVEL %s (SCALE).
> FORMATS %s (TIME8).
> VARIABLE WIDTH %s (8).
> EXECUTE.""") %(Var, Var,
> Var,time_variables[Var],Var,time_variables[Var],time_variables[Var],
> time_variables[Var],time_variables[Var]) 
> print(time_manips)
> 
> All help welcome, or if a different approach is better please let me
> know

I'd clean up the original code a bit rather than introducing another source
of errors (the regexes):

# no warranties
time_variables = [
    ("ActivityTime", "Activity_Time"),
    ("ExposureTime_Last","Exposure_Time_1")]
time_variables.extend(("ExposureTime_Last%d" % i, "Exposure_Time_%d" % i)
for i in range(2, 11))

for key, value in time_variables:
        time_manips = """COMPUTE %(key)s = SUBSTR(%(key)s
(INDEX(%(key)s,'T'))+1) .
        COMPUTE %(value)s = number(%(key)s, TIME8).
        VARIABLE LABEL %(value)s.
        VARIABLE LEVEL %(value)s (SCALE).
        FORMATS %(value)s (TIME8).
        VARIABLE WIDTH %(value)s (8).
        EXECUTE.""" % dict(key=key, value=value)
        spss.Submit(time_manips)

It might be even better to move the logic into SPSS and to replace the
numerical suffices with array indices (if supported), but I can't help you
with that.

Peter



More information about the Python-list mailing list