Re: Events: The Python Way

2007-07-28 Thread Antti Rasinen

On 2007-07-29, at 02:34, David Wilson wrote:

> Hi there,
>
> Python has no built-in way of doing this. You may consider writing
> your own class if you like this pattern (I personally do):
>
> class Event(object):
> def __init__(self):
> self.subscribers = set()

...

> def __isub__(self, subscriber):
> self.subscribers.pop(subscriber)
> return self

A slight typo there. For sets s.pop() returns an arbitrary element  
and s.remove(x) or s.discard(x) removes the element x from the set s.  
For the OP: remove raises an exception if x is not in the set,  
discard does not.

-- 
[ [EMAIL PROTECTED] <*> Antti Rasinen ]

"The music of rebellion makes you wanna rage
But it's made by millionaires who are nearly twice your age"


-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Detecting __future__ features

2007-07-30 Thread Antti Rasinen

On 2007-07-30, at 15:29, Steven D'Aprano wrote:

> How would one tell at runtime if a particular feature has been  
> enabled by
> the "from __future__ import thing" statement?
>
> (I don't especially care whether the feature in question has been  
> enabled
> via an explicit call to import, or because it has become the default.)
>
> Is there any general mechanism?

You probably have to care about imports vs. language defaults. But  
it's not very difficult.

For imports you can use __future__ to help you. If your namespace  
contains a feature you want to check for and it is identical to the  
same feature in __future__, then the code has used from __future__  
import feature. You could probably try something like this:

import __feature__
feature = "division"
if globals().get(feature, None) == __future__.__dict__[feature]:
 print "Bingo!"

You can probably figure out how to use sys.version_info to check  
whether the current Python version is higher than the one specified  
in a feature line:

import __future__
import sys
if sys.version_info >= __future__.division.mandatory:
 print "Bingo! Two in a row!"

Check the __future__ docstrings for more information.

-- 
[ [EMAIL PROTECTED] <*> Antti Rasinen ]

This drone-vessel speaks with the voice and authority of the Ur-Quan.

-- 
http://mail.python.org/mailman/listinfo/python-list


Re: removing redundant elements in a dictionary

2007-08-01 Thread Antti Rasinen


On 2007-08-01, at 06:50, Astan Chee wrote:


Hi,
I have a dictionary which looks something like this:

elem = {"co":[1,2,3],"cp":[3,2,1],"pp":[5,6,7],"cl":[1,2,3],"qw": 
[6,7,8],"qa":[8,7,6]}


what Im trying to do is find all keys in the list that have the  
same value and delete those (except one); thereby removing all  
redundant keys so that the above dict will look like this:


elem = {"co":[1,2,3],"pp":[5,6,7],"qa":[8,7,6]}

my code so far looks like this:

for out1 in elem.keys():
for out2 in elem.keys():
if out1 != out2:
elem[out1].sort()
elem[out2].sort()
if elem[out1] == elem[out2]:
del elem[out1]

This returns in a KeyError exception when sorting. What am I  
missing here?


Consider a dict {'a': [1], 'b': [1], 'c': [1]}.

First time we reach the innerloop, out1 == 'a'. Then we iterate over  
the list ['a', 'b', 'c'].


'a' is skipped. At 'b', we remove 'a' from the dictionary. And then  
it all goes wrong.


The last time we go through the inner loop, out1 is still 'a' -- an  
element which does not exist in the dictionary anymore. Then your  
attempts to reference it fail at both the sorting and comparison  
stages. You should break out of the inner loop immediately after  
you've deleted the item or delete out2 instead..


Furthermore, the algorithm is somewhat inefficient due to the fact  
that it sorts the lists O(n^2) times when you only need to do them n  
times. If your data allows it, you might like to use sets instead of  
lists. This would avoid the need to sort the lists.


However, here is a very quick version that works in my tests:
for out_list in elem.values():
out_list.sort()

for out1 in elem.keys():
for out2 in elem.keys():
if out1 != out2 and elem[out1] == elem[out2]:
del elem[out1]
break

There are probably several ways to improve on that, however.

--
[ [EMAIL PROTECTED] <*> Antti Rasinen ]

"Pay the man his wage, he's making paper to fuel the Information Age."


-- 
http://mail.python.org/mailman/listinfo/python-list

Re: Use variable in regular expression

2007-08-02 Thread Antti Rasinen

On 2007-08-02, at 13:43, [EMAIL PROTECTED] wrote:

> I know I can use a variable in regular expressions. I want to use a
> regex to find something based on the beginning of the string. I am
> using yesterday's date to find all of my data from yesterday.
> Yesterday's date is 20070731, and assigned to the variable
> "yesterday_date". I want to loop thru a directory and find all of the
> yesterday's data ONLY IF the feature class has the date at the
> BEGINNING of the filename.
>

> ...

> I don't want the one's that start with "Copy". I can't figure out the
> syntax of inserting the "^" into the regex. I've tried all of the
> following, with no luck:
>
> re.compile(^yesterday_date)
> re.compile(r'^yesterday_date')
> re.compile(r'^[yesterday_date]')
> re.compile(r'[^yesterday_date]')

The first one is a syntax error (^ outside a string means the xor- 
operation). The rest are just strings containing the _string_  
'yesterday_date' and not the value of the variable. So you need to do  
some string formatting(*

search_str = '^%s' % yesterday_date # I'm assuming yesterday_date is  
a string.
re.compile(search_str)

*) http://docs.python.org/lib/typesseq-strings.html

-- 
[ [EMAIL PROTECTED] <*> Antti Rasinen ]

This drone-vessel speaks with the voice and authority of the Ur-Quan.

-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Question about properties.

2007-08-10 Thread Antti Rasinen

> Hi,
>
> i read in a book the following code snippet that is dealing with
> properties:
>
> class ProtectAndHideX(object):
> def __init__(self, x):
> assert isinstance(x, int), '"x" must be an integer!"'
> self.__x = ~x
>
> def get_x(self):
> return ~self.__x
>
> x = property(get_x)
>
>
> Can anyone please help me understand what the symbol "~" does here ??

My guess is that the example tries to show that it does not matter how the
property computes the value. You can -- if you want -- to store integers
as their bit-inverted versions (the ~ operator) and then do the conversion
when getting the property value.

Assume you initialized the object with ProtectAndHideX(4). Outside the
object you don't have access to the original __x. And! Even if you changed
the name of the variable name to y, you'd have hidden_x.y == -5 instead of
4.

The example is very contrived. There might be some security related cases
where you need to hide what you store in memory, though. (Hopefully they
do more than just invert the bits! :)

NB: I don't know what the original author was thinking here -- my
telepathy isn't what it used to be.

-- 
[ Antti Rasinen <*> [EMAIL PROTECTED] ]

-- 
http://mail.python.org/mailman/listinfo/python-list