Properties
Class¶
-
class
javaproperties.
Properties
(data=None, defaults=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 store strings (str
andunicode
in Python 2; juststr
in Python 3). Attempts to use a non-string object as a key or value will produce aTypeError
.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 (
Properties
orNone
) – a set of default properties that will be used as fallback forgetProperty
-
copy
()[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
= None¶ 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, defaultValue=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 (text string) – the key to look up the value of
defaultValue – the value to return if
key
is not found in theProperties
instance
- Return type
text string (if
key
was found)- Raises
TypeError – if
key
is not a string
-
load
(inStream)[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 (file-like object) – the file from which to read the
.properties
document- Returns
- Raises
InvalidUEscapeError – if an invalid
\uXXXX
escape sequence occurs in the input
-
loadFromXML
(inStream)[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.Note
This uses
xml.etree.ElementTree
for parsing, which does not have decent support forunicode
input in Python 2. Files containing non-ASCII characters need to be opened in binary mode in Python 2, while Python 3 accepts both binary and text input.- Parameters
inStream (file-like object) – 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
()[source]¶ Returns a generator of all distinct keys in the
Properties
instance and itsdefaults
(and itsdefaults
’sdefaults
, etc.) in unspecified order- Return type
generator of text strings
-
store
(out, comments=None)[source]¶ Write the
Properties
instance’s entries (in unspecified order) in.properties
format toout
, including the current timestamp.
-
storeToXML
(out, comment=None, encoding='UTF-8')[source]¶ Write the
Properties
instance’s entries (in unspecified order) in XML properties format toout
.- Parameters
out (binary file-like object) – a file-like object to write the properties to
comment (text string or
None
) – if non-None
,comment
will be output as a<comment>
element before the<entry>
elementsencoding (string) – the name of the encoding to use for the XML document (also included in the XML declaration)
- Returns