[Tutor] Movement controls useing pygame

2010-03-19 Thread mark-ireland
A little stuck and could do with any sudjestions.

Aim:-
When the character goes past the middle of the screen, the background & level 
move downwards so the character can get to higher places, think sonic the 
hedgehog.

This is the bit I'm having problems with, I can get the character to get to the 
center, the background moves up & down but the character won't go back down 
when the level returns to normal height.

if bkGroundRect['rect'].top < 0 and charRect['rect'].top < 
winCenterRect.top:

bkGroundRect['rect'].top += spY
platRect['rect'].top += spY
jumpRect.top += spY
charRect['dir'] = STOP


if levelRect['rect'].bottom > winHeight and charRect['rect'].centery == 
winCenterx:
bkGroundRect['rect'].bottom -= spY
platRect['rect'].bottom -= spY
#jumpRect.bottom -= spY
charRect['dir'] = STOP

  #--this won't execute ? --#   
if levelRect['rect'].bottom <= winHeight:
if charRect['rect'].centery >= winCenterx and charRect['rect'].centerx 
< jumpRect.centerx:
charRect['dir'] = DOWN

I've included the whole code as a note pad so the above makes more sense, don't 
worry about all the comments, that's just for my convenience.

Thanks guys

Mark

'knight meets wall, wall won't let knight pass, knight says 'neh'

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


Re: [Tutor] Movement controls useing pygame

2010-03-19 Thread Luke Paireepinart
Are you just reposting this exact same e-mail to the list because you didn't
see my reply to the other one, or are you ignoring what I said and posting
the same message again, hoping someone else will answer?
I hope it's not the latter, that's kind of insulting.  The least you could
do is say that my idea didn't make sense or wouldn't work for you in this
case.

-Luke

On Thu, Mar 18, 2010 at 7:45 PM,  wrote:

>  A little stuck and could do with any sudjestions.
>
> Aim:-
> When the character goes past the middle of the screen, the background &
> level move downwards so the character can get to higher places, think sonic
> the hedgehog.
>
> This is the bit I'm having problems with, I can get the character to get to
> the center, the background moves up & down but the character won't go back
> down when the level returns to normal height.
>
> if bkGroundRect['rect'].top < 0 and charRect['rect'].top <
> winCenterRect.top:
>
> bkGroundRect['rect'].top += spY
> platRect['rect'].top += spY
> jumpRect.top += spY
> charRect['dir'] = STOP
>
>
> if levelRect['rect'].bottom > winHeight and charRect['rect'].centery ==
> winCenterx:
> bkGroundRect['rect'].bottom -= spY
> platRect['rect'].bottom -= spY
> #jumpRect.bottom -= spY
> charRect['dir'] = STOP
>
>   #--this won't execute ? --#
> if levelRect['rect'].bottom <= winHeight:
> if charRect['rect'].centery >= winCenterx and
> charRect['rect'].centerx < jumpRect.centerx:
> charRect['dir'] = DOWN
>
> I've included the whole code as a note pad so the above makes more sense,
> don't worry about all the comments, that's just for my convenience.
>
> Thanks guys
>
> Mark
>
> 'knight meets wall, wall won't let knight pass, knight says 'neh'
>
>
> ___
> 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] movement controls

2010-03-19 Thread Luke Paireepinart
On Fri, Mar 19, 2010 at 4:09 AM,  wrote:

>  But yes, I'm not quite sure about what you mean. I guess you mean keep
> the character on the x plane <> then for the jumping movements move the
> level? what do you mean by 'You should probably decouple the view from the
> model'
>
>
> and it's preferable not to use html when sending e-mails, 'cause your font
settings don't necessarily look good on other people's computers.

You should really be asking this on the Pygame list, and you probably still
should.

Having said that... I'll try to answer your question.

The way you're currently doing it, you have all of your sprites' coordinates
just stored in random variables, and you're calculating where they should be
on the screen and then drawing them directly, right?
In other words, if player.x = 100 and player.y = 200 then he is located at
100, 200 on the screen?

Having a view and a model relates to the MVC (model-view-controller)
paradigm in programming.

Basically think of it like this.

Suppose I have a window that I'm looking out, and my dog is outside running
around somewhere.  Now you walk by the window.  Does it make more sense for
me to model your interaction with my dog in relation to what I can see out
of my window, or should I model your interaction with the dog in a separate
space, independent of where I may be looking?

Say I want to describe your position to my friend who lives up and to the
right of me.  Would I say "see that guy by the bench petting my dog?" or
would I say "see the guy that is 12 meters forward and 1 meter to the right
of the center of my window?"  The friend doesn't have any idea what
perspective _I_ view the world from, and he really shouldn't have to.

SO in the MVC framework, you model your _world_ space and your VIEW is just
an outside observer "peeking" at the current state of the world.  So, for
example, say your world is a grid
of 1000 x 8000 units, and your character is located at 200x200.  There might
be an enemy moving around at 800x2000, who may not be on the screen, but I
still want to model that enemy moving around,
so that when the character gets over there, the enemy will be in a new
location.  Or maybe he'll walk over and be on the screen.

When you do it this way, you can merely say "render everything that is
around my player's current position" and then all your problems are solved.
The view will automatically follow the player, and when he jumps up, the
view will raise with him.  If you set an upper and lower limit, then the
view will naturally not move until the player moves past a certain point of
the screen.

If you continue to refer to the game objects in terms of their graphical
position in relation to the screen, it's going to get very convoluted.

In order to  "decouple your view from your model" you will likely have to
rewrite a lot of your code -- the key here is that you should've written it
this way (MVC) in the first place.

Read up more on MVC, and specifically how it relates to game design.

Also, when you reply,
use Reply to All
otherwise your reply is only seen by me, not by the other members on the
list.

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


[Tutor] difflib context to string-object?

2010-03-19 Thread Karjer Jdfjdf
With difflib.context_diff it is possible to write the context to files.

difflib.context_diff(a, b[, fromfile][, tofile][, fromfiledate][, tofiledate][, 
n][, lineterm])

Is it also possible to do this to seperate string-objects instead of writing 
them to files? 





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


[Tutor] using pythnn to open a password protected website

2010-03-19 Thread richard west
Hi,

Im trying to use python to open up a password protected website(e.g.
facebook / gmail) in Firefox. supplying the login and password automatically
at runtime - so that I can interface my code with fingerprint recognition
code. So far I have only found urllib, urllib2 and web browser, none of
which I have been able to use.

urllib2 can log me in, but then I cant pass this authenicated login to the
browser. At the very least I need to be able to send the form post
information to the browser via my code, and preferably auto-submit the form.

Does anybody have any ideas?

Many Thanks,

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


Re: [Tutor] using pythnn to open a password protected website

2010-03-19 Thread Wayne Werner
On Fri, Mar 19, 2010 at 7:33 AM, richard west wrote:

> Hi,
>
> Im trying to use python to open up a password protected website(e.g.
> facebook / gmail) in Firefox. supplying the login and password automatically
> at runtime - so that I can interface my code with fingerprint recognition
> code. So far I have only found urllib, urllib2 and web browser, none of
> which I have been able to use.
>
> urllib2 can log me in, but then I cant pass this authenicated login to the
> browser. At the very least I need to be able to send the form post
> information to the browser via my code, and preferably auto-submit the form.
>
> Does anybody have any ideas?


Check the tamper data plugin for firefox. I think that's the one. It should
help you get the types of parameters, etc. that are actually passed to the
website. Then I think you can use urllib2's variety of methods to connect to
the site. HTH,
Wayne
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] difflib context to string-object?

2010-03-19 Thread bob gailer

On 3/19/2010 6:55 AM, Karjer Jdfjdf wrote:

With difflib.context_diff it is possible to write the context to files.

difflib.context_diff(/a/, /b/[, /fromfile/][, /tofile/][, 
/fromfiledate/][, /tofiledate/][, /n/][, /lineterm/])


Is it also possible to do this to seperate string-objects instead of 
writing them to files?




Take a look at the stringIO module.

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

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


Re: [Tutor] Understanding (Complex) Modules

2010-03-19 Thread Wayne Watson
(I meant to post this to both the list and Steven back a week or so  
ago. I missed the list, so am posting it here now. Unfortunately, I'm 
again in a position where I cannot respond for several days, but I will 
be back early next week to examine some posts that followed this.)

===

Thanks. I'm on the road for a few days and will be able to read this 
more carefully when I get back. I had a feeling from my first attempts 
of looking at MPL and other heavy-duty libs that it was going to require 
work to dig out what I needed from the module itself. In my personal 
view, the usage and learning documents are fairly limiting. Perhaps on 
in MPL, which is less traditional than numpy and the others, which are 
founded on common and historic implementations of math and science from 
older languages.


From: Steven D'Aprano 
To: tutor@python.org
Sent: Thu, March 4, 2010 6:24:15 PM
Subject: Re: [Tutor] Understanding (Complex) Modules

