On 02/02/2011 06:00 AM, tutor-requ...@python.org wrote:
Send Tutor mailing list submissions to
        tutor@python.org

To subscribe or unsubscribe via the World Wide Web, visit
        http://mail.python.org/mailman/listinfo/tutor
or, via email, send a message with subject or body 'help' to
        tutor-requ...@python.org

You can reach the person managing the list at
        tutor-ow...@python.org

When replying, please edit your Subject line so it is more specific
than "Re: Contents of Tutor digest..."


Today's Topics:

    1. Re: Help with range of months spanning across years (Hugo Arts)
    2. Re: Help with range of months spanning across years (Hugo Arts)
    3. Re: Help with range of months spanning across years (Sean Carolan)
    4. Re: decimal module and precision (Richard D. Moores)


----------------------------------------------------------------------

Message: 1
Date: Wed, 2 Feb 2011 03:55:25 +0100
From: Hugo Arts<hugo.yo...@gmail.com>
To: ian douglas<ian.doug...@iandouglas.com>
Cc: Tutor@python.org
Subject: Re: [Tutor] Help with range of months spanning across years
Message-ID:
        <aanlktinmcbaq_a5jjk8mjcdnrzczwzdsurldtsztw...@mail.gmail.com>
Content-Type: text/plain; charset=UTF-8

On Wed, Feb 2, 2011 at 2:30 AM, ian douglas<ian.doug...@iandouglas.com>  wrote:
It bugs me that so many people are quick to jump on the "we wont' do your
homework" bandwagon -- I was accused of the same thing when I posted a
question to the list myself. I've been programming professionally for many
years but learning Python in my spare time... I sent this reply to Sean
privately, but frankly I'm so annoyed at the 'homework' replies, I figured
I'd just post it here.

Okay, this has gotten rather long, but I'll post it anyway because I
think the "we won't do your homework" response is valid and there's
good reasoning behind it.

It's not just homework. The thing is, the point of this list is to
teach people how to program (in general, but in python specifically).
When someone posts a question, responding with a lump of code and a
crisp "This is how you do it" just isn't a very effective teaching
method. More of the equivalent of giving a man a fish, as in the old
saying.

Another point, mentioned in Eric Raymond's "How to ask questions the
smart way"[1], is that I generally dislike answering questions for
people who don't appear to have put in any work in solving the problem
themselves. It leaves us with little options to give pointers, and
we're stuck with the lump of code mentioned above, or a few vague
hints as to how to approach the problem.

This isn't a place that solves your coding problems for free. But I'm
happy to help you learn python. For those reasons, I *never* give out
a straight answer, especially not to someone who doesn't show their
own attempts. In those cases, I will tell them that I won't and why,
give them some hints, and encourage them to try some things on their
own and get back here, at which point we can help them further. That's
what tutoring is all about after all.

So, in short, we're not "accusing" you of trying to make us do your
homework, and we're certainly not dismissing your questions. We're
simply saying "show us what you have, and we'll give you tips. if
you're totally at a loss, try doing so-and-so, or look at this and
that documentation."

[1]: http://www.catb.org/~esr/faqs/smart-questions.html, everybody
should read this before posting to a mailing list, imho.

I'm still very junior to Python, but this seems to work for me using
recursion. I'm sure there's a much more elegant way of doing this, but like
I said, I'm still pretty new to the language.

def makelist(startmonth,startyear,endmonth,endyear):
??? mylist = []
??? if (startyear == endyear):
??? ??? for month in range (startmonth,endmonth+1):
??? ??? ??? mylist += [(startyear,month)]
??? else:
??? ??? for month in range (startmonth,13):
??? ??? ??? mylist += [(startyear,month)]
??? ??? mylist += makelist(1,startyear+1, endmonth, endyear)
??? return mylist ;

print makelist(8,2009,1,2010)

I'd love to hear any replies from the experts on the list on how to make
this more efficient in Python 'cause I'm still learning myself.

Your solution feels rather lispy to me. I really like that. I actually
had a simple iterative solution in mind when I wrote that first post,
but while trying to write it now I'm running into some complications I
hadn't considered before, mea culpa (I'm such a good armchair
programmer</sarcasm>)

In any case, you can replace the for loops with a list comprehensions,
and factor them out into a single one since they're so similar. Let me
write it out:

def datelist(startyear, startmonth, endyear, endmonth):
     em = endmonth + 1 if startyear == endyear else 13
     dates = [(startyear, m) for m in range(startmonth, em)]
     if startyear<  endyear:
         dates += datelist(startyear + 1, 1, endyear, endmonth)
     return dates

print(datelist(2008, 8, 2009, 1))
[(2008, 8), (2008, 9), (2008, 10), (2008, 11), (2008, 12), (2009, 1)]

Hugo


------------------------------

Message: 2
Date: Wed, 2 Feb 2011 04:11:52 +0100
From: Hugo Arts<hugo.yo...@gmail.com>
To: Sean Carolan<scaro...@gmail.com>
Cc: Tutor@python.org
Subject: Re: [Tutor] Help with range of months spanning across years
Message-ID:
        <aanlktimtal-__rgif4mupi2cs5l39dio_1_5+yee4...@mail.gmail.com>
Content-Type: text/plain; charset=UTF-8

