Menu
  • Use Cases
  • Stacks
  • Stackbuilder
  • MyStacks
  • Support
  • About Us
  • Contact
  • Use Cases
  • Stacks
  • Stackbuilder
  • MyStacks
  • Support
  • About Us
  • Contact
  • View Cart

More Python programming in Interstacks

Please also refer to the support documents “Make a software block in My Blocks” and “Basic Python programming in Interstacks”. Interstacks software blocks are made using the Python scripting language. Python is very powerful and yet easy to learn. We will make no attempt here to be a Python programming manual. There are many online resources that serve that purpose. At best, the support documentation serves as a quick overview reference guide.

Stackbuilder
The Stackbuilder authoring tool (IDE) supports both conventional synchronous programming and visual asynchronous data flow. It is a hybrid development environment that allows for the creation of powerful inventions that mix conventional, linear, Python software with a visual asynchronous data flow (messages flow asynchronously over channels between blocks). The same messages that flow within a stack, can flow between stacks and web sites. By using this asynchronous message flow architecture, distributed systems can be scaled incrementally and naturally. The hierarchical component architecture enables systems to be generative (composeable) in a powerful and fun way.

Scope
Each software block is in its own namespace. If you want to share information between software blocks, you must send messages between them. You can use globals within a software block to share data between message handlers inside that block. If you want to share functions across software blocks, you must create a library and then add it to every software block that wants to use the functions.

Properties in software blocks
You can expose properties internal to a software block by entering that property name in the property editor in the “Exposed As” column. You can then use the following functions to access them.

I.setprop(‘foo’, value) # set the property of the current  block
I.getprop('foo')  # get the property foo of the current block
I.listprop()   # lists available properties

To send a set property message to a set prop terminal of another block:

I.sendmessage(3, ('x',1)) # sets property x to 1.

Race conditions
When programming an asynchronous message flow, “race conditions” can occur due to the non-deterministic delivery in time of messages, and hence result in unexpected behaviors. Many times the message flow can be composed in a different way to avoid this. Sometimes a synchronizing or transactional approach can be used. Creating an input handler for a block that receives a transaction complete message, then using I.getnextmessage in the handler that originated the message is another technique. A third method is to reduce the number of input terminals for a block by having an input terminal that receives a list that requires parsing e.g. [command, data1, data2, data3]. Each action the block performs would be determined by the value ‘command’ in the input message list.

Base – Stack Ready
Sometimes a message flow needs to be kick started when the stack is powered up. There is an output terminal on the Base hardware block called “Stack Ready”. It sends a message one time, as soon as the stack is powered up and running.

Time
I.timer() # returns system time in seconds.
I.delay( delay_time_in_seconds) # delays for time amount

A third type of handler, called an “Interval Handler”, can be added to a software block.
I.interval is a global variable (to the software block only) in seconds, that specifies when the handler will be called.
It is often initialized in the “Startup Handler” but can be changed anywhere, including from within the interval handler
itself. I.interval can be set to None to disable calling of the handler.

Binary Values
Interstacks uses the following form for binary values:
Binary(‘\xfeQ’) # two bytes. hex FE and ‘Q’
so can do: I.sendmessage(2, Binary(‘\xfeQ’))
Also note that in Python: << left shift. x << 2. double shift left.

Math Library
Interstacks supports the standard Python math library.
For example:
import math
x = math.sin(40) # sine

Random Number
x = I.random() # will return random number
x = I.random(min, max) # will return random number between min and max

Types and type conversion
Type bool can be True or False.
Python also uses None to mean nothing.

int(’42’) # convert string to int
int(3.141) # converts float to integer
str(42) # convert int to string. converts anything to string
ord(‘s’) # returns 115
chr(115) # returns ‘s’
float(‘1.5’) # returns 1.5
hex(85) # returns string ‘0x55’

if you must, you can type check via:
if IS_A_LIST(a):
if IS_A_STRING(a):
if IS_A_BLOB(a): # meaning binary.
if IS_A_FRAGMENT(a):
if IS_AN_EFORM(a):
if IS_A_UFORM(a):
if IS_A_UUID(a):
if IS_AN_ERROR(a):

Dictionaries
No reliable ordering. keys are mapped to values.
D = {‘food’: ‘spam’, ‘quantity’:4, ‘color’:’pink’} D[‘food’] is ‘spam’
D = {} D[‘name’] = ‘bob’ D[‘job’] = ‘dev’ D[‘age’] = 40 or dict(name=’Bob’, job=’dev’, age= 40)
del D[‘name’] deletes.
You can nest lists in dictionary values. and .append .pop (del) still work
list(D.keys()) returns list of all the keys. len(D) number of stored entries.
for key in D: # iterate through each key
do something.

Classes

class Worker:
     def __init__(self, name, pay):
          self.name = name
          self.pay = pay
     def lastName(self):
          return self.name.split()[-1]  # split string on blanks
     def giveRaise(self, percent):
          self.pay *= (1.0 + percent)

Then can say: bob = Worker(‘bob smith’, 50000) bob.lastName bob.giveRaise(.10)

UUID class
A UUID is a large, unique number. Universal, unique, ID.
It is typically used internally with data storage.

new_uuid = vsmf.UUID() # automatically generate a new UUID
You can say:
I.sendmessage(2, vsmf.UUID(‘~017a1c781a515511e3864ab6cb910dc55e’))
and also: strtosend = current_uuid.toString(myuuid)

Python Libraries
Interstacks does not directly support the importing of many Python libraries. Interstacks software block namespaces are separate. They impose a somewhat transactional structure using asynchronous message queues between blocks. In addition, the Python virtual machine that runs inside a stack does not support conventional operating system functions.

Python Built-in modules for Interstacks
Python standard:
array, cmath, math
_struct, operator
_collections, itertools,
strop, _weakref, binascii,
_json, _heapq
_sre, _codecs,
_symtable, _functools,
marshal,_ast,sys,exceptions

copy_reg
functools
encodings
randbytes
codecs
encodings.hex_codec
encodings.utf8
encodings.utf16
encodings.aliases
abc
struct
sre_constants
sre_compile
sre_parse
re
json
_abcoll
UserDict
copy
types
weakref
_weakrefset

Refer to documentation on many other topics at interstacks.com/knowledge-base/.
Please email info@interstacks.com with any questions.

Popular Articles

  • MyStacks Cloud Dashboard
  • Internet communication with HTTP block
  • Download and install Stackbuilder

Contact Us

Contact us with any questions or help with deploying your IoT projects.

CONTACT US

Connect

  • Facebook
  • Twitter
  • LinkedIn
  • Email

Sign up for the latest Interstacks news.

Join Our Newsletter
  Thank you for Signing Up
  Please correct the marked field(s) below.
1,false,1,First Name,2
Email:
1,true,6,Subscriber Email,2
Name:
1,false,1,Last Name,2

Copyright © 2023 Interstacks. All rights reserved. Privacy Policy | Terms and Conditions

Site by Imagebox