Re: [Tutor] deleting elements of a dictionary

2017-05-20 Thread Mats Wichmann
On 05/19/2017 02:54 PM, Mats Wichmann wrote:
> On 05/19/2017 11:17 AM, Alan Gauld via Tutor wrote:
>> On 19/05/17 15:23, Michael C wrote:
>>> list(read_dictionary) converts the dictionary into a list right? How can
>>> you save the list as a dictionary?
>>
>> Nope, list() produces a new list object containing the
>> keys of the dictionary. In the old day(of python 2) you
>> used to get the same effect using
>>
>> for key in mydict.keys()
>>
>> but keys() now returns a funky view of the original dict
>> keys and I suspect you'd have the same problems when
>> deleting items. So Peter's list() is probably the best
>> option.
>>
> 
> Or to take another related view:
> 
> don't remove items from an iterable while iterating over it: del() is
> okay as long as you're not looping over the thing.
> 
> Dictionaries have a method for this called pop(), but to my blushes, I
> don't really have a lot of experience with it.
> 
> What I'd think of just off the bat is build a new dictionary on the fly,
> omitting the things you were trying to delete, and then if you like,
> save the new dict by the old name (which will cause the old one to have
> no references and be dropped.

Having now done a quick check, mydict.pop() is no better for this case.

Here's a simplistic sample that does work:

d = {
100:3,
200:4,
111:5,
222:5,
333:5,
500:6,
}

print "original: ", d

new = {key:value for (key,value) in d.iteritems() if value != 5}

print "new: ", new

___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Collecting output from Python scripts executed via Cron

2017-05-20 Thread Leo Silver
Thanks, I'll give this ago and explore the full logging option if I need
more control.
Regards, Leo.

On 20 May 2017 at 20:04, Cameron Simpson  wrote:

> On 19May2017 11:48, Leo Silver  wrote:
>
>> I have written a several Python scripts to collect data from external
>> sources (an email account and an sftp site).
>>
>> In development I run the scripts from IDLE or the command line and can
>> view
>> the output of various print statements in the scripts which helps me to
>> monitor progress and confirm correct operation.
>>
>> However, in production I will be running the scripts via Cron.
>>
>> Is there a recommended/ elegant way to collect this output on the fly for
>> later review/ processing.
>>
>> Previously I have written bash scripts simply redirecting the standard
>> output to a text file and emailed this back to myself but I would like to
>> do this directly within Python rather than having to wrap the Python
>> script
>> in a bash script.
>>
>
> You know that cron jobs _are_ shell commands?
>
> Is there anything wrong with either:
>
>  . your-program.py | mail -s subject-line y...@example.com
>
> Or, if you're using Vixie cron (very very common), set MAILTO in your
> crontab and let cron do the work:
>
>  MAILTO=y...@example.com
>  ... your-program.py
>
> Obviously "..." is your cron schedule values.
>
> Cheers,
> Cameron Simpson 
>
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Collecting output from Python scripts executed via Cron

2017-05-20 Thread Cameron Simpson

On 19May2017 11:48, Leo Silver  wrote:

I have written a several Python scripts to collect data from external
sources (an email account and an sftp site).

In development I run the scripts from IDLE or the command line and can view
the output of various print statements in the scripts which helps me to
monitor progress and confirm correct operation.

However, in production I will be running the scripts via Cron.

Is there a recommended/ elegant way to collect this output on the fly for
later review/ processing.

Previously I have written bash scripts simply redirecting the standard
output to a text file and emailed this back to myself but I would like to
do this directly within Python rather than having to wrap the Python script
in a bash script.


You know that cron jobs _are_ shell commands?

Is there anything wrong with either:

 . your-program.py | mail -s subject-line y...@example.com

Or, if you're using Vixie cron (very very common), set MAILTO in your crontab 
and let cron do the work:


 MAILTO=y...@example.com
 ... your-program.py

Obviously "..." is your cron schedule values.

Cheers,
Cameron Simpson 
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor