So, I made my own Python library, so I wrote about how to do it. However, since the pioneers are writing excellent articles, I will basically write the parts that I stumbled upon and the original parts. If you look at this article
--You can make your own library --Useful for directory structure
I think so, please take a look.
Maybe it doesn't matter, but just in case.
First of all, I would like you to take a look at my directory structure. If you read this article, you will understand the meaning of this structure. ..
.
├── .gitignore
├── MANIFEST.in
├── README.md
├── package
│ ├── __init__.py
│ └── func.py
├── notebook
│ └── test.ipynb
├── requirements.txt
└── setup.py
I said that, but in reality, most of the content is as you can see from this article.
Create a Python directory structure that you won't regret later
So, I would like to add some things that I did not implement, added, supplements, etc. that were mentioned in this article. Also, I'm actually uploading it to Github with this directory structure, so please see here if you like.
https://github.com/kotabrog/image_100
The one I wanted to make didn't need to be run on the command line, so I cut it down. The scraped ones are as follows.
setup.py
entry_points = ...
pytest
I thought this was good, but I wondered if it was necessary now. The scraped ones are as follows.
I think this depends on the case, but does it work regardless of the version? At that time, I thought that I didn't have to write the version information, so I cut it. I don't know the details, but I wondered if I could prevent it from being changed to a new version because of my own library, for example, when I had to implement it in a slightly older version. So my requirements.txt looks like this, for example.
matplotlib
numpy
scikit-image
Notebook
This is not for the library, but for posting something like this, or posting the testing process. It is unnecessary for the library and the file size will be large, so I think it would be kinder to write in the readme that it can be deleted.
Also, for that reason, I have added the following to .gitignore.
.ipynb_checkpoints/
Do you need this? I also thought, but it seems that it is better not to give it to git because it may interfere with something.
Remove jupyter checkpoints related files from git management
Of course, the article I referred to was very easy to understand, but I will write about what I researched due to my lack of knowledge. If one person stumbles, 100 people will stumble (no), so it will surely be useful to someone.
How do you do a general folder structure other than your own library? Because it was the situation, I used it as a reference in various ways.
Project Structure Structure of project Folder structure for data analysis
I was wondering how to read the function I made from another file, so I will summarize it.
Modularize and package your own functions with python Specify upper directory / subdirectory with relative import of Python How to use Python, import (from, as, recommended style of PEP8, notes, etc.)
I only knew it somehow, so I did a lot of research.
--You can see this for the time being-> What is Python's \ _ \ _ init \ _ \ _. Py --You can cut the module name-> How to write Python \ _ \ _ init \ _ \ _. Py
Also, if \ _ \ _ all \ _ \ _ is set,
from package import *
However, when it is troublesome to write \ _ \ _ all \ _ \ _ by yourself, there was a code to write in \ _ \ _ init \ _ \ _.
-About \ _ \ _ all \ _ \ _-> Explanation of Python special variables \ _ \ _ all \ _ \ _ by active engineers [for beginners] --How to insert-> [Put all python files in the same hierarchy as \ _ \ _ init \ _ \ _. Py into \ _ \ _ all \ _ \ _](https://medium.com/@hokan_dev/init- py% E3% 81% A8% E5% 90% 8C% E3% 81% 98% E9% 9A% 8E% E5% B1% A4% E3% 81% AB% E3% 81% 82% E3% 82% 8B python% E3% 83% 95% E3% 82% A1% E3% 82% A4% E3% 83% AB% E3% 82% 92% E5% 85% A8% E9% 83% A8-all-% E3% 81% AB % E7% AA% 81% E3% 81% A3% E8% BE% BC% E3% 82% 80-ac632f71594)
However, this method may not work depending on the environment. The reason is that the way to connect directories is different between windows and mac ... (Why couldn't they be unified ...) So, I will put the rewritten one referring to the following.
Split strings in Python (delimiters, newlines, regular expressions, number of characters)
__init__.py
import glob
import os
py_list = glob.glob('package/*.py')
__all__ = list(map(lambda file_path: os.path.basename(file_path).split('.', 1)[0], py_list))
__all__.remove('__init__')
By the way, if you do this, you will be able to call everything, so when you want to choose what you can call, you should write it manually or put together the things you do not want to call in a file and delete it from all ... It may be.
What is \ _ \ _ pycache \ _ \ _?
So, I didn't have the ability to write better articles than the pioneers, so I made it feel like a summary. If there is something like "This is different!", I would be grateful if you could let me know.
Recommended Posts