Python Forum
first try with python class, suggestion for improvement please
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
first try with python class, suggestion for improvement please
#2
(Oct-24-2019, 11:03 AM)anna Wrote: 1) is it good practice to have multiple class in single script?
2) how can i return converted values within class?
3) Guide me for improvement

1) Yes, if they are all related. For example, I have a cards.py file in my t_games project, which has two classes for different kinds of cards, a class for a deck of each of those card objects, and a class for a hand of cards. But then all of the classes for board games are in another file, and the different player classes are in a third file, and so on.

2) I'm not sure exactly what you mean, but by converted values do you mean things like get_fanstatus on line 22? If so, you can return that value into another method:

class Foo(object):

    def __init__(self, num):
        self.id = self.get_id(num)

    def get_id(self, num):
        return num + 1 * 2 ** 3
3) Here's what I noted skimming through your class, not really trying to understand what the code is doing:

3a) Comment your code. Each method or function should have a docstring explaining what it does and what it's parameters are. Each class should have a docstring explaining what it's for, what's it's methods do, and what it's attributes are for. The way I do it is like this:

class Foo(object):
    """
    A object for demostrating conversion of values. (object)

    Attributes:
    id: An ID number for the foo meeting the Scotty criteria. (int)

    Methods:
    get_id: Convert a number to a valid ID. (int)
    """

    def __init__(self, num):
        """
        Set up the foo's ID. (None)

        Parameters:
        num: The integer basis for the foo. (int)
        """
        self.id = self.get_id(num)

    def get_id(self, num):
        """
        Convert a number to a Scotty ID.  (int)

        Parameters:
        num: The base number for the Scotty ID. (int)
        """
        return num + 1 * 2 ** 3
That may seem like too much, but that's because it's just a ditzy little test class. If it still seems like too much after you do it with your code, that's because you haven't yet had to deal with other people's undocumented code. And other people includes you six months ago. Just because it's totally clear to you now doesn't mean it's totally clear to someone else or somewhen else.

In addition to the docstrings, methods longer than 4 or 5 lines, or that go through multiple steps (as you conceive steps), should have an outline of what they are doing mixed in with the code using # comments.

3b) Be consistent with your variable names. Generally you have different styles for variables, functions/methods, classes, and global constants. You see this a lot in Python: variable_names, function_names, ClassNames, CONSTANT_NAMES. That's PEP 8 style, which is common but not mandatory. However, being consistent with whatever style you use helps make the role of each name clear in the code.

3c) FanSpeed and fanstatus should be class variables. They're don't change but they are closely related to the class. I would do something like this:

class Fan:
    fan_speed = { '0':'Not Applicable',
                 '1':'Unknown',
                 '2':'Half Speed',
                 '3':'Full Speed',
                 '4':'Low Speed'
               }
    fan_status = { '1':'Unknown',
                  '2':'Fan Removed',
                  '3':'Up',
                  '4':'Fail',
                  '5':'Out of Service'
                }
                
    def __init__(self,host,community,version,timeout):
        self.host = host
        self.community = community
        self.version = version
        self.timeout = timeout

    def get_fan_speed(value):
        return self.fan_speed.get(value,"other")

    def get_fan_status(value):
        return self.fan_status.get(value,"other")
Likewise your Record namedtuple should either be a class attribute or a module level constant. As it is you are reprocessing these objects every time you call those methods. There is no need to do that.

3d) Try/except blocks should be tight and specific. Try to limit the code in the try block to just what will cause the error. If you have one or two lines to process if there is no error, they can go in there. If there's more than a couple of lines to process if there is no error, the except block should return, continue, or break, or there should be an else block for that code. The except statement should specify the specific errors you are expecting. Otherwise they might hide unexpected errors as expected errors, making it harder to debug the unexpected ones. The try/except block is especially heinous.
Craig "Ichabod" O'Brien - xenomind.com
I wish you happiness.
Recommended Tutorials: BBCode, functions, classes, text adventures
Reply


Messages In This Thread
RE: first try with python class, suggestion for improvement please - by ichabod801 - Oct-24-2019, 02:59 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
  Suggestion on how to speed up this code? sawtooth500 1 274 May-04-2024, 07:13 PM
Last Post: sawtooth500
  Can you give me some suggestion about PCEP Newbie1114 0 1,054 Oct-14-2021, 03:02 PM
Last Post: Newbie1114
  instagram followers name without suggestion for you jacklee26 1 3,225 Oct-02-2021, 04:57 AM
Last Post: ndc85430
  Random coordinate generator speed improvement saidc 0 2,085 Aug-01-2021, 11:09 PM
Last Post: saidc
  Python Dev suggestion: Combining line-wrap with comments inside functions NikoNiels 2 1,776 Sep-26-2020, 07:45 PM
Last Post: bowlofred
  Function Improvement Santino 1 1,843 May-23-2020, 03:30 PM
Last Post: jefsummers
  Name Mashup Program Improvement in Python rhat398 3 2,639 Apr-05-2020, 12:09 PM
Last Post: perfringo
  Optimization suggestion Julia 2 1,776 Mar-29-2020, 12:02 PM
Last Post: Julia
  Need suggestion how to handle this error farrukh 1 2,329 Dec-21-2019, 03:21 PM
Last Post: DeaD_EyE
  Python Based Data QA Automation tool suggestion Sonia567 1 2,033 Nov-19-2019, 04:46 PM
Last Post: Larz60+

Forum Jump:

User Panel Messages

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