Re: [Tutor] How to convert seconds to hh:mm:ss format

2012-02-16 Thread Alan Gauld

On 17/02/12 01:29, Daryl V wrote:


  def Int2Digit(integer):
 if integer < 9:
 strInteger = "0"+str(integer)
 else:
 strInteger = str(integer)
 return strInteger


Or using format strings:

def int2Digit(integer):
return "%02d" % integer

Although this doesn't behave quite right for negative numbers.
But I suspect the function  above's handling of negatives is not 
strictly as intended either...


--
Alan G
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] finding a maximum between the absolute difference of several columns

2012-02-16 Thread Andre' Walker-Loud
Hi Elaina,

> Vhel_fdiff3=[]
> for i in xrange(len(Vmatch3_1)):
> Vhel_fdiff3.append(max([abs(Vmatch3_1[i] - Vmatch3_2[i]),abs(Vmatch3_1[i] 
> - Vmatch3_3[i]),abs(Vmatch3_3[i] - Vmatch3_2[i])]))
> 
> #print Vhel_fdiff3
> #appending writes a list, but numpy which makes the with_ work needs an array 
> Vhel_fdiff3=array(Vhel_fdiff3)

You can skip making the list and directly make a numpy array.

"""
import numpy as np
...
Vhel_fdiff3 = np.zeros_like(Vmatch3_1)
for i in xrange(len(Vmatch3_1):
Vhel_fdiff3[i] = max(abs(Vmatch3_1[i] - Vmatch3_2[i]),abs(Vmatch3_1[i] 
- Vmatch3_3[i]),abs(Vmatch3_3[i] - Vmatch3_2[i]))
"""

But, more importantly, you can do it all in numpy - have a read about the "max" 
function

http://scipy.org/Numpy_Example_List#head-7918c09eea00e59ec399064e7d5e1e672d242f60

Given an 2-dimensional array, you can find the max for each row or each column 
by specifying which axis you want to compare against.  Almost surely, the built 
in numpy function will be faster than your loop - even if you precompile it by 
importing your functions in another file.  You can do something like

"""
import numpy as np
Vhel_fdiff3 = np.array([abs(Vmatch3_1 - Vmatch3_2), abs(Vmatch3_1 - Vmatch3_3), 
abs(Vmatch3_3 - Vmatch3_2)]) #create an array of the absolute values of the 
differences of each pair of data
# Vhel_fdiff3 is a 2D array
# the first index runs over the three pairs of differences
# the second index runs over the elements of each pair
# to get the maximum difference, you want to compare over the first axis (0)
your_answer = Vhel_fdiff3.max(axis=0)
"""

but you may not want the abs value of the differences, but to actually keep the 
differences, depending on which one has the maximum abs difference - that would 
require a little more coding.


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


Re: [Tutor] finding a maximum between the absolute difference of several columns

2012-02-16 Thread Elaina Ann Hyde
On Fri, Feb 17, 2012 at 2:27 PM, Mark Lawrence wrote:

> On 17/02/2012 02:53, Elaina Ann Hyde wrote:
>
>> On Fri, Feb 17, 2012 at 1:41 PM, Rohan Sachdeva  wrote:
>>
>>  The way I would do this is to put all of the differences in a list then
>>> take the maximum of the list. So..
>>>
>>> a = Vmatch3_1 - Vmatch3_2
>>> b = Vmatch3_1 - Vmatch3_3
>>> c = Vmatch3_3 - Vmatch3_2
>>>
>>> Vhel_fdiff3=max([a,b,c])
>>>
>>> That should work. a,b,c are pretty bad variable names...
>>>
>>> Rohan
>>>
>>> On Thu, Feb 16, 2012 at 5:04 PM, Elaina Ann Hyde**
>>> wrote:
>>>
>>>  Hello all,
I am still scripting away and have reached my next quandry, this one
 is much simpler than the last, basically I read in a file with several
 columns.
 Vmatch3_1=dat[col1]
 Vmatch3_2=dat[col2]
 Vmatch3_3=dat[col3]
 Vdav=5.0
 Vhel_fdiff3=max(abs(Vmatch3_1 - Vmatch3_2),abs(Vmatch3_1 -
 Vmatch3_3),abs(Vmatch3_3 - Vmatch3_2))
 
 What I would like this to return is the maximum difference in each case,
 so I end up with one column which contains only the largest differences.
 now I use this to write the condition:

 with_v1_3=(Vhel_fdiff3>= Vdav)

 I know the condition works and plots if

  Vhel_fdiff3=(Vmatch3_1 - Vmatch3_2)

 for example, and I know this syntax would work if it was numbers instead
 of columns.

