Python Forum
Improving my understanding of functions and methods
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Improving my understanding of functions and methods
#1
I'm taking some advice from one of the tips here and using pylint and pylint3.
I'm using it on the code I posted in the review. I got the original code only throwing to warnings about(too many instances) which I am working on now.
On some of the code for my methods says could be a function. If I put self in front it, it goes away. Using the code I posted, it should be safe to day for the header image and the footer I could choose to put those as functions in the top of my program. They never change. For data manipulation those should be methods. Example I can use methods to pull the titles out and display. The same for the information that the titles display when clicked.

Just need input best practices most used. A lot of code I've seen seems to be what the author favors.

On a side note, I've noticed pylint/pylint3 throws warnings if too many methods are in a class. Is this also a preference? Whether it be one or 100? I know a class creates an object and methods describe what the object look like so to speak. Say the class creates person and the methods give that person brown hair, height, build, and so forth. Well anyway any input would be great.

Thanks
I welcome all feedback.
The only dumb question, is one that doesn't get asked.
My Github
How to post code using bbtags


Reply
#2
I think you're going to need to post code if you want to talk about code. Could you post a minimal example that causes pylint to whine?
Reply
#3
This is the complaint from pylint. Im re-working the scrpits to get rid of it. Seems adding classes do the trick.
Output:
************* Module test R: 91, 0: Too many instance attributes (11/7) (too-many-instance-attributes) ------------------------------------------------------------------ Your code has been rated at 9.91/10 (previous run: 9.91/10, +0.00)
class MainWindow:
    '''Docstring'''

    def __init__(self):
        self.root_window()
        self.container_frames()
        self.left_inner_frame()
        self.right_inner_frame()
        self.logo()
        self.show_footer()

    def root_window(self):
        '''Docstring'''
        self.root = ttk.Frame()
        self.root.grid_columnconfigure(0, weight=1)
        self.root.grid_rowconfigure(0, weight=1)

    def container_frames(self):
        '''Doc'''
        self.logo_frame = ttk.Frame(self.root)
        self.logo_frame.grid(column=0, row=0, sticky='new')
        self.logo_frame.grid_columnconfigure(0, weight=3)

        self.letter_menu_frame = ttk.Frame(self.root, relief='raised', padding=5)
        self.letter_menu_frame.grid(column=0, row=1, sticky='new')
        for i in range(26):
            self.letter_menu_frame.grid_columnconfigure(i, weight=3)

        self.multigrid_frame = ttk.Frame(self.root)
        self.multigrid_frame.grid(column=0, row=2, sticky='nw')
        self.multigrid_frame.grid_columnconfigure(0, weight=3)

        self.footer_frame = ttk.Frame(self.root)
        self.footer_frame.grid(column=0, row=3, sticky='new')
        self.footer_frame.grid_columnconfigure(0, weight=3)

    def left_inner_frame(self):
        '''Doc'''

        self.menu_title_frame = ttk.Frame(self.multigrid_frame)
        self.menu_title_frame.grid(column=0, row=0, sticky='nsw')
        self.menu_title_frame.grid_columnconfigure(0, weight=3)
        self.menu_title_frame.grid_rowconfigure(0, weight=3)

    def right_inner_frame(self):
        '''Doc'''

        self.get_recipe_frame = ttk.Frame(self.multigrid_frame)
        self.get_recipe_frame.grid(column=1, row=0, sticky='nsw')
        self.get_recipe_frame.grid_columnconfigure(0, weight=3)
        self.get_recipe_frame.grid_rowconfigure(0, weight=3)

    def logo(self):
        '''Docstring'''

        self.imgfile = tk.PhotoImage(file='/home/johnny/Desktop/play/cookbook_logo.png')
        self.img_logo = ttk.Label(self.logo_frame, image=self.imgfile, anchor='n')
        self.img_logo.grid(column=0, row=0, sticky='new')

    def show_footer(self):
        '''Docstring'''

        self.style = ttk.Style()
        self.style.map('my_footer.TLabel')
        self.style.configure('my_footer.TLabel', \
        foreground='blue', font=('Sans', 9, 'normal'), padding=8, border=2)

        self.footer = ttk.Button(self.footer_frame, \
        text='Register @ Johnny\'s CookBook', \
        style='my_footer.TLabel', cursor='hand2', command=web)

        self.footer.grid(column=0, row=0, sticky='n')
        self.footer.grid_columnconfigure(0, weight=3)

def main():
    '''Socstring'''
    root = tk.Tk()
    root.title('Johnny\'s CookBook')
    imgfile = tk.PhotoImage(file='/home/johnny/Desktop/play/cookbook_logo.png')
    root.configure(width=imgfile.width(), border=5, relief='ridge')
    root.resizable(width=False, height=False)
    MainWindow()
    root.mainloop()

if __name__ == '__main__':
    main()

I disabled the message with # pylint: disable=R0902

Was going to do it in the configuration but decided that someone though 7 was enough in coding?
I welcome all feedback.
The only dumb question, is one that doesn't get asked.
My Github
How to post code using bbtags


Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  functions/methods menator01 2 1,590 Oct-02-2021, 07:37 PM
Last Post: menator01
  Need help improving function that reads file into list of tuples t4keheart 6 2,986 Nov-03-2020, 04:58 AM
Last Post: ndc85430
  Improving code to autorename if filename exists Den0st 5 2,946 Sep-23-2019, 07:53 AM
Last Post: wavic
  Need help understanding a couple of functions (encrypt,decrypt, int_to_bytes) xoani 0 1,968 Jun-09-2019, 03:25 PM
Last Post: xoani
  Improving bot SheeppOSU 0 19,097 Jun-01-2019, 08:06 PM
Last Post: SheeppOSU
  How to find functions or methods in a module? deepakdeshp 8 4,174 May-23-2019, 09:36 AM
Last Post: DeaD_EyE
  Is there a way of improving this leaderboard system? Zelpha 2 3,833 Feb-11-2019, 06:32 PM
Last Post: ichabod801
  i have problems understanding 2 functions,look for help. Tony 2 2,597 Dec-05-2018, 12:35 PM
Last Post: Tony
  Help in understanding scope of dictionaries and lists passed to recursive functions barles 2 3,149 Aug-11-2018, 06:45 PM
Last Post: barles
  Improving Efficiency of SVM by various available kernels Sachtech 0 2,058 Apr-09-2018, 07:29 AM
Last Post: Sachtech

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020