Link to home
Start Free TrialLog in
Avatar of WAS
WASFlag for United States of America

asked on

add properties

HonorGod,

How are you now, i hope you are feeling better now.

Please provide me a function which will scan the data dictionary and add properties that start with "os-environment." to the beginning of myproperties.properties file
 location can be "beginning" or "end"

 basically you need to implement a function that inserts a text into a properties file (beginning or end). Once i have that function i will add it to the <addpropeties> class

 def addproperties(propertieslist, propertiesfile, location)
 where properties list is something like {property1: value1, property2: value2}
 basically a dictionary
Avatar of mish33
mish33
Flag of United States of America image

Try that:
def addproperties(properties, propertiesfile, location):
    newprops = ['%s: %s\n' % i for i in properties.iteritems() if i[0].startswith('os-environment.')]
    if location == 'end':
        append(newprops, propertiesfile)
    else:
        prepend(newprops, propertiesfile)

def append(lines, file):
    with open(file, 'a') as f:
        f.writelines(lines)

def prepend(lines, file):
    tmp = 'new_properties_file'
    append(lines, tmp)
    append(open(file).readlines(), tmp)
    os.rename(tmp, file)

Open in new window

Avatar of WAS

ASKER

Mish33,

Thanks for helping me.
I am using Jython 2.5.2 interpreter for compiling my python programs,

i get below error
SyntaxError: 'with' will become a reserved keyword in Python 2.6
line 9

and also please explain the functions append and prepend, what are they doing, again what i am looking for is my addproperties function should look into dictionary for attributes (or what ever you call) "os-environment", and add those lines to my propertiesfile.

Let me know if you don't understand my question. It is very important that you understand my question.

Have a nice day.



ASKER CERTIFIED SOLUTION
Avatar of mish33
mish33
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of WAS

ASKER

The code looks good now.
 I was just wondering why didn't you write the prepend function in the same manner as append. instead or creating a temp file and then renaming it.

There is no file open mode for adding to the beginning, so standard technic for that is creating a new file and appending the old file data to it.
Avatar of WAS

ASKER

Thanks a lot Mish33