Re: Diversity in Python

2009-08-19 Thread Hendrik van Rooyen
On Tuesday 18 August 2009 12:38:36 Ben Finney wrote:
> Hendrik van Rooyen  writes:
> > On Tuesday 18 August 2009 06:45:39 Aahz wrote:
> > > Mainly an opportunity to flog the new diversity list.
> >
> > Here my English fails me - flog as in "whip", or flog as in "sell"?
>
> Yes :-)

Thank you that clears it up.

:-)

- Hendrik

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


Re: recording input from USB port and write to text file

2009-08-19 Thread Diez B. Roggisch

Allan schrieb:

Hi! I'm fairly new to Python.  I understand the basics basics but I'm
been trying to write a simple python code that will let me read input
data (such as mouse movement) from my USB port and write it in a text
file and I am so lost.  Can anyone help or direct me to some
resources?  Thank you!


This isn't as easy as you think. And dependend on your operating-system.

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


Re: Inheriting dictionary

2009-08-19 Thread Hendrik van Rooyen
On Tuesday 18 August 2009 21:44:55 Pavel Panchekha wrote:
> I want a dictionary that will transparently "inherit" from a parent
> dictionary. So, for example:
>
> """
> a = InheritDict({1: "one", 2: "two", 4: "four"})
> b = InheritDict({3: "three", 4: "foobar"}, inherit_from=a)
>
> a[1] # "one"
> a[4] # "four"
> b[1] # "one"
> b[3] # "three"
> b[4] # "foobar"
> """
>
> I've written something like this in Python already, but I'm wondering
> if something like this already exists, preferably written in C, for
> speed.

Its not inheritance, but have you looked at the update method?

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


Re: Parallelization in Python 2.6

2009-08-19 Thread Hendrik van Rooyen
On Tuesday 18 August 2009 22:45:38 Robert Dailey wrote:

> Really, all I'm trying to do is the most trivial type of
> parallelization. Take two functions, execute them in parallel. This
> type of parallelization is called "embarrassingly parallel", and is
> the simplest form. There are no dependencies between the two
> functions. They do requires read-only access to shared data, though.
> And if they are being spawned as sub-processes this could cause
> problems, unless the multiprocess module creates pipelines or other
> means to handle this situation.

Just use thread then and thread.start_new_thread.
It just works.

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


Re: Read C++ enum in python

2009-08-19 Thread Mark Tolonen


"MRAB"  wrote in message 
news:[email protected]...

Ludo wrote:

Hello,

I work in a very large project where we have C++ packages and pieces of 
python code.


I've been googleing for days but what I find seems really too complicated 
for what I want to do.


My business is, in python, to read enum definitions provided by the 
header file of an c++ package.
Of course I could open the .h file, read the enum and transcode it by 
hand into a .py file but the package is regularly updated and thus is the 
enum.


My question is then simple : do we have :
- either a simple way in python to read the .h file, retrieve the c++ 
enum and provide an access to it in my python script
- either a simple tool (in a long-term it would be automatically run 
when the c++ package is compiled) generating from the .h file a .py file 
containing the python definition of the enums ?


Thank you for any suggestion.


Speaking personally, I'd parse the .h file using a regular expression
(re module) and generate a .py file. Compilers typically have a way of
letting you run external scripts (eg batch files in Windows or, in this
case, a Python script) when an application is compiled.


This is what 3rd party library pyparsing is great for:

begin code--
from pyparsing import *

# sample string with enums and other stuff
sample = '''
   stuff before

   enum hello {
   Zero,
   One,
   Two,
   Three,
   Five=5,
   Six,
   Ten=10
   }

   in the middle

   enum blah
   {
   alpha,
   beta,
   gamma = 10 ,
   zeta = 50
   }

   at the end
   '''

# syntax we don't want to see in the final parse tree
_lcurl = Suppress('{')
_rcurl = Suppress('}')
_equal = Suppress('=')
_comma = Suppress(',')
_enum = Suppress('enum')

identifier = Word(alphas,alphanums+'_')
integer = Word(nums)

enumValue = Group(identifier('name') + Optional(_equal + integer('value')))
enumList = Group(enumValue + ZeroOrMore(_comma + enumValue))
enum = _enum + identifier('enum') + _lcurl + enumList('list') + _rcurl

# find instances of enums ignoring other syntax
for item,start,stop in enum.scanString(sample):
   id = 0
   for entry in item.list:
   if entry.value != '':
   id = int(entry.value)
   print '%s_%s = %d' % (item.enum.upper(),entry.name.upper(),id)
   id += 1
--end code

Output:
HELLO_ZERO = 0
HELLO_ONE = 1
HELLO_TWO = 2
HELLO_THREE = 3
HELLO_FIVE = 5
HELLO_SIX = 6
HELLO_TEN = 10
BLAH_ALPHA = 0
BLAH_BETA = 1
BLAH_GAMMA = 10
BLAH_ZETA = 50

-Mark


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


socket bug or not?

2009-08-19 Thread Gabriel Rossetti

Hello everyone,

I found what was not working in my code (see Twisted mailing list topics 
"self.socket.accept() in doRead() in tcp.py has (11, 'Resource 
temporarily unavailable') error", "Twisted, PushProducer, Words & big 
msgs, any limit?" and Python mailing list topic "socket.send : (11, 
'Resource temporarily unavailable')"). I was just wondering if this was 
a bug or not, so here it is, I wrote aserver using twisted words' 
xmlstream class. When a client connects, it sends "" and the 
server also sends it, so a bi-directional xmlstream communication is 
established. This works fine, I at some point wrote a webservice to send 
msgs to my system, not using any twisted code, that sends a message to 
my server in this format : ".". 
This worked fine as long as the messages were small, but if it send 
larger msgs (I tried +128k msgs) the server only received part of the 
msg and then the parser died because of this, so my msg appeared to be 
dropped. I discovered that it was because my webservice did not read the 
"" element sent by the server when it connected (since I was not 
interested in having a bidirectional xml stream communication with it), 
when I added the code to read it, everything worked as expected. I 
tested the webservice to without the server to see if it had a problem, 
using netcat, and it does, so I wonder if there is a bug in the socket 
code or not. Here is the test code :



## Listing 1 Start ##
import socket, time
STREAM_START = ""
STREAM_END = ""

def sendTestMessage(host, port):
   msg = "" + ('a' * 175177) + ""
   burstSize = 4096
   sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
   sock.connect((host, port))
   sock.send(STREAM_START)
   time.sleep(5)
   while msg:
   sent = sock.send(msg[:burstSize])
   print "Sending %d bytes..." % sent
   if sent == 0:
   raise RuntimeError, "socket connection broken"
   msg = msg[burstSize:]
   sock.send(STREAM_END)
   sock.close()
## Listing 1 End ##

To test it:

1) open a python interpretor, copy-past the code above
2) open another terminal and run netcat using the following command : 
nc -v -l -p 
3) call the above function using : sendTestMessage("localhost", ), 
it will wait 5 seconds after having sent the , for the first 
test, just wait.


The message will be completely send and received by the netcat "server", 
now let's test the case I have, where the server sends a , to do 
that, repeat the above steps (you have to re-run netcat as it exits when 
the client disconnects), except in step 3 instead of just waiting, type 
 and press enter in the netcat terminal after having received 
the  element from the sending code. This time you will see that 
the message is incomplete.


If you send a smaller message, like by replacing the following line :

msg = "" + ('a' * 175177) + ""

with :

msg = "" + ('a' * (175177/4)) + ""

it works in both cases.

Now test the "fixed" code :

## Listing 2 Start ##
import socket
STREAM_START = ""
STREAM_END = ""

def sendTestMessage(host, port):
   msg = "" + ('a' * 175177) + ""
   burstSize = 4096
   sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
   sock.connect((host, port))
   sock.send(STREAM_START)
   sock.recv(len(STREAM_START)+1)
   while msg:
   sent = sock.send(msg[:burstSize])
   print "Sending %d bytes..." % sent
   if sent == 0:
   raise RuntimeError, "socket connection broken"
   msg = msg[burstSize:]
   sock.send(STREAM_END)
   sock.close()
## Listing 2 End ##

This time the code reads for the  element after sending the 
 element so it works, just try the steps described above, the 
ones where you have to type  and press enter in the netcat 
terminal and this time it works.


Is this a bug in the sockets code, or is this normal? If it's normal I 
think it should be mentioned somewhere. Oh, by the way, I use linux and 
this code was tested only on linux, I don't know if the problem also 
occurs on other platforms.


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


expandtabs acts unexpectedly

2009-08-19 Thread [email protected]
Python 2.6.2 (release26-maint, Apr 19 2009, 01:56:41)
[GCC 4.3.3] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> ' test\ttest'.expandtabs(4)
' test   test'
>>> 'test \ttest'.expandtabs(4)
'testtest'

1st example: expect returning 4 spaces between 'test', 3 spaces
returned
2nd example: expect returning 5 spaces between 'test', 4 spaces
returned

Is it a bug or something, please advice.
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: expandtabs acts unexpectedly

2009-08-19 Thread Xavier Ho
On Wed, Aug 19, 2009 at 5:57 PM, [email protected]
wrote:

> Python 2.6.2 (release26-maint, Apr 19 2009, 01:56:41)
> [GCC 4.3.3] on linux2
> Type "help", "copyright", "credits" or "license" for more information.
> >>> ' test\ttest'.expandtabs(4)
> ' test   test'
> >>> 'test \ttest'.expandtabs(4)
> 'testtest'
>
> Is it a bug or something, please advice.
> --


 It is completely working as intended. What were you expecting instead?

Regards,
-Xav
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Parallelization in Python 2.6

2009-08-19 Thread Paul Rubin
Hendrik van Rooyen  writes:
> Just use thread then and thread.start_new_thread.
> It just works.

The GIL doesn't apply to threads made like that?!
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Read C++ enum in python

2009-08-19 Thread Neil Hodgson
AggieDan04:

> file_data = open(filename).read()
> # Remove comments and preprocessor directives
> file_data = ' '.join(line.split('//')[0].split('#')[0] for line in
> file_data.splitlines())
> file_data = ' '.join(re.split(r'\/\*.*\*\/', file_data))

   For some headers I tried it didn't work until the .* was changed to a
non-greedy .*? to avoid removing from the start of the first comment to
the end of the last comment.

file_data = ' '.join(re.split(r'\/\*.*?\*\/', file_data))

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


Re: expandtabs acts unexpectedly

2009-08-19 Thread Peter Brett
"[email protected]"  writes:

> Python 2.6.2 (release26-maint, Apr 19 2009, 01:56:41)
> [GCC 4.3.3] on linux2
> Type "help", "copyright", "credits" or "license" for more information.
 ' test\ttest'.expandtabs(4)
> ' test   test'
 'test \ttest'.expandtabs(4)
> 'testtest'
>
> 1st example: expect returning 4 spaces between 'test', 3 spaces
> returned
> 2nd example: expect returning 5 spaces between 'test', 4 spaces
> returned
>
> Is it a bug or something, please advice.

Consider where the 4-space tabstops are relative to those strings:

 test   test
testtest
^   ^   ^

So no, it's not a bug.

If you just want to replace the tab characters by spaces, use:

  >>> " test\ttest".replace("\t", "")
  ' testtest'
  >>> "test \ttest".replace("\t", "")
  'test test'

HTH,

   Peter

-- 
Peter Brett 
Remote Sensing Research Group
Surrey Space Centre
-- 
http://mail.python.org/mailman/listinfo/python-list


reading a text file

2009-08-19 Thread superpollo

hi clp

what's the difference between:

while True:
input_line = sys.stdin.readline()
if input_line:
sys.stdout.write(input_line.upper())
else:
break

and:


while True:
try:
sys.stdout.write(sys.stdin.next().upper())
except StopIteration:
break

???

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


Re: reading a text file

2009-08-19 Thread Paul Rubin
superpollo  writes:
> while True:
>  try:
>  sys.stdout.write(sys.stdin.next().upper())
>  except StopIteration:
>  break

Maybe there is some subtle difference, but it looks like you really mean

   for line in sys.stdin:
   sys.stdout.write(line.upper())
-- 
http://mail.python.org/mailman/listinfo/python-list


difference between 2 arrays

2009-08-19 Thread Pierre
Hello,

I would like to know how to find the difference (set operation)
between 2 arrays :

a = array([1,2, 3,2,5,2])
b = array([1,2])
I want a - b = [3,5]

Well, the equivalence of setdiff in matlab...

I thought a.difference(b) could work, but no : AttributeError:
'numpy.ndarray' object has no attribute 'difference'

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


Re: Identifying a class type - bad practice?

2009-08-19 Thread James Harris
On 18 Aug, 21:50, Chris Rebert  wrote:
> On Tue, Aug 18, 2009 at 10:09 AM, James
>
> Harris wrote:
> > I am writing some code to form a tree of nodes of different types. The
> > idea is to define one class per node type such as
>
> > class node_type_1(node):
> >  
> > class node_type_2(node):
> >  
> > etc
>
> > (Class "node" would hold any common properties).
>
> > When walking the tree I need to know what type of node I'm dealing
> > with so polymorphism isn't generally useful. The action to be taken
> > depends on the node type.
>
> I'm sure it relates to the exact type of tree you're walking and the
> calculation you're doing on it, but what is the reason why your code,
> which in the abstract sounds like it will vaguely resemble this:
>
> def walk_tree(tree):
>     if isinstance(tree, node_type_1):
>         #code
>         walk_tree(subtree)
>     elif isinstance(tree, node_type_2):
>         #code
>         walk_tree(subtree)
>     #etc...
>
> can't be written instead as:
>
> class node_type_1:
>     def walk_tree(self):
>         #code
>         self.subtree.walk()
>
> class node_type_2:
>     def walk_tree(self):
>         #code
>         self.subtree.walk()
>
> #etc

Interesting idea. This may be a better and a more OO solution than
what I had in mind. I'm not sure if I can use this but I'll see how it
fits in as the work progresses.

The tree is for a compiler. Initially the tree is for parsing of
source code. Then it will need to be processed and changed as further
compiler phases are written. I don't know yet whether it will be
easier to modify the tree or to create a new one for each phase.

So I guess whether I use the idea depends on the commonality of
operations.

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


Re: difference between 2 arrays

2009-08-19 Thread Diez B. Roggisch
Pierre wrote:

> Hello,
> 
> I would like to know how to find the difference (set operation)
> between 2 arrays :
> 
> a = array([1,2, 3,2,5,2])
> b = array([1,2])
> I want a - b = [3,5]
> 
> Well, the equivalence of setdiff in matlab...
> 
> I thought a.difference(b) could work, but no : AttributeError:
> 'numpy.ndarray' object has no attribute 'difference'

Not sure if this works with numpy-arrays, but

set(a) - set(b)

might just work if the arrays are iterable.

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


Re: difference between 2 arrays

2009-08-19 Thread Michel Claveau - MVP
(envoyé via news:\\news.wanadoo.fr\comp.lang.python)

Hi!

See the module "sets"

@-salutations 
-- 
Michel Claveau 
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: reading a text file

2009-08-19 Thread Peter Otten
superpollo wrote:

> hi clp
> 
> what's the difference between:
> 
> while True:
>  input_line = sys.stdin.readline()
>  if input_line:
>  sys.stdout.write(input_line.upper())
>  else:
>  break
> 
> and:
> 
> 
> while True:
>  try:
>  sys.stdout.write(sys.stdin.next().upper())
>  except StopIteration:
>  break

You should write the latter as

for line in sys.stdin:
sys.stdout.write(line.upper())

or 

sys.stdout.writelines(line.upper() for line in sys.stdin)

You seem to know already that next() and readline() use different ways to 
signal "I'm done with the file". Also, after the first StopIteration 
subsequent next() calls are guaranteed to raise a StopIteration. But the 
main difference is that file.next() uses an internal buffer, file.readline() 
doesn't. That means you would lose data if you tried to replace the 
readline() call below with next()

first_line = f.readline()
read_of_the_file = f.read()

In newer Python versions you will get a ValueError when mixing next() and 
read()/readline() but in older Pythons (before 2.5 I think) you are in for a 
surprise.

As 

for line in file: ...

is both the fastest and most readable approach if you want to access a file 
one line at a time I recommend that you use it unless there is a specific 
reason not to.

Peter

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


Re: Scope and classes

2009-08-19 Thread Bruno Desthuilliers

Chris Rebert a écrit :
(snip)

To access class-level variables from within instance methods of the
class, you have 2 options:
A. Use the class name, i.e. Abc.message
B. Reference the class indirectly, i.e. self.__class__.message


Or even simpler - *if* there's no synonym instance attribute:

=> self.message

Attribute lookup will try to resolve the attribute name on the class 
(and it's parent classes) if it's not found on the instance.


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


How to create ones own lib

2009-08-19 Thread Horst Jäger

Hi,

I would like to create my own lib "hotte.py" which I can import like


import string,hotte

. How do I do that?

I'm working on MacOS 10.5.6 .

Thanks in advance

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


Re: How to create ones own lib

2009-08-19 Thread Xavier Ho
On Wed, Aug 19, 2009 at 7:00 PM, Horst Jäger wrote:

> Hi,
>
> I would like to create my own lib "hotte.py" which I can import like
>
>
>import string,hotte
>
> . How do I do that?
>

1) Have the hotte.py in the same directory of any of your other Python code
that imports it, or
2) Put the hotte.py file in the Python import directive, whose location I do
not know in Mac.. someone else will know, I'm sure =]

All in all, I'm pretty sure you don't have to compile it or anything, but it
can be an option.

Cheers,
Xav
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: How to create ones own lib

2009-08-19 Thread Francesco Bochicchio
On 19 Ago, 11:00, Horst Jäger  wrote:
> Hi,
>
> I would like to create my own lib "hotte.py" which I can import like
>
>         import string,hotte
>
> . How do I do that?
>
> I'm working on MacOS 10.5.6 .
>
> Thanks in advance

Just create the file 'hotte.py' and place it somewhere python can find
it, that is:
- in the same directory of the code using it (which is most probablyt
what you want to do )
- in a directory listed in  sys.path variable (which you can extend
using sys.path.append("full_path_of_my_library_directory") before
doing import hotte

There are other options, but these should cover your needs.

Ciao
-
FB

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


Re: Scope and classes

2009-08-19 Thread Bruno Desthuilliers

David a écrit :
(snip)


Out of 'Abc.message' and 'self.message', which is the favoured
convention? It would be very easy to accidentally override
'self.messages' with an instance attribute!


Only use 'Abc.message' if you want to make sure you get the Abc class 
'message' attribute - that is, if you want to skip possible overrides in 
subclasses. As far as I'm concerned, I'd tend to consider this bad style 
 unless it's a very very specific implementation point of an abstract 
class (in which case it would probably be named '__message' to prevent 
accidental override) - but YMMV of course.


Use 'self.__class__.message' (or 'type(self).message') if you want to 
make sure you get the class 'message' attribute - that is, if you want 
to honor possible overrides in subclasses, but not per-instance 
override. But then - at least in your example code - I'd use a classmethod:


class Abc:
message = 'Hello World'

@classmethod
def print_message(cls):
print cls.message


Now the most common idiom - that is, outside classmethods - is to just 
use 'self.message'. Someone (even you) might have a very valid reason to 
 override the 'message' attribute on a per-instance basis. FWIW, if you 
start to worry about possible accidental overrides here, then Python 
might not be the right language for you - nothing prevents "accidental" 
overrides of method and even the class (the '__class__' attribute) 
itself - yes, they are all attributes, and you can dynamically override 
them !-)

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


