Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #7589 > unrolled thread

Rant on web browsers

Started byChris Angelico <rosuav@gmail.com>
First post2011-06-14 16:31 +1000
Last post2011-07-24 13:00 -0700
Articles 8 — 6 participants

Back to article view | Back to comp.lang.python


Contents

  Rant on web browsers Chris Angelico <rosuav@gmail.com> - 2011-06-14 16:31 +1000
    Re: Rant on web browsers "Martin P. Hellwig" <martin.hellwig@gmail.com> - 2011-06-14 09:39 +0100
      Re: Rant on web browsers Chris Angelico <rosuav@gmail.com> - 2011-06-14 18:46 +1000
      Re: Rant on web browsers Daniel Kluev <dan.kluev@gmail.com> - 2011-06-21 18:12 +1100
    Re: Rant on web browsers Asen Bozhilov <asen.bozhilov@gmail.com> - 2011-06-14 15:33 -0700
    Re: Rant on web browsers Thomas 'PointedEars' Lahn <PointedEars@web.de> - 2011-06-22 18:37 +0200
    Re: Rant on web browsers lkcl <luke.leighton@gmail.com> - 2011-07-24 12:58 -0700
    Re: Rant on web browsers lkcl <luke.leighton@gmail.com> - 2011-07-24 13:00 -0700

#7589 — Rant on web browsers

FromChris Angelico <rosuav@gmail.com>
Date2011-06-14 16:31 +1000
SubjectRant on web browsers
Message-ID<mailman.210.1308033067.11593.python-list@python.org>
Random rant and not very on-topic. Feel free to hit Delete and move on.

I've just spent a day coding in Javascript, and wishing browsers
supported Python instead (or as well). All I needed to do was take two
dates (as strings), figure out the difference in days, add that many
days to both dates, and put the results back into DOM Input objects
(form entry fields). Pretty simple, right? Javascript has a Date
class, it should be fine. But no. First, the date object can't be
outputted as a formatted string. The only way to output a date is "Feb
21 2011". So I have to get the three components (oh and the month is
0-11, not 1-12) and emit those. And Javascript doesn't have a simple
format function that would force the numbers to come out with leading
zeroes, so I don't bother with that.

What if I want to accept any delimiter in the date - slash, hyphen, or
dot? Can I just do a simple translate, turn all slashes and dots into
hyphens? Nope. Have to go regular expression if you want to change
more than the first instance of something. There's no nice string
parse function (like sscanf with "%d-%d-%d"), so I hope every browser
out there has a fast regex engine. When all you have is a half-ton
sledgehammer, everything looks like a really REALLY flat nail...

Plus, Javascript debugging is annoyingly difficult if you don't have
tools handy. I need third-party tools to do anything other than code
blind? Thanks.

Oh, and "int i" is illegal in Javascript. Whoops. That one is my fault, though.

Javascript's greatest strength is that it exists in everyone's
browser. That is simultaneously it's worst failing, because it becomes
nigh impossible to improve it. If Chrome's V8 starts supporting new
features and everyone else's JS engines don't, we can't use those
features. Even if they're added to the standard, there'll still be old
browsers that don't support things. The only way to add to the
language is to dump stuff into a .js file and include it everywhere.

But if anyone feels like writing an incompatible browser, please can
you add Python scripting?

Chris Angelico

[toc] | [next] | [standalone]


#7596

From"Martin P. Hellwig" <martin.hellwig@gmail.com>
Date2011-06-14 09:39 +0100
Message-ID<it76ov$98p$1@dont-email.me>
In reply to#7589
On 14/06/2011 07:31, Chris Angelico wrote:
<cut>
> But if anyone feels like writing an incompatible browser, please can
> you add Python scripting?

You might find that Pyjamas already fill your needs python/javascript 
wise. It is truly great to just write python, translate it, and then 
have it work in the browser.

-- 
mph

[toc] | [prev] | [next] | [standalone]


#7597

FromChris Angelico <rosuav@gmail.com>
Date2011-06-14 18:46 +1000
Message-ID<mailman.212.1308041189.11593.python-list@python.org>
In reply to#7596
On Tue, Jun 14, 2011 at 6:39 PM, Martin P. Hellwig
<martin.hellwig@gmail.com> wrote:
> On 14/06/2011 07:31, Chris Angelico wrote:
> <cut>
>>
>> But if anyone feels like writing an incompatible browser, please can
>> you add Python scripting?
>
> You might find that Pyjamas already fill your needs python/javascript wise.
> It is truly great to just write python, translate it, and then have it work
> in the browser.

I had a (very) quick glance at that; it entails a fairly large library
that has to get loaded. It still comes down to "the only way to
improve this is to dish out a huge helping of .js to everyone who
comes", which imho is unideal.

That said, though, my context for this job was "tiny thing, just do
the same as you're doing on the back-end". For something larger, I may
well give Pyjamas a whirl.

Chris Angelico

[toc] | [prev] | [next] | [standalone]


#8078

FromDaniel Kluev <dan.kluev@gmail.com>
Date2011-06-21 18:12 +1100
Message-ID<mailman.219.1308640361.1164.python-list@python.org>
In reply to#7596
Regarding pyjamas lib size, its actually not that big if you want only
bare python, without DOM wrapper.

You only need pyjslib, which is less than 30kb gzipped when compiled
even with --strict (quite verbose mode).
Even that could be further reduced if you drop unused by your code
python features from it.

-- 
With best regards,
Daniel Kluev

[toc] | [prev] | [next] | [standalone]


#7637

FromAsen Bozhilov <asen.bozhilov@gmail.com>
Date2011-06-14 15:33 -0700
Message-ID<1790b780-52ab-422a-b966-0f04e2604fac@j23g2000yqc.googlegroups.com>
In reply to#7589
Chris Angelico wrote:

> I've just spent a day coding in Javascript, and wishing browsers
> supported Python instead (or as well). All I needed to do was take two
> dates (as strings), figure out the difference in days, add that many
> days to both dates, and put the results back into DOM Input objects
> (form entry fields). Pretty simple, right? Javascript has a Date
> class, it should be fine. But no. First, the date object can't be
> outputted as a formatted string. The only way to output a date is "Feb
> 21 2011". So I have to get the three components (oh and the month is
> 0-11, not 1-12) and emit those. And Javascript doesn't have a simple
> format function that would force the numbers to come out with leading
> zeroes, so I don't bother with that.

Actually there is not Date class. There are not any classes in
ECMAScript.

> What if I want to accept any delimiter in the date - slash, hyphen, or
> dot? Can I just do a simple translate, turn all slashes and dots into
> hyphens? Nope. Have to go regular expression if you want to change
> more than the first instance of something. There's no nice string
> parse function (like sscanf with "%d-%d-%d"), so I hope every browser
> out there has a fast regex engine. When all you have is a half-ton
> sledgehammer, everything looks like a really REALLY flat nail...

function formatDate(date) {
    return ('000' + date.getFullYear()).slice(-4) + '-' +
           ('0' + (date.getMonth() + 1)).slice(-2) + '-' +
           ('0' + date.getDate()).slice(-2);
}

formatDate(new Date());

> Plus, Javascript debugging is annoyingly difficult if you don't have
> tools handy. I need third-party tools to do anything other than code
> blind? Thanks.

It depends on the environment. It is good idea to read c.l.js and
JSMentors.

[toc] | [prev] | [next] | [standalone]


#8227

FromThomas 'PointedEars' Lahn <PointedEars@web.de>
Date2011-06-22 18:37 +0200
Message-ID<1529050.qVoOGUtdWV@PointedEars.de>
In reply to#7589
[I am biting only because this is my field of expertise, and I am really 
getting tired reading from people not having a shadow of a trace of a 
minimum clue what these languages that I like can and can't do.]

Chris Angelico wrote:

> Random rant and not very on-topic. Feel free to hit Delete and move on.

Why not post on-topic in the first place, and *ask* before being 
presumptuous?  Get better.
 
> I've just spent a day coding in Javascript,

There is no "Javascript".

> and wishing browsers supported Python instead (or as well).

Some browsers do in a way, because of the ECMAScript implementations they 
employ.  Mozilla.org JavaScript 1.7+ is pretty pythonic as compared to, e.g. 
Microsoft JScript.  Array comprehension, Array methods like filter(), and 
iterators/generators come to mind.  This is not a coincidence; Brendan Eich 
has explicitly referred to Python in his blog.

> All I needed to do was take two dates (as strings), figure out the
> difference in days, add that many days to both dates, and put the results
> back into DOM Input objects (form entry fields).

How that would be done would first depend on what you consider a day to be.
(We have been over this elsewhere.)  You can easily compute the difference 
between the time value of Date instances, which is stored as milliseconds 
since epoch, by subtraction (conversion to number is implicit, you do not 
have to call the getTime() method):

  var d1 = new Date(…, …, …, 12);
  var d2 = new Date(…, …, …, 12);
  var ms = d2 - d1;

[I have been told that the 12 hours into the day can avoid problems with DST 
changes.  You may use any hours value that you see fit, even none (which 
defaults to 0).]

Dividing that by the number of milliseconds per "day" (which is conveniently 
written 864e5) would give you a good idea of the number of "day"s (but watch 
out for floating-point precision).

You can also write a method (of Date instances, if desired!) that can take 
calendar days into account – like saying that there are two days "between" 
`new Date(2011, 5, 22)' and `new Date(2011, 5, 24)' –, since overflow on 
Date instances is easily detected:

  Date.prototype.diff = function(d2) {
    // …
  };

  var d = new Date(…);
  var diffInDays = d.diff(new Date(…));

Finally, adding or subtracting calendar days to a date is very simple, of 
course:

  var d = new Date();
  d.setDate(d.getDate() + 2);

> Pretty simple, right? Javascript has a Date class,

No, it does not.  (Have you, by chance, read Flanagan's latest edition on 
the subject, full of misconceptions?)

For your purposes, "Javascript" has no classes at all; if accepted as an 
umbrella term (which I strongly recommend against¹), then the programming 
*languages* it describes in your context all use *prototype-based* 
inheritance.

> it should be fine. But no. First, the date object can't be
> outputted as a formatted string.

Yes, it can.  There are several methods on the Date prototype object for 
that, and you can always write and add your own.

> The only way to output a date is "Feb 21 2011".

Wrong.

> So I have to get the three components (oh and the month is
> 0-11, not 1-12) and emit those.

Not necessarily.  (Yes, the month value is zero-based.  This is not 
particular to ECMAScript implementations.)

> And Javascript doesn't have a simple format function that would force the
> numbers to come out with leading zeroes,

But it is easily written, simplified:

  function leadingZero(n, width)
  {
    n = String(n);
    var len = n.length;
    if (len >= width) return n;

    var a = [];
    a.length = width - len + 1;
    return a.join("0") + n;
  }

> so I don't bother with that.

Your problem alone.
 
> What if I want to accept any delimiter in the date - slash, hyphen, or
> dot?

The most simple way is to convert it so that the string format is understood 
by the Date constructor.  There is precedence for the formats accepted by 
implementations in Web browsers as they are specified in ECMAScript Ed. 5.

However, the most reliable way, unless you are using dates before 100 CE, is 
to parse the components and pass them as separate arguments to the Date 
constructor.  There is even a way to use an Array instance for that (a 
construct method added to Function.prototype or Date), so you can reuse the 
return value of String.prototype.match().

> Can I just do a simple translate, turn all slashes and dots into
> hyphens?

Yes, you can, but it depends on the input format.

> Nope. Have to go regular expression if you want to change
> more than the first instance of something.

Again, that depends on the input format.

> There's no nice string parse function (like sscanf with "%d-%d-%d"), so I
> hope every browser out there has a fast regex engine.

I think sscanf() can be easily written (and is going to be [once again]; 
still working on an efficient sprintf()).

But why accept the date as a string, which is known to be ambiguous, in the 
first place?  Date input should be facilitated by three form controls, not 
one.  The least you should do if you use only one control is to provide a 
date picker widget (HTML5 defines a way to do this without scripting, but so 
far the feature appears to be experimental at least in non-mobile browsers).

> When all you have is a half-ton sledgehammer, everything looks like a
> really REALLY flat nail...

When all you have is a toothbrush, every dirty room must look like taking a 
lifetime to get cleaned up…  RTFM!
 
> Plus, Javascript debugging is annoyingly difficult if you don't have
> tools handy.

Script debuggers are built into most recent Web browsers.  AFAIK of the Big 
6[tm] (IE, Fx, Op, Cr, Sf, Kq) it is only Firefox which requires e.g. 
Firebug to be installed as an extension.  But installation of that is very 
straightforward, and useful.

> I need third-party tools to do anything other than code blind?

No, you don't.  WebCore-based browsers et al. have debuggers built-in 
(Ctrl+Shift+J etc.)  All widely distributed Web browsers have an error 
console built-in, and almost all graphical Web browsers to date support the 
`javascript:' URI scheme, with which you can test script code from the 
Location/Address Bar even without an error console (just be sure that you 
use the `void' operator or other means that return an undefined-compatible 
result).

> Thanks.

You're welcome.

> Oh, and "int i" is illegal in Javascript.

That depends on your idea of "Javascript".  It is certainly a syntax error 
in implementations of ECMAScript Ed. other than 4.

> Whoops. That one is my fault, though.

Yes, those languages are loosely typed.  Like Python, BTW.

> Javascript's greatest strength is that it exists in everyone's
> browser.

A language named "Javascript" exists in exactly no browser.¹

> That is simultaneously it's worst failing, because it becomes
> nigh impossible to improve it.

No, it has been done.

> If Chrome's V8 starts supporting new features and everyone else's JS
> engines don't, we can't use those features.

Yes, we can.  You only cannot because you are too lazy.

> Even if they're added to the standard, there'll still be old
> browsers that don't support things.

You can deal with incompatible syntax extensions the same as before with, 
e.g. Netscape 4.  eval() comes in handy there, but you lose performance.

You can deal with API extensions the same as before, by run-time feature-
testing (which costs performance as well, but when cleverly done not nearly 
as much).

Obviously you have not the slightest idea what you are talking about.  But 
that is a common trait among JavaScript/ECMAScript neophytes.

> The only way to add to the language is to dump stuff into a .js file and
> include it everywhere.

No, you can make adjustments on an as-needed basis (much like in Python).
 
> But if anyone feels like writing an incompatible browser, please can
> you add Python scripting?

Get yourself informed for a change!

______________
[1] <http://PointedEars.de/es-matrix>
-- 
PointedEars

Bitte keine Kopien per E-Mail. / Please do not Cc: me.

[toc] | [prev] | [next] | [standalone]


#10226

Fromlkcl <luke.leighton@gmail.com>
Date2011-07-24 12:58 -0700
Message-ID<e0fa14b8-e028-4c35-9c12-38057121f07f@m10g2000yqd.googlegroups.com>
In reply to#7589
On Jun 14, 7:31 am, Chris Angelico <ros...@gmail.com> wrote:

> But if anyone feels like writing an incompatible browser, please can
> you add Python scripting?

 http://wiki.python.org/moin/WebBrowserProgramming

 already been done, chris - you want the firefox plugin, pyxpcomext
and then if you actually want to manipulate the DOM in the browser
engine as well, you need the 2nd plugin xpcom as well.  oh, i also
forgot (because it's quite new) there's firebreath as well:
https://github.com/Nitrogenycs/firebreath-x

 so... yeah, if you're completely off your head, knock yourself out :)

 l.

 p.s. pyjamas rocks!  sorry, had to say that.

[toc] | [prev] | [next] | [standalone]


#10227

Fromlkcl <luke.leighton@gmail.com>
Date2011-07-24 13:00 -0700
Message-ID<1fb74162-d322-4711-bf98-3b807f63c81e@t9g2000vbs.googlegroups.com>
In reply to#7589
On Jun 14, 7:31 am, Chris Angelico <ros...@gmail.com> wrote:
> Random rant and not very on-topic. Feel free to hit Delete and move on.
>
> I've just spent a day coding in Javascript, and wishing browsers
> supported Python instead (or as well). All I needed to do was take two

 ok your next best thing is to try pyjampiler.  it's the absolute
minimum and easiest stuff you'll need to be able to write code in
python yet integrate it easily into a pre-existing web site.  there
are alternatives out there (competitor projects to pyjamas) - take a
look on http://wiki.python.org/moin/WebBrowserProgramming

 l.

[toc] | [prev] | [standalone]


Back to top | Article view | comp.lang.python


csiph-web