On Thu, 4 Mar 2010 12:24:35 pm Wayne Watson wrote:
> First a little preamble before my questions.
>
> Most of my work in Python has required modifying a program that uses
> modules that were imported by the original program. I've made some
> use of modules on a command line like math, and have used the idea of
> a qualifier.  On occasion, I've used examples from matplotlib that
> required from matplotlib.image import AxesImage. Further, I've
> created some simple classes, and produced  objects with them. No use
> of inheritance though.  So far so good.  In a few places, it is said
> modules are objects. I'm slightly puzzled by that, but in some way it
> seems reasonable from the standpoint of period notation. So far I
> have not created a module.

Yes you have, you just don't know it.

In simple language, a file with Python-usable code in it is a module.
There are some technicalities and complexities, but in basic terms,
that's all it is.

Technically, "module" refers only to the object which exists inside the
Python environment after you call "import thing". The import statement
does a whole lot of tricks, but in a nutshell it:

* finds a file containing code called 'thing'
* loads it into memory, executing code as needed
* creates a 'module' object to store the code and data in the file
* and makes it available to your code

Python can create module objects from:

compiled Windows DLLs .dll
compiled Linux object files .so
Python source code .py
Python byte code .pyc and .pyo (and .pyw on Windows)
Zip files containing any of the above

and probably other things as well, but they're the main ones.

So any Python file you create (any .py file) is a module, or at least it
would be a module if you import it.

Ignoring all the various compiled files (.dll, .so, etc) what happens
when you run a .py file from the command line (or from IDLE or another
IDE). E.g. you type something like:

python.exe myscript.py [enter]


Python reads the file myscript.py and executes it, but no module object
is created. It is possible that a module object *is* created, for
internal use, then thrown away when the script is finished. But your
script doesn't see the module object.

However, if you enter the Python interactive environment, and do this:

>>> import myscript  # no .py

Python loads the file myscript.py into a module object, executing any
code in it, and makes it available as a module.



> Here comes the questions. Recently I began to use matplotlib, scipy,
> and pylab, mostly matplotlib. I've ground out a few useful pieces of
> code, but I'm fairly baffled by the imports required to get at
> various elements, of say, matplotlib (MPL).  Some of the MPL examples
> use of imports make sense, but how the writer pulled in the necessary
> elements is not.  How does one go about understanding the
> capabilities of such modules?

Time, experience, and a lot of hard work. Welcome to programming!

If the documentation is good, then read the documentation.

If the documentation is lacking, or bad, or even wrong, then read the
source code, or search the Internet for a tutorial, or buy a book about
it (the numpy people, for example, sell books about numpy).

Python makes experimentation easy: there are a lot of powerful
introspection facilities in Python. The interactive interpreter is your
best friend. You will live with it, eat with it, sleep with it, take it
on double-dates, and throw yourself on live grenades to protect it.
Whenever I'm programming, I almost always have three or five
interactive sessions open for experimentation.

The dir() and help() functions are also good friends. In an interactive
session:

import math
dir(math)  # prints a list of names in the math module
help(math.sin)
help(math)


Don't feel that you have to understand the entire module before you use
it. Many (alas, not all) modules have a reasonably gentle learning
curve: you can start using math.sin without needing to know what
math.sinh is for.

Google and Wikipedia are also you

[Tutor] Efficiency and speed

2010-03-19 Thread James Reynolds
Hello all:

I've still been working towards learning the language, albeit slowly and
I've been working on a project that is somewhat intense on the numerical
calculation end of things.

Running 10,000 trials takes about 1.5 seconds and running 100,000 trials
takes 11 seconds. Running a million trials takes about 81 seconds or so. I
don't think 1M trials is needed for accuracy, but 100K probably is.

I've made a few other optimizations today that I won't be able to test until
I get home, but I was wondering if any of you could give some general
pointers on how to make python run a little more quickly.

There are two major pieces of code that seem slow, so I'll share the first
with you. This section takes up about 1/3 of the time used when running all
trials, where trials is 10K or larger.

This section doesn't actually do any math, all it's doing is returning the
"bin" that the randomly generated number falls in.

The second section, the one that is taking up most of the time, does the
math.

The list nx1 is about 220 floating point numbers long.



> sample = random.sample(range(int(self.nx1[b])), trials) # a list of sample
> values based on nx1

countlist = []

self.nx1.append(0) #puts a zero on the end of nx1. This is for the case
> where one of the random values lies past the minimum number (it scales from
> 10M down to almost 0, but not exactly 0) Antyhing past this dates yields a
> 0.

for s in self.mcrange_gen(sample):

 countlist.append(s-1) # This appends the bin number (the number
> returned previously minus one) to make slices of the premium streams.


and here is the generator section:

def mcrange_gen(self, sample):#, lensample):

lensample = len(sample) # this section is just for speed. All of these
> are renames from the globals to bring calc times down at the expense of
> memory. I haven't tested these yet.

nx2 = self.nx1

nx2_append = nx2.append

nx2_sort = nx2.sort

nx2_reverse = nx2.reverse

nx2_index = nx2.index

nx2_remove = nx2.remove

for s in range(lensample):

q = sample[s] #takes the next randomly generated number from the
> sample list

nx2_append(q) # and appends it to nx list.

nx2_sort() # and sorts it in place

nx2_reverse() # reverses the list, because this was the original
> order

i = nx2_index(q) #get the index of that element

nx2_remove(q) # and remove the element.

yield i # send the position of that element back to the main
> program.
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Efficiency and speed

2010-03-19 Thread Stefan Behnel

James Reynolds, 19.03.2010 17:41:

I've still been working towards learning the language, albeit slowly and
I've been working on a project that is somewhat intense on the numerical
calculation end of things.

Running 10,000 trials takes about 1.5 seconds and running 100,000 trials
takes 11 seconds. Running a million trials takes about 81 seconds or so. I
don't think 1M trials is needed for accuracy, but 100K probably is.

I've made a few other optimizations today that I won't be able to test until
I get home, but I was wondering if any of you could give some general
pointers on how to make python run a little more quickly.

There are two major pieces of code that seem slow, so I'll share the first
with you. This section takes up about 1/3 of the time used when running all
trials, where trials is 10K or larger.

This section doesn't actually do any math, all it's doing is returning the
"bin" that the randomly generated number falls in.

The second section, the one that is taking up most of the time, does the
math.

The list nx1 is about 220 floating point numbers long.




sample = random.sample(range(int(self.nx1[b])), trials) # a list of sample
values based on nx1


countlist = []

self.nx1.append(0) #puts a zero on the end of nx1. This is for the case

where one of the random values lies past the minimum number (it scales from
10M down to almost 0, but not exactly 0) Antyhing past this dates yields a
0.


for s in self.mcrange_gen(sample):

  countlist.append(s-1) # This appends the bin number (the number

returned previously minus one) to make slices of the premium streams.



and here is the generator section:

def mcrange_gen(self, sample):#, lensample):

 lensample = len(sample) # this section is just for speed. All of these

are renames from the globals to bring calc times down at the expense of
memory. I haven't tested these yet.


 nx2 = self.nx1

 nx2_append = nx2.append

 nx2_sort = nx2.sort

 nx2_reverse = nx2.reverse

 nx2_index = nx2.index

 nx2_remove = nx2.remove

 for s in range(lensample):

 q = sample[s] #takes the next randomly generated number from the

sample list


 nx2_append(q) # and appends it to nx list.

 nx2_sort() # and sorts it in place

 nx2_reverse() # reverses the list, because this was the original

order


 i = nx2_index(q) #get the index of that element

 nx2_remove(q) # and remove the element.

 yield i # send the position of that element back to the main

program.


This certainly falls into the bin of the most inefficient algorithms I've 
ever seen. Even walking through the samples one by one to find the target 
bin would be faster than the above.


Could you try to describe in a couple of words what this algorithm is 
supposed to do? That will almost certainly make it clear how you should 
write it instead.


Stefan

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


Re: [Tutor] parsing a "chunked" text file

2010-03-19 Thread Karim Liateni

Hello,

Thanks both of you for these useful information.

Regards
Karim

Hugo Arts wrote:

On Thu, Mar 18, 2010 at 12:54 PM, Stefan Behnel  wrote:
  

Karim Liateni, 04.03.2010 01:23:

Yes, a *big* difference in the true sense of the word. Your code (assuming
you meant to write "... for line in ..." ) evaluates the entire list
comprehension before returning from the call. Steven's code returns a
generator that only handles one line (or a couple of empty lines) at a time.
So, assuming that this runs against a large file, Steven's code uses only a
constant amount of memory, compared to the whole file in your case, and is
likely also a lot faster than your code as it involves less looping.




Though, if you changed the brackets into parentheses, you'd get a
generator expression, which *is* equivalent to Steven's version,
except that it calls strip() twice, which is a bit wasteful.

If the unnecessary extra call bothers you, you could do one of two things:
1) Learn how the yield keyword works. You should do this. It's an
awesome feature, and you'll come across it many more times.
2) go functional and import itertools. ifilter with a generator
expression, like so (pure functional programmers can also use imap
instead of the generator expr., which might be faster. profile to be
sure)

