Re: [Tutor] Need help on Setup.py

2011-02-01 Thread Alan Gauld


"Victor Binns"  wrote

I have been trying to add some of the extras with python
such as pywin32 and others.
when i click on setup.py it does not work. I need help on the 
problem.


"It does not work" is pretty vague, can you give more specifics?
How are you running it? What error messages do you get?
What is the end state - is anything installed and if so where?

Also, is there a reason you are using setup.py for pywin32?
That is normally installed as a binary installer. See here:

http://sourceforge.net/projects/pywin32/files/pywin32/Build%20214/

There is an exe file for most Python versions...

Although I just noticed that a zip file including the source seems
to be the default option if you just hit the big geen button...

HTH,


--
Alan Gauld
Author of the Learn to Program web site
http://www.alan-g.me.uk/


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


[Tutor] Formatting a string

2011-02-01 Thread Becky Mcquilling
Quick question to the group to solve an immediate problem and then if anyone
has a dead simple reference on formatting strings it would be greatly
appreciated as I'm finding this to be pretty confusing.

Basically, I need to format a string as an example:

"He is {what}.format("{wild}")

I want to insert wild in place of what and output the resulting text WITH
the curly braces.  This is not the actual code but is the idea of what I
need to do.

Thanks all,

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


Re: [Tutor] Need Help in installing MySQLdb-Python

2011-02-01 Thread Tommy Kaas
 

 

Fra: tutor-bounces+tommy.kaas=kaasogmulvad...@python.org
[mailto:tutor-bounces+tommy.kaas=kaasogmulvad...@python.org] På vegne af
Victor Binns
Sendt: 1. februar 2011 01:19
Til: Tutor python
Emne: [Tutor] Need Help in installing MySQLdb-Python

 

I have Python 2.6.3
 
I went to the following link http://pypi.python.org/pypi/
Found - file (MySQL-python 1.2.3)
 




 
I had similar problems last week. The windows installer file placed at this
site helped. You just have to pick the right version.

 
http://www.lfd.uci.edu/~gohlke/pythonlibs/

 

Tommy

 

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


Re: [Tutor] Formatting a string

2011-02-01 Thread Karim


Hello,

>>> "He is {what}".format(what="{wild}")
'He is {wild}'

Regards
Karim

On 02/01/2011 09:44 AM, Becky Mcquilling wrote:
Quick question to the group to solve an immediate problem and then if 
anyone has a dead simple reference on formatting strings it would be 
greatly appreciated as I'm finding this to be pretty confusing.


Basically, I need to format a string as an example:

"

I want to insert wild in place of what and output the resulting text 
WITH the curly braces.  This is not the actual code but is the idea of 
what I need to do.


Thanks all,

Becky


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


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


Re: [Tutor] decimal module and precision

2011-02-01 Thread col speed
You can always change the precision in decimal. Just an idea

On 31 January 2011 22:23, Richard D. Moores  wrote:

>
> Which is accurate to only 16 digits; my Windows Vista calculator gives
> 2.9231329473018093516404474158812 for 23.45**.34
>
> And using mpmath with Python 2.6 does exactly as poorly:
> >>> from mpmath import mp, mpf
> >>> mp.dps=32;mp.pretty=True
> >>> mpf(23.45)**mpf(.34)
> 2.9231329473018095467750783681372
>
> So it seems I shouldn't use d(), or my revised d(), or mpmath for
> maximum precision, which seems to be at least 32 when using the
> decimal module the standard way.
>
> > It would probably be a good idea, if the Python compiler would issue a
> warning
> > when it encounters a `float` constant with excessive precision.
>
> Well, it sure would have helped me!
>
> Thanks, Eike.
>
> Dick
> ___
> Tutor maillist  -  Tutor@python.org
> To unsubscribe or change subscription options:
> http://mail.python.org/mailman/listinfo/tutor
>



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


Re: [Tutor] Formatting a string

2011-02-01 Thread col speed
You're missing a "." that if your computer is the same as mine, looks like
something left behind by a mosquito

On 1 February 2011 18:33, Karim  wrote:

>
> Hello,
>
> >>> "He is {what}".format(what="{wild}")
> 'He is {wild}'
>
> Regards
> Karim
>
>
> On 02/01/2011 09:44 AM, Becky Mcquilling wrote:
>
> Quick question to the group to solve an immediate problem and then if
> anyone has a dead simple reference on formatting strings it would be greatly
> appreciated as I'm finding this to be pretty confusing.
>
> Basically, I need to format a string as an example:
>
>  "
>
>  I want to insert wild in place of what and output the resulting text WITH
> the curly braces.  This is not the actual code but is the idea of what I
> need to do.
>
>  Thanks all,
>
>  Becky
>
>
> ___
> Tutor maillist  -  Tutor@python.org
> To unsubscribe or change subscription 
> options:http://mail.python.org/mailman/listinfo/tutor
>
>
>
> ___
> Tutor maillist  -  Tutor@python.org
> To unsubscribe or change subscription options:
> http://mail.python.org/mailman/listinfo/tutor
>
>


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


Re: [Tutor] Formatting a string

2011-02-01 Thread Karim


Complete test copy & paste:

karim@Requiem4Dream:~$ python
Python 2.7.1rc1 (r271rc1:86455, Nov 16 2010, 21:53:40)
[GCC 4.4.3] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> "He is {what}".format(what="{wild}")
'He is {wild}'
>>>

I don't get the missing "."   ?!

Regards
Karim

On 02/01/2011 01:35 PM, col speed wrote:
You're missing a "." that if your computer is the same as mine, looks 
like something left behind by a mosquito


On 1 February 2011 18:33, Karim > wrote:



Hello,

>>> "He is {what}".format(what="{wild}")
'He is {wild}'

Regards
Karim


On 02/01/2011 09:44 AM, Becky Mcquilling wrote:

Quick question to the group to solve an immediate problem and
then if anyone has a dead simple reference on formatting strings
it would be greatly appreciated as I'm finding this to be pretty
confusing.

Basically, I need to format a string as an example:

"

I want to insert wild in place of what and output the resulting
text WITH the curly braces.  This is not the actual code but is
the idea of what I need to do.

Thanks all,

Becky


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



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




--




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


Re: [Tutor] Need help on Setup.py

2011-02-01 Thread Walter Prins
Hi Victor,

On 1 February 2011 07:38, Victor Binns  wrote:

>  I have been trying to add some of the extras with python such as pywin32
> and others.
>
> when i click on setup.py it does not work.
>
> I need help on the problem.
>
>
This is because setup.py is not intended to just be run without
paramters/arguments.  This is explained in the readme, which states:

"'setup.py' is a standard distutils build script.  You probably want to:

% setup.py install
or
% setup.py --help

As for Python itself, these extensions require MSVC7 for Python 2.4 and
later, otherwise MSVC6.  Some extensions require a recent "Platform SDK"
from Microsoft, and in general, the latest service packs should be
installed, but run 'setup.py' without any arguments to see
specific information about dependencies.  A vanilla MSVC installation should

be able to build most extensions and list any extensions that could not be
built due to missing libraries - if the build actually fails with your
configuration, please log a bug via
http://sourceforge.net/projects/pywin32.@
"

Notice, this implies that pywin32 includes C modules..., which means that to
install from pywin32 from sources you'll also need a C compiler (Microsoft
Visual Studio I guess.)  This is, I strongly suspect, not what you intended
at all, so for PyWin32 you really want to just download a Windows .exe
installer package and install that, as Alan suggested.

In general, if you're installing pure Python packages (e.g. no C modules),
"setup.py install" is a perfectly acceptable way to install such
packages/modules.  If there's native Windows intsaller packages available
that's also of course fine (and probably easier for Windows users due to
being more familiar.)

Regards,

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


Re: [Tutor] Formatting a string

2011-02-01 Thread Alan Gauld

"Becky Mcquilling"  wrote


Basically, I need to format a string as an example:

"He is {what}.format("{wild}")

I want to insert wild in place of what and output the resulting text 
WITH

the curly braces.



what = 'wild'
"Here is {what}".format(what=what)

'Here is wild'

"Here is {what}".format(what='wild')

'Here is wild'

"Here is {{what}}".format(what='wild')

'Here is {what}'

"Here is {what}".format(what='{wild}')

'Here is {wild}'




Take your pick :-)

--
Alan Gauld
Author of the Learn to Program web site
http://www.alan-g.me.uk/


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


Re: [Tutor] Formatting a string

2011-02-01 Thread Becky Mcquilling
Thanks, as always.  It all works.

Becky

On Tue, Feb 1, 2011 at 7:08 AM, Alan Gauld wrote:

> "Becky Mcquilling"  wrote
>
>
>  Basically, I need to format a string as an example:
>>
>> "He is {what}.format("{wild}")
>>
>> I want to insert wild in place of what and output the resulting text WITH
>> the curly braces.
>>
>
>  what = 'wild'
 "Here is {what}".format(what=what)

>>> 'Here is wild'
>
>> "Here is {what}".format(what='wild')

>>> 'Here is wild'
>
>> "Here is {{what}}".format(what='wild')

>>> 'Here is {what}'
>
>> "Here is {what}".format(what='{wild}')

>>> 'Here is {wild}'
>
>>

> Take your pick :-)
>
> --
> Alan Gauld
> Author of the Learn to Program web site
> http://www.alan-g.me.uk/
>
>
>
> ___
> Tutor maillist  -  Tutor@python.org
> To unsubscribe or change subscription options:
> http://mail.python.org/mailman/listinfo/tutor
>
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


[Tutor] tkinter nested class question

2011-02-01 Thread Elwin Estle
...at least I think it would be nested.

Anyway, I have several "widget groups" that I want to create inside a frame, 
which is inside a class.  I started to just do them with copying and pasting, 
and changing the values for each one (the first one is commented out, I left it 
in so you could see what I am after), but thought maybe I could make a class 
for one widget group.

...and it works, sort of.

When I call the widget group class inside the main class, it shows up, but 
appears to be using the root window instead of the frame, which doesn't 
surprise me, since there is no connection between the widget group and the 
frame I want them to appear in.  But the question is, how to make that 
connection?

Here is the code.  I want "self.rev_stuff" to show up inside 
self.ck_header_frame.  Am I right in guessing that it has to do either with the 
Header_item class being an object, or I need to do something in its def 
__init__ ?

from Tkinter import *

class Header_item(object):

def __init__(self, lab_text = '', lab_width = 5, ent_width = 5, grid_row = 
1, grid_col = 1):
self.container = Frame()
self.info_label = Label(self.container, text = lab_text, width = 
lab_width)
self.info_ent = Entry(self.container, width = ent_width)
self.info_label.pack(side = 'left')
self.info_ent.pack(side = 'left')
self.container.grid(row = grid_row, column = grid_col)
   

class Checksheet(Frame):

def __init__(self, master, rows):

rows += 2

self.ck_header_frame = Frame()
self.ck_header_frame.grid(row = 1, column = 1, padx = 5, pady = 5)

self.feature_block = Frame()
self.feature_block.grid(row = 2, column = 1, padx = 5, pady = 5)

"""
self.f_rev_frame = Frame(self.ck_header_frame)  
self.f_rev_lab = Label(self.ck_header_frame, text = 'FORM REV:', width 
= 12)
self.f_rev_ent = Entry(self.ck_header_frame, width = 12)
self.f_rev_lab.pack(side = 'left')
self.f_rev_ent.pack(side = 'left')
self.f_rev_frame.grid(row = 1, column = 2)
"""
self.rev_stuff = Header_item(lab_text = 'FORM REV:',
 lab_width = 12,
 ent_width = 12,
 grid_row = 1,
 grid_col = 2)

features_header = []
features_header.append(('NO.', 5))
features_header.append(('CofC', 5))
features_header.append(('FEATURE DESCRIPTION', 50))
features_header.append(('FREQ.', 12))
features_header.append(('GAGES', 12))

column_count = 1

self.feat_hdr = dict()
self.feat_num = dict()
self.feat_class = dict()
self.feat_desc = dict()
self.feat_freq = dict()
self.feat_gages = dict()

for _column, item in enumerate(features_header):
_column += 1
col_name, _width = item
self.feat_hdr[_column] = Label(self.feature_block, text = col_name, 
width = _width, relief = 'groove')
self.feat_hdr[_column].grid(row = 1, column = _column)

for col_name, _width in features_header:
if col_name == 'NO.':
for i in range(2, rows):
self.feat_num[i] = Entry(self.feature_block, width = 
_width, relief = 'sunken')
self.feat_num[i].grid(row = i, column = 1)
elif col_name == 'CofC':
for i in range(2, rows):
self.feat_class[i] = Entry(self.feature_block, width = 
_width, relief = 'sunken')
self.feat_class[i].grid(row = i, column = 2)
elif col_name == 'FEATURE DESCRIPTION':
for i in range(2, rows):
self.feat_desc[i] = Entry(self.feature_block, width = 
_width, relief = 'sunken')
self.feat_desc[i].grid(row = i, column = 3)
elif col_name == 'FREQ.':
for i in range(2, rows):
self.feat_freq[i] = Entry(self.feature_block, width = 
_width, relief = 'sunken')
self.feat_freq[i].grid(row = i, column = 4)
elif col_name == 'GAGES':
for i in range(2, rows):
self.feat_gages[i] = Entry(self.feature_block, width = 
_width, relief = 'sunken')
self.feat_gages[i].grid(row = i, column = 5)


root = Tk()
checksheet = Checksheet(root, 5)
root.mainloop()


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


[Tutor] Alternate button hit problem in tkinter

2011-02-01 Thread ANKUR AGGARWAL
Hey
I am developing a zero-cross game from the basics of the tkinter. I want
something like this-

We have 4 buttons like 1 2 3 4
i want the output should change on the alternate button hit. Like inorder i
hit-
Button  return
1  1
3   0
2   1
4   0

Basically i want to change the output of the button alternatively. like for
example take this scenario-

Button   return
2 1
4  0
1  1
3   0

It depends on the user which button it hits first but i want the output to
be different for alternate button pick.
i tried to think upon this very much but unable to finally come up with any
idea. Cn anyone suggest me a way out.
Thanks in Advance.
Ankur Aggarwal
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Alternate button hit problem in tkinter

2011-02-01 Thread Wayne Werner
On Tue, Feb 1, 2011 at 3:24 PM, ANKUR AGGARWAL wrote:

> Hey
> I am developing a zero-cross game from the basics of the tkinter. I want
> something like this-
>
> We have 4 buttons like 1 2 3 4
> i want the output should change on the alternate button hit. Like inorder i
> hit-
> Button  return
> 1  1
> 3   0
> 2   1
> 4   0
>
> Basically i want to change the output of the button alternatively. like for
> example take this scenario-
>
> Button   return
> 2 1
> 4  0
> 1  1
> 3   0
>
> It depends on the user which button it hits first but i want the output to
> be different for alternate button pick.
> i tried to think upon this very much but unable to finally come up with any
> idea. Cn anyone suggest me a way out.
> Thanks in Advance.
> Ankur Aggarwal
>

def one_or_zero():
x = 0
while True:
x = not x
yield x

Something like that?
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


[Tutor] Composing lists from both items and other lists

2011-02-01 Thread John Simon
I'm looking for a way to flatten lists inside a list literal, kind of like
this:

>>> start = '('
>>> end = ')'
>>> items = ['abc', '+', 'def']
>>> [start, *items, end]
['(', 'abc', '+', 'def', ')']

Of course, the star doesn't work there. Is there any easy,
syntactically-lightweight way to get that output?

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


[Tutor] Help with range of months spanning across years

2011-02-01 Thread Sean Carolan
I have a function that accepts four arguments, namely startmonth,
startyear, endmonth, and endyear.  For example:

startmonth = 8
startyear = 2009
endmonth = 1
endyear = 2010

What would be the most straightforward way to create a list of
year/month pairs from start to end?  I want to end up with a list of
tuples like this:

mylist = [(2009, 8), (2009, 9), (2009, 10), (2009, 11), (2009, 12), (2010, 1)]
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Composing lists from both items and other lists

2011-02-01 Thread Corey Richardson
On 02/01/2011 03:40 PM, John Simon wrote:
> I'm looking for a way to flatten lists inside a list literal, kind of like
> this:
> 
 start = '('
 end = ')'
 items = ['abc', '+', 'def']
 [start, *items, end]
> ['(', 'abc', '+', 'def', ')']
> 
> Of course, the star doesn't work there. Is there any easy,
> syntactically-lightweight way to get that output?
> 
> Thanks,
> John
> 

Look into list comprehensions:

http://docs.python.org/tutorial/datastructures.html#list-comprehensions

You could use:
[start, [item for item in items], end]

But then you will have a nested list, which probably doesn't lend itself
to what you want. I myself don't know how to get rid of that, maybe some
of the gurus on this list know how.
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Composing lists from both items and other lists

