Python Forum
How to use isintance with condition?
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
How to use isintance with condition?
#1
Hello! I am trying to make my own language and I came across a problem.

I want it to check the type of data the user inputted, but the thing is that its a string.
And as I was looking for sources, I thought of eval() but they said they could hack into my computer.

I also resided to isinstance, but it will always result in True ONLY if I'm checking if its a string.
So how can I get the type of a variable in a string type while not using eval()? Huh

Full Code:
class Token:
    def __init__(self, type, value):
        self.type = type
        self.value = value

class Reader:
    def __init__(self, text):
        self.text = text
    def get_tokens(self):
        save = ''
        tokens = []
        for index in range(len(self.text)):
            if self.text[index] != ',' and self.text[index] != ' ':
                save += self.text[index]
            else:
                print(self.text[index])
                if save == '+':
                    tokens.append(Token('TT_SYMBOL_PLUS', '+'))
                elif save == '-':
                    tokens.append(Token('TT_SYMBOL_MINUS', '-'))
                elif save == '*' or save == '++':
                    tokens.append(Token('TT_SYMBOL_MULTI', save))
                elif save == '/' or save == '--':
                    tokens.append(Token('TT_SYMBOL_DIVIDE', save))
                elif save == '^' or save == '**' or save == '+++':
                    tokens.append(Token('TT_MATH_EXPONENT', save))
                elif save == '=':
                    tokens.append(Token('TT_SYMBOL_EQUALSIGN', '='))
                else:
                    tokens.append(Token('TT_SPECIAL_UNKNOWN', save))
                save = ''
        return tokens

class Decoder:
    def __init__(self, tokens):
        self.tokens = tokens
    def decode(self):
        updatedtokens = []
        for token in self.tokens:
            updatedtokens += [token.type, token.value]
        return updatedtokens
        
while True:
    cmd = input()
    print(Decoder(Reader(cmd).get_tokens()).decode())
Reply
#2
What types can it be?
Reply
#3
You are trying to create a lexer in Python. A simple way to do that is to use regular expressions to recognize lexical tokens in the input streams.

If you are ready to start with an external module that already tackles this problem, you could start with PLY which comes with support for writing lexical analysers.
« We can solve any problem by introducing an extra level of indirection »
Reply
#4
(Yesterday, 11:51 PM)Azdaghost Wrote: I also resided to isinstance, but it will always result in True ONLY if I'm checking if its a string.
Just to mention that this is not surprising if you check what has been returned from input() - it is always string
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  else condition not called when if condition is false Sandz1286 10 8,036 Jun-05-2020, 05:01 PM
Last Post: ebolisa
  [HELP] Nested conditional? double condition followed by another condition. penahuse 25 12,031 Jun-01-2020, 06:00 PM
Last Post: penahuse

Forum Jump:

User Panel Messages

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