Help Bites
-
OK, I have a stupid small problem and I don't feel like it deserves its own thread. Maybe other people also have such issues. So I figured I'd start a "WTF Bites" style help thread.
Anyway, I'm trying to do a stupid regex trick. This is a one-off, so I don't care how dirty or hacky it is - it's not going into a codebase anywhere. I just have a bunch of gross markup I am trying to clean en masse.
Basically, I'm trying to do two things at once with this regex, either of which by itself is trivial. I need to:
- Match all the name="foo" in an HTML fragment. (OK to assume that all attributes are double-quoted.)
/\bname="([^"]+)"/
See, I told you it was trivial. - Remove all the hyphens in the names.
/-/g
Except I'm trying to do both of these tasks at once with a regex replace in my editor, and that's tricky. Basically, I want to do a find-and-replace within (restricted to) a regex find. To be clear, the whole match needs to be reinserted back into the document once the replacement is made.
Of course, if my approach is back-assward and there's a much easier option, let me know that, too!
Edit: Solved this more or less manually now, but still would like to know if this is possible, because it's not that uncommon of a task.
- Match all the name="foo" in an HTML fragment. (OK to assume that all attributes are double-quoted.)
-
@error Which is the editor of choice? Most have a regex find and replace function
-
@mrguyorama said in Help Bites:
Which is the editor of choice?
VS Code for the moment.
@mrguyorama said in Help Bites:
Most have a regex find and replace function
Yes, the stated problem is how do I handle a recursive regex find and replace?
In other words, find and find and replace?
-
@error shit wait yeah, I'm way out of my regex depth. I'd make a simple script to do this instead
-
@mrguyorama said in Help Bites:
@error shit wait yeah, I'm way out of my regex depth. I'd make a simple script to do this instead
It seems possible because I can
- Find all instances of the text I want to restrict scope to.
- Have multiple selections at once in VS Code.
- Restrict find and replace to current selection.
So all I'm really lacking is a bridge from 1 to 2 (i.e., select everything I matched in step 1, so it can be scoped for step 3).
Edit: no, actually, while 2 and 3 are true, they don't seem to work together. I should file a bug for that, actually.
-
@error open one of those other editors in the meantime. They probably have that feature working
-
@error said in Help Bites:
/\bname="([^"]+)"/
Starting to think outloud-- should be something like "([^"-]+)?" so it captures multiple groups of not-quote-not-dash inside two quotes.
-
@error said in Help Bites:
Solved this more or less manually now,
I've found this is sometimes the most efficient way...
-
grep -r | sed > file; find *.code -exec sed -f file {} +
-
At times like these, what you need is a worse editor.
M-x replace-regexp RET name="\([^"]+\)" RET name="\,(replace-regexp-in-string "-" "" \1)" RET
-
You know that the string has to be
name="[some string not containing "]"
, so why not replace(name="[^"-]*)-
with$1
?Keep running it until it doesn't replace anything.
-
@error said in Help Bites:
would like to know if this is possible
Not in a single operation; it's just a little bit beyond what that can do. Needs either scoped replacements or external looping.
-
Help bite: what the hell happened to my Chrome user icon?
(ssh icon included for reference)
-
@ben_lubar said in Help Bites:
Help bite: what the hell happened to my Chrome user icon?
(ssh icon included for reference)
It left in its place.
-
@ben_lubar mine changed too, when Chrome isn't open it shows the "missing photo" icon, but it shows my google account icon as normal when the window is open. Not sure what's up with that.
-
@ben_lubar Google
doodledoodoo.
-
@ben_lubar said in Help Bites:
You know that the string has to be
name="[some string not containing "]"
, so why not replace(name="[^"-]*)-
with$1
?Keep running it until it doesn't replace anything.
You didn't need to add
-
inside the[^"]
(the only difference it makes is the order in which they'll be removed, which doesn't matter), and it's probably a syntax error anyway since you didn't escape it.
-
In a theoretical perfect Model-View-Controller application that exists nowhere but in academia, where exactly are DB updates performed? I get the part that Model queries data and View reads it from model, and that View calls into Controllers, but what happens later - does Controller execute SQL INSERT/UPDATE statements and lets Model know that it did, or does Model expose a method that executes SQL statements, which gets called from Controller?
I need the most pedantic, the most strictly-by-the-book, the most ignore-reality-of-software-development-for-a-moment answer - I'm not making software, I'm doing homework. So "whatever works", "whatever makes sense the most", "it depends on details of what exactly you're doing" are all wrong answers.
-
@Gąska said in Help Bites:
does Model expose a method that executes SQL statements, which gets called from Controller
That, I think.
I believe the idea is that the model actually represents the state of the database, so by changing the model the DB should be updated too.
-
@Gąska Have you yet been called upon to determine whether the Tab character is an MVC violation?
-
@kazitor said in Help Bites:
I believe the idea is that the model actually represents the state of the database, so by changing the model the DB should be updated too.
Arguably, the model is the database (or a view over it).
-
In PL/SQL, how do I find out whether a given object is inside a given varray of references? Alternatively, what other container instead of varray can I use as an attribute of a type to store references of another object type, that lets me add objects and check if a given object is there already? Both types have NUMBER as primary key (well, it's PK of their tables, but it's an unique ID still).
Yes, it has to be objects and references. College work.
-
-
@Gribnit Okay, okay.
-
I'm losing my mind over something.
I'm using SQLite for a project. Don't ask why. It's the officialSystem.Data.SQLite.Core
package.
I have in my C# code:const string connectionString = "Data Source=database.db;Version=3;Pooling=true;"; using (var conn = new SQLiteConnection(connectionString).OpenAndReturn()) { using (conn.BeginTransaction()) { using (var cmd = conn.CreateCommand()) { cmd.CommandText = "CREATE TABLE IF NOT EXISTS foo(bar INTEGER)" await cmd.ExecuteNonQueryAsync(); Console.WriteLine("the query completed"); } } }
And even though I get "the query completed" in stdout (so there's definitely no exceptions thrown and the code's definitely being run), the table is not created at all. Nothing else has the database open; there are no strange file permission issues on the file; there is absolutely no reason that it should fail. Especially given that the same statement pasted into the SQLite DB browser adds the table successfully.
What the hell is going wrong here?
-
@pie_flavor said in Help Bites:
What the hell is going wrong here?
Looks like you're not completing your transaction.
-
@Tsaukpaetra Isn't the
using
block supposed to do that?
-
@pie_flavor said in Help Bites:
@Tsaukpaetra Isn't the
using
block supposed to do that?Using block ensures the transaction is cancelled (if memory serves) so you don't have to cancel it if something goes wrong.
Easiest thing is to try it out. Try like so:
const string connectionString = "Data Source=database.db;Version=3;Pooling=true;"; using (var conn = new SQLiteConnection(connectionString).OpenAndReturn()) { using (SqliteTransaction tr = conn.BeginTransaction()) { using (var cmd = conn.CreateCommand()) { cmd.CommandText = "CREATE TABLE IF NOT EXISTS foo(bar INTEGER)" await cmd.ExecuteNonQueryAsync(); Console.WriteLine("the query completed"); } tr.Commit(); } }
-
@Tsaukpaetra that was in fact it. I am of equal parts "I'm an idiot" and "the documentation writer is an idiot". Thanks!
-
@pie_flavor said in Help Bites:
I'm using MySQL for a project.
I'm using
MySQLSQLite as well and I've seen weird stuff like SQLite's own CLI not seeing new data even though it is there in the file. In fact Linqpad is also able to see the data there, not just my application.If I copy the file to a new one and I open the copy in sqlite3.exe it can see the data there.
And yes, I'm committing my transactions.
-
@Zecc what's MySQL have to do with it? I never said that.
-
@pie_flavor said in Help Bites:
@Zecc what's MySQL have to do with it? I never said that.
Yes you did! I saw it too. Before your edits!
-
@dcon fake news
-
@Zecc said in Help Bites:
I'm using
MySQLSQLite as well and I've seen weird stuff like SQLite's own CLI not seeing new data even though it is there in the file. In fact Linqpad is also able to see the data there, not just my application.Is one of the builds of SQLite built or configured to use the write-ahead log (WAL) and the other not? Because that's a separate, temporary file, and disagreeing over that could cause all sorts of fun with consistency.
-
@dkf WAL is something I had in my mind back then, and I still have a leftover browser tab open in https://sqlite.org/wal.html somewhere. I didn't finish reading that page however, as I have had more pressing things to do (such as keeping up with TDWTF. cough). The application I was working on did see the data, some other SQLite clients did as well; only sqlite3 did not .
What struck me as most weird is that I saw the new data in the copy I made (without bringing along any potential WAL file) and not the original. Which makes me think it might have been some issue with locking.
Either way that particular aspect of the application I inherited is working, and I have far bigger fish to fry.
-
@Zecc said in Help Bites:
What struck me as most weird is that I saw the new data in the copy I made (without bringing along any potential WAL file) and not the original. Which makes me think it might have been some issue with locking.
It's also possible to get that sort of crazy thing if accessing the DB from one place via a link (either hardlink or symlink) and the other directly. Always use a single real pathname to a DB (at least within its directory) as the name is used to construct the name of associated temporary files (the WAL among others). A stuck process with a lock might also cause trouble; those aren't copied when you copy the bytes to a new file, and the locking used is usually not of the kind that a simple copy would respect.
-
I've tried this several times, with the same result:
$ git push -v Pushing to https://[*REDACTED*] Counting objects 28991, done. Delta compression using up to 56 threads. Compressing objects: 100% 11560/11560), done. POST git-receive-pack (chunked)1) Writing objects: 100% (28991/28991), 9.02 GiB | 7.87 MiB/s, done. Total 28991 (delta 9563), reused 28234 (delta 9508) remote: Analyzing objects... (28991/28991) (1218491 ms) ... several minutes elapse ... error: RPC failed; curl 56 SSL read: errno -5961 fatal: The remote end hung up unexpectedly fatal: The remote end hung up unexpectedly Everything up-to-date $ git status Your branch is ahead of 'origin/foo' by 4 commits. (use "git push" to publish your local commits)
I especially love the last line; why didn't I think of doing that?
Any idea what might be going wrong?
-
-
Small design question. Let's say you have a bunch of functions (or "modules") that each implements different logic to check for a condition and the main program that calls them and does whatever's necessary in response. Something like
bool fireAlert(){ return getHouseTemperature() > 100; } bool intruderAlert(){ foreach (var sensor in intruderSensors) if (sensor.getStatus()) return true; return false; } bool somethingElseAlert(){ return whatever(); } int main(){ while(true){ if(fireAlert() || intruderAlert() || somethingElseAlert()) soundAlarm(); sleep(500); } }
But now we need to add a parameter to enable or disable each of them, for example
bool enableCheckFireAlert
. Should I check it inside the the fireAlert function or outside?On one hand, it's part of the "is there a fire alert going on" logic. On the other hand, it's not part of the core logic of checking for a fire alert, it's more like program flow logic.
-
struct { bool enabled; bool (*fun)(void); } alerts = { { 0, fireAlert }, { 1, intruderAlert }, { 1, somethingElseAlert } }; int main() { while (true) { for( int i = 0 ; i < sizeof(alerts)/sizeof(alerts[0]) ; i++ ) { if( alerts[i].enabled && alerts[i].fun() ) { soundAlarm(); break; } } sleep(500); } }
Never mind me.
-
@anonymous234 I would probably do something like:
bool fireAlertAlarm = fireAlertEnabled && fireAlert(); bool intruderAlertAlarm = intruderAlertEnabled && intruderAlert(); bool somethingElseAlertAlarm = somethingElseAlertEnabled && somethingElseAlert(); if(fireAlertAlarm || intruderAlertAlarm || somethingElseAlertAlarm) soundAlarm(); sleep(500);
Either that or just use one bool flag to indicate alarm state, and a separate if statement to check each alarm.
bool alarm = false; if(fireAlertEnabled && fireAlert()) alarm = true; ... if(alarm) soundAlarm(); sleep(500);
-
@anonymous234 Both.
public bool ShouldFireAlert() { if (this.FireAlertsEnabled) { return this.FireAlertConditions(); } } bool FireAlertConditions() { return this.GetHouseTemperature() > 100; }
-
@pie_flavor your
ShouldFireAlert
function has multiple exit points now.
-
@anotherusername One return point and one "fall off" point.
But yeah, I got the idea.
-
@anonymous234 I'm not saying that it's always bad to have multiple exit points. But he took what should be a pretty simple one-liner and made it into two separate functions, one of which has multiple exit points. It made it a lot less easy to read, in my opinion.
-
@anotherusername but it separates the logic of whether an alert should be sent from the logic of whether there's a fire worth alerting.
-
@pie_flavor The former doesn't need to be a separate function, I don't think. He had it in
main
and that's fine.
-
@PleegWat said in Help Bites:
struct { bool enabled; bool (*fun)(void); } alerts = { { 0, fireAlert }, { 1, intruderAlert }, { 1, somethingElseAlert } };
Never mind me.
I agree with whatever approximation of classes @PleegWat posted above.
-
@Zecc Yeah, I parsed the original as C style. If you've got classes, I'd recommend implementing this pattern with classes instead.
Also, abstracting out like this in the example case is overbuilt (which is one reason I skipped half the code). But if you've got a dozen checks, some of them being the same code with a different argument, and 2 or 3 shared properties, it becomes a useful pattern. Don't scoff the function pointer if you don't have classes.
-
@PleegWat said in Help Bites:
Don't scoff the function pointer if you don't have classes.