From 2eee7028e3102688901883505864b42b2a27af1e Mon Sep 17 00:00:00 2001 From: Hugo Trentesaux <hugo@trentesaux.fr> Date: Fri, 17 Jul 2020 00:36:54 +0200 Subject: [PATCH] [fix] formatting --- duniterpy/localblockchain.py | 53 ++++++++++++++++++++++-------------- 1 file changed, 32 insertions(+), 21 deletions(-) diff --git a/duniterpy/localblockchain.py b/duniterpy/localblockchain.py index e8b3c779..3cff1031 100644 --- a/duniterpy/localblockchain.py +++ b/duniterpy/localblockchain.py @@ -14,18 +14,25 @@ from .documents import Block CHUNK_SIZE = 250 -class JsonBlockchain(): + +class JsonBlockchain: def __init__(self, folder): - self.folder = folder # folder where chunks are stored - self.chunks = len(glob.glob(os.path.join(folder, "chunk_*"))) # number of files starting with "chunk_" - self.current_block_in_chunk = 0 # number from 0 to 249 equal to current_block // current_chunk - self.current_chunk = 0 # current chunk number - self.chunk = [] # parsed json for current chunk (length = 250) - self.parsechunk() # parse first chunk + self.folder = folder # folder where chunks are stored + self.chunks = len( + glob.glob(os.path.join(folder, "chunk_*")) + ) # number of files starting with "chunk_" + self.current_block_in_chunk = ( + 0 # number from 0 to 249 equal to current_block // current_chunk + ) + self.current_chunk = 0 # current chunk number + self.chunk = [] # parsed json for current chunk (length = 250) + self.parsechunk() # parse first chunk def parsechunk(self): """parse a json chunk file""" - with open(os.path.join(self.folder, f"chunk_{self.current_chunk}-250.json")) as f: + with open( + os.path.join(self.folder, f"chunk_{self.current_chunk}-250.json") + ) as f: s = f.read() p = json.loads(s) self.chunk = p["blocks"] @@ -35,14 +42,19 @@ class JsonBlockchain(): def __next__(self): """if current block is outside current chunk, parse next one, otherwise, return current block parsed json""" - if self.current_block_in_chunk == 250: # block outside chunk - self.current_block_in_chunk = 0 # reset to next chunk start - self.current_chunk += 1 # increment current chunk number - if self.current_chunk >= self.chunks: # outside range + if self.current_block_in_chunk == 250: # block outside chunk + self.current_block_in_chunk = 0 # reset to next chunk start + self.current_chunk += 1 # increment current chunk number + if self.current_chunk >= self.chunks: # outside range raise StopIteration() - self.parsechunk() # parse this chunk - self.current_block_in_chunk += 1 # increment current block number for next iteration - return self.chunk[self.current_block_in_chunk-1] # return block (before incrementation) + self.parsechunk() # parse this chunk + self.current_block_in_chunk += ( + 1 # increment current block number for next iteration + ) + return self.chunk[ + self.current_block_in_chunk - 1 + ] # return block (before incrementation) + def Blockchain(json_blockchain): """convert json to duniterpy block document""" @@ -50,12 +62,11 @@ def Blockchain(json_blockchain): for jb in jbc: yield Block.from_parsed_json(jb) + def load(path=".config/duniter/duniter_default/g1/"): """returns an iterator allowing to browse all the blockchain in practice, it will load chunk by chunk and only keep one in memory at a time""" - path = os.path.join(os.path.expanduser("~"), path) # expand path - jbc = JsonBlockchain(path) # gets an iterator over json blockchain - bc = Blockchain(jbc) # convert it to an iterator over blocks - return bc # returns - - + path = os.path.join(os.path.expanduser("~"), path) # expand path + jbc = JsonBlockchain(path) # gets an iterator over json blockchain + bc = Blockchain(jbc) # convert it to an iterator over blocks + return bc # returns -- GitLab