Python Forum
i want to write symbole as varibales in python to make translator - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: General Coding Help (https://python-forum.io/forum-8.html)
+--- Thread: i want to write symbole as varibales in python to make translator (/thread-32282.html)



i want to write symbole as varibales in python to make translator - rachidel07 - Feb-01-2021

hey

i want to write symbole as varibales in python to make translator .
for example: i want this [-10;-30] became this (-10,-30).
i want my code translate automatically those symbols: [ ; ]


i tried to type this but i have error :

[='('
]=')'
;=','


RE: i want to write symbole as varibales in python to make translator - deanhystad - Feb-01-2021

Could you provide more details about what it is you are trying to do?

Python has rules about identifier (variable, function, class, method) names, the most important being that they must be names. [ cannot be a variable name. 123 cannot be a variable name 'variable' cannot be a variable name. You can use upper and lower case letters, numbers and underscores. The first character must be a letter. Identifiers cannot be a python keyword (if, with, for, while, in …)


RE: i want to write symbole as varibales in python to make translator - rachidel07 - Feb-01-2021

(Feb-01-2021, 04:07 PM)deanhystad Wrote: Could you provide more details about what it is you are trying to do?

Python has rules about identifier (variable, function, class, method) names, the most important being that they must be names. [ cannot be a variable name. 123 cannot be a variable name 'variable' cannot be a variable name. You can use upper and lower case letters, numbers and underscores. The first character must be a letter. Identifiers cannot be a python keyword (if, with, for, while, in …)

i want to create a script who do this : when i type this [20;20] i want this result (20,20)
^^!


RE: i want to write symbole as varibales in python to make translator - deanhystad - Feb-01-2021

Need more details. What do you mean "when I type this[20, 20] I want this result (20, 20)"? Where are you typing? Are you trying to make a tool that sits between you and the interpreter changing keystrokes as you type? Are you trying to make a file processor that will scan a file and change some of the content?

Your description may sound clear to you, but you have all the context and I have none. You need to provide the context.


RE: i want to write symbole as varibales in python to make translator - rachidel07 - Feb-01-2021

(Feb-01-2021, 06:26 PM)deanhystad Wrote: Need more details. What do you mean "when I type this[20, 20] I want this result (20, 20)"? Where are you typing? Are you trying to make a tool that sits between you and the interpreter changing keystrokes as you type? Are you trying to make a file processor that will scan a file and change some of the content?

Your description may sound clear to you, but you have all the context and I have none. You need to provide the context.

i'm sorry i will try to explain more .
i use a website who gives me the indications of a game quest . like this :[20, 20] . as you see in the picture marks yellow .
and me i want to copy this indication from the website to the game but without brakets [] . that is way i want to make a script by python , who will work as atranslator .
i want to copy the indicator from website to my python project to replace brakets by () and then copy the final result to the game .
[Image: Capture.png]


RE: i want to write symbole as varibales in python to make translator - deanhystad - Feb-01-2021

Let's make sure I understand.

You have a game. It takes input. The input has to have a certain format. You cannot change the game to accept a different format.

You have a website that provides input for your game. The information from the website is not in the correct format for your game.

You want a translator that will take the output from the website and convert it to a format that can be input to the game.

Is that correct?

If that is correct, I would write a really small GUI application (using tkinter for example). There would be a control for pasting text from the website, a function that translates the text, and a control that displays the translated text so it could be copied and pasted into your game.
import tkinter as tk

translation = {'[':'(', ']':')', ';':','}

def translate(*args):
    outstr.set(''.join([translation.get(c, c) for c in inpstr.get()]))
    
root = tk.Tk()
inpstr = tk.StringVar()
outstr = tk.StringVar()

inpstr.trace('w', translate)

tk.Label(root, text='From Website:').pack(padx=5, pady=5)
tk.Entry(root, textvariable=inpstr, width=20).pack(padx=5, pady=5)

tk.Label(root, text='To Game:').pack(padx=5, pady=5)
tk.Entry(root, textvariable=outstr, width=20).pack(padx=5, pady=5)

tk.mainloop()



RE: i want to write symbole as varibales in python to make translator - rachidel07 - Feb-01-2021

(Feb-01-2021, 08:22 PM)deanhystad Wrote: Let's make sure I understand.

You have a game. It takes input. The input has to have a certain format. You cannot change the game to accept a different format.

You have a website that provides input for your game. The information from the website is not in the correct format for your game.

You want a translator that will take the output from the website and convert it to a format that can be input to the game.

Is that correct?

If that is correct, I would write a really small GUI application (using tkinter for example). There would be a control for pasting text from the website, a function that translates the text, and a control that displays the translated text so it could be copied and pasted into your game.
import tkinter as tk

translation = {'[':'(', ']':')', ';':','}

def translate(*args):
    outstr.set(''.join([translation.get(c, c) for c in inpstr.get()]))
    
root = tk.Tk()
inpstr = tk.StringVar()
outstr = tk.StringVar()

inpstr.trace('w', translate)

tk.Label(root, text='From Website:').pack(padx=5, pady=5)
tk.Entry(root, textvariable=inpstr, width=20).pack(padx=5, pady=5)

tk.Label(root, text='To Game:').pack(padx=5, pady=5)
tk.Entry(root, textvariable=outstr, width=20).pack(padx=5, pady=5)

tk.mainloop()

yes you understood my idea ^^ and that is what i want exactlly. thank you soo much bro i apreciate your help and your time
thank you ^^



RE: i want to write symbole as varibales in python to make translator - deanhystad - Feb-01-2021

There may be ways to automate the entire thing. I know for sure that you can scape the web information so you wouldn't have to open the web page and copy the info manually. Translation is obviously easy. The tricky bit would be entering the info into your game. Not sure how that would happen. Is the game text based?


RE: i want to write symbole as varibales in python to make translator - rachidel07 - Feb-01-2021

(Feb-01-2021, 09:57 PM)deanhystad Wrote: There may be ways to automate the entire thing. I know for sure that you can scape the web information so you wouldn't have to open the web page and copy the info manually. Translation is obviously easy. The tricky bit would be entering the info into your game. Not sure how that would happen. Is the game text based?

yes, i will try to automate the entire thing with pyautogui . and for the game it's a 2D game , so i write in the text barre of the game /travel with the indications (x,y) and the character will go automatically to the position .


RE: i want to write symbole as varibales in python to make translator - nilamo - Feb-03-2021

str.maketrans() can create a translation table, which makes this look cleaner fyi. https://docs.python.org/3/library/stdtypes.html#str.maketrans

>>> translator = str.maketrans({
...   '[': '(',
...   ']': ')',
...   ';': ','
... })
>>> "[-31; -52]".translate(translator)
'(-31, -52)'