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?
@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?
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:
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.
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?
Nothing lept out at me in a quick look.
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.
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).
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.
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?
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:
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.
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
So, no
www.reddit.com
to replace, unfortunately. You can, instead, check for the/r/
prefix in a href and injectwww.old.reddit.com
in front of the url.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.
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.
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.