Bottom Page

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.
Quote
#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
Quote
#3
self.output_file = None
to
self.output_file = []
99 percent of computer problems exists between chair and keyboard.
hobby-programmer.com
Quote
#4
Thanks a lot Shivraj and Windspar, for your time.
I have tested Windspar suggestion and its working perfectly as required. Dance
Quote

Top Page

Possibly Related Threads...
Thread Author Replies Views Last Post
  Details of attachment files in a msg file such as file names save into a python list klllmmm 2 127 Nov-12-2019, 05:59 AM
Last Post: klllmmm
  how can I changing a base class method voidptr 2 61 Nov-10-2019, 10:53 PM
Last Post: voidptr
  Difference in list output OokaydO 6 262 Nov-09-2019, 12:33 AM
Last Post: OokaydO
  How to Call a method of class having no argument dataplumber 7 320 Oct-31-2019, 01:52 PM
Last Post: dataplumber
  print all method and property of list object engmoh 4 258 Oct-26-2019, 05:33 PM
Last Post: engmoh
  How to extract a matrix from .xml.gz file to a excel file or any other output? enyrb 0 83 Oct-21-2019, 01:01 PM
Last Post: enyrb
  Writing list as a file, then reading that file as a list Zoastria_Balnala 3 144 Oct-17-2019, 07:54 PM
Last Post: Zoastria_Balnala
  Read each line, replace string and save into a new file igormonteiro 2 220 Sep-15-2019, 01:24 PM
Last Post: buran
  Call method from another method within a class anteboy65 3 145 Sep-11-2019, 08:40 PM
Last Post: Larz60+
  Appending a list in a class from a callback function snizbatch 5 206 Sep-01-2019, 06:27 AM
Last Post: snizbatch

Forum Jump:


Users browsing this thread: 1 Guest(s)