Hasan Yousef
Hasan Yousef
MModular
Created by Hasan Yousef on 9/14/2024 in #questions
How can I convert String to StringLiteral
I understood that str() is required to convert things to String like (StringLiteral, Int, Bool, ...), but is there a way to covert String to LiteralString. As part of learning Mojolang I'm trying the below code, then this conversion issue poped up.
from collections import List
from sys.ffi import external_call

alias system = external_call["system", Int, StringLiteral]

fn run_commands(arg0: String, args: List[String]) -> Int:
var command: String = arg0
for arg in args:
command += " " + arg[]
return system(command) # invalid indirect call: argument #0 cannot be converted from 'String' to 'StringLiteral'

fn system2(arg0: String, args: List[String]) -> Int:
var command: String = arg0
for arg in args:
command += " " + arg[] # Access the element inside the list
return external_call["system", Int, String](command)

fn main():
# var args = List[String]("kill", "-9", "$(lsof -t -i :8000)")
var args = List[String]("ls")
_ = run_commands(args[0], args[1:])
_ = system2(args[0], args[1:])
from collections import List
from sys.ffi import external_call

alias system = external_call["system", Int, StringLiteral]

fn run_commands(arg0: String, args: List[String]) -> Int:
var command: String = arg0
for arg in args:
command += " " + arg[]
return system(command) # invalid indirect call: argument #0 cannot be converted from 'String' to 'StringLiteral'

fn system2(arg0: String, args: List[String]) -> Int:
var command: String = arg0
for arg in args:
command += " " + arg[] # Access the element inside the list
return external_call["system", Int, String](command)

fn main():
# var args = List[String]("kill", "-9", "$(lsof -t -i :8000)")
var args = List[String]("ls")
_ = run_commands(args[0], args[1:])
_ = system2(args[0], args[1:])
5 replies
MModular
Created by Hasan Yousef on 9/14/2024 in #questions
C/C++ Interop
My understaffed is horror we can get something like: https://docs.modular.com/mojo/roadmap#cc-interop And for clib interop we have sys.ffi.external_call My question is if I have a foo-lib.so that is a c/c++ lob, how can I call it's functions, and do I need the foo.h also?
5 replies
MModular
Created by Hasan Yousef on 9/12/2024 in #questions
Embedding
Hi all, not sure if I have to ask here or at MAX channek, is there embedding, vector database and similarity search in MAX that I can use with LLM?
3 replies
MModular
Created by Hasan Yousef on 2/13/2024 in #questions
Printf - print format
Is there a print format in mojo، how can I rewrite this python in mojo:
def fun (arg):
print(f'arg = {arg}')
bar = [5, 10, 15, 20]
def fun (arg):
print(f'arg = {arg}')
bar = [5, 10, 15, 20]
1 replies
MModular
Created by Hasan Yousef on 1/28/2024 in #questions
import gpu.Function
No description
4 replies
MModular
Created by Hasan Yousef on 1/6/2024 in #questions
Reducing binary size
No description
4 replies
MModular
Created by Hasan Yousef on 12/23/2023 in #questions
Running Mojo executable at Alpine docker
I created a mojo dockerfile the is working fine:
FROM ubuntu:latest
FROM ubuntu:latest
And wanted to use it as a builder image:
#Builder
FROM mojo-sdk:latest as builder
WORKDIR /app
COPY main.mojo /app/main.mojo
RUN /root/.modular/pkg/packages.modular.com_mojo/bin/mojo build /app/main.mojo

#Final
FROM alpine:latest
WORKDIR /app
COPY --from=builder /app/main /app/main
RUN chmod +x /app/main
ENTRYPOINT ["tail"]
CMD ["-f", "/dev/null"]
#Builder
FROM mojo-sdk:latest as builder
WORKDIR /app
COPY main.mojo /app/main.mojo
RUN /root/.modular/pkg/packages.modular.com_mojo/bin/mojo build /app/main.mojo

#Final
FROM alpine:latest
WORKDIR /app
COPY --from=builder /app/main /app/main
RUN chmod +x /app/main
ENTRYPOINT ["tail"]
CMD ["-f", "/dev/null"]
But I could not run the executable at the alpine based docker, and got the error:
/app # ls
main
/app # ./main
sh: ./main: not found
/app # ls -l
total 1040
-rwxr-xr-x 1 root root 1061752 Dec 23 18:27 main
/app # ls
main
/app # ./main
sh: ./main: not found
/app # ls -l
total 1040
-rwxr-xr-x 1 root root 1061752 Dec 23 18:27 main
I guess there are some dependencies that are exisiting at ubuntu and not exisiting at the Alpine docker, that is making the executable not be seen, noting that I just built the simpletst "heelo world" example.
1 replies
MModular
Created by Hasan Yousef on 12/15/2023 in #questions
Building Shared and Dynamic library
Can I build an executable and a library with mojo lang or executable only?
2 replies
MModular
Created by Hasan Yousef on 12/8/2023 in #questions
Distributing the binary as a docker image
I'm playing with mojo at ArchLinux and I am very excited about it, considering it is not yet supporting Windows, nor cross compilation, I would like to build a docker image of my binary, and import it at my Windows docker desktop, I would like to import from the scratch docker, what is the minimal setup or dependencies that should be including in my docker image to get the binary built using mojo run smoothly?
5 replies
MModular
Created by Hasan Yousef on 11/18/2023 in #questions
Running code at GPU
How can I write a mojo code that is executed at the GPU instead of the CPU?
14 replies
MModular
Created by Hasan Yousef on 10/19/2023 in #questions
Create a CSR
I can create CSR in Go as below, is there something similar in Mojo
package main

import (
"crypto/rand"
"crypto/rsa"
"crypto/x509"
"crypto/x509/pkix"
"encoding/pem"
"fmt"
)

func main() {
// Generate a new private key.
priv, err := rsa.GenerateKey(rand.Reader, 2048)
if err != nil {
fmt.Println(err)
return
}

// Create a simple template for the CSR.
csrTemplate := x509.CertificateRequest{
Subject: pkix.Name{
CommonName: "example.com",
Organization: []string{"Example, LLC"},
},
SignatureAlgorithm: x509.SHA256WithRSA,
}

// Create the CSR.
csrBytes, err := x509.CreateCertificateRequest(rand.Reader, &csrTemplate, priv)
if err != nil {
fmt.Println(err)
return
}

// PEM encode the CSR.
csrPEM := pem.EncodeToMemory(&pem.Block{
Type: "CERTIFICATE REQUEST",
Bytes: csrBytes,
})

fmt.Printf("%s\n", csrPEM)
}
package main

import (
"crypto/rand"
"crypto/rsa"
"crypto/x509"
"crypto/x509/pkix"
"encoding/pem"
"fmt"
)

func main() {
// Generate a new private key.
priv, err := rsa.GenerateKey(rand.Reader, 2048)
if err != nil {
fmt.Println(err)
return
}

// Create a simple template for the CSR.
csrTemplate := x509.CertificateRequest{
Subject: pkix.Name{
CommonName: "example.com",
Organization: []string{"Example, LLC"},
},
SignatureAlgorithm: x509.SHA256WithRSA,
}

// Create the CSR.
csrBytes, err := x509.CreateCertificateRequest(rand.Reader, &csrTemplate, priv)
if err != nil {
fmt.Println(err)
return
}

// PEM encode the CSR.
csrPEM := pem.EncodeToMemory(&pem.Block{
Type: "CERTIFICATE REQUEST",
Bytes: csrBytes,
})

fmt.Printf("%s\n", csrPEM)
}
2 replies
MModular
Created by Hasan Yousef on 9/28/2023 in #questions
Parallelism and multithreading
I couldn't find a sample code of Mojo multithreading, Can anyone guide me where I can find the proper guide, or provide me with sample snippets to learn from. thanks
3 replies
MModular
Created by Hasan Yousef on 9/25/2023 in #questions
HOWTO fetch internet resources
Is there a way in mojo that can be used for fetching websites, get and post?
14 replies