A dynamic, user-friendly application for creating, managing, and applying custom rules to map account numbers to different formats.
✨ Rule Builder - Create rules using multiple strategies:
- Regex Pattern Matching - Use regular expressions for complex transformations
- Prefix Replacement - Replace account number prefixes
- Suffix Replacement - Replace account number suffixes
- Custom Python Expressions - Write custom logic for transformations
📋 Rule Management
- Create unlimited mapping rules
- Enable/disable rules without deleting
- Edit existing rules
- Delete rules
- Persistent storage (JSON format)
🎯 Rule Application
- Apply single or multiple rules to account numbers
- Single account processing
- Batch processing with CSV uploads
- Step-by-step transformation tracking
- Download results as CSV
🔧 Tools
- Export rules as JSON (backup/share)
- Import rules from JSON files
- Auto-save functionality
- Clone or download this repository
- Install dependencies:
pip install -r requirements.txtstreamlit run mapping_rules.pyThe application will open in your default browser at http://localhost:8501
- Navigate to 📋 Rule Builder
- Enter rule details:
- Rule Name: Descriptive name for the rule
- Description: What the rule does
- Rule Type: Choose from 4 rule types
- Define the pattern and replacement based on rule type
- Test with a sample account number using the preview
- Click Save Rule
Use regular expressions for powerful pattern matching.
Example:
- Pattern:
^ACC(\d+)$ - Replacement:
ACCOUNT-\1 - Transforms:
ACC123456→ACCOUNT-123456
Replace the beginning of account numbers.
Example:
- Prefix to Find:
ACC - Replacement:
ACCOUNT - Transforms:
ACC123456→ACCOUNT123456
Replace the end of account numbers.
Example:
- Suffix to Find:
-USD - Replacement:
-CURR - Transforms:
ACC123456-USD→ACC123456-CURR
Write custom Python code using the account_number variable.
Example:
- Expression:
account_number.upper() + '-MAPPED' - Transforms:
acc123456→ACC123456-MAPPED
Available in Custom Mode:
account_number- The account number as a stringre- Python regex module
- Navigate to 🎯 Apply Rules
- Select which rules to apply (checkboxes)
- Choose input method:
- Single Account: Enter one account number
- Batch Upload: Upload CSV file
- View results:
- Original and transformed account numbers
- Optional: Step-by-step transformation view
- Download results if needed (batch mode)
- Navigate to 📂 Manage Rules
- View all created rules in expandable sections
- For each rule, you can:
- Toggle: Enable/disable without deleting
- Delete: Permanently remove the rule
Export Rules:
- Click 📥 Export Rules in the sidebar
- Download as JSON file for backup
Import Rules:
- Click 📤 Import Rules in the sidebar
- Select a previously exported JSON file
- Rules will be merged with existing ones
Rules are automatically saved to mapping_rules.json in the application directory. This file contains all rule configurations and is created/updated automatically.
Example mapping_rules.json:
[
{
"rule_id": "rule_20240215120000",
"name": "Standardize Account Format",
"description": "Convert old ACC format to new ACCOUNT format",
"rule_type": "regex",
"pattern": "^ACC(\\d+)$",
"replacement": "ACCOUNT-\\1",
"enabled": true,
"created_at": "2024-02-15T12:00:00.000000"
}
]Represents a single mapping rule with:
- Rule metadata (ID, name, description)
- Rule type and pattern/replacement
- Apply method for transformations
- Enable/disable toggle
Handles all rule operations:
- Load/save rules to JSON
- Add, update, delete rules
- Apply rules to account numbers
- Toggle rule status
show_rule_builder()- Rule creation interfaceshow_rule_manager()- Rule management interfaceshow_rule_applicator()- Rule application interfacemain()- Main application with navigation
- Prepare a CSV file with account numbers (first column)
- Use the batch upload feature
- Select rules to apply
- Download the mapped results
- Use the exported CSV in your systems
Chain multiple rules to create complex transformations:
Example Chain:
- Rule 1 (Regex):
^(\w+)(\d+)$→$2-$1- Reorder parts - Rule 2 (Custom):
account_number.upper()- Uppercase - Rule 3 (Suffix Replace): Remove unwanted suffix
Apply all three sequentially for compound transformations.
- Test Before Saving - Always use the preview feature before saving rules
- Clear Names - Use descriptive rule names for easy identification
- Disable Before Deleting - Disable a rule to test impact before deletion
- Backup Rules - Regularly export rules for backup
- Order Matters - Rules apply in the order they were created
- Documentation - Use descriptions to document transformation logic
Rules not saving:
- Check file permissions in the application directory
- Ensure
mapping_rules.jsonis writable
CSV upload failing:
- Verify CSV format (UTF-8 encoding recommended)
- Ensure first column contains account numbers
- Check for special characters in data
Regex errors:
- Test regex patterns in an online regex tester first
- Use raw strings with proper escape sequences
- Remember to use proper capture groups with
\1,\2, etc.
Custom expression errors:
- Use valid Python syntax
- Remember
account_numberis a string - Avoid dangerous operations (file I/O, imports, etc.)
- Python 3.7+
- Streamlit 1.28.0+
- Pandas 2.0.0+
This application is provided as-is for account mapping purposes.
For issues or feature requests, document and test them thoroughly before reporting.