banananas
banananas
Explore posts from servers
MModular
Created by banananas on 10/23/2024 in #questions
Creating files from code
Is there a way to create a file from code? I've tried using a python script:
def create_proj(path: str):
with open(path + "/main.mojo", "x") as f:
f.write("text goes here")
def create_proj(path: str):
with open(path + "/main.mojo", "x") as f:
f.write("text goes here")
then:
from python import *

fn create_proj(folder: String) raises:
Python.add_to_path(".")
var file = Python.import_module("createproj")
file.create_proj(folder)
from python import *

fn create_proj(folder: String) raises:
Python.add_to_path(".")
var file = Python.import_module("createproj")
file.create_proj(folder)
but I have been getting this error:
Unhandled exception caught during execution: module 'createproj' has no attribute 'create_proj'
/home/[USERNAME]/pyregl/.magic/envs/default/bin/mojo: error: execution exited with a non-zero result: 1
Unhandled exception caught during execution: module 'createproj' has no attribute 'create_proj'
/home/[USERNAME]/pyregl/.magic/envs/default/bin/mojo: error: execution exited with a non-zero result: 1
10 replies
MModular
Created by banananas on 10/1/2024 in #questions
Using structs that inherit from a specific trait as a type?
If you have a trait and a struct that inherits from it, could you use that trait's children as a type in, for example, a list? E.g.:
trait MyTrait:
fn __init__(inout self): ...

struct MyStruct(MyTrait):
fn __init__(inout self): pass

fn main():
let my_list = List[structs that inherit from MyTrait](MyStruct())
trait MyTrait:
fn __init__(inout self): ...

struct MyStruct(MyTrait):
fn __init__(inout self): pass

fn main():
let my_list = List[structs that inherit from MyTrait](MyStruct())
15 replies
MModular
Created by banananas on 9/28/2024 in #questions
Calling methods from list indexes
When I try running update from here:
alias CptList = List[ComponentList]

struct Entity:
var components: CptList

fn __init__(inout self, components: CptList):
self.components = components

fn update(inout self):
for i in range(self.components.__len__()):
self.components[i].update()

fn addCpt(inout self, value: ComponentList):
self.components.append(value)
alias CptList = List[ComponentList]

struct Entity:
var components: CptList

fn __init__(inout self, components: CptList):
self.components = components

fn update(inout self):
for i in range(self.components.__len__()):
self.components[i].update()

fn addCpt(inout self, value: ComponentList):
self.components.append(value)
It returns this error:
error: 'Variant[BaseComponent]' value has no attribute 'update'
self.components[i].update()
~~~~~~~~~~~~~~~~~~^
error: 'Variant[BaseComponent]' value has no attribute 'update'
self.components[i].update()
~~~~~~~~~~~~~~~~~~^
ComponentList is defined here:
alias ComponentList = Variant[
BaseComponent,
]
alias ComponentList = Variant[
BaseComponent,
]
BaseComponent is defined here:
@value
struct BaseComponent: # example
fn __init__(inout self): print("hi!!!!!")

fn update(inout self): print("update method")
fn render(inout self): pass
@value
struct BaseComponent: # example
fn __init__(inout self): print("hi!!!!!")

fn update(inout self): print("update method")
fn render(inout self): pass
18 replies
MModular
Created by banananas on 9/25/2024 in #questions
Assigning additional traits to Variants
Is there a way to assign a trait to the Variant type? I'm specifically trying to use the __str__ method with List[Variant[types..]] specifically.
6 replies
MModular
Created by banananas on 9/23/2024 in #questions
"Linking" structs together?
I'm not talking about inheritance from other structs, but is there a way to refer to other structs as well when referring to one of them? For example:
var thing = MyStruct()
var other_thing = MyOtherStruct()
var list_of_things = List[MyStruct](thing, other_thing) # this is valid code since MyOtherStruct is linked to MyStruct
var thing = MyStruct()
var other_thing = MyOtherStruct()
var list_of_things = List[MyStruct](thing, other_thing) # this is valid code since MyOtherStruct is linked to MyStruct
44 replies
MModular
Created by banananas on 9/23/2024 in #questions
Is there a Struct type or something similar?
I'm trying to create a Set / List which can hold structs, but I'm not sure how to initialise this sort of type.
21 replies
MModular
Created by banananas on 9/21/2024 in #questions
Using the .append() method on structs
I have the following code:
alias Component = fn() -> None

fn baseComponent(): pass

struct Entity():
var components: List[Component]

fn __init__(inout self):
self.components = List[Component]()

fn add(self, method: Component):
self.components.append(method)
alias Component = fn() -> None

fn baseComponent(): pass

struct Entity():
var components: List[Component]

fn __init__(inout self):
self.components = List[Component]()

fn add(self, method: Component):
self.components.append(method)
However, line 12 gives an error even though I think it should be valid. What is the proper syntax in this case?
10 replies
MModular
Created by banananas on 9/15/2024 in #questions
Setting up SDL2 with Mojo on WSL
SDL is known for being notoriously hard to set up, and I only have experience with doing it in VS using NuGet (which is frankly a lot simpler). Does anybody know how to do it with Mojo and DLHandle?
5 replies
MModular
Created by banananas on 9/13/2024 in #questions
Removing a mojo project
Is there a way to remove a mojo project using the magic tool?
magic remove
magic remove
in the directory doesn't seem to do anything. Thanks in advance.
6 replies
MModular
Created by banananas on 9/13/2024 in #questions
Importing python modules
Whenever I try to import a python module using
import ctypes
import ctypes
(ctypes used as an example) VSCode gives me an error saying the module is inaccessible, and running mojo [file] says the same thing. I've checked and Python 3 is installed on both Windows 11 and WSL. (Using WSL) Thanks in advance.
4 replies
MModular
Created by banananas on 9/13/2024 in #questions
Adding a .mojo or .🔥file to a magic project
I've created a project using
magic init hello-world --format mojo-project
cd hello-world && magic shell
magic init hello-world --format mojo-project
cd hello-world && magic shell
but how could I add a file to this project / view its directory? (Using WSL Ubuntu) Thanks in advance.
5 replies
MModular
Created by banananas on 9/11/2024 in #questions
Installing Mojo on WSL?
I've got WSL downloaded and I have been trying to install Mojo, but all the tutorials that I find seem to be outdated, and running mojo commands isn't possible after installing Magic. Can someone tell me how to install Mojo with WSL?
7 replies