lhoestq HF Staff commited on
Commit
58f6a14
·
1 Parent(s): 9057a9d

Fix streaming

Browse files

gzip.open only works for local files, not with URLs

To open remote files in streaming mode, we extend `open` to support URLs.
This should fix https://github.com/huggingface/datasets/issues/5060

Files changed (1) hide show
  1. pubchem_selfies.py +11 -12
pubchem_selfies.py CHANGED
@@ -472,15 +472,14 @@ class PubchemSelfies(datasets.GeneratorBasedBuilder):
472
  def _generate_examples(self, subdirs, split):
473
  # The `key` is for legacy reasons (tfds) and is not important in itself, but must be unique for each example.
474
  for filepath in subdirs:
475
- with gzip.open(filepath, mode="rt") as f:
476
- rows = f.readlines()
477
- for key, row in enumerate(rows):
478
- data = json.loads(row)
479
- properties = data["molecules"][0]["properties"]
480
- yield key, {
481
- "PUBCHEM_COMPOUND_CID": properties["PUBCHEM_COMPOUND_CID"],
482
- "PUBCHEM_OPENEYE_CAN_SMILES": properties[
483
- "PUBCHEM_OPENEYE_CAN_SMILES"
484
- ],
485
- "CAN_SELFIES": properties["CAN_SELFIES"],
486
- }
 
472
  def _generate_examples(self, subdirs, split):
473
  # The `key` is for legacy reasons (tfds) and is not important in itself, but must be unique for each example.
474
  for filepath in subdirs:
475
+ with gzip.open(open(filepath, "rb"), "rt", encoding="utf-8") as f:
476
+ for key, row in enumerate(rows):
477
+ data = json.loads(row)
478
+ properties = data["molecules"][0]["properties"]
479
+ yield key, {
480
+ "PUBCHEM_COMPOUND_CID": properties["PUBCHEM_COMPOUND_CID"],
481
+ "PUBCHEM_OPENEYE_CAN_SMILES": properties[
482
+ "PUBCHEM_OPENEYE_CAN_SMILES"
483
+ ],
484
+ "CAN_SELFIES": properties["CAN_SELFIES"],
485
+ }