Skip to content

Commit

Permalink
Merge pull request #18 from ihrke/master
Browse files Browse the repository at this point in the history
Replace {code} blocks in path string
  • Loading branch information
rossant committed Sep 24, 2014
2 parents aaaa4e0 + 617f408 commit c1305bf
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 5 deletions.
14 changes: 11 additions & 3 deletions ipycache.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
#------------------------------------------------------------------------------

# Stdlib
import inspect, os, sys, textwrap, cPickle
import inspect, os, sys, textwrap, cPickle, re

# Our own
from IPython.config.configurable import Configurable
Expand Down Expand Up @@ -54,10 +54,18 @@ def iteritems(d, **kw):
# Functions
#------------------------------------------------------------------------------
def conditional_eval(var, variables):
""" Evaluates the variable string if it starts with $. """
"""
Evaluates the variable string if it starts with $.
If the variable string contains one or several {code} statements, the code
is executed and the result stringified (wrapped in str()) into the rest of
the string.
"""
if var[0] == '$':
return variables.get(var[1:], var)
return var
def evalfun(x):
code=x.group(0)[1:-1]
return str(eval(code, variables))
return re.sub(r'{.*?}', evalfun, var, flags=re.DOTALL)

def clean_var(var):
"""Clean variable name, removing accidental commas, etc."""
Expand Down
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ def read(fname):
keywords="ipython notebook cache",
url="http://packages.python.org/ipycache",
py_modules=['ipycache'],
long_description=read('README'),
long_description=read('README.md'),
classifiers=[
"Development Status :: 3 - Alpha",
"Topic :: Utilities",
Expand Down
6 changes: 5 additions & 1 deletion test_ipycache.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,11 @@
def test_conditional_eval():
test_var = 'abc'
assert conditional_eval('$test_var', locals()) == 'abc'

x,fun=10,lambda x: x
test_eval='abc_{"10" if x==10 else "not_10"}_{fun(10)}'
expect='abc_10_10'
assert conditional_eval(test_eval, locals())==expect

def test_clean_var():
assert clean_var('abc') == 'abc'
assert clean_var('abc ') == 'abc'
Expand Down

0 comments on commit c1305bf

Please sign in to comment.