Python Forum
[Python Class] save method output to global file/list
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
[Python Class] save method output to global file/list
#1
Hi All,

--------------
class Xyz(object):

Hi All,

I want to save mutliple method output to a global file so that I can use it in another method. I am able to the output when I run sigle method. However output is over writing when I run mutliple methods at same time. My code looks like this,

class Abc(object):

    def __init__(self, net, cost):
        self.net = net
        self.cost = cost
        self.output_file = None

    def method1(self):
        self.output_file = list()
		input = {'Class': 1, 'Range': 'top'}
		output_file.append(input)

    def method2(self):
        #self.output_file = list()
		input = {'Class': 2, 'Range': 'low'}
		output_file.append(input)

    def method3(self):
		for line in self.output_file:
			print line
	


I want to save method1 & 2 output to output_file and use it in mothod3. Could you please suggest how I can achieve this? Thanks in advance.
Reply
#2
Something like this?

class FileMethod:

    _file = None

    def __init__(self):
        pass

    def __enter__(self):
        try:
            self._file = open("./logging", "w")
            return self
        except IOError as e:
            print(e)

    def method1(self):
        self._file.writelines("Printing from method1")
        self._file.writelines('\n')

    def method2(self):
        self._file.writelines("Printing from method2")
        self._file.writelines('\n')

    def __exit__(self, exc_type, exc_val, exc_tb):
        self._file.close()


def main():
    with FileMethod() as file:
        file.method1()
        file.method2()


if __name__ == "__main__":
    main()
Output:
cat logging Printing from method1 Printing from method2
Reply
#3
self.output_file = None
to
self.output_file = []
99 percent of computer problems exists between chair and keyboard.
Reply
#4
Thanks a lot Shivraj and Windspar, for your time.
I have tested Windspar suggestion and its working perfectly as required. Dance
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Accessing method attributes of python class Abedin 1 99 9 hours ago
Last Post: buran
  Read TXT file in Pandas and save to Parquet zinho 2 1,210 Sep-15-2024, 06:14 PM
Last Post: zinho
  comtypes: how to provinde a list of string to a COM method zalanthas 0 899 Jun-26-2024, 01:27 PM
Last Post: zalanthas
  Why is the copy method name in python list copy and not `__copy__`? YouHoGeon 2 1,306 Apr-04-2024, 01:18 AM
Last Post: YouHoGeon
  class definition and problem with a method HerrAyas 2 1,395 Apr-01-2024, 03:34 PM
Last Post: HerrAyas
  super() and order of running method in class inheritance akbarza 7 2,335 Feb-04-2024, 09:35 AM
Last Post: Gribouillis
  Open/save file on Android frohr 0 1,093 Jan-24-2024, 06:28 PM
Last Post: frohr
  Why doesn't list require global keyword? johnywhy 9 3,841 Jan-15-2024, 11:47 PM
Last Post: sgrey
  how to save to multiple locations during save cubangt 1 1,272 Oct-23-2023, 10:16 PM
Last Post: deanhystad
  How to read module/class from list of strings? popular_dog 1 1,280 Oct-04-2023, 03:08 PM
Last Post: deanhystad

Forum Jump:

User Panel Messages

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