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

Write a script to turn DataClass into JSON #71

Closed
bee-san opened this issue Mar 19, 2021 · 8 comments
Closed

Write a script to turn DataClass into JSON #71

bee-san opened this issue Mar 19, 2021 · 8 comments
Labels
good first issue Good for newcomers help wanted Extra attention is needed

Comments

@bee-san
Copy link
Member

bee-san commented Mar 19, 2021

Turn all the DataClass's in this file:
https://github.com/HashPals/Name-That-Hash/blob/main/name_that_hash/hashes.py

Into JSON with this format:
https://github.com/psypanda/hashID/blob/master/prototypes.json

Please submit a PR to the repo with your script :)

@bee-san bee-san added good first issue Good for newcomers help wanted Extra attention is needed labels Mar 19, 2021
@bburky
Copy link
Contributor

bburky commented Mar 19, 2021

I've forgotten the full history of prototypes.json. At one point I played with the idea of moving the hash definitions completely out of Python and into JSON, but we never finished it: psypanda/hashID#30

I'm not really sure if that's better or not than having it in Python.

I've also forgotten how we generated it. I'm pretty sure it was just a python json.dump() of the data. You may need to do something like this to dump dataclasses:
https://stackoverflow.com/a/51286749

@bee-san
Copy link
Member Author

bee-san commented Mar 20, 2021

I've forgotten the full history of prototypes.json. At one point I played with the idea of moving the hash definitions completely out of Python and into JSON, but we never finished it: psypanda/hashID#30

I'm not really sure if that's better or not than having it in Python.

I've also forgotten how we generated it. I'm pretty sure it was just a python json.dump() of the data. You may need to do something like this to dump dataclasses:
https://stackoverflow.com/a/51286749

This is purely for inter-language operability 😅 I'm working on a project in Go and can't use the DB here unless it's in a text-file based format (such as JSON)

I feel like this app is 2 parts:

  1. The database
  2. The CLI / API in Python

If we can export to JSON, it means other languages can use it and that means we can get more contributors.

I was playing around with the idea of having the database be a Git submodule, but I don't see why a JSON export wouldn't work for what I'm imagining :)

@Jayy001
Copy link
Contributor

Jayy001 commented Mar 20, 2021

Hi bee, here you go:

import json

return_value = []

for prototype in prototypes:
    return_value.append(
        {
            "regex": str(prototype.regex.pattern),
            "modes": [asdict(i) for i in prototype.modes],
        }
    )

with open("result.txt", "w") as file:
    file.write(json.dumps(return_value, indent=4))

hashes.txt

add this to the end of the hashes.py and run it. Ill put the json attached

@bee-san
Copy link
Member Author

bee-san commented Mar 20, 2021

Hi bee, here you go:

import json

return_value = []

for prototype in prototypes:
    return_value.append(
        {
            "regex": str(prototype.regex.pattern),
            "modes": [asdict(i) for i in prototype.modes],
        }
    )

with open("result.txt", "w") as file:
    file.write(json.dumps(return_value, indent=4))

hashes.txt

add this to the end of the hashes.py and run it. Ill put the json attached

Thank you!!!
Why are all the dollar signs escaped?

@Jayy001
Copy link
Contributor

Jayy001 commented Mar 20, 2021

What do you mean by this? I can't see it

@bee-san
Copy link
Member Author

bee-san commented Mar 20, 2021

Open the file
image

@bee-san
Copy link
Member Author

bee-san commented Mar 20, 2021

Ah wait that's the regex itself nvm

@Jayy001 Jayy001 closed this as completed Mar 20, 2021
@Jayy001
Copy link
Contributor

Jayy001 commented Mar 20, 2021

Ahh no worries 😄

@Jayy001 Jayy001 reopened this Mar 20, 2021
@bee-san bee-san closed this as completed Apr 8, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
good first issue Good for newcomers help wanted Extra attention is needed
Projects
None yet
Development

No branches or pull requests

3 participants