[PYTHON] [Numpy / pandas / matplotlib Exercise 01] Update template

At first

Before proceeding with the exercise in the title, I want to learn the basics of python, so I think that the template created previous will be modified and used. I don't think it's necessary to make it that way, but I'll proceed with the spirit of remembering it as I use it (I have to use it), so if there's something that's obviously dangerous, I'm sorry to trouble you. Please point out. .. ..

It's a personal method, but if you're studying, I think it's better to think about using the knowledge you've learned forcibly than Kodawaru to write beautiful code. I'm going to learn what's wrong while failing.

Template

Add class

sample.py


# -*- coding:utf-8 -*-

import logging
import pandas as pd
import matplotlib.pyplot as plt
import numpy as np
import japanize_matplotlib	### [Japanese support]

#Specifying the log format
# %(asctime)s :A human-readable representation of the time the LogRecord was generated.
# %(funcName)s :The name of the function that contains the logging call
# %(levelname)s :Character logging level for messages
# %(lineno)d :Source line number where the logging call was issued
# %(message)s : msg %Log message requested as args
fomatter = logging.Formatter('%(asctime)s:%(funcName)s:%(levelname)s:%(lineno)d:\n%(message)s')

#Logger settings(INFO log level)
logger = logging.getLogger(__name__)
logger.setLevel(logging.INFO)

#Handler settings(Change output file/Log level settings/Log format settings)
handler = logging.FileHandler('handler_log.log')
handler.setLevel(logging.INFO)
handler.setFormatter(fomatter)

logger.addHandler(handler)

class SampleClass(object) :
	def SampleFunc(self) :
		try :
			logger.info("Hello World")

		#Exception handling
		except :
			logger.exception("", exc_info=True)

if __name__ == '__main__' :

	try :
		
		main_Class = SampleClass()
		main_Class.SampleFunc()
		
	#Exception handling
	except :
		logger.exception("", exc_info=True)

Execution result

2019-11-22 16:54:28,955:SampleFunc:INFO:31:
Hello World

Added class initialization

sample.py


(Omitted)
class SampleClass(object) :
	def __init__(self) :
		try :
			logger.info("Class initialization")

		#Exception handling
		except :
			logger.exception("", exc_info=True)
(Omitted)

Execution result

2019-11-22 17:00:41,888:__init__:INFO:31:
Class initialization
2019-11-22 17:00:41,889:SampleFunc:INFO:39:
Hello World

In-class function argument initialization and in-class function invocation

sample.py


(Omitted)
class SampleClass(object) :
	def __init__(self, argv="sample_01") :
		try :
			self.argv = argv
			logger.info("Class initialization: " + argv)

		#Exception handling
		except :
			logger.exception("", exc_info=True)
	
	def SampleFunc(self) :
		try :
			logger.info("Hello World : " + self.argv)
			self.SampleFunc02()

		#Exception handling
		except :
			logger.exception("", exc_info=True)

	def SampleFunc02(self, argv02="InitSample02") :
		try :
			logger.info("SampleFunc02 : " + argv02)

		#Exception handling
		except :
			logger.exception("", exc_info=True)
(Omitted)

Execution result

2019-11-22 17:12:43,213:__init__:INFO:32:
Class initialization: sample_01
2019-11-22 17:12:43,213:SampleFunc:INFO:40:
Hello World : sample_01
2019-11-22 17:12:43,213:SampleFunc02:INFO:49:
SampleFunc02 : InitSample02

Destructor

This destructor has been hard to understand since I was studying C ++. .. .. For the time being, I will make a lot of mistakes and I will take a stance to remember while making mistakes, so I will try to incorporate it forcibly.

sample.py


(Omitted)
def __del__(self) :
	try :
		print('This Class is deleted')

	#Exception handling
	except :
		logger.exception("", exc_info=True)
(Omitted)

Execution result

abridgement

Class inheritance

You can use inheritance in the same way, but I will do it in my own way because I am self-taught.

sample.py


(Omitted)
class ChildClass01(SampleClass) : 
	pass

class ChildClass02(SampleClass) : 
	def ChildClass02Func(self) :
		try :
			logger.info("ChildClass02Func Hello World : " + self.argv)

		#Exception handling
		except :
			logger.exception("", exc_info=True)
(Omitted)
if __name__ == '__main__' :

	try :
		
		#Creating class variables(at this point__init__Function is called)
		main_Class = SampleClass()
		main_Class1 = ChildClass01("★")
		main_Class2 = ChildClass02("■")
		#Calling a class function
		main_Class.SampleFunc()
		main_Class1.SampleFunc()
		main_Class2.SampleFunc()
		main_Class2.ChildClass02Func()
(Omitted)

Execution result

2019-11-22 17:37:03,556:__init__:INFO:32:
Class initialization: sample_01
2019-11-22 17:37:03,556:__init__:INFO:32:
Class initialization: ★
2019-11-22 17:37:03,556:__init__:INFO:32:
Class initialization: ■
2019-11-22 17:37:03,556:SampleFunc:INFO:40:
Hello World : sample_01
2019-11-22 17:37:03,556:SampleFunc02:INFO:49:
SampleFunc02 : test
2019-11-22 17:37:03,556:SampleFunc:INFO:40:
Hello World : ★
2019-11-22 17:37:03,557:SampleFunc02:INFO:49:
SampleFunc02 : test
2019-11-22 17:37:03,557:SampleFunc:INFO:40:
Hello World : ■
2019-11-22 17:37:03,557:SampleFunc02:INFO:49:
SampleFunc02 : test
2019-11-22 17:37:03,557:ChildClass02Func:INFO:69:
ChildClass02Func Hello World : ■

Finally

There are many other things I would like to try, but since I have exceeded the capacity of my brain, I would like to forcibly write a program using this template (while reviewing it as appropriate) next time.

Recommended Posts

[Numpy / pandas / matplotlib Exercise 01] Update template
[Numpy / pandas / matplotlib Exercise 01]
Install Python3, numpy, pandas, matplotlib, etc. on Windows
Introduction to Python numpy pandas matplotlib (~ towards B3 ~ part2)
Python Basic --Pandas, Numpy-
pandas Matplotlib Summary by usage
NumPy and matplotlib environment construction