Advertisement
themaleem

dataset pseudocode

Apr 13th, 2023 (edited)
79
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 3.45 KB | Source Code | 0 0
  1. # DATASET MODULE
  2.  
  3.  
  4. ####### CODE ########
  5.  
  6. def load_users_transactions():
  7.     user_transactions = {}
  8.     with open("Transaction.txt", "r") as transactions:
  9.         for row in transactions:
  10.             transaction = row.split(":")
  11.             (user_id, transaction_id, name, amount, x, y, is_fraud) = row.split(":")[
  12.                 0:7
  13.             ]
  14.  
  15.             if transaction[0] not in user_transactions.keys():
  16.                 user_transactions[user_id] = {}
  17.                 user_transactions[user_id]["transactions"] = {}
  18.                 user_transactions[user_id]["transactions"][transaction_id] = {
  19.                     "name": name,
  20.                     "amount": float(amount),
  21.                     "x": float(x),
  22.                     "y": float(y),
  23.                     "is_fraud": is_fraud.strip().capitalize(),
  24.                 }
  25.             else:
  26.                 user_transactions[user_id]["transactions"][transaction_id] = {
  27.                     "name": name,
  28.                     "amount": float(amount),
  29.                     "x": float(x),
  30.                     "y": float(y),
  31.                     "is_fraud": is_fraud.strip().capitalize(),
  32.                 }
  33.     return user_transactions
  34.  
  35.  
  36.  
  37.  
  38.  
  39. ####### Pseudo code ########
  40.  
  41. function load_users_transactions()
  42.     users_transactions dictionary => empty dictionary
  43.     transactions => with open "Transaction.txt"
  44.         for each row_of_transaction in transactions
  45.             transaction => row.split(:)
  46.             user_id => transaction[position 0]
  47.             transaction_id=> transaction[position 1]
  48.            
  49.             if user_id is not in users_transactions.keys
  50.                 user_transactions[user_id] => empty dictionary
  51.                 user_transactions[user_id][transactions] => empty dictionary
  52.                 user_transactions[user_id][transactions][transaction_id] => {"name":[position 2],"amount":float([position 3]),"x":[position 4], "y":[position 5],"bool":[position 5].strip()}
  53.              
  54.             else
  55.                 user_transactions[user_id][transactions][transaction_id] => {"name":[position 2],"amount":float([position 3]),"x":[position 4], "y":[position 5],"bool":[position 5].strip()}
  56.      
  57.      return users_transactions dictionary
  58.      
  59. end function
  60.  
  61.  
  62.  
  63.  
  64.  
  65.  
  66. ####### ENGLISH explanation ########
  67.  
  68. we define a function called "load_users_transactions"
  69. indented inside it is the code that'll return the transactions of each user in a dictionary structured in the manner below:
  70.  
  71. we create an empty dictionary called users_transactions
  72. load the Transaction.txt file => with open("Transaction.txt", "r") as transactions:
  73.     then we loop through line of a transaction in transactions,
  74.         split it into a list by the ":" character, and assign the first item to a variable called "user_id",
  75.         then we check if the user_id doesn't already exist in the users_transactions dictionary keys
  76.             if it does, we create an empty dictionary for the user and add the single-row transaction we are working on
  77.             else, that means it already exist, then we just add the single-row transaction we are working on to the existing dictionary
  78.  
  79. after the loop is completed, we return the final users_transactions
  80.  
  81.  
  82. sample of the final returned dictionary looks like this below:
  83. {
  84. user_id: {
  85.             transactions: {name: name of the transaction, amount: transaction amount in float, "x": x-coordinate, "y": y-coordinate, is_fraud: a boolean key for if the transaction is fraudulent or not } }
  86. }
  87.  
  88.  
  89.  
  90.  
  91.  
  92.  
  93.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement