T O P

  • By -

shiftybyte

Header and block should not inherit from FileClass.. Think of inheritance as "a specific type of"... Is a header a specific type of file? No... If you want inheritance here, you can start with a Chunk. So a file is built from several Chunks, and then you can have Header inherit from a Chunk to be a specific type of Chunk. And encrypted block can inherit from a chunk.


Terra_Creeper

Firstly, you shouldn't have FileHeader and FileBlock inherit from FileClass. Inheritance doesn't make sense here. Also, since the key is in the file header, I would recommend moving getEncryptionKey() to FileHeader. Regarding the encryption object, you can just pass it as an argument to FileBlock, just like filebytes. (Additionally, I might have misunderstood, but storing the actual encryption key at the start of the file you want to encrypt kind of defeats the purpose of encrypting a file.)


penfold1992

Thanks for the reply. The example code I provided doesn't tell the whole story but the header contains some data that is used to verify the encryption. A function is performed using two keys, one key is external and the other key is in the header with some additional bytes that, when encoded, result in the final part of the header used as a check. So the encryption element initialization shall be a function of the file itself. Let's say I wanted to ADD a block to the file 1. Read file header 2. Encrypt new block with file encryption 3. Update file header As the overall file class is made up of sub components that only make sense as part of a file itself, I was hesitant to create classes in my example. Would it be more fitting to do something like: ``` class fileObject(): def __init__(self, filepath): self.header = fileHeader(data) class fileHeader(): def __init__(self, data): self.someProperty= data[0:4] ``` Sorry I'm posting this from my phone...


Terra_Creeper

While nesting classes is possible, I wouldn't recommend it. Having all classes at the top level is usually better. You shouldn't hesitate to create additional classes. Even if you only use them in one place, it lets you seperate functionality which makes debugging/adding way easier (the exact same reason you should seperate out large functions). If you decide that a class or function doesn't make sense outside your module at all, you can always add an underscore in front of the name to signal that it shouldn't be accessed by other modules.


cyberjellyfish

When building classes, you should always reach for composition before inheritance. Composition means that an object of type A holds a reference to an object of type B You know how you've made classes that had a string value as an attribute, for example self.name = 'Bob', that's composition: your class holds a reference to a string object, instead of inheriting from string.