def skip_blanks(lines):
return ifilter(None, (l.strip() for l in lines))

Very short, has all the memory and speed benefits of the generator.
Personally I really like terse functional programming like this,
though I believe the general consensus in the python community is that
imperative alternatives are usually clearer to read.

If you want to know more about the yield keyword:
A terse description (assumes that you know how iterators work) is
here: http://docs.python.org/tutorial/classes.html#generators
A more detailed description of iterators and generators can be found
here: http://www.ibm.com/developerworks/library/l-pycon.html

Hugo
___
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] Efficiency and speed

2010-03-19 Thread James Reynolds
Well, I'm always out to impress!

This is a monte-carlo simulation.

The simulation measures the expiration of something and those somethings
fall into bins that are not evenly dispersed. These bins are stored in the
nx list mentioned previously.

So let's say you have the bins, a, b,c,d,e,f and you have the value z from
the sample list where z >b and <= a. In this case, it should return the
index value at position (a).

These, in turn, are used to take slices of other lists and only apply those
slices from position zero to position (a), in the case above.

I previously tried doing something like: if z >b and <= a then append to new
list, else pass, but that seemed like a slower way of going about it,
because python needs to cycle through the entire list to find the right
value. I guess i could add a count to the list, like len(appendedlist) and
using a while loop, so it will stop checking once it's been appended and
move to the next iteration in the sample list.

But before you recommend a better way of writing it, I would greatly
appreciate to know why the above is inefficient and some other way is more
efficient. I'm trying to learn the concepts behind programming in general
and how to program in python at the same time.

On Fri, Mar 19, 2010 at 1:56 PM, Stefan Behnel  wrote:

> James Reynolds, 19.03.2010 17:41:
>
>  I've still been working towards learning the language, albeit slowly and
>> I've been working on a project that is somewhat intense on the numerical
>> calculation end of things.
>>
>> Running 10,000 trials takes about 1.5 seconds and running 100,000 trials
>> takes 11 seconds. Running a million trials takes about 81 seconds or so. I
>> don't think 1M trials is needed for accuracy, but 100K probably is.
>>
>> I've made a few other optimizations today that I won't be able to test
>> until
>> I get home, but I was wondering if any of you could give some general
>> pointers on how to make python run a little more quickly.
>>
>> There are two major pieces of code that seem slow, so I'll share the first
>> with you. This section takes up about 1/3 of the time used when running
>> all
>> trials, where trials is 10K or larger.
>>
>> This section doesn't actually do any math, all it's doing is returning the
>> "bin" that the randomly generated number falls in.
>>
>> The second section, the one that is taking up most of the time, does the
>> math.
>>
>> The list nx1 is about 220 floating point numbers long.
>>
>>
>>
>>  sample = random.sample(range(int(self.nx1[b])), trials) # a list of
>>> sample
>>> values based on nx1
>>>
>>
>> countlist = []
>>
>> self.nx1.append(0) #puts a zero on the end of nx1. This is for the case
>>
>>> where one of the random values lies past the minimum number (it scales
>>> from
>>> 10M down to almost 0, but not exactly 0) Antyhing past this dates yields
>>> a
>>> 0.
>>>
>>
>> for s in self.mcrange_gen(sample):
>>
>>  countlist.append(s-1) # This appends the bin number (the number
>>
>>> returned previously minus one) to make slices of the premium streams.
>>>
>>
>>
>> and here is the generator section:
>>
>> def mcrange_gen(self, sample):#, lensample):
>>
>> lensample = len(sample) # this section is just for speed. All of these
>>
>>> are renames from the globals to bring calc times down at the expense of
>>> memory. I haven't tested these yet.
>>>
>>
>> nx2 = self.nx1
>>
>> nx2_append = nx2.append
>>
>> nx2_sort = nx2.sort
>>
>> nx2_reverse = nx2.reverse
>>
>> nx2_index = nx2.index
>>
>> nx2_remove = nx2.remove
>>
>> for s in range(lensample):
>>
>> q = sample[s] #takes the next randomly generated number from the
>>
>>> sample list
>>>
>>
>> nx2_append(q) # and appends it to nx list.
>>
>> nx2_sort() # and sorts it in place
>>
>> nx2_reverse() # reverses the list, because this was the original
>>
>>> order
>>>
>>
>> i = nx2_index(q) #get the index of that element
>>
>> nx2_remove(q) # and remove the element.
>>
>> yield i # send the position of that element back to the main
>>
>>> program.
>>>
>>
> This certainly falls into the bin of the most inefficient algorithms I've
> ever seen. Even walking through the samples one by one to find the target
> bin would be faster than the above.
>
> Could you try to describe in a couple of words what this algorithm is
> supposed to do? That will almost certainly make it clear how you should
> write it instead.
>
> Stefan
>
> ___
> 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] using pythnn to open a password protected website

2010-03-19 Thread Alan Gauld


"richard west"  wrote


Im trying to use python to open up a password protected website(e.g.
facebook / gmail) in Firefox. 


So you are trying to control Firefox not the web site?
Is that correct?

What mechanism are you using to communicate with Firefox?


code. So far I have only found urllib, urllib2 and web browser, none of
which I have been able to use.


urllib is for interacting with the web site not with the browser.

webbrowser is for launching a browser for a user to interact with, 
it does not really control the browser programatically.


You may need to use OS dependent tools like COM on 
Windows or maybe even GUI events/messages to drive the 
browser remotely. Are you sure that's really what you want to do?


Or do you really want to fill in and submit a web form on a 
page protected by a login? If thats the case you can do it all 
from Python without going near Firefox.


--
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] Efficiency and speed

2010-03-19 Thread Emile van Sebille

On 3/19/2010 9:41 AM James Reynolds said...


OK, so starting here:

def mcrange_gen(self, sample):
lensample = len(sample)
nx2 = self.nx1
nx2_append = nx2.append
nx2_sort = nx2.sort
nx2_reverse = nx2.reverse
nx2_index = nx2.index
nx2_remove = nx2.remove
for s in range(lensample):
q = sample[s]
nx2_append(q)
nx2_sort()
nx2_reverse()
i = nx2_index(q)
nx2_remove(q)
yield i


First, the two lines:

for s in range(lensample):
q = sample[s]

variable s is never used again, so instead we'll do:

for q in sample:

Which renders lensample as unused, so throw out

lensample = len(sample)

We now have:


def mcrange_gen(self, sample):
nx2 = self.nx1
nx2_append = nx2.append
nx2_sort = nx2.sort
nx2_reverse = nx2.reverse
nx2_index = nx2.index
nx2_remove = nx2.remove
for q in sample:
nx2_append(q)
nx2_sort()
nx2_reverse()
i = nx2_index(q)
nx2_remove(q)
yield i


Now, let's see what's going on for each q. You append it to self.nx1 
(through the nx2 reference), then sort nx1, then reserve nx1, then scan 
for the new position of q, note the index, remove it from nx1, and yeild 
the index.  OK, so that way you find out between which two elements of 
nx1 the current q falls, and i becomes the bin.


So, how about something like (untested):

def mcrange_gen(self, sample):
self.nx1.sort() # now the bin boundries are in order
for q in sample:
ii = -1
for binlimit in self.nx1:
if qhttp://mail.python.org/mailman/listinfo/tutor


[Tutor] Tutorial executable from python script.

2010-03-19 Thread Karim Liateni


Hello,

I want to do a executable for linux/unix from python scripts and thus o 
allow to run
on machine which doesn't have recent version (2.0) of python. I found 
the compile()
method but how can I use it to make all in one executable which could be 
run on old

system (old python). If you have any links I would love to have it

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


Re: [Tutor] Efficiency and speed

2010-03-19 Thread Alan Gauld

"James Reynolds"  wrote

I've made a few other optimizations today that I won't be able to test 
until

I get home, but I was wondering if any of you could give some general
pointers on how to make python run a little more quickly.


Always, always, get the algorithm efficient before trying to make
the code efficient.

Then eliminate redundant variable assignments, extra loops,
hidden loops (like in, any etc)

Then use the profiler to identify the hot spots.

Then fine tune the hot spots.
This is where you can start to worry about the speedups of
using local variables etc.

There are two major pieces of code that seem slow, so I'll share the 
first
with you. This section takes up about 1/3 of the time used when running 
all

trials, where trials is 10K or larger.


How are you measuring? Is it via the profiler? Is it by inserying print
time statements? Is is subjectively timing it by hand?


The second section, the one that is taking up most of the time, does the
math.


Thats probably what you would expect if the math is complex.


The list nx1 is about 220 floating point numbers long.


So not very big at all...

sample = random.sample(range(int(self.nx1[b])), trials) # a list of 
sample

values based on nx1


The use of self suggests there is an object, or at least a class definition 
involved?



for s in self.mcrange_gen(sample):
countlist.append(s-1) # This appends the bin number (the number



def mcrange_gen(self, sample):#, lensample):
   lensample = len(sample) # this section is just for speed. All of these

are renames from the globals to bring calc times down at the expense of
memory. I haven't tested these yet.


This is premature optimisation at this stage. Its cluttering up the code
for relatively little benefit.


   for s in range(lensample):
   q = sample[s] #takes the next randomly generated number from the


for q in sample

would be more pythonic


   nx2_append(q) # and appends it to nx list.
   nx2_sort() # and sorts it in place
   nx2_reverse() # reverses the list, because this was the original


So you sort and reverse the entire list every time round the for loop?
Might it be more efficient to keep the list in the right order to start 
with?



   i = nx2_index(q) #get the index of that element
   nx2_remove(q) # and remove the element.


Now you find the thing you inserted and remove it. Wow.


   yield i # send the position of that element back to the main


So you really just want to find out where you would like to insert it
in an already sorted/reversed list?

Back to step one - can you improve the algorithm?

--
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] Tutorial executable from python script.

2010-03-19 Thread Alan Gauld


"Karim Liateni"  wrote

on machine which doesn't have recent version (2.0) of python. 


Given that v2 is at least 10 years old now that's not really "recent"
I'd be surprised if any current Linux distros had anything that old 
on them! Even the ones designed for old hardware.
In fact, are you sure your code runs on v1 python? There have 
been a lot of language changes since then.



the compile()
method but how can I use it to make all in one executable 


compile() doesn't make an exe, it compiles python script into 
python byte code - much as the javac compiler compiles 
java source into java bytecode.


run on old system (old python). 


Exactly how old is old?

If you write the code to run on an old Python interpreter it should 
still work(mostly!) on Python 2.6. The trick is not to compile the 
code but to write code that is consistent wityh the oldest version 
of Python you need to run on.


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] Efficiency and speed

2010-03-19 Thread James Reynolds
Here's another idea I had. I thought this would be slower than then the
previous algorithm because it has another for loop and another while loop. I
read that the overhead of such loops is high, so I have been trying to avoid
using them where possible.

def mcrange_gen(self, sample):
nx2 = self.nx1
for q in sample:
for a in nx2:
while a > q:
 pass
yield a
break


On Fri, Mar 19, 2010 at 3:15 PM, Alan Gauld wrote:

> "James Reynolds"  wrote
>
>
>  I've made a few other optimizations today that I won't be able to test
>> until
>> I get home, but I was wondering if any of you could give some general
>> pointers on how to make python run a little more quickly.
>>
>
> Always, always, get the algorithm efficient before trying to make
> the code efficient.
>
> Then eliminate redundant variable assignments, extra loops,
> hidden loops (like in, any etc)
>
> Then use the profiler to identify the hot spots.
>
> Then fine tune the hot spots.
> This is where you can start to worry about the speedups of
> using local variables etc.
>
>
>  There are two major pieces of code that seem slow, so I'll share the first
>> with you. This section takes up about 1/3 of the time used when running
>> all
>> trials, where trials is 10K or larger.
>>
>
> How are you measuring? Is it via the profiler? Is it by inserying print
> time statements? Is is subjectively timing it by hand?
>
>
>  The second section, the one that is taking up most of the time, does the
>> math.
>>
>
> Thats probably what you would expect if the math is complex.
>
>
>  The list nx1 is about 220 floating point numbers long.
>>
>
> So not very big at all...
>
>
>  sample = random.sample(range(int(self.nx1[b])), trials) # a list of sample
>>> values based on nx1
>>>
>>
> The use of self suggests there is an object, or at least a class definition
> involved?
>
>
>  for s in self.mcrange_gen(sample):
>>countlist.append(s-1) # This appends the bin number (the number
>>
>
>  def mcrange_gen(self, sample):#, lensample):
>>   lensample = len(sample) # this section is just for speed. All of these
>>
>>> are renames from the globals to bring calc times down at the expense of
>>> memory. I haven't tested these yet.
>>>
>>
> This is premature optimisation at this stage. Its cluttering up the code
> for relatively little benefit.
>
>
>for s in range(lensample):
>>   q = sample[s] #takes the next randomly generated number from the
>>
>
> for q in sample
>
> would be more pythonic
>
>
>nx2_append(q) # and appends it to nx list.
>>   nx2_sort() # and sorts it in place
>>   nx2_reverse() # reverses the list, because this was the original
>>
>
> So you sort and reverse the entire list every time round the for loop?
> Might it be more efficient to keep the list in the right order to start
> with?
>
>
>i = nx2_index(q) #get the index of that element
>>   nx2_remove(q) # and remove the element.
>>
>
> Now you find the thing you inserted and remove it. Wow.
>
>
>yield i # send the position of that element back to the main
>>
>
> So you really just want to find out where you would like to insert it
> in an already sorted/reversed list?
>
> Back to step one - can you improve the algorithm?
>
> --
> Alan Gauld
> Author of the Learn to Program web site
> http://www.alan-g.me.uk/
>
> ___
> Tutor maillist  -  Tutor@python.org
> To unsubscribe or change subscription options:
> http://mail.python.org/mailman/listinfo/tutor
>
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] difflib context to string-object?

