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
  Why is the copy method name in python list copy and not `__copy__`? YouHoGeon 2 249 Apr-04-2024, 01:18 AM
Last Post: YouHoGeon
  class definition and problem with a method HerrAyas 2 227 Apr-01-2024, 03:34 PM
Last Post: HerrAyas
  super() and order of running method in class inheritance akbarza 7 722 Feb-04-2024, 09:35 AM
Last Post: Gribouillis
  Open/save file on Android frohr 0 314 Jan-24-2024, 06:28 PM
Last Post: frohr
  Why doesn't list require global keyword? johnywhy 9 795 Jan-15-2024, 11:47 PM
Last Post: sgrey
  how to save to multiple locations during save cubangt 1 542 Oct-23-2023, 10:16 PM
Last Post: deanhystad
  How to read module/class from list of strings? popular_dog 1 468 Oct-04-2023, 03:08 PM
Last Post: deanhystad
  save values permanently in python (perhaps not in a text file)? flash77 8 1,204 Jul-07-2023, 05:44 PM
Last Post: flash77
  Why Pip is not listed in the official Global Python Module index? quazirfan 2 755 Apr-21-2023, 10:55 AM
Last Post: snippsat
  Reading data from excel file –> process it >>then write to another excel output file Jennifer_Jone 0 1,089 Mar-14-2023, 07:59 PM
Last Post: Jennifer_Jone

Forum Jump:

User Panel Messages

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