Welcome, guest | Sign In | My Account | Store | Cart

After looking at featurelist.org and noticing that someone wanted to import code stored on Utility Mill, the following code was written as an extension of previous work on automated testing. The original thread can be found at http://featurelist.org/features/details/238 . The code imported by the test at the end of this code can be found at http://utilitymill.com/edit/SPICE/27 & http://utilitymill.com/edit/nysiis/20 . Please note that this is Python 3.0 code importing Python 2.5 code and may not be useful in all contexts that you may wish to use it in. Also, modules stored on Utility Mill may need unconditionally executing code removed first before importing.

Python, 26 lines
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
import urllib.request
import xml.dom.minidom

def um_import(name, revision):
    url = 'http://utilitymill.com/api/xml/utility/{0}/{1}/code'
    file = urllib.request.urlopen(url.format(name, revision))
    out = file.read().decode()
    fix = out.split('<', 1)[1].rsplit('>', 1)[0]
    dom = xml.dom.minidom.parseString('<' + fix + '>')
    elements = dom.getElementsByTagName('code')
    assert len(elements) == 1, 'XML Error'
    code = elements[0]
    assert len(code.childNodes) == 1, 'XML Error'
    child = code.childNodes[0]
    assert child.nodeType == child.CDATA_SECTION_NODE, 'XML Error'
    module = child.nodeValue
    open(name + '.py', 'w').write(module)
    return __import__(name)

# TEST 1
SPICE = um_import('SPICE', 27)

# TEST 2
nysiis = um_import('nysiis', 20).nysiis
while True:
    print(nysiis(input('Name or Word: ')))

1 comment

greg p 12 years, 11 months ago  # | flag

Hi, Utility Mill developer here, this is really neat. I can't wait to try it out. Good point about removing unconditional execution. Drop me an email if you have any questions or ideas for developing this further. (my email should be on utility mill somewhere)