partpy Package

sourcestring Module

SourceString stores the entire string to be parsed in memory and provides some simple methods for retrieving and moving current position aswell as methods for matching strings and patterns.

class partpy.sourcestring.SourceLine(string, lineno)[source]

Bases: partpy.sourcestring.SourceString

Contains an entire line of a source with handy line specific methods.

get_first_char()[source]

Return the first non-whitespace character of the line.

get_last_char()[source]

Return the last non-whitespace character of the line.

pretty_print(carrot=False)[source]

Return a string of this line including linenumber. If carrot is True then a line is added under the string with a carrot under the current character position.

strip_trailing_ws()[source]

Remove trailing whitespace from internal string.

class partpy.sourcestring.SourceString(string=None)[source]

Bases: object

Stores the parse string and its length followed by current position in the string and if the end of the string has been reached.

It also stores the current row and column position as manually counted.

Provides multiple methods for matching strings and patterns and working with the source string.

__contains__(string)[source]

Returns a boolean if the given string is within the base string. Called by ‘word’ in SourceString.

__getitem__(index)[source]

Returns the character at the given index. Called by SourceString[index] where index is an integer.

__init__(string=None)[source]

Accepts a string or None by default. If a string is given then self.set_string(string) is run automatically. If you wish to load a file then create a SourceString object with no arguments and then use load_file or overload this function when inheriting from SourceString.

__iter__()[source]

Yields the current char and moves the position onwards until eos.

__len__()[source]

Returns the length of base string. Called by len(SourceString).

__repr__()[source]

Returns the entire base string. Called from the repr() builtin.

__weakref__

list of weak references to the object (if defined)

add_string(string)[source]

Add to the working string and its length and reset eos.

count_indents(spacecount, tabs=0, offset=0)[source]

Counts the number of indents that can be tabs or spacecount number of spaces in a row from the current line.

count_indents_last_line(spacecount, tabs=0, back=5)[source]

Finds the last meaningful line and returns its indent level. Back specifies the amount of lines to look back for a none whitespace line.

count_indents_length(spacecount, tabs=0, offset=0)[source]

Counts the number of indents that can be tabs or spacecount number of spaces in a row from the current line.

Also returns the character length of the indents.

count_indents_length_last_line(spacecount, tabs=0, back=5)[source]

Finds the last meaningful line and returns its indent level and character length. Back specifies the amount of lines to look back for a none whitespace line.

eat_length(length)[source]

Move current position forward by length and sets eos if needed.

eat_line()[source]

Move current position forward until the next line.

eat_string(string)[source]

Move current position by length of string and count lines by .

eol_distance_last(offset=0)[source]

Return the ammount of characters until the last newline.

eol_distance_next(offset=0)[source]

Return the amount of characters until the next newline.

get_all_lines()[source]

Return all lines of the SourceString as a list of SourceLine’s.

get_char(offset=0)[source]

Return the current character in the working string.

get_current_line()[source]

Return a SourceLine of the current line.

get_length(length, trim=0, offset=0)[source]

Return string at current position + length. If trim == true then get as much as possible before eos.

get_line(lineno)[source]

Return any line as a SourceLine and None if lineno doesnt exist.

get_lines(first, last)[source]

Return SourceLines for lines between and including first & last.

get_string(offset=0)[source]

Return non space chars from current position until a whitespace.

get_surrounding_lines(past=1, future=1)[source]

Return the current line and x,y previous and future lines. Returns a list of SourceLine’s.

has_space(length=1, offset=0)[source]

Returns boolean if self.pos + length < working string length.

load_file(filename)[source]

Read in file contents and set the current string.

match_any_char(chars, offset=0)[source]

Match and return the current SourceString char if its in chars.

match_any_string(strings, word=0, offset=0)[source]

Attempts to match each string in strings in order. Will return the string that matches or an empty string if no match.

If word arg >= 1 then only match if string is followed by a whitespace which is much higher performance.

If word is 0 then you should sort the strings argument yourself by length.

match_function_pattern(first, rest=None, least=1, offset=0)[source]

Match each char sequentially from current SourceString position until the pattern doesnt match and return all maches.

Integer argument least defines and minimum amount of chars that can be matched.

This version takes functions instead of string patterns. Each function must take one argument, a string, and return a value that can be evauluated as True or False.

If rest is defined then first is used only to match the first arg and the rest of the chars are matched against rest.

match_string(string, word=0, offset=0)[source]

Returns 1 if string can be matches against SourceString’s current position.

If word is >= 1 then it will only match string followed by whitepsace.

match_string_pattern(first, rest=None, least=1, offset=0)[source]

Match each char sequentially from current SourceString position until the pattern doesnt match and return all maches.

Integer argument least defines and minimum amount of chars that can be matched.

If rest is defined then first is used only to match the first arg and the rest of the chars are matched against rest.

reset_position()[source]

Reset all current positions.

rest_of_string(offset=0)[source]

A copy of the current position till the end of the source string.

set_string(string)[source]

Set the working string and its length then reset positions.

skip_whitespace(newlines=0)[source]

Moves the position forwards to the next non newline space character. If newlines >= 1 include newlines as spaces.

spew_length(length)[source]

Move current position backwards by length.

partpyerror Module

Custom exception for classes inheriting SourceString or Matcher.

exception partpy.partpyerror.PartpyError(obj, msg=None)[source]

Bases: exceptions.Exception

Takes a SourceString or Matcher derived object and an optional message.

When converted to a string will display the previous and current line with line numbers and a ‘^’ under the current position of the object with the optional message on the following line.

__weakref__

list of weak references to the object (if defined)

pretty_print(carrot=True)[source]

Print the previous and current line with line numbers and a carret under the current character position.

Will also print a message if one is given to this exception.

spattern Module

Predefined string patterns for use in Matcher.match_pattern methods.

Defines the following:
  • alphal = lower case alphabet
  • alphau = upper case alphabet
  • alpha = lower and upper case alphabet
  • number = digits
  • alnum = digits or lower and upper case alphabet
  • identifier = first(alpha) rest(alnum | ‘_’)
  • qualified = first(alpha) rest(alnum | ‘.’ | ‘_’)
  • integer = first(number | ‘-‘) rest(number)

fpattern Module

Predefined function patterns for use in Matcher.match_function methods.

Defines the following:
  • alphal = lower case alphabet
  • alphau = upper case alphabet
  • alpha = lower and upper case alphabet
  • number = digits
  • alnum = digits or lower and upper case alphabet
  • identifier = first(alpha) rest(alnum | ‘_’)
  • qualified = first(alpha) rest(alnum | ‘.’ | ‘_’)
  • integer = first(number | ‘-‘) rest(number)

Project Versions

Table Of Contents

Previous topic

Changelog

Next topic

examples Package

This Page