![]() Second, instead of pre-processing the file at once and printing it later, you could read it by blocks of 16 bytes and process them before going to the next block. File processingįirst off, you open the file but never close it: time to get yourself familiar with the with statement. You should at least use a set instead, but I would favor using two constants BEGIN_PRINTABLE = 33 and END_PRINTABLE = 126 as all these characters are contiguous in the ASCII table. In this file, bina is never used, lmap should be replaced by list-comprehensions at the calling point, and chunks very look like the itertools recipe grouper.Īlso, printable_ascii being a list is a poor choice knowing that it will be used for existence checking. utils.py is more common in the Python's world. Gen.py is a terrible name for a file holding general utilities functions as gen mostly associates to generate/generation. Especially if you plan on doing nothing and exit anyway… And the bare except to just print the exception… is just useless as it is the default behaviour anyway. HEX FILE VIEWER CODENothing in your code explicitly generate a SystemExit so you can drop this except clause. Lastly, I have a hard time understanding the logic behing your exceptions handling. You also fall into the bad habit of using from import * to try and avoid this complexity. There is no need for a class here, a simple function with tiny helpers should suffice.Īlso, even though separating concerns between files is a good thing for complex projects, I find it adds complexity for such small task. It's core is located withing Hexviewer._str_ with a little bit of preprocessing around. This code is way too complicated for what it is achieving. Self.ascii_chunks = chunks(self.ascii_data, 16) Self.hex_chunks = chunks(chunks(self.hex_data, 4), 4) Printable_ascii = lmap(ord, string.digits + string.ascii_letters + string.punctuation) Hex_digits_chunks = chunks(lmap(hexa, range(16)), 4) Return for i in range(0, len(arr), size)] The code is divided into 3 files, first one is general utils for the task, second is the main class and the third is the runner: How can I improve the code or considerate styling standards? Any other advice regarding the code or the functionality? However, I have used many list comprehensions and mapping to cut up times. ![]() I was pretty satisfied with the final result. My implementation was in the form of a class, with a separate main file running with argparse to allow choosing the file (runs with a demo file by default). ![]() In cases where we need to save the output, instead of just printing it on the terminal, we can use the “>” operator and specify a filename: $ od -x sample.txt > hex_dump_file 6.I have created an hex viewer in python, as a timed-challenge by friend. We can also combine the -t x1 flag with the -c flag to display each character along with its hex value: $ od -t x1 -c sample.txt 0000000 54 68 69 73 20 77 69 6c 6c 20 62 65 20 6f 75 72 Od has the following syntax: $ od.īy default, the od command displays data in octal format. We can use this command to convert executable files to a human-readable format which is useful for debugging. The od command can take in multiple files as input by combining the data in each of the files in the order we listed them in. Odor octal dump is a built-in command to convert files to different formats. ![]()
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |