Skip to content

pfun.dict.Dict dataclass

Immutable dictionary class with functional helper methods

__contains__(self, key)

Test if key is a key in this dictionary

Examples:

>>> 'key' in Dict({'key': 'value'})
True

Parameters:

Name Type Description Default
key object

The key to test for membership

required

Returns:

Type Description
bool

True if key is a key in this dictionary, False otherwise

__eq__(self, other)

Compare self with other

Returns:

Type Description
bool

True if keys and associated values are equal, False otherwise

__getitem__(self, key)

get the value associated with a key

Examples:

>>> Dict(key='value')['key']
'value'

Parameters:

Name Type Description Default
key ~K

the key to retrieve

required
Return required

__init__(self, d={})

Parameters:

Name Type Description Default
d Mapping[~K, ~V]

dict instance to wrap

{}

__iter__(self)

Get an iterator over the keys in this dictionary

Examples:

>>> tuple(Dict({'key': 'value'}))
('key',)

Returns:

Type Description
Iterator[~K]

Iterator of the keys in this dictionary

__len__(self)

Get the number of key/value pairs in this dictionary

Examples:

>>> len(Dict({'key': 'value'}))
1

Returns:

Type Description
int

Number of key/value pairs in this dictionary

copy(self)

Get a shallow copy of this dictionary.

Examples:

>>> Dict({'key': 'value'}).copy()
Dict({'key': 'value'})

Returns:

Type Description
Dict[K, V]

Copy of this dict

get(self, key)

get the value associated with a key

Examples:

>>> Dict().get('key')
Nothing()
>> Dict({'key': 'value'}).get('key')
Just('value')

Parameters:

Name Type Description Default
key ~K

the key to retrieve

required
default

value to return if the key is not found

required

Returns:

Type Description
Union[pfun.maybe.Nothing, pfun.maybe.Just[~V]]

Just if key is found in dictionary or default is given, Nothing otherwise

items(self)

Get the keys and values of this dictionary

Examples:

>>> Dict({'key': 'value'}).items()
dict_items([('key', 'value')])

Returns:

Type Description
ItemsView[~K, ~V]

Keys and values of this dictionary

keys(self)

Get the keys in this dictionary

Examples:

>>> Dict({'key': 'value'}).keys()
dict_keys(['key'])

Returns:

Type Description
KeysView[~K]

Dictionary keys

set(self, key, value)

Combine keys and values from this dictionary with a new dictionary that includes key and value

Examples:

>>> Dict().set('key', 'value')
{'key': 'value'}

Parameters:

Name Type Description Default
key ~K

key to add to the new dictionary

required
value ~V

value to associate with key

required

Returns:

Type Description
Dict[K, V]

new dictionary with existing keys and values in addition to key and value

update(self, other)

Get a copy of this dictionary updated with key/value pairs from other

Examples:

>>> Dict({'key': 'value'}).update({'new_key': 'new_value'})
Dict({'key': 'value', 'new_key': 'new_value'})

Parameters:

Name Type Description Default
other Union[Mapping[~K, ~V], Dict[K, V]]

Dict to add to keys/values of this dictionary

required

Returns:

Type Description
Dict[K, V]

copy of self with keys and values added

values(self)

Get the values in this dictionary

Examples:

>>> Dict({'key': 'value'}).values()
dict_values(['value'])

Returns:

Type Description
ValuesView[~V]

Dictionary values

without(self, key)

Get a copy of this dictionary without the mapping associated with key.

Examples:

>>> Dict({'key': 'value'}).without('key')
Dict({})

Parameters:

Name Type Description Default
key ~K

The key to remove

required

Returns:

Type Description
Dict[K, V]

Copy of this dictionary without key