[Tutor] Nested for loops, possibly?
Bob Williams
linux at barrowhillfarm.org.uk
Fri Feb 6 10:25:23 CET 2015
-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1
On 05/02/15 18:46, DaveA wrote:
> You don't need much class understanding at all for this. Something
> like:
>
> class Job: def __init__(self, retain, srcpath, suffix, syncpath,
> snappath, ...): self.retain = retain = datetime.timedelta (days =
> retain) self.srcpath = os.path.join (srcpath, suffix) self.syncpath
> = os.path.join (....
>
> Notice that in the above method, the object is called self, while
> in the loop in main it'll be called job, or whatever you use as a
> loop variable.
>
>>>
>>>>> The class would hold retain, srcpath, syncpath, snappath,
>>>>> etc. And your list would have 7 instances of that class
>>>>> currently, corresponding to your doc, pic, misc, ...
>>>>>
>>>>> That list would be THE global, replacing these 35 or so. It
>>>>> would be populated something like:
>>>>>
>>>>> def initialize (worklist=[]): worklist . append (Job (90,
>>>>> src_path, "Documents", "documents", "docsnaps") worklist
>>>>> .append (Job (90, src_path, "Pictures", ... .... return
>>>>> worklist
> (Reformatting )
>
> def initialize (worklist=[]): worklist . append (Job (90,
> src_path, "Documents", "documents", "docsnaps") worklist
> .append(Job (90, src_path, "Pictures", ... .... return worklist
>
>>>>>
>>>>> Now all the joins and globs are done in the Job
>>>>> initializer, just once.
>>>>>
>
>
>>>>>>>
>>>>>>> def main(): print("Backing up ", src_path,
>>>>>>> "/Documents\n", sep='') do_sync(doc_srcpath,
>>>>>>> doc_syncpath) create_snaps(doc_syncpath, doc_snappath)
>>>>>>> print("Documents backup completed.")
>>>>>>> expire_snaps(doc_snaplist, today, doc_retain)
>>>>>
>>>>> At this point main becomes something like
>>>>>
> def main (): jobs = initialize () for job in jobs: do_sync
> (job.srcpath, job.syncpath) create_snaps (job.syncpath,
> job.snappath) expire_snaps (job.snaplist, ...
>
Thanks, that formatted nicely, and is a nice clear explanation.
Bob
- --
Bob Williams
System: Linux 3.16.7-7-desktop
Distro: openSUSE 13.2 (x86_64) with KDE Development Platform: 4.14.3
Uptime: 06:00am up 7:55, 3 users, load average: 0.16, 0.05, 0.06
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v2
iEYEARECAAYFAlTUiIIACgkQ0Sr7eZJrmU6arwCfXABX41NotsGViaEuozzqRJG1
UUUAmwd8zbZO1uOQuCTSoyDxt/kTx/yB
=RkjX
-----END PGP SIGNATURE-----
More information about the Tutor
mailing list