[Tutor] importing variables

Stefan Lesicnik stefan at lsd.co.za
Thu Nov 12 16:03:04 CET 2009


On Thu, Nov 12, 2009 at 4:38 PM, Alan Gauld <alan.gauld at btinternet.com> wrote:
>
> "Stefan Lesicnik" <stefan at lsd.co.za> wrote
>
>> features file contains
>> rt='''text'''
>>
>> import features
>>
>> a = 'rt'
>> print features.rt  #this works
>> print features.a  #this fails
>
> Because you defined a in your current file.
> You need to define it in features.
>
>> I need to use features.a as i am iterating through a list and a would
>> be the different features i want to check if they exist, and then use
>> the variable.
>
> So you need to define a in features.
> Note that if you define a in features to be an empty list you can add/delete
> items to that list from the importing module.
>
> ########## features.py##########
> a = []
>
> ###########main.py##########
>
> import features
> print features.a
>
> features.a.append(66)
> print features.a
>
> features.a.remove(66)
> print features.a
>
>
> Does that help?
>
> NB. I don't really recommend this approach, but it is a way to do what I
> think you want...
>

Thanks Alan & Christian,  but i think I probably didnt communicate
what i was trying to do. Maybe a sample of the actual code will help


#!/usr/bin/python

import fileinput
import re
import features

client = open('clients/bob.txt', 'r')
template = open('temp.txt', 'w')

#Read all client data to variable dictionary
data = {}
for line in client:
    line = line.strip()
    line = line.split('=')
    data[line[0]] = line[1]

requestedfeatures = data['features'].split(',')
client.close()

#Write template and modify all relevant sections
for line in fileinput.input('template/noc-template.txt'):
    line = line.strip()
    templatereplace = re.match(r'.*\%\%(.*)\%\%.*', line)
    featurereplace = re.match(r'.*\$\$(.*)\$\$.*', line)
    if templatereplace:
        newline = re.sub('\%\%' + templatereplace.group(1) + '\%\%',
data[templatereplace.group(1)], line)
        print newline
        template.write(newline + '\n')
    elif featurereplace:
        if featurereplace.group(1) in requestedfeatures:
            newline = re.sub('\$\$' + featurereplace.group(1) +
'\$\$', features.rt, line)
            print newline
            template.write(newline + '\n')
    else:
        print line
        template.write(line + '\n')
template.close()



So.  At the end of the day, i want to generate a page that will be
read in a wiki.  The requestedfeatures is a variable of features. So
the end generated file will have certain extra fields depending on
what features were selected. So the noc-template.txt is the main
template which looks like this

========= %%clientname%% =========

this is the report

tech contact: %%technical_contact%%

$$rt$$


So my idea was to replace %%clientname%% with the dictionary
and replace $$rt$$ with features from the features.py.  The issue i
have is with the featurereplace. and this line --newline =
re.sub('\$\$' + featurereplace.group(1) + '\$\$', features.rt,
line)--.  Using features.rt works, but i essentially want to use
features.featurereplace.group(1)  which should be features.rt , but
saying features.featurereplace.group(1) seems to look for that
variable and doesnt substitute.  I've tried things like features. +
featuresreplace.group(1) etc but that doesnt work.

Furthermore, i think what im trying to do here is a mix of templating,
but i looked at cheetah and mako and cant work it out from the docs
:(.

I am probably doing this completely the wrong way round and appreciate
better ideas on 'how to do things (tm)'.

thanks!!

:)


More information about the Tutor mailing list