From 44bb45d2b4cea2186988020d692c9fd9b6eea78d Mon Sep 17 00:00:00 2001 From: elioat Date: Tue, 13 Dec 2022 11:47:58 -0500 Subject: cleaning up --- src/catalogue.lil | 44 ----------------- src/catalogue/catalogue.lil | 44 +++++++++++++++++ src/catalogue/test.csv | 6 +++ src/catalogue/tmp.lil | 113 ++++++++++++++++++++++++++++++++++++++++++++ src/pokedex.rkt | 59 ----------------------- src/pokedex/pokedex.rkt | 59 +++++++++++++++++++++++ src/pokedex/tmp.rkt | 63 ++++++++++++++++++++++++ src/test.csv | 6 --- src/tmp.lil | 113 -------------------------------------------- src/tmp.rkt | 63 ------------------------ 10 files changed, 285 insertions(+), 285 deletions(-) delete mode 100644 src/catalogue.lil create mode 100644 src/catalogue/catalogue.lil create mode 100644 src/catalogue/test.csv create mode 100644 src/catalogue/tmp.lil delete mode 100644 src/pokedex.rkt create mode 100644 src/pokedex/pokedex.rkt create mode 100644 src/pokedex/tmp.rkt delete mode 100644 src/test.csv delete mode 100644 src/tmp.lil delete mode 100644 src/tmp.rkt diff --git a/src/catalogue.lil b/src/catalogue.lil deleted file mode 100644 index 0b68c02..0000000 --- a/src/catalogue.lil +++ /dev/null @@ -1,44 +0,0 @@ -csv_db:"test.csv" - -on load_db db do - data:read[db] - out:readcsv[data "sss"] -end - -on save_db t db do - out:writecsv[t] - write[db out] -end - -on entry d t do - # where d is a table representing a new row to be added to t, an existing table - date_fmt_string:"date +%Y-%m-%d | tr -d '\n'" - wget_fmt_string:"wget -qO- " , d.url , " | grep -o \"[^<]*\" | sed -e 's/<[^>]*>//g' | tr -d '\n'" - wget_res:shell[wget_fmt_string] - if wget_res.exit = -1 do - exit["\n wget failed\n"] # this is weaksauce, but better than nothing, I suppose - else - t:insert title:(wget_res.out) reference:(d.url) created_date:(shell[date_fmt_string].out) into t - end -end - -if args[2] do - # read in a csv, and store in memory as a table - catalogue:load_db[csv_db] - - if args[2] ~ "-l" do - show[catalogue] # listing everything is probs not the most ideal if you've got thousands of entries... - else - url:args[2] - - # add a row to the table stored in memory - row.url:url - row:table row - catalogue:entry[row catalogue] - - # write the updated data to csv - save_db[catalogue csv_db] - end -else - print["\n please pass a url as an argument\n"] -end \ No newline at end of file diff --git a/src/catalogue/catalogue.lil b/src/catalogue/catalogue.lil new file mode 100644 index 0000000..0b68c02 --- /dev/null +++ b/src/catalogue/catalogue.lil @@ -0,0 +1,44 @@ +csv_db:"test.csv" + +on load_db db do + data:read[db] + out:readcsv[data "sss"] +end + +on save_db t db do + out:writecsv[t] + write[db out] +end + +on entry d t do + # where d is a table representing a new row to be added to t, an existing table + date_fmt_string:"date +%Y-%m-%d | tr -d '\n'" + wget_fmt_string:"wget -qO- " , d.url , " | grep -o \"<title>[^<]*\" | sed -e 's/<[^>]*>//g' | tr -d '\n'" + wget_res:shell[wget_fmt_string] + if wget_res.exit = -1 do + exit["\n wget failed\n"] # this is weaksauce, but better than nothing, I suppose + else + t:insert title:(wget_res.out) reference:(d.url) created_date:(shell[date_fmt_string].out) into t + end +end + +if args[2] do + # read in a csv, and store in memory as a table + catalogue:load_db[csv_db] + + if args[2] ~ "-l" do + show[catalogue] # listing everything is probs not the most ideal if you've got thousands of entries... + else + url:args[2] + + # add a row to the table stored in memory + row.url:url + row:table row + catalogue:entry[row catalogue] + + # write the updated data to csv + save_db[catalogue csv_db] + end +else + print["\n please pass a url as an argument\n"] +end \ No newline at end of file diff --git a/src/catalogue/test.csv b/src/catalogue/test.csv new file mode 100644 index 0000000..8217333 --- /dev/null +++ b/src/catalogue/test.csv @@ -0,0 +1,6 @@ +title,reference,created_date +small and nearly silent | pubnix programming playground for friends,https://smallandnearlysilent.com,2022-12-10 +Oatmeal,https://eli.li,2022-12-10 +Ten Forward,https://tenforward.social,2022-12-10 +"DuckDuckGo Privacy, simplified.",https://duckduckgo.com,2022-12-10 +Google,https://google.com,2022-12-11 \ No newline at end of file diff --git a/src/catalogue/tmp.lil b/src/catalogue/tmp.lil new file mode 100644 index 0000000..60fbb53 --- /dev/null +++ b/src/catalogue/tmp.lil @@ -0,0 +1,113 @@ +# lil includes a fun query system as well as language features for both +# functional programming and vector programming -- this invites interesting +# opportunities for applications built around little databases/datasets and +# the manipulation and querying of that data! + +# I'm not 100% certain what I'm going to build with these features, yet, but +# I will use this as a space to explore what lil has to offer. + +# people.name:"Alice","Sam","Thomas","Sara","Walter" +# people.age:25,28,40,34,43 +# people.job:"Chef","Sous Chef","Baker","Front of house","Baker" +# people:table people + +# t:select from people + +# show[t] + +# s:select where ("S%m" parse name) from people + +# show[s] + +# d:select where ("Baker" = job) from people + +# show[d] + +# a:select name where ("Alice" = name) from people + +# show[a] + +# w:update job:"Amazing Chef" where job = "Chef" from people + +# Show[w] + +# l:insert name:"John" job:"Critic" age:32 into people + +# show[l] + +# # save:writecsv[l "sis" ","] # where sis formats the column type, here string integer string -- the other option is boolean. + +# # write["test.csv" save] + +# on cons x y do +# x,y +# end + +# breakfast: insert food:("Eggs","Pancakes","Grapefruit") quantity:(3,4,1) tasty:(1,1,0) into 0 # NOTE, 0 here could also be the name of the variable, so, in this example, breakfast + +# show[breakfast] + +# needle: "apple" +# haystack: ("frog","apple","chicken","toadstool","apple","rice","fish") + +# filter:count needle take haystack +# spread_conforming:sum needle = haystack +# print[filter] +# print[spread_conforming] + + +# spent a good bit of time playing with lil and reviewing the docs. I am now +# totally smitten and excited to use lil. + +# a basic sketch of the data table at the center of the action + +# catalogue.entry_type +# catalogue.parent # not 100% certain how to reference another entry by id +# catalogue.title +# catalogue.body +# catalogue.reference +# catalogue.created_date +# catalogue.updated_date # does this matter? (sorta leaning towards "no") + +# entry_type's +# - link +# - note +# - folder + +# every item can have 1 parent +# a parent is usually a folder + +# data table wrapped in an interactive cli for adding, querying, and viewing +# input data. Input data stored as CSV between sessions + +# NOTE: if input data is stored as CSV is becomes trivial to pipe in and out +# of SQLite in the future...if that'd be desirable + +# NOTE: I'm using just 1 table so that this can be easily exported as CSV... +# if it weren't for that requirement, I'd have multiple tables + +# catalogue:table catalogue + +# alternate syntax to make a table, +# t: insert food:("Eggs","Pancakes","Grapefruit") quantity:(3,4,1) tasty:(1,1,0) into 0 + + +# validate that I can write a table to disk and read it back in +# show[catalogue] +# out:writecsv[catalogue] +# show[out] +# write["test.csv" out] + +# data:read["test.csv"] +# newtable:readcsv[data "sis "] # this column spec. syntax looks hilarious +# show[newtable] +# HUGE SUCCESS! + + +if args[2] do + url:args[2] + print[url] +else + print["\n please pass a url as an argument\n"] +end + diff --git a/src/pokedex.rkt b/src/pokedex.rkt deleted file mode 100644 index 3aebea1..0000000 --- a/src/pokedex.rkt +++ /dev/null @@ -1,59 +0,0 @@ - -#lang racket - -;; let's try to make a pokedex in racket! - -;; first step will be to write a function to query the pokeAPI -;; like so, https://pokeapi.co/api/v2/pokemon/ditto - -;; once there is a function in place that returns data I will -;; have a choice to make -- do I want to always query the API, -;; do I want to cache results, or do I want to create an offline -;; repository of poke-data to search through and sometimes update? - - -;; basic GET request -(require net/url) -(require json) - -; 🤔 -(require slideshow/pict racket/draw) - -;; API URL -(define *POKE-API* "https://pokeapi.co/api/v2/") - - -(define (get-pokemon id) - "queries the api, returns a butt ton of info" - (call/input-url (string->url (~a *POKE-API* "pokemon/" id)) ; ~a, like string-append but appends *either* strings or numbers to a string - get-pure-port - (compose string->jsexpr port->string))) ; QUESTION: could I use read-json here, instead? - -(define (dex-entry id) - "selects the info we need from the api, builds a hash table of the data that will build the entry" - (let ([poke-data (get-pokemon id)]) - (define entry-data (make-hash)) - (hash-set! entry-data "name" (hash-ref poke-data 'name)) - (hash-set! entry-data "id" (hash-ref poke-data 'id)) - (hash-set! entry-data "sprite" (hash-ref (hash-ref poke-data 'sprites) 'front_default)) - (hash-set! entry-data "stats" (hash-ref poke-data 'stats)) - (hash-set! entry-data "types" (hash-ref poke-data 'types)) - entry-data)) - -(define (inspector h) - "display the contents of a hash table for human eyeballs and let the hash table fall back out" - (hash-map h - (lambda (k v) - (if (list? v) (map ; consider making this if a cond and testing for lists/hash tables to recursively dive into nested data - (lambda (x) (inspector x)) v) - (display (~a " key: " k "\nvalue: " v "\n=====\n"))))) - h) - -(inspector (dex-entry 11)) -(inspector (dex-entry "bulbasaur")) - -(define img (hash-ref (dex-entry 11) "sprite")) - -(define display-image (bitmap (make-object bitmap% img))) - -(frame (scale display-image 0.3)) ; this doesn't actually seem to work with URLs/PNGs diff --git a/src/pokedex/pokedex.rkt b/src/pokedex/pokedex.rkt new file mode 100644 index 0000000..3aebea1 --- /dev/null +++ b/src/pokedex/pokedex.rkt @@ -0,0 +1,59 @@ + +#lang racket + +;; let's try to make a pokedex in racket! + +;; first step will be to write a function to query the pokeAPI +;; like so, https://pokeapi.co/api/v2/pokemon/ditto + +;; once there is a function in place that returns data I will +;; have a choice to make -- do I want to always query the API, +;; do I want to cache results, or do I want to create an offline +;; repository of poke-data to search through and sometimes update? + + +;; basic GET request +(require net/url) +(require json) + +; 🤔 +(require slideshow/pict racket/draw) + +;; API URL +(define *POKE-API* "https://pokeapi.co/api/v2/") + + +(define (get-pokemon id) + "queries the api, returns a butt ton of info" + (call/input-url (string->url (~a *POKE-API* "pokemon/" id)) ; ~a, like string-append but appends *either* strings or numbers to a string + get-pure-port + (compose string->jsexpr port->string))) ; QUESTION: could I use read-json here, instead? + +(define (dex-entry id) + "selects the info we need from the api, builds a hash table of the data that will build the entry" + (let ([poke-data (get-pokemon id)]) + (define entry-data (make-hash)) + (hash-set! entry-data "name" (hash-ref poke-data 'name)) + (hash-set! entry-data "id" (hash-ref poke-data 'id)) + (hash-set! entry-data "sprite" (hash-ref (hash-ref poke-data 'sprites) 'front_default)) + (hash-set! entry-data "stats" (hash-ref poke-data 'stats)) + (hash-set! entry-data "types" (hash-ref poke-data 'types)) + entry-data)) + +(define (inspector h) + "display the contents of a hash table for human eyeballs and let the hash table fall back out" + (hash-map h + (lambda (k v) + (if (list? v) (map ; consider making this if a cond and testing for lists/hash tables to recursively dive into nested data + (lambda (x) (inspector x)) v) + (display (~a " key: " k "\nvalue: " v "\n=====\n"))))) + h) + +(inspector (dex-entry 11)) +(inspector (dex-entry "bulbasaur")) + +(define img (hash-ref (dex-entry 11) "sprite")) + +(define display-image (bitmap (make-object bitmap% img))) + +(frame (scale display-image 0.3)) ; this doesn't actually seem to work with URLs/PNGs diff --git a/src/pokedex/tmp.rkt b/src/pokedex/tmp.rkt new file mode 100644 index 0000000..089e066 --- /dev/null +++ b/src/pokedex/tmp.rkt @@ -0,0 +1,63 @@ +#lang racket + +; (require sugar) + +(define test-data '#hash(("id" . 12) + ("name" . "butterfree") + ("sprite" . "https://raw.githubusercontent.com/PokeAPI/sprites/master/sprites/pokemon/12.png") + ("stats" + . (#hasheq((base_stat . 60) + (effort . 0) + (stat . #hasheq((name . "hp") (url . "https://pokeapi.co/api/v2/stat/1/")))) + #hasheq((base_stat . 45) + (effort . 0) + (stat . #hasheq((name . "attack") (url . "https://pokeapi.co/api/v2/stat/2/")))) + #hasheq((base_stat . 50) + (effort . 0) + (stat . #hasheq((name . "defense") (url . "https://pokeapi.co/api/v2/stat/3/")))) + #hasheq((base_stat . 90) + (effort . 2) + (stat . #hasheq((name . "special-attack") (url . "https://pokeapi.co/api/v2/stat/4/")))) + #hasheq((base_stat . 80) + (effort . 1) + (stat . #hasheq((name . "special-defense") (url . "https://pokeapi.co/api/v2/stat/5/")))) + #hasheq((base_stat . 70) + (effort . 0) + (stat . #hasheq((name . "speed") (url . "https://pokeapi.co/api/v2/stat/6/")))))) + ("types" + . (#hasheq((slot . 1) + (type . #hasheq((name . "bug") (url . "https://pokeapi.co/api/v2/type/7/")))) + #hasheq((slot . 2) + (type . #hasheq((name . "flying") (url . "https://pokeapi.co/api/v2/type/3/")))))))) + + +; (define d (dict->list test-data)) ; or...you know...could convert from hash table to list? + + +; (hash? (car (hash-ref test-data "stats"))) ; #t --- BOOM! this gets us a hash table! +; (display (car (hash-ref test-data "stats"))) +; (list? (hash-ref test-data "stats")) ;; #t + +;; "stats" and "types" AREN'T exactly nested hash tables! They're lists, with hash tables embedded in them! +;; SO! I need a way to test for if something is a LIST, and then if it is a LIST I think then I hash map through that! + +;; NEXT STEPS -> https://stackoverflow.com/questions/13504302/racket-scheme-filtering + +; (hash-ref test-data "stats") ; all stats +; (hash-ref (car (hash-ref test-data "stats")) 'base_stat) ; value for a given stat +; (hash-ref (hash-ref (car (hash-ref test-data "stats")) 'stat) 'name) ; name for a given stat + +; [(list? v) (map (lambda (x) (inspector x)) v)] +; [(hash? (car (hash-ref k "stats")) (map (lambda (x) (inspector x)) v))] + +(define (inspector h) + "display the contents of a hash table for human eyeballs and let the hash table fall back out" + (hash-map h + (lambda (k v) + (cond [(list? v) (cond + [(hash? (car (hash-ref k "stats")) (map (lambda (x) (inspector x) v)))] ; this is probably dumb, since it'll require at least another level of nesting... + [map (lambda (x) (inspector x)) v])] + [(display (~a " key: " k "\nvalue: " v "\n=====\n"))]))) + h) + +(inspector test-data) diff --git a/src/test.csv b/src/test.csv deleted file mode 100644 index 8217333..0000000 --- a/src/test.csv +++ /dev/null @@ -1,6 +0,0 @@ -title,reference,created_date -small and nearly silent | pubnix programming playground for friends,https://smallandnearlysilent.com,2022-12-10 -Oatmeal,https://eli.li,2022-12-10 -Ten Forward,https://tenforward.social,2022-12-10 -"DuckDuckGo Privacy, simplified.",https://duckduckgo.com,2022-12-10 -Google,https://google.com,2022-12-11 \ No newline at end of file diff --git a/src/tmp.lil b/src/tmp.lil deleted file mode 100644 index 60fbb53..0000000 --- a/src/tmp.lil +++ /dev/null @@ -1,113 +0,0 @@ -# lil includes a fun query system as well as language features for both -# functional programming and vector programming -- this invites interesting -# opportunities for applications built around little databases/datasets and -# the manipulation and querying of that data! - -# I'm not 100% certain what I'm going to build with these features, yet, but -# I will use this as a space to explore what lil has to offer. - -# people.name:"Alice","Sam","Thomas","Sara","Walter" -# people.age:25,28,40,34,43 -# people.job:"Chef","Sous Chef","Baker","Front of house","Baker" -# people:table people - -# t:select from people - -# show[t] - -# s:select where ("S%m" parse name) from people - -# show[s] - -# d:select where ("Baker" = job) from people - -# show[d] - -# a:select name where ("Alice" = name) from people - -# show[a] - -# w:update job:"Amazing Chef" where job = "Chef" from people - -# Show[w] - -# l:insert name:"John" job:"Critic" age:32 into people - -# show[l] - -# # save:writecsv[l "sis" ","] # where sis formats the column type, here string integer string -- the other option is boolean. - -# # write["test.csv" save] - -# on cons x y do -# x,y -# end - -# breakfast: insert food:("Eggs","Pancakes","Grapefruit") quantity:(3,4,1) tasty:(1,1,0) into 0 # NOTE, 0 here could also be the name of the variable, so, in this example, breakfast - -# show[breakfast] - -# needle: "apple" -# haystack: ("frog","apple","chicken","toadstool","apple","rice","fish") - -# filter:count needle take haystack -# spread_conforming:sum needle = haystack -# print[filter] -# print[spread_conforming] - - -# spent a good bit of time playing with lil and reviewing the docs. I am now -# totally smitten and excited to use lil. - -# a basic sketch of the data table at the center of the action - -# catalogue.entry_type -# catalogue.parent # not 100% certain how to reference another entry by id -# catalogue.title -# catalogue.body -# catalogue.reference -# catalogue.created_date -# catalogue.updated_date # does this matter? (sorta leaning towards "no") - -# entry_type's -# - link -# - note -# - folder - -# every item can have 1 parent -# a parent is usually a folder - -# data table wrapped in an interactive cli for adding, querying, and viewing -# input data. Input data stored as CSV between sessions - -# NOTE: if input data is stored as CSV is becomes trivial to pipe in and out -# of SQLite in the future...if that'd be desirable - -# NOTE: I'm using just 1 table so that this can be easily exported as CSV... -# if it weren't for that requirement, I'd have multiple tables - -# catalogue:table catalogue - -# alternate syntax to make a table, -# t: insert food:("Eggs","Pancakes","Grapefruit") quantity:(3,4,1) tasty:(1,1,0) into 0 - - -# validate that I can write a table to disk and read it back in -# show[catalogue] -# out:writecsv[catalogue] -# show[out] -# write["test.csv" out] - -# data:read["test.csv"] -# newtable:readcsv[data "sis "] # this column spec. syntax looks hilarious -# show[newtable] -# HUGE SUCCESS! - - -if args[2] do - url:args[2] - print[url] -else - print["\n please pass a url as an argument\n"] -end - diff --git a/src/tmp.rkt b/src/tmp.rkt deleted file mode 100644 index 089e066..0000000 --- a/src/tmp.rkt +++ /dev/null @@ -1,63 +0,0 @@ -#lang racket - -; (require sugar) - -(define test-data '#hash(("id" . 12) - ("name" . "butterfree") - ("sprite" . "https://raw.githubusercontent.com/PokeAPI/sprites/master/sprites/pokemon/12.png") - ("stats" - . (#hasheq((base_stat . 60) - (effort . 0) - (stat . #hasheq((name . "hp") (url . "https://pokeapi.co/api/v2/stat/1/")))) - #hasheq((base_stat . 45) - (effort . 0) - (stat . #hasheq((name . "attack") (url . "https://pokeapi.co/api/v2/stat/2/")))) - #hasheq((base_stat . 50) - (effort . 0) - (stat . #hasheq((name . "defense") (url . "https://pokeapi.co/api/v2/stat/3/")))) - #hasheq((base_stat . 90) - (effort . 2) - (stat . #hasheq((name . "special-attack") (url . "https://pokeapi.co/api/v2/stat/4/")))) - #hasheq((base_stat . 80) - (effort . 1) - (stat . #hasheq((name . "special-defense") (url . "https://pokeapi.co/api/v2/stat/5/")))) - #hasheq((base_stat . 70) - (effort . 0) - (stat . #hasheq((name . "speed") (url . "https://pokeapi.co/api/v2/stat/6/")))))) - ("types" - . (#hasheq((slot . 1) - (type . #hasheq((name . "bug") (url . "https://pokeapi.co/api/v2/type/7/")))) - #hasheq((slot . 2) - (type . #hasheq((name . "flying") (url . "https://pokeapi.co/api/v2/type/3/")))))))) - - -; (define d (dict->list test-data)) ; or...you know...could convert from hash table to list? - - -; (hash? (car (hash-ref test-data "stats"))) ; #t --- BOOM! this gets us a hash table! -; (display (car (hash-ref test-data "stats"))) -; (list? (hash-ref test-data "stats")) ;; #t - -;; "stats" and "types" AREN'T exactly nested hash tables! They're lists, with hash tables embedded in them! -;; SO! I need a way to test for if something is a LIST, and then if it is a LIST I think then I hash map through that! - -;; NEXT STEPS -> https://stackoverflow.com/questions/13504302/racket-scheme-filtering - -; (hash-ref test-data "stats") ; all stats -; (hash-ref (car (hash-ref test-data "stats")) 'base_stat) ; value for a given stat -; (hash-ref (hash-ref (car (hash-ref test-data "stats")) 'stat) 'name) ; name for a given stat - -; [(list? v) (map (lambda (x) (inspector x)) v)] -; [(hash? (car (hash-ref k "stats")) (map (lambda (x) (inspector x)) v))] - -(define (inspector h) - "display the contents of a hash table for human eyeballs and let the hash table fall back out" - (hash-map h - (lambda (k v) - (cond [(list? v) (cond - [(hash? (car (hash-ref k "stats")) (map (lambda (x) (inspector x) v)))] ; this is probably dumb, since it'll require at least another level of nesting... - [map (lambda (x) (inspector x)) v])] - [(display (~a " key: " k "\nvalue: " v "\n=====\n"))]))) - h) - -(inspector test-data) -- cgit 1.4.1-2-gfad0