2010-03-19 Thread Steven D'Aprano
On Fri, 19 Mar 2010 09:55:06 pm Karjer Jdfjdf wrote:
> With difflib.context_diff it is possible to write the context to
> files.
>
> difflib.context_diff(a, b[, fromfile][, tofile][, fromfiledate][,
> tofiledate][, n][, lineterm])
>
> Is it also possible to do this to seperate string-objects instead of
> writing them to files?


Use StringIO objects, which are fake files that can be read as strings.

Untested and entirely from memory, use:

from StringIO import StringIO
fromfile = StringIO("some text goes here")
tofile = StringIO("some different text goes here")




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


Re: [Tutor] using pythnn to open a password protected website

2010-03-19 Thread Steven D'Aprano
On Fri, 19 Mar 2010 11:33:36 pm richard west wrote:
> Hi,
>
> Im trying to use python to open up a password protected website(e.g.
> facebook / gmail) in Firefox. supplying the login and password
> automatically at runtime - so that I can interface my code with
> fingerprint recognition code. So far I have only found urllib,
> urllib2 and web browser, none of which I have been able to use.
>
> urllib2 can log me in, but then I cant pass this authenicated login
> to the browser. At the very least I need to be able to send the form
> post information to the browser via my code, and preferably
> auto-submit the form.

You might like to look at Mechanize, which is a third-party Python 
project for dealing with just that sort of problem.




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


Re: [Tutor] Efficiency and speed

2010-03-19 Thread Luke Paireepinart
On Fri, Mar 19, 2010 at 3:17 PM, James Reynolds  wrote:

> Here's another idea I had. I thought this would be slower than then the
> previous algorithm because it has another for loop and another while loop. I
> read that the overhead of such loops is high, so I have been trying to avoid
> using them where possible.
>
> def mcrange_gen(self, sample):
> nx2 = self.nx1
> for q in sample:
> for a in nx2:
> while a > q:
>  pass
> yield a
> break
>
> One thing to consider is whether you need to run this simulation online or
offline.
That is, whether you can reorder your sample space.

IFF you can reorder your sample space, then this algorithm can be extremely
simplified / sped up,
because then you can sort both the sample space and the binning list in
numerical order
Then you simply do a single pass through each list and only spend O(n+m)
time to categorize your entire sample space.
Sort of how a "merge" step of "merge sort" works, except rather than
emitting the sort order you would just be emitting
tuples with the sample value and its corresponding bin index.

If this doesn't make sense let me know.
If you are able to run the sim offline and reorder your sample space,
and I'm not just being dumb & misunderstanding what you're trying to do
(which is entirely possible),
try it this way.  If you have problems with my explanation, let me know and
I'll try to explain it better.

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


Re: [Tutor] Efficiency and speed

2010-03-19 Thread Alan Gauld


"James Reynolds"  wrote


Here's another idea I had. I thought this would be slower than then the
previous algorithm because it has another for loop and another while 
loop. I
read that the overhead of such loops is high, so I have been trying to 
avoid

using them where possible.


Thats often true but nested loops are sometimes unavoidable.
Its unnecessary nests that are really bad!

But the other important rule when tuning performamce is: don't guess, 
measure.
Put the new design in and test it to see if it runs any faster. Ultimately 
thats
the only thing that matters (assuming its giving the correct results of 
course!)



   def mcrange_gen(self, sample):
   nx2 = self.nx1
   for q in sample:
   for a in nx2:
   while a > q:
pass
   yield a
   break


You shouldn't need the break after the yield. The yield returns a value
and the loop iteration ends naturally.

Alan G. 



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


Re: [Tutor] Efficiency and speed

2010-03-19 Thread Stefan Behnel

James Reynolds, 19.03.2010 21:17:

Here's another idea I had. I thought this would be slower than then the
previous algorithm because it has another for loop and another while loop. I
read that the overhead of such loops is high, so I have been trying to avoid
using them where possible.


Premature optimisation is the source of all evil.



 def mcrange_gen(self, sample):
 nx2 = self.nx1
 for q in sample:
 for a in nx2:
 while a>  q:
  pass


Looks like an infinite loop to me.

Stefan

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