Solved

add properties

Posted on 2011-03-11
6
384 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
Comment Utility
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
Comment Utility
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 500 total points
Comment Utility
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
What Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

 

Author Comment

by:wasman
Comment Utility
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
Comment Utility
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
Comment Utility
Thanks a lot Mish33
0

Featured Post

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Article by: Swadhin
Introduction of Lists in Python: There are six built-in types of sequences. Lists and tuples are the most common one. In this article we will see how to use Lists in python and how we can utilize it while doing our own program. In general we can al…
The purpose of this article is to demonstrate how we can use conditional statements using Python.
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 lists in Python. Lists, as their name suggests, are a means for ordering and storing values. : Lists are declared using brackets; for example: t = [1, 2, 3]: Lists may contain a mix of data types; for example: t = ['string', 1, T…

771 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

Need Help in Real-Time?

Connect with top rated Experts

15 Experts available now in Live!

Get 1:1 Help Now