▲ 8 r/learnpython+1 crossposts

Learning PyTorch tensors, anyone can explain?

Hello! I am currently trying to build the knowledge needed to make an Artificial Intelligence with Python, and I have discovered that there is some tensors involved in this, I learnt matrices (briefly), but apparently math tensors are not the same as CS tensors (whatever that means?), can someone please help me? And I wonder if I even need to know this (well ig so but not sure). And I am not in any math or cs major, but I am in mid-high school, so as you can guess, I did not learn anything tensors related.

Note: for anyone wondering; no I don't have anybody that I could contact which could explain me PyTorch tensors.

reddit.com
u/Objective-Farmer4183 — 3 days ago

I've seen a lot of people saying that Rust = C but more secure. Is that true? Is there any catch or something? If you've used both of these programming languages, what was your experience with each? Which ones did you prefer?

I am writing this because I still don't know in which language should I code my backend for a website, and I don't want to mess things up. I am choosing low level because I want performance and low RAM usage + I am more familiar with low level languages.

TLDR; When do you think I should rather use C over Rust and vice versa?

reddit.com
u/Objective-Farmer4183 — 4 months ago
▲ 19 r/rust

I have been thinking about learning Rust for some time now and I was wondering where could I learn the language? I come from C and thought that Rust would be a good language to improve the security of my projects using different languages to do different tasks. Perhaps maybe someone has any website I could use? I have found a website called "The Rust Programming Language Book", wondered if anything was better/easier to learn me the basics from a C background.

Any advice is welcome, so thanks in advance.

reddit.com
u/Objective-Farmer4183 — 4 months ago

So I have found this website: https://www.geeksforgeeks.org/cpp/sql-using-c-c-and-sqlite/ thanks to someone on Reddit and found that the last bit would help me as it outputted a result from the database. But I need a string to make what I want to do. Is there anyway I can do that? This might be unclear so let me try to make this less complicated:

SQL finds data in database -> do ??? -> get a string -> then use the open() function from my code (below) -> voila

Basically what I need is the second part. Where I want to implement that is in my gameslist_play function. If you want clarification pls ask.

This is my code (shortened as some part are useless in this case):

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <windows.h>
#include <direct.h>
#include <stdbool.h>
#include <errno.h>
#include <shellapi.h>
#include <time.h>
#include "sqlite3.h"


void run_sql(char *sql) {  //gameslist.db connection via SQLite
    sqlite3 *db;
    char *zErrMsg = 0;
    
    //path string placeholder to gameslist
    const char* db_path = "C:/Users/My_Profil/Desktop/C (or more)/The_shell/gameslist.db";


    if (sqlite3_open(db_path, &db)) {
        fprintf(stderr, "DB Error: %s\n", sqlite3_errmsg(db));
        return;
    }


    if (sqlite3_exec(db, sql, 0, 0, &zErrMsg) != SQLITE_OK) {
        fprintf(stderr, "SQL Error: %s\n", zErrMsg);
        sqlite3_free(zErrMsg);
    }


    sqlite3_close(db);
}


char *buffer;


void remover(char *input) { //changes input pos to get to file/folder name to avoid error due 
                            //to program searching to for example open file.exe by looking for "open file.exe"
                            //instead of "file.exe" (always use after input detection)
    size_t position = strcspn(input, "\n");
    input[position] = '\0';
}


void cd (char *input) { //change directory (cd .. to go backwards)
    if (_chdir( input + 3 ) ) {
        switch (errno) {
            case ENOENT:
                printf( "Unable to locate the directory: %s\n", input + 3);
                break;
            case EINVAL:
                printf( "Invalid buffer.\n");
                break;
            default:
                printf( "Unknown error.\n");
            }
    }
}

void open_but_not_the_stupid_API_though_mine (char *input) { //open file
    SHELLEXECUTEINFO sei = {0};
    sei.cbSize = sizeof(SHELLEXECUTEINFO);
    sei.lpVerb = "open";
    sei.lpFile = input + 5;
    sei.nShow = SW_SHOWNORMAL;
    ShellExecuteEx(&sei);
}

void gameslist_play(char *input) {
    //This is where my question is
}


void gameslist_add(char *input) {
    char sql_cmd[512];
    sprintf(sql_cmd, "INSERT INTO games (title) VALUES ('%s')", input);
    run_sql(sql_cmd);
}


int main () {
    bool loop = true;
    printf("Hey!! I am a shell, type help to show tutorial\n");
    char input[256];
    int go_back = 3;
    for (int i = 0; i < go_back; i++) {
        _chdir("..");
    }
    
    run_sql("CREATE TABLE IF NOT EXISTS games (id INTEGER PRIMARY KEY, title TEXT);"); //table init


    while(loop == true) {
        if ((buffer = _getcwd( NULL, 0)) == NULL )
            perror( "_getcwd error" );
        else {
            printf("%s> ", buffer);
            free(buffer);}
        
        memset(input, 0, sizeof(input));
        fgets(input, 256, stdin);
        
        //input detection ↓


        if (strncmp(input, "leave", 5) == 0) {
            loop = false;
            continue;
        }
        else if (strncmp(input, "cd", 2) == 0) {
            remover(input);
            cd(input);
        }
        
        else if (strncmp(input, "open", 4) == 0) {
            remover(input);
            open_but_not_the_stupid_API_though_mine(input);
        }
        
        else if (strncmp(input, "gameslist_play", 14) == 0) {
            remover(input);
            
        }
        else if (strncmp(input, "gameslist_add", 13) == 0) {
            remover(input);
            gameslist_add(input);
        }
    }
}

Feel free to say anything about my code even if it is unrelated to my specific question as I am trying to improve my coding skills.

Thanks in advance,

u/Objective-Farmer4183 — 4 months ago

I have been trying to make a shell to learn me fundamentals of how to interact with the OS and file/folder management. And have recently decided to launch me into another way to make me depressive by learning SQLite to make my shell remember paths selected by the user by making a "gameslist" (original idea of my shell being a library for games, hence the name).
I managed (I think) to add those paths into the database, but now I am stuck with how to extract those same programs into a string (a char * (well I think)) to later run/open that same program through another other function. Couldn't figure out how to make that (that's probably a skill issue)

My code looks like this:

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <windows.h>
#include <direct.h>
#include <stdbool.h>
#include <errno.h>
#include <shellapi.h>
#include <time.h>
#include "sqlite3.h"


void run_sql(char *sql) {  //gameslist.db connection via SQLite
    sqlite3 *db;
    char *zErrMsg = 0;
    
    //path string placeholder to gameslist
    const char* db_path = "C:/Users/My_Profil/Desktop/C (or more)/The_shell/gameslist.db";


    if (sqlite3_open(db_path, &db)) {
        fprintf(stderr, "DB Error: %s\n", sqlite3_errmsg(db));
        return;
    }


    if (sqlite3_exec(db, sql, 0, 0, &zErrMsg) != SQLITE_OK) {
        fprintf(stderr, "SQL Error: %s\n", zErrMsg);
        sqlite3_free(zErrMsg);
    }


    sqlite3_close(db);
}


char *buffer;


void remover(char *input) { //changes input pos to get to file/folder name to avoid error due 
                            //to program searching to for example open file.exe by looking for "open file.exe"
                            //instead of "file.exe" (always use after input detection)
    size_t position = strcspn(input, "\n");
    input[position] = '\0';
}


void cd (char *input) { //change directory (cd .. to go backwards)
    if (_chdir( input + 3 ) ) {
        switch (errno) {
            case ENOENT:
                printf( "Unable to locate the directory: %s\n", input + 3);
                break;
            case EINVAL:
                printf( "Invalid buffer.\n");
                break;
            default:
                printf( "Unknown error.\n");
            }
    }
}


void my_mkdir_because_some_random_library_took_my_function_name (char *input) { //create folder (dir)
    if (_mkdir(input + 6)) {
        switch (errno) {
            case ENOENT:
                printf( "Unable to create folder due to path not existing: %s\n", input + 6);
                break;
            case EINVAL:
                printf("Invalid folder name.\n");
                break; 
            case EEXIST:
                printf("Folder already existing.\n");
                break;
            default:
                printf("Unknown error.\n");
        }
    }
}


void open_but_not_the_stupid_API_though_mine (char *input) { //open file
    SHELLEXECUTEINFO sei = {0};
    sei.cbSize = sizeof(SHELLEXECUTEINFO);
    sei.lpVerb = "open";
    sei.lpFile = input + 5;
    sei.nShow = SW_SHOWNORMAL;
    ShellExecuteEx(&sei);
}


void my_dear_dir (char *input) { // tells dirrectory information 
                                 //(Name, Creation Date, Last Access Date, Modification Date)
    WIN32_FIND_DATA wfd = {0};
    HANDLE hFind = INVALID_HANDLE_VALUE;


    SYSTEMTIME st;


    char search_path[MAX_PATH];
    
    if (input == NULL || strlen(input) == 0 || strcmp(input, " ") == 0) {
        sprintf(search_path, ".\\*");
    } else {
        sprintf(search_path, "%s\\*", input);
    }


    hFind = FindFirstFile(search_path, &wfd);


    


    if (hFind == INVALID_HANDLE_VALUE) {
        printf("Invalid search path or directory empty.\n");
        return;
    }


    do {
        if (strcmp(wfd.cFileName, ".") == 0 || strcmp(wfd.cFileName, "..") == 0) {
            continue;
        }
        
        printf("Found: %s\n", wfd.cFileName);
        Sleep(50);
        FileTimeToSystemTime(&wfd.ftCreationTime, &st);
        printf(" Date Created: %02d/%02d/%d\n", st.wDay, st.wMonth, st.wYear);
        Sleep(50);
        FileTimeToSystemTime(&wfd.ftLastAccessTime, &st);
        printf(" Last Accessed: %02d/%02d/%d\n", st.wDay, st.wMonth, st.wYear);
        Sleep(50);
        FileTimeToSystemTime(&wfd.ftLastWriteTime, &st);
        printf(" Last Modified: %02d/%02d/%d\n", st.wDay, st.wMonth, st.wYear);
        Sleep(250);
    } while (FindNextFile(hFind, &wfd) != 0);


    FindClose(hFind);
    
}


void tutorial() { //tells what each keyword does 
    printf("Commands below: \nLeave: leave\nChange Directory Onwards: cd *directory name*\nChange Directory Backwards: cd ..\nCreate Folder: mkdir *desired folder name*\nRun/Open File: open *file name*\nShow Directory Information: dir *direcotry name*\n");
}


void my_pwd() { //shows current dir.
    char *cwd = _getcwd(NULL, 0);
    if (cwd != NULL) {
        printf("Current Working Directory: %s\n", cwd);
        free(cwd);
    } else {
        perror("pwd error");
    }
}



void gameslist_play(char input) {
    //This is where my question is
}


void gameslist_add(char *input) {
    char sql_cmd[512];
    sprintf(sql_cmd, "INSERT INTO games (title) VALUES %s", input);
    sql_run(sql_cmd);
}


int main () {
    bool loop = true;
    printf("Hey!! I am a shell, type help to show tutorial\n");
    char input[256];
    int go_back = 3;
    for (int i = 0; i < go_back; i++) {
        _chdir("..");
    }
    
    run_sql("CREATE TABLE IF NOT EXISTS games (id INTEGER PRIMARY KEY, title TEXT);"); //table init


    while(loop == true) {
        if ((buffer = _getcwd( NULL, 0)) == NULL )
            perror( "_getcwd error" );
        else {
            printf("%s> ", buffer);
            free(buffer);}
        
        memset(input, 0, sizeof(input));
        fgets(input, 256, stdin);
        
        //input detection ↓


        if (strncmp(input, "leave", 5) == 0) {
            loop = false;
            continue;
        }
        else if (strncmp(input, "cd", 2) == 0) {
            remover(input);
            cd(input);
        }
        else if (strncmp(input, "mkdir", 5) == 0) {
            remover(input);
            my_mkdir_because_some_random_library_took_my_function_name(input);
        }
        else if (strncmp(input, "open", 4) == 0) {
            remover(input);
            open_but_not_the_stupid_API_though_mine(input);
        }
        else if (strncmp(input, "dir", 3) == 0) {
            remover(input);
            if (strlen(input) <= 4) {
                my_dear_dir("."); 
            } else {
                my_dear_dir(input + 4); 
            }
        }
        else if (strncmp(input, "help", 4) == 0) {
            tutorial();
        }
        else if (strncmp(input, "pwd", 3) == 0) {
            my_pwd();
        }
        else if (strncmp(input, "gameslist_play", 14) == 0) {
            remover(input);
            //what do I do?
        }
        else if (strncmp(input, "gameslist_add", 13) == 0) {
            remover(input);
            gameslist_add(input);
        }
    }
}

Can someone help me pls.
Thanks,

reddit.com
u/Objective-Farmer4183 — 4 months ago