Re: [Tutor] binary, ascii, steganography

2011-03-19 Thread Alan Gauld

"Steven D'Aprano"  wrote

with how to get binary to text, so your suggestions are greatly 
appreciated.

 I'll get to it! (0001 = a, 0010 = b, ...)


>>> int('01101110', 2)
110
>>> chr(110)
'n'


And if you really want to use non standard character values you
could look at the string maketrans() function and string.translate()
method...

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] Waiting until a thread ends

2011-03-19 Thread lists
>
>
>> Only really glanced at this, but you seem to be checking only the last
>>> thread *after* the loop?  Surely you should be storing all the threads in a
>>> list (or someplace) as you create them, and then check them all for liveness
>>> and if so join them each in turn, to ensure you only print 'FINISHED' once
>>> you've checked and confirmed that all the threads created have in fact
>>> finished.
>>>
>>> Walter
>>>
>>>
>> That makes absolute sense. Doh on my part!
>>
>> Thanks!
>>
>>
> Just done a little more reading and came across this in an O'Reilly article
> here http://www.oreillynet.com/onlamp/blog/2008/01/pymotw_threading.html
>
> Seems like an elegant way to accomplish a wait until all running threads
> have finished.
>
> Using enumerate() to wait for all running threads:
>
> It is not necessary to retain an explicit handle to all of the daemon
> threads you start in order to ensure they have completed before exiting the
> main process. threading.enumerate()returns a list of active Thread instances.
> The list includes the current thread, and since joining the current thread
> is not allowed (it introduces a deadlock situation), we must check before
> joining.
>
>
In my continuing quest the find the best way of doing this I came across the
following method:

for thread in threading.enumerate():
if thread is not threading.currentThread():
thread.join()
print 'FINISHED'

In my newbie understanding, you can't join() the current thread, because
it's the main thread (the one from which the others are called), join()ing
it would lock the program up (it would never complete).

The above only join()s a thread if it isn't the current thread, thus
(hopefully) getting around this. Swapping my earlier stupid code for this
seems to work as expected in my tests.

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


[Tutor] lambda

2011-03-19 Thread Ajit Deshpande
I am trying to figure out where lambda functions can be useful. Has anyone
used them in real world?

>From my reading so far, I hear people claim that lambda can be a useful
replacement for small functions. Most examples  didn't make much sense to
me. Why would anyone use a one liner anonymous function, if you never plan
to use it elsewhere? You would then be implementing the logic directly in
the line, isn't it?. Functions are useful if they plan to get called
multiple times within your code.

For example:

add_one = lambda x: x + 1

In real world, why would I use a lambda for this. I would simply do:

add_one = x + 1

Can you provide some useful use cases for lambda functions?

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


[Tutor] Need some clarification on this

2011-03-19 Thread Yaşar Arabacı

>>>a=5
>>>b=5
>>>a == b
True
>>>a is b
True

My question is, why "a is b" is true. What I expected it to be is that, 
a and b are different things with same value.


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


Re: [Tutor] Need some clarification on this

2011-03-19 Thread Joel Goldstick
2011/3/19 Yaşar Arabacı 

> >>>a=5
> >>>b=5
> >>>a == b
> True
> >>>a is b
> True
>
> My question is, why "a is b" is true. What I expected it to be is that, a
> and b are different things with same value.
>
> ___
> Tutor maillist  -  Tutor@python.org
> To unsubscribe or change subscription options:
> http://mail.python.org/mailman/listinfo/tutor
>


True means not zero, not "".  Any numeric value but 0 is true.  so you have
True and True.

I think what you are thinking of would be a == b.  Since a does not equal b,
the result is False

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


Re: [Tutor] Need some clarification on this

2011-03-19 Thread Joel Goldstick
2011/3/19 Yaşar Arabacı 

> >>>a=5
> >>>b=5
> >>>a == b
> True
> >>>a is b
> True
>
> My question is, why "a is b" is true. What I expected it to be is that, a
> and b are different things with same value.
>
> ___
> Tutor maillist  -  Tutor@python.org
> To unsubscribe or change subscription options:
> http://mail.python.org/mailman/listinfo/tutor
>

Oops.  I misread your post.  I (why I don't know) thought I saw a = 5, b =
4.

a and b are names.  Python has an integer of 5 from a = 5, so it just refers
to that same object with b.



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


Re: [Tutor] lambda

2011-03-19 Thread Adam Bark

On 19/03/11 14:44, Ajit Deshpande wrote:
I am trying to figure out where lambda functions can be useful. Has 
anyone used them in real world?


From my reading so far, I hear people claim that lambda can be a 
useful replacement for small functions. Most examples  didn't make 
much sense to me. Why would anyone use a one liner anonymous function, 
if you never plan to use it elsewhere? You would then be implementing 
the logic directly in the line, isn't it?. Functions are useful if 
they plan to get called multiple times within your code.


For example:

add_one = lambda x: x + 1

In real world, why would I use a lambda for this. I would simply do:

add_one = x + 1

Can you provide some useful use cases for lambda functions?

~ Ajit Deshpande


Here's one from the python docs, reduce takes a function so technically 
the programmer only uses it once but the program calls it many times. 
Map is another that works similarly. Have a look into functional 
programming as that's the branch of computer science those two functions 
and lambdas tend to come from I believe.

http://docs.python.org/library/functions.html?highlight=lambda#reduce

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


Re: [Tutor] lambda

2011-03-19 Thread bob gailer

On 3/19/2011 9:44 AM, Ajit Deshpande wrote:
I am trying to figure out where lambda functions can be useful. Has 
anyone used them in real world?


From my reading so far, I hear people claim that lambda can be a 
useful replacement for small functions. Most examples  didn't make 
much sense to me. Why would anyone use a one liner anonymous function, 
if you never plan to use it elsewhere? You would then be implementing 
the logic directly in the line, isn't it?. Functions are useful if 
they plan to get called multiple times within your code.


For example:

add_one = lambda x: x + 1

In real world, why would I use a lambda for this. I would simply do:

add_one = x + 1

Can you provide some useful use cases for lambda functions?


One is the list sort method.

someList.sort([cmp[, key[, reverse]]])

/"cmp/ specifies a custom comparison function of two arguments (list 
items) which should return a negative, zero or positive number depending 
on whether the first argument is considered smaller than, equal to, or 
larger than the second argument: cmp=lambda x,y: cmp(x.lower(), y.lower())."


--
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] lambda

2011-03-19 Thread bob gailer

In my Python Pipelines program I have the following

  opts = {
'characters' : (lambda rec, tot: tot + len(rec), 0),
'words' :  (lambda rec, tot: tot + len(rec.split()), 0),
'lines' :  (lambda rec, tot: tot + 1, 0),
'minline' :(lambda rec, tot: min(len(rec), tot), 9),
'maxline' :(lambda rec, tot: max(len(rec), tot), 0)
  }

The user specifies option(s) (the dictionary keys). Thje program then 
creates a class instance for each option, passing the corresponding 
function as a class initialization parameter:


for option in options:
  func, initTotal = Count.opts.get(option , 0)
  if func:
ctr = Count.CounterClass(func, spec, initTotal)

it made lots of sense to me to code these as lambdas.

The alternatve would have been 4 defs: (4 more lines of code and more 
indirection in code reading).


def char(rec, tot): return tot + len(rec)
etc

--
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] Need some clarification on this

2011-03-19 Thread Emmanuel Ruellan
2011/3/19 Yaşar Arabacı 

>
>
> >>>a=5
> >>>b=5
> >>>a == b
> True
> >>>a is b
> True
>
> My question is, why "a is b" is true. What I expected it to be is that, a
> and b are different things with same value.
>
Even stranger:

>>> a = 10**10
>>> b = 10**10
>>> a == b
True
>>> a is b
False

>>> a = 5
>>> b = 5
>>> a == b
True
>>> a is b
True

In the general case, you're right: a and b point to two different objects,
but there is also some kind of optimisation for small numbers, and as a
result when a = 5 and b = 5, both point the same '5' object.

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


Re: [Tutor] Need some clarification on this

2011-03-19 Thread xDog Walker
On Saturday 2011 March 19 08:35, Emmanuel Ruellan wrote:
> 2011/3/19 Yaşar Arabacı 
>
> > >>>a=5
> > >>>b=5
> > >>>a == b
> >
> > True
> >
> > >>>a is b
> >
> > True
> >
> > My question is, why "a is b" is true. What I expected it to be is that, a
> > and b are different things with same value.
>
> Even stranger:
> >>> a = 10**10
> >>> b = 10**10
> >>> a == b
>
> True
>
> >>> a is b
>
> False
>
> >>> a = 5
> >>> b = 5
> >>> a == b
>
> True
>
> >>> a is b
>
> True
>
> In the general case, you're right: a and b point to two different objects,
> but there is also some kind of optimisation for small numbers, and as a
> result when a = 5 and b = 5, both point the same '5' object.
>
> Emmanuel Ruellan

From http://docs.python.org/c-api/int.html

The current implementation keeps an array of integer objects for all 
integers between -5 and 256, when you create an int in that range you 
actually just get back a reference to the existing object.
-- 
I have seen the future and I am not in it.
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Need some clarification on this

2011-03-19 Thread Hugo Arts
2011/3/19 Yaşar Arabacı :
a=5
b=5
a == b
> True
a is b
> True
>
> My question is, why "a is b" is true. What I expected it to be is that, a
> and b are different things with same value.
>

It's an optimization thing. When you type "a=5," the python
interpreter is obligated to give you an integer object with the value
5. However, it doesn't need to be a *new* object. If an interpreter
happens to have an object like that lying around already, it is
perfectly allowed to give you that one.

This doesn't mess things up for anyone, because integers are
immutable. You can't change them, so it's safe to give the same object
to different people. Interpreters can make the same optimization for
strings, if they like. It makes it more efficient (since they don't
have to allocate a new object) without changing the semantics of the
language.
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Need some clarification on this

2011-03-19 Thread Ajit Deshpande
This is a special feature called interning in python. As of Python 2.6,
values of -5 to 105 are never cleared from memory for performance reasons.
This is applicable to integers only.

"==" is a value comparator, whereas "is" is a reference compartor.

Check this interesting extension to your code:
>>> a=5.0
>>> b=5.0
>>> a==b
True
>>> a is b
False

Because I used "5.0" instead of "5", the "is" operator is giving a different
result ("False")

~ Ajit Deshpande


2011/3/19 Yaşar Arabacı 

> >>>a=5
> >>>b=5
> >>>a == b
> True
> >>>a is b
> True
>
> My question is, why "a is b" is true. What I expected it to be is that, a
> and b are different things with same value.
>
> ___
> 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] Need some clarification on this

2011-03-19 Thread Dave Angel

On 01/-10/-28163 02:59 PM, Joel Goldstick wrote:

2011/3/19 Yaşar Arabacı


a=5
b=5
a == b

True

a is b

True

My question is, why "a is b" is true. What I expected it to be is that, a
and b are different things with same value.

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



Oops.  I misread your post.  I (why I don't know) thought I saw a = 5, b =
4.

a and b are names.  Python has an integer of 5 from a = 5, so it just refers
to that same object with b.





Sometimes a particular implementation of Python will reuse the same 
object, if it's immutable.  So certain strings and certain integers may 
be reused, presumably to save some space.  But you cannot count on it.


If you tried the same thing with a value of 741, you'd probably get a 
different answer.  It's not time-efficient for Python to go hunting 
through all the existing objects to find a match, so this optimization 
is done pretty narrowly.


You seldom want to use 'is' on an integer or a string anyway.  Use == 
unless it really matters, which is usually either a custom object, or a 
singleton like True.


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


Re: [Tutor] lambda

2011-03-19 Thread Dave Angel

On 01/-10/-28163 02:59 PM, Ajit Deshpande wrote:

I am trying to figure out where lambda functions can be useful. Has anyone
used them in real world?


From my reading so far, I hear people claim that lambda can be a useful

replacement for small functions. Most examples  didn't make much sense to
me. Why would anyone use a one liner anonymous function, if you never plan
to use it elsewhere? You would then be implementing the logic directly in
the line, isn't it?. Functions are useful if they plan to get called
multiple times within your code.

For example:

add_one = lambda x: x + 1

In real world, why would I use a lambda for this. I would simply do:

add_one = x + 1

Can you provide some useful use cases for lambda functions?

~ Ajit Deshpande



The other comments are useful.  Lambdas are usually used for callback 
functions.  But I wanted to point out that your two add_one objects are 
not at all equivalent.  The first one is a function, which can be called 
at a later time.  The second one is probably an integer, depending of 
course on the type of x.



To get the equivalent of the lambda function above, you'd use

def add_one(x):
return x+1


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


Re: [Tutor] Waiting until a thread ends

2011-03-19 Thread lists
>
>
>
>> In my continuing quest the find the best way of doing this I came across
>> the following method:
>>
>> for thread in threading.enumerate():
>> if thread is not threading.currentThread():
>> thread.join()
>> print 'FINISHED'
>>
>> In my newbie understanding, you can't join() the current thread, because
>> it's the main thread (the one from which the others are called), join()ing
>> it would lock the program up (it would never complete).
>>
>> The above only join()s a thread if it isn't the current thread, thus
>> (hopefully) getting around this. Swapping my earlier stupid code for this
>> seems to work as expected in my tests.
>>
>
> Thanks for the postbacks, it's been useful/interesting for me.
>
> Best,
>
> Walter
>

I'm really pleased that it was of some help to somebody else too.

Kind Regards,

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


Re: [Tutor] lambda

2011-03-19 Thread Alan Gauld


"Ajit Deshpande"  wrote

I am trying to figure out where lambda functions can be useful. Has 
anyone

used them in real world?


Yers lots of people, all over the place. They are very useful things.


Why would anyone use a one liner anonymous function,


To pass as an argument to another function.
GUI widgets for example frequently take a function as
an argument so that when they are clicked they call
the function. If the function is only doing something
trivial a lambda is better than writing a small function
just to pass to the GUI widget.

Similarly functions like sort() often take functions as
arguments so that you can customise the sort
algorithm. Often these are just one liners defining
how a comparison should work.

lambda comes from the matth/computer science term
lambda calculus and lambdas are at the very core of
theoretical computing. Functional programming is a style
closely aligned to computer science theory and as
such makes much use of lambdas (Lisp does this too).

You can read more on the use of Lambdas and
functional programming in the Functional Programming
topic of my tutorial. You can see lambdas in use in the
GUI topic of my tutor.

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] lambda

2011-03-19 Thread Steven D'Aprano

Ajit Deshpande wrote:

I am trying to figure out where lambda functions can be useful. Has anyone
used them in real world?


Of course. lambdas are especially useful for callback functions, which 
are especially common when doing GUI programming.




From my reading so far, I hear people claim that lambda can be a useful
replacement for small functions. Most examples  didn't make much sense to
me. Why would anyone use a one liner anonymous function, if you never plan
to use it elsewhere?


That's precisely the point -- why would you define a non-anonymous 
function that you only use once?


def spam(x):
return x + something

register_handler(spam)  # Provide a callback to some part of your app
# never use spam again


is better written as:

register_handler(lambda x: x+something)



 You would then be implementing the logic directly in
the line, isn't it?. Functions are useful if they plan to get called
multiple times within your code.


That's often the case, but not necessarily.




For example:

add_one = lambda x: x + 1


This defines a function "add_one". This is equivalent to writing:

def add_one(x):
return x+1


In real world, why would I use a lambda for this. I would simply do:

add_one = x + 1


This takes the current value of x, adds one to it, and assigns it to the 
very badly named variable "add_one".




Can you provide some useful use cases for lambda functions?


Sorting with comparison or key functions.
min or max with a key function.
Callbacks.
map
reduce
Solving numerical equations.

There are probably others, but they're the obvious ones.

Here's an example you can try:

text = """Li Europan lingues es membres del sam familie. Lor separat 
existentie es un myth. Por scientie, musica, sport etc, litot Europa usa 
li sam vocabular. Li lingues differe solmen in li grammatica, li 
pronunciation e li plu commun vocabules. Omnicos directe al desirabilite 
de un nov lingua franca: On refusa continuar payar custosi traductores."""

words = text.lower().split()
sorted(words)  # sort in alphabetical order


Now suppose you want to sort by the number of vowels. Here's how you can 
do it with an anonymous key function:


sorted(words, key=lambda word: sum(word.count(c) for c in 'aeiou'))


--
Steven

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