Re: [Tutor] get aws path from argParser

2016-01-08 Thread Alan Gauld
On 08/01/16 14:04, sebastian cheung via Tutor wrote:
> * take an s3 destination path as an argument optionally containing the string 
> ++DATE++ as a placeholder (e.g. s3://my-bucket/objects/++DATE++/, 
> s3://my-bucket/objects/++DATE++/file-++DATE++.txt and s3://my-bucket/objects/ 
> should all be valid)
> I already have something for something more simple, but for s3 maybe use 
> awscli etc? Thanks Seb

I have no idea what you are asking about (other than I
assume its something related to AWS?). This is the python
tutor list for answering questions about the Python language
and its standard library. Did you mean to post here?

If so you need to give us a bit more information about
what you are doing and what exactly you want help with.

> def dateType(string):
> """
> Convert a date string to a date object
> """
> try:
> date = datetime.datetime.strptime(string, '%Y-%m-%d').date()
> except ValueError:
> msg = "%r is not a valid date" % string
> raise argparse.ArgumentTypeError(msg)
> return date
> 
> 
> def is_valid_file(parser, arg):
> if not os.path.exists(arg):
> parser.error("The file %s does not exist!" % arg)
> else:
> return open(arg, 'r')parser = argparse.ArgumentParser(
> description="Take CLI options called start-date and end-date, which 
> must be formatted -MM-DD. "
> "These should default to today if not supplied",
> epilog="See http://bitbucket.org/niceseb/ for details about the 
> Project Time Tracker.")
> parser.add_argument('-e', '--end-date', metavar='DATE', type=dateType, 
> default=datetime.date.today(),
> help='the date tracking data should start at, inclusive 
> in the format -MM-DD (defaults to today)')
> parser.add_argument('-s', '--start-date', metavar='DATE', type=dateType, 
> default=datetime.date.today(),
> help='the date tracking data should end at,   inclusive 
> in the format -MM-DD (defaults to today)')
> parser.add_argument('-v', action='version', version='%(prog)s 1.0')
> parser.add_argument('-i', dest="filename", required=False, help="input file 
> name", metavar="FILE",
> type=lambda x: is_valid_file(parser, x))


-- 
Alan G
Author of the Learn to Program web site
http://www.alan-g.me.uk/
http://www.amazon.com/author/alan_gauld
Follow my photo-blog on Flickr at:
http://www.flickr.com/photos/alangauldphotos


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


[Tutor] idle??

2016-01-08 Thread bruce
Hey guys/gals - list readers

Recently came across someone here mentioning IDLE!! -- not knowing
this. I hit google for a look.

Is IDLE essentially an ide for doing py dev? I see there's a
windows/linux (rpms) for it.

I'm running py.. I normally do $$python to pop up the py env for quick
tests.. and of course run my test scripts/apps from the cmdline via
./foo.py...

So, where does IDLE fit into this

Thanks

(and yeah, I know I could continue to look at google, and even install
the rpms to really check it out!!)

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


Re: [Tutor] idle??

2016-01-08 Thread Alan Gauld
On 09/01/16 00:11, bruce wrote:

> So, as an IDE/shell.. I assume it's not quite Eclipse, butallows you
> to do reasonable editing/snyax tracking/etc.. as well as run apps
> within the window/shell.. I assume breakpoints as well, and a good
> chunk of the rest of the usual IDE functions...

Exactly.

> What about function completion? Where I type a function.. and it
> displays a "list" of potential function/defs ? 

Yes.

> Does it provide "function" or item hoovering. where cursor 
> can be placed of a function/item and
> information about the func, or item
> (type/struct/etc..) is displayed?

I don't think so but there is a primitive class browser.

Also idleX has something called a code context that shows
things like the method definition within its class.

To be honest I don't use IDLE that much any more and if I do
its idleX. Mostly I just use vim and a command shell and
for debugging winpdb (in the rare cases where print and
the >>> prompt are insufficient.)

The other important thing is that it is all written in
Python, and Tkinter as the GUI, so its a good source of
sample code.

But the easiest thing is just install the package and
have a play! :-)

-- 
Alan G
Author of the Learn to Program web site
http://www.alan-g.me.uk/
http://www.amazon.com/author/alan_gauld
Follow my photo-blog on Flickr at:
http://www.flickr.com/photos/alangauldphotos


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


Re: [Tutor] idle??

2016-01-08 Thread bruce
Thanks Alan...

So, as an IDE/shell.. I assume it's not quite Eclipse, butallows you
to do reasonable editing/snyax tracking/etc.. as well as run apps
within the window/shell.. I assume breakpoints as well, and a good
chunk of the rest of the usual IDE functions...

What about function completion? Where I type a function.. and it
displays a "list" of potential function/defs ? Does it provide
"function" or item hoovering. where cursor can be placed of a
function/item and information about the func, or item
(type/struct/etc..) is displayed?

Thanks again' much appreciated!!




On Fri, Jan 8, 2016 at 6:42 PM, Alan Gauld  wrote:
> On 08/01/16 19:07, bruce wrote:
>
>> Is IDLE essentially an ide for doing py dev? I see there's a
>> windows/linux (rpms) for it.
>
> Yes, its the official IDE for Python.
>
> There is an "unofficial" version called xidle which tends
> to get a lot of the new stuff before it makes it into the
> official release. For a long time not much happened with
> IDLE but recently there has been a bunch of activity so
> I'm hopeful we may soon see some new features appearing.
>
>> So, where does IDLE fit into this
>
> It incorporates a shell window where you can type commands
> and you can create blank editor windows(with syntax
> highlighting etc etc) from which you can save files,
> run them, debug them etc.
>
> There are some YouTube and ShowMeDo videos around and
> Danny Yoo has a short tutorial that is quite old but
> still pretty much applicable.
>
> There is official documentation on the python.org
> website too.
>
> Finally, it's not universally loved and definitely has
> some quirks but it's adequate for getting started,
> definitely better than notepad, say, on Windows.
>
> --
> Alan G
> Author of the Learn to Program web site
> http://www.alan-g.me.uk/
> http://www.amazon.com/author/alan_gauld
> Follow my photo-blog on Flickr at:
> http://www.flickr.com/photos/alangauldphotos
>
>
> ___
> Tutor maillist  -  Tutor@python.org
> To unsubscribe or change subscription options:
> https://mail.python.org/mailman/listinfo/tutor
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] idle??

2016-01-08 Thread Alan Gauld
On 08/01/16 19:07, bruce wrote:

> Is IDLE essentially an ide for doing py dev? I see there's a
> windows/linux (rpms) for it.

Yes, its the official IDE for Python.

There is an "unofficial" version called xidle which tends
to get a lot of the new stuff before it makes it into the
official release. For a long time not much happened with
IDLE but recently there has been a bunch of activity so
I'm hopeful we may soon see some new features appearing.

> So, where does IDLE fit into this

It incorporates a shell window where you can type commands
and you can create blank editor windows(with syntax
highlighting etc etc) from which you can save files,
run them, debug them etc.

There are some YouTube and ShowMeDo videos around and
Danny Yoo has a short tutorial that is quite old but
still pretty much applicable.

There is official documentation on the python.org
website too.

Finally, it's not universally loved and definitely has
some quirks but it's adequate for getting started,
definitely better than notepad, say, on Windows.

-- 
Alan G
Author of the Learn to Program web site
http://www.alan-g.me.uk/
http://www.amazon.com/author/alan_gauld
Follow my photo-blog on Flickr at:
http://www.flickr.com/photos/alangauldphotos


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


Re: [Tutor] method, type?

2016-01-08 Thread Alan Gauld
On 08/01/16 04:24, Steven D'Aprano wrote:

> I maintain that "constructor" for the most part has to be understood of 
> a statement of intention, not a hard definition. Apart from __new__ 
> itself, which genuinely is special, "constructor" in a language like 
> Python refers to the intention of creating new instances, as opposed to 
> "doing some work".

I think the question here is whether we are talking about the
specifics of how Python does things or about the meaning of
the terms in the wider OOP community. I'd argue that in wider OOP
circles constructor is a much more specific term but in Python
it is more about intent. (The init case is a good example where
we generally, and sloppily, refer to init as a constructor when
in fact its only an initialiser)

>From the point of view of the tutor list we have a slight dilemma
because we are here to teach both Python the language as well as
wider programming skills.

A thread like this exposes where Python's specific way of doing
things is at odds with the wider practice and terminology and
probably confuses noobies (sorry folks) but at the same time
throws up a lot of interesting details about just how Python
does its particular brand of OOP magic.

> class MyClass:
> def __new__(cls):
> return "Surprise!"
> 
> Surprising though this is, it is actually allowed,

And again this is one of the differences between Python and
the vast majority of OOP languages.  In most cases constructors
are not allowed to explicitly return anything because the language
implicitly returns an instance (or occasionally throws an
exception). But that's because those languages have a very
specific meaning for constructor, often with different syntax
or idioms from normal methods. Whereas, as you've pointed
out, Python's construction mechanism is much more open and
consistent making the meaning of 'constructor' more loosely
defined.

> (1) __new__ is the default constructor;
> 
> (2) __init__ is the initialiser, but sometimes we're lazy and call it 
> the constructor;
> 
> (3) If you see a CLASS method called "from_foo" or similar, which 
> returns an instance of the class, that's probably an alternate 
> (non-default) constructor;
> 
> (4) But we usually don't count methods which take an instance of the 
> class, and transform or copy them in some way as "constructors" even if 
> they do in fact construct a new instance.

Yep. I'll buy that for when we are talking about Python
In the wider OOP world there are more definite distinctions
in the terms.

The good news is that for working programmers most of
these distinctions are subtle enough, or theoretical enough,
not to matter. You create objects by either
a) calling an established default constructor protocol or
b) by invoking a class method or factory function.

Coming back to the original theme of this thread it looks like
we are coming to the conclusion that, at least when applied
to objects, factory methods/functions can also be called
constructors. (And from my foray into the "named constructor"
idiom of C++ those guys are allowing such latitude in terminology
too.) So the only place where confusion would exist is in those
languages (such as objective C or Object Pascal) where constructor
or 'factory method' have a specific syntax and meaning in the
language.

-- 
Alan G
Author of the Learn to Program web site
http://www.alan-g.me.uk/
http://www.amazon.com/author/alan_gauld
Follow my photo-blog on Flickr at:
http://www.flickr.com/photos/alangauldphotos


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


[Tutor] new line to list of strings send by email

2016-01-08 Thread Emil Natan
Hello list,

I have a function which receives a string and sends it as a body of an
email.

It is a part of a program which does certain checks on network
infrastructure. When a check fails I append error message to a
error_collector list:


if self.check_axfr_refused(ip):
 error_collector.append('%s:%s AXFR test for %s FAILED' %
   (ns, ip, self.domainname))

At the  end I send the alert like this:

if len(error_collector) != 0:
email_body = str(error_collector)
email_alert(email_body)

The problem is the resulted email (expectedly) has the alert message as one
long string.

['pdns6.ultradns.co.uk.:204.74.115.1 AXFR test for amazon.com FAILED',
'pdns6.ultradns.co.uk.:2610:a1:1017::1 AXFR test for amazon.com FAILED',
'ns4.p31.dynect.net.:204.13.251.31 AXFR test for amazon.com FAILED',...]

I tried adding '\n' to end of each string error_collector collects, but
then these were simply added to the resulted email.

What I want to achieve is that each collected error is shown on a separate
line in the email. Any advice will be well appreciated.

Here is the email sending function if in interest:

def email_alert(message, recipient=DEFAULT_RECIPIENT, subject_prefix=''):
''' Send email alert. '''
# check if we are running in quiet mode
if QUIET.lower() == 'yes':
return
msg = MIMEText(message)
msg['From'] = SENDER
msg['To'] = recipient
msg['Subject'] = subject_prefix + SUBJECT

s = smtplib.SMTP(SMTP_SERVER)
s.send_message(msg)
s.quit()

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


Re: [Tutor] new line to list of strings send by email

2016-01-08 Thread Bob Gailer
On Jan 8, 2016 11:03 AM, "Emil Natan"  wrote:
>
> Hello list,
>
> I have a function which receives a string and sends it as a body of an
> email.
>
> It is a part of a program which does certain checks on network
> infrastructure. When a check fails I append error message to a
> error_collector list:
>
>
> if self.check_axfr_refused(ip):
>  error_collector.append('%s:%s AXFR test for %s FAILED' %
>(ns, ip, self.domainname))
>
> At the  end I send the alert like this:
>
> if len(error_collector) != 0:
> email_body = str(error_collector)
> email_alert(email_body)
Instead of str( str(error_collector) ) try '\n'.join(error_collector)
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor


[Tutor] get aws path from argParser

2016-01-08 Thread sebastian cheung via Tutor
* take an s3 destination path as an argument optionally containing the string 
++DATE++ as a placeholder (e.g. s3://my-bucket/objects/++DATE++/, 
s3://my-bucket/objects/++DATE++/file-++DATE++.txt and s3://my-bucket/objects/ 
should all be valid)
I already have something for something more simple, but for s3 maybe use awscli 
etc? Thanks Seb
def dateType(string):
"""
Convert a date string to a date object
"""
try:
date = datetime.datetime.strptime(string, '%Y-%m-%d').date()
except ValueError:
msg = "%r is not a valid date" % string
raise argparse.ArgumentTypeError(msg)
return date


def is_valid_file(parser, arg):
if not os.path.exists(arg):
parser.error("The file %s does not exist!" % arg)
else:
return open(arg, 'r')parser = argparse.ArgumentParser(
description="Take CLI options called start-date and end-date, which 
must be formatted -MM-DD. "
"These should default to today if not supplied",
epilog="See http://bitbucket.org/niceseb/ for details about the Project 
Time Tracker.")
parser.add_argument('-e', '--end-date', metavar='DATE', type=dateType, 
default=datetime.date.today(),
help='the date tracking data should start at, inclusive in 
the format -MM-DD (defaults to today)')
parser.add_argument('-s', '--start-date', metavar='DATE', type=dateType, 
default=datetime.date.today(),
help='the date tracking data should end at,   inclusive in 
the format -MM-DD (defaults to today)')
parser.add_argument('-v', action='version', version='%(prog)s 1.0')
parser.add_argument('-i', dest="filename", required=False, help="input file 
name", metavar="FILE",
type=lambda x: is_valid_file(parser, x))
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor