Click here to Skip to main content
15,867,756 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
Hello! I'm trying to write a bot in Python to play a Web-based game of tic-tac-toe. I've found a tutorial written in Java that closely fits my own purposes, but I'm having trouble understanding how to duplicate the Java code in Python. Specifically, how would I go about implementing an enum in Python that functions the same way as this block of Java code:

public enum Space {
 
    TOP_LEFT("square top left"),
    TOP_CENTER("square top"),
    TOP_RIGHT("square top right"),
    CENTER_LEFT("square left"),
    CENTER_CENTER("square"),
    CENTER_RIGHT("square right"),
    BOTTOM_LEFT("square bottom left"),
    BOTTOM_CENTER("square bottom"),
    BOTTOM_RIGHT("square bottom right");
 
    private String className;
    private By locator;
 
    Space(String className){
        this.className = className;
        locator = By.xpath(format("//div[@class='%s']", className));
    }
 
    public String getClassName(){
        return className;
    }
 
    public By getLocator(){
        return locator;
    }
}


Would I put the Java public methods into a "@classmethod"?

Is the following Java code:

Space(String className){
    this.className = className;
    locator = By.xpath(format("//div[@class='%s']", className));
}


a structure? How do I create it in Python?

What I have tried:

This is what I have so far:

import enum

class Squares(enum.Enum):
    TOP_LEFT = "top left square"
    TOP_CENTER = "top middle square"
    TOP_RIGHT = "top right square"
    CENTER_LEFT = "center left square"
    CENTER_CENTER = "center middle square"
    CENTER_RIGHT = "square right square"
    BOTTOM_LEFT = "bottom left square"
    BOTTOM_CENTER = "bottom middle square"
    BOTTOM_RIGHT = "bottom right square"
Posted
Updated 27-Oct-22 20:48pm

1 solution

This question and your very related one have a huge amount in common: Python dictionary and java hash maps[^]
You are making a mistake, and a pretty big one.

You are assuming that code in Language A just needs to be translated to Language B in order to get a good app in the target language. That doesn't happen - it produces bad code in the target and worse, it teaches you nothing about writing code, designing an app, or pretty much anything else.

The two languages use different frameworks, so what works well in Java doesn't necessarily translate well to Python (very little does).

So what you are doing is making your life harder in the long run: you don't learn how to develop an app, you don't learn Python particularly well, and you don't get a good grade when you hand it in!

So why do it?

Instead, read the Java code and extract the design, the overall way it works - not the specifics of the language it uses - and use that as a specification for your own Python app. Then design an app based on that specification, and start coding that. You'll get a better app, quicker, and you'll learn stuff that will help you with the next assignment. And probably get a better grade to boot.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900