9 votes

Fortnightly Programming Q&A Thread

General Programming Q&A thread! Ask any questions about programming, answer the questions of other users, or post suggestions for future threads.

Don't forget to format your code using the triple backticks or tildes:

Here is my schema:

```sql
CREATE TABLE article_to_warehouse (
  article_id   INTEGER
, warehouse_id INTEGER
)
;
```

How do I add a `UNIQUE` constraint?

12 comments

  1. [2]
    Kuromantis
    (edited )
    Link
    @Adys told me they would have recommended python as a first learning language if they were in charge of my school and I wanted to look into that suggestion. So where can I go for an introduction...

    @Adys told me they would have recommended python as a first learning language if they were in charge of my school and I wanted to look into that suggestion. So where can I go for an introduction to that language?

    I also once heard about there being "documentation" for programming languages, particularly rust. What does this mean. Is there a PDF made by the developers of a programming language explaining everything in a language?

    Lastly, what exactly is HTML5's relation to the web/web browsers? I've understood it as a kind of default language for that kind of stuff but I'm not sure that's true?

    5 votes
    1. Deimos
      Link Parent
      Al Sweigart has written multiple books teaching Python that seem to be well-regarded, and they're all available for free on his website. These ones are all aimed at beginners and have different...

      Al Sweigart has written multiple books teaching Python that seem to be well-regarded, and they're all available for free on his website. These ones are all aimed at beginners and have different themes, so you should be able to pick whichever one seems most interesting to work through:

      Documentation for programming languages is usually available on the web, and is a reference to how most things in the language work. Python's is here, Rust's is here, etc. Most people don't read a language's docs fully or in order, but it's normal to refer to the documentation regularly while programming. For example I currently have this page in the Python docs open, because I needed to generate some random stuff, and those are the built-in features in Python for doing that. Some language's docs can be downloaded in PDF and other formats, but it's usually easiest to just use the web version.

      HTML is... kind of the language that defines the content of a web page, and a browser reads that HTML and uses it to decide how to display the content. It's a "markup language" (that's what the "ML" stands for), so you can compare it to the markdown format you write in Tildes comments. The markdown you write actually gets converted to HTML. For example, when I wrote that list of book titles above, the HTML generated is:

      <ul>
          <li><a href="http://inventwithpython.com/invent4thed/">Invent Your Own Computer Games with Python</a></li>
          <li><a href="http://inventwithpython.com/cracking/">Cracking Codes with Python</a></li>
          <li><a href="https://automatetheboringstuff.com/">Automate the Boring Stuff with Python</a></li>
      </ul>
      

      The <ul> and </ul> are defining the start and end of an "unordered list", the <li>...</li> are each "list item", the <a> are links with defined destinations.

      I don't think it's usually possible on mobile, but if you're on a PC you can usually right-click any page in a browser and pick something like "View page source" to see the HTML of the page you're looking at.

      Web development is complicated though, because there are so many different languages and layers involved. When you visit Tildes, in the background, Python code runs on the Tildes server and generates the HTML for the page you requested. Your browser receives that HTML, and interprets and combines it with CSS (another language that defines visual presentation like colors/sizes) to decide how to make the page look. When you click a vote button, the browser uses Javascript code (which runs on your device, inside the browser itself) to do some logic, sends a request to Tildes in the background, which causes other Python code to run, which sends back a small piece of HTML that the Javascript then injects into the page, causing the browser to replace the old vote button with the "activated" one.

      10 votes
  2. [4]
    bltzkrg22
    (edited )
    Link
    EDIT: Well, the app is live. There are still some edges to iron out. Some more resource-intensive parts never complete on my phone; on my PC though everything “works” (with the exception of 22B)....

    EDIT: Well, the app is live. There are still some edges to iron out. Some more resource-intensive parts never complete on my phone; on my PC though everything “works” (with the exception of 22B).


    I would appreciate a little help.

    https://gitlab.com/bltzkrg.22/advent-of-code-2020-in-blazor-wasm

    As a learning exercise, I’m currently trying to make a simple in-browser solver for last year’s Advent of Code problems, something akin to this. I didn’t publish it anywhere yet, but the repository contents should be buildable either in VS 2019 or with dotnet build/dotnet run command.

    I run into an issue specifically with problem 22. If I try to run the solver for part B of that day, it eats around 2.5 GiB of RAM and then crashes. The same solver runs fine as a simple console app. I don’t know if I’m doing something wrong, or if it’s some Blazor or WebAssembly limitation. Requesting garbage collection explicitly in C# code seems to have to effect on the app running in browser. Choosing Debug/Release also seems to have no effect on crashing.

    Is there anything I could do to reduce RAM usage?

    2 votes
    1. [3]
      TemulentTeatotaler
      (edited )
      Link Parent
      Nothing lept out at me in a quick look. I ran it in the VS 2022 preview. Other solutions worked fine. I had the same crash on 22 part B. Extracting part B into a quick notebook confirmed it worked...

      Nothing lept out at me in a quick look.

      • I ran it in the VS 2022 preview.
      • Other solutions worked fine.
      • I had the same crash on 22 part B.
      • Extracting part B into a quick notebook confirmed it worked elsewhere.
      • Firefox/Edge made no difference. No errors thrown.

      I'm not experienced with Blazor, but afaik the WASM is still a bit of a work in progress. Blazor Server might be worth trying out for the short term, if you aren't.

      3 votes
      1. [2]
        bltzkrg22
        Link Parent
        Thank you for taking the time to look into my issue. I guess I’ll leave this part as is, and maybe come back in a few months and see if anything improves after rebuilding. Whole idea was to deploy...

        Thank you for taking the time to look into my issue.

        I guess I’ll leave this part as is, and maybe come back in a few months and see if anything improves after rebuilding. Whole idea was to deploy this as a “static” app and make user’s browser handle all the computing, with no backing server.

        I wish I was able to see someone else’s attempt at the same problem, but all the projects I was able to find didn’t get past day 20. (There are plenty of solvers, just none specifically using Blazor WASM).

        3 votes
        1. balooga
          Link Parent
          I don't have much to contribute but I love the concept, a WASM solver is an awesome idea. I built a solver for AoC 2020 as well, mine was a CLI built in TypeScript. Never finished the solutions...

          I don't have much to contribute but I love the concept, a WASM solver is an awesome idea. I built a solver for AoC 2020 as well, mine was a CLI built in TypeScript. Never finished the solutions past day 12 though, one of these days I should circle back and finish! And maybe do some previous years as well if I get really bored and suddenly have tons of free time, haha.

          2 votes
  3. helloworld
    Link
    Any NixOS + Emacs users developing common lisp, here? Because I could use some help. I'm trying to run a simple webapp, it compiles and loads into SLY repl just fine. But when I try to start it, I...

    Any NixOS + Emacs users developing common lisp, here? Because I could use some help.

    I'm trying to run a simple webapp, it compiles and loads into SLY repl just fine. But when I try to start it, I get errors that it couldn't find OpenSSL. So I created a shell.nix modifying LD_LIBRARY_PATH but still no luck. I have the direnv package for emacs which detects the shell.nix and loads it automatically but for some reason its not working with SLY. Interestingly enough, when I check the path that SLY is using to look for libraries in, is NIL. I even set it in .sbclrc but still no dice. What gives?

    2 votes
  4. [5]
    joplin
    Link
    OK, I’ve got a web development question for y’all. I don’t normally do web development, but I want to write myself a content filter that does some simple find/replace and deletion of annoying...

    OK, I’ve got a web development question for y’all. I don’t normally do web development, but I want to write myself a content filter that does some simple find/replace and deletion of annoying things. So I’m implementing a Safari Web Extension. I have it mostly working, but I’m unclear on some things. One of the things it does is replace all links that point to www.reddit.com with links that instead point to old.reddit.com. (I rarely visit reddit, but it seemed like an easy thing to change.) It works on most sites, but not, oddly enough, on reddit.com. For example all links here on Tildes are properly rewritten and go to the old version of the page in question, but if I manually go to www.reddit.com, it doesn’t quite work. If I hover over a link on the main page, it shows as being to old.reddit.com/whatever, but if I click on it, it actually goes to www.reddit.com/whatever. And once I reach that page, none of the links on that page are overwritten. So I figure they’re probably doing something similar and their own script is running after mine or something. How can I debug this, and if that is the problem, is there a way to tell Javsascript to always run my script last, or something like that?

    For reference, here’s how I’m changing the links on a page:

    var anchors = document.getElementsByTag(“a”);
    for (var i = 0; i < anchors.length(); i++) {
        var ref = anchors [ i ].href;
        ref = ref.replace(“www.reddit.com”, “old.reddit.com”);
        anchors [ i ].href = ref;
    }
    

    It’s nothing fancy and may not be a good way to do this, but it’s what I came up with so far and seems to almost work. 🙂 Any help appreciated.

    2 votes
    1. [2]
      stu2b50
      Link Parent
      I took a glance at the reddit frontpage and what I suspect is the issue is that reddit uses all relative links. For instance, here's one of the anchor tags <a data-click-id="body"...

      I took a glance at the reddit frontpage and what I suspect is the issue is that reddit uses all relative links. For instance, here's one of the anchor tags

      <a data-click-id="body" class="SQnoC3ObvgnGjWt90zD9Z _2INHSNB8V5eaWp4P0rY_mE" 
      href="/r/AskReddit/comments/osoje1/you_wake_up_tomorrow_with_jeff_bezos_current_net/">
      

      So, no www.reddit.com to replace, unfortunately. You can, instead, check for the /r/ prefix in a href and inject www.old.reddit.com in front of the url.

      3 votes
      1. joplin
        Link Parent
        Oh, duh! That makes complete sense. Thank you for pointing it out. It’s obvious now that you say it. I probably would have wasted a week looking at it and thinking about it before it occurred to me.

        Oh, duh! That makes complete sense. Thank you for pointing it out. It’s obvious now that you say it. I probably would have wasted a week looking at it and thinking about it before it occurred to me.

        1 vote
    2. [2]
      Moonchild
      Link Parent
      Another approach could be to check if the current URI is reddit.com, and if so redirect to old.reddit.com. You should probably do this in parallel with what the sibling suggests; this solution...

      Another approach could be to check if the current URI is reddit.com, and if so redirect to old.reddit.com. You should probably do this in parallel with what the sibling suggests; this solution adds a slight amount of lag (because you have to wait for the page to load twice), but it also solves the case when you enter 'reddit.com' directly; or when some random application tries to open reddit.com.

      3 votes
      1. joplin
        Link Parent
        Thanks! I actually just realized I could do this last night and gave it a try, and it works. I'd rather it rewrote the URL before the first load, but I'll take what I can get.

        Thanks! I actually just realized I could do this last night and gave it a try, and it works. I'd rather it rewrote the URL before the first load, but I'll take what I can get.

        1 vote