Properties
Class
- class javaproperties.Properties(data: None | Mapping[str, str] | Iterable[tuple[str, str]] = None, defaults: Properties | None = 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 normalMutableMapping
class (i.e., you can doprops[key] = value
and so forth), except that it may only be used to storestr
values.Two
Properties
instances compare equal iff both their key-value pairs anddefaults
attributes are equal. When comparing aProperties
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 todict
s and other mapping types- Parameters:
data (mapping or
None
) – A mapping or iterable of(key, value)
pairs with which to initialize theProperties
instance. All keys and values indata
must be text strings.defaults (Optional[Properties]) – a set of default properties that will be used as fallback for
getProperty
- copy() 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’sdefaults
.
- defaults
A
Properties
subobject used as fallback forgetProperty
. OnlygetProperty
,propertyNames
,stringPropertyNames
, and__eq__
use this attribute; all other methods (including the standard mapping methods) ignore it.
- getProperty(key: str, defaultValue: T | None = None) str | T | None [source]
Fetch the value associated with the key
key
in theProperties
instance. If the key is not present,defaults
is checked, and then itsdefaults
, etc., until either a value forkey
is found or the nextdefaults
isNone
, in which casedefaultValue
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 theProperties
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 anInvalidUEscapeError
to be raised- Parameters:
inStream (IO) – the file from which to read the
.properties
document- Returns:
- 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 fileinStream
.Beyond basic XML well-formedness,
loadFromXML
only checks that the root element is namedproperties
and that all of itsentry
children havekey
attributes; no further validation is performed.- Parameters:
inStream (IO) – the file from which to read the XML properties document
- Returns:
- Raises:
ValueError – if the root of the XML tree is not a
<properties>
tag or an<entry>
element is missing akey
attribute
- propertyNames() Iterator[str] [source]
Returns a generator of all distinct keys in the
Properties
instance and itsdefaults
(and itsdefaults
’sdefaults
, etc.) in unspecified order- Return type:
Iterator[str]
- store(out: TextIO, comments: str | None = None) None [source]
Write the
Properties
instance’s entries (in unspecified order) in.properties
format toout
, including the current timestamp.