Properties file in python (similar to Java Properties)

后端 未结 25 2649
故里飘歌
故里飘歌 2020-11-29 17:41

Given the following format (.properties or .ini):

propertyName1=propertyValue1
propertyName2=propertyValue2
...
propertyNam         


        
相关标签:
25条回答
  • 2020-11-29 18:20

    Here is link to my project: https://sourceforge.net/projects/pyproperties/. It is a library with methods for working with *.properties files for Python 3.x.

    But it is not based on java.util.Properties

    0 讨论(0)
  • 2020-11-29 18:20

    This is a one-to-one replacement of java.util.Propeties

    From the doc:

      def __parse(self, lines):
            """ Parse a list of lines and create
            an internal property dictionary """
    
            # Every line in the file must consist of either a comment
            # or a key-value pair. A key-value pair is a line consisting
            # of a key which is a combination of non-white space characters
            # The separator character between key-value pairs is a '=',
            # ':' or a whitespace character not including the newline.
            # If the '=' or ':' characters are found, in the line, even
            # keys containing whitespace chars are allowed.
    
            # A line with only a key according to the rules above is also
            # fine. In such case, the value is considered as the empty string.
            # In order to include characters '=' or ':' in a key or value,
            # they have to be properly escaped using the backslash character.
    
            # Some examples of valid key-value pairs:
            #
            # key     value
            # key=value
            # key:value
            # key     value1,value2,value3
            # key     value1,value2,value3 \
            #         value4, value5
            # key
            # This key= this value
            # key = value1 value2 value3
    
            # Any line that starts with a '#' is considerered a comment
            # and skipped. Also any trailing or preceding whitespaces
            # are removed from the key/value.
    
            # This is a line parser. It parses the
            # contents like by line.
    
    0 讨论(0)
  • 2020-11-29 18:20

    I did this using ConfigParser as follows. The code assumes that there is a file called config.prop in the same directory where BaseTest is placed:

    config.prop

    [CredentialSection]
    app.name=MyAppName
    

    BaseTest.py:

    import unittest
    import ConfigParser
    
    class BaseTest(unittest.TestCase):
        def setUp(self):
            __SECTION = 'CredentialSection'
            config = ConfigParser.ConfigParser()
            config.readfp(open('config.prop'))
            self.__app_name = config.get(__SECTION, 'app.name')
    
        def test1(self):
            print self.__app_name % This should print: MyAppName
    
    0 讨论(0)
  • 2020-11-29 18:22

    I have created a python module that is almost similar to the Properties class of Java ( Actually it is like the PropertyPlaceholderConfigurer in spring which lets you use ${variable-reference} to refer to already defined property )

    EDIT : You may install this package by running the command(currently tested for python 3).
    pip install property

    The project is hosted on GitHub

    Example : ( Detailed documentation can be found here )

    Let's say you have the following properties defined in my_file.properties file

    foo = I am awesome
    bar = ${chocolate}-bar
    chocolate = fudge
    

    Code to load the above properties

    from properties.p import Property
    
    prop = Property()
    # Simply load it into a dictionary
    dic_prop = prop.load_property_files('my_file.properties')
    
    0 讨论(0)
  • 2020-11-29 18:24

    create a dictionary in your python module and store everything into it and access it, for example:

    dict = {
           'portalPath' : 'www.xyx.com',
           'elementID': 'submit'}
    

    Now to access it you can simply do:

    submitButton = driver.find_element_by_id(dict['elementID'])
    
    0 讨论(0)
  • 2020-11-29 18:25

    If you have an option of file formats I suggest using .ini and Python's ConfigParser as mentioned. If you need compatibility with Java .properties files I have written a library for it called jprops. We were using pyjavaproperties, but after encountering various limitations I ended up implementing my own. It has full support for the .properties format, including unicode support and better support for escape sequences. Jprops can also parse any file-like object while pyjavaproperties only works with real files on disk.

    0 讨论(0)
提交回复
热议问题