Python Forum
variable changing types for error?
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
variable changing types for error?
#1
I was trying to debug this error in my code and I noticed something weird:
if I did the function as if the parameter was a string, it would tell me its a Token type
if I did the function as if the parameter was a Token, it would tell me its a string type
has this ever happened to y'all???
class Token:
    def __init__(self, type, value=None):
        self.type = type
        self.value = value

    def __repr__(self):
        return f'{self.type}: {self.value}' if self.value != None else f'{self.type}'

class Lexer:
    def __init__(self, line):
        self.line = line

    def getTokens(self):
        tokens = []
        save = ''

        for i in range(len(self.line)):
            char = self.line[i]
            save += char

            if save == '0':
                tokens.append(Token('TT_BINARY', 0))
                save = ''
            elif save == '1':
                tokens.append(Token('TT_BINARY', 1))
                save = ''
            elif save == ':':
                tokens.append(Token('TT_SETSLOT'))
                save = ''
            elif save == 'S':
                tokens.append(Token('TT_VARIABLE'))
                save = ''
            elif save == ' ' and i != 0:
                tokens.append(Token('TT_SPACE'))
                save = ''

        return tokens

    def execute(self):
        tokens = self.getTokens()
        saveTokens = []

        # Stage 1: Group Binary

        binarySave = ''
        binaryLoading = False

        for token in tokens:
            if token.type == 'TT_BINARY' and not binaryLoading:
                binaryLoading = True
                binarySave += str(token.value)
            elif token.type == 'TT_BINARY':
                binarySave += str(token.value)
            else:
                binaryLoading = False
                if binarySave != '': saveTokens.append(Token('TT_GBINARY', binarySave))
                saveTokens.append(token)
                binarySave = ''

        if binarySave != '':
            saveTokens.append(Token('TT_GBINARY', binarySave))

        tokens = saveTokens
        saveTokens = []

        # Stage 2: Replacing Variables

        skip = False

        for i in range(len(tokens)):
            token = tokens[i]

            if skip:
                skip = False
                break
            
            if token.type == 'TT_VARIABLE':
                saveTokens.append(system.slots[System.convert_binary(tokens[i+1])])
                skip = True
            else:
                saveTokens.append(token)

        tokens = saveTokens
        saveTokens = []

        for i in range(len(tokens)):
            token = tokens[i]

            if token.type == 'TT_SETSLOT':
                system.slots[System.convert_binary(tokens[i-1].value)] = tokens[i+1]

class System:
    def __init__(self, script):
        self.script = script
        self.lexer = Lexer(None)
        self.slots = {}

        for i in range(256):
            self.slots[i] = ''

    @staticmethod
    def convert_binary(binary):
        print(type(binary))
        if isinstance(binary, str): reversed = binary[::-1]
        if isinstance(binary.value, str): reversed = binary.value[::-1]
        rv = 0

        for i in range(len(reversed)):
            if reversed[i] == '1':
                rv += 2 ** i

        return rv

    def run(self):
        for line in self.script:
            self.lexer.line = line
            self.lexer.execute()

system = System([
    '11:110'
    '0:S11'
])

system.run()
print(system.slots)
Reply
#2
(Sep-15-2025, 05:20 PM)Azdaghost Wrote: if I did the function as if the parameter was a string, it would tell me its a Token type
if I did the function as if the parameter was a Token, it would tell me its a string type
hmm. Which function, which parameter, who tells you the parameter is a Token type or a string type? Please add details about the exact error.
« We can solve any problem by introducing an extra level of indirection »
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Variable Types in a class nafshar 9 6,157 Oct-07-2022, 07:13 PM
Last Post: deanhystad
  [variable] is not defined error arises despite variable being defined TheTypicalDoge 4 5,365 Apr-05-2022, 04:55 AM
Last Post: deanhystad
  Keeping a value the same despite changing the variable it was equated to TheTypicalDoge 2 2,940 Mar-13-2022, 10:50 PM
Last Post: Yoriz
  Line charts error "'isnan' not supported for the input types," issac_n 1 3,828 Jul-22-2020, 04:34 PM
Last Post: issac_n
  Error: variable can not be defined julio2000 2 4,784 Feb-09-2020, 08:51 PM
Last Post: julio2000
  Changing a variable's name on each iteration of a loop rix 6 106,215 Jan-03-2020, 07:06 AM
Last Post: perfringo
  Changing Data Types BallisticSwami 2 3,919 Jun-27-2019, 01:17 PM
Last Post: BallisticSwami
  Type error when reading in different data types on an __init__ method Dylanmull 3 4,568 May-09-2019, 02:05 PM
Last Post: buran
  Changing Directory; Syntax Error DavidRobinsons 1 4,950 Oct-08-2018, 02:13 AM
Last Post: ichabod801
  int(variable) not changing variable to an integer StoopidChicken 2 4,510 Jan-11-2018, 10:30 AM
Last Post: StoopidChicken

Forum Jump:

User Panel Messages

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