pdw / Source / pyconsole_syntax.py

The default branch has multiple heads

import re
# # # # # # # # # # # # # # # # # # 
# An Example of User Defined Syntax 
#(Familiarity with regex is assumed)
# --------------------------------- 
# First create a regular expression to match the syntax you want      
re_add = re.compile(r'(?P<left_op>\S+)\s*[+]\s*(?P<right_op>\S+)')
# This one allows us to find things that match "x + y" where x and y  
# are integers.						
#
# Then we define a function.        
# The console will always pass itself as the first argument, 
# and the SRE_Match object as the second.
#
# Everything in the match group will be a string at first so you need
# to convert each element to its correct type, the console can handle this
# with the console.convert_token method
# perform whatever operation you wish to perform (don't foget
# to validate the input, at the very least enclose it in a try..except block)
# Then output it to the console, or do whatever you want with the data.
def console_add(console, match):
	left = console.convert_token(match.group("left_op"))
	right = console.convert_token(match.group("right_op"))
	try:
		out = left + right
	except Exception, strerror:
		console.output(strerror)
	else:
		console.output(out)
	return out

# # # # # # # # # # # # # # # # # #
# Another Example, this one       #
# lets you call functions like:   #
# name(arg1,arg2,...,argn)        #
re_function = re.compile(r'(?P<name>\S+)(?P<params>[\(].*[\)])')
def console_func(console, match):
	func = console.convert_token(match.group("name"))
	params = console.convert_token(match.group("params"))
	
	
	if not isinstance(params, tuple):
		params = [params]
	
	try:
		out = func(*params)
	except Exception, strerror:
		console.output(strerror)
	else:
		console.output(out)
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.