Note that there are some explanatory texts on larger screens.

plurals
  1. POModule organization, inheritance, and @classmethods
    primarykey
    data
    text
    <p>I'm trying to write a class that works kind of like the builtins and some of the other "grown-up" Python stuff I've seen. My Pythonic education is a little spotty, classes-wise, and I'm worried I've got it all mixed up. </p> <p>I'd like to create a class that serves as a kind of repository, containing a dictionary of unprocessed files (and their names), and a dictionary of processed files (and their names). I'd like to implement some other (sub?)classes that handle things like opening and processing the files. The file handling classes should be able to update the dictionaries in the main class. I'd also like to be able to directly call the various submodules without having to separately instantiate everything, e.g.:</p> <pre><code>import Pythia p = Pythia() p.FileManager.addFile("/path/to/some/file") </code></pre> <p>or even</p> <pre><code>Pythia.FileManager.addFile("/path/to/some/file") </code></pre> <p>I've been looking around at stuff about <code>@classmethod</code> and <code>super</code> and such, but I can't say I entirely understand it. I'm also beginning to suspect that I might have the whole chain of inheritance backwards--that what I think of as my main class should actually be the child class of the handling and processing classes. I'm also wondering whether this would all work better as a package, but that's a separate, very intimidating issue.</p> <p>Here's my code so far:</p> <pre><code>#!/usr/bin/python import re import os class Pythia(object): def __init__(self): self.raw_files = {} self.parsed_files = {} self.FileManger = FileManager() def listf(self,fname,f): if fname in self.raw_files.keys(): _isRaw = "raw" elif fname in self.parsed_files.keys(): _isRaw = "parsed" else: return "Error: invalid file" print "{} ({}):{}...".format(fname,_isRaw,f[:100]) def listRaw(self,n=None): max = n or len(self.raw_files.items()) for item in self.raw_files.items()[:max]: listf(item[0],item[1]) def listParsed(self,n=None): max = n or len(self.parsed_files.items()) for item in self.parsed_files.items()[:max]: listf(item[0],item[1]) class FileManager(Pythia): def __init__(self): pass def addFile(self,f,name=None,recurse=True,*args): if name: fname = name else: fname = ".".join(os.path.basename(f).split(".")[:-1]) if os.path.exists(f): if not os.path.isdir(f): with open(f) as fil: Pythia.raw_files[fname] = fil.read() else: print "{} seems to be a directory.".format(f) if recurse == False: return "Stopping..." elif recurse == True: print "Recursively navingating directory {}".format(f) addFiles(dir,*args) else: recurse = raw_input("Recursively navigate through directory {}? (Y/n)".format(f)) if recurse[0].lower() == "n": return "Stopping..." else: addFiles(dir,*args) else: print "Error: file or directory not found at {}".format(f) def addFiles(self,directory=None,*args): if directory: self._recursivelyOpen(directory) def argHandler(arg): if isinstance(arg,str): self._recursivelyOpen(arg) elif isinstance(arg,tuple): self.addFile(arg[0],arg[1]) else: print "Warning: {} is not a valid argument...skipping..." pass for arg in args: if not isinstance(arg,(str,dict)): if len(arg) &gt; 2: for subArg in arg: argHandler(subArg) else: argHandler(arg) elif isinstance(arg,dict): for item in arg.items(): argHandler(item) else: argHandler(arg) def _recursivelyOpen(self,f): if os.path.isdir(f): l = [os.path.join(f,x) for x in os.listdir(f) if x[0] != "."] for x in l: _recursivelyOpen(x) else: addFile(f) </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload