Modulo, String, Import Bugs

UPDATE: Most (if not all) of these bugs have been fixed since I started using Pyjamas. This is here mainly for reference.

Pyjamas has a very useful migration guide which summarizes the current issues/differences between JS and Python that the developers did not yet resolve. It’s a very useful read for anyone using Pyjamas, both with JS and Python background and a must-read for anyone wishing to make an app that works both in browser and in Pyjamas-Desktop. I did find a few issues, however, that weren’t yet documented. So I decided to post them here, including the work-arounds.

Importing radians function from math

Description:

For some reason, the following code will cause an ImportError in JS:

import math
math.radians(degs)

However, doing the same with math.pi, math.sin, math.cos, math.sqrt works fine

Solution:

Either use the following:

from math import radians
radians(degs)

Or:

from math import pi
degs*pi/180

Concatenating two strings using +

Description:

The following works fine in Python and Pyjamas-Desktop, but throws an exception (that Pyjamas catches, and gives “inform the webmaster” error):

b="jamas"
a="py"+b

Ironically, passing the same into a function as an argument works fine in both:

b="jamas"
fun("py"+b)

Solution:

The following alternative works in both:

b="jamas"
a=''.join(["py",b])

Alternatively, if you want a cleaner-looking concatenation:

def strcat(*args):
  return ''.join(args) b="jamas"
a=strcat("py",b)

Different modulo implementation

Description:

When performing the modulo command, Python uses the sign of the divisor for the result. JS, however uses the sign of the dividend. The following code in Pyjamas-Desktop will set a to 0.6, while in JS/browser it will set it to -0.4:

a=(-0.4)%1

Solution:

So far, my solution is to do the following:

a=dividend%divisor if a<0:
   a+=divisor

I also tried using math.fmod() instead, but ran into the same ImportError issue as described above for radians, unfortunately using the work-around described above only works for radians(). For fmod() Pyjamas will throw “$module.math.fmod is not a function” exception

This entry was posted in Frameworks, Hacks and Tricks and tagged , by Alexander Tsepkov. Bookmark the permalink.

About Alexander Tsepkov

Founder and CEO of Pyjeon. He started out with C++, but switched to Python as his main programming language due to its clean syntax and productivity. He often uses other languages for his work as well, such as JavaScript, Perl, and RapydScript. His posts tend to cover user experience, design considerations, languages, web development, Linux environment, as well as challenges of running a start-up.

One thought on “Modulo, String, Import Bugs

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>