Re: Read C++ enum in python

2009-08-19 Thread Bill Davy
"Mark Tolonen"  wrote in message 
news:[email protected]...
>
> "MRAB"  wrote in message 
> news:[email protected]...
>> Ludo wrote:
>>> Hello,
>>>
>>> I work in a very large project where we have C++ packages and pieces of 
>>> python code.
>>>
>>> I've been googleing for days but what I find seems really too 
>>> complicated for what I want to do.
>>>
>>> My business is, in python, to read enum definitions provided by the 
>>> header file of an c++ package.
>>> Of course I could open the .h file, read the enum and transcode it by 
>>> hand into a .py file but the package is regularly updated and thus is 
>>> the enum.
>>>
>>> My question is then simple : do we have :
>>> - either a simple way in python to read the .h file, retrieve the 
>>> c++ enum and provide an access to it in my python script
>>> - either a simple tool (in a long-term it would be automatically run 
>>> when the c++ package is compiled) generating from the .h file a .py file 
>>> containing the python definition of the enums ?
>>>
>>> Thank you for any suggestion.
>>
>> Speaking personally, I'd parse the .h file using a regular expression
>> (re module) and generate a .py file. Compilers typically have a way of
>> letting you run external scripts (eg batch files in Windows or, in this
>> case, a Python script) when an application is compiled.
>
> This is what 3rd party library pyparsing is great for:
>
> begin code--
> from pyparsing import *
>
> # sample string with enums and other stuff
> sample = '''
>stuff before
>
>enum hello {
>Zero,
>One,
>Two,
>Three,
>Five=5,
>Six,
>Ten=10
>}
>
>in the middle
>
>enum blah
>{
>alpha,
>beta,
>gamma = 10 ,
>zeta = 50
>}
>
>at the end
>'''
>
> # syntax we don't want to see in the final parse tree
> _lcurl = Suppress('{')
> _rcurl = Suppress('}')
> _equal = Suppress('=')
> _comma = Suppress(',')
> _enum = Suppress('enum')
>
> identifier = Word(alphas,alphanums+'_')
> integer = Word(nums)
>
> enumValue = Group(identifier('name') + Optional(_equal + 
> integer('value')))
> enumList = Group(enumValue + ZeroOrMore(_comma + enumValue))
> enum = _enum + identifier('enum') + _lcurl + enumList('list') + _rcurl
>
> # find instances of enums ignoring other syntax
> for item,start,stop in enum.scanString(sample):
>id = 0
>for entry in item.list:
>if entry.value != '':
>id = int(entry.value)
>print '%s_%s = %d' % (item.enum.upper(),entry.name.upper(),id)
>id += 1
> --end code
>
> Output:
> HELLO_ZERO = 0
> HELLO_ONE = 1
> HELLO_TWO = 2
> HELLO_THREE = 3
> HELLO_FIVE = 5
> HELLO_SIX = 6
> HELLO_TEN = 10
> BLAH_ALPHA = 0
> BLAH_BETA = 1
> BLAH_GAMMA = 10
> BLAH_ZETA = 50
>
> -Mark
>
>


Python and pythoneers are amazing! 


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


Re: Need cleanup advice for multiline string

2009-08-19 Thread Jean-Michel Pichavant

Grant Edwards wrote:

On 2009-08-18, Simon Forman  wrote:

  

Sexism, racism, homophobia, religious intolerance, etc., all
stem from a fundamental forgetfulness of our Unity in God (as
I would put it) and this is perhaps the single greatest cause
of human misery.



You mean the single greatest cause of human misery isn't
Microsoft Windows?

  

No, emacs is responsible ! Hail to Vi !

JM

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


Re: Identifying a class type - bad practice?

2009-08-19 Thread Bruno Desthuilliers

James Harris a écrit :

On 18 Aug, 21:50, Chris Rebert  wrote:

(snip)

class node_type_1:
def walk_tree(self):
#code
self.subtree.walk()

class node_type_2:
def walk_tree(self):
#code
self.subtree.walk()

#etc


Interesting idea.


Basic OO stuff, really. I'd say it's even the whole point of OO.

(snip)


The tree is for a compiler. Initially the tree is for parsing of
source code. Then it will need to be processed and changed as further
compiler phases are written. I don't know yet whether it will be
easier to modify the tree or to create a new one for each phase.

So I guess whether I use the idea depends on the commonality of
operations.


You may want to have a look at the "composite" and "visitor" design 
patterns. AST are canonical use case for these patterns.



HTH

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


Re: Need cleanup advice for multiline string

2009-08-19 Thread Jean-Michel Pichavant

Simon Forman wrote:

On Tue, Aug 18, 2009 at 8:03 PM, Steven
D'Aprano wrote:
  

On Tue, 18 Aug 2009 17:13:02 -0400, Simon Forman wrote:



Sexism, racism, homophobia, religious intolerance, etc., all stem from a
fundamental forgetfulness of our Unity in God (as I would put it) and
  

Of the tens of thousands of Gods that people have invented, which is the
one we're supposed to believe in? I always forget which ones we're
supposed to dismiss as nonsense, and which one we're not.



Why the heck are you asking me?  (I'd say "/you/ are the God you
should believe in.")
  


Steven, a God... funny :o)

No the only God we should surely  worship is Guido, our BDFL :bow:. That 
is not questionable, and those who dare will be hung by the balls. (In 
order to be fairly cruel we should find an appropriate torture for our 
ladies, I don't want to be tagged as sexist).


JM

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


Re: How to create ones own lib

2009-08-19 Thread Nick Craig-Wood
Horst Jäger  wrote:
>  I would like to create my own lib "hotte.py" which I can import like
> 
>   import string,hotte
> 
>  . How do I do that?

One of the nice things about python is that you don't need to do
anything special to define a library.

Just define your classes / functions in hotte.py

then use them like

  import hotte

  hotte.MyClass()
  hotte.my_function()

See here for the relevant bit of the tutorial

  http://docs.python.org/tutorial/modules.html

-- 
Nick Craig-Wood  -- http://www.craig-wood.com/nick
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: expandtabs acts unexpectedly

2009-08-19 Thread [email protected]
On Aug 19, 4:16 pm, Peter Brett  wrote:
> "[email protected]"  writes:
> > Python 2.6.2 (release26-maint, Apr 19 2009, 01:56:41)
> > [GCC 4.3.3] on linux2
> > Type "help", "copyright", "credits" or "license" for more information.
>  ' test\ttest'.expandtabs(4)
> > ' test   test'
>  'test \ttest'.expandtabs(4)
> > 'test    test'
>
> > 1st example: expect returning 4 spaces between 'test', 3 spaces
> > returned
> > 2nd example: expect returning 5 spaces between 'test', 4 spaces
> > returned
>
> > Is it a bug or something, please advice.
>
> Consider where the 4-space tabstops are relative to those strings:
>
>  test   test
> test    test
> ^   ^   ^
>
> So no, it's not a bug.
>
> If you just want to replace the tab characters by spaces, use:
>
>   >>> " test\ttest".replace("\t", "    ")
>   ' test    test'
>   >>> "test \ttest".replace("\t", "    ")
>   'test     test'
>
> HTH,
>
>                                Peter
>
> --
> Peter Brett 
> Remote Sensing Research Group
> Surrey Space Centre

You corrected me for the understanding of tab stop. Great explanation.
Thank you so much.
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: How to create functors?

2009-08-19 Thread Terry Reedy

Robert Dailey wrote:


I'm using Python 2.6. And using the legacy syntax in the lambda does
not work either. I want to avoid using a def if possible. Thanks.


In Python, writing

name = lambda arg: expr

instead of

def name(arg): return expr

is all negative and no positive and should be avoided.

Your experience illustrates one reason why.

tjr

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


Re: Raw Strings with Variables

2009-08-19 Thread Terry Reedy

WilsonOfCanada wrote:

Hellos,

I know that if you have:

happy = r"C:\moo"
print happy

you get C:\moo instead of C:\\moo

The thing is that I want to do this a variable instead.

ex. testline = fileName.readline()
  rawtestline = r testline


Python does not have 'raw strings'. It only has 'raw string literals', 
which is to say, string literals with 'r' prepended to signal less 
processing (cooking) of the literal in the process of turning it into a 
string object.


tjr

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


Re: urlopen errors in script

2009-08-19 Thread Sleepy Cabbage
On Tue, 18 Aug 2009 13:05:03 +, Sleepy Cabbage wrote:

> Thanks for the time you've spent anyway Peter. I have superkaramba
> installed and the rest of the script is running fine, it's only when I
> put the urlopen part in that it comes back with errors. The quotes are
> just to make it readable on here as my first attempt at posting muted
> the text.

This has been solved by upgrading to kde 4.3
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: reading a text file

2009-08-19 Thread Tino Wildenhain

superpollo wrote:

hi clp

what's the difference between:

while True:
input_line = sys.stdin.readline()
if input_line:
sys.stdout.write(input_line.upper())
else:
break

and:


while True:
try:
sys.stdout.write(sys.stdin.next().upper())
except StopIteration:
break



More useless code, under the hood its working similar.
But why not use it in the way intended?

for input_line in sys.stdin:
sys.stdout.write(input_line.upper())

?

Regards
Tino


smime.p7s
Description: S/MIME Cryptographic Signature
-- 
http://mail.python.org/mailman/listinfo/python-list


Help with libxml2dom

2009-08-19 Thread Nuno Santos
I have just started using libxml2dom to read html files and I have some 
questions I hope you guys can answer me.


The page I am working on (teste.htm):

 
   
 Title
   
 
 
   
 
   
   


   
   
  8/15/2009
   
 
   
 


>>> import libxml2dom
>>> foo = open('teste.htm', 'r')
>>> str1 = foo.read()
>>> doc = libxml2dom.parseString(str1, html=1)
>>> html = doc.firstChild
>>> html.nodeName
u'html'
>>> head = html.firstChild
>>> head.nodeName
u'head'
>>> title = head.firstChild
>>> title.nodeName
u'title'
>>> body = head.nextSibling
>>> body.nodeName
u'body'
>>> table = body.firstChild
>>> table.nodeName
u'text' #?! Why!? Shouldn't it be a table? (1)
>>> table = body.firstChild.nextSibling #why this works? is there a 
text element hidden? (2)

>>> table.nodeName
u'table'
>>> tr = table.firstChild
>>> tr.nodeName
u'tr'
>>> td = tr.firstChild
>>> td.nodeName
u'td'
>>> font = td.firstChild
>>> font.nodeName
u'text' # (1)
>>> font = td.firstChild.nextSibling # (2)
>>> font.nodeName
u'font'
>>> a = font.firstChild
>>> a.nodeName
u'text' #(1)
>>> a = font.firstChild.nextSibling #(2)
>>> a.nodeName
u'a'


It seems like sometimes there are some text elements 'hidden'. This is 
probably a standard in DOM I simply am not familiar with this and I 
would very much appreciate if anyone had the kindness to explain me this.


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


About Python Symbian 60

2009-08-19 Thread Kannan
Hi friends
I want a Python SDK like Netbeans to work with the Symbian 60 python.
I don't have the Symbian 60 mobile.I create some Apps for S60 Platform.To
execute that i want this
Help me..






With regards,

Kannan. R. P,
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Parallelization in Python 2.6

2009-08-19 Thread sturlamolden
On 18 Aug, 11:19, Robert Dailey  wrote:

> I'm looking for a way to parallelize my python script without using
> typical threading primitives. For example, C++ has pthreads and TBB to
> break things into "tasks".

In C++, parallelization without "typical threading primitives" usually
means one of three things:

- OpenMP pragmas
- the posix function fork(), unless you are using Windows
- MPI

In Python, you find the function os.fork and wrappers for MPI, and
they are used as in C++. With os.fork, I like to use a context
manager, putting the calls to fork in __enter__ and the calls to
sys.exit in __exit__. Then I can just write code like this:

with parallel():
   # parallel block here

You can also program in the same style as OpenMP using closures. Just
wrap whatever loop or block you want to execute in parallel in a
closure. It requires minimal edition of the serial code. Instead of

def foobar():
   for i in iterable:
   #whatever

you can add a closure (internal function) and do this:

def foobar():
   def section(): # add a closure
   for i in sheduled(iterable): # balance load
   #whatever
   parallel(section) # spawn off threads

Programs written in C++ are much more difficult to parallelize with
threads because C++ do not have closures. This is why pragma-based
parallelization (OpenMP) was invented:

#pragma omp parallel for private(i)
for (i=0; ihttp://mail.python.org/mailman/listinfo/python-list


Re: Re: Parallelization in Python 2.6

2009-08-19 Thread Dave Angel

Hendrik van Rooyen wrote:

On Tuesday 18 August 2009 22:45:38 Robert Dailey wrote:

  

Really, all I'm trying to do is the most trivial type of
parallelization. Take two functions, execute them in parallel. This
type of parallelization is called "embarrassingly parallel", and is
the simplest form. There are no dependencies between the two
functions. They do requires read-only access to shared data, though.
And if they are being spawned as sub-processes this could cause
problems, unless the multiprocess module creates pipelines or other
means to handle this situation.



Just use thread then and thread.start_new_thread.
It just works.

- Hendrik

  
But if you do it that way, it's slower than sequential.  And if you have 
a multi-core processor, or two processors, or ...   then it gets much 
slower yet, and slows down other tasks as well.


With the current GIL implementation, for two CPU-bound tasks, you either 
do them sequentially, or make a separate process.


Now, you can share data between separate processes, and if the data is 
truly going to be readonly, you shouldn't have any locking issues.


Naturally you should do your own timings.  Maybe your particular CPU and 
OS will have different results.


DaveA

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


Excel edit using python

2009-08-19 Thread suman
Hi,

Is there any package to edit the existing excel cell data using python
or
to create excel charts using python

please help.

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


Re: Parallelization in Python 2.6

2009-08-19 Thread sturlamolden
On 18 Aug, 13:45, Robert Dailey  wrote:

> Really, all I'm trying to do is the most trivial type of
> parallelization. Take two functions, execute them in parallel. This
> type of parallelization is called "embarrassingly parallel", and is
> the simplest form. There are no dependencies between the two
> functions.

If you are using Linux or Mac, just call os.fork for this.

You should also know that you function "create_task" is simply

from threading import Thread
def create_task(task):
   Thread(target=task).start()

If your task releases the GIL, this will work fine.


> They do requires read-only access to shared data, though.
> And if they are being spawned as sub-processes this could cause
> problems, unless the multiprocess module creates pipelines or other
> means to handle this situation.

With forking or multiprocessing, you have to use IPC. That is, usually
pipes, unix sockets / named pipes, or shared memory. Multiprocessing
helps you with this. Multiprocessing also has a convinient Queue
object for serialised read/write access to a pipe.

You can also create shared memory with mmap.mmap, using fd 0 with
Windows or -1 with Linux.









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


Re: Excel edit using python

2009-08-19 Thread Tim Wintle
On Wed, 2009-08-19 at 05:25 -0700, suman wrote:
> 
> Is there any package to edit the existing excel cell data using python
> or
> to create excel charts using python

Chris, Chris, where are you?

http://www.python-excel.org/

Site provided by Chris Withers:
http://mail.python.org/pipermail/python-list/2009-June/716845.html


Tim Wintle

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


Re: Parallelization in Python 2.6

2009-08-19 Thread Martin P. Hellwig

sturlamolden wrote:


The human brain is bad at detecting
computational bottlenecks though. So it almost always pays off to
write everything in Python first, and use the profiler to locate the
worst offenders.


+1 QOTW

--
MPH
http://blog.dcuktec.com
'If consumed, best digested with added seasoning to own preference.'
--
http://mail.python.org/mailman/listinfo/python-list


Re: Help with libxml2dom

2009-08-19 Thread Diez B. Roggisch
Nuno Santos wrote:

> I have just started using libxml2dom to read html files and I have some
> questions I hope you guys can answer me.
> 
> The page I am working on (teste.htm):
> 
>   
> 
>   Title
> 
>   
>   
> 
>   
> 
>
> 
> 
> 
>8/15/2009
> 
>   
> 
>   
> 
> 
>  >>> import libxml2dom
>  >>> foo = open('teste.htm', 'r')
>  >>> str1 = foo.read()
>  >>> doc = libxml2dom.parseString(str1, html=1)
>  >>> html = doc.firstChild
>  >>> html.nodeName
> u'html'
>  >>> head = html.firstChild
>  >>> head.nodeName
> u'head'
>  >>> title = head.firstChild
>  >>> title.nodeName
> u'title'
>  >>> body = head.nextSibling
>  >>> body.nodeName
> u'body'
>  >>> table = body.firstChild
>  >>> table.nodeName
> u'text' #?! Why!? Shouldn't it be a table? (1)
>  >>> table = body.firstChild.nextSibling #why this works? is there a
> text element hidden? (2)
>  >>> table.nodeName
> u'table'
>  >>> tr = table.firstChild
>  >>> tr.nodeName
> u'tr'
>  >>> td = tr.firstChild
>  >>> td.nodeName
> u'td'
>  >>> font = td.firstChild
>  >>> font.nodeName
> u'text' # (1)
>  >>> font = td.firstChild.nextSibling # (2)
>  >>> font.nodeName
> u'font'
>  >>> a = font.firstChild
>  >>> a.nodeName
> u'text' #(1)
>  >>> a = font.firstChild.nextSibling #(2)
>  >>> a.nodeName
> u'a'
> 
> 
> It seems like sometimes there are some text elements 'hidden'. This is
> probably a standard in DOM I simply am not familiar with this and I
> would very much appreciate if anyone had the kindness to explain me this.

Without a schema or something similar, a parser can't tell if whitespace is
significant or not. So if you have 


  


you will have not 2, but 4 nodes - root, text containing a newline + 2
spaces, child, and again a text with a newline.

You have to skip over those that you are not interested in, or use a
different XML-library such as ElementTree (e.g. in the form of lxml) that
has a different approach about text-nodes.

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


Re: recording input from USB port and write to text file

2009-08-19 Thread Piet van Oostrum
> Allan  (A) wrote:

>A> Hi! I'm fairly new to Python.  I understand the basics basics but I'm
>A> been trying to write a simple python code that will let me read input
>A> data (such as mouse movement) from my USB port and write it in a text
>A> file and I am so lost.  Can anyone help or direct me to some
>A> resources?  Thank you!

You could try http://sourceforge.net/projects/pyusb/
-- 
Piet van Oostrum 
URL: http://pietvanoostrum.com [PGP 8DAE142BE17999C4]
Private email: [email protected]
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Parallelization in Python 2.6

2009-08-19 Thread sturlamolden
On 19 Aug, 05:16, sturlamolden  wrote:

> You should know about the GIL. It prevents multiple threads form using
> the Python interpreter simultaneously. For parallel computing, this is
> a blessing and a curse. Only C extensions can release the GIL; this
> includes I/0 routines in Python's standard library. If the GIL is not
> released, the C library call are guaranteed to be thread-safe.
> However, the Python interpreter will be blocked while waiting for the
> library call to return. If the GIL is released, parallelization works
> as expected; you can also utilise multi-core CPUs (it is a common
> misbelief that Python cannot do this).


Since I am at it, this is how the GIL can be released:

- Many functions in Python's standard library, particularly all
blocking i/o functions, release the GIL.

- In C or C++ extensions, use the macros Py_BEGIN_ALLOW_THREADS and
Py_END_ALLOW_THREADS.

- With ctypes, functions called from a cdll release the GIL, whereas
functions called from a pydll do not.

- In f2py, declaring a Fortran function threadsafe in a .pyf file or
cf2py comment releases the GIL.

- In Cython or Pyrex, use a "with nogil:" block to execute code
without holding the GIL.


Regards,
Sturla Molden

























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


Re: Subclass dynamically

2009-08-19 Thread Piet van Oostrum
> Robert Dailey  (RD) wrote:

>RD> Hey,
>RD> I have a class that I want to have a different base class depending on
>RD> a parameter that I pass to its __init__method. For example
>RD> (pseudocode):

>RD> class MyDerived( self.base ):
>RD>   def __init__( self, base ):
>RD> self.base = base


>RD> Something like that... and then I would do this:

>RD> foo = MyDerived( MyBase() )

What do you want? As you write it now foo would be an instance of
MyDerived, but you say you want to have a class with a different base
class...

So does this mean that foo should become that class or that foo should
become an instance of a new anonymous class that has a specified base
class?

And on the other hand is MyBase the required base class. But you pass an
instance of MyBase, not MyBase itself. As you have it above MyBase()
should be a class, therefore MyBase should be a metaclass. Or is that
not what you want?
-- 
Piet van Oostrum 
URL: http://pietvanoostrum.com [PGP 8DAE142BE17999C4]
Private email: [email protected]
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Parallelization in Python 2.6

2009-08-19 Thread Hendrik van Rooyen
On Wednesday 19 August 2009 10:13:41 Paul Rubin wrote:
> Hendrik van Rooyen  writes:
> > Just use thread then and thread.start_new_thread.
> > It just works.
>
> The GIL doesn't apply to threads made like that?!

The GIL does apply - I was talking nonsense again.  Misread the OP's 
intention.

- Hendrik

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


Re: recording input from USB port and write to text file

2009-08-19 Thread Diez B. Roggisch
Piet van Oostrum wrote:

>> Allan  (A) wrote:
> 
>>A> Hi! I'm fairly new to Python.  I understand the basics basics but I'm
>>A> been trying to write a simple python code that will let me read input
>>A> data (such as mouse movement) from my USB port and write it in a text
>>A> file and I am so lost.  Can anyone help or direct me to some
>>A> resources?  Thank you!
> 
> You could try http://sourceforge.net/projects/pyusb/

As I said - this is more complicated as it appears. HID-devices will be
captured by the OS and can't be accessed by libusb then (with good reason -
otherwise writing a keylogger is something not even kernel-space is needed
anymore)

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


Re: Parallelization in Python 2.6

2009-08-19 Thread sturlamolden
On 19 Aug, 05:27, Dave Angel  wrote:

> With the current GIL implementation, for two CPU-bound tasks, you either
> do them sequentially, or make a separate process.

I'd also like to add that most compute-bound code should be delegated
to specialized C libraries, many of which are prewritten. For example,
FFTW, Intel MKL, ATLAS, LAPACK, NAG. When you do this, the GIL has no
consequence unless it is kept locked. So even for scientific programs,
writing parallel compute-bound code mostly involves calling into a C
or Fortran library with the GIL released. I have yet to see compute-
bound code that could not be easily migrated to C or Fortran, either
using existing libraries (the common case) or specialised code.
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Parallelization in Python 2.6

2009-08-19 Thread sturlamolden
On 19 Aug, 05:34, Hendrik van Rooyen  wrote:

> The GIL does apply - I was talking nonsense again.  Misread the OP's
> intention.

It depends on what the OP's functions "doStuff1" and "doStuff2"
actually do. If they release the GIL (e.g. make I/O calls) it does not
apply. The GIL only serialize access to the interpreter.

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


Re: Parallelization in Python 2.6

2009-08-19 Thread sturlamolden
On 18 Aug, 11:41, Stefan Behnel  wrote:

> I think the canonical answer is to use the threading module or (preferably)
> the multiprocessing module, which is new in Py2.6.
>
> http://docs.python.org/library/threading.htmlhttp://docs.python.org/library/multiprocessing.html
>
> Both share a (mostly) common interface and are simple enough to use. They
> are pretty close to the above interface already.

There is a big difference between them, which is that multiprocessing
do not work with closures. This means that the threading module is
simpler to use than multiprocessing if you want to parallelize serial
code. You just wrap a closure around whatever block of code you want
to run in a thread. For the same reason, programming with OpenMP is
easier than using pthreads directly in C/C++. C does not have
closures, which is the raison d'etre for OpenMP. Multiprocessing has
the same limitation as abstraction for parallel programming as
pthreads in C. Python's threading module do not, but the GIL can be a
limitation.






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


listen and receive the stock price from a VPN

2009-08-19 Thread John Liu
Hi, My friends
 
I am trying a Pyhton script to connect a VPN with (IP address, user ID, and 
passwrod).
 
suppose I need to use Socket to connect the VPN and compare to a list of stock 
tickers I want to receive. 
 
Is there any wasy way and fast way to do that?
 
Thanks so much in advance.
 
Kindly regards,
 
John


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


Re: Using a Callback Function - ftplib

2009-08-19 Thread nitebirdz
On Mon, Aug 17, 2009 at 11:10:25AM -0700, seldan24 wrote:
> 
> I didn't even notice the higher level methods.  I changed the
> retrieval line to:
> 
> ftp.nlst("testfile*.txt")
> 
> This works great.  The result is even captured in an array.  I really
> have no idea what the difference between a LIST and NLST is within
> FTP.  Never delved that deep into it.  I did notice that an NLST will
> return a specific FTP code if a file doesn't exist, whereas a LIST
> doesn't.  So, I ended up using NLST as that'll generate an
> ftplib.error_perm exception.  Based on if the job cares if a file is
> not available or not (some do, some don't), I'll either exit, or
> continue on with the file loop.
> 

The following thread from a NetBSD mailing list may help clarify this
issue:

http://mail-index.netbsd.org/netbsd-users/2001/01/30/0016.html


NLST returns a machine-readable list of names, while LIST returns a
human-readable list.  Hene the presence of the FTP code in the case of
NLST.  

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


Re: Parallelization in Python 2.6

2009-08-19 Thread sturlamolden
On 19 Aug, 05:27, Dave Angel  wrote:

> But if you do it that way, it's slower than sequential.  And if you have
> a multi-core processor, or two processors, or ...   then it gets much
> slower yet, and slows down other tasks as well.
>
> With the current GIL implementation, for two CPU-bound tasks, you either
> do them sequentially, or make a separate process.

For CPU bound tasks, one should put the bottleneck in C/Fortran/Cython
and release the GIL. There is a speed penalty of 200x from using
Python instead of C. With a quadcore processor you can gain less than
4x speed-up by parallelizing. If you really care enough about speed to
write parallel code, the first thing you should do is migrate the
bottleneck to C.








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


Re: Need cleanup advice for multiline string

2009-08-19 Thread Carl Banks
On Aug 18, 8:49 pm, Ben Finney  wrote:
> Grant Edwards  writes:
> > On 2009-08-19, Ben Finney  wrote:
> > > Simon Forman  writes:
> > >> We are one family.
>
> > > Agreed.
>
> > That's not much comfort if you've seen the way many families get along
> > with each other.
>
> Demonstrable facts, by nature of being independently verifiable, are a
> better point of unification than comforting illusions, however
> confidently asserted.

You know, if you're going to escalate a budding flame war the least
you could do is to choose to do it some other way than by following up
to an obvious joke, probably one designed to diffuse the ill-feeling.


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


Re: Changing Python Opcodes

2009-08-19 Thread Peter Otten
Diez B. Roggisch wrote:

> Sreejith K wrote:

>> So I compiled Python from source changing some opcode values

>  Nobody
> can be helping you there, because it's *your* code, not Python anymore.
> And giving others access to it defies somewhat the purpose of the whole
> exercise

...and everyone with the expertise to decompile your application and do 
something useful with the result will also be able to find this thread. 
Basically you're trying the security through obscurity stunt without the 
obscurity...

Peter

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


Re: difference between 2 arrays

2009-08-19 Thread Michel Claveau - MVP
Re ! 

Juste pour signaler qu'il existe un newsgroup en français sur Python, qui 
permet de recevoir des réponses en français (donc plus complètes/détaillées).

@-salutations
-- 
Michel Claveau 

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


can't retrieve data from pyxml

2009-08-19 Thread Sakib
well, i need to retrive data from the following line of xml.

 

i need the Caption and the type data.

is any one out there help me doing that?

thanks,
Sakib
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Using a Callback Function - ftplib

2009-08-19 Thread seldan24
On Aug 18, 6:02 am, Nitebirdz  wrote:
> On Mon, Aug 17, 2009 at 11:10:25AM -0700, seldan24 wrote:
>
> > I didn't even notice the higher level methods.  I changed the
> > retrieval line to:
>
> > ftp.nlst("testfile*.txt")
>
> > This works great.  The result is even captured in an array.  I really
> > have no idea what the difference between a LIST and NLST is within
> > FTP.  Never delved that deep into it.  I did notice that an NLST will
> > return a specific FTP code if a file doesn't exist, whereas a LIST
> > doesn't.  So, I ended up using NLST as that'll generate an
> > ftplib.error_perm exception.  Based on if the job cares if a file is
> > not available or not (some do, some don't), I'll either exit, or
> > continue on with the file loop.
>
> The following thread from a NetBSD mailing list may help clarify this
> issue:
>
> http://mail-index.netbsd.org/netbsd-users/2001/01/30/0016.html
>
> NLST returns a machine-readable list of names, while LIST returns a
> human-readable list.  Hene the presence of the FTP code in the case of
> NLST.  

Nitebirdz,

Thanks for the information.  I knew it stood for 'named list' but had
no idea how that differed from the standard.  I appreciate the link.
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: can't retrieve data from pyxml

2009-08-19 Thread Diez B. Roggisch

Sakib schrieb:

well, i need to retrive data from the following line of xml.

 

i need the Caption and the type data.

is any one out there help me doing that?


That's not XML. It lacks namespace-declarations. So no XML-parser will 
(or should) grok it.


Also, to get help here it's better to show what you already tried, 
instead of simply asking others to do your work for you.


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


Re: Code formatting question: conditional expression

2009-08-19 Thread Diez B. Roggisch
> BTW, from the (admittedly few) responses to my original post, it seems
> there's some sentiment that "conditional expressions" are a non-Pythonic
> misfeature. Interesting ...

No. I love them. But not if they are so large that they stretch over several
lines (or to many columns).

 foo = bar if cond else baz

is more than fine for me. But

 foo = I_need_to_do_something_really_complicated_here() if cond else baz

isn't, because one doesn't grasp as easily in one look that we're talking a
ternary operator here.

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


Re: Need cleanup advice for multiline string

2009-08-19 Thread Ben Finney
Jean-Michel Pichavant  writes:

> Anyway the hysteria that is surrounding this thread is just amazing.

If the calm reproach that has been the maximum response so far seems
like “hysteria” to you, I can only conclude you have only been using the
internet for a few hours.

-- 
 \  “The fact that I have no remedy for all the sorrows of the |
  `\ world is no reason for my accepting yours. It simply supports |
_o__)  the strong probability that yours is a fake.” —Henry L. Mencken |
Ben Finney
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Scope and classes

2009-08-19 Thread Ben Finney
David  writes:

> Out of 'Abc.message' and 'self.message', which is the favoured
> convention?

It's not a matter of convention. They mean different things, so you use
each depending on what you mean.

> It would be very easy to accidentally override 'self.messages' with an
> instance attribute!

Right. So you use ‘Abc.message’ when you specifically want a class
independent of any instance, and you use ‘self.message’ when you want
the attribute of this instance (with fallback to the usual resolution
order).

-- 
 \  “Any intelligent fool can make things bigger and more complex… |
  `\It takes a touch of genius – and a lot of courage – to move in |
_o__)the opposite direction.” —Albert Einstein |
Ben Finney
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Need cleanup advice for multiline string

2009-08-19 Thread Ben Finney
Grant Edwards  writes:

> On 2009-08-19, Ben Finney  wrote:
> > Simon Forman  writes:
> >> We are one family.
> >
> > Agreed.
>
> That's not much comfort if you've seen the way many families get along
> with each other.

Demonstrable facts, by nature of being independently verifiable, are a
better point of unification than comforting illusions, however
confidently asserted.

-- 
 \  “Life does not cease to be funny when people die any more than |
  `\  it ceases to be serious when people laugh.” —George Bernard Shaw |
_o__)  |
Ben Finney
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Need cleanup advice for multiline string

2009-08-19 Thread Ben Finney
Simon Forman  writes:

> On Tue, Aug 18, 2009 at 8:42 PM, Ben Finney wrote:
> > We're all unified by our humanity. Bringing any god into the picture
> > is surely counter to any goals of unity.
>
> Unity "in humanity" is, to my way of thinking, the same as Unity "in
> God".

Then you're playing Humpty-Dumpty games with words. You know very well
that “God” has established meanings entirely different from “humanity”,
and those meanings played a part in your choice of that word.

I maintain that you can't consistently make a declaration in favour of
human unity and unfounded religious assertions.

> I think Unity, like None, is a built-in singleton, so to speak.

This is white noise.

> >> We are one family.
> >
> > Agreed.
>
> <3

I think we can peaceably leave it at that.

-- 
 \“Spam will be a thing of the past in two years' time.” —Bill |
  `\ Gates, 2004-01-24 |
_o__)  |
Ben Finney
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Need cleanup advice for multiline string

2009-08-19 Thread Grant Edwards
On 2009-08-19, Ben Finney  wrote:
> Simon Forman  writes:

>> We are one family.
>
> Agreed.

That's not much comfort if you've seen the way many families
get along with each other.

-- 
Grant

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


Re: Converting DD MM YYYY into YYYY-MM-DD?

2009-08-19 Thread Gilles Ganault
I find it odd that the regex library can't handle European characters
:-/
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Parallelization in Python 2.6

2009-08-19 Thread Neal Becker
sturlamolden wrote:

> On 18 Aug, 11:41, Stefan Behnel  wrote:
> 
>> I think the canonical answer is to use the threading module or
>> (preferably) the multiprocessing module, which is new in Py2.6.
>>
>> 
http://docs.python.org/library/threading.htmlhttp://docs.python.org/library/multiprocessing.html
>>
>> Both share a (mostly) common interface and are simple enough to use. They
>> are pretty close to the above interface already.
> 
> There is a big difference between them, which is that multiprocessing
> do not work with closures. This means that the threading module is
> simpler to use than multiprocessing if you want to parallelize serial
> code. You just wrap a closure around whatever block of code you want
> to run in a thread.

Do you have an example of this technique?


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


Re: Subclass dynamically

2009-08-19 Thread Bruno Desthuilliers

Robert Dailey a écrit :

Hey,

I have a class that I want to have a different base class depending on
a parameter that I pass to its __init__method. For example
(pseudocode):

class MyDerived( self.base ):
  def __init__( self, base ):
self.base = base


Something like that... and then I would do this:

foo = MyDerived( MyBase() )



What is your real use case ? I mean, what problem are you actually 
trying to solve this way ?


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


Re: Code formatting question: conditional expression

2009-08-19 Thread Bruno Desthuilliers

Richard Brodie a écrit :
"John Posner"  wrote in message 
news:[email protected]...



 if total > P.BASE:
 excessblk = Block(total - P.BASE, srccol, carry_button_suppress=True)
 else:
 excessblk = None


I wonder if it is appropriate to replace the None sentinel with one that is an 
instance
of Block() e.g.

size = total - P.BASE
excessblk = Block(size, srccol, carry_button_suppress=True, empty_block=(size 
<= 0) )


In which case the last param is possibly redundant - the Block object 
knows its size, so it might be able to know by itself if it's empty.


NB : please notice the 'possibly' and 'might' cautions !-)
--
http://mail.python.org/mailman/listinfo/python-list


NOOB: Developing using python on two different computers

2009-08-19 Thread Smeagol
Hi there,

Occasionally I have to develop on two different computers, and I was
wondering if there was a way to copy the python "environment" from one
to the other?

Access to the data is trivial (networked database) but various
packages etc exist on one computer, and I want to ensure I have
everything package-wise on the other.

Both are "windows" machines (native windows box and a Macbook with
VMWare running XP)...

Is it as simple as copying the Python directory, or is there a script
I can run that will tell me what packages are installed on the native
windows box and then I can use easy_install to update the VM partition
on the Macbook?

Much obliged for any help.
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: How to create functors?

2009-08-19 Thread Bruno Desthuilliers

Terry Reedy a écrit :

Robert Dailey wrote:


I'm using Python 2.6. And using the legacy syntax in the lambda does
not work either. I want to avoid using a def if possible. Thanks.


In Python, writing

name = lambda arg: expr

instead of

def name(arg): return expr

is all negative and no positive and should be avoided.


Except that def is a statement, and as such can't be used as a named 
params when calling a function expecting a callback, ie:


vroom = some_dead('parrot', name=lambda arg: exp)

(notice the 'name = lambda arg: exp' ?-)

Ok, nitpicking. Me --->[]


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


Re: difference between 2 arrays

2009-08-19 Thread John Machin
On Aug 19, 6:56 pm, "Michel Claveau -
MVP" wrote:

> See the module "sets"

See especially the notice at the front of the current sets doc which
says "deprecated since 2.6" and the comparison down the end which
explains why the built-in set() and frozenset() are better than the
sets module. Starting now to use the sets module is not a good idea
unless the OP is stuck on using Python 2.3 .
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Help with libxml2dom

2009-08-19 Thread Paul Boddie
On 19 Aug, 13:55, Nuno Santos  wrote:
> I have just started using libxml2dom to read html files and I have some
> questions I hope you guys can answer me.

[...]

>  >>> table = body.firstChild
>  >>> table.nodeName
> u'text' #?! Why!? Shouldn't it be a table? (1)

You answer this yourself just below.

>  >>> table = body.firstChild.nextSibling #why this works? is there a
> text element hidden? (2)
>  >>> table.nodeName
> u'table'

Yes, in the DOM, the child nodes of elements include text nodes, and
even though one might regard the whitespace before the first child
element and that appearing after the last child element as
unimportant, the DOM keeps it around in case it really is important.

[...]

> It seems like sometimes there are some text elements 'hidden'. This is
> probably a standard in DOM I simply am not familiar with this and I
> would very much appreciate if anyone had the kindness to explain me this.

Well, the nodes are actually there: they're whitespace used to provide
the indentation in your example. I recommend using XPath to get actual
elements:

table = body.xpath("*")[0] # get child elements and then select the
first

Although people make a big "song and dance" about the DOM being a
nasty API, it's quite bearable if you use it together with XPath
queries.

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


Re: Parallelization in Python 2.6

2009-08-19 Thread Grant Edwards
On 2009-08-19, Stefan Behnel  wrote:
> Dennis Lee Bieber wrote:

>>  If they are number crunchers (CPU-bound) and don't make use of
>> binary extension libraries that release the GIL (for the most common
>> Python implementation), they'll run faster being called in sequence
>> since you won't have the overhead of task switching.
>
> ... unless, obviously, the hardware is somewhat up to date
> (which is not that uncommon for number crunching environments)
> and can execute more than one thing at once.

Even with multiple processors, it'll still be faster to call
them sequentially in a single thread than run them sequentially
in multiple threads with the GIL serializing them.

-- 
Grant Edwards   grante Yow! Gibble, Gobble, we
  at   ACCEPT YOU ...
   visi.com
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: difference between 2 arrays

2009-08-19 Thread baalu aanand
On Aug 19, 1:48 pm, Pierre  wrote:
> Hello,
>
> I would like to know how to find the difference (set operation)
> between 2 arrays :
>
> a = array([1,2, 3,2,5,2])
> b = array([1,2])
> I want a - b = [3,5]
>
> Well, the equivalence of setdiff in matlab...
>
> I thought a.difference(b) could work, but no : AttributeError:
> 'numpy.ndarray' object has no attribute 'difference'
>
> Thanks !



Hi,

  Here I have given my logic, check whether it helps for you

a = [1,2, 3,2,5,2]
b = [1,2]
j = 0
dif = []
for i in range(len(a)) :
   if a[i] not in b:
 dif.append(a[i])
 j += 1

print dif[k]



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


Re: difference between 2 arrays

2009-08-19 Thread baalu aanand
On Aug 19, 1:48 pm, Pierre  wrote:
> Hello,
>
> I would like to know how to find the difference (set operation)
> between 2 arrays :
>
> a = array([1,2, 3,2,5,2])
> b = array([1,2])
> I want a - b = [3,5]
>
> Well, the equivalence of setdiff in matlab...
>
> I thought a.difference(b) could work, but no : AttributeError:
> 'numpy.ndarray' object has no attribute 'difference'
>
> Thanks !

Hi,

  Here I have given my logic, check whether it helps for you

a = [1,2, 3,2,5,2]
b = [1,2]
j = 0
dif = []
for i in range(len(a)) :
   if a[i] not in b:
 dif.append(a[i])
 j += 1

print dif

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


Re: difference between 2 arrays

2009-08-19 Thread Matthias Huening

Pierre (19.08.2009 10:48):

Hello,

I would like to know how to find the difference (set operation)
between 2 arrays :

a = array([1,2, 3,2,5,2])
b = array([1,2])
I want a - b = [3,5]



What about set()?

>>> a = set([1,2, 3,2,5,2])
>>> b = set([1,2])
>>> a.difference(b)
set([3, 5])


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


New message

2009-08-19 Thread La Vie Spirituelle
Test
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: difference between 2 arrays

2009-08-19 Thread David Robinow
On Wed, Aug 19, 2009 at 4:48 AM, Pierre wrote:
> Hello,
>
> I would like to know how to find the difference (set operation)
> between 2 arrays :
>
> a = array([1,2, 3,2,5,2])
> b = array([1,2])
> I want a - b = [3,5]
>
> Well, the equivalence of setdiff in matlab...
>
> I thought a.difference(b) could work, but no : AttributeError:
> 'numpy.ndarray' object has no attribute 'difference'
>
> Thanks !
> --
> http://mail.python.org/mailman/listinfo/python-list
>

import numpy
a = numpy.array([1,2,3,2,5,2])
b = numpy.array([1,2])
c = list(set(a)-set(b))
# or c = numpy.array(list(set(a)-set(b)))   if you want to continue w/ arrays
print c
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Read C++ enum in python

2009-08-19 Thread Ludo

Neil Hodgson a écrit :


   For some headers I tried it didn't work until the .* was changed to a
non-greedy .*? to avoid removing from the start of the first comment to
the end of the last comment.

file_data = ' '.join(re.split(r'\/\*.*?\*\/', file_data))


Thank you ! I adopt it !

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


Re: difference between 2 arrays

2009-08-19 Thread Diez B. Roggisch
baalu aanand wrote:

> On Aug 19, 1:48 pm, Pierre  wrote:
>> Hello,
>>
>> I would like to know how to find the difference (set operation)
>> between 2 arrays :
>>
>> a = array([1,2, 3,2,5,2])
>> b = array([1,2])
>> I want a - b = [3,5]
>>
>> Well, the equivalence of setdiff in matlab...
>>
>> I thought a.difference(b) could work, but no : AttributeError:
>> 'numpy.ndarray' object has no attribute 'difference'
>>
>> Thanks !
> 
> 
> 
> Hi,
> 
>   Here I have given my logic, check whether it helps for you
> 
> a = [1,2, 3,2,5,2]
> b = [1,2]
> j = 0
> dif = []
> for i in range(len(a)) :
>if a[i] not in b:
>  dif.append(a[i])
>  j += 1
> 
> print dif[k]


EEEK, quadratic behavior!!

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


Re: difference between 2 arrays

2009-08-19 Thread Robert Kern

On 2009-08-19 01:48 AM, Pierre wrote:

Hello,

I would like to know how to find the difference (set operation)
between 2 arrays :

a = array([1,2, 3,2,5,2])
b = array([1,2])
I want a - b = [3,5]

Well, the equivalence of setdiff in matlab...


You will want to ask numpy questions on the numpy mailing list.

  http://www.scipy.org/Mailing_Lists

Using set() is frequently a good option, but for large arrays, you will want to 
avoid the overhead of converting to and from sets and use numpy.setdiff1d(a, b):


In [2]: a = array([1,2, 3,2,5,2])

In [3]: b = array([1,2])

In [4]: numpy.setdiff1d(a, b)
Out[4]: array([3, 5])

--
Robert Kern

"I have come to believe that the whole world is an enigma, a harmless enigma
 that is made terrible by our own mad attempt to interpret it as though it had
 an underlying truth."
  -- Umberto Eco

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


Re: How do I convert an iterator over bytes into a str?

2009-08-19 Thread markscottwright
On Aug 18, 6:52 pm, "Jan Kaliszewski"  wrote:
> 19-08-2009 o 00:24:20 markscottwright  wrote:
>
> > What's the correct way to turn an iterator over bytes into a string?
> > This works, but, ewww:
> >     In [8]: "".join(iter("four score and seven years ago"))
> >     Out[8]: 'four score and seven years ago'
>
> But it is the correct way (and even recommended over s=s+t or s+=t, when
> applicable
> -- see:  
> http://docs.python.org/library/stdtypes.html#sequence-types-str-unico...).
>
> Cheers,
> *j
>
> --
> Jan Kaliszewski (zuo) 

Thanks Jan (and all other responders).  I suppose I shouldn't be
surprised - it's a known wart (http://wiki.python.org/moin/
PythonWarts), but it just looks so darn wrong.  It is, as you point
out, much faster than "better looking" alternatives, though -
http://www.skymind.com/~ocrow/python_string/
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Need cleanup advice for multiline string

2009-08-19 Thread Aahz
In article ,
Jean-Michel Pichavant   wrote:
>MRAB wrote:
>> Carl Banks wrote:
>>> On Aug 17, 10:03 am, Jean-Michel Pichavant 
>>> wrote:

 I'm no English native, but I already heard women/men referring to a
 group as "guys", no matter that group gender configuration. It's even
 used for group composed exclusively of women. Moreover it looks like a
 *very* friendly form, so there is really nothing to worry about it.
>>>
>>> I like how being very friendly means calling people after a guy who
>>> tried to blow up the English Parliament.
>>
>> Guy Fawkes adopted the name Guido while fighting for the Spanish in the
>> Low Countries:
>>
>> http://en.wikipedia.org/wiki/Guy_Fawkes
>
>I didn't get Carl's reference. The only thing I know about blowing the 
>parliament is from the movie V for Vendetta (no comment please !).

You should read the original comic book, it's much more interesting (and
clearly mentions the Guy Fawkes connection).
-- 
Aahz ([email protected])   <*> http://www.pythoncraft.com/

"I saw `cout' being shifted "Hello world" times to the left and stopped
right there."  --Steve Gonedes
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: difference between 2 arrays

2009-08-19 Thread sturlamolden
On 19 Aug, 01:48, Pierre  wrote:

> Well, the equivalence of setdiff in matlab...

That would be numpy.setdiff1d.
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: recording input from USB port and write to text file

2009-08-19 Thread Simon Forman
On Aug 18, 7:33 pm, Allan  wrote:
> Hi! I'm fairly new to Python.  I understand the basics basics but I'm
> been trying to write a simple python code that will let me read input
> data (such as mouse movement) from my USB port and write it in a text
> file and I am so lost.  Can anyone help or direct me to some
> resources?  Thank you!

This isn't exactly what you were looking for, but maybe it will give
you some ideas:


from Tkinter import *

def printMotionEvents(event):
print "mouse coords: %i x %i" % (event.x, event.y)

t = Tk()
t.bind('', printMotionEvents)
t.mainloop()


This script creates a little Tkinter window and then makes it so that
when you move the mouse over the window the mouse coordinates are
printed to stdout.
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Database query execution times in Python?

2009-08-19 Thread Aahz
In article ,
pwnedd   wrote:
>
>> Look up EXPLAIN
>
>Thanks for the suggestion. I don't see any option to have EXPLAIN display
>the query time though?

My suggestion was partly a gentle push toward a database forum to get
more information -- this isn't really a Python question.  Unless all you
want to do is get the wall-clock time, in which case just use
time.time().
-- 
Aahz ([email protected])   <*> http://www.pythoncraft.com/

"Given that C++ has pointers and typecasts, it's really hard to have a
serious conversation about type safety with a C++ programmer and keep a
straight face.  It's kind of like having a guy who juggles chainsaws
wearing body armor arguing with a guy who juggles rubber chickens wearing
a T-shirt about who's in more danger."  --Roy Smith
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Need cleanup advice for multiline string

2009-08-19 Thread Simon Forman
On Aug 19, 12:05 am, Ben Finney  wrote:
> Simon Forman  writes:
> > On Tue, Aug 18, 2009 at 8:42 PM, Ben Finney 
> > wrote:
> > > We're all unified by our humanity. Bringing any god into the picture
> > > is surely counter to any goals of unity.
>
> > Unity "in humanity" is, to my way of thinking, the same as Unity "in
> > God".
>
> Then you're playing Humpty-Dumpty games with words. You know very well
> that “God” has established meanings entirely different from “humanity”,
> and those meanings played a part in your choice of that word.
>
> I maintain that you can't consistently make a declaration in favour of
> human unity and unfounded religious assertions.
>
> > I think Unity, like None, is a built-in singleton, so to speak.
>
> This is white noise.
>
> > >> We are one family.
>
> > > Agreed.
>
> > <3
>
> I think we can peaceably leave it at that.

Hear hear!

(FWIW, I've always admired Humpty Dumpty's attitude to words.  Have
you ever read R.A. Wilson's "Quantum Psychology"?)

Regards,
~Simon
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: httplib incredibly slow :-(

2009-08-19 Thread Aahz
In article ,
Chris Withers   wrote:
>Aahz wrote:
>>
>> What do you need to know for a decent example?
>
>Simple download of a file from a url with some auth headers added would 
>do me.

Well, I've hacked up some sample code from my company's codebase:

# !!! UNTESTED !!!
c = pycurl.Curl()
c.setopt(pycurl.URL, url)
c.setopt(pycurl.USERPWD, "%s:%s" % (user, pwd))
c.setopt(pycurl.FOLLOWLOCATION, 1)
c.setopt(pycurl.MAXREDIRS, 5)
c.setopt(pycurl.CONNECTTIMEOUT, 30)
f = StringIO()
c.setopt(pycurl.WRITEDATA, f)
c.perform()
c.close()
-- 
Aahz ([email protected])   <*> http://www.pythoncraft.com/

"Given that C++ has pointers and typecasts, it's really hard to have a
serious conversation about type safety with a C++ programmer and keep a
straight face.  It's kind of like having a guy who juggles chainsaws
wearing body armor arguing with a guy who juggles rubber chickens wearing
a T-shirt about who's in more danger."  --Roy Smith
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: generate keyboard/mouse event under windows

2009-08-19 Thread Ray
On Aug 19, 2:07 pm, yaka  wrote:
> Read this and see if it helps:
>
> http://kvance.livejournal.com/985732.html

is there a way to generate a 'true' keyboard event? (works like user
pressed a key on keyboard)
not send the 'send keyboard event to application' ?
-- 
http://mail.python.org/mailman/listinfo/python-list


Python and PHP encryption/decryption

2009-08-19 Thread Jean-Claude Neveu
I'm looking for a recommendation about encryption/decryption packages 
for Python.


I'm working on a project that will require me to store some values in 
a database in encrypted format. I'll be storing them from a PHP 
script and retrieving them (decrypting them) using Python. I'm 
currently using PHP's mcrypt package to encrypt the values, and I'm 
using AES for encryption, so something AES-compatible would be ideal. 
However, the project is at the development stage so I can easily 
change how I'm encrypting things in PHP if there is a compelling 
reason on the Python side of things to do so.


Many Thanks,
Jean-Claude

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


Re: Any way to adjust difflib algorithm?

2009-08-19 Thread Aahz
In article ,
Grant Edwards   wrote:
>On 2009-08-14, Grant Edwards  wrote:
>>
>> In my particular usage, no lines have ever been
>> inserted/deleted, so perhaps I should be running diffs on
>> individual lines instead?  If I do that, I can't figure out
>> how to generate HTML output.
>
>I ended up using the SequenceMatcher on individual pairs of
>lines and generating my own HTML based on the results of
>get_matching_blocks().
>
>That produced the desired results.

Good work!  Note that IME most diff software shows changed lines as a
delete-and-add.  For example, diff -u
-- 
Aahz ([email protected])   <*> http://www.pythoncraft.com/

"Given that C++ has pointers and typecasts, it's really hard to have a
serious conversation about type safety with a C++ programmer and keep a
straight face.  It's kind of like having a guy who juggles chainsaws
wearing body armor arguing with a guy who juggles rubber chickens wearing
a T-shirt about who's in more danger."  --Roy Smith
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: NOOB: Developing using python on two different computers

2009-08-19 Thread Jonathan Gardner
On Aug 19, 6:50 am, Smeagol  wrote:
> Hi there,
>
> Occasionally I have to develop on two different computers, and I was
> wondering if there was a way to copy the python "environment" from one
> to the other?
>
> Access to the data is trivial (networked database) but various
> packages etc exist on one computer, and I want to ensure I have
> everything package-wise on the other.
>
> Both are "windows" machines (native windows box and a Macbook with
> VMWare running XP)...
>
> Is it as simple as copying the Python directory, or is there a script
> I can run that will tell me what packages are installed on the native
> windows box and then I can use easy_install to update the VM partition
> on the Macbook?
>

Simple copy won't do it.

Check out virtualenv (http://pypi.python.org/pypi/virtualenv).
Reinstalling each env from scratch is usually the best bet. List your
dependencies in your packages and all you have to do is easy_install
your packages.
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Any way to adjust difflib algorithm?

2009-08-19 Thread Grant Edwards
On 2009-08-19, Aahz  wrote:
> In article ,
> Grant Edwards   wrote:
>>On 2009-08-14, Grant Edwards  wrote:
>>>
>>> In my particular usage, no lines have ever been
>>> inserted/deleted, so perhaps I should be running diffs on
>>> individual lines instead?  If I do that, I can't figure out
>>> how to generate HTML output.
>>
>>I ended up using the SequenceMatcher on individual pairs of
>>lines and generating my own HTML based on the results of
>>get_matching_blocks().
>>
>>That produced the desired results.
>
> Good work!  Note that IME most diff software shows changed
> lines as a delete-and-add.  For example, diff -u

Right -- though difflib did show _some_ lines as changed rather
than deleted/added, it wasn't obvious how it decided between
the two.  I suspect it used some sort of percentage-changed
threshold.

For this application both files had all the same lines (by
definition), so what I was interested in was what parts of each
line changed.

-- 
Grant Edwards   grante Yow! I just heard the
  at   SEVENTIES were over!!  And
   visi.comI was just getting in touch
   with my LEISURE SUIT!!
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: python doc available in emacs info format?

2009-08-19 Thread A.Politz
On Aug 17, 6:43 am, Xah Lee  wrote:
> btw, is there still [no] info format for python doc?
>
> i feel kinda sad [...]
> Part of this is due to [other peoples fault]

Someone started a rst2info project (google it), maybe you want to help
this guy out.

Though, he might be a techgeeker, so watch out !

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


Dictionary from a list

2009-08-19 Thread iu2
Hi all,

I need to create a dictionary out of a list.

Given the list [1, 2, 3, 4, 5, 6]

I need the dictionary: {1:2, 3:4, 5:6}

I'll appreciate your help
Thanks
iu2
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Code formatting question: conditional expression

2009-08-19 Thread John Posner

Diez wrote:

No. I love them. But not if they are so large that they stretch over several
lines (or to many columns).

 foo = bar if cond else baz

is more than fine for me. But

 foo = I_need_to_do_something_really_complicated_here() if cond else baz

isn't, because one doesn't grasp as easily in one look that we're talking a
ternary operator here.
  


But the right side of my brain (see Peter Keller's discussion in the 
recent "importance of syntax" thread) finds it quite easy to recognize 
this as a ternary op:


 foo = (I_need_to_do_something_really_complicated_here()
   if cond else
   baz)

The whitespace below "foo" provides a visual hint, which is confirmed by 
the nearby appearance of "if" on the second line. De gustibus non est 
disputandum! (or not)


Many thanks to all responders!

-John

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


Re: Dictionary from a list

2009-08-19 Thread Diez B. Roggisch

iu2 schrieb:

Hi all,

I need to create a dictionary out of a list.

Given the list [1, 2, 3, 4, 5, 6]

I need the dictionary: {1:2, 3:4, 5:6}


dict(zip(l[::2], l[1::2]))

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


Re: Python and PHP encryption/decryption

2009-08-19 Thread Diez B. Roggisch

Jean-Claude Neveu schrieb:
I'm looking for a recommendation about encryption/decryption packages 
for Python.


I'm working on a project that will require me to store some values in a 
database in encrypted format. I'll be storing them from a PHP script and 
retrieving them (decrypting them) using Python. I'm currently using 
PHP's mcrypt package to encrypt the values, and I'm using AES for 
encryption, so something AES-compatible would be ideal. However, the 
project is at the development stage so I can easily change how I'm 
encrypting things in PHP if there is a compelling reason on the Python 
side of things to do so.


With PyCrypto[1] you have a wide range of choices, amongst others AES.

If that's the best algorithm, or even the best python-package to 
implement it, I don't know.



[1] http://www.dlitz.net/software/pycrypto/


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


Re: recording input from USB port and write to text file

2009-08-19 Thread Diez B. Roggisch

Simon Forman schrieb:

On Aug 18, 7:33 pm, Allan  wrote:

Hi! I'm fairly new to Python.  I understand the basics basics but I'm
been trying to write a simple python code that will let me read input
data (such as mouse movement) from my USB port and write it in a text
file and I am so lost.  Can anyone help or direct me to some
resources?  Thank you!


This isn't exactly what you were looking for, but maybe it will give
you some ideas:


from Tkinter import *

def printMotionEvents(event):
print "mouse coords: %i x %i" % (event.x, event.y)

t = Tk()
t.bind('', printMotionEvents)
t.mainloop()


This script creates a little Tkinter window and then makes it so that
when you move the mouse over the window the mouse coordinates are
printed to stdout.


If it has focus.

The overall problem is hard - see e.g. this:

http://www.wintellect.com/CS/blogs/jrobbins/archive/2008/08/30/so-you-want-to-set-a-windows-journal-recording-hook-on-vista-it-s-not-nearly-as-easy-as-you-think.aspx

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


  1   2   >