Finally the book is out! Happy to have in hands.
Samstag, 16. Juli 2016
Donnerstag, 7. Juli 2016
Python: Concate code to a single string
Using the code below, the code from a source file can be concatenated to a single string. This string however can still be printed and formates nicely on a JavaScript console.
It returns a string variable `s' and requires two command line arguments.
# Usage: # > python ~/path/to/script/multiline.py <name_of_ouput_variable>
filename = sys.argv[1] f = open(filename, "r") d = f.readlines() f.close() s = "var %s = " % sys.argv[2] s = s.strip() # s += '"' + "".join(d).replace("\n", "") + '";' cnt = 0 n = len(d) # '\\n' is explicitly written to the 'source'-file such that # the JavaScript console can print the source code including line # breaks. for line in d: # First line. if cnt == 0: s += "\n" + ' "' + d[cnt].replace("\n", "") + '\\n"\n' cnt += 1 # Inbetween lines. elif cnt > 0 and cnt < (n-1): s += '+ "' + d[cnt].replace("\n", "") + '\\n"\n' cnt += 1 # Last line. else: s += '+ "' + d[cnt].replace("\n", "") + '"'
Labels:
blockchain,
ethereum,
geth,
go-ethereum,
python,
solidity
Abonnieren
Posts (Atom)