GithubHelp home page GithubHelp logo

Comments (6)

shaolo1 avatar shaolo1 commented on August 30, 2024

I am not doing any string.format() calls. Is it possible to have it not generate this method? My current workaround is to delete this function manually, which works around the problem but is a pain.

from transcrypt.

JdeH avatar JdeH commented on August 30, 2024

You can rename the function from transcrypt\modules\org\transcrypt__javascript____builtin__.mod.js

// Since it's worthwhile for the 'format' function to be able to deal with *args, it is defined as a property
// __get__ will produce a bound function if there's something before the dot
// Since a call using *args is compiled to e.g. <object>.<function>.apply (null, args), the function has to be bound already
// Otherwise it will never be, because of the null argument
// Using 'this' rather than 'null' contradicts the requirement to be able to pass bound functions around
// The object 'before the dot' won't be available at call time in that case, unless implicitly via the function bound to it
// While for Python methods this mechanism is generated by the compiler, for JavaScript methods it has to be provided manually
// Call memoizing is unattractive here, since every string would then have to hold a reference to a bound format method
Object.defineProperty (String.prototype, 'format', {
    get: function () {return __get__ (this, function (self) {
        var args = tuple ([] .slice.apply (arguments).slice (1));           
        var autoIndex = 0;
        return self.replace (/\{(\w*)\}/g, function (match, key) { 
            if (key == '') {
                key = autoIndex++;
            }
            if (key == +key) {  // So key is numerical
                return args [key] == 'undefined' ? match : args [key];
            }
            else {              // Key is a string
                for (var index = 0; index < args.length; index++) {
                    // Find first 'dict' that has that key and the right field
                    if (typeof args [index] == 'object' && typeof args [index][key] != 'undefined') {
                        return args [index][key];   // Return that field field
                    }
                }
                return match;
            }
        });
    });},
    enumerable: true
});

which is a one time action, saving you the trouble of doing it over and over again. You can e.g. rename it to py_format. The altered line of code will look like:

Object.defineProperty (String.prototype, 'py_format', {

You can then use the alias pragma

http://sterlicht.alwaysdata.net/transcrypt.org/docs/html/special_facilities.html#pragma-alias

to still use under the same name from Python, but since you don't use it that's not even needed.

By the way I am curious why the code complains: Cannot redefine etc. Do you have any idea where the other 'format' property comes from? Must be from some other lib you use. If I know where it comes from, there may be better ways around this, that maybe could be part of the next version of Transcrypt.

from transcrypt.

shaolo1 avatar shaolo1 commented on August 30, 2024

Thanks for the response!
I'm not yet importing any libraries. When I catch up, I'll try to whittle my code down to something I can post.
In case it helps, I'm using Firefox on Ubuntu. I also tried Chromium and got the same error.

from transcrypt.

JdeH avatar JdeH commented on August 30, 2024

OK, I'd shure like to understand what's going on here, so I'd like to be able to reproduce the problem.

from transcrypt.

shaolo1 avatar shaolo1 commented on August 30, 2024

I believe the other format comes from the base class. I reproduced this with the hello sample. I renamed hello.py to hello_base.py and created a new hello.py that contains the following...

from .hello_base import SolarSystem

class SolarSystem2(SolarSystem):
    pass

solarSystem = SolarSystem2 ()

I then added the following to hello.html
<script src="__javascript__/hello_base.js"></script>

from transcrypt.

JdeH avatar JdeH commented on August 30, 2024

Only now do I understand what you mean here!
Transcrypt is meant as a means to create web applications, and it has a somewhat different programming model from typical JavaScript snippets.
All Transcrypt for one URL c.q webpage is in ONE and only ONE program (with as many entrypoints (functions or methods) as you wish, so as many "subprograms" or "snippets"as you wish.
So effectively a webpage containing Transcrypt code is a "stand alone" application.

If you have multiple Transcrypt apps on one URL c.q. page, they will conflict. It is as if you run multiple programs in one desktop window.

This is explained in:

http://sterlicht.alwaysdata.net/transcrypt.org/docs/html/what_why.html#code-structure

from transcrypt.

Related Issues (20)

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.