Python Forum
How to move a class to a custom module?
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
How to move a class to a custom module?
#3
(Feb-28-2022, 04:42 PM)deanhystad Wrote: I am confused. In our test you used the global variable "i" and you incremented it like htis:
i += 1; logger.info("I am INFO 0")
In log_format.py you changed "i" to an instance variable and you stop incrementing it.

Why do you expect "i" to change if you don't change it?

Thank you for your reply.
Actually in RUN 2 I did try to have class ContextFilter as
class ContextFilter(logging.Filter):
    def filter(self, record):
        record.log_count = i
        return True
And call the logger.addFilter as
logger.addFilter(my.ContextFilter())
but then I get following error:
Traceback (most recent call last):
  File "./log_format_test.py", line 36, in <module>
    i += 1; logger.info("I am INFO 0")
  File "~/miniconda3/lib/python3.6/logging/__init__.py", line 1301, in info
    self._log(INFO, msg, args, **kwargs)
  File "~miniconda3/lib/python3.6/logging/__init__.py", line 1437, in _log
    self.handle(record)
  File "~/miniconda3/lib/python3.6/logging/__init__.py", line 1446, in handle
    if (not self.disabled) and self.filter(record):
  File "~/miniconda3/lib/python3.6/logging/__init__.py", line 713, in filter
    result = f.filter(record)
  File "/local_home/log_format.py", line 19, in filter
    record.log_count = i
NameError: name 'i' is not defined
I am new to object oriented programming so don't understand it very well yet. I tried defining i as global in log_format_test.py and tried passing it as
logger.addFilter(my.ContextFilter(i))
and many other combinations. The problem that you pointed was one of those many attempts.

I think I am missing something very basic here.
Reply


Messages In This Thread
RE: How to move a class to a custom module? - by python300 - Feb-28-2022, 05:20 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
  How to read module/class from list of strings? popular_dog 1 599 Oct-04-2023, 03:08 PM
Last Post: deanhystad
  My code displays too much output when importing class from a module lil_e 4 1,327 Oct-22-2022, 12:56 AM
Last Post: Larz60+
  Error when refering to class defined in 'main' in an imported module HeRo 2 2,526 Apr-13-2021, 07:22 PM
Last Post: HeRo
  Custom file class deanhystad 11 4,684 Feb-01-2021, 05:09 PM
Last Post: nilamo
  importing same python library in multiple custom module escape_freedom13 6 4,015 May-10-2020, 07:01 PM
Last Post: escape_freedom13
  How to serialize custom class objects in JSON? Exsul1 4 3,637 Sep-23-2019, 08:27 AM
Last Post: wavic
  How can I save a class as a module sakawaka 2 5,156 Feb-07-2018, 03:01 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