Re: [Tutor] Little problem with math module

2008-05-21 Thread Terry Carroll
> "Tiago Katcipis" <[EMAIL PROTECTED]> wrote 
> 
> > def newton_divergente(x): 
> >  return math.pow(x, 1.0/3.0)
> > 
> > but when x = -20 it returns this error
> > 
> > return math.pow(x, 1.0/3.0)
> > ValueError: math domain error
> > 
> > but why is that? is it impossible to calculate -20 ^ (1/3) ?

Can I chime in late on this one?  I'm catching up on some old email.

It's already been pointed out that a negative number cannot be raised to a
fractional power.  But as long as you're only looking to calculate roots
you should be able to calculate them unless the number is negative *and*
the root is even. For example, you cannot (without getting into complex
numbers) take the square root (an even root) of -8.  But the cube root (an 
odd root) of -8 is -2.

N**1/x is equal to -(-N**1/x), as long as the constraints above are 
met.  Relying on this, you can easily write an "nthroot" routine that 
calculates the non-complex nth root of a number, unless the number is 
negative and the root-number is even:

def nthroot(x, n):
"""returns the non-complex nth root of x"""
if x > 0:
return x**(1.0/n)
elif n%2 == 1:
return -(-x)**(1.0/n)
else:
raise ValueError, "even root cannot be calculated for negative number"

For example:

>>> nthroot(100, 2)
10.0
>>> nthroot(8, 3)
2.0
>>> nthroot(-8, 3)
-2.0
>>> nthroot(-8, 2)  # this is a no-no; negative number & even root
Traceback (most recent call last):
  File "", line 1, in 
  File "nthroots.py", line 8, in nthroot
raise ValueError, "even root cannot be calculated for negative number"
ValueError: even root cannot be calculated for negative number
>>>

Now, if you *are* willing to get into complex numbers, i.e., numbers with 
an imaginary component, you should be aware that there's no such thing as 
*the* Nth root of a number.  Instead there are N Nth roots, evenly spaced 
in a circle centered on 0+0i.

For example, the number 16 has two square roots, at 4 and -4; or more
completely 4+0i and -4+0i.

But it has 4 4th roots.  Obviously it has 2 and -2 (2+0i and -2+0i); but 
it also has 0+2i and 0-2i:

   (0+2i)*(0+2i)*(0+2i)*(0+2i) = -4 * -4 = 16
   (0-2i)*(0-2i)*(0-2i)*(0-2i) =  4 *  4 = 16

The following (barely-tested) routine should calculate all the Nth roots
of a given x, even when x is negative and N is even:

def nthroots(x, n):
"""returns a list of all nth roots of x"""
from math import pi, cos, sin
roots = []
if x >=1:
startangle = 0
else:
startangle = pi/2
firstroot = abs(x)**(1.0/n)
for i in range(0, n):
angle = (i*(2*pi)/n)+startangle
root = complex(firstroot*cos(angle), firstroot*sin(angle))
roots.append(root)
return roots

Example:

>>> nthroots(16,4)
[(2+0j), (1.2246063538223773e-016+2j), (-2+2.4492127076447545e-016j), 
(-3.6738190614671318e-016-2j)]

Apart from precision issues, those are 2+0j, 0+2j, -2+0j and 0-2j.

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


[Tutor] String Replacement question

2008-05-21 Thread Faheem
Hi all,
 How do I replace the same value multiple times without repeating the
same variable name/value repeatedly?
for ex.  

 some = 'thing' 
 print '%s %s %s %s' % (some,some,some,some)

in this case, my question is how do i replace "% (some,some,some)" with
something more concise?

thanks in advance,
Faheem
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


[Tutor] python investment scheme assistance

2008-05-21 Thread Binish Huma Qadir
Hi,

I'm doing a Investment implementation project where i need to use the
following data:
http://app.lms.unimelb.edu.au/bbcswebdav/courses/600151_2008_1/datasets/finance/asx_large.csv

I've been instructed to design an investment scheme in python.
The investment scheme i have chosen is based on investing in 3companies
with the highest dividend yield and lowest debt equity ratio.

The problem is that i dont know how to set up a python code that gives me
the top 3 companies with the above characterisics.

Your help would be much appreciated.
Thank you very much for your time.
Regards,
Binish

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


Re: [Tutor] String Replacement question

2008-05-21 Thread Wolfram Kraus

Am 21.05.2008 11:35, Faheem schrieb:

Hi all,
 How do I replace the same value multiple times without repeating the
same variable name/value repeatedly?
for ex.  

 some = 'thing' 
 print '%s %s %s %s' % (some,some,some,some)


in this case, my question is how do i replace "% (some,some,some)" with
something more concise?

thanks in advance,
Faheem


Hi!

Two possible solutions:

print "%s %s %s %s" % (tuple([some]*4))

print " ".join([some for x in range(4)])

HTH,
Wolfram

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


Re: [Tutor] String Replacement question

2008-05-21 Thread Kent Johnson
On Wed, May 21, 2008 at 5:35 AM, Faheem <[EMAIL PROTECTED]> wrote:
> Hi all,
>  How do I replace the same value multiple times without repeating the
> same variable name/value repeatedly?
> for ex.
>
>  some = 'thing'
>  print '%s %s %s %s' % (some,some,some,some)

You can use named parameters, which moves the repetition to the format string:

In [24]: print '%(some)s %(some)s %(some)s %(some)s' % (dict(some=some))
thing thing thing thing

With multiple values, a common trick is to pass vars() or locals() as
the dict, giving the format access to all defined variables:

In [27]: a=1

In [28]: b=2

In [29]: print '%(a)s %(b)s' % vars()
1 2

If you literally need to repeat as in your example, you could do this:

In [26]: print '%s %s %s %s' % (4*(some,))
thing thing thing thing

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


Re: [Tutor] String Replacement question

2008-05-21 Thread Moishy Gluck
Some other solutions might be

>>> animal = 'cat'
>>> " ".join((animal, ) * 4)
'cat cat cat cat'
>>> animal = 'cat'
>>> print " ".join((animal, ) * 4)
cat cat cat cat
>>>#If you need the string injected into another string
>>> print "My %s's name is ginger." % (" ".join((animal,) * 4))
My cat cat cat cat's name is ginger.
>>>

On Wed, May 21, 2008 at 6:36 AM, Kent Johnson <[EMAIL PROTECTED]> wrote:

> On Wed, May 21, 2008 at 5:35 AM, Faheem <[EMAIL PROTECTED]>
> wrote:
> > Hi all,
> >  How do I replace the same value multiple times without repeating the
> > same variable name/value repeatedly?
> > for ex.
> >
> >  some = 'thing'
> >  print '%s %s %s %s' % (some,some,some,some)
>
> You can use named parameters, which moves the repetition to the format
> string:
>
> In [24]: print '%(some)s %(some)s %(some)s %(some)s' % (dict(some=some))
> thing thing thing thing
>
> With multiple values, a common trick is to pass vars() or locals() as
> the dict, giving the format access to all defined variables:
>
> In [27]: a=1
>
> In [28]: b=2
>
> In [29]: print '%(a)s %(b)s' % vars()
> 1 2
>
> If you literally need to repeat as in your example, you could do this:
>
> In [26]: print '%s %s %s %s' % (4*(some,))
> thing thing thing thing
>
> Kent
> ___
> Tutor maillist  -  Tutor@python.org
> http://mail.python.org/mailman/listinfo/tutor
>
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] python investment scheme assistance

2008-05-21 Thread Kent Johnson
On Wed, May 21, 2008 at 5:35 AM, Binish Huma Qadir
<[EMAIL PROTECTED]> wrote:
> Hi,
>
> I'm doing a Investment implementation project where i need to use the
> following data:
> http://app.lms.unimelb.edu.au/bbcswebdav/courses/600151_2008_1/datasets/finance/asx_large.csv
>
> I've been instructed to design an investment scheme in python.
> The investment scheme i have chosen is based on investing in 3companies
> with the highest dividend yield and lowest debt equity ratio.

This sounds like a homework problem; we try to avoid doing homework.
We will help you with specific questions.

Do you know how to read the file? Maybe you could start with a program
that just reads and prints the csv file. The csv module can help with
that. Then compute and print the measures you need; finally pick out
the top three.

Try to write some code and ask here when you have trouble.

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


[Tutor] String Replacement Question

2008-05-21 Thread Kinuthia Muchane

st = "String"
print "%s " %st*3

String String String




Does this help?

Kinuthia...

-

Message: 6
Date: Wed, 21 May 2008 15:05:21 +0530
From: Faheem <[EMAIL PROTECTED]>
Subject: [Tutor] String Replacement question
To: tutor@python.org
Message-ID: <[EMAIL PROTECTED]>
Content-Type: text/plain; charset=US-ASCII

Hi all,
 How do I replace the same value multiple times without repeating the
same variable name/value repeatedly?
for ex.

 some = 'thing'
 print '%s %s %s %s' % (some,some,some,some)

in this case, my question is how do i replace "% (some,some,some)" with
something more concise?

thanks in advance,
Faheem

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


Re: [Tutor] String Replacement Question

2008-05-21 Thread Moishy Gluck
On Wed, May 21, 2008 at 10:29 AM, Moishy Gluck <[EMAIL PROTECTED]>
wrote:

> >>> "%s " %st*3
> 'String String String '
> ^
> If you look closely at the end of the string there is an extra space.
>
> >>> " ".join((st, )*3)
> 'String String String'
>^
> No extra space.
>
>
> On Wed, May 21, 2008 at 9:42 AM, Kinuthia Muchane <[EMAIL PROTECTED]>
> wrote:
>
>>  st = "String"
> print "%s " %st*3
>
 String String String
