Giter VIP home page Giter VIP logo

javajythonconverter's People

Contributors

jbfaden avatar

Stargazers

 avatar

Watchers

 avatar

javajythonconverter's Issues

merge Java routines

int parseInt( String s ) and
int parseInt( String s, int deft ) must be merged in Python. This is probably best for the human reviewer to do, but it's worth considering.

conditional expression to Jython 2.2

This conditional expression still causes problems with Jython 2.2:

int daysInMonth = time[1] == 0 ? 31 : TimeUtil.DAYS_IN_MONTH[isLeapYear(time[0]) ? 1 : 0][time[1]];

Python 3.10 match statement

Python 3.10 has a nice match statement which is roughly a superset of a case statement's functionality. The match statement should be used when this is a common target.

java.awt.* imports, getBounds() in Snowperson

I was able to convert a Java code to an Autoplot Jython code almost trivially, but there were two small things it missed: import java.awt.* didn't translate and this then didn't call self.getBounds.

import java.awt.*;                     // Java abstract window toolkit

public class Snowperson extends Frame {

  public static void main(String[] args) {
    Snowperson c = new Snowperson ();             // create a window
    c.setSize (400, 400);                // set its size
    c.setBackground (Color.white);       // set the background color
    c.show ();                           // display the window
  }

  public void paint (Graphics g) {
    Rectangle bb = getBounds ();
    bb.x = 0;
    bb.y = 0;
    paintBox (g, bb);
  }

  public static void paintBox (Graphics g, Rectangle bb) {

    // Mr. Snowperson
    int personWidth = 30 * bb.width / 100;
    int personHeight = bb.height;
    int personx = bb.x + 20 * bb.width / 100;
    int persony = bb.y;
    Rectangle mrbox =
      new Rectangle (personx, persony, personWidth, personHeight);
    paintSnowperson (g, mrbox);

    // Mrs. Snowperson
    personWidth = 90 * personWidth / 100;
    personHeight = 90 * bb.height / 100;
    personx = 50 * bb.width / 100;
    persony = bb.height - personHeight;

    Rectangle mrsbox =
      new Rectangle (personx, persony, personWidth, personHeight);
    paintSnowperson (g, mrsbox);
  }

  public static void paintSnowperson (Graphics g, Rectangle bb) {

    if (bb.height < 10) return;     // refuse to draw very small snowpeople
		
    int headheight = 20 * bb.height / 100;
    int midheight = 30 * bb.height / 100;
    int botheight = bb.height - midheight - headheight;
		
    int headwidth = 30 * bb.width / 100;
    int midwidth = 60 * bb.width / 100;
		
    int headgap = (bb.width - headwidth) / 2;
    int midgap = (bb.width - midwidth) / 2;
		
    // top oval and face
    g.drawOval (bb.x + headgap, bb.y, headwidth, headheight);
    Rectangle headBox =
      new Rectangle (bb.x + headgap, bb.y, headwidth, headheight);
    paintFace (g, headBox);
		
    // middle oval and rings
    g.drawOval (bb.x + midgap, bb.y + headheight, midwidth, midheight);
    Rectangle thoraxBox =
      new Rectangle (bb.x + midgap, bb.y + headheight, midwidth, midheight);
    paintRings (g, thoraxBox);
		
    // bottom oval and baby snowpeople
    int abdomeny =  bb.y + headheight + midheight;
    g.drawOval (bb.x, abdomeny, bb.width, botheight);
    Rectangle abdomenBox =
      new Rectangle (bb.x + bb.width/3, abdomeny, bb.width/3, botheight);
    paintSnowperson (g, abdomenBox);
  }
	
  public static void paintFace (Graphics g, Rectangle bb) {

    int eyeheight = 20 * bb.height / 100;
    int eyewidth = 20 * bb.width / 100;
    int gap = (bb.width - 2 * eyewidth) / 3;
    int brow = 25 * bb.height / 100;
		
    // just draw two eyes
    g.fillOval (bb.x + gap, bb.y + brow, eyewidth, eyeheight);
    g.fillOval (bb.x + 2 * gap + eyewidth, bb.y + brow, eyewidth, eyeheight);
  }
	
  public static void paintRings (Graphics g, Rectangle bb) {

    // Polite methods avoid doing things like changing the current color,
    // because the calling method might not like it.  Before we change the
    // color, we stow the old value.
    Color oldColor = g.getColor ();
		
    int ringWidth = 30 * bb.width/100;
    int ring_height = 60 * bb.height/100;
    int gap = (bb.width - 3 * ringWidth) / 2;
    
    int xx = bb.x;
    int yy = bb.y;
    g.setColor (Color.blue);
    g.drawOval (xx, yy, ringWidth, ring_height);
		
    xx += ringWidth + gap;
    g.setColor (Color.black);
    g.drawOval (xx, yy, ringWidth, ring_height);
		
    xx += ringWidth + gap;
    g.setColor (Color.red);
    g.drawOval (xx, yy, ringWidth, ring_height);
		
    yy = bb.y + bb.height - ring_height;
    xx = bb.x + (bb.width - 2 * ringWidth - gap) / 2;
    g.setColor (Color.yellow);
    g.drawOval (xx, yy, ringWidth, ring_height);

    xx += ringWidth + gap;
    g.setColor (Color.green);
    g.drawOval (xx, yy, ringWidth, ring_height);
    
    // before returning we restore the original color
    g.setColor (oldColor);
  }
}

static fields vs class fields

I made the early mistake of not worrying about static fields vs class fields, and I realize now I need to address this. Presently it replaces field access with ClassName.field, when sometimes it should be self.field.

I should create a nice demo of this with a Java class which contains a static field constant and a class field.

json conversion would be nice

URITemplateTest has a little bit of JSON handling, which could be automatically converted for Python 3.6. I did this by hand, but for other applications this would be helpful.

javascript String.format

I've been working on Java to JavaScript now, and this morning on String.format, which is used all over the place in the TimeUtil and URI_Templates codes.

I just discovered padstart: '123'.padstart(9,'0') will return '000000123' and should work nicely.

static methods conversion

It would be nice if one Java static method could be pasted into the converter, but I can't figure out how to compile this. I started to handle this by wrapping the code in a "class Foo {" code, but the result has a reference to the Foo class. I need to either remove the method or provide a way to conveniently wrap the class.

for loop with continue

A for loop is converted to a Python while loop, but if a continue statement is within the loop the loop variable is not incremented.

for ( int i=0; i<10; i++ ) {
    if ( i<3 ) continue;
    System.err.println("i="+i);
}

name conflict between field and method

Java allows a class field and a method to have the same name. I had the variable "format" and a method "format", and in Python this resulted in my field definition to be clobbered by the field definition without a compile error. This was a very confusing bug to figure out.

The converter needs to check for name conflicts like this, and at least throw an error.

combine two methods used to provide a default value

The following Java code has two methods so that a default value of offset equal to 0 is
available:

public class DefaultArg {
    public static String method( String arg ) {
        return method( arg, 0 );
    }
    
    public static String method( String arg, int offset ) {
        return arg.substring(offset);
    }
}

This should be translated to Jython/Python with:

def method( arg, offset=0 ):
    return arg[offset:]

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.