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


Groups > comp.lang.python > #88460

Re: Strategy/ Advice for How to Best Attack this Problem?

Date 2015-04-02 17:10 -0400
From Dave Angel <davea@davea.name>
Subject Re: Strategy/ Advice for How to Best Attack this Problem?
References (5 earlier) <0623f75c-bf93-4a0f-9d87-86986185cdc3@googlegroups.com> <mailman.371.1427807964.10327.python-list@python.org> <fcd8bbbc-2d81-4797-aa0e-090683e72f3f@googlegroups.com> <mailman.16.1427932335.12925.python-list@python.org> <ba29c06d-3cdc-412e-90a0-b1119a360570@googlegroups.com>
Newsgroups comp.lang.python
Message-ID <mailman.23.1428009062.12925.python-list@python.org> (permalink)

Show all headers | View raw


On 04/02/2015 09:06 AM, Saran A wrote:

>
> Thanks for your help on this homework assignment. I started from scratch last night. I have added some comments that will perhaps help clarify my intentions and my thought process. Thanks again.
>
> from __future__ import print_function

I'll just randomly comment on some things I see here.  You've started 
several threads, on two different forums, so it's impractical to figure 
out what's really up.


    <snip some code I'm not commenting on>
>
> #Helper Functions for the Success and Failure Folder Outcomes, respectively
>
>      def file_len(filename):

This is an indentation error, as you forgot to start at the left margin

>          with open(filename) as f:
>              for i, l in enumerate(f):
>                  pass
>              return i + 1
>
>
>      def copy_and_move_File(src, dest):

ditto

>          try:
>              shutil.rename(src, dest)

Is there a reason you don't use the move function?  rename won't work if 
the two directories aren't on the same file system.

>          # eg. src and dest are the same file
>          except shutil.Error as e:
>              print('Error: %s' % e)
>          # eg. source or destination doesn't exist
>          except IOError as e:
>              print('Error: %s' % e.strerror)
>
>
> # Call main(), with a loop that calls # validate_files(), with a sleep after each pass. Before, my present #code was assuming all filenames come directly from the commandline.  There was no actual searching #of a directory.
>
> # I am assuming that this is appropriate since I moved the earlier versions of the files.
> # I let the directory name be the argument to main, and let main do a dirlist each time through the loop,
> # and pass the corresponding list to validate_files.
>
>
> path = "/some/sample/path/"
> dirlist = os.listdir(path)
> before = dict([(f, None) for f in dirlist)
>
> #####Syntax Error?     before = dict([(f, None) for f in dirlist)
>                                               ^
> SyntaxError: invalid syntax

Look at the line in question. There's an unmatched set of brackets.  Not 
that it matters, since you don't need these 2 lines for anything.  See 
my comments on some other forum.

>
> def main(dirlist):

bad name for a directory path variable.

>      while True:
>          time.sleep(10) #time between update check

Somewhere inside this loop, you want to obtain a list of files in the 
specified directory.  And you want to do something with that list.  You 
don't have to worry about what the files were last time, because 
presumably those are gone.  Unless in an unwritten part of the spec, 
you're supposed to abort if any filename is repeated over time.


>      after = dict([(f, None) for f in dirlist)
>      added = [f for f in after if not f in before]
>      if added:
>          print('Sucessfully added new file - ready to validate')
>        ####add return statement here to pass to validate_files
> if __name__ == "__main__":
>      main()

You'll need an argument to call main()

>
>
> #check for record time and record length - logic to be written to either pass to Failure or Success folder respectively
>
> def validate_files():

Where are all the parameters to this function?

>      creation = time.ctime(os.path.getctime(added))
>      lastmod = time.ctime(os.path.getmtime(added))
>
>
>
> #Potential Additions/Substitutions  - what are the implications/consequences for this
>
> def move_to_failure_folder_and_return_error_file():
>      os.mkdir('Failure')
>      copy_and_move_File(filename, 'Failure')
>      initialize_logger('rootdir/Failure')
>      logging.error("Either this file is empty or there are no lines")
>
>
> def move_to_success_folder_and_read(f):
>      os.mkdir('Success')
>      copy_and_move_File(filename, 'Success')
>      print("Success", f)
>      return file_len()
>
> #This simply checks the file information by name------> is this needed anymore?
>
> def fileinfo(file):
>      filename = os.path.basename(f)
>      rootdir = os.path.dirname(f)
>      filesize = os.path.getsize(f)
>      return filename, rootdir, filesize
>
> if __name__ == '__main__':
>     import sys
>     validate_files(sys.argv[1:])
>
> # -- end of file
>


-- 
DaveA

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


Thread

Strategy/ Advice for How to Best Attack this Problem? Saran Ahluwalia <ahlusar.ahluwalia@gmail.com> - 2015-03-29 04:32 -0700
  Addendum to Strategy/ Advice for How to Best Attack this Problem? Saran Ahluwalia <ahlusar.ahluwalia@gmail.com> - 2015-03-29 04:37 -0700
    Re: Addendum to Strategy/ Advice for How to Best Attack this Problem? Peter Otten <__peter__@web.de> - 2015-03-29 14:33 +0200
      Re: Addendum to Strategy/ Advice for How to Best Attack this Problem? Saran A <ahlusar.ahluwalia@gmail.com> - 2015-03-29 05:41 -0700
      Re: Addendum to Strategy/ Advice for How to Best Attack this Problem? Saran A <ahlusar.ahluwalia@gmail.com> - 2015-04-01 07:17 -0700
    Re: Addendum to Strategy/ Advice for How to Best Attack this Problem? Dave Angel <davea@davea.name> - 2015-03-29 09:27 -0400
  Re: Strategy/ Advice for How to Best Attack this Problem? Dennis Lee Bieber <wlfraed@ix.netcom.com> - 2015-03-29 19:00 -0400
  Re: Strategy/ Advice for How to Best Attack this Problem? Paul Rubin <no.email@nospam.invalid> - 2015-03-29 18:08 -0700
    Re: Strategy/ Advice for How to Best Attack this Problem? Chris Angelico <rosuav@gmail.com> - 2015-03-30 13:04 +1100
      Re: Strategy/ Advice for How to Best Attack this Problem? Saran A <ahlusar.ahluwalia@gmail.com> - 2015-03-30 09:45 -0700
        Re: Strategy/ Advice for How to Best Attack this Problem? Dave Angel <davea@davea.name> - 2015-03-30 14:35 -0400
          Re: Strategy/ Advice for How to Best Attack this Problem? Saran A <ahlusar.ahluwalia@gmail.com> - 2015-03-31 04:00 -0700
            Re: Strategy/ Advice for How to Best Attack this Problem? Dave Angel <davea@davea.name> - 2015-03-31 09:19 -0400
              Re: Strategy/ Advice for How to Best Attack this Problem? Saran A <ahlusar.ahluwalia@gmail.com> - 2015-04-01 06:43 -0700
                Re: Strategy/ Advice for How to Best Attack this Problem? Dave Angel <davea@davea.name> - 2015-04-01 19:51 -0400
                Re: Strategy/ Advice for How to Best Attack this Problem? Saran A <ahlusar.ahluwalia@gmail.com> - 2015-04-02 06:06 -0700
                Re: Strategy/ Advice for How to Best Attack this Problem? Dave Angel <davea@davea.name> - 2015-04-02 17:10 -0400
                Re: Strategy/ Advice for How to Best Attack this Problem? Saran A <ahlusar.ahluwalia@gmail.com> - 2015-04-02 16:43 -0700
                Re: Strategy/ Advice for How to Best Attack this Problem? Peter Otten <__peter__@web.de> - 2015-04-03 12:45 +0200
                Re: Strategy/ Advice for How to Best Attack this Problem? Saran A <ahlusar.ahluwalia@gmail.com> - 2015-04-03 06:40 -0700
                Re: Strategy/ Advice for How to Best Attack this Problem? Dave Angel <d@davea.name> - 2015-04-03 07:59 -0400
                Re: Strategy/ Advice for How to Best Attack this Problem? Saran A <ahlusar.ahluwalia@gmail.com> - 2015-04-03 05:50 -0700
                Re: Strategy/ Advice for How to Best Attack this Problem? Dave Angel <davea@davea.name> - 2015-04-03 16:21 -0400
                Re: Strategy/ Advice for How to Best Attack this Problem? Rustom Mody <rustompmody@gmail.com> - 2015-04-03 19:16 -0700

csiph-web