<commit_before>#!/usr/bin/python
#
# Urwid advanced example column calculator application
#    Copyright (C) 2004-2009  <NAME>
#
#    This library is free software; you can redistribute it and/or
#    modify it under the terms of the GNU Lesser General Public
#    License as published by the Free Software Foundation; either
#    version 2.1 of the License, or (at your option) any later version.
#
#    This library is distributed in the hope that it will be useful,
#    but WITHOUT ANY WARRANTY; without even the implied warranty of
#    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
#    Lesser General Public License for more details.
#
#    You should have received a copy of the GNU Lesser General Public
#    License along with this library; if not, write to the Free Software
#    Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
#
# Urwid web site: http://excess.org/urwid/

"""
Urwid advanced example column calculator application

Features:
- multiple separate list boxes within columns
- custom edit widget for editing calculator cells
- custom parent widget for links to other columns
- custom list walker to show and hide cell results as required
- custom wrap and align modes for editing right-1 aligned numbers
- outputs commands that may be used to recreate expression on exit
"""

import urwid
import urwid.raw_display
import urwid.web_display

# use appropriate Screen class
if urwid.web_display.is_web_request():
    Screen = urwid.web_display.Screen
else:
    Screen = urwid.raw_display.Screen


def div_or_none(a,b):
    """Divide a by b.  Return result or None on divide by zero."""
    if b == 0: 
        return None
    return a/b

# operators supported and the functions used to calculate a result
OPERATORS = {
    '+': (lambda a, b: a+b),
    '-': (lambda a, b: a-b),
    '*': (lambda a, b: a*b),
    '/': div_or_none,
    }

# the uppercase versions of keys used to switch columns
COLUMN_KEYS = list( "?ABCDEF" )

# these lists are used to determine when to display errors
EDIT_KEYS = OPERATORS.keys() + COLUMN_KEYS + ['backspace','delete']
MOVEMENT_KEYS = ['up','down','left','right','page up','page down']

# Event text
E_no_such_column = "Column %s does not exist."
E_no_more_columns = "Maxumum number of columns reached."
E_new_col_cell_not_empty = "Column must be started from an empty cell."
E_invalid_key = "Invalid key '%s'."
E_no_parent_column = "There is no parent column to return to."
E_cant_combine = "Cannot combine cells with sub-expressions."
E_invalid_in_parent_cell = "Cannot enter numbers into parent cell."
E_invalid_in_help_col = [
    "Help Column is in focus.  Press ", 
    ('key',COLUMN_KEYS[1]),"-",('key',COLUMN_KEYS[-1]), 
    " to select another column."]

# Shared layout object
CALC_LAYOUT = None


class CalcEvent:
    """Events triggered by user input."""
    
    attr = 'event'
    
    def __init__(self, message):
        self.message = message
    
    def widget(self):
        """Return a widget containing event information"""
        text = urwid.Text( self.message, 'center' )
        return urwid.AttrWrap( text, self.attr )

class ColumnDeleteEvent(CalcEvent):
    """Sent when user wants to delete a column"""

    attr = 'confirm'

    def __init__(self, letter, from_parent=0):
        self.message = ["Press ", ('key',"BACKSPACE"),
            " again to confirm column removal."]
        self.letter = letter

class UpdateParentEvent:
    """Sent when parent columns may need to be updated."""
    pass<commit_msg>calc.py exception base classes from python3 branch<commit_after>#!/usr/bin/python
#
# Urwid advanced example column calculator application
#    Copyright (C) 2004-2009  <NAME>
#
#    This library is free software; you can redistribute it and/or
#    modify it under the terms of the GNU Lesser General Public
#    License as published by the Free Software Foundation; either
#    version 2.1 of the License, or (at your option) any later version.
#
#    This library is distributed in the hope that it will be useful,
#    but WITHOUT ANY WARRANTY; without even the implied warranty of
#    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
#    Lesser General Public License for more details.
#
#    You should have received a copy of the GNU Lesser General Public
#    License along with this library; if not, write to the Free Software
#    Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
#
# Urwid web site: http://excess.org/urwid/

"""
Urwid advanced example column calculator application

Features:
- multiple separate list boxes within columns
- custom edit widget for editing calculator cells
- custom parent widget for links to other columns
- custom list walker to show and hide cell results as required
- custom wrap and align modes for editing right-1 aligned numbers
- outputs commands that may be used to recreate expression on exit
"""

import urwid
import urwid.raw_display
import urwid.web_display

# use appropriate Screen class
if urwid.web_display.is_web_request():
    Screen = urwid.web_display.Screen
else:
    Screen = urwid.raw_display.Screen


def div_or_none(a,b):
    """Divide a by b.  Return result or None on divide by zero."""
    if b == 0: 
        return None
    return a/b

# operators supported and the functions used to calculate a result
OPERATORS = {
    '+': (lambda a, b: a+b),
    '-': (lambda a, b: a-b),
    '*': (lambda a, b: a*b),
    '/': div_or_none,
    }

# the uppercase versions of keys used to switch columns
COLUMN_KEYS = list( "?ABCDEF" )

# these lists are used to determine when to display errors
EDIT_KEYS = OPERATORS.keys() + COLUMN_KEYS + ['backspace','delete']
MOVEMENT_KEYS = ['up','down','left','right','page up','page down']

# Event text
E_no_such_column = "Column %s does not exist."
E_no_more_columns = "Maxumum number of columns reached."
E_new_col_cell_not_empty = "Column must be started from an empty cell."
E_invalid_key = "Invalid key '%s'."
E_no_parent_column = "There is no parent column to return to."
E_cant_combine = "Cannot combine cells with sub-expressions."
E_invalid_in_parent_cell = "Cannot enter numbers into parent cell."
E_invalid_in_help_col = [
    "Help Column is in focus.  Press ", 
    ('key',COLUMN_KEYS[1]),"-",('key',COLUMN_KEYS[-1]), 
    " to select another column."]

# Shared layout object
CALC_LAYOUT = None


class CalcEvent(Exception):
    """Events triggered by user input."""
    
    attr = 'event'
    
    def __init__(self, message):
        self.message = message
    
    def widget(self):
        """Return a widget containing event information"""
        text = urwid.Text( self.message, 'center' )
        return urwid.AttrWrap( text, self.attr )

class ColumnDeleteEvent(CalcEvent):
    """Sent when user wants to delete a column"""

    attr = 'confirm'

    def __init__(self, letter, from_parent=0):
        self.message = ["Press ", ('key',"BACKSPACE"),
            " again to confirm column removal."]
        self.letter = letter

class UpdateParentEvent(Exception):
    """Sent when parent columns may need to be updated."""
    pass
{'fraction_non_alphanumeric': 0.0950847220277272, 'fraction_numerical': 0.008262148158521216, 'mean_word_length': 3.55484693877551, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4270781', 'n_tokens_mistral': 2110, 'n_tokens_neox': 2053, 'n_words': 843}