Sunday, November 8, 2015

Some Additional Resources for CodeSkulptor

As we've seen before, Codeskulptor is a browser-based Python programming environment built by Scott Rixner that uses Skulpt to interpret/run python code in your browser.

Here are a few additional resources you can use:

super()

The super() function is not implemented in CodeSkulptor (actually in Skulpt). Here's a Super class disguised as the super function you can use instead. This uses python 2's syntax of super(Classname, self).method(...)

from user40_oKHcLUHi2y_6 import super

base64()

The following imports a base64 encoder that, thanks to one of CodeSkulptor's unique libraries (simplegui), works at almost the same speed as an equivalent JavaScript function.

from user40_fLJEVmzz56qKjnu import base64
print base64("Kevin van Zonneveld")
 

Reading Text Data from Dropbox or Google Drive

One of the custom CodeSkulptor modules is codeskulptor, which includes a file2url function. This function takes as a parameter a CodeSkulptor filename, and converts it into a readable/downloadable url. The following functions are designed to work similarly, but for Dropbox and Google Drive instead. They will return a url that can be used with CodeSkulptor's urllib2.urlopen function.

To read a shared text file from Dropbox
Call this function, passing it the dropbox url of the shared text file.
def dropboxfile2url(url):
    url = url.replace("www.dropbox.","dl.dropboxusercontent.")
    return url
For example,
import urllib2
my_file = urllib2.urlopen(dropboxfile2url(url))
print my_file.read()
Reading a shared text file from Google Drive
Similarly, call this function and pass it a Google Documents url (if the text file has been shared).
def googlefile2url(url):
    #converts
    #https://docs.google.com/document/d/randomID/edit
    #to
    #https://docs.google.com/document/export?format=txt&id=randomID
    url = url.replace("/edit","")
    baseurl = url.find("/",9)
    part1 = url.find("/",baseurl+1)
    part2 = url.find("/",part1+1)
    url = url.replace(url[part1:part2+1],"/export?format=txt&id=")
    return url
For example,
import urllib2
my_file = urllib2.urlopen(googlefile2url(url))
print my_file.read()

Data Permanence

The following store information to the cloud. The data will remain in the cloud permanently, or at least until the webservice clears the data.

You could, instead, set up your own server for data or database storage, but this is at your own risk and requires some knowledge and skills outside the scope of this post.
TinyWebDB
MIT App Inventor for Android provides an online public database service called TinyWebDB. By using the service provided by TinyWebDB, you can read/write a strings of data to the cloud.

App Inventor's TinyWebDB service has some limitations, but it provides instructions for running your own TinyWebDB service if you want privacy, or to not be bound by these limits.

According to the App Inventor for Android: TinyWebDB Service site:
NOTE: This service is being modified. It might go offline without notice
This service is only a demo. The database will store at most 250 entries; adding entries beyond that will cause the oldest entries to be deleted. Also, individual data values are limited to at most 500 characters.

Whether you wish to use MIT's public TinyWebDB database or run your own, the following code can be used to read and write data to the database:
from user40_z7IvquyR5AIkShD_0 import TinyWebDB

db = TinyWebDB(optional url for the TinyWebDB database)
a = db.read_cookie('test')
print a
db.write_cookie('test','this is cool')
print db.read_cookie('test')

db.write_cookie('test',a)
Yahoo's Sherpa Storage using YQL
By using the power of Yahoo Query Language (YQL), we can store information to Yahoo's Sherpa Storage. One of the tables available in YQL is the csv table which manages data in a comma-separated values file. This pseudo-database DB class can be imported and saves "cookies" to Yahoo's Sherpa storage, with the following restrictions:
from user37_sy2hPG6N72tRcaz_0 import DB

#sets up access to a YQL storage
#if you don't pass a select and an update URL, 
#    the default store will be used
db = DB(optional YQL_store_select, optional YQL_store_update)

# db.ReadUserCookies(app_name,user)
#    read all cookies for a given app and user
# db.ReadCookie(app,user,cookie)
#    read a cookie for a given app, user, and cookie name
# db.ReadAppUsers(app,cookie)
#    read all users for a given app and cookie
# db.WriteCookie(app,user,cookie,value)
#    write a cookie for a given app, user, and cookie name