[Tutor] Scripting-Puzzle Pirates

2010-10-24 Thread Nathan Finney

Hey,
 
So I got bored of having to do a repeated task on this game, YPP Puzzle Pirates 
and I was wondering if it was possible to script it. My task would be to start 
at a dock, click on the port arrow, choose a ship (a different one each time 
and in order preferably), go to its hold, select materials to be moved and move 
them to a set ship (the same one each time), then return to the starting 
position.
 
If this is possible would I first need a set of the games coding (which uses 
javascript) to be obtained so it could be read and a script used upon it.
 
Thank you 
 
Yours faithfully,
Nathan Finney.___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Scripting-Puzzle Pirates

2010-10-24 Thread Alan Gauld


"Nathan Finney"  wrote

So I got bored of having to do a repeated task on this game, 
YPP Puzzle Pirates and I was wondering if it was possible 
to script it. 


No idea, never heard of it.

If this is possible would I first need a set of the games coding 
(which uses javascript) to be obtained so it could be read and 
a script used upon it.


Probably, but as this is a Python mailing list you are probably 
asking in the wrong place. Try a Javascript forum or better still, 
one dedicated to the game in question.


--
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] Scripting-Puzzle Pirates

2010-10-24 Thread Lie Ryan
On 10/24/10 15:33, Nathan Finney wrote:
> Hey,
>  
> So I got bored of having to do a repeated task on this game, YPP Puzzle
> Pirates and I was wondering if it was possible to script it. 

Even if you can (hint: no, you can't), most games consider writing
scripts to do tasks as botting, i.e. cheating.

> My task
> would be to start at a dock, click on the port arrow, choose a ship (a
> different one each time and in order preferably), go to its hold, select
> materials to be moved and move them to a set ship (the same one each
> time), then return to the starting position.

Now, that would be stealing or just rude, even if you do it manually.

> If this is possible would I first need a set of the games coding (which
> uses javascript) to be obtained so it could be read and a script used
> upon it.

Java is not the same as Javascript. Puzzle Pirate is a Java game.

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


[Tutor] What's the best way to model an unfair coin?

2010-10-24 Thread Richard D. Moores
What's the best way to model an unfair coin?

This is one way to do it, I suppose: Create a list containing only
'H's and 'T's. If you want the coin to have the probability of a head
to be 6/11,

['H', 'H', 'H', 'H', 'H', 'H', 'T', 'T', 'T', 'T', 'T']

is the list to use. Use random.choice on the list, for a 6/11 heads
probability.

See .

That's the only way I can think of. But surely there's a better, more
general solution. What if the probability I want is an irrational
number, such as 1/e? Sure, I can calculate a fraction that's as close
to that irrational number as I want, but..

Am I missing something that's already there in Python 2.6 or 3.1 (the
2 I have)?

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


Re: [Tutor] What's the best way to model an unfair coin?

2010-10-24 Thread Evert Rol
> What's the best way to model an unfair coin?
> 
> This is one way to do it, I suppose: Create a list containing only
> 'H's and 'T's. If you want the coin to have the probability of a head
> to be 6/11,
> 
> ['H', 'H', 'H', 'H', 'H', 'H', 'T', 'T', 'T', 'T', 'T']
> 
> is the list to use. Use random.choice on the list, for a 6/11 heads
> probability.
> 
> See .
> 
> That's the only way I can think of. But surely there's a better, more
> general solution. What if the probability I want is an irrational
> number, such as 1/e? Sure, I can calculate a fraction that's as close
> to that irrational number as I want, but..

My statistics might be too rusty to have this correct, but I would do something 
similar as you have now, just not for integer numbers.
Assuming you only want True or False, you can use a uniform distribution, 
through random.random(), and see if the result is lower or higher than your 
probability. 
Eg:

return random.random() < 1/e

or 

return random.random() < 6/11.

will return True or False with your specificied probability. 
Again, I just might be overlooking something in the statistics.

Cheers,

  Evert

Btw, to be pedantic, 1/e is not an irrational number, just a real number. i/e 
would be.


> 
> Am I missing something that's already there in Python 2.6 or 3.1 (the
> 2 I have)?
> 
> Dick Moores
> ___
> 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] What does "TypeError: 'int' object is not iterable" mean?

2010-10-24 Thread Richard D. Moores
On Sat, Oct 23, 2010 at 02:28, col speed  wrote:
>
>
>>
>>
>> Message: 7
>> Date: Fri, 22 Oct 2010 21:48:29 -0700
>> From: "Richard D. Moores" 
>> To: "Steven D'Aprano" 
>> Cc: tutor@python.org
>> Subject: Re: [Tutor] What does "TypeError: 'int' object is not
>>        iterable"       mean?
>> Message-ID:
>>        
>> Content-Type: text/plain; charset=UTF-8
>>
>> It's great to have you chime in, Steven. I do wish you would stop
>> pulling your punches, however. ;)
>>
>> <-snip>
>> I've never seen that convention, but I'll try to follow it.
>>
>> >
>> > (BTW, I really hate the name "floatt". It makes me think you're
>> > stuttering.)
>>
>> I'll use "x" instead. Anything you'd like to say about the rest of the
>> script?
>>
>> Thanks, Steven.
>>
>> Dick
>>
>>
>> --
>> Excuse me, I'm not a professional. Rather than "x", I would use "float_"
>> or even "not_int", as mentioned in PEP8:
>
> If a function argument's name clashes with a reserved keyword, it is
>   generally better to append a single trailing underscore rather than
> use
>   an abbreviation or spelling corruption.  Thus "print_" is better than
>
> "prnt". (Perhaps better is to avoid such clashes by using a synonym.)
> Steven knows his subject, please don't answer like this.

And I have a request of you: Please don't change the Subject header
when you reply. I didn't see your post with it's important suggestion
until just now.

Thanks for the quote from PEP8.  I went with "myfloat", on Dave
Angel's suggestion, but float_ looks good as well. 'not_int' is not so
good, because many kinds of objects are "not_int"s.

As for Steven, you're absolutely correct -- and I've learned a lot from him.

Dave Angel wrote:
> Sometimes Steven's style can be a bit caustic, but there's almost always a
> few important nuggets.

and I replied:
>Absolutely there are! And I have no problem with his style. I just
>couldn't hold back what I intended to be a gentle jab of sarcasm.
>Dangerous in email--especially an email list.

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


Re: [Tutor] What's the best way to model an unfair coin?

2010-10-24 Thread Steven D'Aprano

Richard D. Moores wrote:

What's the best way to model an unfair coin?


Let probability of heads = p, where 0 <= p <= 1
Then probability of tails = 1-p.

if random.random() <= p: print("got heads")
else: print("got tails")

[...]

That's the only way I can think of. But surely there's a better, more
general solution. What if the probability I want is an irrational
number, such as 1/e? Sure, I can calculate a fraction that's as close
to that irrational number as I want, but..


Well, technically speaking all floats in Python are rational numbers, 
since they're base-2 floating point numbers. But the difference between 
(say) float pi and the true irrational number π is around about

0.0001, so close enough for most purposes.



--
Steven

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


Re: [Tutor] What's the best way to model an unfair coin?

2010-10-24 Thread Alan Gauld


"Richard D. Moores"  wrote

'H's and 'T's. If you want the coin to have the probability of a 
head

to be 6/11,

['H', 'H', 'H', 'H', 'H', 'H', 'T', 'T', 'T', 'T', 'T']

is the list to use. Use random.choice on the list, for a 6/11 heads
probability.


That will work but as you say is not very general.
You could write a function that takers the desired probablity
as an input and returns a result based on that.

The simplest way its to generate a random number between
0 and 1 and compare to the required probability expressed
as a decimal fraction.

In pseudo code:

def coinToss(prob = 0.5):
   rand = random()
   if rand >= prob: return True
   else: return False

print "Heads" if coinToss(6/11) else "Tails"


Am I missing something that's already there in Python 2.6 or 3.1 
(the

2 I have)?


There may well be functions in some of the modules that do it but
the approach above suffices for most applications.

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] What's the best way to model an unfair coin?

2010-10-24 Thread Evert Rol
> Btw, to be pedantic, 1/e is not an irrational number, just a real number. i/e 
> would be.

My bad: irrational != imaginary. And real = irrational.
Things are definitely a bit rusty...


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


Re: [Tutor] What's the best way to model an unfair coin?

2010-10-24 Thread Steven D'Aprano

Evert Rol wrote:


Btw, to be pedantic, 1/e is not an irrational number, just a real number. i/e 
would be.


Actually, Richard is correct. Like π, e and 1/e are irrational numbers. 
"Irrational" means the number is not rational, in the sense of *ratio*, 
not sanity :)


There is no exact ratio of integers which give *exactly* π, e, 1/e, or 
many other numbers, although you can get as close an approximation as 
you like. Hence they are known as irrationals. Many surds, like square 
root of 2, are irrational. Not all of them though: sqrt(4) is rational.


Mathematicians deal with a hierarchy of numbers:

Natural numbers: 1, 2, 3, 4, ...
Integers: 0, 1, -1, 2, -2, 3, -3, ...
Rationals: numbers that can be written as ratios of two integers, e.g. 
1/2, 3/7, ...

Irrationals: numbers that can't be written as such ratios.

The rationals and the irrationals together make up the real numbers. 
"Real" in the sense that when you measure real quantities like distance 
or weight, you get a real number.


The number system has been extended to "imaginary" numbers, complex 
numbers, quaternions, surreal numbers, and other more exotic families. 
Python includes support for complex numbers, using "j" as the symbol for 
the imaginary unit:


>>> 2 + 3j  # real number + imaginary number makes a complex number
(2+3j)

(Mathematicians will be more familiar with the symbol i, but electrical 
engineers usually use j.)




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


Re: [Tutor] Problem with python

2010-10-24 Thread Richard D. Moores
On Wed, Oct 20, 2010 at 03:13, Steven D'Aprano  wrote:

> Let's start with an easy example: factorial(0). When Python sees
> factorial(0), it does this:
>
> Call factorial with argument 0:
> * if n == 0 <-- this is true, so execute the "if" block:
>    return 1
>
> so the function factorial(0) returns the value 1. All that goes on
> behind the scenes. What *we* see is:
>
 factorial(0)
> 1
>
> That part is easy. Now, the recursive part. What does Python do when you
> call factorial(1)?
>
> Call factorial with argument 1:
> * if n == 0 <-- this is false, so execute the "else" block:
>    recurse = factorial(n-1)
>
> Here Python is calling a function. It happens to be the same function,
> but Python doesn't care about that, and neither should you. So Python
> simply continues:
>
>    Call factorial with argument 1-1 = 0:
>
> Now, we've already seen this above, but Python does the simplest thing
> that could work: it calculates the answer from scratch:
>

I won't quote the whole thing, which should be enshrined somewhere. I
just wanted to note that Steven is a great teacher!

Dick Moores
(teacher, retired)
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] What's the best way to model an unfair coin?

2010-10-24 Thread Richard D. Moores
On Sun, Oct 24, 2010 at 05:17, Steven D'Aprano  wrote:

> Let probability of heads = p, where 0 <= p <= 1
> Then probability of tails = 1-p.
>
> if random.random() <= p: print("got heads")
> else: print("got tails")

My thanks to Evert, Steven, and Alan. I should have thought of that solution.

Actually, I used the unfair coin model as the simplest example of the
kind of thing I want to do -- which is to model the USD->Yen exchange
rate. I want the next quote to vary in a controlled random way, by
assigning probabilities to various possible changes in the rate. See
. So I assign probability 1/40
to a change of plus or minus .05; 3/40 to .04; 5/40 to .03, etc.

An analogy to this would be an unfair 6-sided die, with each side
assigned probabilities slightly differing from 1/6 (but totaling 1, of
course). I can't see off-hand how to apply the Evert-Steven-Alan
solution to these, but is it possible? I'd just like a yes or no -- if
yes, I'd like to try to work it out myself.

Thanks,

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


Re: [Tutor] What's the best way to model an unfair coin?

2010-10-24 Thread Evert Rol
> Actually, I used the unfair coin model as the simplest example of the
> kind of thing I want to do -- which is to model the USD->Yen exchange
> rate. I want the next quote to vary in a controlled random way, by
> assigning probabilities to various possible changes in the rate. See
> . So I assign probability 1/40
> to a change of plus or minus .05; 3/40 to .04; 5/40 to .03, etc.
> 
> An analogy to this would be an unfair 6-sided die, with each side
> assigned probabilities slightly differing from 1/6 (but totaling 1, of
> course). I can't see off-hand how to apply the Evert-Steven-Alan
> solution to these, but is it possible? I'd just like a yes or no -- if
> yes, I'd like to try to work it out myself.

Yes, and in a very similar way.

Cheers,

  Evert

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


Re: [Tutor] What's the best way to model an unfair coin?

2010-10-24 Thread Richard D. Moores
On Sun, Oct 24, 2010 at 06:36, Evert Rol  wrote:
>> Actually, I used the unfair coin model as the simplest example of the
>> kind of thing I want to do -- which is to model the USD->Yen exchange
>> rate. I want the next quote to vary in a controlled random way, by
>> assigning probabilities to various possible changes in the rate. See
>> . So I assign probability 1/40
>> to a change of plus or minus .05; 3/40 to .04; 5/40 to .03, etc.
>>
>> An analogy to this would be an unfair 6-sided die, with each side
>> assigned probabilities slightly differing from 1/6 (but totaling 1, of
>> course). I can't see off-hand how to apply the Evert-Steven-Alan
>> solution to these, but is it possible? I'd just like a yes or no -- if
>> yes, I'd like to try to work it out myself.
>
> Yes, and in a very similar way.

Thanks, Evert. I'm on it.

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


Re: [Tutor] Tutor Digest, Vol 80, Issue 108

2010-10-24 Thread Art Cla Media
mai date  in pula  mea  cu masurile  tale  mai acrit  la coie  mai termina  in 
plm cu  ele

--- On Sun, 10/24/10, tutor-requ...@python.org  wrote:

From: tutor-requ...@python.org 
Subject: Tutor Digest, Vol 80, Issue 108
To: tutor@python.org
Date: Sunday, October 24, 2010, 12:25 PM

Send Tutor mailing list submissions to
    tutor@python.org

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

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

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


Today's Topics:

   1. Re: Scripting-Puzzle Pirates (Lie Ryan)
   2. What's the best way to model an unfair coin? (Richard D. Moores)
   3. Re: What's the best way to model an unfair coin? (Evert Rol)
   4. Re: What does "TypeError: 'int' object is not iterable"    mean?
      (Richard D. Moores)
   5. Re: What's the best way to model an unfair coin? (Steven D'Aprano)
   6. Re: What's the best way to model an unfair coin? (Alan Gauld)
   7. Re: What's the best way to model an unfair coin? (Evert Rol)


--

Message: 1
Date: Sun, 24 Oct 2010 21:05:30 +1100
From: Lie Ryan 
To: tutor@python.org
Subject: Re: [Tutor] Scripting-Puzzle Pirates
Message-ID: 
Content-Type: text/plain; charset=ISO-8859-1

On 10/24/10 15:33, Nathan Finney wrote:
> Hey,
>  
> So I got bored of having to do a repeated task on this game, YPP Puzzle
> Pirates and I was wondering if it was possible to script it. 

Even if you can (hint: no, you can't), most games consider writing
scripts to do tasks as botting, i.e. cheating.

> My task
> would be to start at a dock, click on the port arrow, choose a ship (a
> different one each time and in order preferably), go to its hold, select
> materials to be moved and move them to a set ship (the same one each
> time), then return to the starting position.

Now, that would be stealing or just rude, even if you do it manually.

> If this is possible would I first need a set of the games coding (which
> uses javascript) to be obtained so it could be read and a script used
> upon it.

Java is not the same as Javascript. Puzzle Pirate is a Java game.



--

Message: 2
Date: Sun, 24 Oct 2010 04:32:43 -0700
From: "Richard D. Moores" 
To: tutor@python.org
Subject: [Tutor] What's the best way to model an unfair coin?
Message-ID:
    
Content-Type: text/plain; charset=UTF-8

What's the best way to model an unfair coin?

This is one way to do it, I suppose: Create a list containing only
'H's and 'T's. If you want the coin to have the probability of a head
to be 6/11,

['H', 'H', 'H', 'H', 'H', 'H', 'T', 'T', 'T', 'T', 'T']

is the list to use. Use random.choice on the list, for a 6/11 heads
probability.

See .

That's the only way I can think of. But surely there's a better, more
general solution. What if the probability I want is an irrational
number, such as 1/e? Sure, I can calculate a fraction that's as close
to that irrational number as I want, but..

Am I missing something that's already there in Python 2.6 or 3.1 (the
2 I have)?

Dick Moores


--

Message: 3
Date: Sun, 24 Oct 2010 13:54:16 +0200
From: Evert Rol 
To: "Richard D. Moores" 
Cc: tutor@python.org
Subject: Re: [Tutor] What's the best way to model an unfair coin?
Message-ID: <1f4296e5-b373-43fd-b2a1-5b175cce7...@gmail.com>
Content-Type: text/plain; charset=us-ascii

> What's the best way to model an unfair coin?
> 
> This is one way to do it, I suppose: Create a list containing only
> 'H's and 'T's. If you want the coin to have the probability of a head
> to be 6/11,
> 
> ['H', 'H', 'H', 'H', 'H', 'H', 'T', 'T', 'T', 'T', 'T']
> 
> is the list to use. Use random.choice on the list, for a 6/11 heads
> probability.
> 
> See .
> 
> That's the only way I can think of. But surely there's a better, more
> general solution. What if the probability I want is an irrational
> number, such as 1/e? Sure, I can calculate a fraction that's as close
> to that irrational number as I want, but..

My statistics might be too rusty to have this correct, but I would do something 
similar as you have now, just not for integer numbers.
Assuming you only want True or False, you can use a uniform distribution, 
through random.random(), and see if the result is lower or higher than your 
probability. 
Eg:

return random.random() < 1/e

or 

return random.random() < 6/11.

will return True or False with your specificied probability. 
Again, I just might be overlooking something in the statistics.

Cheers,

  Evert

Btw, to be pedantic, 1/e is not an irrational number, just a real number. i/e 
would be.


> 
> Am I missing something that's already there in Py

Re: [Tutor] What's the best way to model an unfair coin?

2010-10-24 Thread Richard D. Moores
Here's my model unfair die.



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


Re: [Tutor] What's the best way to model an unfair coin?

2010-10-24 Thread Jose Amoreira
On Sunday, October 24, 2010 01:18:52 pm Alan Gauld wrote:

> In pseudo code:
> 
> def coinToss(prob = 0.5):
> rand = random()
> if rand >= prob: return True
> else: return False
> 
> print "Heads" if coinToss(6/11) else "Tails"
> 

The only problem with this snippet is integer division: 6/11=0, at least in 
Python 2.6, so that the final line will always print "Heads".

But wait! This is pseudo code! Ah, OK. Then 6/11=0.545454..., and Alan was 
right (as usual).
 
This pseudo code snippet is almost Python code. It looks like Python, it 
smells like Python, it even runs as Python, if you import random from random 
beforehand.
Python really is executable pseudo code!
Cheers
Jose Amoreira
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] What's the best way to model an unfair coin?

2010-10-24 Thread Lie Ryan
On 10/25/10 02:46, Jose Amoreira wrote:
> On Sunday, October 24, 2010 01:18:52 pm Alan Gauld wrote:
> 
>> In pseudo code:
>>
>> def coinToss(prob = 0.5):
>> rand = random()
>> if rand >= prob: return True
>> else: return False
>>
>> print "Heads" if coinToss(6/11) else "Tails"
>>
> 
> The only problem with this snippet is integer division: 6/11=0, at least in 
> Python 2.6, so that the final line will always print "Heads".
> 
> But wait! This is pseudo code! Ah, OK. Then 6/11=0.545454..., and Alan was 
> right (as usual).

Except for the missing import (which is traditionally omitted in mailing
list discussions when the context makes it clear), Alan's snippet is
correct in Python 3, which defaults to real division.

Though, I'd probably writes it differently:

def coinToss(prob = 0.5):
rand = random()
return rand >= prob

or even:

def coinToss(prob = 0.5):
return random() >= prob

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


Re: [Tutor] What does "TypeError: 'int' object is not iterable" mean?

2010-10-24 Thread Lie Ryan
On 10/23/10 01:19, David Hutto wrote:
> If I understand what i just said correctly, it just means it tells the
> string what type to convert from when placing it into the final
> result.

basically, when doing this %-interpolation, python does this:

("NEW LOW: %%.%sf at %%s" % i) % (lowz, timestamp)

do the first interpolation:

"NEW LOW: %%.%sf at %%s" % i

it's to be noted that %% is replaced by a single %, and %s is replaced
by the right argument of %, so if i == 5 it now becomes:

"NEW LOW: %.5sf at %s" % (lowz, timestamp)

and now do the second interpolation, lowz is formatted with %.5f which
means a floating value (f) with 5 decimal place, and timestamp is
inserted in place of %s; so if you have lowz = 81.345678901234 and
timestamp = "last year":

"NEW LOW: 81.34567 at last year"


However, as Steven noted, the complex, 2-phase interpolation can be
simplified using the '*' decimal place specifier:

("NEW LOW: %%.%sf at %%s" % i) % (lowz, timestamp)
# is equivalent with:
"NEW LOW: %.*f at %s" % (i, lowz, timestamp)

and Steven also remarked that now the interpolation is so simple, there
is very little benefit in separating it into a different function.

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


Re: [Tutor] What's the best way to model an unfair coin?

2010-10-24 Thread Steven D'Aprano

Richard D. Moores wrote:


Actually, I used the unfair coin model as the simplest example of the
kind of thing I want to do -- which is to model the USD->Yen exchange
rate. I want the next quote to vary in a controlled random way, by
assigning probabilities to various possible changes in the rate. See
. So I assign probability 1/40
to a change of plus or minus .05; 3/40 to .04; 5/40 to .03, etc.


Another approach you might take is to model the change as a normal 
distribution (bell curve probability) rather than uniform. This is 
probably more realistic. It would make most sense to have it symmetrical 
around zero, so you want a random number with a normal distribution, a 
mean of zero, and a standard deviation yet to be determined.


To determine the standard deviation, use this rule of thumb: for a 
normal (bell) curve, approximately 68% of events are plus or minus one 
standard deviation from the mean; 95% are plus or minus two std 
deviations; and 99.7% are plus or minus three std deviations.


So if you decide that 99.7% of the time the change in exchange rate 
should be less than 1.00, for example, that corresponds to a std 
deviation of 0.333.


You then generate a normally-distributed value using the random module, 
round it to two decimal places to correspond to cents, and Bob's your uncle.




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


Re: [Tutor] What's the best way to model an unfair coin?

2010-10-24 Thread Richard D. Moores
On Sun, Oct 24, 2010 at 09:22, Steven D'Aprano  wrote:
> Richard D. Moores wrote:
>
>> Actually, I used the unfair coin model as the simplest example of the
>> kind of thing I want to do -- which is to model the USD->Yen exchange
>> rate. I want the next quote to vary in a controlled random way, by
>> assigning probabilities to various possible changes in the rate. See
>> . So I assign probability 1/40
>> to a change of plus or minus .05; 3/40 to .04; 5/40 to .03, etc.
>
> Another approach you might take is to model the change as a normal
> distribution (bell curve probability) rather than uniform. This is probably
> more realistic. It would make most sense to have it symmetrical around zero,
> so you want a random number with a normal distribution, a mean of zero, and
> a standard deviation yet to be determined.
>
> To determine the standard deviation, use this rule of thumb: for a normal
> (bell) curve, approximately 68% of events are plus or minus one standard
> deviation from the mean; 95% are plus or minus two std deviations; and 99.7%
> are plus or minus three std deviations.
>
> So if you decide that 99.7% of the time the change in exchange rate should
> be less than 1.00, for example, that corresponds to a std deviation of
> 0.333.
>
> You then generate a normally-distributed value using the random module,
> round it to two decimal places to correspond to cents, and Bob's your uncle.

Ah, use random.normalvariate(mu, sigma), or random.gauss(mu, sigma)
where mu is the mean, sigma the standard deviation. Great idea. Thanks!

Bob IS my uncle! How'd you know? (First I'd heard that expression.
Interesting. )

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


[Tutor] Problems with partial string matching

2010-10-24 Thread Josep M. Fontana
Hi,

As I said in another message with the heading "Using contents of a
document to change file names", I'm trying to learn Python "by doing"
and I was working on a little project where I had to change the names
of the files in a directory according to some codes contained in a CSV
file. With the help of the participants in this list I managed to
overcome some of the first obstacles I found and managed to create a
dictionary out of the structured contents of the CSV file.

Unfortunately, I didn't have much time to continue working on my
project and I didn't get back to the script until now. I have
encountered another hurdle, though, which doesn't allow me to go on.
The problem might be *very* simple to solve but I've spent the last
couple of hours checking manuals and doing searches on the internet
without having much success.

What I'm trying to do now is to use the dictionary I created (with
entries such as {'I-02': '1399', 'I-01': '1374',...}) to iterate over
the file names I want to change and do the necessary string
substitutions. If one of the keys in the dictionary matches the code
that is found at the beginning of every file, then the value of the
dictionary representing the year in which the text was written is
appended at the end of the file name.

Here is what I've done so far:
--
import os, sys, glob, re
fileNameYear = 
open(r'/Volumes/DATA/Documents/workspace/GCA/CORPUS_TEXT_LATIN_1/FileNameYear.txt',
"U").readlines()
name_year = {}
for line in fileNameYear: #File objects have built-in iteration
name, year = line.strip().split(',')
name_year[name] = year #effectively creates the dictionary by
creating keys with the element 'name' returned by the loop and
assigning them values corresponding to the element 'year' --> !d[key]
= value" means Set d[key] to value.
os.getcwd()
os.chdir('/Volumes/DATA/Documents/workspace/GCA/CORPUS_TEXT_LATIN_1')
file_names = glob.glob('*.txt')
for name_of_file in file_names:
if name_of_file.startswith(name):
re.sub('__', '__' + year, name_of_file) #The files have names
such as 'B-13-Viatges_Marco_Polo__.txt' so the first argument in
re.sub() is the string '__' which should be replaced by the same
string followed by the string corresponding to the year value in the
dictionary (also a string)
--

I run this and I don't get any errors. The names of the files in the
directory, however, are not changed. What am I doing wrong?

As always, your help is greatly appreciated.


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


Re: [Tutor] Problems with partial string matching

2010-10-24 Thread Joel Goldstick
On Sun, Oct 24, 2010 at 2:16 PM, Josep M. Fontana  wrote:

> Hi,
>
> As I said in another message with the heading "Using contents of a
> document to change file names", I'm trying to learn Python "by doing"
> and I was working on a little project where I had to change the names
> of the files in a directory according to some codes contained in a CSV
> file. With the help of the participants in this list I managed to
> overcome some of the first obstacles I found and managed to create a
> dictionary out of the structured contents of the CSV file.
>
> Unfortunately, I didn't have much time to continue working on my
> project and I didn't get back to the script until now. I have
> encountered another hurdle, though, which doesn't allow me to go on.
> The problem might be *very* simple to solve but I've spent the last
> couple of hours checking manuals and doing searches on the internet
> without having much success.
>
> What I'm trying to do now is to use the dictionary I created (with
> entries such as {'I-02': '1399', 'I-01': '1374',...}) to iterate over
> the file names I want to change and do the necessary string
> substitutions. If one of the keys in the dictionary matches the code
> that is found at the beginning of every file, then the value of the
> dictionary representing the year in which the text was written is
> appended at the end of the file name.
>
> Here is what I've done so far:
> --
> import os, sys, glob, re
> fileNameYear =
> open(r'/Volumes/DATA/Documents/workspace/GCA/CORPUS_TEXT_LATIN_1/FileNameYear.txt',
> "U").readlines()
> name_year = {}
> for line in fileNameYear: #File objects have built-in iteration
>name, year = line.strip().split(',')
>name_year[name] = year #effectively creates the dictionary by
> creating keys with the element 'name' returned by the loop and
> assigning them values corresponding to the element 'year' --> !d[key]
> = value" means Set d[key] to value.
> os.getcwd()
> os.chdir('/Volumes/DATA/Documents/workspace/GCA/CORPUS_TEXT_LATIN_1')
> file_names = glob.glob('*.txt')
> for name_of_file in file_names:
>if name_of_file.startswith(name):
>re.sub('__', '__' + year, name_of_file) #The files have names
> such as 'B-13-Viatges_Marco_Polo__.txt' so the first argument in
> re.sub() is the string '__' which should be replaced by the same
> string followed by the string corresponding to the year value in the
> dictionary (also a string)
> --
>
> I run this and I don't get any errors. The names of the files in the
> directory, however, are not changed. What am I doing wrong?
>
> As always, your help is greatly appreciated.
>
>
> Josep M.
> ___
> Tutor maillist  -  Tutor@python.org
> To unsubscribe or change subscription options:
> http://mail.python.org/mailman/listinfo/tutor
>

You are not renaming the file, just the string named name_of_file.

Check out os.rename

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


Re: [Tutor] Problems with partial string matching

2010-10-24 Thread Dave Angel


On 2:59 PM, Josep M. Fontana wrote:

Hi,

As I said in another message with the heading "Using contents of a
document to change file names", I'm trying to learn Python "by doing"
and I was working on a little project where I had to change the names

I run this and I don't get any errors. The names of the files in the
directory, however, are not changed. What am I doing wrong?

As always, your help is greatly appreciated.


Josep M.


You call re.sub(), but don't do anything with the result.

Where do you call os.rename() ?

DaveA

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


[Tutor] What is random.triangular() used for

2010-10-24 Thread Richard D. Moores
In looking through the doc on the random module I came across
random.triangular():


random.triangular(low, high, mode)
Return a random floating point number N such that low <= N <= high and
with the specified mode between those bounds. The low and high bounds
default to zero and one. The mode argument defaults to the midpoint
between the bounds, giving a symmetric distribution.

New in version 2.6.


I fooled around with it a bit and wrote this rough demo:


The skewing and centering possible by varying the mode, are obvious,
but how is random.trangular() actually used?

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


Re: [Tutor] What is random.triangular() used for

2010-10-24 Thread Emile van Sebille

On 10/24/2010 1:17 PM Richard D. Moores said...

In looking through the doc on the random module I came across
random.triangular():


random.triangular(low, high, mode)
Return a random floating point number N such that low<= N<= high and
with the specified mode between those bounds. The low and high bounds
default to zero and one. The mode argument defaults to the midpoint
between the bounds, giving a symmetric distribution.

New in version 2.6.


I fooled around with it a bit and wrote this rough demo:


The skewing and centering possible by varying the mode, are obvious,
but how is random.trangular() actually used?



From people who would know found at 
http://docs.scipy.org/doc/numpy/reference/generated/numpy.random.triangular.html



"""The triangular distribution is often used in ill-defined problems 
where the underlying distribution is not known, but some knowledge of 
the limits and mode exists. Often it is used in simulations."""


HTH,

Emile




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


Re: [Tutor] Problem with python

2010-10-24 Thread python
> I just wanted to note that Steven is a great teacher!

+1

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


Re: [Tutor] What is random.triangular() used for

2010-10-24 Thread Richard D. Moores
On Sun, Oct 24, 2010 at 13:29, Emile van Sebille  wrote:

> From people who would know found at
> http://docs.scipy.org/doc/numpy/reference/generated/numpy.random.triangular.html
>
>
> """The triangular distribution is often used in ill-defined problems where
> the underlying distribution is not known, but some knowledge of the limits
> and mode exists. Often it is used in simulations."""

Emile,

from the source code link on that page, I got

# Draw values from the distribution and plot the histogram:

import matplotlib.pyplot as plt
h = plt.hist(np.random.triangular(-3, 0, 8, 10), bins=200,
 normed=True)
plt.show()

Which when I run it I get
Traceback (most recent call last):
  File "c:\P26Working\untitled-8.py", line 2, in 
h = plt.hist(np.random.triangular(-3, 0, 8, 10), bins=200, normed=True)
NameError: name 'np' is not defined
Process terminated with an exit code of 1

The import goes OK:

>>> import matplotlib.pyplot as plt
>>>

and apparently I have numpy, if that's relevant:
>>> import numpy
>>>

but where do I get np?

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


Re: [Tutor] What is random.triangular() used for

2010-10-24 Thread Adam Bark

On 24/10/10 21:57, Richard D. Moores wrote:

On Sun, Oct 24, 2010 at 13:29, Emile van Sebille  wrote:

   

 From people who would know found at
http://docs.scipy.org/doc/numpy/reference/generated/numpy.random.triangular.html


"""The triangular distribution is often used in ill-defined problems where
the underlying distribution is not known, but some knowledge of the limits
and mode exists. Often it is used in simulations."""
 

Emile,

from the source code link on that page, I got

# Draw values from the distribution and plot the histogram:

import matplotlib.pyplot as plt
h = plt.hist(np.random.triangular(-3, 0, 8, 10), bins=200,
  normed=True)
plt.show()

Which when I run it I get
Traceback (most recent call last):
   File "c:\P26Working\untitled-8.py", line 2, in
 h = plt.hist(np.random.triangular(-3, 0, 8, 10), bins=200, normed=True)
NameError: name 'np' is not defined
Process terminated with an exit code of 1

The import goes OK:

   

import matplotlib.pyplot as plt

 

and apparently I have numpy, if that's relevant:
   

import numpy

 

but where do I get np?

Dick
   
I think he may be using ipython but putting an "import numpy as np" 
should get that code to work.

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


Re: [Tutor] What is random.triangular() used for

2010-10-24 Thread Steven D'Aprano

Richard D. Moores wrote:

NameError: name 'np' is not defined

[...]

but where do I get np?



I believe that it is common in the scientific python world to do this:

import numpy as np

and then use np.FUNCTION instead of numpy.FUNCTION.

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


Re: [Tutor] What is random.triangular() used for

2010-10-24 Thread Richard D. Moores
On Sun, Oct 24, 2010 at 14:48, Steven D'Aprano  wrote:
> Richard D. Moores wrote:
>>
>> NameError: name 'np' is not defined
>
> [...]
>>
>> but where do I get np?
>
>
> I believe that it is common in the scientific python world to do this:
>
> import numpy as np
>
> and then use np.FUNCTION instead of numpy.FUNCTION.

Yup. That was it.  Beautiful!

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


Re: [Tutor] Writing Scripts.

2010-10-24 Thread John
Autumn,

Here's a basic script, if you save this in a file called hello.py and
type 'python hello.py' at the prompt, or as others are saying using
the python launcher, you should get some output. hth, john

SCRIPT (include lines below here):
#!/usr/bin/env python

import os

user  = os.environ.get('USER')

print "Hello %s!" % (user)


 NOW SOME COMMENTS 
the first line is somewhat 'unix or linux' dependent, but it tells
your 'shell' to call the file using python

the next line "import os" is a python import statement, telling python
to import the os module

the next line "user os.environ.get('USER')" is call a method (get)
from the environ class of the os module and returns the name of the
logged in user to the variable 'user'

the next line is doing a few things. First it is calling the built in
function 'print' to print a string "Hello %s!". However, the "%s" does
something fancy. This is string substitution, so by having the
variable 'user' after the second '%' sign, it replaces "%s" with that
variable. Don't worry, it will come fast! Have fun with it!!







On Wed, Oct 20, 2010 at 6:13 PM, Autumn Cutter  wrote:
> I just started learning Python last night and I'm a little stuck on how to 
> write a Python script.
> I'm using Python 2.5.0 on a Mac OS X 10.6.4. I've tried using programs like 
> Idle, Applescript and Textmate but I'm still unable to write a script and run 
> it successfully. I'm wondering if I'm using the write program or if I 
> understand correctly how to write and run scripts. Help! :(
>
> ___
> Tutor maillist  -  tu...@python.org
> To unsubscribe or change subscription options:
> http://mail.python.org/mailman/listinfo/tutor
>



-- 
Configuration
``
Plone 2.5.3-final,
CMF-1.6.4,
Zope (Zope 2.9.7-final, python 2.4.4, linux2),
Python 2.6
PIL 1.1.6
Mailman 2.1.9
Postfix 2.4.5
Procmail v3.22 2001/09/10
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] What is random.triangular() used for

2010-10-24 Thread Dave Angel


On 2:59 PM, Richard D. Moores wrote:

On Sun, Oct 24, 2010 at 13:29, Emile van Sebille  wrote:


 From people who would know found at
http://docs.scipy.org/doc/numpy/reference/generated/numpy.random.triangular.html


"""The triangular distribution is often used in ill-defined problems where
the underlying distribution is not known, but some knowledge of the limits
and mode exists. Often it is used in simulations."""

Emile,

from the source code link on that page, I got

# Draw values from the distribution and plot the histogram:

import matplotlib.pyplot as plt
h = plt.hist(np.random.triangular(-3, 0, 8, 10), bins=200,
  normed=True)
plt.show()

Which when I run it I get
Traceback (most recent call last):
   File "c:\P26Working\untitled-8.py", line 2, in
 h = plt.hist(np.random.triangular(-3, 0, 8, 10), bins=200, normed=True)
NameError: name 'np' is not defined
Process terminated with an exit code of 1

The import goes OK:


import matplotlib.pyplot as plt


and apparently I have numpy, if that's relevant:

import numpy


but where do I get np?

Dick


import numpy as np

see 
http://matplotlib.sourceforge.net/plot_directive/mpl_examples/pylab_examples/histogram_demo.py
for one example.  I don't know anything about matplotlib as a whole, 
this was just a lucky shot


DaveA



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


Re: [Tutor] What is random.triangular() used for

2010-10-24 Thread Richard D. Moores
On Sun, Oct 24, 2010 at 16:38, Dave Angel  wrote:

> import numpy as np
>
> see
> http://matplotlib.sourceforge.net/plot_directive/mpl_examples/pylab_examples/histogram_demo.py
> for one example.  I don't know anything about matplotlib as a whole, this
> was just a lucky shot

Thanks, Dave. That's lovely:
. I gotta learn this.

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


[Tutor] 2.6 vs 2.7: package compatibility?

2010-10-24 Thread Alex Hall
Hi all,
I want to run a certain program from source. One dependency, Durus,
keeps giving me an error that no one can figure out. Someone said that
it will work if I use 2.7 instead of 2.6, but a lot of packages I have
installed work only on 2.6. I know I can install both, but here is the
question: all these packages that say they need 2.6... would they work
on 2.7? That is, is the 2.6 the only version of python with which they
will work, or just the minimum version (I know about the
incompatibility between 2.x and 3). I would rather have just one
version of python running; it makes keeping track of what packages I
have installed easier, and I never could get my idea of two system
variables, one for each version, to work properly. TIA.

-- 
Have a great day,
Alex (msg sent from GMail website)
mehg...@gmail.com; http://www.facebook.com/mehgcap
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor