2
\$\begingroup\$

I would like to use this feature

>>> def func():
>>>     return 1, 2
>>> x, y = func()

But I have a formatting problem, because I want to assign values in the __init__() of a Class.

import csv

class Filewriter:
    def __init__(self, jsonstring, filepath):
        self.katAsk, self.katBid, self.promAsk, self.promBid, self.montAsk, self.montBid, self.katProm, self.katMont, self.promKat, self.promMont, self.montKat, self.montProm, self.ticker = self.get_row()

        self.jsonstring = jsonstring
        self.filepath = filepath

    def jsonparse(self, ticker, label):
        try:
            output = self.jsonstring[ticker][label]
        except (KeyError, ValueError) as e:
            output = None
        return output

    def test(self, teststring, ticker, cx):
        return self.jsonparse(ticker, cx+"."+teststring)

    def get_row(self, ticker):

        checkdic = {"ticker": ticker}
        # Check if asks/bids exist
        checkdic["cx"] = "CI1"
        try:
            katAsk = float(self.test("ask", **checkdic))
        except (TypeError, ValueError) as e:
            katAsk = -1

        try:
            katBid = float(self.test("bid", **checkdic))
        except (TypeError, ValueError) as e:
            katBid = -1

        checkdic["cx"]="IC1"
        try:
            promAsk = float(self.test("ask", **checkdic))
        except (TypeError, ValueError) as e:
        promAsk = -1
        try:
            promBid = float(self.test("bid", **checkdic))
        except (TypeError, ValueError) as e:
            promBid = -1

        checkdic["cx"]="NC1"
        try:
            montAsk = float(self.test("ask", **checkdic))
        except (TypeError, ValueError) as e:
            montAsk = -1
        try:
            montBid = float(self.test("bid", **checkdic))
        except (TypeError, ValueError) as e:
            montBid = -1

        # bidDest - askSource = sales
        # Determine sales
        # Kat -> Prom
        if(promBid > 0 and katAsk > 0):
            katProm = promBid - katAsk
        else:
            katProm = -1

        # Kat -> Mont
        if(montBid > 0 and katAsk > 0):
            katMont = montBid - katAsk
        else:
            katMont = -1

        # Prom -> Kat
        if(katBid > 0 and promAsk > 0):
            promKat = katBid - promAsk
        else:
            promKat = -1

        # Prom -> Mont
        if(montBid > 0 and promAsk > 0):
            promMont = montBid - promAsk
        else:
            promMont = -1

        # Mont -> Kat
        if(katBid > 0 and montAsk > 0):
            montKat = katBid - montAsk
        else:
            montKat = -1

        # Mont -> Prom
        if(promBid > 0 and montAsk > 0):
            montProm = promBid - montAsk
        else:
            montProm = -1

        return katAsk, katBid, promAsk, promBid, montAsk, montBid, katProm, katMont, promKat, promMont, montKat, montProm, ticker

    def tablewriter(self):
        with open(self.filepath, mode='w', newline='') as ag:
            agWriter = csv.writer(ag, delimiter=',',
                                  quotechar='"',
                                  quoting=csv.QUOTE_MINIMAL)

            agWriter.writerow([self.ticker,
                               self.katAsk,
                               self.katBid,
                               self.promAsk,
                               self.promBid,
                               self.montAsk,
                               self.montBid,
                               "",
                               self.katProm,
                               self.katMont,
                               self.promKat,
                               self.promMont,
                               self.montKat,
                               self.montProm,
                               "",
                               ])

Is it possible to format this statement on more than one line ?

edit: edited with real code

edit2: noticed a missing "self." before test() calls

\$\endgroup\$
2
  • \$\begingroup\$ Thank you for the edit, this looks good. :) \$\endgroup\$ Commented Mar 14, 2020 at 0:27
  • \$\begingroup\$ I have to add, that I'm trying here to convert a bad written stand alone python script into a module for a webapp. I'm not sure if the whole process should be re-thinked. Maybe the use of a class is not the right one? \$\endgroup\$ Commented Mar 14, 2020 at 0:48

1 Answer 1

2
\$\begingroup\$

Use parens:

(self.katAsk, self.katBid, self.promAsk, self.promBid, self.montAsk,
 self.montBid, self.katProm, self.katMont, self.promKat, self.promMont,
 self.montKat, self.montProm, self.ticker) = self.get_row()
\$\endgroup\$
3
  • \$\begingroup\$ Those variables may be stored in a tupple? \$\endgroup\$ Commented Mar 14, 2020 at 1:04
  • \$\begingroup\$ @Halavus self.get_row returns a tuple. 1, 2 is short hand for (1, 2). \$\endgroup\$ Commented Mar 14, 2020 at 1:12
  • \$\begingroup\$ The target (the left hand side) of an assignment statement can be an iterable such as a list or tuple. Here, the commas make it a tuple, the parens just let it span multiple lines. You could use '[' and ']' instead. \$\endgroup\$ Commented Mar 14, 2020 at 1:48

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.