Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #88487

Re: New to Programming: TypeError: coercing to Unicode: need string or buffer, list found

From Dennis Lee Bieber <wlfraed@ix.netcom.com>
Subject Re: New to Programming: TypeError: coercing to Unicode: need string or buffer, list found
Date 2015-04-03 11:33 -0400
Organization IISS Elusive Unicorn
References <6203299c-f9b2-4169-9d68-4c92e0f7b32f@googlegroups.com> <mailman.18.1427977502.12925.python-list@python.org> <57693d65-e683-4972-ac8d-97b2feace3bb@googlegroups.com> <mailman.24.1428019417.12925.python-list@python.org> <81391d9c-74a0-429a-82e6-d874057b8f9c@googlegroups.com>
Newsgroups comp.lang.python
Message-ID <mailman.34.1428075217.12925.python-list@python.org> (permalink)

Show all headers | View raw


On Thu, 2 Apr 2015 17:14:30 -0700 (PDT), Saran A
<ahlusar.ahluwalia@gmail.com> 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 <looptime>, 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/

Back to comp.lang.python | Previous | NextPrevious in thread | Next in thread | Find similar | Unroll thread


Thread

New to Programming: TypeError: coercing to Unicode: need string or buffer, list found Saran A <ahlusar.ahluwalia@gmail.com> - 2015-04-02 05:02 -0700
  Re: New to Programming: TypeError: coercing to Unicode: need string or buffer, list found Chris Angelico <rosuav@gmail.com> - 2015-04-02 23:24 +1100
    Re: New to Programming: TypeError: coercing to Unicode: need string or buffer, list found Saran A <ahlusar.ahluwalia@gmail.com> - 2015-04-02 05:46 -0700
      Re: New to Programming: TypeError: coercing to Unicode: need string or buffer, list found Chris Angelico <rosuav@gmail.com> - 2015-04-03 00:06 +1100
        Re: New to Programming: TypeError: coercing to Unicode: need string or buffer, list found Saran A <ahlusar.ahluwalia@gmail.com> - 2015-04-02 06:28 -0700
          Re: New to Programming: TypeError: coercing to Unicode: need string or buffer, list found Chris Angelico <rosuav@gmail.com> - 2015-04-03 00:57 +1100
      Re: New to Programming: TypeError: coercing to Unicode: need string or buffer, list found Dennis Lee Bieber <wlfraed@ix.netcom.com> - 2015-04-02 20:03 -0400
        Re: New to Programming: TypeError: coercing to Unicode: need string or buffer, list found Saran A <ahlusar.ahluwalia@gmail.com> - 2015-04-02 17:14 -0700
          Re: New to Programming: TypeError: coercing to Unicode: need string or buffer, list found Dennis Lee Bieber <wlfraed@ix.netcom.com> - 2015-04-03 11:33 -0400
      Re: New to Programming: TypeError: coercing to Unicode: need string or buffer, list found Chris Angelico <rosuav@gmail.com> - 2015-04-03 11:12 +1100
  Re: New to Programming: TypeError: coercing to Unicode: need string or buffer, list found Peter Otten <__peter__@web.de> - 2015-04-02 14:26 +0200
    Re: New to Programming: TypeError: coercing to Unicode: need string or buffer, list found Saran A <ahlusar.ahluwalia@gmail.com> - 2015-04-02 05:51 -0700

csiph-web