On Wed, Feb 2, 2011 at 2:55 AM, Sean Carolan<scaro...@gmail.com>  wrote:
This sounds somewhat like homework. If it is, that's fine, mention it,
and we will help you. But we won't do your homework for you, so keep
that in mind.
A reasonable assumption but this is actually going in a cgi tool that
I'm using at work. ?The input comes from pull-down menus on a web
page.
I apologize for assuming homework. It's the tutor list, and I try to
focus on effective teaching more than anything. No offense was meant.

Here's what I came up with, feel free to give suggestions on how this
might be made more efficient:

[snip code]

I was hoping there was some whiz-bang function that would just iterate
through months if it was fed a start and end date. ?Can datetime or
calendar do this?
As far as I can tell from quickly going through documentation, no. At
least, not with a quick and easy function. datetime can represent the
dates just fine, and you can add days to that until you hit your end
date, but adding months is harder. timedelta can't represent a month,
which makes sense since they're not of constant length anyway. So
using datetime is not really your best option. Calendar is more about
displaying than doing calculations, making it even less useful.

You can use your code or ian's (or my modification of ian's code) to
your taste, I don't expect any measurable difference in efficiency. If
it's really important, *measure*. But I suspect it isn't, unless
you'll run it either on time spans of thousands of years or if it will
be used by thousands of people at a time.

Hugo


------------------------------

Message: 3
Date: Tue, 1 Feb 2011 22:00:29 -0600
From: Sean Carolan<scaro...@gmail.com>
To: Hugo Arts<hugo.yo...@gmail.com>
Cc: Tutor@python.org
Subject: Re: [Tutor] Help with range of months spanning across years
Message-ID:
        <aanlktimmvkc6i4sgkf0+tgx8hr9e43ahzkw_v+m+z...@mail.gmail.com>
Content-Type: text/plain; charset=ISO-8859-1

As far as I can tell from quickly going through documentation, no. At
least, not with a quick and easy function. datetime can represent the
dates just fine, and you can add days to that until you hit your end
date, but adding months is harder. timedelta can't represent a month,
which makes sense since they're not of constant length anyway. So
using datetime is not really your best option. Calendar is more about
displaying than doing calculations, making it even less useful.
Thank you, this is useful information.

You can use your code or ian's (or my modification of ian's code) to
your taste, I don't expect any measurable difference in efficiency. If
it's really important, *measure*. But I suspect it isn't, unless
you'll run it either on time spans of thousands of years or if it will
be used by thousands of people at a time.
Yes, the code runs quickly enough for our needs.  Thanks again for the
helpful suggestions.


------------------------------

Message: 4
Date: Tue, 1 Feb 2011 20:33:38 -0800
From: "Richard D. Moores"<rdmoo...@gmail.com>
To: col speed<ajarnco...@gmail.com>
Cc: Eike Welk<eike.w...@gmx.net>, tutor@python.org
Subject: Re: [Tutor] decimal module and precision
Message-ID:
        <AANLkTi=M8XhBya-OtwRMpHgmU7WGN1CqPEeas+t_jR8=@mail.gmail.com>
Content-Type: text/plain; charset=UTF-8

On Tue, Feb 1, 2011 at 04:29, col speed<ajarnco...@gmail.com>  wrote:
You can always change the precision in decimal. Just an idea....
Not exactly sure what you mean. But I just tried using decimal to get
123.2345274523452345235432452345 ** 2.3 to 300 digits:

from decimal import Decimal as D
import decimal
decimal.getcontext().prec =300
D('123.2345274523452345235432452345')**D('2.3')
Decimal('64370.1512280246915272663511041234541758816386398199132394466583175597615075198590980955633694480202503045760664137267271735342845242951082979103782026356856312125096217781701992298765824436994198599115081342290327111836807693742546891271393004992808057677786573779518236419674381269758803681315430784')
len('64370.1512280246915272663511041234541758816386398199132394466583175597615075198590980955633694480202503045760664137267271735342845242951082979103782026356856312125096217781701992298765824436994198599115081342290327111836807693742546891271393004992808057677786573779518236419674381269758803681315430784')
301
I also did this on WolframAlpha.com (except the len) and found that
decimal and WA agree exactly! (I had come to believe that setting
decimal.getcontext().prec to n meant that the result would be in n
digits, but that the accuracy would be much less, for large n.)

Here's a screen shot of the WA result:

<http://www.rcblue.com/Misc/WolframAlphaCalculationResult.gif>.

The decimal module output ends with '15430784'; The 6th line of the WA
output begins with "154307841'

I also tried the same calculation with decimal.getcontext().prec
=1840. WA's and decimal's results agreed exactly.

Dick


------------------------------

_______________________________________________
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


End of Tutor Digest, Vol 84, Issue 8
************************************

    Hello Python Collective,

I would have to agree with you Ian. Coming from an art then computer animation visual effects background, it's not until recently that it became evident to me that in order to push the potential of this medium, I would definitely have to learn to code. I think the stigma of the "homework bandwagon" comes from the very nature of coding and it's secretive and cryptic undertones, it's something very personal in the sense that, although there is usually more than 1 way to solve a problem, there's always "THE MOST" and more efficient way IMHO, that some people are just not willing to share. And, of course, this most efficient way utilizes less memory and resources. This is why I always feel apprehensive to post so I purchased many books on python, and until I've gone through them and feel competent enough to post, then I'll post, which to me does no good. Thank you for your insight, it makes me feel a little bet more human and inspires me not to give up.

Best Regards,

Nevins Duret
_______________________________________________
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor

Reply via email to