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

Append a JSON data to the last existing JSON file! #8

Open
black-fractal opened this issue Jan 19, 2021 · 1 comment
Open

Append a JSON data to the last existing JSON file! #8

black-fractal opened this issue Jan 19, 2021 · 1 comment
Assignees
Labels
bug Something isn't working

Comments

@black-fractal
Copy link
Owner

black-fractal commented Jan 19, 2021

  • Describe the bug
    When the link fetching rate is too high, the new JSON data is appended to the last existing JSON file!

  • To Reproduce and Cause
    When the interval time of writing the two files is less than one second.

  • ERROR
    while function read_history_files is trying to fetch last JSON file including duplicate JSON data.

  File "/home/ubuntu/python/wikipedia-philosophy-game/src/philosophy-fast-crawler.py", line 403, in <module>
    main()
  File "/home/ubuntu/python/wikipedia-philosophy-game/src/philosophy-fast-crawler.py", line 393, in main
    read_history_files( '../json/', files_history )
  File "/home/ubuntu/python/wikipedia-philosophy-game/src/philosophy-fast-crawler.py", line 341, in read_history_files
    data = json.load( handler )
  File "/usr/lib/python3.9/json/__init__.py", line 293, in load
    return loads(fp.read(),
  File "/usr/lib/python3.9/json/__init__.py", line 346, in loads
    return _default_decoder.decode(s)
  File "/usr/lib/python3.9/json/decoder.py", line 340, in decode
    raise JSONDecodeError("Extra data", s, end)
json.decoder.JSONDecodeError: Extra data: line 22 column 2 (char 1174)
 [2021-01-19 12:15:26]   26683 files are reading..
  • Expected behaviour
    Create a new file by adding a counter number if the file exists.
@black-fractal black-fractal self-assigned this Jan 19, 2021
@black-fractal black-fractal added the bug Something isn't working label Jan 19, 2021
@black-fractal
Copy link
Owner Author

black-fractal commented Jan 19, 2021

  • Using the following function may fix the bug!
import tempfile
import itertools as IT
import os

def uniquify(path, sep = '-'):
    def name_sequence():
        count = IT.count()
        next(count)
        yield ''
        while True:
            yield '{s}{n:d}'.format(s = sep, n = next(count))
    orig = tempfile._name_sequence
    with tempfile._once_lock:
        tempfile._name_sequence = name_sequence()
        path = os.path.normpath(path)
        dirname, basename = os.path.split(path)
        filename, ext = os.path.splitext(basename)
        fd, filename = tempfile.mkstemp(dir = dirname, prefix = filename, suffix = ext)
        tempfile._name_sequence = orig
    return filename
    ...:
print(uniquify('./json/files.txt'))

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

1 participant