I made my own Python library

Overview

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.

environment

Maybe it doesn't matter, but just in case.

My directory structure

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

What I didn't implement

Command line execution function

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.

Requirements version

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

What I added

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

Supplement (or rather, what I stumbled upon)

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.

About folder structure

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

How do you do import ...?

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.)

What is \ _ \ _ init \ _ \ _!

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.

Other

What is \ _ \ _ pycache \ _ \ _?

At the end

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

I made my own Python library
I made my own language. (1)
I made my own language (2)
I made my own AML
[Python] I made my own library that can be imported dynamically
I published my own Python baseball library to Packaging & PyPI
I made my goimports
Python> I made a test code for my own external file
I made my own primitive static site generator
I made blackjack with python!
I made a python text
I made blackjack with Python.
I made wordcloud with Python.
I made my own parallel link robot (software version)
I made a python library to do rolling rank
I made my own parallel link robot (mechanical edition)
I made a Line-bot using Python!
I made a fortune with Python.
I made my own OSS because I wanted to contribute to it
I made a daemon with Python
I made a library to easily read config files with Python
I made my own filter plugin for text parsing of Ansible
I made a library that adds docstring to a Python stub file.
I made a Python wrapper library for docomo image recognition API.
I made Othello to teach Python3 to children (4)
I made a payroll program in Python!
I made a character counter with Python
I made Othello to teach Python3 to children (2)
[Python] Register your own library on PyPI
I am making my own aws cli
Until you install your own Python library
I made Othello to teach Python3 to children (5)
I made a Hex map with Python
Publish your own Python library with Homebrew
After studying Python3, I made a Slackbot
I made a roguelike game with Python
I made Othello to teach Python3 to children (3)
I made Othello to teach Python3 to children (1)
I made a simple blackjack with Python
I made a configuration file with Python
I made a library for actuarial science
I made a neuron simulator with Python
Python 3.6 email library
My Numpy (Python)
My sys (python)
My pyproj (python)
I made my own research tool using the legal API [Smart Roppo]
Since I made my own smart lock, I will summarize the difficult points
My str (python)
I started python
My PySide (Python)
Python Library notes
My shutil (python)
My matplotlib (python)
I don't have the skills or strength, but I made my own compiler
My urllib (python)
My pyperclip (python)
My sklearn (python)
[My memo] python
My ConfigParser (Python)
My Webdriver (Python)