Python that doesn't work in GlowScript

43 views
Skip to first unread message

Daniel Schroeder

unread,
Feb 7, 2018, 12:19:20 AM2/7/18
to Glowscript Users
I just discovered that the one-line program

print([1,2,3] * 3)

works in standard Python but not in GlowScript.  Is there a summary somewhere of such limitations?  I mean language features--never mind packages.  Stuff like complex numbers, and who knows what else.  Being a Python ignoramus I hardly know what to even look for.  But as my students learn more, I want to tell them the truth about what is and isn't Python.  Also, it would be kinda nice if you could initialize an array by writing x = [0] * 100, instead of having to write a loop!

Dan


Bruce Sherwood

unread,
Feb 7, 2018, 11:06:48 AM2/7/18
to Glowscript Users
Actually, this works for me. Could be however because I just updated GlowScript 2.7 to handle this correctly.

The best guide to what is or is not possible is the README at the GitHub repository for rapydscript-ng:


However, in this particular case the fault was mine, not that of RapydScript-NG, which isn't solely responsible for how Python is handled. The problem is that RapydScript, like JavaScript, does not handle "operator overloading", meaning the ability for the "+" sign in vec+vec to be interpreted to mean "add the x components together, the y components together, and the z components together". There is a great deal of preprocessing of your code before it is handed to RapydScript, and a great deal of postprocessing after getting the JavaScript code from RapydScript. The two big components of the postprocessing are 1) Streamline, which permits you to write infinite loops, which are not legal in JavaScript, and 2) PaperScript, which converts X+Y into X['+'](Y), that is, a function X['+'] that is called with the argument Y, which then carries out the operation based on what type of entities X and Y are. This is a lot less expensive in time than one would expect, because one can add methods even to primitive JavaScript objects such as Number and String which at execution time are called very efficiently. I made measurements of execution speeds before and after adding multiplication operations to Number and String, and there is no difference (I do check first for the common cases).

If you're curious as to what this looks like, view the following file, and search for String.prototype and the code that follows:

Bruce Sherwood

unread,
Feb 7, 2018, 11:08:19 AM2/7/18
to Glowscript Users
As for complex numbers, no, not yet, though a colleague (unfortunately a very busy colleague) has expressed serious interest in adding them.

Bruce

Bruce Sherwood

unread,
Feb 7, 2018, 11:15:07 AM2/7/18
to Glowscript Users
I misspoke: I of course added multiplication operations to Array and String, not Number and String, though some changes were necessary with the Number class to make this all work. Note that 3*[10,20] is turned into 3['+']([10,20]) and at execution time drives the '+' method of the Number class, whereas [10,20]*3 is turned into [10,20]['+'](3), which drives the '+' method of the Array class.

Bruce

Daniel Schroeder

unread,
Feb 7, 2018, 12:42:50 PM2/7/18
to Glowscript Users
Indeed, list multiplication now works! That sure was quick. Thanks!

I've just browsed the RapydScript Readme page and most of it is way over my head. What I'd love to see is a short list of differences between standard Python and GlowScript that beginners like me, and my students, are likely to stumble over.

The specific reason I'm asking now is that some of my students have asked me how they can get a little more practice with Python, beyond the exercises that I'm giving them. These are mostly the students who have never programmed before. So I'm looking around on the web for Python tutorials, of which there seem to be hundreds, but most of them quickly get into features that I've never used and that I'm not sure if GlowScript even has. Using * on a list was one of those features. Another that I just encountered is the object() function, which seems not to exist in GlowScript. Yet another is that I'm seeing different syntax for string formatting (e.g., inserting a floating point number rounded to a certain number of decimal places) in other Python references, which differs from the GlowScript documentation (no curly braces, more percent signs).

Bruce Sherwood

unread,
Feb 7, 2018, 12:56:19 PM2/7/18
to Glowscript Users
At the moment I don't see how to compile such a document, because Python is vast even without importing anything. Nor do I even know what "object()" is or is for. Nor had I ever multiplied an array or string times a number before this morning. As for string formatting, it is true that older Python instructions emphasize the use of "%" whereas GlowScript, as is emphasized in modern Python, uses the string.format(....{}....) form. If your students are encountering such recondite elements of Python in ostensibly introductory materials, those materials aren't very good, and I can't deal with the problems they raise.

What your students might find useful is Rhett Allain's tutorial, mentioned on the first page of the Help:


Bruce

Daniel Schroeder

unread,
Feb 7, 2018, 1:44:04 PM2/7/18
to Glowscript Users
Yikes! When I followed the link to Allain's tutorial index on Wordpress, it came up with a bunch of Victoria's Secret underwear ads. Not sure I want to point my students to that!

What I'm looking for is a pure Python tutorial for students who have never programmed before: something that introduces basic programming concepts like variables and if-else and loops and functions, and includes some nontrivial exercises for practicing with these, without all the vectors, 3D graphics, and physics (all of which are also present in the tutorial materials that I've written myself). And as you say, the problem seems to be that so many of the materials out there quickly introduce features of Python that you and I don't use. I do think x = [0]*100 is useful, though, as an alternative to x = []; for i in range(100): x.append(0).

So % is older and string.format() is newer? Didn't know that--thanks. The specific tutorial I was looking at is for Python 3, yet it uses %. Guess I'd better move on and look at some more tutorials.


Bruce Sherwood

unread,
Feb 7, 2018, 2:24:35 PM2/7/18
to Glowscript Users
???

When I click on 


I get Allain's tutorials. I can't imagine how this could possibly work differently for you and me.

If I remember correctly, Code Academy's intro to Python is decent:


However, alas, they use the Python 2 syntax for print, that is, "print x" instead of "print(x)"

Bruce

Bruce Sherwood

unread,
Feb 7, 2018, 2:28:06 PM2/7/18
to Glowscript Users
In the Python 3 documentation on print, at


the % form is described briefly as "Old string formatting".

Bruce

Daniel Schroeder

unread,
Feb 7, 2018, 5:12:57 PM2/7/18
to Glowscript Users
Maybe you're using an ad blocker?

Bruce Sherwood

unread,
Feb 7, 2018, 5:32:45 PM2/7/18
to Glowscript Users
No, I'm not using an ad blocker, to my knowledge (does Chrome do anything special?). But I must just have been lucky, then. I'll alert Rhett to the problem. It doesn't cost very much per year to have Wordpress not show ads.

Bruce

Daniel Schroeder

unread,
Feb 7, 2018, 6:41:29 PM2/7/18
to Glowscript Users
I get three ads every time I load the WordPress page. They're presumably targeted (personalized), so perhaps nobody else is seeing the same ads I'm seeing. But I honestly have no idea why I'd be targeted for ads from Victoria's Secret, and I do think it would be inappropriate to refer a student to a page that is likely to display those types of ads.

Reply all
Reply to author
Forward
0 new messages