Skip to content

Instantly share code, notes, and snippets.

@slowli slowli/
Created Jun 21, 2015

What would you like to do?
Embedding declarations in runnable Python scripts
Example of an embeddable module.
#region get_input
def get_input():
''' Scans and returns input until EOF symbol. '''
input_text = ''
while True: input_text += raw_input() + '\n'
except EOFError:
return input_text[:-1]
#region cached
def cached(fn):
''' Decorator for functions that allows caching their values. '''
_cache = dict()
def cached_fn(*args):
mapped_args = args
if not mapped_args in _cache:
_cache[mapped_args] = fn(*args)
return _cache[mapped_args]
return cached_fn
#region cat depends on cached
def cat(n):
''' Calculates Catalan numbers using the recurrent formula. '''
if n == 0: return 1
sum = 0
for i in range(n):
sum += cat(i) * cat(n - 1 - i)
return sum
A simple util to automatically embed declarations from modules into
runnable scripts.
Can be used, e.g., in competetive programming, where it is usually required
to submit a solution as a single file.
import sys, os, re
def usage():
print '''\
Usage: {0} [-n] path/to/ path/to/
Embeds declarations from a module into a runnable script.
The modified script is output to the standard output.
If a module name is not specified, it is assumed the filename
of the module without the `.py' extension.
Called without arguments, the command prints this help message.
Preparing modules
Declarations in the module file must be marked with
#region / #endregion comments, e.g.
#region foo
def foo():
Regions may be dependent on other regions, e.g.
#region bar depends on foo
def bar():
Imports in the runnable script must be in the form
from <module> import <declaration1>, <declaration2>, ...
{0} -n common test/ test/
def wrong_usage():
print '''\
Invalid command usage. Type `{0}' for help.
class Module(dict):
# RegExp for region starting comment
# Groups:
# 1 - name of the region
# 3 - dependencies
_R_REGEX = re.compile('^#region (\w+)?(\s+depends on\s+(\w+(\s*,\s*\w+)*))?')
# RegExp for region ending comment
_E_REGEX = re.compile('^#endregion')
# RegExp for splitting comma-separated lists
_COMMA = re.compile('\s*,\s*')
# Template for RegExp of the import declaration
_I_REGEX_T = '^from {0} import (\w+(\s*,\s*\w+)*)'
def __init__(self, lines, name):
region_name = ''
region = ''
self.dependencies = dict() = name
for line in lines:
match = self._R_REGEX.match(line)
if match:
region_name =
region = ''
dependencies =
if dependencies:
dependencies = self._COMMA.split(dependencies)
self.dependencies[region_name] = dependencies
elif self._E_REGEX.match(line):
self[region_name] = region[:-1]
region = ''
region += line
def inline_imports(self, imports, start_msg='', end_msg=''):
''' Creates the embedded code for the given imported declarations. '''
all_imports = imports
ptr = len(all_imports) - 1
while ptr >= 0:
im = all_imports[ptr]
if self.dependencies[im] is not None:
for dp in self.dependencies[im]:
if dp not in all_imports:
all_imports[0:0] = [ dp ]
ptr -= 1
code = '\n\n'.join([self[im] for im in all_imports])
code = start_msg + '\n' + code + '\n' + end_msg
return code
def embed(self, lines, start_msg='', end_msg=''):
''' Replaces import declarations from this module in the script
with fragments of the module. '''
import_re = re.compile(self._I_REGEX_T.format(
code = ''
for line in lines:
match = import_re.match(line)
if match:
imports = self._COMMA.split(
code += self.inline_imports(imports)
code += line
return code
if __name__ == '__main__':
if len(sys.argv) == 1:
usage(); sys.exit(0)
module_name = None
if sys.argv[1] == '-n':
if len(sys.argv) < 3:
wrong_usage(); sys.exit(2)
module_name = sys.argv[2]
del sys.argv[1:3]
if module_name is None:
module_name = os.path.basename(sys.argv[1])
if module_name.endswith('.py'):
module_name = module_name[:-3]
if len(sys.argv) < 3:
wrong_usage(); sys.exit(2)
with open(sys.argv[1], 'r') as fh:
module = Module(fh, name=module_name)
with open(sys.argv[2], 'r') as fh:
print module.embed(fh)
Runnable script illustrating automatical code embedding.
The script calculates Catalan numbers
Indices of numbers are read from the standard input, one index at a line.
To test code embedding, use the command
python >
and then run
import unittest, sys
from common import get_input, cat
if __name__ == '__main__':
for line in get_input().splitlines():
i = int(line)
print 'Cat({0}) = {1}'.format(i, cat(i))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.