simplejson buglet?

Posted on
Tue Jun 16, 2020 7:46 am
DaveL17 offline
User avatar
Posts: 6751
Joined: Aug 20, 2013
Location: Chicago, IL, USA

simplejson buglet?

This is a curious one. The following statements run error-free in a (non-Indigo) terminal session:
Code: Select all
print(simplejson.dumps(entry_tp, sort_keys=True, indent=2 * ' '))
print(simplejson.dumps(entry_tp, sort_keys=True, indent='  '))
print(simplejson.dumps(entry_tp, sort_keys=True, indent=2'))

All but the last one results in an error in an Indigo scripting window (for example):
Code: Select all
   Script Error                    embedded script: can't multiply sequence by non-int of type 'str'
   Script Error                    Exception Traceback (most recent call shown last):

     embedded script, line 95, at top level
     File "/Library/Application Support/Perceptive Automation/Indigo 7.4/IndigoPluginHost.app/Contents/Resources/PlugIns/simplejson/__init__.py", line 237, in dumps
       **kw).encode(obj)
     File "/Library/Application Support/Perceptive Automation/Indigo 7.4/IndigoPluginHost.app/Contents/Resources/PlugIns/simplejson/encoder.py", line 202, in encode
       chunks = list(chunks)
     File "/Library/Application Support/Perceptive Automation/Indigo 7.4/IndigoPluginHost.app/Contents/Resources/PlugIns/simplejson/encoder.py", line 426, in _iterencode
       for chunk in _iterencode_dict(o, _current_indent_level):
     File "/Library/Application Support/Perceptive Automation/Indigo 7.4/IndigoPluginHost.app/Contents/Resources/PlugIns/simplejson/encoder.py", line 344, in _iterencode_dict
       newline_indent = '\n' + (' ' * (_indent * _current_indent_level))
TypeError: can't multiply sequence by non-int of type 'str'

This syntax runs error-free in Indigo:
Code: Select all
indigo.server.log(simplejson.dumps(entry_tp, sort_keys=True, indent=2))

I came here to drink milk and kick ass....and I've just finished my milk.

[My Plugins] - [My Forums]

Posted on
Tue Jun 16, 2020 8:15 am
FlyingDiver offline
User avatar
Posts: 7211
Joined: Jun 07, 2014
Location: Southwest Florida, USA

Re: simplejson buglet?

Have you tried using json instead of simplejson?

joe (aka FlyingDiver)
my plugins: http://forums.indigodomo.com/viewforum.php?f=177

Posted on
Tue Jun 16, 2020 8:49 am
DaveL17 offline
User avatar
Posts: 6751
Joined: Aug 20, 2013
Location: Chicago, IL, USA

Re: simplejson buglet?

Not in an attempt to control for the errors. AFAIK, Indigo ships with support for simplejson and I can't for the life of me remember why. A quick Google speaks of them being largely the same, with simplejson having support for more Python versions.

I came here to drink milk and kick ass....and I've just finished my milk.

[My Plugins] - [My Forums]

Posted on
Tue Jun 16, 2020 9:06 am
jay (support) offline
Site Admin
User avatar
Posts: 18212
Joined: Mar 19, 2008
Location: Austin, Texas

Re: simplejson buglet?

The built-in json library was added in Python 2.6, but we shipped while Python 2.5 was the latest install with macOS, so we shipped simplejson. Point of fact: the built-in json module is built from simplejson, so the majority of the library calls are identical. Here's what we recommended devs do when 2.6 started shipping on macOS:

Code: Select all
try:
    import json
except:
    import simplejson as json


That way you get the best version available.

As for your specific case, I suspect that the simplejson library we ship is somewhat different (older) than the version of simplejson that Python from the command-line is finding, but Indigo's ordering of path searches finds the one we build-in before the one that Python directly finds.

I personally don't recall ever seeing the technique you post in the first line before (multiplying a string) - again, you learn something new every day...

Jay (Indigo Support)
Twitter | Facebook | LinkedIn

Posted on
Tue Jun 16, 2020 9:08 am
DaveL17 offline
User avatar
Posts: 6751
Joined: Aug 20, 2013
Location: Chicago, IL, USA

Re: simplejson buglet?

Thanks Jay -- Jon just suggested that I compare the versions and there is a difference.

Indigo: 2.0.9
Terminal: 3.6.5

Your suggestion for the import structure is a good one and that's what we'll do.

I came here to drink milk and kick ass....and I've just finished my milk.

[My Plugins] - [My Forums]

Posted on
Tue Jun 16, 2020 1:54 pm
RogueProeliator offline
User avatar
Posts: 2501
Joined: Nov 13, 2012
Location: Baton Rouge, LA

Re: simplejson buglet?

I personally don't recall ever seeing the technique you post in the first line before (multiplying a string) - again, you learn something new every day...

Me neither, was wondering what kind of voodoo Dave was up to...

Posted on
Tue Jun 16, 2020 1:59 pm
DaveL17 offline
User avatar
Posts: 6751
Joined: Aug 20, 2013
Location: Chicago, IL, USA

Re: simplejson buglet?

This is something I do often.
Code: Select all
print(80 * "=")

>>> ================================================================================

Interesting that I knew something that y'all didn't know...

I came here to drink milk and kick ass....and I've just finished my milk.

[My Plugins] - [My Forums]

Posted on
Tue Jun 16, 2020 2:04 pm
jay (support) offline
Site Admin
User avatar
Posts: 18212
Joined: Mar 19, 2008
Location: Austin, Texas

Re: simplejson buglet?

Just for grins I tried it in Python 3 and it still works. Amazing! :lol:

Jay (Indigo Support)
Twitter | Facebook | LinkedIn

Posted on
Tue Jun 16, 2020 2:23 pm
DaveL17 offline
User avatar
Posts: 6751
Joined: Aug 20, 2013
Location: Chicago, IL, USA

Re: simplejson buglet?

I use this script as a quickie tool for documenting code:
Code: Select all
#! /usr/bin/env python
# -*- coding: utf-8 -*-

foo = 'Hello world.'

print(u'# {0:=^77}'.format(''))
print(u'# {0:=^77}'.format(u" {0} ".format(foo)))
print(u'<!{0:-^76}>'.format(u" {0} ".format(foo)))

Code: Select all
# =============================================================================
# =============================== Hello world. ================================
<!------------------------------- Hello world. ------------------------------->

and then just copy/paste the relevant bit.

I came here to drink milk and kick ass....and I've just finished my milk.

[My Plugins] - [My Forums]

Page 1 of 1

Who is online

Users browsing this forum: No registered users and 2 guests