The simplest Python memo in Japan (classes and objects)

I couldn't understand the class and the object, so I wrote it.

What is a class? (There are various analogies, and the explanation seems to differ depending on the kindness of the person who explains it.)

--Object: A set of variables and related functions ⇒ Easy to program because it is a set --Class: The name of the object template --Constructor ... Initial value of template * Init is surrounded by two underscores, and self is added to the argument. --Method: A function defined in the class. Use this function to run the class

--Instance: A base that calls the constructor of the class (template) and actually operates it. --Member: Refer to the class (template) argument

When applied to this program

--Object: A set of variables related to a function that sorts by file name in descending order and measures the size of a file. --Class: FileControl --Constructor ... Declare a dir variable to store the file reference as an argument --Method: Declare make_r_list to sort by file name in descending order and size_check to get the file size.

--Instance: Create an instance path by calling FileControl with a file reference as an argument. --Member: Check the stored value with dir that stores the argument in the instance path.

In other words

"We called the FileControl class to create a path instance, sorted the files in descending order with the make_r_list method, and measured the size of the file with the size_check method."

python


import os
import pprint
#Define FiileControl class
class FileControl:
    #Define constructor
    def __init__(self,dir):
        self.dir=dir
    #Method to sort by file name in descending order
    def make_r_list(self):
        dirs=[]
        for n in os.listdir(self.dir):
                            #↓ Add the full path to the array because the full path is required to measure the file size.
            dirs.append(self.dir+n)
        dirs.sort(reverse=True)
        return dirs
    #Method to get file size
    def size_check(self,fname):
        s_var=os.path.getsize(fname)
        s_var=s_var/(1024*100)
        return s_var
#↑↑↑↑↑ Everything above is defined.
#↓ ↓ ↓ ↓ ↓ From here on down is the actual operation.
#Call FileControl to create a path (instance)
path=FileControl('c:/users/user/desktop/PDF/')
#Confirmation of members
print(path.dir)
#Sort by file name in descending order and return a list, make_r_run list
list=path.make_r_list()
#Get the file size by reading the files one by one from the list variable size_execute check method
for f in list:
#Because format can be displayed at the same time without converting the integer type
    pprint.pprint('file name:{0}--file size_{1:.1f}KB'.format(f,path.size_check(f)))

Execution result. The file name and size (KB conversion) are displayed. class.png

Is it like attaching tires (methods) and pedals (methods) to a bicycle frame (class) and giving air (arguments) to the tires and propulsion (arguments) to the pedals? Eventually I saw somewhere that every language had to remember arrays and classes, so I wrote it.

Recommended Posts

The simplest Python memo in Japan (classes and objects)
About python objects and classes
Assignments and changes in Python objects
Examine the object's class in python
Open an Excel file in Python and color the map of Japan
[Introduction to Python3 Day 12] Chapter 6 Objects and Classes (6.3-6.15)
Organize the meaning of methods, classes and objects
About the difference between "==" and "is" in python
[Introduction to Python3 Day 11] Chapter 6 Objects and Classes (6.1-6.2)
Receive the form in Python and do various things
[python] Get the list of classes defined in the module
Carefully understand the exponential distribution and draw in Python
Plot and understand the multivariate normal distribution in Python
Carefully understand the Poisson distribution and draw in Python
Find the Hermitian matrix and its eigenvalues in Python
Download the file in Python
Find the difference in Python
About Python variables and objects
Python and ruby slice memo
Equivalence of objects in Python
Python classes learned in chemoinformatics
Stack and Queue in Python
Unittest and CI in Python
[Memo] The mystery of cumulative assignment statements in Python functions
List method argument information for classes and modules in Python
Get the MIME type in Python and determine the file format
Sort and output the elements in the list as elements and multiples in Python.
Manipulate the clipboard in Python and paste the table into Excel
I tried programming the chi-square test in Python and Java.
[Python] Display the elapsed time in hours, minutes, and seconds (00:00:00)
Get the current date and time in Python, considering the time difference
[Tips] Problems and solutions in the development of python + kivy
Graph the Poisson distribution and the Poisson cumulative distribution in Python and Java, respectively.
Determine the date and time format in Python and convert to Unixtime
Difference between java and python (memo)
The story of Python and the story of NaN
MIDI packages in Python midi and pretty_midi
Count the number of Thai and Arabic characters well in Python
Getting the arXiv API in Python
Difference between list () and [] in Python
perl objects and python class part 2.
Difference between == and is in python
Python ABC-Abstract Classes and Duck Typing
View photos in Python and html
Sorting algorithm and implementation in Python
Python in the browser: Brython's recommendation
Save the binary file in Python
Hit the Sesami API in Python
[Python] df Read and do the first memo (NaN confirmation etc.)
Web application development memo in python
New Python grammar and features not mentioned in the introductory book
Get the desktop path in Python
Manipulate files and folders in Python
About dtypes in Python and Cython
Get the script path in Python
In the python command python points to python3.8
Implement the Singleton pattern in Python
[Python] About Executor and Future classes
Check and move directories in Python
Ciphertext in Python: IND-CCA2 and RSA-OAEP
Hashing data in R and Python