> max(abs(1-2),abs(3-7),abs(2-4)**)
>> 4
>>
>
 The error is:
 ---
 Traceback (most recent call last):
   File "double_plot.py", line 109, in
 Vhel_fdiff3=max(abs(Vmatch3_1 - Vmatch3_2),abs(Vmatch3_1 -
 Vmatch3_3),abs(Vmatch3_3 - Vmatch3_2))
 ValueError: The truth value of an array with more than one element is
 ambiguous. Use a.any() or a.all()
 -
 So it can't handle the fact that it's columns of numbers and not single
 numbers, so my question is, can I solve this without doing a loop around
 it... use numpy, or some other function instead?
 I've been searching around and haven't found any good ways forward so I
 thought one of you might know.  Thanks
 ~Elaina

 --
 PhD Candidate
 Department of Physics and Astronomy
 Faculty of Science
 Macquarie University
 North Ryde, NSW 2109, Australia

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



>>>
>> --
>> Thanks for the replies so far.  I don't think that Rohan's idea solves the
>> numbers versus columns issue.  If I run it I get
>> ---
>> Traceback (most recent call last):
>>   File "double_plot.py", line 112, in
>> Vhel_fdiff3=max(a,b,c)
>> ValueError: The truth value of an array with more than one element is
>> ambiguous. Use a.any() or a.all()
>> -
>> which is just the same error, I looked at the forums and the link
>> suggested
>> and I guess maybe my problem is trickier than I first thought!
>> ~Elaina
>>
>> __**_
>> Tutor maillist  -  Tutor@python.org
>> To unsubscribe or change subscription options:
>> http://mail.python.org/**mailman/listinfo/tutor
>>
>
> I've found this 
> http://comments.gmane.org/**gmane.comp.python.tutor/72882
> .
>
> HTH.
>
> --
> Cheers.
>
> Mark Lawrence.
>
>
> __**_
> Tutor maillist  -  Tutor@python.org
> To unsubscribe or change subscription options:
> http://mail.python.org/**mailman/listinfo/tutor
>




Interesting, thanks for all the ideas everyone I did finally get it to
work in a loop form.  Anyone wanting to save themselves lots of confusion,
firstly use .append, and secondly don't forget to convert created lists
back to arrays for numpy, here is the working code to look through columns,
match them, return and make an array to operate on with numpy:
-
Vhel_fdiff3=[]
for i in xrange(len(Vmatch3_1)):
Vhel_fdiff3.append(max([abs(Vmatch3_1[i] -
Vmatch3_2[i]),abs(Vmatch3_1[i] - Vmatch3_3[i]),abs(Vmatch3_3[i] -
Vmatch3_2[i])]))

#print Vhel_fdiff3
#appending writes a list, but numpy which makes the with_ work needs an
array
Vhel_fdiff3=array(Vhel_fdiff3)
-
~Elaina
-- 
PhD Candidate
Department of Physics and Astronomy
Faculty of Science
Macquarie University
North Ryde, NSW 2109, Australia
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Debugging While Loops for Control

2012-02-16 Thread Luke Thomas Mergner
> 
> --
> 
> Message: 1
> Date: Wed, 15 Feb 2012 23:57:08 -0500
> From: Luke Thomas Mergner 
> To: tutor@python.org
> Subject: [Tutor] Debugging While Loops for Control
> Message-ID: 
> Content-Type: text/plain; charset=us-ascii
> 
> Hi,
> 
> I've been translating and extending the Blackjack project from 
> codeacademy.com into Python. My efforts so far are here: 
> https://gist.github.com/1842131
> 
> My problem is that I am using two functions that return True or False to 
> determine whether the player receives another card.  Because of the way it 
> evaluates the while condition, it either prints too little information or 
> previously called the hitMe() function too many times.  I am assuming that I 
> am misusing the while loop in this case. If so, is there an elegant 
> alternative still running the functions at least once.
> 
> e.g. 
> while ask_for_raw_input() AND is_the_score_over_21():
>   hitMe(hand)
> 
> 
> Any advice or observations are appreciated, but please don't solve the whole 
> puzzle for me at once! And no, not all the functionality of a real game is 
> implemented. The code is pretty raw. I'm just a hobbyist trying to learn a 
> few things in my spare time.
> 
> Thanks in advance.
> 
> Luke
> 
> --
> 
> Message: 2
> Date: Thu, 16 Feb 2012 09:05:39 +
> From: Alan Gauld 
> To: tutor@python.org
> Subject: Re: [Tutor] Debugging While Loops for Control
> Message-ID: 
> Content-Type: text/plain; charset=ISO-8859-1; format=flowed
> 
> On 16/02/12 04:57, Luke Thomas Mergner wrote:
> 
>> My problem is that I am using two functions that return True or False
>> to determine whether the player receives another card.
> 
>> Because of the way it evaluates the while condition, it either
>> prints too little information or previously called the hitMe()
>> function too many times.
> 
>> I am assuming that I am misusing the while loop in this case.
> 
>> while ask_for_raw_input() AND is_the_score_over_21():
>>  hitMe(hand)
> 
> I haven't looked at the code for the functions but going
> by their names I'd suggest you need to reverse their order to
> 
> while is_the_score_over_21() and ask_for_raw_input():
>   hitMe(hand)
> 
> The reason is that the first function will always get called
> but you (I think) only want to ask for, and give out, another
> card if the score is over 21 (or should that maybe be
> *under* 21?).
> 
> Personally I would never combine a test function with
> an input one. Its kind of the other side of the rule that
> says don't don;t put print statements inside logic functions.
> In both cases its about separating himan interaction/display from 
> program logic. So I'd make the ask_for_raw_input jusat return a value(or 
> set of values) and create a new funtion to test
> the result and use that one in the while loop.
> 
> HTH,
> -- 
> Alan G
> Author of the Learn to Program web site
> http://www.alan-g.me.uk/


Alan (and list),

Thanks for the advice. It at least points me to an answer: I'm trying to be too 
clever for my experience level. I am going to go back and incorporate your 
suggestions.

In the meantime, and continuing my problem of over-cleverness, I was trying to 
rethink the program in classes.  With the caveat that I'm only a few hours into 
this rethinking, I've added the code below. My question is: when I want to 
build in a "return self" into the Hand class, which is made up of the card 
class; how do I force a conversion from card object into integer object which 
is all the card class is really holding? Should I make the class Card inherit 
from Integers? or is there a __repr__ def I don't understand yet? 

Bonus question: when I create a the "def score(self)" in class Hand, should 
that be an generator? And if so where do I go as a newb to understand 
generators? I'm really not understanding them yet.  The "x for x in y:" syntax 
makes it harder to follow for learners, even if I appreciate brevity.


Thanks in advance,
Luke


class Card(object):
def __init__(self):
self.score = self.deal()

def deal(self):
"""deal a card from 1 to 52 and return it's points"""
return self.getValue(int(math.floor(random.uniform(1, 52

def getValue(self, card):
"""Converts the values 1 - 52 into a 1 - 13 and returns the 
correct blackjack score based on remainder."""
if (card % 13 == 0 or card % 13 == 11 or card % 13 == 12):
#Face Cards are 10 points
return 10
elif (card % 13 == 1):
return 11
else:
#Regular cards, return their value
return card % 13

def showCard(self):
return repr(self.score)

class Hand:
def __init__(self):
 

Re: [Tutor] finding a maximum between the absolute difference of several columns

2012-02-16 Thread Mark Lawrence

On 17/02/2012 02:53, Elaina Ann Hyde wrote:

On Fri, Feb 17, 2012 at 1:41 PM, Rohan Sachdeva  wrote:


The way I would do this is to put all of the differences in a list then
take the maximum of the list. So..

a = Vmatch3_1 - Vmatch3_2
b = Vmatch3_1 - Vmatch3_3
c = Vmatch3_3 - Vmatch3_2

Vhel_fdiff3=max([a,b,c])

That should work. a,b,c are pretty bad variable names...

Rohan

On Thu, Feb 16, 2012 at 5:04 PM, Elaina Ann Hydewrote:


Hello all,
I am still scripting away and have reached my next quandry, this one
is much simpler than the last, basically I read in a file with several
columns.
Vmatch3_1=dat[col1]
Vmatch3_2=dat[col2]
Vmatch3_3=dat[col3]
Vdav=5.0
Vhel_fdiff3=max(abs(Vmatch3_1 - Vmatch3_2),abs(Vmatch3_1 -
Vmatch3_3),abs(Vmatch3_3 - Vmatch3_2))

What I would like this to return is the maximum difference in each case,
so I end up with one column which contains only the largest differences.
now I use this to write the condition:

with_v1_3=(Vhel_fdiff3>= Vdav)

I know the condition works and plots if

  Vhel_fdiff3=(Vmatch3_1 - Vmatch3_2)

for example, and I know this syntax would work if it was numbers instead
of columns.

max(abs(1-2),abs(3-7),abs(2-4))
4


The error is:
---
Traceback (most recent call last):
   File "double_plot.py", line 109, in
 Vhel_fdiff3=max(abs(Vmatch3_1 - Vmatch3_2),abs(Vmatch3_1 -
Vmatch3_3),abs(Vmatch3_3 - Vmatch3_2))
ValueError: The truth value of an array with more than one element is
ambiguous. Use a.any() or a.all()
-
So it can't handle the fact that it's columns of numbers and not single
numbers, so my question is, can I solve this without doing a loop around
it... use numpy, or some other function instead?
I've been searching around and haven't found any good ways forward so I
thought one of you might know.  Thanks
~Elaina

--
PhD Candidate
Department of Physics and Astronomy
Faculty of Science
Macquarie University
North Ryde, NSW 2109, Australia

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






--
Thanks for the replies so far.  I don't think that Rohan's idea solves the
numbers versus columns issue.  If I run it I get
---
Traceback (most recent call last):
   File "double_plot.py", line 112, in
 Vhel_fdiff3=max(a,b,c)
ValueError: The truth value of an array with more than one element is
ambiguous. Use a.any() or a.all()
-
which is just the same error, I looked at the forums and the link suggested
and I guess maybe my problem is trickier than I first thought!
~Elaina

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


I've found this http://comments.gmane.org/gmane.comp.python.tutor/72882.

HTH.

--
Cheers.

Mark Lawrence.

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


Re: [Tutor] finding a maximum between the absolute difference of several columns

2012-02-16 Thread Elaina Ann Hyde
On Fri, Feb 17, 2012 at 1:41 PM, Rohan Sachdeva  wrote:

> The way I would do this is to put all of the differences in a list then
> take the maximum of the list. So..
>
> a = Vmatch3_1 - Vmatch3_2
> b = Vmatch3_1 - Vmatch3_3
> c = Vmatch3_3 - Vmatch3_2
>
> Vhel_fdiff3=max([a,b,c])
>
> That should work. a,b,c are pretty bad variable names...
>
> Rohan
>
> On Thu, Feb 16, 2012 at 5:04 PM, Elaina Ann Hyde wrote:
>
>> Hello all,
>>I am still scripting away and have reached my next quandry, this one
>> is much simpler than the last, basically I read in a file with several
>> columns.
>> Vmatch3_1=dat[col1]
>> Vmatch3_2=dat[col2]
>> Vmatch3_3=dat[col3]
>> Vdav=5.0
>> Vhel_fdiff3=max(abs(Vmatch3_1 - Vmatch3_2),abs(Vmatch3_1 -
>> Vmatch3_3),abs(Vmatch3_3 - Vmatch3_2))
>> 
>> What I would like this to return is the maximum difference in each case,
>> so I end up with one column which contains only the largest differences.
>> now I use this to write the condition:
>>
>> with_v1_3=(Vhel_fdiff3 >= Vdav)
>>
>> I know the condition works and plots if
>>
>>  Vhel_fdiff3=(Vmatch3_1 - Vmatch3_2)
>>
>> for example, and I know this syntax would work if it was numbers instead
>> of columns.
>> >>max(abs(1-2),abs(3-7),abs(2-4))
>> >>4
>>
>> The error is:
>> ---
>> Traceback (most recent call last):
>>   File "double_plot.py", line 109, in 
>> Vhel_fdiff3=max(abs(Vmatch3_1 - Vmatch3_2),abs(Vmatch3_1 -
>> Vmatch3_3),abs(Vmatch3_3 - Vmatch3_2))
>> ValueError: The truth value of an array with more than one element is
>> ambiguous. Use a.any() or a.all()
>> -
>> So it can't handle the fact that it's columns of numbers and not single
>> numbers, so my question is, can I solve this without doing a loop around
>> it... use numpy, or some other function instead?
>> I've been searching around and haven't found any good ways forward so I
>> thought one of you might know.  Thanks
>> ~Elaina
>>
>> --
>> PhD Candidate
>> Department of Physics and Astronomy
>> Faculty of Science
>> Macquarie University
>> North Ryde, NSW 2109, Australia
>>
>> ___
>> Tutor maillist  -  Tutor@python.org
>> To unsubscribe or change subscription options:
>> http://mail.python.org/mailman/listinfo/tutor
>>
>>
>

--
Thanks for the replies so far.  I don't think that Rohan's idea solves the
numbers versus columns issue.  If I run it I get
---
Traceback (most recent call last):
  File "double_plot.py", line 112, in 
Vhel_fdiff3=max(a,b,c)
ValueError: The truth value of an array with more than one element is
ambiguous. Use a.any() or a.all()
-
which is just the same error, I looked at the forums and the link suggested
and I guess maybe my problem is trickier than I first thought!
~Elaina

-- 
PhD Candidate
Department of Physics and Astronomy
Faculty of Science
Macquarie University
North Ryde, NSW 2109, Australia
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] finding a maximum between the absolute difference of several columns

2012-02-16 Thread Mark Lawrence

On 17/02/2012 01:04, Elaina Ann Hyde wrote:

Hello all,
I am still scripting away and have reached my next quandry, this one is
much simpler than the last, basically I read in a file with several
columns.
Vmatch3_1=dat[col1]
Vmatch3_2=dat[col2]
Vmatch3_3=dat[col3]
Vdav=5.0
Vhel_fdiff3=max(abs(Vmatch3_1 - Vmatch3_2),abs(Vmatch3_1 -
Vmatch3_3),abs(Vmatch3_3 - Vmatch3_2))

What I would like this to return is the maximum difference in each case, so
I end up with one column which contains only the largest differences.
now I use this to write the condition:

with_v1_3=(Vhel_fdiff3>= Vdav)

I know the condition works and plots if

  Vhel_fdiff3=(Vmatch3_1 - Vmatch3_2)

for example, and I know this syntax would work if it was numbers instead of
columns.

max(abs(1-2),abs(3-7),abs(2-4))
4


The error is:
---
Traceback (most recent call last):
   File "double_plot.py", line 109, in
 Vhel_fdiff3=max(abs(Vmatch3_1 - Vmatch3_2),abs(Vmatch3_1 -
Vmatch3_3),abs(Vmatch3_3 - Vmatch3_2))
ValueError: The truth value of an array with more than one element is
ambiguous. Use a.any() or a.all()
-
So it can't handle the fact that it's columns of numbers and not single
numbers, so my question is, can I solve this without doing a loop around
it... use numpy, or some other function instead?
I've been searching around and haven't found any good ways forward so I
thought one of you might know.  Thanks
~Elaina




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


Throwing the error message into google gave a pile of hits from 
stackoverflow.com, which indicate that the error message is from numpy. 
 See if this can get you going 
http://stackoverflow.com/questions/1322380/gotchas-where-numpy-differs-from-straight-python. 
 The fourth answer down states "this is a horrible problem" so I'll 
duck out here, sorry :)


--
Cheers.

Mark Lawrence.

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


Re: [Tutor] How to convert seconds to hh:mm:ss format

2012-02-16 Thread Daryl V
You've gotten some really excellent advice about how to approach working
with the time format.  Here's a chunk of code I use quite a bit.  It's
probably sub-optimal, but playing with it might help you come up with a
solution that works for your application.


 def Int2Digit(integer):
if integer < 9:
strInteger = "0"+str(integer)
else:
strInteger = str(integer)
return strInteger

def TimeStampMaker():
import datetime
t = datetime.datetime.now()
strTmonth = Int2Digit(t.month)
strTday = Int2Digit(t.day)
strThour = Int2Digit(t.hour)
strTminute = Int2Digit(t.minute)
timeStamp = str(t.year)+strTmonth+strTday+strThour+strTminute
return timeStamp

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


[Tutor] finding a maximum between the absolute difference of several columns

2012-02-16 Thread Elaina Ann Hyde
Hello all,
   I am still scripting away and have reached my next quandry, this one is
much simpler than the last, basically I read in a file with several
columns.
Vmatch3_1=dat[col1]
Vmatch3_2=dat[col2]
Vmatch3_3=dat[col3]
Vdav=5.0
Vhel_fdiff3=max(abs(Vmatch3_1 - Vmatch3_2),abs(Vmatch3_1 -
Vmatch3_3),abs(Vmatch3_3 - Vmatch3_2))

What I would like this to return is the maximum difference in each case, so
I end up with one column which contains only the largest differences.
now I use this to write the condition:

with_v1_3=(Vhel_fdiff3 >= Vdav)

I know the condition works and plots if

 Vhel_fdiff3=(Vmatch3_1 - Vmatch3_2)

for example, and I know this syntax would work if it was numbers instead of
columns.
>>max(abs(1-2),abs(3-7),abs(2-4))
>>4

The error is:
---
Traceback (most recent call last):
  File "double_plot.py", line 109, in 
Vhel_fdiff3=max(abs(Vmatch3_1 - Vmatch3_2),abs(Vmatch3_1 -
Vmatch3_3),abs(Vmatch3_3 - Vmatch3_2))
ValueError: The truth value of an array with more than one element is
ambiguous. Use a.any() or a.all()
-
So it can't handle the fact that it's columns of numbers and not single
numbers, so my question is, can I solve this without doing a loop around
it... use numpy, or some other function instead?
I've been searching around and haven't found any good ways forward so I
thought one of you might know.  Thanks
~Elaina

-- 
PhD Candidate
Department of Physics and Astronomy
Faculty of Science
Macquarie University
North Ryde, NSW 2109, Australia
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


[Tutor] Python in Java [was Re: Tutor Digest, Vol 96, Issue 69]

2012-02-16 Thread Steven D'Aprano

Nicholas Palmer wrote:

I am fairly experienced in java and I was wondering how to use java in
python code, if you could give me any tips.\



My first tip is to learn how to use email effectively. Doing so will increase 
the chances that volunteers on mailing lists will answer your questions with 
respect instead of dismissing you as rude or incompetent or both.


(1) Please do not hijack existing threads for a new question, because your 
question may be missed or ignored by others. Always use your mail program's 
"Write Email" command (or equivalent) to ask a new question, not Reply or 
Reply All.


(2) That includes replying to message digests. Never use Reply unless you are 
actually *replying*.


(3) Always use Reply All to ensure a copy of your reply goes to the list, 
unless you wish to make a private (personal) comment to the person you are 
replying to.


(4) Always choose a sensible, meaningful subject line, such as "How to use 
Python with Java", and not "Re: [Tutor] Tutor Digest, Vol 96, Issue 69" or "Help".


(5) When replying, trim the irrelevant parts of the previous message. There is 
no need to include quotes of quotes of quotes of quotes going back through 
fifteen messages.


To answer your question about running Python in Java, there is an 
implementation of Python written in Java which has very high integration with 
the Java virtual machine and runtime libraries:


http://www.jython.org/

You might also try JPype, which promises Java to Python integration:

http://jpype.sourceforge.net/

or Jepp:

http://jepp.sourceforge.net/

or basically google for "python in java" or similar:

https://duckduckgo.com/html/?q=python%20in%20java



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


Re: [Tutor] Tutor Digest, Vol 96, Issue 69

2012-02-16 Thread Alan Gauld

On 16/02/12 23:45, Nicholas Palmer wrote:

I am fairly experienced in java and I was wondering how to use java in
python code, if you could give me any tips.\


In general you don't you use Python instead of Java.

But if you really must you can use Jython which is an implementation of 
python in Java. This allows you to use Java classes in Python and 
Python(Jython) classes in Java. Of course nothing is free so you get a 
performance hit and a resource hit (you have to embed the interpreter in 
your app). But you get a big development efficiency boon.


HTH,

--
Alan G
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] Using Java from Python was RE: Tutor Digest, Vol 96, Issue 69

2012-02-16 Thread Prasad, Ramit
>I am fairly experienced in java and I was wondering how to use java in python 
>code, if you could give me any tips.\

Hi,
Please remove any irrelevant sections of the digest if you are replying to it 
(which in this case is everything) and change the subject to be more 
descriptive. Also, top posting (replying above relevant text) is considered bad 
form.

Now getting down to your question, what exactly do you mean by "using" java 
"in" python? 1. Call a java program from Python? 2. Using Python code with a 
Java virtual machine? 3. Use actual Java code in Python?

My answers:
1. Look at Subprocess.call (preferred) or os.system functions.
2. Take a look at Jython.
3. I have no idea.

Ramit


Ramit Prasad | JPMorgan Chase Investment Bank | Currencies Technology
712 Main Street | Houston, TX 77002
work phone: 713 - 216 - 5423

--

This email is confidential and subject to important disclaimers and
conditions including on offers for the purchase or sale of
securities, accuracy and completeness of information, viruses,
confidentiality, legal privilege, and legal entity disclaimers,
available at http://www.jpmorgan.com/pages/disclosures/email.  
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Tutor Digest, Vol 96, Issue 69

2012-02-16 Thread Nicholas Palmer
I am fairly experienced in java and I was wondering how to use java in
python code, if you could give me any tips.\

On Thu, Feb 16, 2012 at 6:00 AM,  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. Debugging While Loops for Control (Luke Thomas Mergner)
>   2. Re: Debugging While Loops for Control (Alan Gauld)
>
>
> --
>
> Message: 1
> Date: Wed, 15 Feb 2012 23:57:08 -0500
> From: Luke Thomas Mergner 
> To: tutor@python.org
> Subject: [Tutor] Debugging While Loops for Control
> Message-ID: 
> Content-Type: text/plain; charset=us-ascii
>
> Hi,
>
> I've been translating and extending the Blackjack project from
> codeacademy.com into Python. My efforts so far are here:
> https://gist.github.com/1842131
>
> My problem is that I am using two functions that return True or False to
> determine whether the player receives another card.  Because of the way it
> evaluates the while condition, it either prints too little information or
> previously called the hitMe() function too many times.  I am assuming that
> I am misusing the while loop in this case. If so, is there an elegant
> alternative still running the functions at least once.
>
> e.g.
> while ask_for_raw_input() AND is_the_score_over_21():
>hitMe(hand)
>
>
> Any advice or observations are appreciated, but please don't solve the
> whole puzzle for me at once! And no, not all the functionality of a real
> game is implemented. The code is pretty raw. I'm just a hobbyist trying to
> learn a few things in my spare time.
>
> Thanks in advance.
>
> Luke
>
> --
>
> Message: 2
> Date: Thu, 16 Feb 2012 09:05:39 +
> From: Alan Gauld 
> To: tutor@python.org
> Subject: Re: [Tutor] Debugging While Loops for Control
> Message-ID: 
> Content-Type: text/plain; charset=ISO-8859-1; format=flowed
>
> On 16/02/12 04:57, Luke Thomas Mergner wrote:
>
> > My problem is that I am using two functions that return True or False
>  > to determine whether the player receives another card.
>
> > Because of the way it evaluates the while condition, it either
>  > prints too little information or previously called the hitMe()
>  > function too many times.
>
> > I am assuming that I am misusing the while loop in this case.
>
> > while ask_for_raw_input() AND is_the_score_over_21():
> >   hitMe(hand)
>
> I haven't looked at the code for the functions but going
> by their names I'd suggest you need to reverse their order to
>
> while is_the_score_over_21() and ask_for_raw_input():
>hitMe(hand)
>
> The reason is that the first function will always get called
> but you (I think) only want to ask for, and give out, another
> card if the score is over 21 (or should that maybe be
> *under* 21?).
>
> Personally I would never combine a test function with
> an input one. Its kind of the other side of the rule that
> says don't don;t put print statements inside logic functions.
> In both cases its about separating himan interaction/display from
> program logic. So I'd make the ask_for_raw_input jusat return a value(or
> set of values) and create a new funtion to test
> the result and use that one in the while loop.
>
> HTH,
> --
> Alan G
> Author of the Learn to Program web site
> http://www.alan-g.me.uk/
>
>
>
> --
>
> ___
> Tutor maillist  -  Tutor@python.org
> http://mail.python.org/mailman/listinfo/tutor
>
>
> End of Tutor Digest, Vol 96, Issue 69
> *
>
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] How to convert seconds to hh:mm:ss format

2012-02-16 Thread Dave Angel

On 02/16/2012 04:46 PM, alain Delon wrote:

userName = raw_input("Enter your name ")
  print "hi " + userName + ". \n "
  seconds = input("Enter the number of seconds since midnight:")
  hours = seconds/3600
  hoursRemain = hours%60
  minutes = hoursReamain/60
  secondsRemain = minutes%60
  print "hours: " + "minutes: " + "seconds"


I'm going to be crazy. Please give me a hand. Thank you very much.



Welcome to the list.  Are you new to programming, or just to Python, or 
just to this list?


Did you copy/paste this from somewhere?  Or retype it?  Have you tried 
running it?  What happens?


Is it a learning exercise, or something where you want the results with 
the least effort?  There are library functions in datetime that'll 
practically do it for you, but you don't already know about integer 
division and remainders, it's a good exercise to do by hand.


Why do you need help going crazy?

When I try running it, I get indentation errors first, because the code 
doesn't line up. next, I get an error because you spelled hoursRemain 
two different ways.


Then I get output that doesn't print out any of the results you 
calculated.  that's because you never print out any of the variables, 
only literal strings.


There also, you could use formatting functions, but if you just want to 
see the results, print them out with three separate print statements.  
Only when you decide they're correct should you worry about making it 
pretty, with leading zeroes, colons, and such.




--

DaveA

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


Re: [Tutor] How to convert seconds to hh:mm:ss format

2012-02-16 Thread Martin A. Brown


Welcome to the joys of time and date handling.

: userName = raw_input("Enter your name ")
:      print "hi " + userName + ". \n "
:      seconds = input("Enter the number of seconds since midnight:")
:      hours = seconds/3600
:      hoursRemain = hours%60
:      minutes = hoursReamain/60
:      secondsRemain = minutes%60
:      print "hours: " + "minutes: " + "seconds"
: 
: I'm going to be crazy.


In fact, yes, you probably will.  This is one of those wonderful 
places where you probably really want to look into what a library 
can do for you.  There are many, but I'll just start with the 
standard library called time.


Read the documenation on the time module:

 http://docs.python.org/library/time.html

Here's a simple example of getting the time right now, and 
presenting in in a variety of ways.  I give '%T' as the example you 
asked for, and then, I make up a nonstandard example to show how 
rich the format operators are when using strftime().


 >>> import time
 >>> time.gmtime()
 time.struct_time(tm_year=2012, tm_mon=2, tm_mday=16, tm_hour=22, tm_min=2, 
tm_sec=26, tm_wday=3, tm_yday=47, tm_isdst=0)
 >>> time.strftime('%T',time.gmtime())
 '22:02:28'
 >>> time.strftime('%R %A %B %d, %Y',time.gmtime())
 '22:02 Thursday February 16, 2012'
 >>> time.strftime('%R %A %B %d, %Y',time.localtime())
 '17:02 Thursday February 16, 2012'

Now, you will probably need to think carefully about whether you 
want localtime() or gmtime().  You will probably need to consider 
whether you need to do any date math.


: Please give me a hand. Thank you very much.

Play with strftime().  This is a feature of many languages and 
programming environments.  It should be able to produce most (if not 
all) output formats you probably need.


Now, you specifically asked about how to deal with adding and 
subtracting dates and times.  For this, consider the datetime 
module/library.  Yes, there's a small bit of overlap...notably that 
you can also use strftime() on datetime objects.


 http://docs.python.org/library/datetime.html

 >>> import datetime
 >>> now = datetime.datetime.now()
 >>> twohrsago = datetime.timedelta(minutes=120) 
 >>> now

 datetime.datetime(2012, 2, 16, 17, 15, 46, 655472)
 >>> now + twohrsago
 datetime.datetime(2012, 2, 16, 19, 15, 46, 655472)
 >>> then = now + twohrsago
 >>> then.strftime('%F-%T')
 '2012-02-16-19:15:46'

I would add my voice to Ramit Prasad's voice and strongly suggest 
using the date and time handling tools available in libraries, 
rather than try to build your own.


Good luck,

-Martin

--
Martin A. Brown
http://linux-ip.net/___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] How to convert seconds to hh:mm:ss format

2012-02-16 Thread Prasad, Ramit
>userName = raw_input("Enter your name ")
> print "hi " + userName + ". \n "
> seconds = input("Enter the number of seconds since midnight:")
> hours = seconds/3600
> hoursRemain = hours%60
> minutes = hoursReamain/60
> secondsRemain = minutes%60
> print "hours: " + "minutes: " + "seconds"

I avoid any kind of manual date/time calculation unless I 
am forced to. Here is how I would do it:

>>> midnight = datetime.datetime.combine( datetime.date.today(), 
>>> datetime.time() )
>>> seconds = datetime.timedelta( seconds=234 )
>>> time = midnight + seconds
>>> time.strftime( '%H:%M:%S' )
'00:03:54'
>>> time.strftime( 'Hours:%H minutes:%M seconds:%S' )
'Hours:00 minutes:03 seconds:54'

Ramit


Ramit Prasad | JPMorgan Chase Investment Bank | Currencies Technology
712 Main Street | Houston, TX 77002
work phone: 713 - 216 - 5423

--

This email is confidential and subject to important disclaimers and
conditions including on offers for the purchase or sale of
securities, accuracy and completeness of information, viruses,
confidentiality, legal privilege, and legal entity disclaimers,
available at http://www.jpmorgan.com/pages/disclosures/email.  
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


[Tutor] How to convert seconds to hh:mm:ss format

2012-02-16 Thread alain Delon
userName = raw_input("Enter your name ")
     print "hi " + userName + ". \n "
     seconds = input("Enter the number of seconds since midnight:")
     hours = seconds/3600
     hoursRemain = hours%60
     minutes = hoursReamain/60
     secondsRemain = minutes%60
     print "hours: " + "minutes: " + "seconds"


I'm going to be crazy. Please give me a hand. Thank you very much.___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Debugging While Loops for Control

2012-02-16 Thread Alan Gauld

On 16/02/12 04:57, Luke Thomas Mergner wrote:


My problem is that I am using two functions that return True or False

> to determine whether the player receives another card.


Because of the way it evaluates the while condition, it either

> prints too little information or previously called the hitMe()
> function too many times.


I am assuming that I am misusing the while loop in this case.



while ask_for_raw_input() AND is_the_score_over_21():
hitMe(hand)


I haven't looked at the code for the functions but going
by their names I'd suggest you need to reverse their order to

while is_the_score_over_21() and ask_for_raw_input():
hitMe(hand)

The reason is that the first function will always get called
but you (I think) only want to ask for, and give out, another
card if the score is over 21 (or should that maybe be
*under* 21?).

Personally I would never combine a test function with
an input one. Its kind of the other side of the rule that
says don't don;t put print statements inside logic functions.
In both cases its about separating himan interaction/display from 
program logic. So I'd make the ask_for_raw_input jusat return a value(or 
set of values) and create a new funtion to test

the result and use that one in the while loop.

HTH,
--
Alan G
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