Is there a component to handle dictionaries without coding?

I know they can be coded but I’m too shamefully lazy to learn to do it properly.
I mean, just two lists of key value pairs and tada!

Two panels?

// Rolf

1 Like

For what it’s worth here are two implementations (my own code) of the basic web cookie for JavaScript and PHP. Of course, each language has native support for “objects”, AKA “associative arrays”, which in themselves are nested name/value pairs:

JavaScript:

/********* BEGIN: jt_.Cookie *******/
// NOTE: jt_.Cookie is based largely on code originally from Netscape
jt_.Cookie = function(document, name, hours, path, domain, secure) {
	this.$document = document;
	this.$name = name;
	this.$hours = hours;
	this.$path = path;
	this.$domain = domain;
	this.$secure = secure;
}

jt_.Cookie.prototype.store = function() {
	var cookieval = "";
	for(var prop in this) {
		if ((prop.charAt(0) == '$') || ((typeof this[prop]) == 'function')) continue;
		if (cookieval != "") cookieval += '&';
		cookieval += prop + ':' + escape(this[prop]);
	}
	var cookie = this.$name + '=' + cookieval;
	if (this.$hours) {
		var expiration = new Date((new Date()).getTime() + this.$hours*3600000);
		cookie += '; expires=' + expiration.toGMTString();
	}
	if (this.$path) cookie += '; path=' + this.$path;
	if (this.$domain) cookie += '; domain=' + this.$domain;
	if (this.$secure) cookie += '; secure';
	this.$document.cookie = cookie;
}

jt_.Cookie.prototype.load = function() {
	var allcookies = this.$document.cookie;
	if (allcookies == "") return false;

	var start = allcookies.indexOf(this.$name + '=');
	if (start == -1) return false;
	start += this.$name.length + 1;
	var end = allcookies.indexOf(';', start);
	if (end == -1) end = allcookies.length;
	var cookieval = allcookies.substring(start, end);

	var a = cookieval.split('&');
	for(var i=0; i < a.length; i++) a[i] = a[i].split(':');
	for(var i = 0; i < a.length; i++) {this[a[i][0]] = unescape(a[i][1])};
	return true;
}

jt_.Cookie.prototype.setAsObj = function(obj) {
	var old = this.getAsObj();
	for(var prop in old) delete this[old[prop]];
	for(var prop in obj) this[prop] = obj[prop];
}

jt_.Cookie.prototype.getAsObj = function() {
	var obj = {};
	for(var prop in this) {
		if ((prop.charAt(0) == '$') || ((typeof this[prop]) == 'function')) continue;
		obj[prop] = this[prop];
	}
	return obj;
}

jt_.Cookie.prototype.remove = function() {
	var cookie = this.$name + '=';
	if (this.$path) cookie += '; path=' + this.$path;
	if (this.$domain) cookie += '; domain=' + this.$domain;
	cookie += '; expires=Fri, 02-Jan-1970 00:00:00 GMT';
	this.$document.cookie = cookie;
} /********* END: jt_.Cookie *******/

PHP:

<?php
/**
 * class 'jt_Cookie' - implements well formed cookie of encoded name/value pairs
 *
 * matches JavaScript equivalent 'jt_Cookie.js' (original Netscape code)
 * example cookie value: position:4&location:0&endPoint:1164913
 */
class jt_Cookie {
  var $name;
  var $cookieArray;

  function jt_Cookie($cookieName) {
    // CONSTRUCTOR - read and parse '$cookieName'
    $this->name = $cookieName;
    $this->cookieArray = array();
    $nameValues = explode("&", $_COOKIE{$cookieName});
    for ($i=0; $i < count($nameValues); $i++) {
      $nameVal = explode(":", $nameValues[$i]);
      $this->cookieArray[$nameVal[0]] = $nameVal[1];
      }
    }

  function get($field) {
    // return value of '$field'
    return rawurldecode($this->cookieArray[$field]);
    }

  function put($field, $value) {
    // set '$field' to '$value'
    $this->cookieArray[$field] = rawurlencode($value);
    }

  function save($expireDate, $path="/", $domain="", $secure=false) {
    $cookieVal = "";
    foreach ($this->cookieArray as $key => $value)
      if ($key) $cookieVal .= ($cookieVal ? '&' : '') . $key . ':' . $value;

    // NOTE: the following is used because PHP Ver 4.3 doesn't support 'setrawcookie()'
	$sHeader = "Set-Cookie: $this->name=$cookieVal";
    $sHeader .= "; expires=" . gmstrftime("%A, %d-%b-%Y %H:%M:%S GMT", $expireDate);
    if ($path) $sHeader .= "; Path=$path";
    if ($domain) $sHeader .= "; Domain=$domain";
    if ($secure) $sHeader .= "; Secure";
    $sHeader .= "; Version=\"1\"";
	header($sHeader,false);
    }

  } // class jt_Cookie
?>
1 Like

Way above my skill level.

Will test if it can be “this” easy. Thx!

Maybe Python dictionaries are a better match. I use these a lot for structuring/interfacing/writing/reading data (as they are very simple and map onto to e.g. JSON natively with the json module) . Here’s a quick example:

191012_GHPython_Dicts_00.gh (13.2 KB)

3 Likes

Error in R5:

  1. Solution exception:cannot import treehelpers from ghpythonlib

Yes.

2 Likes

Thanks! Will try it asap!

1 Like

:man_facepalming:

Here you Joseph, just for you. I think this might even work in V4:

191012_GHPython_Dicts_V5_Approved_00.gh (23.8 KB)

3 Likes