Properties Class

class javaproperties.Properties(data: Union[None, Mapping[str, str], Iterable[Tuple[str, str]]] = None, defaults: Optional[javaproperties.propclass.Properties] = None)[source]

A port of Java 8’s java.util.Properties that tries to match its behavior as much as is Pythonically possible. Properties behaves like a normal MutableMapping class (i.e., you can do props[key] = value and so forth), except that it may only be used to store str values.

Two Properties instances compare equal iff both their key-value pairs and defaults attributes are equal. When comparing a Properties instance to any other type of mapping, only the key-value pairs are considered.

Changed in version 0.5.0: Properties instances can now compare equal to dicts and other mapping types

Parameters
  • data (mapping or None) – A mapping or iterable of (key, value) pairs with which to initialize the Properties instance. All keys and values in data must be text strings.

  • defaults (Optional[Properties]) – a set of default properties that will be used as fallback for getProperty

copy()javaproperties.propclass.Properties[source]

New in version 0.5.0.

Create a shallow copy of the mapping. The copy’s defaults attribute will be the same instance as the original’s defaults.

defaults

A Properties subobject used as fallback for getProperty. Only getProperty, propertyNames, stringPropertyNames, and __eq__ use this attribute; all other methods (including the standard mapping methods) ignore it.

getProperty(key: str, defaultValue: Optional[T] = None)Optional[Union[str, T]][source]

Fetch the value associated with the key key in the Properties instance. If the key is not present, defaults is checked, and then its defaults, etc., until either a value for key is found or the next defaults is None, in which case defaultValue is returned.

Parameters
  • key (str) – the key to look up the value of

  • defaultValue (Any) – the value to return if key is not found in the Properties instance

Return type

str (if key was found)

load(inStream: IO)None[source]

Update the Properties instance with the entries in a .properties file or file-like object.

inStream may be either a text or binary filehandle, with or without universal newlines enabled. If it is a binary filehandle, its contents are decoded as Latin-1.

Changed in version 0.5.0: Invalid \uXXXX escape sequences will now cause an InvalidUEscapeError to be raised

Parameters

inStream (IO) – the file from which to read the .properties document

Returns

None

Raises

InvalidUEscapeError – if an invalid \uXXXX escape sequence occurs in the input

loadFromXML(inStream: IO)None[source]

Update the Properties instance with the entries in the XML properties file inStream.

Beyond basic XML well-formedness, loadFromXML only checks that the root element is named properties and that all of its entry children have key attributes; no further validation is performed.

Parameters

inStream (IO) – the file from which to read the XML properties document

Returns

None

Raises

ValueError – if the root of the XML tree is not a <properties> tag or an <entry> element is missing a key attribute

propertyNames()Iterator[str][source]

Returns a generator of all distinct keys in the Properties instance and its defaults (and its defaults’s defaults, etc.) in unspecified order

Return type

Iterator[str]

setProperty(key: str, value: str)None[source]

Equivalent to self[key] = value

store(out: TextIO, comments: Optional[str] = None)None[source]

Write the Properties instance’s entries (in unspecified order) in .properties format to out, including the current timestamp.

Parameters
  • out (TextIO) – A file-like object to write the properties to. It must have been opened as a text file with a Latin-1-compatible encoding.

  • comments (Optional[str]) – If non-None, comments will be written to out as a comment before any other content

Returns

None

storeToXML(out: BinaryIO, comment: Optional[str] = None, encoding: str = 'UTF-8')None[source]

Write the Properties instance’s entries (in unspecified order) in XML properties format to out.

Parameters
  • out (BinaryIO) – a file-like object to write the properties to

  • comment (Optional[str]) – if non-None, comment will be output as a <comment> element before the <entry> elements

  • encoding (str) – the name of the encoding to use for the XML document (also included in the XML declaration)

Returns

None

stringPropertyNames()Set[str][source]

Returns a set of all keys in the Properties instance and its defaults (and its defaults’s defaults, etc.)

Return type

Set[str]