Path: csiph.com!usenet.pasdenom.info!news.redatomik.org!newsfeed.xs4all.nl!newsfeed2a.news.xs4all.nl!xs4all!post.news.xs4all.nl!not-for-mail Return-Path: X-Original-To: python-list@python.org Delivered-To: python-list@mail.python.org X-Spam-Status: OK 0.000 X-Spam-Evidence: '*H*': 1.00; '*S*': 0.00; 'else:': 0.03; 'syntax': 0.04; 'argument': 0.05; 'that?': 0.05; 'true,': 0.05; '"""': 0.07; '(so': 0.07; 'none:': 0.07; 'removes': 0.07; 'sys': 0.07; 'welcome.': 0.07; 'string': 0.09; '"__main__":': 0.09; '%s"': 0.09; '__name__': 0.09; 'calls.': 0.09; 'defines': 0.09; 'differing': 0.09; 'none):': 0.09; 'os.path': 0.09; 'received:80.91': 0.09; 'received:80.91.229': 0.09; 'received:gmane.org': 0.09; 'received:list': 0.09; 'subject:string': 0.09; 'true)': 0.09; 'python': 0.11; 'def': 0.12; '"r")': 0.16; '%s\\n"': 0.16; '(true)': 0.16; '1234': 0.16; '>on': 0.16; 'as-is': 0.16; 'create:': 0.16; 'encountered,': 0.16; 'false)': 0.16; 'file"': 0.16; 'garbage': 0.16; 'loop.': 0.16; 'loops': 0.16; 'message-id:@4ax.com': 0.16; 'optional': 0.16; 'or;': 0.16; 'purported': 0.16; 'received:80.91.229.3': 0.16; 'received:plane.gmane.org': 0.16; 'responses.': 0.16; 'subject:Programming': 0.16; 'subject:Unicode': 0.16; 'true:': 0.16; 'will:': 0.16; 'written.': 0.16; '(you': 0.16; 'thursday,': 0.16; 'appropriate': 0.16; 'wrote:': 0.18; 'library': 0.18; 'pointed': 0.19; 'subject:need': 0.19; 'thu,': 0.19; 'programming': 0.22; 'import': 0.22; 'error': 0.23; 'exists': 0.24; 'finally,': 0.24; 'passes': 0.24; 'url:home': 0.24; 'pending': 0.26; 'task': 0.26; 'least': 0.26; 'defined': 0.27; 'header:X-Complaints-To:1': 0.27; 'tried': 0.27; 'function': 0.29; 'correct': 0.29; 'reporting': 0.29; 'errors': 0.30; 'subject:list': 0.30; 'lines': 0.31; 'bunch': 0.31; 'directory,': 0.31; 'fixing': 0.31; 'indentation': 0.31; 'file': 0.32; 'run': 0.32; 'text': 0.33; 'running': 0.33; 'bugs': 0.33; 'checking': 0.33; 'actual': 0.34; 'prepare': 0.35; 'test': 0.35; 'false': 0.36; 'next': 0.36; "didn't": 0.36; 'charset:us-ascii': 0.36; 'should': 0.36; 'subject:New': 0.37; 'wrong': 0.37; 'too': 0.37; 'list': 0.37; 'feedback': 0.38; 'whatever': 0.38; 'to:addr:python- list': 0.38; 'files': 0.38; 'skip:- 10': 0.38; 'rather': 0.38; 'to:addr:python.org': 0.39; 'received:org': 0.40; 'even': 0.60; 'read': 0.60; 'signal': 0.60; 'free': 0.61; 'break': 0.61; 'length': 0.61; 'full': 0.61; 'success': 0.61; 'first': 0.61; "you've": 0.63; 'name': 0.63; 'skip:n 10': 0.64; 'confirm': 0.64; 'provide': 0.64; 'more': 0.64; 'note:': 0.66; 'between': 0.67; 'apart': 0.72; '2015': 0.84; 'fin': 0.84; 'here)': 0.84; 'homework': 0.84; 'moves': 0.84; 'nominal': 0.84; 'good,': 0.91; 'subject:found': 0.93 X-Injected-Via-Gmane: http://gmane.org/ To: python-list@python.org From: Dennis Lee Bieber Subject: Re: New to Programming: TypeError: coercing to Unicode: need string or buffer, list found Date: Fri, 03 Apr 2015 11:33:23 -0400 Organization: IISS Elusive Unicorn References: <6203299c-f9b2-4169-9d68-4c92e0f7b32f@googlegroups.com> <57693d65-e683-4972-ac8d-97b2feace3bb@googlegroups.com> <81391d9c-74a0-429a-82e6-d874057b8f9c@googlegroups.com> Mime-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Transfer-Encoding: 7bit X-Gmane-NNTP-Posting-Host: adsl-108-79-220-28.dsl.klmzmi.sbcglobal.net X-Newsreader: Forte Agent 6.00/32.1186 X-No-Archive: YES X-BeenThere: python-list@python.org X-Mailman-Version: 2.1.20 Precedence: list List-Id: General discussion list for the Python programming language List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Newsgroups: comp.lang.python Message-ID: Lines: 164 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1428075217 news.xs4all.nl 2845 [2001:888:2000:d::a6]:60194 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:88487 On Thu, 2 Apr 2015 17:14:30 -0700 (PDT), Saran A declaimed the following: >On Thursday, April 2, 2015 at 8:03:53 PM UTC-4, Dennis Lee Bieber wrote: >> >> Yeesh... Did you even try running that? >> >> validate_files is not defined >> file_len is at the wrong indentation >> is syntactically garbage >> is a big time-waste (you read the file just to >> enumerate the number of lines? Why didn't you count the lines while >> checking the line lengths) >> copyFile is at the wrong indentation >> (after a bunch of word_word, why camelCase here) >> >> Correct all the edit errors and copy/paste the actual file that at >> least attempts to run. >> >> You might also want to look at os.stat, rather than using three os.path >> calls. >> -- >> Wulfraed Dennis Lee Bieber AF6VN >> wlfraed@ix.netcom.com HTTP://wlfraed.home.netcom.com/ > >@Dennis: > >Below is my full program (so far). Please feel free to tear it apart and provide me with constructive criticism. I have been programming for 8 months now and this is a huge learning experience for me. Feedback and modifications is very welcome. > You still seem to be adding more and more to this program without ever showing that you've tried to run it OR fixing the bugs that have been pointed out to you in multiple responses. The following is more help than should be given for a homework assignment: -=-=-=-=-=-=- """ PSEUDO-CODE -- DO NOT ATTEMPT TO RUN THIS AS-IS """ import sys LOOPTIME = 1234 #TBD optimize between too fast and too slow def validateFile(fid): """ Validates the contents of the provided file Currently a simplistic test for line length differing from that of the first line in the file Returns good (True) and number of lines encountered, or not good (False) and an error string reporting the first line at which the length differed """ fin = open(fid, "r") #assuming a text file good = True i = None for i, l in enumerate(fin): if i == 0: nominal = len(l) #or whatever defines "good" if len(l) != nominal: good = False i = "Mismatch length at line %s" % i break fin.close() if i is None: good = False i = "Empty file" return good, i #i is error message OR number of lines def mainLoop(spoolDir, successDir, failureDir): """ Main processing loop for the monitor task Obtains a pending list of the current contents of the spool directory, then enters processing loop. In the loop, it delays for , to allow for any active files in the pending list to be completely written. It then: Obtains the current list of the directory contents Removes from the current list any name found in the pending list Loops over the names in the pending list and, for each that is NOT a directory, passes it to the validation function. Based on the result of the validation function it then moves the file from the spool directory to the appropriate success or failure directory Finally, it moves the current list into the pending list to prepare for the next cycle of the loop """ pending = listdir(spoolDir) while True: sleep(LOOPTIME) current = [ fid for fid in listdir(spoolDir) if fid not in pending ] for fid in pending: fpath = join(spoolDir, fid) if isfile(fpath): v, s = validateFile(fpath) if v: move(fpath, join(successDir, fid)) sys.stdout.write("Processed %s with %s records\n" % (fpath, s)) else: move(fpath, join(failureDir, fid)) sys.stdout.write("Rejected %s for %s\n" % (fpath, s)) pending = current def checkDir(dir, create=False): """ Given a purported directory path this function will: normalize the path confirm the path exists and is a directory or; if the optional create argument is true, create the directory If returns the normalized path if a valid directory is found/created, otherwise it returns None as an error signal """ dir = normalize(dir) if not exists(dir): if create: makedir(dir) else: dir = None else: if not isdir(dir: dir = None return dir if __name__ == "__main__": if len(sys.srgv) == 3: spoolDir = checkDir(sys.argv[0], False) successDir = checkDir(sys.argv[1], True) failureDir = checkDir(sys.argv[2], True) if (spoolDir is None or successDir is None or failureDir is None): sys.stderr.write("Unable to access one or more directories\n") else: mainLoop(spoolDir, successDir, failureDir) else: sys.stderr.write("USAGE: monitor.py spool-directory " "success-directory failure-directory\n") -=-=-=-=-=-=- Any function that is NOT DEFINED in that listing exists in one or more modules of the standard library -- recommend you read that document to locate the correct one. NOTE: above is Python 2 syntax -- Wulfraed Dennis Lee Bieber AF6VN wlfraed@ix.netcom.com HTTP://wlfraed.home.netcom.com/