2011-02-01 Thread Andre Engels
On Tue, Feb 1, 2011 at 9:40 PM, John Simon  wrote:
> I'm looking for a way to flatten lists inside a list literal, kind of like
> this:
>
 start = '('
 end = ')'
 items = ['abc', '+', 'def']
 [start, *items, end]
> ['(', 'abc', '+', 'def', ')']
> Of course, the star doesn't work there. Is there any easy,
> syntactically-lightweight way to get that output?

Is:

[start] + items + [end]

lightweight enough?


-- 
André Engels, andreeng...@gmail.com
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Composing lists from both items and other lists

2011-02-01 Thread Corey Richardson
On 02/01/2011 06:23 PM, Andre Engels wrote:
> On Tue, Feb 1, 2011 at 9:40 PM, John Simon  wrote:
>> I'm looking for a way to flatten lists inside a list literal, kind of like
>> this:
>>
> start = '('
> end = ')'
> items = ['abc', '+', 'def']
> [start, *items, end]
>> ['(', 'abc', '+', 'def', ')']
>> Of course, the star doesn't work there. Is there any easy,
>> syntactically-lightweight way to get that output?
> 
> Is:
> 
> [start] + items + [end]
> 
> lightweight enough?
> 

That's what I was looking for. The simplest things sometimes go right
over my head... Ignore my other post, but list comprehensions are a
useful thing to know.

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


Re: [Tutor] Help with range of months spanning across years

2011-02-01 Thread Hugo Arts
On Wed, Feb 2, 2011 at 12:19 AM, Sean Carolan  wrote:
> I have a function that accepts four arguments, namely startmonth,
> startyear, endmonth, and endyear.  For example:
>
> startmonth = 8
> startyear = 2009
> endmonth = 1
> endyear = 2010
>
> What would be the most straightforward way to create a list of
> year/month pairs from start to end?  I want to end up with a list of
> tuples like this:
>
> mylist = [(2009, 8), (2009, 9), (2009, 10), (2009, 11), (2009, 12), (2010, 1)]
>

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.

That said, you can do this rather straightforwardly with two nested
for loops and the range() or xrange(), one for the year and one for
the month. The only problem here is that endmonth < startmonth in some
cases, like the example you gave above. In that case, you can adjust
endmonth so the range function will give the right amount of months,
then correct in the final output to get the right numbers.

Go and code something up, try stuff out, and come back and post what
you get. We'll give you tips on how to improve on it.

Hugo

P.S.: There is also a solution using the itertools package that is
possibly more succinct, but also less clear. You can investigate if
you'd like, see the documentation of the itertools.product function.
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Composing lists from both items and other lists

2011-02-01 Thread John Simon
Andre Engels  gmail.com> writes
> Is:
>
> [start] + items + [end]
>
> lightweight enough?

Oh man, duh. I knew it was something simple. Thanks :)
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] tkinter nested class question

2011-02-01 Thread Alan Gauld


"Elwin Estle"  wrote


from Tkinter import *

class Header_item(object):

   def __init__(self, lab_text = '', lab_width = 5, ent_width = 5, 
grid_row = 1, grid_col = 1):

   self.container = Frame()


You haven't specified a parent object for the Frame.
I suspect the default parent will be the root object...


class Checksheet(Frame):
   def __init__(self, master, rows):
   rows += 2
   self.ck_header_frame = Frame()


And again here

Also, although you inherit from Frame you do not call
its init function leaving the parent object un initialised
- thats never a good idea in a GUI framework(nor in
any framework for that matter)

Remember that its the oparent argument that connects all the
widgets together so that the event passing etc works properly.
Creating parentless widgets is asking for problems and odd behaviour.

HTH,


--
Alan Gauld
Author of the Learn to Program web site
http://www.alan-g.me.uk/


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


Re: [Tutor] Alternate button hit problem in tkinter

2011-02-01 Thread Alan Gauld


"Wayne Werner"  wrote 


def one_or_zero():
   x = 0
   while True:
   x = not x
   yield x


In case its not obvious how this iis used in a GUI context...

So for your exampler crweate a state variable somewhere 
in your GUI (x in Waynes example) and toggle its value 
from your button handler and return the result.


The easiest way to do that is to use a single function, 
like Wayne's, and call it from all of your handlers.


HTH

--
Alan Gauld
Author of the Learn to Program web site
http://www.alan-g.me.uk/


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


Re: [Tutor] Help with range of months spanning across years

2011-02-01 Thread Alan Gauld


"Hugo Arts"  wrote


What would be the most straightforward way to create a list of
year/month pairs from start to end? I want to end up with a list of
tuples like this:

mylist = [(2009, 8), (2009, 9), (2009, 10), (2009, 11), (2009, 12), 
(2010, 1)]



That said, you can do this rather straightforwardly with two nested
for loops and the range() or xrange(), one for the year and one for
the month.


Or if you want to be able to use it across historic times
(taking account of calendar irregularities etc) then you
can get the data from the datetime module... It just
depends how open ended your lists need to be. For
months and years you are probably ok with a simple
loop approach as Hugo suggests, but if you ever need
to go to days or weeks then datetime would be safer..

HTH,

--
Alan Gauld
Author of the Learn to Program web site
http://www.alan-g.me.uk/


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


Re: [Tutor] Help with range of months spanning across years

2011-02-01 Thread Elwin Estle
--- On Tue, 2/1/11, Sean Carolan  wrote:

> From: Sean Carolan 
> Subject: [Tutor] Help with range of months spanning across years
> To: Tutor@python.org
> Date: Tuesday, February 1, 2011, 6:19 PM
> I have a function that accepts four
> arguments, namely startmonth,
> startyear, endmonth, and endyear.  For example:
> 
> startmonth = 8
> startyear = 2009
> endmonth = 1
> endyear = 2010
> 
> What would be the most straightforward way to create a list
> of
> year/month pairs from start to end?  I want to end up
> with a list of
> tuples like this:
> 
> mylist = [(2009, 8), (2009, 9), (2009, 10), (2009, 11),
> (2009, 12), (2010, 1)]


--- On Tue, 2/1/11, Sean Carolan  wrote:

> From: Sean Carolan 
> Subject: [Tutor] Help with range of months spanning across years
> To: Tutor@python.org
> Date: Tuesday, February 1, 2011, 6:19 PM
> I have a function that accepts four
> arguments, namely startmonth,
> startyear, endmonth, and endyear.  For example:
> 
> startmonth = 8
> startyear = 2009
> endmonth = 1
> endyear = 2010
> 
> What would be the most straightforward way to create a list
> of
> year/month pairs from start to end?  I want to end up
> with a list of
> tuples like this:
> 
> mylist = [(2009, 8), (2009, 9), (2009, 10), (2009, 11),
> (2009, 12), (2010, 1)]


Well, I noticed someone else's reply to this, about it potentially being 
homework, so I won't post any code.

I tinkered with your problem and have a solution, but I didn't use nested for 
loops as someone else suggested.  My solution works, so long as the end year 
isn't the same as the start year, tho with a bit of fiddling, one could add 
some conditional code to take care of that.

I would suggest thinking about the situation with the first year/month pair in 
the date range, and the last year/date...vs any years in the middle.  What is 
different?  What is the same?


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


Re: [Tutor] Help with range of months spanning across years

2011-02-01 Thread ian douglas
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.


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.



On 02/01/2011 05:14 PM, Elwin Estle wrote:

--- On Tue, 2/1/11, Sean Carolan  wrote:


What would be the most straightforward way to create a list
of
year/month pairs from start to end?  I want to end up
with a list of
tuples like this:

mylist = [(2009, 8), (2009, 9), (2009, 10), (2009, 11),
(2009, 12), (2010, 1)]
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Help with range of months spanning across years

2011-02-01 Thread Sean Carolan
> 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.

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

if startyear > endyear or (startyear == endyear and startmonth > endmonth):
print 'Your start date must be earlier than the end date.'
else:
datelist = []
month = startmonth
year = startyear
while month != endmonth or year != endyear:
datelist.append((year, month))
if month == 12:
month = 1
year += 1
else:
month += 1
datelist.append((year, month))
print datelist

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?
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Help with range of months spanning across years

2011-02-01 Thread Hugo Arts
On Wed, Feb 2, 2011 at 2:30 AM, ian douglas  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 )

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
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Help with range of months spanning across years

2011-02-01 Thread Hugo Arts
On Wed, Feb 2, 2011 at 2:55 AM, Sean Carolan  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
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Help with range of months spanning across years

2011-02-01 Thread Sean Carolan
> 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.
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] decimal module and precision

2011-02-01 Thread Richard D. Moores
On Tue, Feb 1, 2011 at 04:29, col speed  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:

.

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
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor