Skip to content

Commit

Permalink
Dataclasses working for init, repr and all comparisons, parametrized …
Browse files Browse the repository at this point in the history
…dataclass decorator
  • Loading branch information
JdeH committed Apr 18, 2018
1 parent c6820f4 commit 44214ae
Show file tree
Hide file tree
Showing 7 changed files with 262 additions and 131 deletions.
@@ -1,7 +1,7 @@
from dataclasses import dataclass
from typing import ClassVar

@dataclass
@dataclass (order = True)
class C:
x: ClassVar = 10
y: int = 20
Expand All @@ -23,8 +23,27 @@ def run (autoTester):
c.t = 400
cc.f (456, autoTester)
cc.t = 4000

for obj in c, cc:
autoTester.check (obj.x, obj.y, obj.yy, obj.z, obj.zz, obj.t, obj.a, obj.b)

autoTester.check (c.__repr__ ())
autoTester.check (cc.__repr__ ())
autoTester.check (cc.__repr__ ())

#__pragma__ ('opov')

autoTester.check (c == cc)
autoTester.check (c != cc)

autoTester.check (c < cc)
autoTester.check (c > cc) #
autoTester.check (c <= cc) #
autoTester.check (c >= cc)

autoTester.check (c == c)
autoTester.check (c != c)
autoTester.check (c < c)
autoTester.check (c > c)
autoTester.check (c <= c)
autoTester.check (c >= c)

2 changes: 1 addition & 1 deletion transcrypt/development/shipment/shipment_test.py
Expand Up @@ -57,7 +57,7 @@ def test (relSourcePrepath, run, extraSwitches, messagePrename = '', nodeJs = Fa
buildSwitch = '-b ' if build else ''

# Compile with Transcrypt
os.system (f'{transpileCommand} -m {buildSwitch}-da -sf -n {transitSwitches}{extraSwitches}{sourcePrepath}{redirect}')
os.system (f'{transpileCommand} -m {buildSwitch}-da -sf {transitSwitches}{extraSwitches}{sourcePrepath}{redirect}')

# Run back to back in CPython
if run:
Expand Down
Binary file removed transcrypt/docs/images/slant.jpg
Binary file not shown.
Binary file removed transcrypt/docs/images/tshirt.pptm
Binary file not shown.
112 changes: 73 additions & 39 deletions transcrypt/modules/org/transcrypt/__builtin__.js
Expand Up @@ -914,7 +914,7 @@ export function deepcopy (anObject) {
// List extensions to Array

export function list (iterable) { // All such creators should be callable without new
var instance = iterable ? Array.from (iterable) : [];
let instance = iterable ? Array.from (iterable) : [];
// Sort is the normal JavaScript sort, Python sort is a non-member function
return instance;
}
Expand All @@ -939,8 +939,8 @@ Array.prototype.__getslice__ = function (start, stop, step) {
stop = this.length;
}

var result = list ([]);
for (var index = start; index < stop; index += step) {
let result = list ([]);
for (let index = start; index < stop; index += step) {
result.push (this [index]);
}

Expand All @@ -963,8 +963,8 @@ Array.prototype.__setslice__ = function (start, stop, step, source) {
Array.prototype.splice.apply (this, [start, stop - start] .concat (source));
}
else { // Assign to extended slice, replace designated items one by one
var sourceIndex = 0;
for (var targetIndex = start; targetIndex < stop; targetIndex += step) {
let sourceIndex = 0;
for (let targetIndex = start; targetIndex < stop; targetIndex += step) {
this [targetIndex] = source [sourceIndex++];
}
}
Expand All @@ -975,9 +975,9 @@ Array.prototype.__repr__ = function () {
return 'set()';
}

var result = !this.__class__ || this.__class__ == list ? '[' : this.__class__ == tuple ? '(' : '{';
let result = !this.__class__ || this.__class__ == list ? '[' : this.__class__ == tuple ? '(' : '{';

for (var index = 0; index < this.length; index++) {
for (let index = 0; index < this.length; index++) {
if (index) {
result += ', ';
}
Expand Down Expand Up @@ -1011,7 +1011,7 @@ Array.prototype.insert = function (index, element) {
};

Array.prototype.remove = function (element) {
var index = this.indexOf (element);
let index = this.indexOf (element);
if (index == -1) {
throw ValueError ("list.remove(x): x not in list", new Error ());
}
Expand Down Expand Up @@ -1043,8 +1043,8 @@ Array.prototype.__add__ = function (aList) {
};

Array.prototype.__mul__ = function (scalar) {
var result = this;
for (var i = 1; i < scalar; i++) {
let result = this;
for (let i = 1; i < scalar; i++) {
result = result.concat (this);
}
return result;
Expand All @@ -1055,7 +1055,7 @@ Array.prototype.__rmul__ = Array.prototype.__mul__;
// Tuple extensions to Array

export function tuple (iterable) {
var instance = iterable ? [] .slice.apply (iterable) : [];
let instance = iterable ? [] .slice.apply (iterable) : [];
instance.__class__ = tuple; // Not all arrays are tuples
return instance;
}
Expand All @@ -1066,9 +1066,9 @@ tuple.__bases__ = [object];
// N.B. Since sets are unordered, set operations will occasionally alter the 'this' array by sorting it

export function set (iterable) {
var instance = [];
let instance = [];
if (iterable) {
for (var index = 0; index < iterable.length; index++) {
for (let index = 0; index < iterable.length; index++) {
instance.add (iterable [index]);
}
}
Expand All @@ -1083,12 +1083,12 @@ Array.prototype.__bindexOf__ = function (element) { // Used to turn O (n^2) into

element += '';

var mindex = 0;
var maxdex = this.length - 1;
let mindex = 0;
let maxdex = this.length - 1;

while (mindex <= maxdex) {
var index = (mindex + maxdex) / 2 | 0;
var middle = this [index] + '';
let index = (mindex + maxdex) / 2 | 0;
let middle = this [index] + '';

if (middle < element) {
mindex = index + 1;
Expand Down Expand Up @@ -1119,7 +1119,7 @@ Array.prototype.discard = function (element) {

Array.prototype.isdisjoint = function (other) {
this.sort ();
for (var i = 0; i < other.length; i++) {
for (let i = 0; i < other.length; i++) {
if (this.__bindexOf__ (other [i]) != -1) {
return false;
}
Expand All @@ -1129,7 +1129,7 @@ Array.prototype.isdisjoint = function (other) {

Array.prototype.issuperset = function (other) {
this.sort ();
for (var i = 0; i < other.length; i++) {
for (let i = 0; i < other.length; i++) {
if (this.__bindexOf__ (other [i]) == -1) {
return false;
}
Expand All @@ -1142,8 +1142,8 @@ Array.prototype.issubset = function (other) {
};

Array.prototype.union = function (other) {
var result = set (this.slice () .sort ());
for (var i = 0; i < other.length; i++) {
let result = set (this.slice () .sort ());
for (let i = 0; i < other.length; i++) {
if (result.__bindexOf__ (other [i]) == -1) {
result.push (other [i]);
}
Expand All @@ -1153,8 +1153,8 @@ Array.prototype.union = function (other) {

Array.prototype.intersection = function (other) {
this.sort ();
var result = set ();
for (var i = 0; i < other.length; i++) {
let result = set ();
for (let i = 0; i < other.length; i++) {
if (this.__bindexOf__ (other [i]) != -1) {
result.push (other [i]);
}
Expand All @@ -1163,9 +1163,9 @@ Array.prototype.intersection = function (other) {
};

Array.prototype.difference = function (other) {
var sother = set (other.slice () .sort ());
var result = set ();
for (var i = 0; i < this.length; i++) {
let sother = set (other.slice () .sort ());
let result = set ();
for (let i = 0; i < this.length; i++) {
if (sother.__bindexOf__ (this [i]) == -1) {
result.push (this [i]);
}
Expand All @@ -1178,9 +1178,9 @@ Array.prototype.symmetric_difference = function (other) {
};

Array.prototype.py_update = function () { // O (n)
var updated = [] .concat.apply (this.slice (), arguments) .sort ();
let updated = [] .concat.apply (this.slice (), arguments) .sort ();
this.py_clear ();
for (var i = 0; i < updated.length; i++) {
for (let i = 0; i < updated.length; i++) {
if (updated [i] != updated [i - 1]) {
this.push (updated [i]);
}
Expand All @@ -1195,7 +1195,7 @@ Array.prototype.__eq__ = function (other) { // Also used for list
this.sort ();
other.sort ();
}
for (var i = 0; i < this.length; i++) {
for (let i = 0; i < this.length; i++) {
if (this [i] != other [i]) {
return false;
}
Expand All @@ -1208,19 +1208,53 @@ Array.prototype.__ne__ = function (other) { // Also used for list
};

Array.prototype.__le__ = function (other) {
return this.issubset (other);
if (this.__class__ == set) {
return this.issubset (other);
}
else {
for (let i = 0; i < this.length; i++) {
if (this [i] > other [i]) {
return false;
}
else if (this [i] < other [i]) {
return true;
}
}
return true;
}
};

Array.prototype.__ge__ = function (other) {
return this.issuperset (other);
if (this.__class__ == set) {
return this.issuperset (other);
}
else {
for (let i = 0; i < this.length; i++) {
if (this [i] < other [i]) {
return false;
}
else if (this [i] > other [i]) {
return true;
}
}
return true;
}
};

Array.prototype.__lt__ = function (other) {
return this.issubset (other) && !this.issuperset (other);
return (
this.__class__ == set ?
this.issubset (other) && !this.issuperset (other) :
!this.__ge__ (other)
);
};

Array.prototype.__gt__ = function (other) {
return this.issuperset (other) && !this.issubset (other);
return (
this.__class__ == set ?
this.issuperset (other) && !this.issubset (other) :
!this.__le__ (other)
);
};

// Byte array extensions
Expand All @@ -1230,13 +1264,13 @@ export function bytearray (bytable, encoding) {
return new Uint8Array (0);
}
else {
var aType = py_typeof (bytable);
let aType = py_typeof (bytable);
if (aType == int) {
return new Uint8Array (bytable);
}
else if (aType == str) {
var aBytes = new Uint8Array (len (bytable));
for (var i = 0; i < len (bytable); i++) {
let aBytes = new Uint8Array (len (bytable));
for (let i = 0; i < len (bytable); i++) {
aBytes [i] = bytable.charCodeAt (i);
}
return aBytes;
Expand All @@ -1254,15 +1288,15 @@ export var bytes = bytearray;


Uint8Array.prototype.__add__ = function (aBytes) {
var result = new Uint8Array (this.length + aBytes.length);
let result = new Uint8Array (this.length + aBytes.length);
result.set (this);
result.set (aBytes, this.length);
return result;
};

Uint8Array.prototype.__mul__ = function (scalar) {
var result = new Uint8Array (scalar * this.length);
for (var i = 0; i < scalar; i++) {
let result = new Uint8Array (scalar * this.length);
for (let i = 0; i < scalar; i++) {
result.set (this, i * this.length);
}
return result;
Expand Down
2 changes: 1 addition & 1 deletion transcrypt/modules/org/transcrypt/autotester/__init__.py
Expand Up @@ -81,7 +81,7 @@ def getFileLocation(ancestor):
lineno = m[5]
return( "{}:{}".format(filename, lineno) )
else:
__pragma__('js', '{}', 'console.log("Failed to Match Frame");')
__pragma__('js', '{}', 'console.log("Failed to Match Frame", gpFrame);')
return("UNKNOWN:???")
#ELSE
# Needed because Transcrypt imports are compile time
Expand Down

0 comments on commit 44214ae

Please sign in to comment.