Package: pystow
PyStow👜 Easily pick a place to store data for your python code.🚀 Getting StartedGet a directory for your application.importpystow# Get a directory (as a pathlib.Path) for ~/.data/pykeenpykeen_directory=pystow.join('pykeen')# Get a subdirectory (as a pathlib.Path) for ~/.data/pykeen/experimentspykeen_experiments_directory=pystow.join('pykeen','experiments')# You can go as deep as you wantpykeen_deep_directory=pystow.join('pykeen','experiments','a','b','c')If you reuse the same directory structure a lot, you can save them in a module:importpystowpykeen_module=pystow.module("pykeen")# Access the module's directory with .baseassertpystow.join("pykeen")==pystow.module("pykeen").base# Get a subdirectory (as a pathlib.Path) for ~/.data/pykeen/experimentspykeen_experiments_directory=pykeen_module.join('experiments')# You can go as deep as you want past the original "pykeen" modulepykeen_deep_directory=pykeen_module.join('experiments','a','b','c')Get a file path for your application by adding thenamekeyword argument. This is made explicit so PyStow knows which
parent directories to automatically create. This works withpystowor any module you create withpystow.module.importpystow# Get a directory (as a pathlib.Path) for ~/.data/indra/database.tsvindra_database_path=pystow.join('indra','database',name='database.tsv')Ensure a file from the internet is available in your application's directory:importpystowurl='https://raw.githubusercontent.com/pykeen/pykeen/master/src/pykeen/datasets/nations/test.txt'path=pystow.ensure('pykeen','datasets','nations',url=url)Ensure a tabular data file from the internet and load it for usage (requirespip install pandas):importpystowimportpandasaspdurl='https://raw.githubusercontent.com/pykeen/pykeen/master/src/pykeen/datasets/nations/test.txt'df:pd.DataFrame=pystow.ensure_csv('pykeen','datasets','nations',url=url)Ensure a comma-separated tabular data file from the internet and load it for usage (requirespip install pandas):importpystowimportpandasaspdurl='https://raw.githubusercontent.com/cthoyt/pystow/main/tests/resources/test_1.csv'df:pd.DataFrame=pystow.ensure_csv('pykeen','datasets','nations',url=url,read_csv_kwargs=dict(sep=","))Ensure a RDF file from the internet and load it for usage (requirespip install rdflib)importpystowimportrdfliburl='https://ftp.expasy.org/databases/rhea/rdf/rhea.rdf.gz'rdf_graph:rdflib.Graph=pystow.ensure_rdf('rhea',url=url)Also seepystow.ensure_excel(),pystow.ensure_rdf(),pystow.ensure_zip_df(), andpystow.ensure_tar_df().If your data comes with a lot of different files in an archive,
you can ensure the archive is downloaded and get specific files from it:importnumpyasnpimportpystowurl="https://cloud.enterprise.informatik.uni-leipzig.de/index.php/s/LHPbMCre7SLqajB/download/MultiKE_D_Y_15K_V1.zip"# the path inside the archive to the file you wantinner_path="MultiKE/D_Y_15K_V1/721_5fold/1/20210219183115/ent_embeds.npy"withpystow.ensure_open_zip("kiez",url=url,inner_path=inner_path)asfile:emb=np.load(file)Also seepystow.module.ensure_open_lzma(),pystow.module.ensure_open_tarfile()andpystow.module.ensure_open_gz().⚙️️ ConfigurationBy default, data is stored in the$HOME/.datadirectory. By default, the<app>app will create the$HOME/.data/<app>folder.If you want to use an alternate folder name to.datainside the home directory, you can set thePYSTOW_NAMEenvironment variable. For example, if you setPYSTOW_NAME=mydata, then the following code for thepykeenapp will
create the$HOME/mydata/pykeen/directory:importosimportpystow# Only for demonstration purposes. You should set environment# variables either with your .bashrc or in the command line REPL.os.environ['PYSTOW_NAME']='mydata'# Get a directory (as a pathlib.Path) for ~/mydata/pykeenpykeen_directory=pystow.join('pykeen')If you want to specify a completely custom directory that isn't relative to your home directory, you can set
thePYSTOW_HOMEenvironment variable. For example, if you setPYSTOW_HOME=/usr/local/, then the following code for
thepykeenapp will create the/usr/local/pykeen/directory:importosimportpystow# Only for demonstration purposes. You should set environment# variables either with your .bashrc or in the command line REPL.os.environ['PYSTOW_HOME']='/usr/local/'# Get a directory (as a pathlib.Path) for /usr/local/pykeenpykeen_directory=pystow.join('pykeen')Note: if you setPYSTOW_HOME, thenPYSTOW_NAMEis disregarded.X Desktop Group (XDG) CompatibilityWhile PyStow's main goal is to make application data less opaque and less
hidden, some users might want to use theXDG specificationsfor storing their app data.If you set the environment variablePYSTOW_USE_APPDIRStotrueorTrue, then theappdirspackage will be used to choose
the base directory based on theuser data diroption. This can still be
overridden byPYSTOW_HOME.🚀 InstallationThe most recent release can be installed fromPyPIwith:$pipinstallpystowNote, as of v0.3.0, Python 3.6 isn't officially supported (its
end-of-life was in December 2021). For the time being,pystowmight still
work on py36, but this is only coincidental.The most recent code and data can be installed directly from GitHub with:$pipinstallgit+https://github.com/cthoyt/pystow.gitTo install in development mode, use the following:$gitclonegit+https://github.com/cthoyt/pystow.git
$cdpystow
$pipinstall-e.⚖️ LicenseThe code in this package is licensed under the MIT License.