Author Topic: Database Code Challenge(s)  (Read 24443 times)

Offline AIR

  • BASIC Developer
  • Posts: 932
  • Coder
Re: Database Code Challenge(s)
« Reply #60 on: November 22, 2018, 10:06:19 AM »
I'm curious to see what you mean.  I've attached a json version of the yaml data file I used....

Here is some demo code to read JSON file into map and display the content on screen...


Thanks, jalih, that's real interesting.

Does 8th support maintaining the order of the data when you output it?


Offline jalih

  • Advocate
  • Posts: 111
Re: Database Code Challenge(s)
« Reply #61 on: November 22, 2018, 11:01:40 AM »
Does 8th support maintaining the order of the data when you output it?

8th seems to store the data in different order, so iterating map directly changes order of the data outputted. I guess I would need to iterate the map keys in correct order myself.

Offline John

  • Forum Support / SB Dev
  • Posts: 3598
    • ScriptBasic Open Source Project
Re: Database Code Challenge(s)
« Reply #62 on: November 22, 2018, 11:18:14 AM »
Maybe a future code challenge could be a JSON parser.

Offline AIR

  • BASIC Developer
  • Posts: 932
  • Coder
Re: Database Code Challenge(s)
« Reply #63 on: November 22, 2018, 12:20:15 PM »
Does 8th support maintaining the order of the data when you output it?

8th seems to store the data in different order, so iterating map directly changes order of the data outputted. I guess I would need to iterate the map keys in correct order myself.

Most hash/map implementations don't maintain order, for efficiency.  In Python, for example, the Dict object doesn't maintain order. But the OrderedDict does while being less efficient. Other languages offer this option, but not all.

Offline John

  • Forum Support / SB Dev
  • Posts: 3598
    • ScriptBasic Open Source Project
Re: Database Code Challenge(s)
« Reply #64 on: November 22, 2018, 12:27:35 PM »
Associative arrays seems the only way I know of taming these hash lists.

Offline AIR

  • BASIC Developer
  • Posts: 932
  • Coder
Re: Database Code Challenge(s)
« Reply #65 on: November 22, 2018, 01:28:10 PM »
Associative arrays seems the only way I know of taming these hash lists.

Same thing, different names.  The question is does it maintain order, or do you have to handle that programatically?

Offline John

  • Forum Support / SB Dev
  • Posts: 3598
    • ScriptBasic Open Source Project
Re: Database Code Challenge(s)
« Reply #66 on: November 22, 2018, 01:48:51 PM »
In SB it's about creating freeform structures that can contain any of the data types SB supports. LBOUND/UBOUND are key functions to keep it all together.

No matter how complex your array structure gets, you can iterate though it using standard indexing methods.

Arrays in ScriptBasic

« Last Edit: November 22, 2018, 02:55:23 PM by John »

Offline AIR

  • BASIC Developer
  • Posts: 932
  • Coder
Re: Database Code Challenge(s)
« Reply #67 on: November 22, 2018, 02:59:31 PM »
In SB it's about creating freeform structures that can contain any of the data types SB supports. LBOUND/UBOUND are key functions to keep it all together.

No matter how complex your array structure gets, you can iterate though it using standard indexing methods.


So....you have to handle maintaining the original structure programmatically.  There's nothing bad about that, why not just say that instead of the marketing pitch?

Anyway, we've derailed the original topic enough...


Offline John

  • Forum Support / SB Dev
  • Posts: 3598
    • ScriptBasic Open Source Project
Re: Database Code Challenge(s)
« Reply #68 on: November 22, 2018, 03:07:06 PM »
Quote
Anyway, we've derailed the original topic enough

Just chatting at the water cooler waiting for Alysson to return. I see this challenge as extended. How about you getting us going on something else to keep things moving?

There are no sales pitching with open source. It's called advocating for the project.
« Last Edit: November 22, 2018, 04:05:09 PM by John »

Offline AIR

  • BASIC Developer
  • Posts: 932
  • Coder
Re: Database Code Challenge(s)
« Reply #69 on: November 22, 2018, 05:33:54 PM »
How about you getting us going on something else to keep things moving?


I think your json suggestion could work.  With the stipulation that it be done in native code (meaning no external libs, AND if the language you use already has support for it you don't that capability and instead roll your own).  Otherwise it's not much of a challenge.

Offline John

  • Forum Support / SB Dev
  • Posts: 3598
    • ScriptBasic Open Source Project
Re: Database Code Challenge(s)
« Reply #70 on: November 22, 2018, 06:16:14 PM »
Sounds great!

I didn't use SB LIKE for the challenge EXTRACT. SB needs an easy to use JSON function.

http://www.json.org

Would rewriting the jsmn library in C BASIC count?  :)

Please kick off the new challenge thread when you have time.
« Last Edit: November 22, 2018, 06:31:59 PM by John »

Offline AlyssonR

  • Advocate
  • Posts: 131
Re: Database Code Challenge(s)
« Reply #71 on: December 23, 2018, 01:14:26 AM »
Hi all,

I've taken a quick peek at the thread - and it looks like everyone's been having fun with it.

I've been on extended domestic maintenance (i.e. fixing stuff around the house AND fixing myself), and will likely only look in on occasion for a while more :( The trouble is that I have far too much on the go right now with harware, software AND hard/software projects simmering as well as trying to keep abreast of everything that needs fixing around the house.

Offline John

  • Forum Support / SB Dev
  • Posts: 3598
    • ScriptBasic Open Source Project
Re: Database Code Challenge(s)
« Reply #72 on: December 23, 2018, 12:22:48 PM »
You sound busier than Santa's elves.  :)

Offline AIR

  • BASIC Developer
  • Posts: 932
  • Coder
Re: Database Code Challenge(s)
« Reply #73 on: March 04, 2019, 02:24:19 AM »
I wanted to give this a try using GO, here is what I have:

Code: Go
  1. package main
  2.  
  3. import (
  4.         "fmt"
  5.         "io/ioutil"
  6.         "log"
  7.         "strings"
  8.  
  9.         "gopkg.in/yaml.v2"
  10. )
  11.  
  12. func getRoot(obj interface{}) map[interface{}]interface{} {
  13.         return obj.(map[interface{}]interface{})
  14. }
  15. func getSection(obj interface{}, field string) map[interface{}]interface{} {
  16.         dict := obj.(map[interface{}]interface{})[field].(map[interface{}]interface{})
  17.         return dict
  18. }
  19.  
  20. func dump(obj map[interface{}]interface{}, indent ...string) {
  21.         for key, val := range obj {
  22.                 if key == "Address" {
  23.                         val = strings.ReplaceAll(val.(string), "\n", "\n\t\t   ")
  24.                 }
  25.                 if indent != nil {
  26.                         fmt.Println(indent[0], "\t", key, "=", val)
  27.                 } else {
  28.                         fmt.Println("\t", key, "=", val)
  29.                 }
  30.         }
  31. }
  32.  
  33. func getArray(obj interface{}) []interface{} {
  34.         dict := obj.([]interface{})
  35.         return dict
  36. }
  37.  
  38. func dumpArray(obj interface{}) {
  39.         for _, field := range obj.([]interface{}) {
  40.                 switch t := field.(type) {
  41.                 case string:
  42.                         fmt.Println("\t\t", t)
  43.                 case map[interface{}]interface{}:
  44.                         set := []interface{}{t["var"], t["colour"], t["Matrix"]}
  45.                         // x := t["var"]
  46.                         for _, x := range set {
  47.                                 if x != nil {
  48.                                         fmt.Println("\t\t", x)
  49.                                 }
  50.                         }
  51.                 }
  52.         }
  53. }
  54.  
  55. func dumpList(obj interface{}) {
  56.         for _, x := range obj.([]interface{}) {
  57.                 dump(x.(map[interface{}]interface{}))
  58.                 println()
  59.         }
  60. }
  61.  
  62. type Rec interface{}
  63.  
  64. func main() {
  65.         var rec Rec
  66.         sep := strings.Repeat("-", 40)
  67.         data, err := ioutil.ReadFile("test.yml")
  68.         if err != nil {
  69.                 log.Fatal(err)
  70.         }
  71.  
  72.         err = yaml.Unmarshal(data, &rec)
  73.         if err != nil {
  74.                 log.Fatal(err)
  75.         }
  76.  
  77.         root := getRoot(rec)
  78.         recordTitle := root["RecordTitle"]
  79.         entryNumber := root["Entry No"]
  80.         recordType := root["RecordType"]
  81.         lastSavedTime := root["LastSavedTime"]
  82.         previousVersion := root["Previous Version"]
  83.         Species := getRoot(root["Species"])
  84.         Minerals := getRoot(Species)["Minerals"]
  85.  
  86.         location := getSection(rec, "Location")
  87.         collectionData := getSection(rec, "Collection Data")
  88.         acquisitionData := getSection(rec, "Acquisition Data")
  89.         analyticalData := getSection(rec, "Analytical Data")
  90.         accessionData := getSection(rec, "Accession Data")
  91.         imageData := getSection(rec, "Image Data")
  92.  
  93.         fmt.Println(sep)
  94.         fmt.Println("RecordTitle =", recordTitle)
  95.         fmt.Println("Entry No =", entryNumber)
  96.         fmt.Println(sep)
  97.  
  98.         fmt.Println("Species\n\tMinerals")
  99.         dumpArray(Minerals)
  100.         fmt.Println(sep)
  101.  
  102.         fmt.Println("Location")
  103.         dump(location)
  104.         fmt.Println(sep)
  105.  
  106.         fmt.Println("Collection Data")
  107.         dump(collectionData)
  108.         fmt.Println(sep)
  109.  
  110.         fmt.Println("Acquisition Data")
  111.         dump(acquisitionData)
  112.         fmt.Println(sep)
  113.  
  114.         fmt.Println("Analytical Data")
  115.         fmt.Println("\tCommentary =", analyticalData["Commentary"])
  116.  
  117.         zeunerite := getRoot(analyticalData["Zeunerite"])
  118.         fmt.Println("\n\tZeunerite")
  119.         dump(zeunerite, "\t")
  120.  
  121.         zippeite := getRoot(analyticalData["Zippeite"])
  122.         fmt.Println("\n\tZippeite")
  123.         dump(zippeite, "\t")
  124.         fmt.Println(sep)
  125.  
  126.         fmt.Println("Accension Data")
  127.         dump(accessionData)
  128.         fmt.Println(sep)
  129.  
  130.         fmt.Println("Image Data")
  131.         fmt.Println("\tMugshot =", imageData["Mugshot"], "\n")
  132.         images := imageData["Images"]
  133.         dumpList(images)
  134.         fmt.Println(sep)
  135.  
  136.         fmt.Println("DB Info")
  137.         fmt.Println("\tRecordType =", recordType)
  138.         fmt.Println("\tLastSavedTime =", lastSavedTime)
  139.         fmt.Println("\tPrevious Version =", previousVersion)
  140.         fmt.Println(sep)
  141. }
  142.  

And here is the output:

----------------------------------------
RecordTitle = Mineral Specimen Details
Entry No = ARC-99-000021
----------------------------------------
Species
   Minerals
       Zeunerite
       Rutherfordine
       Zippeite
       Chalcopyrite
       [Blister Copper Quartz Agate]
       [White Opal Transparent map[var:[Common Opal]]]
       Quartz
----------------------------------------
Location
    Address = South Terras Mine
         St Stephen in Brannel
         St Austell
         Cornwall, UK

    Grid Reference = SW 935 524
    GPS Coordinates = 50.33444,-4.90194
----------------------------------------
Collection Data
    collected by = Self collected
    Collection Date = 1979
----------------------------------------
Acquisition Data
    From = Self collected
    Field Ref No = F20180623-STUM-001
    Temp Ref No = TMP 0 0099
    Acquired Date = 14 Jul 1998
----------------------------------------
Analytical Data
   Commentary = 4 species

   Zeunerite
        .Report = AN-201808-14-01
        .Ref_Number = ZZ8088
        .comment = Colour and crystal form are typical for this location.
        .date = 14 Aug 2018

   Zippeite
        .comment = Colour is diagnostic for this location
        .date = 14 Aug 2018
        .Report = AN-201808-14-02
        .Ref_Number = ZZ9099
----------------------------------------
Accension Data
    Accession Date = 21 Sept 2000
    Sub-collection = Main
    Accession No = ZZ9099
----------------------------------------
Image Data
   Mugshot = img0001.jpg

    .filename = img0001.jpg
    .Title = Whole Specimen
    .TakenDate = 11 Nov 2018
    .Width = 11.5mm
    .camera = Fuji FinePix L55

    .filename = img0002.jpg
    .Title = Zeunerite & Chalcopyrite
    .TakenDate = 11 Nov 2018
    .FOV = 1.1mm
    .Camera = CL 1600 USB MicroCam

----------------------------------------
DB Info
   RecordType = 1
   LastSavedTime = 13/11/2018 13:18:37
   Previous Version = 13/11/2018 12:12:47
----------------------------------------


I've attached Linux and Windows binaries (scanned with VirusTotal, clean) cross-compiled on my Mac.

AIR.

Offline John

  • Forum Support / SB Dev
  • Posts: 3598
    • ScriptBasic Open Source Project
Re: Database Code Challenge(s)
« Reply #74 on: March 04, 2019, 07:34:09 AM »
GO AIR!  :)