0

I am still noob at OOP.

class ItemPrices:
def __init__(self, name=" ", exval=0, chval=0, curtype= " "):
    self.name = name
    self.exaltedValue = exval
    self.chaosValue = chval
    self.currType = curtype
  def Incubator_Values(self):
    response_API = requests.get("https://poe.ninja/api/data/itemoverview?league=Scourge&type=Incubator")
    data = response_API.text
    os.chdir(
        r"C:\Users\emosc\PycharmProjects\GithubPushs\psychescape_price_fetcher\psychescape_price_fetcher\values")
    parse_json = json.loads(data)
    with open("incubators.txt", "w") as file:
        file.write(" ")
    j = 0
    for i in parse_json["lines"]:
        list = []
        if (i["chaosValue"] > chaos_ex_ratio):
            self.name = i["name"]
            self.exaltedValue = i["exaltedValue"]
            self.currType = "Exalted Orb"
            print(self.name)
            list.append(self)
            j = j + 1
            print("Current iteration> ", j)
        else:
            self.name = i["name"]
            self.exaltedValue = i["chaosValue"]
            self.currType = "Chaos Orb"
            print(self.name)
            list.append(self)
            j = j + 1
            print("Current iteration> ", j)
        os.chdir(
            r"C:\Users\emosc\PycharmProjects\GithubPushs\psychescape_price_fetcher\psychescape_price_fetcher\values")
        with open("incubators.txt", "a") as file:
            file.write(str(list))
            file.write("\n")
            file.close()
    print("Incubator values has now storaged at IncubatorValues class and imported to incubators.txt")
    print(self)

Above is an OOP loop I am trying to build. Class and self is working perfectly, but when I append values to list and write it to "incubators.txt" it only writes values like:

[<__main__.ItemPrices object at 0x0000023A951ACFD0>]
[<__main__.ItemPrices object at 0x0000023A951ACFD0>]
[<__main__.ItemPrices object at 0x0000023A951ACFD0>]
[<__main__.ItemPrices object at 0x0000023A951ACFD0>]
[<__main__.ItemPrices object at 0x0000023A951ACFD0>]

When printing self.name, self.exaltedValue or self.chaosValue I can see that I manage to fetch strings and integers but can only write objects to the file. How can I also write that strs and ints to the file? Thanks for any help.

4
  • Please update the indentation of your code. Python is very sensitive to indentation, as are python programmers. Commented Jan 22, 2022 at 12:00
  • Yep second def was inappropiately intended sorry for that! Commented Jan 22, 2022 at 12:00
  • Does this answer your question? How to print instances of a class using print()? Commented Jan 22, 2022 at 12:01
  • No sadly. Didnt work when I implemented it. Commented Jan 22, 2022 at 12:04

1 Answer 1

1

You can convert your list to JSON format and then write to the file.

Use this snippet :

result=json.dumps(list, default=lambda o: o.__dict__)

Then, you can then write the result in the file

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.