Coder Social home page Coder Social logo

Comments (4)

JoeLoser avatar JoeLoser commented on June 20, 2024 1

Your second example works fine on top-of-tree and will work in 24.2:

from python import Python

fn main() raises:
    var json = Python.import_module("json")
    var a = json.loads('{"a": 1, "b": 2}')
    print(a.values())
    for v in a.values():
        print(v)

It prints:

dict_values([1, 2])
1
2

Your first example:

from python import Python 
        
def main():
    var json = Python.import_module("json")
    a = json.loads('{"a": 1}')
    print(a.values())
    print(a.values()[0]) 

crashes on the subscript operator as you mentioned. We should fix that to give a better error message. The print(a.values()) prints:

dict_values([1])

as expected

from mojo.

lattner avatar lattner commented on June 20, 2024 1

This is fixed in the next update, thank you for reporting this!

from mojo.

elhigu avatar elhigu commented on June 20, 2024

Tried also iterating over that, which fails to iterator not being mutable or something like that.

(venv) mlepisto@stevetop:~/projects/learn-machine$ cat test.mojo 
from python import Python

fn main() raises:
    var json = Python.import_module("json")
    var a = json.loads('{"a": 1, "b": 2}')
    print(a.values())
    for v in a.values():
        print(v)
(venv) mlepisto@stevetop:~/projects/learn-machine$ mojo run test.mojo 
/home/mlepisto/projects/learn-machine/test.mojo:8:22: error: invalid call to '__iter__': argument #0 must be mutable in order to pass as a by-ref argument
    for v in a.values():
             ~~~~~~~~^~
/home/mlepisto/projects/learn-machine/test.mojo:1:1: note: function declared here
from python import Python
^
mojo: error: failed to parse the provided Mojo

from mojo.

lattner avatar lattner commented on June 20, 2024

@akirchhoff-modular helped track this down - this is caused by register-only types being handled by VariadicList instead of VariadicListMem, so their copy constructors don't get run correctly. There is a big migration to move the variadic list representation over to this new form, which can hopefully help, but I'm not exactly sure when it will converge

from mojo.

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.