PDA

View Full Version : Python or C++ Code example


crmpicco
06-28-2006, 09:52 PM
I have this piece of code that has been handed to me.

My question is can anyone tell me what language it is written in? Is it Python? or C++?? I think it is something along those lines?

Also, if anyone can quickly tell me roughly what the code is doing that would be EXCELLENT!
:thumbsup:


from __future__ import nested_scopes
import os, time

def watch_directories (paths, func, delay=0.2):


all_files = {}
def f (unused, dirname, files):
for filename in files:
path = os.path.join(dirname, filename)

try:
t = os.stat(path)
except os.error:
# A file has been deleted between os.path.walk()
continue

mtime = remaining_files.get(path)
if mtime:
# Record this file as having been seen
del remaining_files[path]
# File's mtime has been changed since we last looked at it.
if t.st_mtime > mtime:
changed_list.append(path)
else:
# No recorded modification time, so it must be
# a brand new file.
changed_list.append(path)
all_files[path] = t.st_mtime

# Main loop
rescan = False
while True: #scan forever
changed_list = []
remaining_files = all_files.copy()
all_files = {}
for path in paths:
os.path.walk(path, f, None)
removed_list = remaining_files.keys()
if rescan:
rescan = False
elif changed_list or removed_list:
rescan = func(changed_list, removed_list)
print 'Changed Files', changed_list
print 'Removed', removed_list
time.sleep(delay)


import os
if __name__ == '__main__':
def f (changed_files, removed_files):
os.nice(10)
print 'Changed Files', changed_files
print 'Removed', removed_files
while True:
try:
watch_directories('/home/crmpicco/', f, 1)
##watch_directories('.', f, 1)
except KeyboardInterrupt:
print "I cant be stopped (unless you try a lot)"


Many Thanks,
Picco

rpgfan3233
06-28-2006, 11:54 PM
It seems to be Python.

crmpicco
06-29-2006, 01:36 AM
from __future__ import nested_scopes
import os, time


thanks rpgfan3233, what is the code above doing? Thanks for noticing it as Python. Do you have a good knowledge of Python? Is it Server-Side/Client-Side?

crmpicco
06-29-2006, 08:13 PM
are there any ways it can be enhanced? or made more reliable?

two-pi
07-03-2006, 07:52 PM
No question this is python code.

It appears to be looking at a directory and noting which files have recently been changed or updated. Of course, the great thing about python is you can try it yourself. It appears this program was written anticipating unix/linux (by the way the test directory is structured. If you've got access to a linux box, you already have python. Just save this file to your local file system, change the directory name to a temporary directory you've got some dummy stuff in (in case it breaks things) and run it : python foo.py.