Solved

add properties

Posted on 2011-03-11
6
393 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 500 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
Gigs: Get Your Project Delivered by an Expert

Select from freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely and get projects done right.

 

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

Courses: Start Training Online With Pros, Today

Brush up on the basics or master the advanced techniques required to earn essential industry certifications, with Courses. Enroll in a course and start learning today. Training topics range from Android App Dev to the Xen Virtualization Platform.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
I cannot read information from Windows Registry using python on windows 8.1 2 74
Python Regex Problem 24 134
How can I do this in Pyhton? 12 125
Modify a small python script 19 108
Variable is a place holder or reserved memory locations to store any value. Which means whenever we create a variable, indirectly we are reserving some space in the memory. The interpreter assigns or allocates some space in the memory based on the d…
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. …
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 …
Learn the basics of modules and packages in Python. Every Python file is a module, ending in the suffix: .py: Modules are a collection of functions and variables.: Packages are a collection of modules.: Module functions and variables are accessed us…

815 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

10 Experts available now in Live!

Get 1:1 Help Now