Solved

add properties

Posted on 2011-03-11
6
398 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 

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

On Demand Webinar - Networking for the Cloud Era

This webinar discusses:
-Common barriers companies experience when moving to the cloud
-How SD-WAN changes the way we look at networks
-Best practices customers should employ moving forward with cloud migration
-What happens behind the scenes of SteelConnect’s one-click button

Question has a verified solution.

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

Suggested Solutions

This article will show the steps for installing Python on Ubuntu Operating System. I have created a virtual machine with Ubuntu Operating system 8.10 and this installing process also works with upgraded version of Ubuntu OS. For installing Py…
When we want to run, execute or repeat a statement multiple times, a loop is necessary. This article covers the two types of loops in Python: the while loop and the for loop.
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…
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…

740 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