Wednesday, December 16, 2020

regexcellent

 I've mentioned it before but I do love my tool regexcellent - parts of my current project are making good use of my old school regex skills to scrape some html by hand. (relevant info from some thing like

SOMEKEY    <a href="SOMEURL"><img src="SOMEIMGURL" width="XX" height="YY" ></a>

with some other stuff.

The match was 

^(.*)\t\<a href\=\"(.*?)\"(.*)src\=\"(.*?)\"(.*)width\=\"(.*?)\" height\=\"(.*?)\"(.*?)$

and the replace was 

'$1':{ url:'$2', src:'$4', width:'$6', height:'$7'  },

Which produces something close enough to JSON that I can wrap it and pop it in JS and then do more formal manipulation...

I think way back when I used do this as macros in editors, lots of ctrl-arrow jumping around. And that was pretty quick! But I've lost track of what editors do that well, so this tool comes in handy.

Wednesday, December 9, 2020

the forager brain

Quote from Joshua Quittner, I think from Time Magazine circa 1998:

I am nuts for information-- as are we all, I suspect, most real men and women.  I can't get enough of the stuff.  When I'm clicking through the hundreds of E-mail messages that await me each morning, sometimes I imagine I'm a mighty information whale, sifting through thousands of tiny (but nutritious!) krill bits.  Yum!  Whether it's reading the cereal box or scanning the advertisement slide show some genius thought to project on the big screen at the movie theater, my appetite for information is unquenchable. 

This metaphor is kind of formalized as Information Foraging Theory. The claim is humans forage for information the way other animals forage for food - and that can be usefully applied to analyze "doom scrolling" and kneejerk email checking  and similar behaviors, where we swing back to the hunting grounds, and then stick/scroll around for periods of time based on the likelihood of something good showing up.

I was thinking about how I also stockpile information, and I use a motley collection of apps and websites to do so:

Simplenote App:

  • Projects Done
  • "Wisest" quotes
  • "Factoids and Intermittently Useful Information"
  • Medical events / things to discuss with my doctor
Google Docs:
  • Shared shopping list with my partner
  • Shared mailing list and zoom info for my science and spirituality reading group
  • Shared idea for future top list
Tot App:

  • Music to Get
  • Videos/Shows to Watch
  • Work Items to Do
Homebrew Online Database:
  • Passwords + Account Info
  • Media Journal
  • Random Useful Websites
For a bit it seemed weird that I don't have trouble remember what's recorded where... but now that I think about it, it makes sense that my brain is pretty well geared at remembering where any given "information hoard" is stored.

I think similarly, I've never been a fan of "RSS style readers" that take pure information content and put it into a generic template... the information loses that extraneous sensory data that helps me intuitively identify and recall the source/hunting ground..

Sunday, December 6, 2020

so random

 Nice piece on js coding with randomness. I need to get to making more artsy stuff myself!

preact and wmr - possible substitute for create-react-app

I am legit astonished at how much create-react-app - the de facto default - pulls. 250 Mb for just a Hello World! I really really really don't understand why the default brings so much... I'm tempted to make a package called "kitchen-sink" and lobby to get it included as part of the default.

JQuery (and modern CSS) really enhanced my homebrew programming, but Angular and React re-establishec the coding for work/coding for fun divide, in part because of this overhead... even though early React was more clearly meant to be part of an existing page, not presuming the "Single Webpage App" paradigm. I've done a few for fun projects in reat, but it never feels great, and since I don't usually use node on the backend, I get that "PHP for the JSON on the server" hubrid feel

I've had some better luck with Parcel. Another option might be wmr... it uses Preact which I haven't explored much, but might be more in tune with  my feeling of "I want to use basic React and JSX in a minimalist" way

on the naming of things (by other programmers)

A paraphrased quote I've gotten a lot of use (and some laughs) at possibly over the years:

It is a near universal belief among programmers that their peers can't name anything properly. Not variables nor subroutines... definitely not servers. Possibly not even their own children.

I decided to look up the original quote... looks like I grabbed it in September 2000 as 

How to tell you're becoming an technology prima donna:
You become convinced that none of your coworkers can name anything properly, whether it's machines, files, or their children. [...]

Alas, I failed to cite it properly and my sites are all Google knows about the original wording.

Thursday, December 3, 2020

php/js hack script to merge json files, keying on file name

 I know I "should" be using node for this, but:

Here is a php page that reads a bunch of files (in this case the files are "raw/_KEY_.json") and then prints out a single json object:

<pre><script>const raw = <?
$path = "raw";
$files = array_values(array_diff(scandir($path), array('.', '..')));
$tree = array();
foreach($files as $file){   
    list($key,$rest) = explode(".",$file);
    $tree[$key] = json_decode(file_get_contents("$path/$file"));
}
print json_encode($tree);
?>;
document.write(JSON.stringify(raw,null,' '));</script></pre>
For some reason the PHP version I had didn't seem to have JSON_PRETTY_PRINT defined (or I was doing it wrong) so I duck into js to do the pretty printing of the result... kind of an ObHack, that :-D

Tuesday, December 1, 2020

walk a json object, display paths of all parent nodes

One of the interesting things about styled components is that they can handle objects full of CSS stylings, not just strings.

So we have these json objects represented nested stylings, and I wanted to see all the keys (with their full paths) that had children (i.e. ignore any { "color" : "red" } simple properties) 

So a pretty good Comp Sci 101 exercise... 

const json = {
  "header":{
      "large":{
        "companyname":{
            color:"red"
        }
      },
    "small":{
        "companyname":{
            color:"green"
      }
    }
  }
}
let buffer = '';
showFullKeysOfParents(json,'');
console.log(buffer);

function showFullKeysOfParents(json,path){
  const keys = Object.keys(json);
  for(key of keys){
    if(typeof json[key] === 'object'){
      const newpath = `${path}${path===''?'':'.'}${key}`;
      buffer += `${newpath}\n`;
      showFullKeysOfParents(json[key],newpath);
    }
  }
  
}

I decided to just punt and use a global variable rather than waste more time fretting about return values and what not. 

The result of that is:

header
header.large
header.large.companyname
header.small
header.small.companyname

I made a code pen for it. Sidenote: I had to turn off "Match Brackets/Tags". I really don't understand why that kind of auto-complete is the default preference, and why coders like it... the "convenience" of saving a key or two comes no where near outweighing the unpredictability, and if you, say, go back to change the quote situation in a string, you can end up in a nightmare of appearing and disappearing quotes and single quotes.