Advertisement
mayankjoin3

first_year query

Apr 26th, 2025 (edited)
221
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.63 KB | None | 0 0
  1. from collections import Counter
  2.  
  3. # Input values
  4. sem = 1
  5. sub_type = 'c'.strip().upper()
  6. session_sa = 'Autumn'.strip().upper()
  7. floated_year = 2025
  8.  
  9. # Step 1: Read, clean, and sort the rolls (case-insensitive)
  10. with open('roll1.txt', 'r') as file:
  11.     lines = [line.strip().upper() for line in file if line.strip()]
  12.  
  13. # Subject codes with credits
  14. SUBJECTS = {
  15.     'HS1101': 3,  #Core for all stud together
  16.     'MA1101': 3,  #Core for all stud together
  17.     'CS1101': 3,  #Core for all stud together
  18.     'ME1201': 3,  #50% stud
  19.     'ME1101': 3,  #50% stud
  20.     'CH1101': 3,  #50% stud
  21.  
  22. }
  23.  
  24. # SUBJECTS_REMAINING_HALF = {
  25.     # 'HS1101': 3,  #Core for all stud together
  26.     # 'MA1101': 3,  #Core for all stud together
  27.     # 'CS1101': 3,  #Core for all stud together
  28. #     'PH1201': 3,  #50% stud
  29. #     'EE1101': 3,  #50% stud
  30. #     'CE1101': 3,  #50% stud
  31.  
  32. # }
  33.  
  34.  
  35. # Sort rolls alphabetically (case-insensitive)
  36. sorted_rolls = sorted(lines, key=str.casefold)
  37.  
  38. # Overwrite roll1.txt with sorted data
  39. with open('roll1.txt', 'w') as file:
  40.     for roll in sorted_rolls:
  41.         file.write(roll + '\n')
  42.  
  43. # Step 2: Load sorted and cleaned rolls
  44. raw_rolls = sorted_rolls
  45.  
  46. # Step 3: Identify duplicates
  47. roll_counts = Counter(raw_rolls)
  48. duplicates = [roll for roll, count in roll_counts.items() if count > 1]
  49.  
  50. # Display duplicates
  51. if duplicates:
  52.     print("Duplicate roll numbers found in roll1.txt:")
  53.     for dup in duplicates:
  54.         print(f" - {dup} (count: {roll_counts[dup]})")
  55.     print(f"Total duplicate entries: {sum(roll_counts[dup] - 1 for dup in duplicates)}")
  56. else:
  57.     print("No duplicate roll numbers found.")
  58.  
  59. # Step 4: Remove duplicates
  60. roll_list = list(dict.fromkeys(raw_rolls))  # Preserves order
  61.  
  62.  
  63. # Normalize subject codes
  64. normalized_subjects = {k.strip().upper(): v for k, v in SUBJECTS.items()}
  65.  
  66. # Build SQL query
  67. query = "INSERT INTO `reg_table` (`roll`, `sem`, `sub_code`, `c`, `grade1`, `grade2`, `type`, `session_sa`, `floated_year`, `added_on`) VALUES\n"
  68.  
  69. values = []
  70. for roll in roll_list:
  71.     for sub_code, c_val in normalized_subjects.items():
  72.         values.append(f"('{roll}', {sem}, '{sub_code}', {c_val}, NULL, NULL, '{sub_type}', '{session_sa}', {floated_year}, NOW())")
  73.  
  74. query += ",\n".join(values) + ";"
  75. from datetime import datetime
  76.  
  77. # Generate timestamp in format YYYY-MM-DD-HH-MM
  78. timestamp = datetime.now().strftime('%Y-%m-%d-%H-%M-%S')
  79.  
  80. # Create filename with sem and timestamp & Save to SQL file
  81. file_name = f"reg_table_insert_sem_{sem}_{timestamp}.sql"
  82.  
  83. with open(file_name, "w") as f:
  84.     f.write(query)
  85.  
  86. print(f"SQL file '{file_name}' has been created with {len(values)} entries.")
  87.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement