Skip to content

Commit 06b6cdb

Browse files
committed
LZ4
1 parent 6ae42a6 commit 06b6cdb

File tree

1 file changed

+15
-7
lines changed

1 file changed

+15
-7
lines changed

names_dataset/nd_v3.py

+15-7
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,23 @@
11
import copy
2-
import json
32
import operator
43
import os
5-
import zipfile
4+
import pickle
65
from collections import defaultdict
76
from pathlib import Path
87
from typing import Optional
98

9+
import lz4.frame
1010
import pycountry
1111

1212

13+
# Function to decompress and unpickle data
14+
def decompress_and_unpickle(compressed_file):
15+
with open(compressed_file, 'rb') as file:
16+
compressed_data = file.read()
17+
original_data = pickle.loads(lz4.frame.decompress(compressed_data))
18+
return original_data
19+
20+
1321
def _query(search_set, key):
1422
key = key.strip().title()
1523
if key in search_set:
@@ -54,16 +62,16 @@ class NameDataset:
5462
def __init__(self, load_first_names=True, load_last_names=True):
5563
if not load_first_names and not load_last_names:
5664
raise ValueError('Select either [load_first_names=True] and/or [load_last_names=True].')
57-
first_names_filename = Path(os.path.dirname(__file__)) / 'v3/first_names.zip'
58-
last_names_filename = Path(os.path.dirname(__file__)) / 'v3/last_names.zip'
65+
first_names_filename = Path(os.path.dirname(__file__)) / 'v3/first_names.lz4'
66+
last_names_filename = Path(os.path.dirname(__file__)) / 'v3/last_names.lz4'
5967
self.first_names = self._read_json_from_zip(first_names_filename) if load_first_names else None
6068
self.last_names = self._read_json_from_zip(last_names_filename) if load_last_names else None
6169

6270
@staticmethod
6371
def _read_json_from_zip(zip_file):
64-
with zipfile.ZipFile(zip_file) as z:
65-
with z.open(z.filelist[0]) as f:
66-
return json.load(f)
72+
print(zip_file)
73+
# return pickle.load(gzip.open(zip_file, 'rb'))
74+
return decompress_and_unpickle(zip_file)
6775

6876
def search(self, name: str):
6977
key = name.strip().title()

0 commit comments

Comments
 (0)