Skip to main content
added 646 characters in body
Source Link
Reinderien
  • 71.1k
  • 5
  • 76
  • 256

Is it worth it to write a function for two lines of code?

In this case, yes.

Are there other approaches / optimisations to make it better / looks nicer ?

Yes: use pathlib -

from pathlib import Path
import pickle
from typing import Any

RES_PATH = Path('.')


def pdump(stem: str, data: Any) -> None:
    path = (RES_PATH / stem).with_suffix('.p')
    with path.open('wb') as f:
        pickle.dump(obj=data, file=f)

You can then call this function with a sequence as Harith demonstrates.

Is it possible to get the generic object name so that i don't have to specify it in the 'open'?

Yes. With your current code you would need to use locals() (the bad kind of clever; I don't recommend doing this). A somewhat cleaner approach would put all of your objects in a parent class Persisted(NamedTuple), and then iterate over all of them.

from pathlib import Path
import pickle
from typing import Any, NamedTuple

RES_PATH = Path('.')


def pdump(stem: str, data: Any) -> None:
    path = (RES_PATH / stem).with_suffix('.p')
    with path.open('wb') as f:
        pickle.dump(obj=data, file=f)


Mystery = Any


class Persisted(NamedTuple):
    res_folds_seeds: Mystery
    res_folds: Mystery
    res_folds_neutralised: Mystery
    dict_coeffs: dict[Mystery, Mystery]
    preds: Mystery
    exps: Mystery

    def dump(self) -> None:
        for name in self.__annotations__.keys():
            pdump(stem=name, data=getattr(self, name))

Is it worth it to write a function for two lines of code?

In this case, yes.

Are there other approaches / optimisations to make it better / looks nicer ?

Yes: use pathlib -

from pathlib import Path
import pickle
from typing import Any

RES_PATH = Path('.')


def pdump(stem: str, data: Any) -> None:
    path = (RES_PATH / stem).with_suffix('.p')
    with path.open('wb') as f:
        pickle.dump(obj=data, file=f)

You can then call this function with a sequence as Harith demonstrates.

Is it possible to get the generic object name so that i don't have to specify it in the 'open'?

Yes. With your current code you would need to use locals() (the bad kind of clever; I don't recommend doing this). A somewhat cleaner approach would put all of your objects in a parent class Persisted(NamedTuple), and then iterate over all of them.

Is it worth it to write a function for two lines of code?

In this case, yes.

Are there other approaches / optimisations to make it better / looks nicer ?

Yes: use pathlib -

from pathlib import Path
import pickle
from typing import Any

RES_PATH = Path('.')


def pdump(stem: str, data: Any) -> None:
    path = (RES_PATH / stem).with_suffix('.p')
    with path.open('wb') as f:
        pickle.dump(obj=data, file=f)

You can then call this function with a sequence as Harith demonstrates.

Is it possible to get the generic object name so that i don't have to specify it in the 'open'?

Yes. With your current code you would need to use locals() (the bad kind of clever; I don't recommend doing this). A somewhat cleaner approach would put all of your objects in a parent class Persisted(NamedTuple), and then iterate over all of them.

from pathlib import Path
import pickle
from typing import Any, NamedTuple

RES_PATH = Path('.')


def pdump(stem: str, data: Any) -> None:
    path = (RES_PATH / stem).with_suffix('.p')
    with path.open('wb') as f:
        pickle.dump(obj=data, file=f)


Mystery = Any


class Persisted(NamedTuple):
    res_folds_seeds: Mystery
    res_folds: Mystery
    res_folds_neutralised: Mystery
    dict_coeffs: dict[Mystery, Mystery]
    preds: Mystery
    exps: Mystery

    def dump(self) -> None:
        for name in self.__annotations__.keys():
            pdump(stem=name, data=getattr(self, name))
Source Link
Reinderien
  • 71.1k
  • 5
  • 76
  • 256

Is it worth it to write a function for two lines of code?

In this case, yes.

Are there other approaches / optimisations to make it better / looks nicer ?

Yes: use pathlib -

from pathlib import Path
import pickle
from typing import Any

RES_PATH = Path('.')


def pdump(stem: str, data: Any) -> None:
    path = (RES_PATH / stem).with_suffix('.p')
    with path.open('wb') as f:
        pickle.dump(obj=data, file=f)

You can then call this function with a sequence as Harith demonstrates.

Is it possible to get the generic object name so that i don't have to specify it in the 'open'?

Yes. With your current code you would need to use locals() (the bad kind of clever; I don't recommend doing this). A somewhat cleaner approach would put all of your objects in a parent class Persisted(NamedTuple), and then iterate over all of them.