Skip to content

Instantly share code, notes, and snippets.

@joefutrelle
Last active August 29, 2015 14:24
Show Gist options
  • Select an option

  • Save joefutrelle/1d396481ae47e3278d1d to your computer and use it in GitHub Desktop.

Select an option

Save joefutrelle/1d396481ae47e3278d1d to your computer and use it in GitHub Desktop.
Ladder API examples
from oii.ldr import Resolver
RESOLVER="""
<rule name="main">
<path var="conf_file" match="/etc/*/*.conf"/>
</rule>
"""
# load the resolver from the text block.
# you can also pass a filename to Resolver
R = Resolver(RESOLVER)
# this is how to invoke the rule "main"
for solution in R.main():
print solution
from oii.ldr import Resolver
RESOLVER="""
<namespace name="myrules">
<rule name="people">
<var name="person">
<val>Hansel</val>
<val>Gretel</val>
<val>Ted</val>
<val>Alice</val>
</var>
</rule>
<rule name="greetpeople">
<invoke rule="myrules.people"/>
<var name="greeting">Hello, ${person}.</var>
</rule>
</namespace>
"""
R = Resolver(RESOLVER)
# invoke "myrules.greetpeople"
for solution in R.myrules.greetpeople():
print solution
from oii.ldr import Resolver
RESOLVER="""
<rule name="both">
<any>
<match var="number" pattern="\(?(\d{3})\)? *-? *(\d{3}) *-? *(\d{4})" groups="areaCode prefix lineNumber">
<test var="areaCode" ne="900"/>
</match>
<match var="number" pattern="(\d{3}) *-? *(\d{4})" groups="prefix lineNumber">
<test var="prefix" ne="555"/>
</match>
</any>
</rule>
"""
R = Resolver(RESOLVER)
# to bind variables when invoking a rule, pass the bindings
# as keyword arguments
for solution in R.both(number="(508) 274-9999"):
print solution
from oii.ldr import Resolver
R = Resolver('ldr_ex4.xml')
for solution in R.dir_filter(dir='/etc',extension='conf'):
print solution.get('file')
<rule name="dir_filter">
<path var="file" match="${dir}/*.${extension}"/>
</rule>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment