[Tutor] Nested for loops, possibly?
DaveA
davea at davea.name
Thu Feb 5 19:46:08 CET 2015
On February 5, 2015 12:33:56 PM EST, Bob Williams <linux at barrowhillfarm.org.uk> wrote:
>-----BEGIN PGP SIGNED MESSAGE-----
>Hash: SHA1
>
>On 05/02/15 14:59, DaveA wrote:
>>
>>
Sorry my last message was in html, and indentation trashed . I just installed k9 on my tablet and hadn't yet found the setting for text-only. Let m know if this one is also messed up.
>>
>> I'd put these Values into a class, and make a list of instances. If
>> you're not yet comfortable with that, it would also be possible to
>> make a list per "paragraph", and use zip to combine them, as
>> already suggested.
>>
>I also felt that there was something wrong with that long list of
>variables. However, I'm new to classes, so I'll need to do some
>background reading. Thank you for the pointer.
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, ...
--
Sent from my Android device with K-9 Mail. Please excuse my brevity.
More information about the Tutor
mailing list