I'm developing a Python package, and the implementation of the Python package depends on some research result. I saved my research result in a Python dictionary. I have 2 questions:
I looked into this, but still couldn't get an idea of how to save package internal data from scratch. It also doesn't show how to load the saved internal data. Is there anything like devtools::use_data in R?
This is what I generally do for standard python3 distribution with pip (it mirrors a bit R data distribution).
Then, If your package is called for instance "my_data_pack" you will have this folder structure:
.
├── my_data_pack
│ ├── __init__.py
│ └── my_data
│ └── data_file.txt
└── setup.py
setup function of your your setup.py file:from setuptools import setup, find_packages
setup(
name='my_data_pack',
packages=find_packages(),
package_data={'my_data_pack': ['my_data/*']}
)
This will make the data to be included in the tar.gz distribution file when building for pip.
Depending on your package structure you may need to change the line to something like package_data={'mypkg': ['my_data/*.dat']},
as it is indicated in the link you mention.
os or pkg_resourcesTo use os include these lines in your __init__.py file (or in any other submodule you are using):
import os
location = os.path.dirname(os.path.realpath(__file__))
my_file = os.path.join(location, 'my_data', 'data_file.txt')
with open(my_file) as fin:
my_data_object = fin.readlines()
or these if you prefer to use pkg_resources:
import pkg_resources
my_file = pkg_resources.resource_filename('my_data_pack', 'my_data/data_file.txt')
with open(my_file2) as fin:
my_data_object = fin.readlines()
Change the readlines section to read your own data format. That is all you need for the package code.
python3 setup.py sdist
This will create a new directory called "dist" with at tar.gz file in it. Then you can install your package as
pip3 install dist/my_data_pack-0.0.0.tar.gz
To access the data in your python session you will do:
import my_data_pack
print(my_data_pack.my_data_object)
In the old R times (before devtools :) you would use the system.file function with the option package
to find the location of your installed library and then load the data... something similar to the python os.path.realpah.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With