Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # DATASET MODULE
- ####### CODE ########
- def load_users_transactions():
- user_transactions = {}
- with open("Transaction.txt", "r") as transactions:
- for row in transactions:
- transaction = row.split(":")
- (user_id, transaction_id, name, amount, x, y, is_fraud) = row.split(":")[
- 0:7
- ]
- if transaction[0] not in user_transactions.keys():
- user_transactions[user_id] = {}
- user_transactions[user_id]["transactions"] = {}
- user_transactions[user_id]["transactions"][transaction_id] = {
- "name": name,
- "amount": float(amount),
- "x": float(x),
- "y": float(y),
- "is_fraud": is_fraud.strip().capitalize(),
- }
- else:
- user_transactions[user_id]["transactions"][transaction_id] = {
- "name": name,
- "amount": float(amount),
- "x": float(x),
- "y": float(y),
- "is_fraud": is_fraud.strip().capitalize(),
- }
- return user_transactions
- ####### Pseudo code ########
- function load_users_transactions()
- users_transactions dictionary => empty dictionary
- transactions => with open "Transaction.txt"
- for each row_of_transaction in transactions
- transaction => row.split(:)
- user_id => transaction[position 0]
- transaction_id=> transaction[position 1]
- if user_id is not in users_transactions.keys
- user_transactions[user_id] => empty dictionary
- user_transactions[user_id][transactions] => empty dictionary
- user_transactions[user_id][transactions][transaction_id] => {"name":[position 2],"amount":float([position 3]),"x":[position 4], "y":[position 5],"bool":[position 5].strip()}
- else
- user_transactions[user_id][transactions][transaction_id] => {"name":[position 2],"amount":float([position 3]),"x":[position 4], "y":[position 5],"bool":[position 5].strip()}
- return users_transactions dictionary
- end function
- ####### ENGLISH explanation ########
- we define a function called "load_users_transactions"
- indented inside it is the code that'll return the transactions of each user in a dictionary structured in the manner below:
- we create an empty dictionary called users_transactions
- load the Transaction.txt file => with open("Transaction.txt", "r") as transactions:
- then we loop through line of a transaction in transactions,
- split it into a list by the ":" character, and assign the first item to a variable called "user_id",
- then we check if the user_id doesn't already exist in the users_transactions dictionary keys
- if it does, we create an empty dictionary for the user and add the single-row transaction we are working on
- else, that means it already exist, then we just add the single-row transaction we are working on to the existing dictionary
- after the loop is completed, we return the final users_transactions
- sample of the final returned dictionary looks like this below:
- {
- user_id: {
- 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 } }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement