Re: [Tutor] List comprehension question

2010-11-08 Thread Steven D'Aprano

Richard D. Moores wrote:


So this version of my function uses a generator, range(), no?

def proper_divisors(n):
sum_ = 0
for x in range(1,n):
if n % x == 0:
sum_ += x
return sum_


I'm going to be pedantic here... but to quote the Middleman, specificity 
is the soul of all good communication.


The term "generator" in Python refers specifically to an object akin to 
ordinary functions. Instead of using return, they use yield:


def g(n):
n = 2*n
yield n
yield n+1

g itself is a "generator function", and the result of calling g is a 
"generator object". In practice, people tend to be sloppy and just refer 
to both the function g and the result of calling it as generators, 
expecting the reader to tell from context which is which.


But pedantically, we can see the difference:

>>> g  # the function itself

>>> type(g)

>>> g(1)  # the result of calling the function

>>> type(g(1))


Generators are a special type of "iterators" -- iterators are objects 
which can be iterated over, like lists, strings, and many others. In 
this case, the generator is a special type of executable function which 
can be paused mid-process. If you do this:


it = g(3)  # say
next(it)

the built-in next() function starts executing the generator object. The 
code is executed up to the first yield statement:


n = 2*n
yield n

and so next(it) returns 2*3 = 6. Then execution pauses, and the 
generator just waits.


(Aside: in Python 2.x, next(it) is written it.next() instead.)

The next time you call next(it), execution continues until the next yield:

yield n+1

and so next(it) will now return 6+1 = 7.

Finally, if you call next(it) again, execution drops off the end of the 
code, and the generator will raise StopIteration.


Now, this seems a bit complicated, but Python can handle most of that 
for you. Instead of manually calling next() on the generator object, if 
you use it in a for-loop:


for i in g(3):
print(i)

the for-loop will handle calling next and catching the StopIterator, and 
you simply see i = 6, 7. Or you can pass it to functions such as list:


list(g(3))

and get pretty much the same result.

(For advanced use: in newer versions of Python, generators have become 
even more powerful, but complex, objects called co-routines. Co-routines 
allow you to send data *into* the middle of the running code, as well as 
extract data using yield.)


Python also has "generator expressions" -- this is a short-cut syntax 
for particularly simple generators. Generator expressions look just like 
list expressions, except they use round brackets instead of square:


>>> (x+1 for x in (1, 2, 4) if x%2 == 0)
 at 0xb7a7be3c>

List comprehensions run all the way to the end, producing a list. 
Generator expressions run lazily, only producing values one at a time as 
needed. So compare:


sum([list comp]) vs sum(gen expr)

The first case needs to find storage for the entire list first, which is 
potentially huge. But the second case just adds the numbers one at a 
time, and so never needs much memory.


So, that's generators... what about range? range is also an iterator, 
that is, something you can iterate over, but it's not a generator:


>>> type(range(20))


It is its own special type of object, like list, str, int or dict.


Coming back to your function:

def proper_divisors(n):
sum_ = 0
for x in range(1,n):
if n % x == 0:
sum_ += x
return sum_


we can write that much more simply:

def proper_divisors(n):
return sum(x for x in range(1, n) if n%x == 0)


And we can speed it up a bit by realising that there's no need to go all 
the way up to n in the range. After all, we know that (say) 100%96 can't 
possibly be zero. The largest factor of n is sqrt(n):



def proper_divisors(n):
return sum(x for x in range(1, int(math.sqrt(n))) if n%x == 0)


For large n, that will save a lot of time.




--
Steven

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


Re: [Tutor] List comprehension question

2010-11-08 Thread Alan Gauld


"Steven D'Aprano"  wrote

I'm going to be pedantic here... but to quote the Middleman, 
specificity is the soul of all good communication.


Be pedantic! :-)
I really liked the explanation although I already sort of knew most of 
it.

But there were a few nuggets in there I'd missed, like range() being
a type all of its own.

But one, slightly off-topic, question:


def proper_divisors(n):
return sum(x for x in range(1, int(math.sqrt(n))) if n%x == 0)



Why use math.sqrt() instead of just using the ** operator?

return sum(x for x in range(1, int(n**0.5)) if n%x == 0)

I'd have expected ** to be significantly faster than calling the
function, and given this is a performance tweak...?

--
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] List comprehension question

2010-11-08 Thread Stefan Behnel

Alan Gauld, 08.11.2010 17:28:

"Steven D'Aprano" wrote

def proper_divisors(n):
return sum(x for x in range(1, int(math.sqrt(n))) if n%x == 0)


Why use math.sqrt() instead of just using the ** operator?

return sum(x for x in range(1, int(n**0.5)) if n%x == 0)

I'd have expected ** to be significantly faster than calling the
function, and given this is a performance tweak...?


Since this operation is only evaluated once in the whole runtime of the 
loop, I think readability beats the likely very tiny performance difference 
here.


Stefan

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


Re: [Tutor] List comprehension question

2010-11-08 Thread Stefan Behnel

Hugo Arts, 08.11.2010 00:53:

On Mon, Nov 8, 2010 at 12:36 AM, Richard D. Moores wrote:

def proper_divisors(n):
 """
 Return the sum of the proper divisors of positive integer n
 """
 return sum([x for x in range(1,n) if int(n/x) == n/x])

The list comprehension is this function is inefficient in that it computes
n/x twice. I'd like to do an  a = n/x and use a in
"if int(a) == a", but I don't know how.



You can't do that inside a list comprehension. Either get rid of the
comprehension and do a regular loop, or get rid of the n/x expression.

I'd suggest replacing the whole check with x % n == 0. n is a proper
divisor of x if there is no remainder after division, after all. This
also means you won't have to do a cast, which tend to be fairly
expensive.

On another note, getting rid of the list comprehension and using a
generator expression will be even faster, since you won't have to
build the list.


I gave this suggestion a try. It is true for me when run in CPython 3.2:

$ python3 -m timeit -s 'from divisors import forloop' 'forloop(100)'
10 loops, best of 3: 161 msec per loop
$ python3 -m timeit -s 'from divisors import genexp' 'genexp(100)'
10 loops, best of 3: 159 msec per loop
$ python3 -m timeit -s 'from divisors import listcomp' 'listcomp(100)'
10 loops, best of 3: 171 msec per loop

However, it is no longer true when I run the same thing in Cython:

$ python3 -m timeit -s 'from divisors import forloop' 'forloop(100)'
100 loops, best of 3: 13.6 msec per loop
$ python3 -m timeit -s 'from divisors import genexp' 'genexp(100)'
100 loops, best of 3: 13.6 msec per loop
$ python3 -m timeit -s 'from divisors import listcomp' 'listcomp(100)'
100 loops, best of 3: 12.6 msec per loop

Here, the listcomp clearly wins, i.e.

return sum([x for x in range(1,n) if n%x == 0])

is actually *faster* than the inlined loop for

result = sum(x for x in range(1,n) if n%x == 0)
return result

This totally surprised me, until I figured out what was going on here.

In the case of the listcomp, the inner loop is smaller, it does not contain 
the adding. It only contains the division that filters out the 
non-divisors. Then, from time to time, it calls into the list appender to 
append a Python integer that actually matched the condition. But the final 
list is tiny, so this is done extremely rarely compared to the number of 
loop iterations. The tighter loop simply wins. And summing up a short list 
in another tight loop is just another very fast operation.


Lesson learned:

Sometimes, it can be worth splitting a loop in two, one with a tight filter 
for the values, and another one to work on the remaining values.


Stefan

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


Re: [Tutor] List comprehension question

2010-11-08 Thread Alan Gauld


"Stefan Behnel"  wrote


Why use math.sqrt() instead of just using the ** operator?

return sum(x for x in range(1, int(n**0.5)) if n%x == 0)

Since this operation is only evaluated once in the whole runtime of 
the loop, I think readability beats the likely very tiny performance 
difference here.


Ah, I wondered if readability was the reason. I hadn't thought about
it hard enough to notice the sqrt() would only be called once! :-)

As to readability I personally prefer the exponentiation sign (or even 
pow()!)

and don't think I have ever used math.sqrt() in my 12 years of Python.
But I recognise that some may prefer sqrt, especially if they don't
have a math background.

Alan G. 



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


Re: [Tutor] List comprehension question

2010-11-08 Thread Alan Gauld


"Stefan Behnel"  wrote


On another note, getting rid of the list comprehension and using a
generator expression will be even faster, since you won't have to
build the list.


I gave this suggestion a try. It is true for me when run in CPython 
3.2:


However, it is no longer true when I run the same thing in Cython:
...
Lesson learned:

Sometimes, it can be worth splitting a loop in two, one with a tight 
filter for the values, and another one to work on the remaining 
values.


And another good example of how hard it is to guess the optimal
solution in performance terms. It is one area where nothing beats
trial and test. And another reason why premature optimisation should
be resisted! :-)

Alan G. 



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


[Tutor] Too different 2.6 vs 2.7?

2010-11-08 Thread Jorge Biquez

Hello all.
Newbie question.

Are there really BIG differences between version 2.6 and 2.7?

Under my ubuntu configuration I can not install version 2.7, 
searching the why, but the version 2.6 is maintained and installed by 
the ubuntu software center.


As a newby , trying to learn all the features of all the libraries. 
Will I miss TOO much if I stay under version 2.6? Or it will be 
better to stay under 2.7 (in that case under Windows XP)


Thanks in advance for your comments.

Jorge Biquez

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


Re: [Tutor] Too different 2.6 vs 2.7?

2010-11-08 Thread Karim


Hello Jorge,

I have Ubuntu 10.04 LTS and get the same issue.
I installed 2.7 version manually in another place
using -prefix option. It works as long as you are
pointing to the correct bin (python2.7) and the local
lib/ installation (PYTHONPATH) from python 2.7.

Regards
Karim

On 11/08/2010 09:44 PM, Jorge Biquez wrote:

Hello all.
Newbie question.

Are there really BIG differences between version 2.6 and 2.7?

Under my ubuntu configuration I can not install version 2.7, searching 
the why, but the version 2.6 is maintained and installed by the ubuntu 
software center.


As a newby , trying to learn all the features of all the libraries. 
Will I miss TOO much if I stay under version 2.6? Or it will be better 
to stay under 2.7 (in that case under Windows XP)


Thanks in advance for your comments.

Jorge Biquez

___
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] Too different 2.6 vs 2.7?

2010-11-08 Thread Emile van Sebille

On 11/8/2010 12:44 PM Jorge Biquez said...

Hello all.
Newbie question.

Are there really BIG differences between version 2.6 and 2.7?


No, however there is quite a lot of info at 
http://docs.python.org/dev/whatsnew/2.7.html that details the changes, 
mostly 3.x features/bug-fixes back-ported to 2.7 to provide an easier 
upgrade path.


Emile

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


Re: [Tutor] Too different 2.6 vs 2.7?

2010-11-08 Thread python
Jorge,

Python 2.7 supports an updated version of the Tkinter GUI framework with
support for native themes (ttk). This makes it possible to create
professional looking user interfaces without having to install a
separate GUI framework like wxPython or pyQt.

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


Re: [Tutor] List comprehension question

2010-11-08 Thread Steven D'Aprano

Alan Gauld wrote:


"Steven D'Aprano"  wrote

I'm going to be pedantic here... but to quote the Middleman, 
specificity is the soul of all good communication.


Be pedantic! :-)
I really liked the explanation although I already sort of knew most of it.
But there were a few nuggets in there I'd missed, like range() being
a type all of its own.


Thank you :)


But one, slightly off-topic, question:


def proper_divisors(n):
return sum(x for x in range(1, int(math.sqrt(n))) if n%x == 0)



Why use math.sqrt() instead of just using the ** operator?

return sum(x for x in range(1, int(n**0.5)) if n%x == 0)

I'd have expected ** to be significantly faster than calling the
function, and given this is a performance tweak...?



Mostly personal taste, but I would expect math.sqrt() to be more 
accurate than a generic exponentiation operator, although in this case 
the difference shouldn't matter. If your value for n is so large that 
you need to care about the floating point rounding errors in n**0.5, 
you're probably using the wrong tool.


As for the speed, sqrt() only gets called once, so the difference 
shouldn't matter. But for what it's worth:


[st...@sylar ~]$ python3 -m timeit -s "import math" "math.sqrt(1234567)"
100 loops, best of 3: 0.486 usec per loop
[st...@sylar ~]$ python3 -m timeit "1234567**0.5"
100 loops, best of 3: 0.265 usec per loop

So on my machine, the overhead of calling a function is about 0.2 
micro-seconds. Compare that to:


[st...@sylar ~]$ python3 -m timeit -s "n=1234567" "sum(x for x in 
range(1, int(n**0.5)) if n%x == 0)"

1000 loops, best of 3: 515 usec per loop


So changing from math.sqrt(n) to n**0.5 increases the overall speed by 
approximately 0.04%. Truly a micro-optimization :)



(I'm deliberately not including the time to import math here. I assume 
that the function proper_divisors() will be in a module with other maths 
related functions, and therefore you need to import it regardless.)




--
Steven

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


Re: [Tutor] Server

2010-11-08 Thread Chris King

 On 11/5/2010 8:10 PM, Alan Gauld wrote:


"Chris King"  wrote

If you are using Windows, turn off the built-in firewall. That's 
what fixed my problems.

~Corey



also, it is on the same network, so the server shouldn't be a problem


I think Corey means the firewall on your PC if you have one. It could
be blocking outgoing traffic to uncommon port numbers or somesuch.

Its worth trying if only to eliminate the possibility

Alan G.

___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor
The firewall pops up and I click allow. It has nothing to do with the 
firewall at all. Did you find any other errors at all?
import SocketServer, cPickle, socket
def IP(): return socket.gethostbyname(socket.gethostname()) #return IP address
def echo(self): #the default handling function
if self.data == 'shut down': # if user wants to kill self
self.shut_down() #shut
print 'Recieved %s from %s. Echoing back.' % (self.data, self.client_address)
self.send(self.data) #echo

class BreakOutError(Exception): pass #an error for breaking out

def create_handler(handle_func=echo): #creates an handler

class Handler(SocketServer.BaseRequestHandler): #the handler
'''A handler which calls %s in the handle method.'''%handle_func
def handle(self): #the handle method
self.data = self.request.recv(1024) #the data
handle_func(self) #call the handle method giving self

def send(self, data): self.request.send(data) #send data
def shut_down(self): #if you want to stop server
'''End server loop'''
self.server.shut_self_down = True #set shut down to true
raise BreakOutError #raise error

return Handler
def pickle_echo(self):
print 'Recived %s, a %s, from %s. Echoing back.' % (self.data, type(self.data), self.client_address)
self.send(self.data)

def pickle_HC(handle_func = pickle_echo): #a function that returns a handler that can send a recvieve more than strings if to a pickle client
base = create_handler(handle_func) #make a base
class pickle_Handler(base):
'''A handler which call %s in the handle method and pickle/unpickles on the way in and out.'''%handle_func
def handle(self):
self.data = cPickle.loads(self.request.recv(1024)) #unpickle data
handle_func(self)
def send(self, data): base.send(self, cPickle.dumps(data))
return pickle_Handler
class EB_Server(SocketServer.TCPServer):
'''Error Breakout Server
When you use server.shutdown, it waits for the handle to end, but this is the only place to do stuff.
So the error breakout server uses error to break out of a server loop, which will be caught outside.'''

def __init__(self, address, handler):
'''Init, set self.shutdown to False'''
SocketServer.TCPServer.__init__(self, address, handler) #make a handler from a function
self.shut_self_down = False #When an error is raised and handle_error catches it, it will check if its an breakout error

def handle_error(self, request, address):
'''Test to see if shutting down.'''
if self.shut_self_down: raise BreakOutError #if shutdown, raise breakout error
else: SocketServer.TCPServer.handle_error(self, request, address) #If not, do normal error handling

def run(handle_func = echo, host = 'localhost', port=1024): #init function
try: EB_Server((host, port), create_handler(handle_func)).serve_forever() #serve forever
except BreakOutError: pass #If it tries to break out, catch it before it destroys us at the main level

def pickle_run(handle_func = pickle_echo, host='localhost', port = 1024):
try: EB_Server((host, port), pickle_HC(handle_func)).serve_forever()
except BreakOutError: pass

if __name__ == '__main__':
print 'The IP is %s.' % IP()
run(port = int(raw_input('What port? '))) #if run directly, run server
import socket, cPickle

class NotConnectedError(Exception): pass #A ClosedError

class Client(object): #client object

def __init__(self, host = 'localhost', port = 1024, timeout = None):
print host
self.host = host #set attributes
self.port = port
self.timeout = timeout
self.closed = False

def send(self, data): #send data
try: self.__sock.close() #close old socket
except AttributeError: pass #If there isn't a socket, don't worry about it
self.__sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM) #make a new one
self.__sock.connect((self.host, self.port))
self.closed = False
self.__sock.settimeout(self.timeout)
self.__sock.sendall(data)

def old_send(self, data): #send with same socket
self.__sock.send(data)
def recv(self): #receive data
if self.closed: raise NotConnectedEr

Re: [Tutor] Too different 2.6 vs 2.7?

2010-11-08 Thread Jorge Biquez

Hello all.

So I guess the best is to have version 2.7 working.
Thanks to all for the comments.

Jorge Biquez

At 03:14 p.m. 08/11/2010, pyt...@bdurham.com wrote:

Jorge,

Python 2.7 supports an updated version of the Tkinter GUI framework with
support for native themes (ttk). This makes it possible to create
professional looking user interfaces without having to install a
separate GUI framework like wxPython or pyQt.

Malcolm



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


Re: [Tutor] List comprehension question

2010-11-08 Thread Steven D'Aprano

Stefan Behnel wrote:

Hugo Arts, 08.11.2010 00:53:

[...]

On another note, getting rid of the list comprehension and using a
generator expression will be even faster, since you won't have to
build the list.


I gave this suggestion a try. It is true for me when run in CPython 3.2:

[...]

However, it is no longer true when I run the same thing in Cython:

[...]

Lesson learned:

Sometimes, it can be worth splitting a loop in two, one with a tight 
filter for the values, and another one to work on the remaining values.


Nice analysis, thank you. However, surely the most important lesson to 
be learned is:


Don't make assumptions about what is faster, measure it and find out!



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


[Tutor] Commercial or Famous Applicattions.?

2010-11-08 Thread Jorge Biquez

Hello all.

Newbie question. Sorry.

Can you mention applications/systems/solutions made with Python that 
are well known and used by public in general? ANd that maybe we do 
not know they are done with Python?


I had a talk with a friend, "PHP-Only-Fan", and he said (you know the 
schema of those talks) that "his" language is better and that "just 
to prove it" there are not too many applications done with Python 
than the ones done with PHP and that "that of course is for 
something". That conversation , that by the way I guess is useless at 
all , makes me thing the idea of ask of applications done with 
Python. Not for debate the argument of that guy BUT to learn what can 
be done with Python. In grphical interface, schemas of jobs, etc. I 
know that there are tons of solutions but would like to hear of 
possible about the ones you have used most or recommend the most.


As an example, I love and have used in the last years MAILMAN, never 
crashed, always works even on my small and old Intel Pentium III with 
a 10GB hard disk and 640KB of RAM. Still working and will work for 
sure (running under FreeBsd by the way).


Thanks in advance for your comments.

Jorge Biquez

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


[Tutor] Columnar Transposition Cipher question

2010-11-08 Thread Natalie Kristine T. Castillo

Hi, I need help on how exactly to solve this:

To send secret messages you and your partner have decided to use the  
columnar function you have written to perform columnar transposition  
cipher for this course. You have received the ciphertext  
EXLYHILOSHOOAETU from your friend. You two decide to use the keyword  
MARS. What message did your friend send you? Show your steps and do it  
by hand.


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


Re: [Tutor] Too different 2.6 vs 2.7?

2010-11-08 Thread Terry Carroll

On Mon, 8 Nov 2010, Jorge Biquez wrote:


Are there really BIG differences between version 2.6 and 2.7?


I'm in a similar boat.  I use Ubuntu 10.04 aw well as Windows XP, Vista 
and 7.  I keep to similar levels just to avoid confusion and at present 
run Python 2.6 on all systems.


If I had an urgent enough need, I'd go to ActivePython 2.7 on the Windows 
boxes, but so far, no need other than to have the latest.


The only feature I'm pining for at all is the new argparse; but it depends 
on your needs.


Under my ubuntu configuration I can not install version 2.7, searching the 
why, but the version 2.6 is maintained and installed by the ubuntu software 
center.


It's possible to install 2.7 on Ubuntu, but I'm not piqued enough to do 
so.  If you're interested, a couple posts on Ubuntu Forums describe it:


http://.ubuntuforums.org/showthread.php?t=1524491
http://ubuntuforums.org/showthread.php?t=1582739

As a newby , trying to learn all the features of all the libraries. Will I 
miss TOO much if I stay under version 2.6? Or it will be better to stay under 
2.7 (in that case under Windows XP)


For myself, I think I'll be fine in 2.6.  I usually upgrade when I see a 
particular feature I want.

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


Re: [Tutor] Server

2010-11-08 Thread Alan Gauld


"Chris King"  wrote

I think Corey means the firewall on your PC if you have one. It 
could

be blocking outgoing traffic to uncommon port numbers or somesuch.


The firewall pops up and I click allow. It has nothing to do with 
the

firewall at all. Did you find any other errors at all?


OK, despite the fact that the firewall is evidently being triggered
lets assume that it is not the firewall to blame, do you gt any
other signs of life? Any error messages? Any responses at all?

So far you haven't given us a whole lot of information to go on...
no code, no OS, no network settings. It's hard to be specific.

If the code works on localhost it is unlikely to be the code at fault.
More likely to be a network issue, either a wrong IP address,
port address or, sorry, a firewall setting.


--
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] Too different 2.6 vs 2.7?

2010-11-08 Thread Alan Gauld


"Jorge Biquez"  wrote


Are there really BIG differences between version 2.6 and 2.7?


No.


As a newby , trying to learn all the features of all the libraries.


That will take you a long time. Better to use it writing software 
IMHO.

Learn the libraries you need as you need them.
If you need something remember to look in the library first...
I've been using Python for 12 years now and only used
about 30% of the modules in the library.

Will I miss TOO much if I stay under version 2.6? Or it will be 
better to stay under 2.7 (in that case under Windows XP)


If 2.6 works stick with it for now till somebody in Ubuntu land
fixes the issue.

I still use 2.4 on my netbook, 2.5 on my Mac and 2.6 (and 3.1)
on my desktop PC. I've been thinking of giving 2.7 a whirl but
I'm in no hurry...

--
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] Commercial or Famous Applicattions.?

2010-11-08 Thread Alan Gauld


"Jorge Biquez"  wrote

Can you mention applications/systems/solutions made with Python that 
are well known and used by public in general? ANd that maybe we do 
not know they are done with Python?


The Python web site has an advocacy section, you will find several
success stories  there.

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] Commercial or Famous Applicattions.?

2010-11-08 Thread Luke Paireepinart
just off the top of my head...
NASA uses it.  Lots of games use Python as their game logic/scripting
language (how many use PHP? probably approaching 0.  LUA is more
popular than Python but Python is much  more popular than PHP).  The
first ever bittorrent client (the official one) was written in Python.
 The game Eve Online is written completely in Python.  Youtube uses
lots of Python, Facebook uses some.  Reddit is written in Python as
well.  Google App Engine has both Python and Java interfaces.  They
don't have a PHP interface; I wonder why?

Lots of jobs at Google (even Java jobs and such) require Python
experience; they don't tend to require PHP experience too.  Because
PHP doesn't really teach you much.  The syntax is not nearly as
elegant.  It's buggy and rough around the edges.  They have gross
workarounds for a lot of things that should be language features.

That's not to say that Python's the only language that is better than
PHP for most things.  Ruby is also a good option.  But these languages
are both infinitely more flexible _right now_ (not based almost solely
for web apps) and better to use for almost everything than PHP.

PHP has its place, and part of the reason it still has that place is
because a lot of web hosts haven't started hosting Python and Ruby
frameworks in a scalable, fast way.  But it's not the frameworks'
fault.

That's just my 2c.

On Mon, Nov 8, 2010 at 6:18 PM, Jorge Biquez  wrote:
> Hello all.
>
> Newbie question. Sorry.
>
> Can you mention applications/systems/solutions made with Python that are
> well known and used by public in general? ANd that maybe we do not know they
> are done with Python?
>
> I had a talk with a friend, "PHP-Only-Fan", and he said (you know the schema
> of those talks) that "his" language is better and that "just to prove it"
> there are not too many applications done with Python than the ones done with
> PHP and that "that of course is for something". That conversation , that by
> the way I guess is useless at all , makes me thing the idea of ask of
> applications done with Python. Not for debate the argument of that guy BUT
> to learn what can be done with Python. In grphical interface, schemas of
> jobs, etc. I know that there are tons of solutions but would like to hear of
> possible about the ones you have used most or recommend the most.
>
> As an example, I love and have used in the last years MAILMAN, never
> crashed, always works even on my small and old Intel Pentium III with a 10GB
> hard disk and 640KB of RAM. Still working and will work for sure (running
> under FreeBsd by the way).
>
> Thanks in advance for your comments.
>
> Jorge Biquez
>
> ___
> Tutor maillist  -  tu...@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] Columnar Transposition Cipher question

2010-11-08 Thread Luke Paireepinart
On Mon, Nov 8, 2010 at 5:28 PM, Natalie Kristine T. Castillo
 wrote:
> Hi, I need help on how exactly to solve this:
That's nice.

>
> To send secret messages you and your partner have decided to use the
> columnar function you have written to perform columnar transposition cipher
> for this course. You have received the ciphertext EXLYHILOSHOOAETU from your
> friend. You two decide to use the keyword MARS. What message did your friend
> send you? Show your steps and do it by hand.
>

Where's the function you've written?
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Server

2010-11-08 Thread Luke Paireepinart
On Mon, Nov 8, 2010 at 3:53 PM, Chris King  wrote:
>  On 11/5/2010 8:10 PM, Alan Gauld wrote:
>>
>> "Chris King"  wrote
>>
 If you are using Windows, turn off the built-in firewall. That's what
 fixed my problems.
 ~Corey
>>
>>> also, it is on the same network, so the server shouldn't be a problem
>>
>> I think Corey means the firewall on your PC if you have one. It could
>> be blocking outgoing traffic to uncommon port numbers or somesuch.
>>
>> Its worth trying if only to eliminate the possibility
>>
>
> The firewall pops up and I click allow. It has nothing to do with the
> firewall at all. Did you find any other errors at all?
>

You should disable the firewall completely on both computers to
perform  an actual test.  Also, perhaps be nicer to people who are
giving up their time to try to help you?
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Commercial or Famous Applicattions.?

2010-11-08 Thread Walter Prins
Hi Jorge,

Have a look at this page:
http://www.python.org/about/quotes/

A few choice points to maybe make him think:
1.) Python is one of the top 3 languages at Google.  (Where is PHP?...)
2.) Python can be used for GUI apps and has bindings for several GUI widget
sets.  (PHP?)
3.) Python can be used for Command line apps (With some work, PHP can be
too, but it's not exactly natural.)
4.) Python can be used for shell type scripts  (OK, so can PHP, but again
it's not really its forte)
5.) Python can be used for text processing and is good at it (e.g. lxml,
Beautiful soup, good regex support, etc. etc... PHP?)
6.) Python can be used for games (as demonstrated by Eve online for example.
50,000 simultaneous players.  PHP? I think not...)
7.) Python can be used for heavy duty number crunching (e.g. Numpy and
friends.  PHP?...)
8.) Python can be used for image manipulation  (e.g. PIL and friends.
PHP?...)
9.) Python easily runs or is available on multiple platforms, including
Windows, Linux, MacOS, as well as other more esoteric platforms, e.g.
Android phones.  (PHP?)
etc. etc...

I think that's enough for now... you get the picture.  (Obviously I'm a bit
biased ;) )

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


Re: [Tutor] Server

2010-11-08 Thread Chris King

 On 11/8/2010 8:20 PM, Alan Gauld wrote:


"Chris King"  wrote


I think Corey means the firewall on your PC if you have one. It could
be blocking outgoing traffic to uncommon port numbers or somesuch.



The firewall pops up and I click allow. It has nothing to do with the
firewall at all. Did you find any other errors at all?


OK, despite the fact that the firewall is evidently being triggered
lets assume that it is not the firewall to blame, do you gt any
other signs of life? Any error messages? Any responses at all?

So far you haven't given us a whole lot of information to go on...
no code, no OS, no network settings. It's hard to be specific.

If the code works on localhost it is unlikely to be the code at fault.
More likely to be a network issue, either a wrong IP address,
port address or, sorry, a firewall setting.


Actually, I fixed the problem myself. On the server, I had to set the 
host to an empty to string. Now I am wondering, how do you exit a server 
loop within the handler, so you can use the port again without it 
throwing a fit.

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


Re: [Tutor] Columnar Transposition Cipher question

2010-11-08 Thread Wayne Werner
On Mon, Nov 8, 2010 at 5:28 PM, Natalie Kristine T. Castillo <
ncasti...@umail.ucsb.edu> wrote:

> Hi, I need help on how exactly to solve this:
>
> To send secret messages you and your partner have decided to use the
> columnar function you have written to perform columnar transposition cipher
> for this course. You have received the ciphertext EXLYHILOSHOOAETU from your
> friend. You two decide to use the keyword MARS. What message did your friend
> send you? Show your steps and do it by hand.


This sounds an awful lot like homework. It's not our policy to solve
homework, but if you get stuck we'll happily give you nudges in the right
direction.

This problem also happens to be language agnostic - meaning it doesn't
matter whether you solve it in Python, Assembly, C++, Ruby, or by hand.

Of course the problem description also says show your steps and do it by
hand, which suggests that the problem is not terribly difficult to solve if
you have some familiarity with the columnar transposition cipher. I've never
heard of it before, but I'm sure www.google.com and
http://en.wikipedia.orghave plenty to say on the subject.

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


Re: [Tutor] Networking

2010-11-08 Thread Chris King

 On 10/14/2010 9:28 PM, James Mills wrote:

On Fri, Oct 15, 2010 at 11:22 AM, chris  wrote:

But what if I want it to serve one client, go to another and then go back.
How does that work?

You do some I/O multi-plexing or multi-processing/threading.

You might want to do some reading on this.

cheers
James


or I could just have one client use multiple socket objects
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


[Tutor] query result set

2010-11-08 Thread Shawn Matlock
I am able to successfully query a MySQL database using zxJDBC but the result 
set seems odd to me. It returns the following:

[(u'envDB', u'systest2'), (u'envDir', u'st2'), (u'envCellName', 
u'Systest2Cell'), (u'envFrontEnd', u'systest2FrontEnd'), (u'envTag', 
u'system_test2')]

Why is there a "u" before every entry? Can someone point me to an example that 
puts the results into a List (or Dictionary) without the "u"?

Thank you,
Shawn



csdbConn = zxJDBC.connect("jdbc:mysql://myhost:3306/mydb", "User", "Password", 
"com.mysql.jdbc.Driver")

csdbCursor = csdbConn.cursor(1)

envsql = "select envVariable, envValue from ODS_ENV_DICT where envName = 'ST2'"

csdbCursor.execute(envsql)

print csdbCursor.fetchall()

csdbCursor.close()
csdbConn.close()

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


Re: [Tutor] Columnar Transposition Cipher question

2010-11-08 Thread Luke Pettit
I can suggest that the X should probably be a "D" you may want to check this
with your tutor
Although as a python noob I don't have the foggiest on how to do this in
python, but will keep this for an exercise for if, and when I do.
On paper it's pretty simple once you Wikipedia it.

On 9 November 2010 12:55, Wayne Werner  wrote:

> On Mon, Nov 8, 2010 at 5:28 PM, Natalie Kristine T. Castillo <
> ncasti...@umail.ucsb.edu> wrote:
>
>> Hi, I need help on how exactly to solve this:
>>
>> To send secret messages you and your partner have decided to use the
>> columnar function you have written to perform columnar transposition cipher
>> for this course. You have received the ciphertext EXLYHILOSHOOAETU from your
>> friend. You two decide to use the keyword MARS. What message did your friend
>> send you? Show your steps and do it by hand.
>>
>
> This sounds an awful lot like homework. It's not our policy to solve
> homework, but if you get stuck we'll happily give you nudges in the right
> direction.
>
> This problem also happens to be language agnostic - meaning it doesn't
> matter whether you solve it in Python, Assembly, C++, Ruby, or by hand.
>
> Of course the problem description also says show your steps and do it by
> hand, which suggests that the problem is not terribly difficult to solve if
> you have some familiarity with the columnar transposition cipher. I've never
> heard of it before, but I'm sure www.google.com and
> http://en.wikipedia.org have plenty to say on the subject.
>
> HTH,
> Wayne
>
> ___
> Tutor maillist  -  Tutor@python.org
> To unsubscribe or change subscription options:
> http://mail.python.org/mailman/listinfo/tutor
>
>


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


Re: [Tutor] Columnar Transposition Cipher question

2010-11-08 Thread Steven D'Aprano
On Mon, Nov 08, 2010 at 03:28:44PM -0800, Natalie Kristine T. Castillo wrote:
> Hi, I need help on how exactly to solve this:
>
> To send secret messages you and your partner have decided to use the
> columnar function you have written to perform columnar transposition
> cipher for this course. You have received the ciphertext
> EXLYHILOSHOOAETU from your friend. You two decide to use the keyword
> MARS. What message did your friend send you? Show your steps and do it
> by hand.

This link might help:
http://en.wikipedia.org/wiki/Transposition_cipher#Columnar_transposition

Otherwise, this isn't exactly on-topic for a Python list, particularly
since you're told to do it by hand. But in case anyone is interested:

http://pypi.python.org/pypi/obfuscate

Good luck :)


--
Steven


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


Re: [Tutor] query result set

2010-11-08 Thread Steven D'Aprano
On Mon, Nov 08, 2010 at 05:58:36PM -0800, Shawn Matlock wrote:
> I am able to successfully query a MySQL database using zxJDBC but the result 
> set seems odd to me. It returns the following:
> 
> [(u'envDB', u'systest2'), (u'envDir', u'st2'), (u'envCellName', 
> u'Systest2Cell'), (u'envFrontEnd', u'systest2FrontEnd'), (u'envTag', 
> u'system_test2')]
> 
> Why is there a "u" before every entry? Can someone point me to an example 
> that puts the results into a List (or Dictionary) without the "u"?


No, you have misunderstood what you are asking for. That is like asking
for lists without [] or dicts without {}.

In Python 2.x, there are two types of strings: byte strings, which 
have delimiters " ", and unicode (text) strings, which have delimiters
u" and ". Notice that the u is not part of the string contents, but is
part of the delimiter, just like the " in byte strings, or [ and ] for 
lists.

Coming from a database, the strings are Unicode, which means that they could
contain characters that can't be stored in byte-strings. So you have to use
Unicode, which means the object repr() looks like:

u"text inside the quotation marks"

But if you print them, you get:

text inside the quotation marks

*without* the quotation marks included. The slight complication is that 
if you put the Unicode string inside a list, dict or tuple, Python prints
the repr() which means you see the u" ". If you don't like it, write a 
helper function that prints the list the way you want, or try the pprint 
module.

-- 
Steven














> 
> Thank you,
> Shawn
> 
> 
> 
> csdbConn = zxJDBC.connect("jdbc:mysql://myhost:3306/mydb", "User", 
> "Password", "com.mysql.jdbc.Driver")
> 
> csdbCursor = csdbConn.cursor(1)
> 
> envsql = "select envVariable, envValue from ODS_ENV_DICT where envName = 
> 'ST2'"
> 
> csdbCursor.execute(envsql)
> 
> print csdbCursor.fetchall()
> 
> csdbCursor.close()
> csdbConn.close()
> 
> ___
> 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] List comprehension question

2010-11-08 Thread Richard D. Moores
On Mon, Nov 8, 2010 at 03:43, Steven D'Aprano  wrote:
> Richard D. Moores wrote:

> Coming back to your function:
>
> def proper_divisors(n):
>    sum_ = 0
>    for x in range(1,n):
>        if n % x == 0:
>            sum_ += x
>    return sum_
>
>
> we can write that much more simply:
>
> def proper_divisors(n):
>    return sum(x for x in range(1, n) if n%x == 0)
>
>
> And we can speed it up a bit by realising that there's no need to go all the
> way up to n in the range. After all, we know that (say) 100%96 can't
> possibly be zero. The largest factor of n is sqrt(n):
>
>
> def proper_divisors(n):
>    return sum(x for x in range(1, int(math.sqrt(n))) if n%x == 0)
>
>
> For large n, that will save a lot of time.

That sqrt(n) works for speeding up the finding of primes, but here we
want to use int(n/2) (and why didn't I think of that?), which results
in about a 2x speedup. See .

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


Re: [Tutor] List comprehension question

2010-11-08 Thread Stefan Behnel

Richard D. Moores, 09.11.2010 06:31:

On Mon, Nov 8, 2010 at 03:43, Steven D'Aprano wrote:

Richard D. Moores wrote:



Coming back to your function:

def proper_divisors(n):
sum_ = 0
for x in range(1,n):
if n % x == 0:
sum_ += x
return sum_

we can write that much more simply:

def proper_divisors(n):
return sum(x for x in range(1, n) if n%x == 0)


And we can speed it up a bit by realising that there's no need to go all the
way up to n in the range. After all, we know that (say) 100%96 can't
possibly be zero. The largest factor of n is sqrt(n):

def proper_divisors(n):
return sum(x for x in range(1, int(math.sqrt(n))) if n%x == 0)


For large n, that will save a lot of time.


That sqrt(n) works for speeding up the finding of primes, but here we
want to use int(n/2) (and why didn't I think of that?), which results
in about a 2x speedup.


Ah, good catch. Another lesson learned:

When you optimise, make sure you have good unit test coverage that proves 
you didn't break anything by trying to make things faster.


But Steven is right in that it's enough to run up to sqrt(n). You can 
improve things again by collecting not only the divisor you find but also 
its counterpart factor at the same time, i.e. when you find out that 2 is a 
divisor of n, add both 2 and n/2.


Then the point to stop is really sqrt(n).

Finally, sorting the resulting list will be a quick operation compared to 
finding the divisors.


Stefan

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


Re: [Tutor] List comprehension question

2010-11-08 Thread Richard D. Moores
On Mon, Nov 8, 2010 at 21:31, Richard D. Moores  wrote:

> That sqrt(n) works for speeding up the finding of primes, but here we
> want to use int(n/2) (and why didn't I think of that?), which results
> in about a 2x speedup. See .

NO! Use  int(n/2)+1 .  I'll correct that in
 and report back.

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


Re: [Tutor] List comprehension question

2010-11-08 Thread Richard D. Moores
On Mon, Nov 8, 2010 at 22:47, Richard D. Moores  wrote:
> On Mon, Nov 8, 2010 at 21:31, Richard D. Moores  wrote:
>
>> That sqrt(n) works for speeding up the finding of primes, but here we
>> want to use int(n/2) (and why didn't I think of that?), which results
>> in about a 2x speedup. See .
>
> NO! Use  int(n/2)+1 .  I'll correct that in
>  and report back.

See 

But now I'll have to redo again using Stefan's ingenious suggestion.

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