Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# hashtable.py

BLANK = object()
BLANK = sentinel("BLANK")


class HashTable:
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# hashtable.py

BLANK = object()
BLANK = sentinel("BLANK")


class HashTable:
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# hashtable.py

BLANK = object()
BLANK = sentinel("BLANK")


class HashTable:
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# hashtable.py

BLANK = object()
BLANK = sentinel("BLANK")


class HashTable:
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
# hashtable.py

import copy
from typing import Any, NamedTuple


Expand Down Expand Up @@ -46,7 +47,7 @@ def get(self, key, default=None):

@property
def pairs(self):
return [pair for pair in self._pairs if pair]
return copy.deepcopy([pair for pair in self._pairs if pair])

def _index(self, key):
return hash(key) % len(self)
2 changes: 1 addition & 1 deletion hashtable/02_linear_probing/hashtable.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

from typing import Any, NamedTuple

DELETED = object()
DELETED = sentinel("DELETED")


class Pair(NamedTuple):
Expand Down
2 changes: 1 addition & 1 deletion hashtable/03_autoresize/hashtable.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

from typing import Any, NamedTuple

DELETED = object()
DELETED = sentinel("DELETED")


class Pair(NamedTuple):
Expand Down
2 changes: 1 addition & 1 deletion hashtable/04_load_factor/hashtable.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

from typing import Any, NamedTuple

DELETED = object()
DELETED = sentinel("DELETED")


class Pair(NamedTuple):
Expand Down
2 changes: 1 addition & 1 deletion hashtable/05_separate_chaining/hashtable.py
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ def get(self, key, default=None):

@property
def pairs(self):
return {pair for bucket in self._buckets for pair in bucket}
return {*bucket for bucket in self._buckets}

@property
def values(self):
Expand Down
4 changes: 3 additions & 1 deletion hashtable/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,12 @@ Code supplementing the [tutorial on the hash table](https://realpython.com/pytho

## Installation

The code requires Python 3.15 or later. It uses the built-in `sentinel()` and unpacking in comprehensions, which are new in Python 3.15.

To get started, create and activate a new virtual environment, and then install the required dependencies into it:

```shell
$ python3 -m venv venv --prompt=hashtable
$ python3.15 -m venv venv --prompt=hashtable
$ source venv/bin/activate
(hashtable) $ python -m pip install -r requirements.txt
```
Expand Down
15 changes: 6 additions & 9 deletions hashtable/requirements.txt
Original file line number Diff line number Diff line change
@@ -1,9 +1,6 @@
attrs==21.4.0
iniconfig==1.1.1
packaging==21.3
pluggy==1.0.0
py==1.11.0
pyparsing==3.0.7
pytest==7.0.1
pytest-unordered==0.4.1
tomli==2.0.1
iniconfig==2.3.1
packaging==26.3
pluggy==1.7.0
pygments==2.21.0
pytest==9.1.1
pytest-unordered==0.8.0
1 change: 1 addition & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ exclude = [
"python315-frozendict/",
"python315-lazy-imports",
"python315-new-features",
"hashtable",
"ai-benchmark",
"python-debugging-pdb"
]
Expand Down
Loading