>>
>>>
>
>> Does this help?
>>
>> Kinuthia...
>>
>> -
>>
>> Message: 6
>> Date: Wed, 21 May 2008 15:05:21 +0530
>> From: Faheem <[EMAIL PROTECTED]>
>> Subject: [Tutor] String Replacement question
>> To: tutor@python.org
>> Message-ID: <[EMAIL PROTECTED]>
>> Content-Type: text/plain; charset=US-ASCII
>>
>> Hi all,
>>  How do I replace the same value multiple times without repeating the
>> same variable name/value repeatedly?
>> for ex.
>>
>>  some = 'thing'
>>  print '%s %s %s %s' % (some,some,some,some)
>>
>> in this case, my question is how do i replace "% (some,some,some)" with
>> something more concise?
>>
>> thanks in advance,
>> Faheem
>>
>> ___
>> Tutor maillist  -  Tutor@python.org
>> http://mail.python.org/mailman/listinfo/tutor
>>
>
>
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


[Tutor] changin two lines in a single python script

2008-05-21 Thread jatinder Singh
format string:
> 
> In [24]: print '%(some)s %(some)s %(some)s %(some)s' % (dict(some=some))
> thing thing thing thing
> 
> With multiple values, a common trick is to pass vars() or locals() as
> the dict, giving the format access to all defined variables:
> 
> In [27]: a=1
> 
> In [28]: b=2
> 
> In [29]: print '%(a)s %(b)s' % vars()
> 1 2
> 
> If you literally need to repeat as in your example, you could do this:
> 
> In [26]: print '%s %s %s %s' % (4*(some,))
> thing thing thing thing
> 
> Kent
> 
> 
> --
> 
> Message: 4
> Date: Wed, 21 May 2008 07:04:16 -0400
> From: "Moishy Gluck" <[EMAIL PROTECTED]>
> Subject: Re: [Tutor] String Replacement question
> To: tutor@python.org
> Message-ID:
>   <[EMAIL PROTECTED]>
> Content-Type: text/plain; charset="iso-8859-1"
> 
> Some other solutions might be
> 
> >>> animal = 'cat'
> >>> " ".join((animal, ) * 4)
> 'cat cat cat cat'
> >>> animal = 'cat'
> >>> print " ".join((animal, ) * 4)
> cat cat cat cat
> >>>#If you need the string injected into another string
> >>> print "My %s's name is ginger." % (" ".join((animal,) * 4))
> My cat cat cat cat's name is ginger.
> >>>
> 
> On Wed, May 21, 2008 at 6:36 AM, Kent Johnson <[EMAIL PROTECTED]> wrote:
> 
> > On Wed, May 21, 2008 at 5:35 AM, Faheem <[EMAIL PROTECTED]>
> > wrote:
> > > Hi all,
> > >  How do I replace the same value multiple times without repeating the
> > > same variable name/value repeatedly?
> > > for ex.
> > >
> > >  some = 'thing'
> > >  print '%s %s %s %s' % (some,some,some,some)
> >
> > You can use named parameters, which moves the repetition to the format
> > string:
> >
> > In [24]: print '%(some)s %(some)s %(some)s %(some)s' % (dict(some=some))
> > thing thing thing thing
> >
> > With multiple values, a common trick is to pass vars() or locals() as
> > the dict, giving the format access to all defined variables:
> >
> > In [27]: a=1
> >
> > In [28]: b=2
> >
> > In [29]: print '%(a)s %(b)s' % vars()
> > 1 2
> >
> > If you literally need to repeat as in your example, you could do this:
> >
> > In [26]: print '%s %s %s %s' % (4*(some,))
> > thing thing thing thing
> >
> > Kent
> > ___
> > Tutor maillist  -  Tutor@python.org
> > http://mail.python.org/mailman/listinfo/tutor
> >
> -- next part --
> An HTML attachment was scrubbed...
> URL: 
> <http://mail.python.org/pipermail/tutor/attachments/20080521/4ac3ef9d/attachment-0001.htm>
> 
> --
> 
> Message: 5
> Date: Wed, 21 May 2008 08:45:10 -0400
> From: "Kent Johnson" <[EMAIL PROTECTED]>
> Subject: Re: [Tutor] python investment scheme assistance
> To: "Binish Huma Qadir" <[EMAIL PROTECTED]>
> Cc: tutor@python.org
> Message-ID:
>   <[EMAIL PROTECTED]>
> Content-Type: text/plain; charset=ISO-8859-1
> 
> On Wed, May 21, 2008 at 5:35 AM, Binish Huma Qadir
> <[EMAIL PROTECTED]> wrote:
> > Hi,
> >
> > I'm doing a Investment implementation project where i need to use the
> > following data:
> > http://app.lms.unimelb.edu.au/bbcswebdav/courses/600151_2008_1/datasets/finance/asx_large.csv
> >
> > I've been instructed to design an investment scheme in python.
> > The investment scheme i have chosen is based on investing in 3companies
> > with the highest dividend yield and lowest debt equity ratio.
> 
> This sounds like a homework problem; we try to avoid doing homework.
> We will help you with specific questions.
> 
> Do you know how to read the file? Maybe you could start with a program
> that just reads and prints the csv file. The csv module can help with
> that. Then compute and print the measures you need; finally pick out
> the top three.
> 
> Try to write some code and ask here when you have trouble.
> 
> Kent
> 
> 
> --
> 
> Message: 6
> Date: Wed, 21 May 2008 16:42:11 +0300
> From: Kinuthia Muchane <[EMAIL PROTECTED]>
> Subject: [Tutor] String Replacement Question
> To: tutor@python.org
> Message-ID: <[EMAIL PROTECTED]>
> Content-Type: text/plain; charset=ISO-8859-1; format=flowed
> 
> >>> st = &q

Re: [Tutor] String Replacement Question

2008-05-21 Thread Kinuthia Muchane



Moishy Gluck wrote:

 >>> "%s " %st*3
'String String String '
^
If you look closely at the end of the string there is an extra space.




 >>> " ".join((st, )*3)
'String String String'
   ^
No extra space.

Ah...Okay!


On Wed, May 21, 2008 at 9:42 AM, Kinuthia Muchane <[EMAIL PROTECTED] 
> wrote:


st = "String"
print "%s " %st*3

String String String



Does this help?

Kinuthia...

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


[Tutor] syntax question

2008-05-21 Thread Eric Abrahamsen

Hi all,

This is a syntax that I've seen on occasion in other people's code:

theme = (VALID_THEMES[0], theme) [ theme in VALID_THEMES ]

I have no idea how this works, or how to go about looking it up. Can  
someone enlighten me as to what's happening here?


Many thanks,
Eric
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] listing classes

2008-05-21 Thread Laureano Arcanio
well it's true, using list it's scalable too. But It's doesn't looks
friendly to the user to write the document. Syntacticly  looks nice to keep
some of the original structure of the html ( I mind, put the thags inside
the document, and so on ).


I'll making some test this days to see what i get, and i keep on mind your
suggestions.

Thanks for all your feedback !
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


[Tutor] Fwd: syntax question

2008-05-21 Thread Moishy Gluck
-- Forwarded message --
From: Moishy Gluck <[EMAIL PROTECTED]>
Date: Wed, May 21, 2008 at 12:00 PM
Subject: Re: [Tutor] syntax question
To: Eric Abrahamsen <[EMAIL PROTECTED]>


You appear to be referring to list comprehension. Here is a link to list
comprehension is the python docs.

http://docs.python.org/tut/node7.html#SECTION00714

[index for index in iterable]

Some examples would be:

colors = ["red", "green", "blue"]

hexValues = {"red": "ff", "green": "00ff00", "blue": "ff"}

#Get hexadecimal values of each color.
print [hexValues[color] for color in colors]

#Get a copy of the same list.
print [color for color in colors]

#Get the length of each member.
print [len(color) for color in colors]

#add optional if statment.
print [color for color in colors if len(color) > 3]

The output would be:

['ff', '00ff00', 'ff']
['red', 'green', 'blue']
[3, 5, 4]
['green', 'blue']


On Wed, May 21, 2008 at 8:27 AM, Eric Abrahamsen <[EMAIL PROTECTED]> wrote:

> Hi all,
>
> This is a syntax that I've seen on occasion in other people's code:
>
> theme = (VALID_THEMES[0], theme) [ theme in VALID_THEMES ]
>
> I have no idea how this works, or how to go about looking it up. Can
> someone enlighten me as to what's happening here?
>
> Many thanks,
> Eric
> ___
> Tutor maillist  -  Tutor@python.org
> http://mail.python.org/mailman/listinfo/tutor
>
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] syntax question

2008-05-21 Thread Kent Johnson
On Wed, May 21, 2008 at 8:27 AM, Eric Abrahamsen <[EMAIL PROTECTED]> wrote:
> Hi all,
>
> This is a syntax that I've seen on occasion in other people's code:
>
> theme = (VALID_THEMES[0], theme) [ theme in VALID_THEMES ]
>
> I have no idea how this works, or how to go about looking it up.

This is one way to implement a conditional expression in Python < 2.5.
In general, the expression

(A, B)[bool(C)]

has the value A if bool(C) is false and B if bool(C) is true.

The boolean value is either 0 or 1. It is used as an index into the
tuple to select the desired value.

In Python 2.5 this can be written with the condition expression

B if C else A

or to use your example

theme = theme if (theme in VALID_THEMES) else VALID_THEMES[0]

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


Re: [Tutor] String Replacement Question

2008-05-21 Thread Jeff Younker

On May 21, 2008, at 6:42 AM, Kinuthia Muchane wrote:


st = "String"
print "%s " %st*3

String String String




Does this help?


Another approach is to use dictionaries for string
replacement.

>>> subst = {'some': 'thing'}
>>> print "%(some)s%(some)s%(some)s"  % subst
thingthingthing

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


Re: [Tutor] syntax question

2008-05-21 Thread Carlos Hanson
On Wed, May 21, 2008 at 5:27 AM, Eric Abrahamsen <[EMAIL PROTECTED]> wrote:
> Hi all,
>
> This is a syntax that I've seen on occasion in other people's code:
>
> theme = (VALID_THEMES[0], theme) [ theme in VALID_THEMES ]
>
> I have no idea how this works, or how to go about looking it up. Can someone
> enlighten me as to what's happening here?
>
> Many thanks,
> Eric


First, I looked at the code to see what I easily understood:

 - VALID_THEMES is a list of type something, since it has an index
applied to it:VALID_THEMES[0]
 - theme is a type something that is in the list: theme in VALID_THEMES

Next, I started playing with it based on what I knew using string as
the theme type:

--> VALID_THEMES = ['sun', 'moon', 'earth']
--> theme1 = 'moon'
--> theme2 = 'mars'
--> print (VALID_THEMES[0], theme1)
('sun', 'moon')
--> print (VALID_THEMES[0], theme2)
('sun', 'mars')
--> [theme1 in VALID_THEMES]
[True]
--> [theme2 in VALID_THEMES]
[False]
--> (VALID_THEMES[0], theme1)[theme1 in VALID_THEMES]
'moon'
--> (VALID_THEMES[0], theme2)[theme2 in VALID_THEMES]
'sun'

So what just happened? First, we created a tuple of the first
VALID_THEME and a variable theme. Then we tested the variable theme to
see if it was a VALID_THEME. The combination of the two always gives
us a valid theme.

The test if [theme in VALID_THEMES] becomes an index to the tuple:

--> (VALID_THEMES[0], theme1)[1]
'moon'
--> (VALID_THEMES[0], theme1)[0]
'sun'

But we got [True] and [False]:

--> True == 1
True
--> False == 0
True

So in words, the code returns the default theme which is the first in
the list of valid themes unless the chosen theme is also in the list
of valid themes.

My suggestion is to play with code in the interpreter that you don't understand.

-- 
Carlos Hanson
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] String Replacement Question

2008-05-21 Thread Carlos Hanson
On Wed, May 21, 2008 at 9:39 AM, Jeff Younker <[EMAIL PROTECTED]> wrote:
> On May 21, 2008, at 6:42 AM, Kinuthia Muchane wrote:
>
> st = "String"
> print "%s " %st*3
>>
>> String String String
>
>>
>> Does this help?
>
> Another approach is to use dictionaries for string
> replacement.
>
 subst = {'some': 'thing'}
 print "%(some)s%(some)s%(some)s"  % subst
> thingthingthing
>

As an alternate to repeating the formated string:

--> print '%(some)s'*3 % subst
thingthingthing

(forgot to reply all)

-- 
Carlos Hanson
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] changin two lines in a single python script

2008-05-21 Thread bob gailer
---

Message: 4
Date: Wed, 21 May 2008 07:04:16 -0400
From: "Moishy Gluck" <[EMAIL PROTECTED]>
Subject: Re: [Tutor] String Replacement question
To: tutor@python.org
Message-ID:
<[EMAIL PROTECTED]>
Content-Type: text/plain; charset="iso-8859-1"

Some other solutions might be



animal = 'cat'
" ".join((animal, ) * 4)
  

'cat cat cat cat'


animal = 'cat'
print " ".join((animal, ) * 4)
  

cat cat cat cat


#If you need the string injected into another string
print "My %s's name is ginger." % (" ".join((animal,) * 4))
  

My cat cat cat cat's name is ginger.

On Wed, May 21, 2008 at 6:36 AM, Kent Johnson <[EMAIL PROTECTED]> wrote:




On Wed, May 21, 2008 at 5:35 AM, Faheem <[EMAIL PROTECTED]>
wrote:
  

Hi all,
 How do I replace the same value multiple times without repeating the
same variable name/value repeatedly?
for ex.

 some = 'thing'
 print '%s %s %s %s' % (some,some,some,some)


You can use named parameters, which moves the repetition to the format
string:

In [24]: print '%(some)s %(some)s %(some)s %(some)s' % (dict(some=some))
thing thing thing thing

With multiple values, a common trick is to pass vars() or locals() as
the dict, giving the format access to all defined variables:

In [27]: a=1

In [28]: b=2

In [29]: print '%(a)s %(b)s' % vars()
1 2

If you literally need to repeat as in your example, you could do this:

In [26]: print '%s %s %s %s' % (4*(some,))
thing thing thing thing

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

  

-- next part --
An HTML attachment was scrubbed...
URL: 
<http://mail.python.org/pipermail/tutor/attachments/20080521/4ac3ef9d/attachment-0001.htm>

--

Message: 5
Date: Wed, 21 May 2008 08:45:10 -0400
From: "Kent Johnson" <[EMAIL PROTECTED]>
Subject: Re: [Tutor] python investment scheme assistance
To: "Binish Huma Qadir" <[EMAIL PROTECTED]>
Cc: tutor@python.org
Message-ID:
<[EMAIL PROTECTED]>
Content-Type: text/plain; charset=ISO-8859-1

On Wed, May 21, 2008 at 5:35 AM, Binish Huma Qadir
<[EMAIL PROTECTED]> wrote:


Hi,

I'm doing a Investment implementation project where i need to use the
following data:
http://app.lms.unimelb.edu.au/bbcswebdav/courses/600151_2008_1/datasets/finance/asx_large.csv

I've been instructed to design an investment scheme in python.
The investment scheme i have chosen is based on investing in 3companies
with the highest dividend yield and lowest debt equity ratio.
  

This sounds like a homework problem; we try to avoid doing homework.
We will help you with specific questions.

Do you know how to read the file? Maybe you could start with a program
that just reads and prints the csv file. The csv module can help with
that. Then compute and print the measures you need; finally pick out
the top three.

Try to write some code and ask here when you have trouble.

Kent


--

Message: 6
Date: Wed, 21 May 2008 16:42:11 +0300
From: Kinuthia Muchane <[EMAIL PROTECTED]>
Subject: [Tutor] String Replacement Question
To: tutor@python.org
Message-ID: <[EMAIL PROTECTED]>
Content-Type: text/plain; charset=ISO-8859-1; format=flowed



st = "String"
print "%s " %st*3
  

String String String

Does this help?


Kinuthia...

-

Message: 6
Date: Wed, 21 May 2008 15:05:21 +0530
From: Faheem <[EMAIL PROTECTED]>
Subject: [Tutor] String Replacement question
To: tutor@python.org
Message-ID: <[EMAIL PROTECTED]>
Content-Type: text/plain; charset=US-ASCII

Hi all,
  How do I replace the same value multiple times without repeating the
same variable name/value repeatedly?
for ex.

  some = 'thing'
  print '%s %s %s %s' % (some,some,some,some)

in this case, my question is how do i replace "% (some,some,some)" with
something more concise?

thanks in advance,
Faheem



--

Message: 7
Date: Wed, 21 May 2008 10:29:41 -0400
From: "Moishy Gluck" <[EMAIL PROTECTED]>
Subject: Re: [Tutor] String Replacement Question
To: tutor@python.org
Message-ID:
<[EMAIL PROTECTED]>
Content-Type: text/plain; charset="iso-8859-1"

On Wed, May 21, 2008 at 10:29 AM, Moishy Gluck <[EMAIL PROTECTED]>
wrote:



"%s " %st*3


'String String String '
^
If you look closely at the end of the string there is an extra space.

  

" ".join((st, )*3)


'String String String'
   ^
No extra space.


On Wed, May 21,

Re: [Tutor] Fwd: syntax question

2008-05-21 Thread Kent Johnson
On Wed, May 21, 2008 at 12:00 PM, Moishy Gluck <[EMAIL PROTECTED]> wrote:

> You appear to be referring to list comprehension.

No, it is not a list comp. See my separate reply.

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


Re: [Tutor] changing two lines in a single python script

2008-05-21 Thread bob gailer
1) When starting a new topic start a new email rather than replying to 
another unrelated post.


jatinder Singh wrote:

Hi .

I am trying to change two lines in a single file (etc/php.ini) but my
script is not doing this in one time .


What does "in one time" mean?
What IS the script doing?

Please hav a look

import re
import sys
import os

def re_new(pat, rep, s):
 print re.sub(pat, rep, s)
  

 It appears that you are using stdout to write the altered file. True?

   
_stext_1 = 'memory_limit...M'#first word to search

_rtext_1 = 'memory_limit = 512M  ;'#to be replaced with first word
_stext_2 = 'upload_max_filesize.*M'  #Second word
_rtext_2 = 'upload_max_filesize = 9M  ;' #to replace with second
word
s = open("/home/ccuser/temp1.txt").readlines()
for record in s:
re_new(_stext_1,_rtext_1,record)  #if found first then replaceit
re_new(_stext_2,_rtext_2,record)  #else if found second then
replace that

Will you please help me to correct this script so as to both of the
changes occure at a singlr time

Thanks for past, now and future

Jatin

--
Bob Gailer
919-636-4239 Chapel Hill, NC

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


Re: [Tutor] changin two lines in a single python script

2008-05-21 Thread bob gailer
---

Message: 4
Date: Wed, 21 May 2008 07:04:16 -0400
From: "Moishy Gluck" <[EMAIL PROTECTED]>
Subject: Re: [Tutor] String Replacement question
To: tutor@python.org
Message-ID:
<[EMAIL PROTECTED]>
Content-Type: text/plain; charset="iso-8859-1"

Some other solutions might be



animal = 'cat'
" ".join((animal, ) * 4)
  

'cat cat cat cat'


animal = 'cat'
print " ".join((animal, ) * 4)
  

cat cat cat cat


#If you need the string injected into another string
print "My %s's name is ginger." % (" ".join((animal,) * 4))
  

My cat cat cat cat's name is ginger.

On Wed, May 21, 2008 at 6:36 AM, Kent Johnson <[EMAIL PROTECTED]> wrote:




On Wed, May 21, 2008 at 5:35 AM, Faheem <[EMAIL PROTECTED]>
wrote:
  

Hi all,
 How do I replace the same value multiple times without repeating the
same variable name/value repeatedly?
for ex.

 some = 'thing'
 print '%s %s %s %s' % (some,some,some,some)


You can use named parameters, which moves the repetition to the format
string:

In [24]: print '%(some)s %(some)s %(some)s %(some)s' % (dict(some=some))
thing thing thing thing

With multiple values, a common trick is to pass vars() or locals() as
the dict, giving the format access to all defined variables:

In [27]: a=1

In [28]: b=2

In [29]: print '%(a)s %(b)s' % vars()
1 2

If you literally need to repeat as in your example, you could do this:

In [26]: print '%s %s %s %s' % (4*(some,))
thing thing thing thing

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

  

-- next part --
An HTML attachment was scrubbed...
URL: 
<http://mail.python.org/pipermail/tutor/attachments/20080521/4ac3ef9d/attachment-0001.htm>

--

Message: 5
Date: Wed, 21 May 2008 08:45:10 -0400
From: "Kent Johnson" <[EMAIL PROTECTED]>
Subject: Re: [Tutor] python investment scheme assistance
To: "Binish Huma Qadir" <[EMAIL PROTECTED]>
Cc: tutor@python.org
Message-ID:
<[EMAIL PROTECTED]>
Content-Type: text/plain; charset=ISO-8859-1

On Wed, May 21, 2008 at 5:35 AM, Binish Huma Qadir
<[EMAIL PROTECTED]> wrote:


Hi,

I'm doing a Investment implementation project where i need to use the
following data:
http://app.lms.unimelb.edu.au/bbcswebdav/courses/600151_2008_1/datasets/finance/asx_large.csv

I've been instructed to design an investment scheme in python.
The investment scheme i have chosen is based on investing in 3companies
with the highest dividend yield and lowest debt equity ratio.
  

This sounds like a homework problem; we try to avoid doing homework.
We will help you with specific questions.

Do you know how to read the file? Maybe you could start with a program
that just reads and prints the csv file. The csv module can help with
that. Then compute and print the measures you need; finally pick out
the top three.

Try to write some code and ask here when you have trouble.

Kent


--

Message: 6
Date: Wed, 21 May 2008 16:42:11 +0300
From: Kinuthia Muchane <[EMAIL PROTECTED]>
Subject: [Tutor] String Replacement Question
To: tutor@python.org
Message-ID: <[EMAIL PROTECTED]>
Content-Type: text/plain; charset=ISO-8859-1; format=flowed



st = "String"
print "%s " %st*3
  

String String String

Does this help?


Kinuthia...

-

Message: 6
Date: Wed, 21 May 2008 15:05:21 +0530
From: Faheem <[EMAIL PROTECTED]>
Subject: [Tutor] String Replacement question
To: tutor@python.org
Message-ID: <[EMAIL PROTECTED]>
Content-Type: text/plain; charset=US-ASCII

Hi all,
  How do I replace the same value multiple times without repeating the
same variable name/value repeatedly?
for ex.

  some = 'thing'
  print '%s %s %s %s' % (some,some,some,some)

in this case, my question is how do i replace "% (some,some,some)" with
something more concise?

thanks in advance,
Faheem



--

Message: 7
Date: Wed, 21 May 2008 10:29:41 -0400
From: "Moishy Gluck" <[EMAIL PROTECTED]>
Subject: Re: [Tutor] String Replacement Question
To: tutor@python.org
Message-ID:
<[EMAIL PROTECTED]>
Content-Type: text/plain; charset="iso-8859-1"

On Wed, May 21, 2008 at 10:29 AM, Moishy Gluck <[EMAIL PROTECTED]>
wrote:



"%s " %st*3


'String String String '
^
If you look closely at the end of the string there is an extra space.

  

" ".join((st, )*3)


'String String String'
   ^
No extra space.


On Wed, May 21,

[Tutor] Supported platform

2008-05-21 Thread Amit Kumar
Can I get list of supported platform by python?

can I install python on AS/400 and HP-Unix?

Regards
Amit
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Supported platform

2008-05-21 Thread Tom Tucker
I would check the docs on the www.python.org website.

>From the main page

"Python runs on Windows, Linux/Unix, Mac OS X, OS/2, Amiga, Palm Handhelds,
and Nokia mobile phones. Python has also been ported to the Java and .NET
virtual machines."



On Wed, May 21, 2008 at 2:11 PM, Amit Kumar <[EMAIL PROTECTED]>
wrote:

> Can I get list of supported platform by python?
>
> can I install python on AS/400 and HP-Unix?
>
> Regards
> Amit
>
> ___
> Tutor maillist  -  Tutor@python.org
> http://mail.python.org/mailman/listinfo/tutor
>
>
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] String Replacement question

2008-05-21 Thread bob gailer

Faheem wrote:

Hi all,
 How do I replace the same value multiple times without repeating the
same variable name/value repeatedly?
for ex.  

 some = 'thing' 
 print '%s %s %s %s' % (some,some,some,some)


in this case, my question is how do i replace "% (some,some,some)" with
something more concise?

  

The tersest I can offer is:
print '%s %s %s %s' % ((some,)*4)


--
Bob Gailer
919-636-4239 Chapel Hill, NC

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


Re: [Tutor] Supported platform

2008-05-21 Thread Jerry Hill
On Wed, May 21, 2008 at 2:11 PM, Amit Kumar <[EMAIL PROTECTED]> wrote:
> Can I get list of supported platform by python?
>
> can I install python on AS/400 and HP-Unix?

I don't know of any sort of official list of supported platforms.

Activestate claims to have a python distribution for HPUX:
http://www.activestate.com/Products/activepython/features.plex

There appears to be an os/400 python package here: http://www.iseriespython.com/

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


Re: [Tutor] Supported platform

2008-05-21 Thread Kent Johnson
> On Wed, May 21, 2008 at 2:11 PM, Amit Kumar <[EMAIL PROTECTED]> wrote:
>> Can I get list of supported platform by python?

See
http://www.python.org/download/
http://www.python.org/download/other

for a partial list.

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


[Tutor] File access times

2008-05-21 Thread W W
I've just made an interesting observation, and I'm curious if anyone
knows any more about it.

  1 import time
  2
  3 start = time.time()
  4 f = open("test.txt")
  5 f.close()
  6 end = time.time()
  7 print end - start
  8
  9
 10 start = time.time()
 11 f = open("/home/wayne/python_files/test/test.txt")
 12 f.close()
 13 end = time.time()
 14 print end - start

As you can see, the open/timing dialogs are precisely the same, yet as
I've run it, I get these results:


2.59876251221e-05
1.8835067749e-05

3.38554382324e-05
1.90734863281e-05

2.8133392334e-05
1.90734863281e-05


However, when I switch the order, so the full path is first, I get
similar results.

Granted, .28 vs .19 (seconds?) isn't terribly long, but it
*is* a discrepancy, and I find it even more puzzling that the second
access is faster, regardless of how detailed the path is. Could it be
that the file is stored in RAM and "remembered" even though it's been
closed?

Just curious,
Wayne


-- 
To be considered stupid and to be told so is more painful than being
called gluttonous, mendacious, violent, lascivious, lazy, cowardly:
every weakness, every vice, has found its defenders, its rhetoric, its
ennoblement and exaltation, but stupidity hasn't. - Primo Levi
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] listing classes

2008-05-21 Thread Laureano Arcanio
Finally, i decide to make this:

from metatag import MetaTag
from htmltools import TagHTML

class Structure(object):
class A(TagHTML):
pass
class B(TagHTML):
pass

def get(self):
__all__ = dir(self)
classes = filter(lambda k: type(getattr(self, k)) == type(TagHTML) ,
__all__)
methods = filter(lambda k: str(type(getattr(self, k))) == "" , __all__)
return  classes, methods


So i can Lis Methods and Classes, and then use setattr or whatever  needed.

There is a think left, i can't jus compare this:

if type(somethingA) == type(somthingB):

I transform type() to a string and then compare them..  (like in the code
below)

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


Re: [Tutor] listing classes

2008-05-21 Thread Laureano Arcanio
sorry this is not true:

There is a think left, i can't jus compare this:

if type(somethingA) == type(somthingB):

I transform type() to a string and then compare them..  (like in the code
below)

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


Re: [Tutor] File access times

2008-05-21 Thread Kent Johnson
On Wed, May 21, 2008 at 12:59 PM, W W <[EMAIL PROTECTED]> wrote:
> Could it be
> that the file is stored in RAM and "remembered" even though it's been
> closed?

Yes, the OS caches file and directory data so the second open is faster.

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


Re: [Tutor] listing classes

2008-05-21 Thread Laureano Arcanio
I'm using the dir() function, but this give me an alphabetic ordered list,
is there any way to do the same but getting an appearance ordered list ?.
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] listing classes

2008-05-21 Thread Kent Johnson
On Wed, May 21, 2008 at 7:45 PM, Laureano Arcanio
<[EMAIL PROTECTED]> wrote:
>
> I'm using the dir() function, but this give me an alphabetic ordered list,
> is there any way to do the same but getting an appearance ordered list ?.

Not easily. Attributes are stored in a dict; dicts don't preserve
order. You would have to make a custom metaclass that remembered the
order.

What is it you are trying to accomplish?

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


Re: [Tutor] Little problem with math module

2008-05-21 Thread Terry Carroll
On Wed, 21 May 2008, Terry Carroll wrote:

> The following (barely-tested) routine should calculate all the Nth roots
> of a given x, even when x is negative and N is even:

I realize I'm probably talking to myself here, but for the benefit of the 
archives, I found a more elegant approach after reading 
http://everything2.com/node/1309141.  The following routine instead uses 
the formula at the end of the page (although I must admit I needed to read 
the entire page very carefully before I actually understood it):

def nthroots(x,n):
"""returns a list of all nth roots of x;
see http://everything2.com/node/1309141 """
from math import atan2, pi
from cmath import exp
i = 0+1j
z=complex(x)
# express the cartesian (x+yi) complex number z
# as polar (modulus R & argument theta) 
R = abs(z)
theta = atan2(z.imag, z.real)
coef = R**(1.0/n)
return [coef * exp(i*(theta/n + 2*pi*k/n)) for k in range(0,n)]  

I'm not sure it's faster than the method from the prior email, but it 
certainly seems a bit less ham-handed.  It could probably be sped up, at 
the cost of some obfuscation, by pulling a few more things out of the list 
comprehension, e.g.,

X = i*theta/n
Y = (0+2j)*pi/n
return [coeff * exp(X + Y*k) for k in range(0,n)]

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