Python类中带规则约束的列表更新实现技术求助
Python Class for Constrained List Updates with UP & Video Number Support
Hey there! I've put together a Python class that meets all your specified requirements, including built-in support for the upcoming video number field. Let's walk through the implementation and how it handles each constraint.
Core Design Approach
To enforce your rules efficiently, we'll use two dictionaries to maintain critical mappings:
code_map: Stores 7-digit codes as keys, with values being tuples of (up_name,video_numbers_list) — this lets us quickly check if a code is already in use and manage videos for each UP.up_code_map: Maps each UP name to its bound 7-digit code, ensuring we can instantly verify that an UP isn't trying to use a conflicting code.
Full Implementation Code
import re class ItemManager: def __init__(self): # Key: 7-digit code, Value: (up_name, list of video numbers) self.code_map = {} # Key: up_name, Value: bound 7-digit code self.up_code_map = {} def _is_valid_code(self, code): """Helper method to check if code is exactly 7 digits""" return re.fullmatch(r'\d{7}', code) is not None def add_item(self, up_name, code, video_number=None): # 1. Validate code format if not self._is_valid_code(code): print("Error: Code must be a 7-digit number. Please try again.") return False # 2. Check if code already exists if code in self.code_map: existing_up, _ = self.code_map[code] print(f"Error: Code {code} is already registered to UP {existing_up}.") return False # 3. Enforce UP-code consistency if up_name in self.up_code_map: bound_code = self.up_code_map[up_name] if bound_code != code: print(f"Error: UP {up_name} is already bound to code {bound_code}. Cannot use code {code}.") return False else: # New UP: bind code and initialize video list self.up_code_map[up_name] = code self.code_map[code] = (up_name, []) # Handle video number (if provided) if video_number is not None: if video_number in self.code_map[code][1]: print(f"Notice: Video number {video_number} already exists for UP {up_name}. No duplicate added.") return True self.code_map[code][1].append(video_number) print(f"Successfully added: UP {up_name}, Code {code}, Video Number: {video_number or 'Not specified'}") return True # Test the implementation if __name__ == "__main__": manager = ItemManager() # Test 1: Add new UP with valid code and video number manager.add_item("ZhangSan", "1234567", "V001") # Test 2: Try adding same UP with different code (should fail) manager.add_item("ZhangSan", "7654321", "V002") # Test 3: Try adding existing code for different UP (should fail) manager.add_item("LiSi", "1234567", "V001") # Test 4: Add new video number for existing UP (should succeed) manager.add_item("ZhangSan", "1234567", "V002") # Test 5: Try invalid 6-digit code (should fail) manager.add_item("WangWu", "123456", "V001")
How It Handles Each Constraint
- 7-digit code requirement: The
_is_valid_codehelper uses a regex to ensure the input is exactly 7 digits (no letters, no shorter/longer lengths). - Duplicate code check: We check
code_mapimmediately — if the code exists, we notify the user and abort the add. - UP-code consistency:
up_code_maptracks which code each UP is bound to. If an UP tries to use a different code, we block the action and show an error. - Video number support: For existing UPs, we simply append new video numbers to their associated list. We also include an optional check to prevent duplicate video numbers (you can remove this if duplicates are allowed in your use case).
Sample Output
When running the test code, you'll see these results:
Successfully added: UP ZhangSan, Code 1234567, Video Number: V001 Error: UP ZhangSan is already bound to code 1234567. Cannot use code 7654321. Error: Code 1234567 is already registered to UP ZhangSan. Successfully added: UP ZhangSan, Code 1234567, Video Number: V002 Error: Code must be a 7-digit number. Please try again.
内容的提问来源于stack exchange,提问作者user9666493
相关产品推荐
相关产品推荐

