#๐Ÿ”’ Cannot get Function to Work

25 messages ยท Page 1 of 1 (latest)

lusty iris
#

I am reworking a project of mine by trying out classes and def's, but cannot figure out how to use them correctly, I have one functional for pulling values from a json file, but I'm struggling to get another definition to pull data and print it out for the user to see.

Code:

import json
import requests


#Read user-specific information from 'config.json'
class Config:
    def __init__(self, config_file):
        self.file = config_file
        with open(self.file, 'r') as file:
            self.config = json.load(file)

    def ParseConfig(self):
        self.Instance = self.config['config']['InstanceURL']
        self.User = self.config['config']['UUID']
        self.RPC_ID = self.config['config']['RpcClientID']

config = Config('config.json')
config.ParseConfig()

class Data:
    def __init__(self, api_link):
        self.link = api_link
        link = requests.get(api_link)
        json_response = link.json()

    def ParseData(self):
        self.roomState = json_response["data"]["activeRoom"]
        print(self.roomState)

print(config.Instance + 'api/v3/users/uuid/' + config.User)
link = config.Instance + 'api/v3/users/uuid/' + config.User
link.ParseData()


###---DEBUG---
print(config.Instance)
print(config.User)
print(config.RPC_ID)
###===========

Error:

Traceback (most recent call last):
  File "/home/kraken/Projects/PyFreshPresence/PyFreshPresence_rewrite.py", line 32, in <module>
    link.ParseData()
    ^^^^^^^^^^^^^^
AttributeError: 'str' object has no attribute 'ParseData'
barren carbonBOT
#

@lusty iris

Python help channel opened

Remember to:

  • Ask your Python question, not if you can ask or if there's an expert who can help.
  • Show a code sample as text (rather than a screenshot) and the error message, if you've got one.
  • Explain what you expect to happen and what actually happens.

:warning: Do not pip install anything that isn't related to your question, especially if asked to over DMs.

lusty iris
#

This is the config.json referenced in the file

#

any help is very much appreciated

fading latch
#

are you using the uuid to log in?

#

if so you should delete the message and get a new uuid

lusty iris
fading latch
#

i've seen people accidentaly doing that multiple times

calm stream
#

ok, you're assigning link there, what type do you expect it to be?

lusty iris
#

str

calm stream
#

so how would link.parseData() work then?

#

the str type does not have a parseData() method

lusty iris
#

How should I go about it then?

calm stream
#

what did you intend to do there?

#

something like link = Data(config.Instance + 'api/v3/users/uuid/' + config.User)?

#

Data has the ParseData method, so did you intend to create a Data out of that joined string?

lusty iris
#
    def __init__(self, api_link):
        self.link = api_link
        link = requests.get(api_link)
        json_response = link.json()```
is intended to get the response data which is presented in a json format from the api

and the second part is intended to parse the data into values, thus the name
calm stream
#

but you're not instantiating Data anywhere, so surely you don't expect link to become a Data instance magically, yes?

lusty iris
#

I'd completely glossed over that

lusty iris
#

!close

barren carbonBOT
#
Python help channel closed

This help channel has been closed and it's no longer possible to send messages here. If your question wasn't answered, feel free to create a new post in #1035199133436354600. To maximize your chances of getting a response, check out this guide on asking good questions.