Re: compare dictionary values

2006-01-03 Thread Tim Williams (gmail)
On 30/12/05, rbt <[EMAIL PROTECTED]> wrote:
What's a good way to compare values in dictionaries? I want to find[snip]My key-values pairs are filepaths and their modify times. I want toidentify files that have been updated or added since the script last ran.

You don't need to store each file's updated time, you only need to
store the last time you ran the script and to compare each file's
modified time against that last-run time.   Dictionaries
aren't required

eg:

 #    to get files changed (added/modified) since a given date
import time, os
last_run = time.time() - (60*60*24*30*12)   # for testing, set to one year ago
changed = [x for x in  os.listdir('c:/python24') if os.path.getmtime(os.path.join('c:/python24' , x)) > last_run]
# script end

>From your previous posts, I believe you are storing details of *every*
file  within a dictionary that is pickled to your hard disk, 
then periodically creating a new dict  of *every* file and
comparing it against the original dict.   Then you must be
updating the original dict with the new times and storing it back to
disk.   The above list comprehension will give the same
result for files/dirs in a single directory,  you just need to
store the time of the last script run to disk instead.

if you are walking several directories, the principle is the same,

>>> last_run = time.time() - (60*60*24*30)  # 1 month ago
>>> for root, dirs, files in os.walk('c:/python24'):
...     for name in files:
...         if os.path.getmtime(os.path.join(root , name)) > last_run:
...             print os.path.join(root , name)
...             
c:/python24\Lib\asynchat.pyc
c:/python24\Lib\calendar.pyc
c:/python24\Lib\gzip.pyc
c:/python24\Lib\imghdr.pyc
c:/python24\Lib\SimpleHTTPServer.pyc
c:/python24\Lib\sndhdr.pyc
c:/python24\Lib\webbrowser.pyc
c:/python24\Lib\_strptime.pyc
c:/python24\Lib\email\MIMEAudio.pyc
c:/python24\Lib\email\MIMEImage.pyc
c:/python24\Lib\email\MIMEMessage.pyc
c:/python24\Lib\email\MIMEMultipart.pyc
>>>




-- 
http://mail.python.org/mailman/listinfo/python-list

Re: compare dictionary values

2005-12-30 Thread Tim Williams (gmail)
In <[EMAIL PROTECTED]>, rbt wrote:
> What's a good way to compare values in dictionaries?
Do you need to compare dictionaries, if its an option it would be
simpler/cheaper  to compare  each entry from your file
listing with entries in a single dict and act accordingly, mainly
because you will already have built your path/date pairs from your dir
listing,  adding them to a dict purely for comparison is an extra
step.

import os

new_files = [ ]
changed = [ ]

listing = os.listdir('c:/python24')
for a_file in listing:
    f_date = os.path.getmtime(os.path.join('c:/python24' , a_file))
    try
    if mydict[a_file] != f_date:
    changed.append(a_file)
    except:
    new_files.append(a_file)
    mydict[a_file] = f_date

deleted = [ key for key in mydict if not key in listing ]

for x in deleted:
    del mydict[x]

print changed
print new_files
print deleted

HTH :)






-- 
http://mail.python.org/mailman/listinfo/python-list

Re: compare dictionary values

2005-12-30 Thread rbt
Marc 'BlackJack' Rintsch wrote:
> In <[EMAIL PROTECTED]>, rbt wrote:
> 
>> What's a good way to compare values in dictionaries?
> 
> Look them up and then compare!?  ;-)
> 
>> I want to find 
>> values that have changed. I look for new keys by doing this:
>>
>> new = [k for k in file_info_cur.iterkeys() if k not in 
>> file_info_old.iterkeys()]
>>  if new == []:
>>  print new, "No new files."
>>  else:
>>  print new, "New file(s)!!!"
>>
>> My key-values pairs are filepaths and their modify times. I want to 
>> identify files that have been updated or added since the script last ran.
> 
> This looks up each `key` from the `new` dictionary and compares the value
> with the `old` one.  If it's not equal or the key is not present in `old`
> the key is appended to the `result`::
> 
>  def new_and_changed_keys(old, new):
>  result = list()
>  for (key, value) in new:
>  try:
>  if old[key] != value:
>  result.append(key)
>  except KeyError:
>  result.append(key)
>  return result
> 
> Ciao,
>   Marc 'BlackJack' Rintsch

Thanks Marc! I changed this line:

for (key, value) in new:

To this:

for (key, value) in new.iteritems():

And, it works great. Thanks again.
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: compare dictionary values

2005-12-30 Thread Marc 'BlackJack' Rintsch
In <[EMAIL PROTECTED]>, rbt wrote:

> What's a good way to compare values in dictionaries?

Look them up and then compare!?  ;-)

> I want to find 
> values that have changed. I look for new keys by doing this:
> 
> new = [k for k in file_info_cur.iterkeys() if k not in 
> file_info_old.iterkeys()]
>  if new == []:
>  print new, "No new files."
>  else:
>  print new, "New file(s)!!!"
> 
> My key-values pairs are filepaths and their modify times. I want to 
> identify files that have been updated or added since the script last ran.

This looks up each `key` from the `new` dictionary and compares the value
with the `old` one.  If it's not equal or the key is not present in `old`
the key is appended to the `result`::

 def new_and_changed_keys(old, new):
 result = list()
 for (key, value) in new:
 try:
 if old[key] != value:
 result.append(key)
 except KeyError:
 result.append(key)
 return result

Ciao,
Marc 'BlackJack' Rintsch
-- 
http://mail.python.org/mailman/listinfo/python-list


compare dictionary values

2005-12-30 Thread rbt
What's a good way to compare values in dictionaries? I want to find 
values that have changed. I look for new keys by doing this:

new = [k for k in file_info_cur.iterkeys() if k not in 
file_info_old.iterkeys()]
 if new == []:
 print new, "No new files."
 else:
 print new, "New file(s)!!!"

My key-values pairs are filepaths and their modify times. I want to 
identify files that have been updated or added since the script last ran.

Thanks,
rbt
-- 
http://mail.python.org/mailman/listinfo/python-list