Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make it so that streak.json and Extraordinary. MD do not have duplicate entry for same account. #87

Open
Prakash4844 opened this issue Nov 18, 2023 · 7 comments
Labels
enhancement New feature or request good first issue Good for newcomers

Comments

@Prakash4844
Copy link
Owner

Although we can add an extra field in json "Times" to show how many times user has used the repo. Same can be done with Extraordinary.MD

@Prakash4844 Prakash4844 added enhancement New feature or request good first issue Good for newcomers labels Nov 18, 2023
@noelb1
Copy link
Contributor

noelb1 commented Nov 21, 2023

I'd Like to know more

@Prakash4844
Copy link
Owner Author

This repo can be used multiple time for same account and each time it will add to open further rank for Pair-Extraordinaire badge. But for each time there will be a separate entry for same person in extraordinary.md and streak.json

Basically what I want to do is if a user decide to reuse the repo it does do all the steps, but while updating the streak.json and Extraordinary.md
It check if user has previously used the repo, if they have then it add an extra entry to their json block instead of adding a new block altogether, this will keep json from having entry for same account over and over, and something similar can also be done for Extraordinary.md

@noelb1
Copy link
Contributor

noelb1 commented Nov 26, 2023

GitHub Copilot: Sure, here's a step-by-step plan in pseudocode:

  1. Define a function update_files(user).
  2. In this function, first check if the user has previously used the repo.
    • Open the streak.json file and read its content.
    • Parse the content to a JSON object.
    • Check if the user's account is in the JSON object.
  3. If the user's account is in the JSON object, add an extra entry to their JSON block.
    • Find the user's block in the JSON object.
    • Add a new entry to the block.
    • Convert the JSON object back to a string.
    • Write the string back to the streak.json file.
  4. If the user's account is not in the JSON object, add a new block for the user.
    • Create a new block with the user's account and the entry.
    • Add the block to the JSON object.
    • Convert the JSON object back to a string.
    • Write the string back to the streak.json file.
  5. Repeat similar steps for the Extraordinary.md file.

Here's a Python code snippet that implements the above pseudocode:

import json

def update_files(user, entry):
    # Open the streak.json file and read its content
    with open('streak.json', 'r+') as file:
        data = json.load(file)

        # Check if the user's account is in the JSON object
        if user in data:
            # Add a new entry to the block
            data[user].append(entry)
        else:
            # Add a new block for the user
            data[user] = [entry]

        # Write the string back to the streak.json file
        file.seek(0)
        json.dump(data, file)
        file.truncate()

    # Repeat similar steps for the Extraordinary.md file
    with open('Extraordinary.md', 'r+') as file:
        content = file.readlines()

        # Check if the user's account is in the file
        if any(user in line for line in content):
            # Add a new entry to the user's line
            for i, line in enumerate(content):
                if user in line:
                    content[i] += '\n' + entry
                    break
        else:
            # Add a new line for the user
            content.append(user + '\n' + entry)

        # Write the content back to the Extraordinary.md file
        file.seek(0)
        file.writelines(content)
        file.truncate()

You can call the update_files function with the user's account and the new entry as arguments:

update_files('user_account', 'new_entry')

This code assumes that the streak.json file is a dictionary where the keys are the user accounts and the values are lists of entries, and that the Extraordinary.md file is a text file where each line starts with a user account followed by the entries for that user.

import json

def update_files(user, entry):
# Open the streak.json file and read its content
with open('streak.json', 'r+') as file:
data = json.load(file)

    # Check if the user's account is in the JSON object
    if user in data:
        # Add a new entry to the block
        data[user].append(entry)
    else:
        # Add a new block for the user
        data[user] = [entry]

    # Write the string back to the streak.json file
    file.seek(0)
    json.dump(data, file)
    file.truncate()

# Repeat similar steps for the Extraordinary.md file
with open('Extraordinary.md', 'r+') as file:
    content = file.readlines()

    # Check if the user's account is in the file
    if any(user in line for line in content):
        # Add a new entry to the user's line
        for i, line in enumerate(content):
            if user in line:
                content[i] += '\n' + entry
                break
    else:
        # Add a new line for the user
        content.append(user + '\n' + entry)

    # Write the content back to the Extraordinary.md file
    file.seek(0)
    file.writelines(content)
    file.truncate()

@noelb1
Copy link
Contributor

noelb1 commented Nov 26, 2023

i used my subscription to to copilot for tis code, let me know what you think

@Prakash4844
Copy link
Owner Author

Hey, please test the code on your end by forking the repo and doing the suggested changes, I also have copilot subscription but this code will most likely not work.

The streak.json is multilevel nested dict and we need to update extraordinary.md file too. Also if you are new to coding, I would suggest not using the copilot. As it will hinder in your skill development.

I know how to do the changes for the issue, I am just not getting enough time to do this.

@noelb1
Copy link
Contributor

noelb1 commented Nov 28, 2023

OK Thanks, for your advice, yes i am new to coding and enjoying it very much, i work in IT but don't get to code so I'm teaching myself, i've forked the repo

@Prakash4844
Copy link
Owner Author

Yeah, Please familiarize yourself with code first. Use the README.md and provided graphs in documentation to understand the workflow. Then it will be much easier to do the changes.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request good first issue Good for newcomers
Projects
None yet
Development

No branches or pull requests

2 participants