Interests: programming, video games, anime, music composition

I used to be on kbin as e0qdk@kbin.social before it broke down.

  • 1 Post
  • 16 Comments
Joined 1 year ago
cake
Cake day: November 27th, 2023

help-circle

  • It’s really about lowering cognitive load when making edits. It’s not necessarily that someone can’t figure out how to do something more sophisticated, but that they’re more likely to get things right if the code is just kind of straightforwardly dumb.

    The last two are definitely situational – changing things like that might lower cognitive load for one kind of work but raise it significantly for another – but I can see where they’re coming from with those suggestions.





  • Try adding some prints to stderr through my earlier test program then and see if you can find where it stops giving you output. Does output work before curl_easy_init? After it? Somewhere later on?

    Note that I did update the program to add the line with CURLOPT_ERRORBUFFER – that’s not strictly needed, but might provide more debug info if something goes wrong later in the program. (Forgot to add the setup line initially despite writing the rest of it… 🤦‍♂️️)

    You could also try adding curl_easy_setopt(curl, CURLOPT_VERBOSE, 1L); to get it to explain more details about what it’s doing internally if you can get it to print output at all.



  • As a sanity check, does this work?

    #include <curl/curl.h>
    #include <stdio.h>
    #include <stdlib.h>
    
    size_t save_to_disk(char* ptr, size_t size, size_t nmemb, void* user_data)
    {
        /* according to curl's docs size is always 1 */
        
        FILE* fp = (FILE*)user_data;
        fprintf(stderr, "got %lu bytes\n", nmemb);
        return fwrite(ptr, size, nmemb, fp);
    }
    
    int main(int argc, char* argv[])
    {
        char errbuf[CURL_ERROR_SIZE];
        FILE* fp = NULL;
        CURLcode res;
        
        CURL* curl = curl_easy_init();
        
        if(!curl)
        {
            fprintf(stderr, "Failed to initialize curl\n");
            return EXIT_FAILURE;
        }
        
        fp = fopen("output.data", "wb");
        if(!fp)
        {
            fprintf(stderr, "Failed to open file for writing!");
            return EXIT_FAILURE;
        }
        
        curl_easy_setopt(curl, CURLOPT_URL, "https://www.wikipedia.org/");
        curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, save_to_disk);
        curl_easy_setopt(curl, CURLOPT_WRITEDATA, fp);
        curl_easy_setopt(curl, CURLOPT_ERRORBUFFER, errbuf);
        
        errbuf[0] = 0;  /* set error buffer to empty string */
        res = curl_easy_perform(curl);
        
        if(fp)
        {
            fclose(fp);
            fp = NULL;
        }
        
        if(res != CURLE_OK)
        {
            fprintf(stderr, "error code   : %d\n", res);
            fprintf(stderr, "error buffer : %s\n", errbuf);
            fprintf(stderr, "easy_strerror: %s\n", curl_easy_strerror(res));
            
            return EXIT_FAILURE;
        }
        else
        {
            fprintf(stderr, "\nDone\n");
            return EXIT_SUCCESS;
        }
    }
    

    That should write a file called output.data with the HTML from https://www.wikipedia.org/ and print out the number of bytes each time the write callback receives data for processing.

    On my machine, it prints the following when it works successfully (byte counts may vary for you):

    got 13716 bytes
    got 16320 bytes
    got 2732 bytes
    got 16320 bytes
    got 16320 bytes
    got 128 bytes
    got 16320 bytes
    got 16320 bytes
    got 1822 bytes
    
    Done
    

    If I change the URL to nonsense instead to make it fail, it prints text like this on my system:

    error code   : 6
    error buffer : Could not resolve host: nonsense
    easy_strerror: Couldn't resolve host name
    

    Edit: corrected missing line in source (i.e. added line with CURLOPT_ERRORBUFFER which is needed to get extra info in the error buffer on failure, of course)

    Edit 2: tweaks to wording to try to be more clear




  • If you want to improve significantly, go read someone else’s code and modify it. Try to fix a bug in a program you use, add a feature you want that doesn’t exist already, or even just do something simple for the sake of proving to yourself that you can do it – like compiling it from source and figuring out how to change some small snippet of text in a message box. Even if you don’t succeed, if you put in a serious effort attempting it, you will almost certainly learn a lot from trying.

    Edit: changed wording to try to be clearer


  • I wrote something like this before for academic researchers to load data sets on display walls by using their cellphones. I approached it by building a simple website. When the user logs in, they’d see a table of entries (from a directory listing on a shared file server that they could drop their data sets onto) and could click a button that made a form post to the server which caused it to run whichever programs were needed to load the data set they wanted (or run a couple of other handy commands – like turning the monitors on/off, etc).

    You can do something like that too in Python if you want:

    1. Learn how to start and stop programs from Python scripts. This can be done with the built-in subprocess library. If you know how to launch the programs you want from the command line, it shouldn’t be too hard to figure out how to do it from Python by reading the documentation. It will take some more effort to figure out how to interact with it (e.g. to stop it from user input) without blocking your script, but this can be done.
    2. Learn how to write a simple program that can respond to HTTP requests in Python. There are a number of libraries like tornado, flask, cherrypy, etc. that can do this. Pick one, read the documentation, and write a tiny page that allows you to submit a form and then trigger an action on the server in response to an HTTP POST. You should be able to interact with it by pointing the browser on your computer to localhost (possibly plus a port) or from on your LAN by putting the IP of your computer into the address bar.
    3. Figure out how you’re going to organize the entries you want to be able to load. You could just do something trivial like putting the files in known folders and running os.listdir, or something more involved like tracking the entries with a spreadsheet or database or JSON file that lets you associate custom metadata with each entry (like a custom name to show or an icon to display or when it was last launched, etc.)
    4. Generate a web page based on that data collection. I recommend using templating – e.g. with mustache, or jinja, etc. Basically you write some HTML-like text that lets you indicate places to fill in data from your program and it will do the conversion of symbols like < into &lt; that are needed for HTML output and also repeat patterns using entries from lists you provide to build the rows of tables and such for you.
    5. Set up some security (e.g. a simple log in system) and polish it up as much as you care to do.

    Good luck and have fun!


  • My old username from reddit and HN was already taken and I couldn’t think of anything else I wanted to be called so I just picked some random characters like this:

    >>> import random
    >>> ''.join([random.choice("abcdefghijklmnopqrstuvwxyz0123456789") for x in range(5)])
    'e0qdk'
    

    I have that literally in my kbin profile, but it’s not on my reddthat one. (I think I tried to copy it there originally when I set up the account but ran into some issue with Lemmy’s UI – been long enough that I forget what exactly.)