Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

add properties

Posted on 2011-03-11
6
Medium Priority
?
405 Views
Last Modified: 2012-06-27
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
0
Comment
Question by:wasman
  • 3
  • 3
6 Comments
 
LVL 15

Expert Comment

by:mish33
ID: 35116500
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

0
 

Author Comment

by:wasman
ID: 35125310
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.



0
 
LVL 15

Accepted Solution

by:
mish33 earned 2000 total points
ID: 35129764
append adds newprop(erties) to the end of the file.
prepend adds newprop(erties) to the beginning of the file.

try read the code. python is almost plain english.

dictionary consists of keys and values. current code select those properties to add whose name starts with 'os-environment.'

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):
    f = open(file, 'a')
    try:
        f.writelines(lines)
    finally:
        f.close()

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

Open in new window

0
New feature and membership benefit!

New feature! Upgrade and increase expert visibility of your issues with Priority Questions.

 

Author Comment

by:wasman
ID: 35130796
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.

0
 
LVL 15

Expert Comment

by:mish33
ID: 35130846
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.
0
 

Author Closing Comment

by:wasman
ID: 35132347
Thanks a lot Mish33
0

Featured Post

Keep up with what's happening at Experts Exchange!

Sign up to receive Decoded, a new monthly digest with product updates, feature release info, continuing education opportunities, and more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

A set of related code is known to be a Module, it helps us to organize our code logically which is much easier for us to understand and use it. Module is an object with arbitrarily named attributes which can be used in binding and referencing. …
Introduction On September 29, 2012, the Python 3.3.0 was released; nothing extremely unexpected,  yet another, better version of Python. But, if you work in Microsoft Windows, you should notice that the Python Launcher for Windows was introduced wi…
Learn the basics of strings in Python: declaration, operations, indices, and slicing. Strings are declared with quotations; for example: s = "string": Strings are immutable.: Strings may be concatenated or multiplied using the addition and multiplic…
Learn the basics of if, else, and elif statements in Python 2.7. Use "if" statements to test a specified condition.: The structure of an if statement is as follows: (CODE) Use "else" statements to allow the execution of an alternative, if the …
Suggested Courses

916 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question