Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8055481
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T08:23:59+00:00 2026-06-05T08:23:59+00:00

How to combine two functions together I have a class controlling some hardware: class

  • 0

How to combine two functions together

I have a class controlling some hardware:

class Heater()
    def set_power(self,dutycycle, period)
       ...
    def turn_on(self)
       ...
    def turn_off(self)

And a class that connects to a database and handles all data logging functionality for an experiment:

class DataLogger()
    def __init__(self)
        # Record measurements and controls in a database
    def start(self,t)
        # Starts a new thread to acquire and record measuements every t seconds

Now, in my program recipe.py I want to do something like:

        log = DataLogger()

        @DataLogger_decorator
        H1 = Heater()

        log.start(60)

        H1.set_power(10,100)
        H1.turn_on()
        sleep(10)
        H1.turn_off()
        etc

Where all actions on H1 are recorded by the datalogger. I can change any of the classes involved, just looking for an elegant way to do this. Ideally the hardware functions remain separated from the database and DataLogger functions. And ideally the DataLogger is reusable for other controls and measurements.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-05T08:24:00+00:00Added an answer on June 5, 2026 at 8:24 am

    For this scenario, I prefer using DataLogger as a BaseClass or Mixin for other classes rather than trying to do some sort of decorator magic (which doesn’t really click for me as a pythonic way to use a decorator)

    e.g.:

    class DataLogger(object):
      def __init__(self):
          # do init stuff
    
      def startlog(self, t):
          # start the log 
    
    
    class Heater(DataLogger):
       def __init__(self):
          # do some stuff before initing your dataLogger
          super(Heater, self).__init__() # init the DataLogger
       #other functions
    

    that way you can just do:

    h1 = Heater()
    h1.startlog(5)
    h1.do_other_stuff()
    

    An example that uses it as a mixin for an existing class:

    class DataLoggerMixin(object): 
      def __init__(self):
        # do your init things
        super(DataLogger, this).__init__()  # this will trigger the next __init__ call in the inheritance chain (i.e. whatever you mix it with)
    
    class Heater(object):
        """ Here's a heater you have lying around that doesn't do data logging.  No need to change it."""
    
    # add a new child class with 2 lines, that includes the DataLoggerMixin as the first parent class, and you will have a new class with all the DataLogging functionality and the Heater functionality. 
    class LoggingHeater(DataLoggerMixin, Heater):
        """ Now its a data logging heater """
        pass  # no further code should be necessary if you list DataLoggerMixin first in the base classes. 
    
    
    >>> logging_heater = LoggingHeater()
    >>> logging_heater.start_log(5)
    >>> logging_heater.do_heater_stuff()
    

    The key with successfully using mixins in python is to understand how the method resolution order (MRO), particularly for super, works in a multiple inheritance situation. See this on cooperative multiple inheritance.

    ____________________________________________________________________
    

    Alternative Method: Use a Wrapper Class

    If Mixin methodology doesn’t work for your scheme, another option would be to use DataLogger as a wrapper class for objects to be logged. Basically Data Logger would accept an object to do logging on in its constructor like so:

    class DataLogger(object)
      def __init__(self, object_to_log)
        self.object = object_to_log   # now you have access to self.object in all your methods.
        # Record measurements and controls in a database
      def start(self,t)
        # Starts a new thread to aqcuire and reccord measuements every t secconds
    

    I’m not sure what type of logging or monitoring is done and whether you need access to the object you’re logging or if it is independent. If the former, presumably Heater, Valve, etc. all implement the same functions that DataLogger cares about so you can Log for them regardless of what class they are. (This is a handy core feature of dynamic languages like Python called “Duck typing”, where you can operate on different types, as long as the types implement the functions or attributes you care about. “if it quacks like a duck . . .”)

    Your code might look more like this, using wrapper class methodology:

    h1 = Heater() 
    log = DataLogger(h1)
    log.start(60)
    h1.set_power(10,100)
    h1.turn_on()
    sleep(10)
    h1.turn_off()
    

    Hope this helps!

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

How can I combine these two functions into one recursive function to have this
I have two Ajax requests that I am wanting to combine together into one.
I have two NSMutableArrays of strings that I wish to combine and shuffle together.
I'm trying to combine two separate javascript functions' outputs but each function is on
I have a merge function which takes time O(log n) to combine two trees
How can you combine two windows in Screen? I have two windows such that
I want to combine two array's, excluding duplicates. I am using a custom class:
I have the following query where I combine two fields from two tables: <cfquery
I have two sources of clinical procedure billing information that I have added together
Let's say I have a div with two anchors: <div id=#dialog> <a href=# class=ok>Delete</a>

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.