Re: How to create a virtual serial port?

2009-04-11 Thread Scott David Daniels

Stuart Davenport wrote:

... I'm on a OS X, python 2.5. Basically I will have a remote application
pushing data (GPS) over the network to a python application I have
running on my Mac, I want this python application to again push the
data on to a "virtual serial port". Then the GPS program I have
running on my MAC, RouteBuddy, can read the data from that serial port
as standard.

The only part I am concerned about here though, is if I can create a
serial port virtually and push data out of it?


OK, this is a Max OS/X question.  Does "RouteBuddy" have any other way
to get data except from the serial port?  Does the mac have a serial
port?or are you talking "serial port over USB" (which would be an
entirely different kettle of fish).  With that out of the wa, I'd try
to ask some Mac guys if the ponying up is possible, and if so how to
do it in _any_ language (likely they'll have an Objective C way).
Then pop back over here (or even better a python-mac list, where
they'll be familiar with how to do lots of stuff) and you can get
help getting python to either do the same thing or talk to a bit
of code that does the byte-transfers.

--Scott David Daniels
[email protected]
--
http://mail.python.org/mailman/listinfo/python-list


communication between objects - help

2009-04-11 Thread Murali kumar
thanks a lot..

I think passing the main object only by reference.. so, this does not causes
any overhead..

am i correct..?


On Fri, Apr 10, 2009 at 4:02 PM, Dave Angel  wrote:

>
>
> Murali kumar wrote:
>
>> hi all..
>> I'm posted in a word doc becoz to add a image to explain my problem..
>> also I think gmail automatically scans for attachments..
>>
>> anyway.. here's my problem...( see the image)
>>
>> http://www.2shared.com/file/5299759/45e4c614/load.html
>>
>> Using : Python 2.6 , wxPython 2.8.9
>> 
>>
>>  * I don’t know how to return config file or data to mainApp object when
>> pressing load button. *
>>
>>
>>  * Is it easy to return filename only and load the file’s data into
>> mainApp
>> object in menu handler itself? *
>>
>>
>>  Please suggest me right direction.. and tell me how to do it?
>>
>> Usually where I can get these informations.. (suggest links and books..)
>>
>>
>> Thanks for any advice
>>
>>
>>
>
> Most of the widget classes in your code should be derived classes, so they
> can hold extra instance data & event handlers and such.  So that means you
> can have extra parameters on the constructor, besides the ones the base
> class requires.  Use one or more of those extra parameters to store your own
> information about the hierarchy.
>
> Simplest example is to add the app instance to each constructor.  Then each
> widget object would know how to call back into the app to do some work, or
> to load data into a common place.
>
> DaveA
>
> --
> http://mail.python.org/mailman/listinfo/python-list
>
--
http://mail.python.org/mailman/listinfo/python-list


Re: safe eval of moderately simple math expressions

2009-04-11 Thread Joel Hedlund

Aaron Brady wrote:

Would you be willing to examine a syntax tree to determine if there
are any class accesses? 


Sure? How do I do that? I've never done that type of thing before so I 
can't really say if it would work or not.


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


Re: safe eval of moderately simple math expressions

2009-04-11 Thread Aaron Brady
On Apr 10, 7:54 pm, Steven D'Aprano  wrote:
> On Thu, 09 Apr 2009 13:13:50 -0400, Terry Reedy wrote:
> > Joel Hedlund wrote:
> >> Hi all!
>
> >> I'm writing a program that presents a lot of numbers to the user, and I
> >> want to let the user apply moderately simple arithmentics to these
> >> numbers. One possibility that comes to mind is to use the eval
> >> function, but since that sends up all kinds of warning flags in my
> >> head,
>
> > Where does the program execute?  If on the user's own machine, no
> > problem.
>
> Until the user naively executes a code sample he downloaded from the
> Internet, and discovers to his horror that his *calculator* is able to
> upload his banking details to an IRC server hosted in Bulgaria.

Mine does that anyway!  ..Often without telling anyone.

>
> How quickly we forget... for twenty or thirty years all malware
> infections was via programs executed on the user's own machine.
>
> > Eval is no more dangerous than Python itself.
>
> But users know Python is a Turing-complete programming language that can
> do anything their computer can do. It would come to an unpleasant
> surprise to discover that (say) your icon editor was also a Turing-
> complete programming language capable of doing anything your C-compiler
> could do. The same holds for applications written in Python.

Don't they know that his calculator is written in Python?  Do many
applications include a programming language?

Why do I get the feeling that the authors of 'pyparsing' are out of
breath?

I wonder if you could do something like copy and paste a "fork" of the
'ast' module, and just remove non-arithmetic classes; then do a normal
walk and transform of the foreign code...
--
http://mail.python.org/mailman/listinfo/python-list


Re: weird try/finally behaviour

2009-04-11 Thread Aaron Brady
On Apr 10, 7:19 pm, Terry Reedy  wrote:
> Sylvain Thénault wrote:
> > Hi there,
>
> > I've encountered the following behaviour which I found surprising:
-
> If you say 'print test()', you shoud see None printed after 'end' (at
> least with 3.0) from the function falling off the end.
>
> The 'if True:' line make no difference.
>
> > As you can see, the 'break' in the finally block makes the 'return 1' 
> > beeing ignored.
> > Is this a known caveat or should it be considered as a bug?
>
> Neither, but it s a bit subtle.  If you replace 'break' with 'return 2'

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


Re: safe eval of moderately simple math expressions

2009-04-11 Thread Joel Hedlund

Matt Nordhoff wrote:

'\x5f'

'_'

getattr(42, '\x5f\x5fclass\x5f\x5f') # __class__



Is that enough to show you the error of your ways?


No, because

>>> print '_' in '\x5f\x5fclass\x5f\x5f'
True


:-D Cuz seriously, it's a bad idea.


Yes probably, but that's not why. :-)


(BTW: What if a user tries to do some ridiculously large calculation to
DoS the app? Is that a problem?)


Nope. If the user wants to hang her own app that's fine with me.

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


Re: safe eval of moderately simple math expressions

2009-04-11 Thread Joel Hedlund

Matt Nordhoff wrote:

'\x5f'

'_'

getattr(42, '\x5f\x5fclass\x5f\x5f') # __class__



Is that enough to show you the error of your ways?


No, because

>>> print '_' in '\x5f\x5fclass\x5f\x5f'
True


:-D Cuz seriously, it's a bad idea.


Yes probably, but that's not why. :-)


(BTW: What if a user tries to do some ridiculously large calculation to
DoS the app? Is that a problem?)


Nope. If the user wants to hang her own app that's fine with me.

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


Re: safe eval of moderately simple math expressions

2009-04-11 Thread Peter Otten
Joel Hedlund wrote:

> Matt Nordhoff wrote:
> '\x5f'
>> '_'
> getattr(42, '\x5f\x5fclass\x5f\x5f') # __class__
>> 
>> 
>> Is that enough to show you the error of your ways?
> 
> No, because
> 
>  >>> print '_' in '\x5f\x5fclass\x5f\x5f'
> True

But what you're planning to do seems more like

>>> def is_it_safe(source):
... return "_" not in source
...
>>> source = "getattr(42, '\\x5f\\x5fclass\\x5f\\x5f')"
>>> if is_it_safe(source):
... print eval(source)
...


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


Reading 3 objects at a time from list

2009-04-11 Thread Matteo
Hi all,
let's see if there is a more "pythonic" way of doing what I'm trying
to do.
I have a lot of strings with numbers like this one:

string = "-1 1.3 100.136 1 2.6 100.726 1 3.9 101.464 -1 5.2 102.105"

I need to pass the numbers to a function, but three at a time, until
the string ends. The strings are of variable length, but always a
multiple of three.

That's what I did:
num = string.split()
for triple in zip(num[::3], num[1::3], num[2::3]):
func(*triple)

it works and I like slices, but I was wondering if there was another
way of doing the same thing, maybe reading the numbers in groups of
arbitrary length n...

any ideas?
--
http://mail.python.org/mailman/listinfo/python-list


Re: Reading 3 objects at a time from list

2009-04-11 Thread Chris Rebert
On Sat, Apr 11, 2009 at 1:44 AM, Matteo  wrote:
> Hi all,
> let's see if there is a more "pythonic" way of doing what I'm trying
> to do.
> I have a lot of strings with numbers like this one:
>
> string = "-1 1.3 100.136 1 2.6 100.726 1 3.9 101.464 -1 5.2 102.105"
>
> I need to pass the numbers to a function, but three at a time, until
> the string ends. The strings are of variable length, but always a
> multiple of three.
>
> That's what I did:
> num = string.split()
> for triple in zip(num[::3], num[1::3], num[2::3]):
>    func(*triple)
>
> it works and I like slices, but I was wondering if there was another
> way of doing the same thing, maybe reading the numbers in groups of
> arbitrary length n...

See the grouper() recipe in the `itertools` module --
http://docs.python.org/library/itertools.html

Cheers,
Chris

-- 
I have a blog:
http://blog.rebertia.com
--
http://mail.python.org/mailman/listinfo/python-list


Re: How to create a virtual serial port?

2009-04-11 Thread Lawrence D'Oliveiro
In message , Stuart Davenport wrote:

> Then the GPS program I have running on my MAC, RouteBuddy, can read the
> data from that serial port as standard.

Macs don't have serial ports.

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


Re: safe eval of moderately simple math expressions

2009-04-11 Thread Joel Hedlund

Peter Otten wrote:

But what you're planning to do seems more like


def is_it_safe(source):

... return "_" not in source
...

source = "getattr(42, '\\x5f\\x5fclass\\x5f\\x5f')"
if is_it_safe(source):

... print eval(source)
...



Bah. You are completely right of course.

Just as a thought experiment, would this do the trick?

def is_it_safe(source):
return "_" not in source and r'\' not in source

I'm not asking because I'm hellbent on having eval in my app, but 
because it's always useful to see what hazards you don't know about.


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


Re: An object that creates (nested) attributes automatically on assignment

2009-04-11 Thread John Machin
On Apr 11, 1:22 pm, Steven D'Aprano  wrote:
> Is "pants" slang for "fragile, hard to understand and difficult to debug"?

pommy slang for "sucks intensely, like the Deathstar's tractor
beam" ... I think we agree with him.
--
http://mail.python.org/mailman/listinfo/python-list


Re: safe eval of moderately simple math expressions

2009-04-11 Thread Peter Otten
Joel Hedlund wrote:

> Peter Otten wrote:
>> But what you're planning to do seems more like
>> 
> def is_it_safe(source):
>> ... return "_" not in source
>> ...
> source = "getattr(42, '\\x5f\\x5fclass\\x5f\\x5f')"
> if is_it_safe(source):
>> ... print eval(source)
>> ...
>> 
> 
> Bah. You are completely right of course.
> 
> Just as a thought experiment, would this do the trick?
> 
> def is_it_safe(source):
>  return "_" not in source and r'\' not in source

>>> "".join(map(chr, [95, 95, 110, 111, 95, 95]))
'__no__'

By the way, a raw string may not end with a backslash:

>>> r'\'
  File "", line 1
r'\'
   ^
SyntaxError: EOL while scanning single-quoted string

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


Re: Python 2.6/3.0 packages for Ubuntu?

2009-04-11 Thread Francesco Bochicchio

[email protected] ha scritto:

Does Ubuntu really not have Python 2.6 or 3.0 packages or do I just have my
package list misconfigured?  I'm setting up a fresh machine and am not too
Ubuntu-aware.  Is there a list of package repositories around somewhere?

Thx,

In current 8.10,  the default python is 2.5, but there is a set of 
packages python3-... which gives you the 3.0.


The upcoming 09.04 will have 2.6 as standard, I think ...

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


Re: safe eval of moderately simple math expressions

2009-04-11 Thread Joel Hedlund

Peter Otten wrote:

def is_it_safe(source):
 return "_" not in source and r'\' not in source



"".join(map(chr, [95, 95, 110, 111, 95, 95]))

'__no__'


But you don't have access to neither map or chr?

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


Re: Adding a Badge to an Icon in Mac OS X

2009-04-11 Thread Cameron Simpson
On 10Apr2009 19:56, Miles  wrote:
| On Fri, Apr 10, 2009 at 5:22 PM, bingo wrote:
| > PyObjc seems to offer the option to add badges to icons in the doc. I
| > need to add badges to any icon... kinda like SCPlugin and dropbox do.
| > I think that SCPlugin is doing it through carbon Icon Services. But I
| > am still trying to figure out how it is done!
| 
| I believe those programs are able to do so because they are Finder
| plugins--it's not something that a separate program could do.  This
| isn't really a Python question, though; you'd probably have better
| luck finding answers on a OS X-related list.

Like this one?

  http://mail.python.org/mailman/listinfo/pythonmac-sig

Cheers,
-- 
Cameron Simpson  DoD#743
http://www.cskk.ezoshosting.com/cs/
--
http://mail.python.org/mailman/listinfo/python-list


Re: safe eval of moderately simple math expressions

2009-04-11 Thread Peter Otten
Joel Hedlund wrote:

> Peter Otten wrote:
>>> def is_it_safe(source):
>>>  return "_" not in source and r'\' not in source
>> 
> "".join(map(chr, [95, 95, 110, 111, 95, 95]))
>> '__no__'
> 
> But you don't have access to neither map or chr?
> 
> /Joel

>>> '5f5f7374696c6c5f6e6f745f736166655f5f'.decode("hex")
'__still_not_safe__'


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


Re: safe eval of moderately simple math expressions

2009-04-11 Thread Joel Hedlund

Peter Otten wrote:

Joel Hedlund wrote:


Peter Otten wrote:

def is_it_safe(source):
 return "_" not in source and r'\' not in source

"".join(map(chr, [95, 95, 110, 111, 95, 95]))

'__no__'

But you don't have access to neither map or chr?

/Joel



'5f5f7374696c6c5f6e6f745f736166655f5f'.decode("hex")

'__still_not_safe__'


Now *that's* a thing of beauty. A horrible, horrible kind of beauty.

Thanks for blowing holes in my inflated sense of security!
/Joel
--
http://mail.python.org/mailman/listinfo/python-list


Re: Reading 3 objects at a time from list

2009-04-11 Thread Francesco Bochicchio

Chris Rebert ha scritto:

On Sat, Apr 11, 2009 at 1:44 AM, Matteo  wrote:

Hi all,
let's see if there is a more "pythonic" way of doing what I'm trying
to do.
I have a lot of strings with numbers like this one:

string = "-1 1.3 100.136 1 2.6 100.726 1 3.9 101.464 -1 5.2 102.105"

I need to pass the numbers to a function, but three at a time, until
the string ends. The strings are of variable length, but always a
multiple of three.

That's what I did:
num = string.split()
for triple in zip(num[::3], num[1::3], num[2::3]):
   func(*triple)

it works and I like slices, but I was wondering if there was another
way of doing the same thing, maybe reading the numbers in groups of
arbitrary length n...


See the grouper() recipe in the `itertools` module --
http://docs.python.org/library/itertools.html

Cheers,
Chris




I would do that with a generator:

>>> def groups(l,n) :
...   while l: yield l[:n]; l=l[n:]
...
>>> list(groups(range(14),4))
[[0, 1, 2, 3], [4, 5, 6, 7], [8, 9, 10, 11], [12, 13]]
>>> list(groups(range(18),3))
[[0, 1, 2], [3, 4, 5], [6, 7, 8], [9, 10, 11], [12, 13, 14], [15, 16, 17]]

Ciao
-
FB




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


Re: How to create a virtual serial port?

2009-04-11 Thread Stuart Davenport
On 11 Apr, 08:52, Scott David Daniels  wrote:
> Stuart Davenport wrote:
> > ... I'm on a OS X, python 2.5. Basically I will have a remote application
> > pushing data (GPS) over the network to a python application I have
> > running on my Mac, I want this python application to again push the
> > data on to a "virtual serial port". Then the GPS program I have
> > running on my MAC, RouteBuddy, can read the data from that serial port
> > as standard.
>
> > The only part I am concerned about here though, is if I can create a
> > serial port virtually and push data out of it?
>
> OK, this is a Max OS/X question.  Does "RouteBuddy" have any other way
> to get data except from the serial port?  Does the mac have a serial
> port?or are you talking "serial port over USB" (which would be an
> entirely different kettle of fish).  With that out of the wa, I'd try
> to ask some Mac guys if the ponying up is possible, and if so how to
> do it in _any_ language (likely they'll have an Objective C way).
> Then pop back over here (or even better a python-mac list, where
> they'll be familiar with how to do lots of stuff) and you can get
> help getting python to either do the same thing or talk to a bit
> of code that does the byte-transfers.
>
> --Scott David Daniels
> [email protected]

Sadly RouteBuddy cannot retrieve data by other means...

Scott, many thanks for the advice. Will try to find a more specific
forum of conversation.

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


Re: An object that creates (nested) attributes automatically on assignment

2009-04-11 Thread Edd
Hi Steven,

Thank you for your response!

On Apr 11, 4:22 am, Steven D'Aprano  wrote:
> On Fri, 10 Apr 2009 19:04:38 -0700, Edd wrote:
> > Hi folks,
>
> > I'd like to use Python itself as the configuration language for my
> > Python application. I'd like the user to be able to write something like
> > this in their config file(s):
>
> >    cfg.laser.on = True
> >    cfg.laser.colour = 'blue'
> >    cfg.discombobulated.vegetables = ['carrots', 'broccoli'] # ...
>
> > To this end, I've created a class that appears to allow instance
> > variables to be created on the fly.
>
> Um, don't all classes allow that?
>
> >>> class MyClass(): pass
> ...
> >>> instance = MyClass()
>
> Or do you mean instance *attributes*? Again, apart from built-in types
> and classes that use __slots__, all classes allow that.
>
> >>> instance.parrot = 'Norwegian Blue'

Yes I probably mean instance attributes. Forgive me, I am not
particularly sure of the terminology. But your MyClass example, won't
quite do what I want, as I'd like to be able to define instance
attributes on top of instance attributes by assignment:

>>> class MyClass(): pass
...
>>> instance = MyClass()
>>> instance.lasers.armed = True
Traceback (most recent call last):
  File "", line 1, in 
AttributeError: MyClass instance has no attribute 'laser'
>>>

> > In other words, I can to the
> > following to read a config file:
>
> >     cfg = Config()
> >     execfile(filename, {'cfg', cfg}, {})
>
> That's okay so long as you trust the user not to put malicious, or buggy,
> code in your config file. Personally, I think config files should be more
> tolerant of errors than a programming language.

That's certainly a valid remark, but this will be a tool for
programmers. I am hoping that the user will make use of the power in
moderation. Often, it really will be useful to allow functions to be
defined in the config files, for example.

> > However, I think my implementation of the Config class is a little
> > crappy. I'd really appreciate the critical eye of a pro. Here's the
> > sauce:
>
> For starters, where is your documentation? No doc strings, not even any
> comments! No, I tell a lie... *one* obscure comment that doesn't really
> explain much.

Yes, you're quite right. I was about to add some doc strings, but I
didn't think the implementation was good enough. That's somewhat
backwards, though, right?! Especially considering I'm asking for
improvements. Anyway, I had hoped that the example usage at the end
would show what the purpose of the class is.

>
> >     class Config(object):
> >         def __init__(self, sealed=False):
> >             def seal():
> >                 for v in self._attribs.values():
> >                     if isinstance(v, self.__class__): v.seal()
> >                 del self.__dict__['seal']
>
> >             d =  {'_attribs': {}, '_a2p': None}
> >             if not sealed: d['seal'] = seal
>
> >             self.__dict__.update(d)
>
> I'm going to try to guess what the above does. When you initialise an
> instance, you can tell the instance to be "sealed" or unsealed. I'm not
> sure what the difference is, or why you would choose one over the other.
> Sealed instances seem to be exactly the same as unsealed instances,
> except they have a seal() method (actually a closure). The seal method,
> when called, recursively seals any embedded Config instances inside the
> current instance, then deletes itself.
>
> Arghhh!!! Self-modifying code!!! Unclean, unclean!!!

Quite!

> I'm not sure why seal() is necessary -- it seems to me that if present,
> all it does is delete itself. So why not just leave it out altogether?

As I said in the original post, such Config objects will be made
available to other kinds of user-written script and it's important
that the Config not change between the execution of one script and the
next. The seal() mechanism was an attempt to help the user from
*accidentally* doing this and then having to try to diagnose the
problem and understand how changing the config might have broken the
invariants of the software. I guess a big "DON'T CHANGE THE CONFIG IN
YOUR SCRIPTS"  message in the manual, might be sufficient, though :)

> You also have a rather complicated way of adding instance attributes.
> Instead of
>
> d =  {'_attribs': {}, '_a2p': None}
> self.__dict__.update(d)
>
> why not just do the more obvious:
>
> self._attribs = {}
> self._a2p = None

Because that would go through __setattr__(), which does something else
(which is the whole point of the class). At least, that was my
understanding, which certainly could be at fault.

This might be nicer I guess:

self.__dict__['_attribs'] = {}
self.__dict__['_a2p'] = None

There used to be more instance attributes than just two so it was
easier to put them in a dict and use update. I agree that it's rather
obfuscated, though.

[Edd's horrendous code snipped]

> It looks like you are just re-inventing the normal attribute mechanism of
> Python. I'm not sure why you feel 

Re: Reading 3 objects at a time from list

2009-04-11 Thread Peter Otten
Matteo wrote:

> Hi all,
> let's see if there is a more "pythonic" way of doing what I'm trying
> to do.
> I have a lot of strings with numbers like this one:
> 
> string = "-1 1.3 100.136 1 2.6 100.726 1 3.9 101.464 -1 5.2 102.105"
> 
> I need to pass the numbers to a function, but three at a time, until
> the string ends. The strings are of variable length, but always a
> multiple of three.
> 
> That's what I did:
> num = string.split()
> for triple in zip(num[::3], num[1::3], num[2::3]):
> func(*triple)
> 
> it works and I like slices, but I was wondering if there was another
> way of doing the same thing, maybe reading the numbers in groups of
> arbitrary length n...
> 
> any ideas?

Ideas? Many. But do they make sense?

>>> from itertools import *
>>> items = range(10)
>>> for chunk in iter((tuple(islice(items, 3)) for items in 
>>> repeat(iter(items))).next, ()):
... print chunk
...
(0, 1, 2)
(3, 4, 5)
(6, 7, 8)
(9,)

>>> for chunk in takewhile(bool, (tuple(islice(items, 3)) for items in 
>>> repeat(iter(items:
... print chunk
...
(0, 1, 2)
(3, 4, 5)
(6, 7, 8)
(9,)
>>> def make_read(items):
... items = iter(items)
... def read(n):
... return tuple(islice(items, n))
... return read
...
>>> from functools import partial
>>> for chunk in iter(partial(make_read(items), 3), ()):
... print chunk
...
(0, 1, 2)
(3, 4, 5)
(6, 7, 8)
(9,)

>>> read = make_read(items)
>>> for n in [3,4,2,1]:
... print read(n)
...
(0, 1, 2)
(3, 4, 5, 6)
(7, 8)
(9,)

>>> items = iter([3, "a", "b", "c", 2, "p", "q", 4, "x", "y", "z", "t"])
>>> read = make_read(items)
>>> for n in items:
... print "".join(read(n))
...
abc
pq
xyzt

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


Re: safe eval of moderately simple math expressions

2009-04-11 Thread Aaron Brady
On Apr 11, 3:18 am, Joel Hedlund  wrote:
> Aaron Brady wrote:
> > Would you be willing to examine a syntax tree to determine if there
> > are any class accesses?
>
> Sure? How do I do that? I've never done that type of thing before so I
> can't really say if it would work or not.
>
> /Joel

NO PROMISES.  No warranty is made, express or implied.

Of course, something this devious, a "white" list, may just make it so
your enemy finds out its weakness before you do.

It's ostensibly for Python 3, but IIRC there's a way to do it in 2.

'ast.literal_eval' appears to evaluate a literal, but won't do
expressions, which is what you are looking for.  We should refer
people to it more often.

+1 ast.walk, btw.

If you want subtraction and division, you'll have to add them
yourself.  You could probably compress the 'is_it_safe' function to
one line, provided that it's sound to start with: if all( x in
safe_node_classes for x in ast.walk( ast.parse( exp ) ) ), or better
yet, if set( ast.walk( ast.parse( exp ) ) )<= safe_node_classes.  +1!

/Source:
import ast
safe_exp= '( 2+ 4 )* 7'
unsafe_exp= '( 2+ 4 ).__class__'
unsafe_exp2= '__import__( "os" )'

safe_node_classes= set( [
ast.Module,
ast.Expr,
ast.BinOp,
ast.Mult,
ast.Add,
ast.Num
] )

def is_it_safe( exp ):
print( 'trying %s'% exp )
top= ast.parse( exp )
for node in ast.walk( top ):
print( node )
if node.__class__ not in safe_node_classes:
return False
print( 'ok!' )
return True

print( safe_exp, is_it_safe( safe_exp ) )
print( )
print( unsafe_exp, is_it_safe( unsafe_exp ) )
print( )
print( unsafe_exp2, is_it_safe( unsafe_exp2 ) )
print( )

/Output:

trying ( 2+ 4 )* 7
<_ast.Module object at 0x00BB5DF0>
<_ast.Expr object at 0x00BB5E10>
<_ast.BinOp object at 0x00BB5E30>
<_ast.BinOp object at 0x00BB5E50>
<_ast.Mult object at 0x00BAF590>
<_ast.Num object at 0x00BB5EB0>
<_ast.Num object at 0x00BB5E70>
<_ast.Add object at 0x00BAF410>
<_ast.Num object at 0x00BB5E90>
ok!
( 2+ 4 )* 7 True

trying ( 2+ 4 ).__class__
<_ast.Module object at 0x00BB5E90>
<_ast.Expr object at 0x00BB5DF0>
<_ast.Attribute object at 0x00BB5E10>
( 2+ 4 ).__class__ False

trying __import__( "os" )
<_ast.Module object at 0x00BB5E10>
<_ast.Expr object at 0x00BB5E30>
<_ast.Call object at 0x00BB5E50>
__import__( "os" ) False
--
http://mail.python.org/mailman/listinfo/python-list


Re: safe eval of moderately simple math expressions

2009-04-11 Thread Steven D'Aprano
On Sat, 11 Apr 2009 11:03:16 +0200, Joel Hedlund wrote:

> Peter Otten wrote:
>> But what you're planning to do seems more like
>> 
> def is_it_safe(source):
>> ... return "_" not in source
>> ...
> source = "getattr(42, '\\x5f\\x5fclass\\x5f\\x5f')" if
> is_it_safe(source):
>> ... print eval(source)
>> ...
>> 
> 
> Bah. You are completely right of course.
> 
> Just as a thought experiment, would this do the trick?
> 
> def is_it_safe(source):
>  return "_" not in source and r'\' not in source
> 
> I'm not asking because I'm hellbent on having eval in my app, but
> because it's always useful to see what hazards you don't know about.

Can we pass your test and still write to a file? Too easy.


>>> file('spam.txt', 'r')  # prove that the file doesn't exist
Traceback (most recent call last):
  File "", line 1, in 
IOError: [Errno 2] No such file or directory: 'spam.txt'
>>>
>>> source = "4+(file('spam.txt', 'w').write('spam spam spam') or 0)+5"
>>> if is_it_safe(source): 
... print eval(source)
...
9
>>> file('spam.txt', 'r').read()
'spam spam spam'


Can we pass your test and import a module and grab its docstring?

>>> source = "getattr(eval(chr(90+5)*2+'im'+'por'+'t'+chr(None or 
>>> 95)*2+'('+chr(39)+'os'+chr(39)+')'), chr(95)*2+'doc'+chr(99-4)*2)"
>>> if is_it_safe(source):
... eval(source)
...
"OS routines for Mac, NT, or Posix depending ... "



Restricting Python is hard. No, not hard. It's *REALLY HARD*. Experts 
have tried and failed. A good example is Tav's recent attempt to secure 
Python code from *one* threat: writing a file on the local disk. Should 
be simple, right? 

If only.

http://tav.espians.com/a-challenge-to-break-python-security.html

The first exploit came an hour after Tav went public.

You can read the discussion on the Python-Dev list starting here:
http://mail.python.org/pipermail/python-dev/2009-February/086401.html


More here:
http://tav.espians.com/paving-the-way-to-securing-the-python-interpreter.html

http://tav.espians.com/update-on-securing-the-python-interpreter.html


My recommendation is that you do one of these:

(1) Give up on making your code "safe". Recognise that the threat is 
relatively small, but real, and put a warning in your documentation about 
the risk to user's own system if they evaluate arbitrary code, and then 
just use eval and hope for the best.

(2) Decide that you don't want your calculate to be a full-fledged 
programming language, and give up on making eval safe. Write your own 
mini-parser to do arithmetic expressions. It's really not that difficult: 
really easy with PyParsing, and not that hard without.



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


Re: I can't get RLock to work (warning, the following code is pretty long)

2009-04-11 Thread Moriaantje
I think it would help if you would call your functions in
get_both_parts with some arguments ...
--
http://mail.python.org/mailman/listinfo/python-list


ANN: PyGUI 2.0

2009-04-11 Thread Greg Ewing

PyGUI 2.0 is available:

  http://www.cosc.canterbury.ac.nz/greg.ewing/python_gui/

Highlights of this release:

* Native Windows implementation, based on pywin32 and ctypes.

* Full set of Postscript-style path construction operators
  available on all platforms.

* Mouse and keyboard events can be intercepted for all
  component classes.

Plus numerous minor improvements, see CHANGES on the website
for details.

What is PyGUI?
--

PyGUI is a cross-platform GUI toolkit designed to be lightweight
and have a highly Pythonic API.

Acknowledgements


Thanks are due to Patrick Forget and Erez-Sh for getting me started on
the Windows implementation. Even though I didn't use much of their
code in the end, I appreciate their efforts and may draw more from it
in the future.

--
Gregory Ewing
[email protected]
http://www.cosc.canterbury.ac.nz/greg.ewing/
--
http://mail.python.org/mailman/listinfo/python-list


storing variable value

2009-04-11 Thread Daniel Dalton
Hi!

I'm writing a program to provide me with battery warnings when my
battery hits certain levels. It just checks the current level and does
something. I plan to call it from a a cron job. But If the cron runs
every minute, warnings every minute would be rather annoying. so is
there a way to make the script check if it has already ran before?
eg. can I write to a variable on one run of the program, and on the next
read that value that was written?

Thx!

Daniel.



signature.asc
Description: Digital signature
--
http://mail.python.org/mailman/listinfo/python-list


Re: Unsupported operand types in if/else list comprehension

2009-04-11 Thread Diez B. Roggisch

Mike H schrieb:

Thanks to all of you.

FYI, I'm doing this because I'm working on creating some insert
statements in SQL, where string values need to be quoted, and integer
values need to be unquoted.

I wanted to be sure that I could pass these values to the list in a
normal way e.g. ['test', 1, 'two'] and have a function correct the
list for me, rather than calling the function with a strangely quoted
list e.g. ['"'test'"', 1, '"'two'"'].> 


Don't do that yourself. This is error-prone. Instead, use the 
parametrized verison of the cursor.execute-method. It will perform the 
necessary escaping, and depending on the database and database adapter 
you use better performance.


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


Re: An object that creates (nested) attributes automatically on assignment

2009-04-11 Thread Steven D'Aprano
On Sat, 11 Apr 2009 03:01:48 -0700, Edd wrote:

> Yes I probably mean instance attributes. Forgive me, I am not
> particularly sure of the terminology. But your MyClass example, won't
> quite do what I want, as I'd like to be able to define instance
> attributes on top of instance attributes by assignment:
> 
 class MyClass(): pass
> ...
 instance = MyClass()
 instance.lasers.armed = True
> Traceback (most recent call last):
>   File "", line 1, in 
> AttributeError: MyClass instance has no attribute 'laser'

Ah, now it is more clear.

Okay, let's try this:


>>> class C(object):
... def __getattr__(self, name):
... # Only called if self.name doesn't exist.
... inst = self.__class__()
... setattr(self, name, inst)
... return inst
...
>>> c = C()
>>> c.x.y.z = 45
>>> c.__dict__
{'x': <__main__.C object at 0xb7c3b78c>}
>>> c.x.__dict__
{'y': <__main__.C object at 0xb7c3b7ec>}
>>> c.x.y.z
45



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


Re: storing variable value

2009-04-11 Thread Diez B. Roggisch

Daniel Dalton schrieb:

Hi!

I'm writing a program to provide me with battery warnings when my
battery hits certain levels. It just checks the current level and does
something. I plan to call it from a a cron job. But If the cron runs
every minute, warnings every minute would be rather annoying. so is
there a way to make the script check if it has already ran before?
eg. can I write to a variable on one run of the program, and on the next
read that value that was written?


Use a file to store previous run's state. Use e.g. the pickle-module to 
store some arbitrary data-structures to the disk.



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


Re: storing variable value

2009-04-11 Thread Steven D'Aprano
On Sat, 11 Apr 2009 21:36:43 +1000, Daniel Dalton wrote:

> Hi!
> 
> I'm writing a program to provide me with battery warnings when my
> battery hits certain levels. It just checks the current level and does
> something. I plan to call it from a a cron job. But If the cron runs
> every minute, warnings every minute would be rather annoying. so is
> there a way to make the script check if it has already ran before? eg.
> can I write to a variable on one run of the program, and on the next
> read that value that was written?

No, every time cron calls your script, it calls it as new. If you want 
persistent storage, you need to write to persistent storage like a file.

Another solution is to use a daemon which is running continuously in the 
background. Because that's a long-running process, the daemon can then 
decide how often it checks the battery, and how often it notifies you. 
However, writing correct daemons is not something for a beginner. I 
recommend you just stick to writing your persistent data to a file 
somewhere, and sticking to cron.



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


Multithreading / multiprocess

2009-04-11 Thread [email protected]
Is there anyway to begin a thread and execute a finite number of lines
of code, or a finite amount of time within it?

For example, say I create three child threads and I want to guarantee
equal timeshare between them, can I specify a quanta (say 400 LOC
although I know that is pretty small) to execute in each one in turn?

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


Re: An object that creates (nested) attributes automatically on assignment

2009-04-11 Thread Edd
On Apr 11, 12:54 pm, Steven D'Aprano  wrote:

> Ah, now it is more clear.
>
> Okay, let's try this:
>
> >>> class C(object):
>
> ...     def __getattr__(self, name):
> ...             # Only called if self.name doesn't exist.
> ...             inst = self.__class__()
> ...             setattr(self, name, inst)
> ...             return inst

Ha! Perfect! I knew it should be simpler. Thanks very much!

Kind regards,

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


Re: Multithreading / multiprocess

2009-04-11 Thread Diez B. Roggisch

[email protected] schrieb:

Is there anyway to begin a thread and execute a finite number of lines
of code, or a finite amount of time within it?

For example, say I create three child threads and I want to guarantee
equal timeshare between them, can I specify a quanta (say 400 LOC
although I know that is pretty small) to execute in each one in turn?


Not as such, no. You can play tricks with the trace-module, but these 
ultimately fail when the code in question runs inside C - which puts a 
stop to any python interpreter scheduling anyway, thus native threads 
are used which can't be controlled on that level.



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


Using percent signs with SafeConfigParser

2009-04-11 Thread Márcio Faustino
Hi,

Does the SafeConfigParser class correctly detects lone percent signs?
For example, shouldn't the string "100%%" be accepted as a valid
value? Executing the code below should only print one error, instead
it prints 2. (I've tested this with version 2.6.1 on Windows XP.)

It seems the "_badpercent_re" regular expression (an attribute of
SafeConfigParser, with value "%[^%]|%$") is flawed:
- The first alternative "%[^%]" fails with the string "%%_", because
it matches "%_".
- The second alternative "%$" fails with the string "%%", because it
matches "%".

The correct regular expression is "(?http://mail.python.org/mailman/listinfo/python-list


Re: communication between objects - help

2009-04-11 Thread Dave Angel



Murali kumar wrote:

thanks a lot..

I think passing the main object only by reference.. so, this does not causes
any overhead..

am i correct..?


On Fri, Apr 10, 2009 at 4:02 PM, Dave Angel  wrote:

  

Murali kumar wrote:



hi all..
I'm posted in a word doc becoz to add a image to explain my problem..
also I think gmail automatically scans for attachments..

anyway.. here's my problem...( see the image)

http://www.2shared.com/file/5299759/45e4c614/load.html

Using : Python 2.6 , wxPython 2.8.9


 * I don’t know how to return config file or data to mainApp object when
pressing load button. *


 * Is it easy to return filename only and load the file’s data into
mainApp
object in menu handler itself? *


 Please suggest me right direction.. and tell me how to do it?

Usually where I can get these informations.. (suggest links and books..)


Thanks for any advice



  

Most of the widget classes in your code should be derived classes, so they
can hold extra instance data & event handlers and such.  So that means you
can have extra parameters on the constructor, besides the ones the base
class requires.  Use one or more of those extra parameters to store your own
information about the hierarchy.

Simplest example is to add the app instance to each constructor.  Then each
widget object would know how to call back into the app to do some work, or
to load data into a common place.

DaveA

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



All objects are passed by reference in python.  And storing it in each widget 
costs very little.  Note that you can choose what object or objects should be 
used;  I just mention the app as a choice that everyone will subclass, and that 
will generally have direct or indirect access to all widgets.
  


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


multiprocessing and Locks

2009-04-11 Thread gvv
Hi All,

I am trying to understand multiprocessing, but I am getting a Runtime
error on the
code below. What am I missing or doing wrong?
Error is:
RuntimeError: Lock objects should only be shared between processes
through inheritance

I am using:
Python 2.6 (r26:66714, Nov 28 2008, 22:17:21)
[GCC 4.1.2 20071124 (Red Hat 4.1.2-42)] on linux2

Thanks in Advance,
George


import multiprocessing
import time

class Base(object):
def __init__(self, id, lock):
self.Id = id
lock.acquire()
self.Sleep()
lock.release()

def Run(self):
pass

def Sleep(self):
time.sleep(5)

class Derived(Base):
def __init__(self, id, lock):
Base.__init__(self, id, lock)

def Run(self):
print self.Id

def RunFunc(id, lock):
obj = Derived(id, lock)
obj.Run()

if __name__ == "__main__":
lock = multiprocessing.Lock()
Pool = multiprocessing.Pool(processes=5)
for i in xrange(100):
Pool.apply_async(func=RunFunc, args=(i,lock))
--
http://mail.python.org/mailman/listinfo/python-list


Re: safe eval of moderately simple math expressions

2009-04-11 Thread Paul McGuire
On Apr 11, 2:41 am, Aaron Brady  wrote:
>
> Why do I get the feeling that the authors of 'pyparsing' are out of
> breath?
>

What kind of breathlessness do you mean?  I'm still breathing, last
time I checked.

The-rumors-of-my-demise-have-been-greatly-exaggerated'ly yours,
-- Paul


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


[ANN] Falcon - powering innovation

2009-04-11 Thread Kless
If anybody is interesed in new technologies, you'll love this new
language called Falcon [1], which has been open sourced ago little
time.

Falcon is a scripting engine ready to empower mission-critical
multithreaded applications. It provides six integrated programming
paradigms: procedural, object oriented, prototype oriented,
functional, tabular and message oriented. You use what you prefer.

To know more about its design, read the interview to the Falcon author
that has published ComputerWorld Australia [2].


[1] http://www.falconpl.org/
[2] 
http://www.computerworld.com.au/article/298655/-z_programming_languages_falcon?fp=2&fpid=
--
http://mail.python.org/mailman/listinfo/python-list


Re: storing variable value

2009-04-11 Thread Dave Angel



Daniel Dalton wrote:

Hi!

I'm writing a program to provide me with battery warnings when my
battery hits certain levels. It just checks the current level and does
something. I plan to call it from a a cron job. But If the cron runs
every minute, warnings every minute would be rather annoying. so is
there a way to make the script check if it has already ran before?
eg. can I write to a variable on one run of the program, and on the next
read that value that was written?

Thx!

Daniel.

  
To put it simply, you want to store a value from one run of the script, 
that persists till the next run of the same script.


The most portable way to to that is to write a file.  And since all you 
care about is the time of writing, the file can be zero length.  So 
simply check for the file, determine it's creation time, and decide if 
it's too recent to want to run again.  If the file doesn't exist, or is 
"old", then write the file and notify the user.



It's still messy to leave this bogus file around, so think carefully 
about where to put it.  If the source directory is writable, it's 
reasonble to me to simply put it there.  That way, if the program gets 
deleted, it'll probably get deleted at the same time.  Failing that, you 
could put it in a TEMP directory.  And perhaps the sneakiest place to 
put it, if you're on Windows, is in the "run once on startup" 
directory.  Make it a do-nothing batch file, and Windows will remove it 
next time the user restarts the system.



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


Re: How to create a virtual serial port?

2009-04-11 Thread Grant Edwards
On 2009-04-10, Stuart Davenport  wrote:

> I'm on a OS X, python 2.5. Basically I will have a remote
> application pushing data (GPS) over the network to a python
> application I have running on my Mac, I want this python
> application to again push the data on to a "virtual serial
> port". Then the GPS program I have running on my MAC,
> RouteBuddy, can read the data from that serial port as
> standard.

You might be able to use a pty.  It depends on what ioctl calls
RouteBuddy uses.  Unless Apple has done major surgury on the
pty driver, the Unix pty driver only supports a subset of the
serial-port ioctl commands: for example pty's don't support the
TIOCMGET and TIOCMSET commands.

If RouteBuddy won't work with a pty, then you could buy two
USB-serial converters (that are supported on Mac OS X) and
connect them to each other with a null-modem adapter.

Or you could fix up the pty driver to support the reset of the
serial-port ioctl commands.  I've been meaning to do that for
Linux for several years now...

-- 
Grant

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


Merging byte arrays

2009-04-11 Thread Gabriel

Hello
I'm using this function to read data in byte format from file

def readBytes(file, offset, size):
   file.seek(offset)
   return file.read(size)

file is opened with open function:

file = open(path, "rb")

then i'm using array.array('B', bytes) to parse read-out  data, for 
example in this function:


def getIntValOfBytes(bytes):
   value = i = 0
   for byte in array.array('B', bytes):
   value |= byte << (8 * i)
   i += 1
  
   return value


when i read one portion of bytes everything works ok. but now i need 
read from more locations in file, then somehow merge these data together 
and then pass to getIntValOfBytes function..


i tried list

list = []
for offset in offsets
list.extend(readBytes(file, offset, size))
 
or string


string = ""
for offset in offsets
string += readBytes(file, offset, size)

but with list i get this error

   for byte in array.array('B', bytes):
TypeError: an integer is required

in getIntValOfBytes (i passed the part of the list to the method, list[x:y])

and with string it just freeze

I will appreciate any help..
[I'm newbie so sorry if i'm missing something obvious]
--
http://mail.python.org/mailman/listinfo/python-list


Definition of Pythonic?

2009-04-11 Thread Emmanuel Surleau
Hi there,

I'm starting an exploratory foray into Python, being generally dissatisfied 
with the Ruby ecosystem (while the language is wonderful, third party 
libraries and documentation are not).

Having written a few trivial scripts in Python, I'm curious as to how you 
would sum up the Pythonic philosophy of development. Judging from Python, it 
seems to exclude (mostly) "magical" variables like '$.'. Is this right? What 
else would you include in this definition?

Cheers,

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


Re: Why is it that *dbm modules don't provide an iterator? (Language design question)

2009-04-11 Thread Akira Kitada
Opened a ticket for this and attached a patch. (experimental)
http://bugs.python.org/issue5736

On Fri, Apr 10, 2009 at 8:39 AM, "Martin v. Löwis"  wrote:
 I assumed there were some decisions behind this, rather than it's just
 not implemented yet.
>>> I believe this assumption is wrong - it's really that no code has been
>>> contributed to do that.
>>
>> But doesn't the issue at http://bugs.python.org/issue662923 imply that
>> there *was* suitable code (for 2.4)?
>
> Yes, for bsddb.
>
>> So is this a regression?
>
> No. bsddb still supports iteration in 2.6.
>
>> And should that issue be re-opened?
>
> I don't think that would matter much. Actually contributing code might.
>
> Regards,
> Martin
> --
> http://mail.python.org/mailman/listinfo/python-list
>
--
http://mail.python.org/mailman/listinfo/python-list


completly unrelated to python but i had to post it

2009-04-11 Thread janus99
i know, i know , this group is about python, but this artilce i just
read, well i was pretty much just surfing the web when i found this
article made in 2001, it is just 2 funny.

http://www.adequacy.org/stories/2001.12.2.42056.2147.html\

if u've never read it, it'll knock ur socks off, and the comments are
riveting

ps. personally i think a hacker wrote it cause no person can be that
ignorant
--
http://mail.python.org/mailman/listinfo/python-list


Re: numpy.where

2009-04-11 Thread Lou Pecora
In article <[email protected]>,
 Peter Pearson  wrote:

> On Thu, 09 Apr 2009 09:09:18 -0400, Lou Pecora wrote:
> >
> > Really,  I've gotta RTFM. :-)
> 
> Hey, if you find TFM, please tell me where it is.  I haven't
> found anything Fine.  I even bought Travis Oliphant's book,
> which helps a little, but . . .

Travis' book is what I have and what I was thinking of when I made the 
statement.

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


llvm vs. parrot

2009-04-11 Thread Paul Watson
Is Parrot out of favor these days?  It appears that Google is going to
use llvm.

http://code.google.com/p/unladen-swallow/

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


Re: Definition of Pythonic?

2009-04-11 Thread Tim Chase
Having written a few trivial scripts in Python, I'm curious as to how you 
would sum up the Pythonic philosophy of development. Judging from Python, it 
seems to exclude (mostly) "magical" variables like '$.'. Is this right? What 
else would you include in this definition?


At the python command-prompt, issue

  import this

which will dump "The Zen of Python".  I'd debate "flat vs. 
nested" depending on context (nested IFs? nested call-graph? 
nested data-structures?), but otherwise this summarizes many of 
the reasons I esteem Python above all the other computer 
languages I've used (pascal and VB came close; ruby & perl were 
so distant from these ideals, I cringe every time I have to touch 
them for anything).


-tkc





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


Re: Definition of Pythonic?

2009-04-11 Thread skip

Emm> Having written a few trivial scripts in Python, I'm curious as to
Emm> how you would sum up the Pythonic philosophy of development.

Try

import this

at your friendly, neighborhood Python prompt.

-- 
Skip Montanaro - [email protected] - http://www.smontanaro.net/
"XML sucks, dictionaries rock" - Dave Beazley
--
http://mail.python.org/mailman/listinfo/python-list


Re: llvm vs. parrot

2009-04-11 Thread Carlos Ribeiro
On Fri, Apr 10, 2009 at 20:16, Paul Watson wrote:

> Is Parrot out of favor these days?  It appears that Google is going to
> use llvm.
>

As far as I can recall, Parrot was supposed to be an April Fools Day joke
(literally) that was taken way too seriously. Parrot may had made a lot of
sense years ago, but given today's compiler + language support + virtual
machine technology, it seems that it has fallen behind.
But that's just my $0.02 worth.
-- 
Carlos Ribeiro
Consultoria em Projetos
blog: http://rascunhosrotos.blogspot.com
blog: http://pythonnotes.blogspot.com
mail: [email protected]
mail: [email protected]
--
http://mail.python.org/mailman/listinfo/python-list


Re: Merging byte arrays

2009-04-11 Thread MRAB

Gabriel wrote:

Hello
I'm using this function to read data in byte format from file

def readBytes(file, offset, size):
   file.seek(offset)
   return file.read(size)

file is opened with open function:

file = open(path, "rb")

then i'm using array.array('B', bytes) to parse read-out  data, for 
example in this function:


def getIntValOfBytes(bytes):
   value = i = 0
   for byte in array.array('B', bytes):
   value |= byte << (8 * i)
   i += 1
 return value

when i read one portion of bytes everything works ok. but now i need 
read from more locations in file, then somehow merge these data together 
and then pass to getIntValOfBytes function..


i tried list

list = []
for offset in offsets
list.extend(readBytes(file, offset, size))
 
or string


string = ""
for offset in offsets
string += readBytes(file, offset, size)

but with list i get this error

   for byte in array.array('B', bytes):
TypeError: an integer is required

in getIntValOfBytes (i passed the part of the list to the method, 
list[x:y])



That's because you're passing a list of strings.

It should for a list of integers or a string.


and with string it just freeze


'Freezes'? I've tried it with a string of 256 characters and it was
fast.


I will appreciate any help..
[I'm newbie so sorry if i'm missing something obvious]



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


Startup with Python

2009-04-11 Thread Strato
Hello, I am a beginner in Python , i am not able to set the
environment variable in windows so that i can execute python script
through command prompt , and also i am not able to male *.py as
executable i.e. whenever i double click the file it should run it.
Please help and reply me at [email protected] . Its urgent brother ,
i will wait for your reply.
--
http://mail.python.org/mailman/listinfo/python-list


Re: Pathological regular expression

2009-04-11 Thread Steven D'Aprano
On Thu, 09 Apr 2009 02:56:00 -0700, David Liang wrote:

> Hi all,
> I'm having a weird problem with a regular expression (tested in 2.6 and
> 3.0):
> 
> Basically, any of these:
> _re_comments = re.compile(r'^(([^\\]+|\\.|"([^"\\]+|\\.)*")*)#.*$')
> _re_comments = re.compile(r'^(([^#]+|\\.|"([^"\\]+|\\.)*")*)#.*$')
> _re_comments = re.compile(r'^(([^"]+|\\.|"([^"\\]+|\\.)*")*)#.*$')
> 
> followed by for example,
> line = r'~/.[m]ozilla/firefox/*.default/chrome'
> print(_re_comments.sub(r'\1', line))
> 
> ...hangs the interpreter.


I can confirm the first one hangs the interpreter in Python 2.5 as well. 
I haven't tested the other two.

To my mind, this is a bug in the RE engine. Is there any reason to not 
treat it as a bug?


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


Re: How to create a virtual serial port?

2009-04-11 Thread Scott David Daniels

Stuart Davenport wrote:

On 11 Apr, 08:52, Scott David Daniels  wrote:

Stuart Davenport wrote:

... I'm on a OS X, python 2.5 Then the GPS program I have
running on my MAC, RouteBuddy, can read the data from that serial port
as standard.

I'ms confused by this statement.  What physical connector does your
"serial port" use to get the serial data to the Mac?  I only have one 
3-year old Mac laptop to look at, and I just don't see anything that I

would call a serial port.


Scott, many thanks for the advice

No problem.  I was short earlier because I wanted to spend time helping
someone out, not pulling clues out.  If I have an hour to answer some
questions, I'd rather read something that let's me hep out.  I figured
you might get a good answer if you asked something with full context,
and there were _way_ too many variables to get you an answer from what
you started with.

--Scott David Daniels
[email protected]
--
http://mail.python.org/mailman/listinfo/python-list


Writing a "Raw" Image to a File (Win, PIL)

2009-04-11 Thread W. eWatson

I have an image of described as:
Img Info:  {}
 size:  (640, 480)
 format:  None
 mode:  P
 palette:  
 bands:  ('P',)
 type:  

I'd like to write it to a file. Apparently, I need to convert it to a string 
first. How do I do that? Pickle?

--
   W. eWatson

 (121.015 Deg. W, 39.262 Deg. N) GMT-8 hr std. time)
  Obz Site:  39° 15' 7" N, 121° 2' 32" W, 2700 feet



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


Re: Unsupported operand types in if/else list comprehension

2009-04-11 Thread Mike H
Ok, thanks again to everyone for their suggestions, even if it appears
I was going down the wrong path at the start. I'm a grad student
creating this database to hold some of my own research on an isolated
server, so security, etc. isn't my biggest concern -- but I would like
to do this right. Here's the code that I've come up with now. Although
it's gotten away from the original question, those that have commented
on this seem to have some SQL knowledge, so I'd like to run it by them
to see if this is better in theory. (I've tried it and it works in
practice!)

FYI, I'm using MySQLdb to connect with the Database.

Also, I realize I should probably add in some try/catch statements and
other error handling... but this is what I have at the moment.



def insert_cmd(myTable, myFields, myValues, myReturnKey):
"""Imports given fields and values into a given table, returns an
SQL variable holding the Autoincrement key"""

#tests to see if myParentKey is valid in mySQL.
if not myReturnKey.startswith("@"): print "Error, myReturnKey must
start with '@'"; sys.exit()

SQLcmd="INSERT INTO " + myTable + " (%s) " % ", ".join(myFields)
SQLcmd=SQLcmd + "VALUES (%s,%s,%s);"
cursor.execute(SQLcmd, (myValues))

#sets and returns SQL variable.
SQLcmd="select " + myReturnKey + ":=last_insert_id();"
cursor.execute(SQLcmd)
return myReturnKey


On Sat, Apr 11, 2009 at 7:38 AM, Diez B. Roggisch  wrote:
> Mike H schrieb:
>>
>> Thanks to all of you.
>>
>> FYI, I'm doing this because I'm working on creating some insert
>> statements in SQL, where string values need to be quoted, and integer
>> values need to be unquoted.
>>
>> I wanted to be sure that I could pass these values to the list in a
>> normal way e.g. ['test', 1, 'two'] and have a function correct the
>> list for me, rather than calling the function with a strangely quoted
>> list e.g. ['"'test'"', 1, '"'two'"'].>
>
> Don't do that yourself. This is error-prone. Instead, use the parametrized
> verison of the cursor.execute-method. It will perform the necessary
> escaping, and depending on the database and database adapter you use better
> performance.
>
> Diez
> --
> http://mail.python.org/mailman/listinfo/python-list
>
--
http://mail.python.org/mailman/listinfo/python-list


Re: Pathological regular expression

2009-04-11 Thread MRAB

Steven D'Aprano wrote:

On Thu, 09 Apr 2009 02:56:00 -0700, David Liang wrote:


Hi all,
I'm having a weird problem with a regular expression (tested in 2.6 and
3.0):

Basically, any of these:
_re_comments = re.compile(r'^(([^\\]+|\\.|"([^"\\]+|\\.)*")*)#.*$')
_re_comments = re.compile(r'^(([^#]+|\\.|"([^"\\]+|\\.)*")*)#.*$')
_re_comments = re.compile(r'^(([^"]+|\\.|"([^"\\]+|\\.)*")*)#.*$')

followed by for example,
line = r'~/.[m]ozilla/firefox/*.default/chrome'
print(_re_comments.sub(r'\1', line))

...hangs the interpreter.



I can confirm the first one hangs the interpreter in Python 2.5 as well. 
I haven't tested the other two.


To my mind, this is a bug in the RE engine. Is there any reason to not 
treat it as a bug?



It's not a bug but one of those pathological cases where there are a LOT
of possibilities to try (we're talking about exponential growth here).

Hmm, maybe the re module needs to let the user set a timeout control
that raises an exception if it takes longer than n seconds (it can
already be interrupted by ^C)...
--
http://mail.python.org/mailman/listinfo/python-list


Re: Pathological regular expression

2009-04-11 Thread John Machin
On Apr 12, 1:07 am, Steven D'Aprano  wrote:
> On Thu, 09 Apr 2009 02:56:00 -0700, David Liang wrote:
> > Hi all,
> > I'm having a weird problem with a regular expression (tested in 2.6 and
> > 3.0):
>
> > Basically, any of these:
> > _re_comments = re.compile(r'^(([^\\]+|\\.|"([^"\\]+|\\.)*")*)#.*$')
> > _re_comments = re.compile(r'^(([^#]+|\\.|"([^"\\]+|\\.)*")*)#.*$')
> > _re_comments = re.compile(r'^(([^"]+|\\.|"([^"\\]+|\\.)*")*)#.*$')
>
> > followed by for example,
> > line = r'~/.[m]ozilla/firefox/*.default/chrome'
> > print(_re_comments.sub(r'\1', line))
>
> > ...hangs the interpreter.
>
> I can confirm the first one hangs the interpreter in Python 2.5 as well.
> I haven't tested the other two.
>
> To my mind, this is a bug in the RE engine. Is there any reason to not
> treat it as a bug?

IMHO it's not a bug -- s/hang/takes a long time to compute/

Just look at it: 2 + operators and 3 * operators ... It's one of those
"come back after lunch" REs.

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


Re: Writing a "Raw" Image to a File (Win, PIL)

2009-04-11 Thread Diez B. Roggisch

W. eWatson schrieb:

I have an image of described as:
Img Info:  {}
 size:  (640, 480)
 format:  None
 mode:  P
 palette:  
 bands:  ('P',)
 type:  

I'd like to write it to a file. Apparently, I need to convert it to a 
string first. How do I do that? Pickle?


Did you bother reading the PIL documentation just for about 30seconds?

http://www.pythonware.com/library/pil/handbook/image.htm

Hint: look for "save".

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


Re: Pathological regular expression

2009-04-11 Thread Dotan Cohen
> IMHO it's not a bug -- s/hang/takes a long time to compute/
>

‎That is quite what a hang is, and why the timeout was invented. The
real bug is that there is no timeout mechanism.

> Just look at it: 2 + operators and 3 * operators ... It's one of those
> "come back after lunch" REs.
>

Some users would not be able to tell that from the beginning. While a
timeout override would be necessary in these cases, a timeout should
be set.


-- 
Dotan Cohen

http://what-is-what.com
http://gibberish.co.il
--
http://mail.python.org/mailman/listinfo/python-list


Re: Definition of Pythonic?

2009-04-11 Thread Aahz
In article ,
Emmanuel Surleau   wrote:
>
>Having written a few trivial scripts in Python, I'm curious as to how you 
>would sum up the Pythonic philosophy of development. Judging from Python, it 
>seems to exclude (mostly) "magical" variables like '$.'. Is this right? What 
>else would you include in this definition?

python -m this
-- 
Aahz ([email protected])   <*> http://www.pythoncraft.com/

Why is this newsgroup different from all other newsgroups?
--
http://mail.python.org/mailman/listinfo/python-list


Re: Unsupported operand types in if/else list comprehension

2009-04-11 Thread Mike H
Well, I'm an idiot. Obviously, the line "VALUES (%s, %s, %s);" needs
to be modified to adapt for the number of arguments in the list. But
otherwise

On Sat, Apr 11, 2009 at 11:28 AM, Mike H  wrote:
> Ok, thanks again to everyone for their suggestions, even if it appears
> I was going down the wrong path at the start. I'm a grad student
> creating this database to hold some of my own research on an isolated
> server, so security, etc. isn't my biggest concern -- but I would like
> to do this right. Here's the code that I've come up with now. Although
> it's gotten away from the original question, those that have commented
> on this seem to have some SQL knowledge, so I'd like to run it by them
> to see if this is better in theory. (I've tried it and it works in
> practice!)
>
> FYI, I'm using MySQLdb to connect with the Database.
>
> Also, I realize I should probably add in some try/catch statements and
> other error handling... but this is what I have at the moment.
>
>
>
> def insert_cmd(myTable, myFields, myValues, myReturnKey):
>    """Imports given fields and values into a given table, returns an
> SQL variable holding the Autoincrement key"""
>
>    #tests to see if myParentKey is valid in mySQL.
>    if not myReturnKey.startswith("@"): print "Error, myReturnKey must
> start with '@'"; sys.exit()
>
>    SQLcmd="INSERT INTO " + myTable + " (%s) " % ", ".join(myFields)
>    SQLcmd=SQLcmd + "VALUES (%s,%s,%s);"
>    cursor.execute(SQLcmd, (myValues))
>
>    #sets and returns SQL variable.
>    SQLcmd="select " + myReturnKey + ":=last_insert_id();"
>    cursor.execute(SQLcmd)
>    return myReturnKey
>
>
> On Sat, Apr 11, 2009 at 7:38 AM, Diez B. Roggisch  wrote:
>> Mike H schrieb:
>>>
>>> Thanks to all of you.
>>>
>>> FYI, I'm doing this because I'm working on creating some insert
>>> statements in SQL, where string values need to be quoted, and integer
>>> values need to be unquoted.
>>>
>>> I wanted to be sure that I could pass these values to the list in a
>>> normal way e.g. ['test', 1, 'two'] and have a function correct the
>>> list for me, rather than calling the function with a strangely quoted
>>> list e.g. ['"'test'"', 1, '"'two'"'].>
>>
>> Don't do that yourself. This is error-prone. Instead, use the parametrized
>> verison of the cursor.execute-method. It will perform the necessary
>> escaping, and depending on the database and database adapter you use better
>> performance.
>>
>> Diez
>> --
>> http://mail.python.org/mailman/listinfo/python-list
>>
>
--
http://mail.python.org/mailman/listinfo/python-list


Re: Definition of Pythonic?

2009-04-11 Thread John Yeung
On Apr 11, 10:08 am, Emmanuel Surleau 
wrote:
> Having written a few trivial scripts in Python, I'm curious as
> to how you would sum up the Pythonic philosophy of development.

A couple of others have already mentioned the Zen of Python, available
at the Python command prompt.  I would agree with that, but also add
the caveat that none of the principles expressed there are hard-and-
fast rules.  Hopefully that is clear from the quasi-contradictory
nature of the principles, but inevitably there will be some people who
complain that Python breaks this or that "rule" from the Zen.

The fact is, it's impossible to satisfy every principle in every
situation.  To me, Python distinguishes itself for how well it
balances all of them.  "Compromise" is a word that comes up a lot when
talking about the design of Python.  To some, that has a negative
connotation; to me, it's an inevitable consequence of being practical.

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


Re: Startup with Python

2009-04-11 Thread Esmail

Strato wrote:

Hello, I am a beginner in Python , i am not able to set the
environment variable in windows so that i can execute python script
through command prompt , and also i am not able to male *.py as
executable i.e. whenever i double click the file it should run it.
Please help and reply me at [email protected] . Its urgent brother ,
i will wait for your reply.
--
http://mail.python.org/mailman/listinfo/python-list



Take a look at

http://docs.python.org/using/index.html

especially section 3 talks about how to setup Python on the Windows
platform.

Have you tried specifying the whole path in the command window?
For instance I have version 2.6 installed, so I can type

c:\Python26\python.exe script.py

to get this to work.

Good luck,

Esmail
ps: try renaming the .py extension to .pyw and then double click it
(should have worked before too - as far as I know this only prevents
 a command window from opening, but it can't hurt trying).
--
http://mail.python.org/mailman/listinfo/python-list


Re: Startup with Python

2009-04-11 Thread Emile van Sebille

Strato wrote:

Hello, I am a beginner in Python , i am not able to set the
environment variable in windows so that i can execute python script
through command prompt , and also i am not able to male *.py as
executable i.e. whenever i double click the file it should run it.


Download and install the activestate distribution -- here's the link
http://downloads.activestate.com/ActivePython/windows/2.6/ActivePython-2.6.1.1-win32-x86.msi

HTH,

Emile


Please help and reply me at [email protected] . Its urgent brother ,
i will wait for your reply.
--
http://mail.python.org/mailman/listinfo/python-list



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


Re: Definition of Pythonic?

2009-04-11 Thread MRAB

John Yeung wrote:

On Apr 11, 10:08 am, Emmanuel Surleau 
wrote:

Having written a few trivial scripts in Python, I'm curious as
to how you would sum up the Pythonic philosophy of development.


A couple of others have already mentioned the Zen of Python, available
at the Python command prompt.  I would agree with that, but also add
the caveat that none of the principles expressed there are hard-and-
fast rules.


"Special cases aren't special enough to break the rules.
Although practicality beats purity."

This also applies to the Zen itself.

> Hopefully that is clear from the quasi-contradictory

nature of the principles, but inevitably there will be some people who
complain that Python breaks this or that "rule" from the Zen.

The fact is, it's impossible to satisfy every principle in every
situation.  To me, Python distinguishes itself for how well it
balances all of them.  "Compromise" is a word that comes up a lot when
talking about the design of Python.  To some, that has a negative
connotation; to me, it's an inevitable consequence of being practical.


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


Re: Writing a "Raw" Image to a File (Win, PIL)

2009-04-11 Thread W. eWatson

Diez B. Roggisch wrote:

W. eWatson schrieb:

I have an image of described as:
Img Info:  {}
 size:  (640, 480)
 format:  None
 mode:  P
 palette:  
 bands:  ('P',)
 type:  

I'd like to write it to a file. Apparently, I need to convert it to a 
string first. How do I do that? Pickle?


Did you bother reading the PIL documentation just for about 30seconds?

http://www.pythonware.com/library/pil/handbook/image.htm

Hint: look for "save".

Diez
So, you are telling me what? To save it as a jpg file, or maybe a bmp file? 
All of them have headers, right?


--
   W. eWatson

 (121.015 Deg. W, 39.262 Deg. N) GMT-8 hr std. time)
  Obz Site:  39° 15' 7" N, 121° 2' 32" W, 2700 feet

Web Page: 

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


Re: Pathological regular expression

2009-04-11 Thread MRAB

Dotan Cohen wrote:

IMHO it's not a bug -- s/hang/takes a long time to compute/



‎That is quite what a hang is, and why the timeout was invented. The
real bug is that there is no timeout mechanism.


I wouldn't call it a "hang" because it is actually doing work. If it was
'stuck' on a certain part of the text and not progressing then it would
be a hang.


Just look at it: 2 + operators and 3 * operators ... It's one of those
"come back after lunch" REs.



Some users would not be able to tell that from the beginning. While a
timeout override would be necessary in these cases, a timeout should
be set.


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


Re: safe eval of moderately simple math expressions

2009-04-11 Thread Aaron Brady
On Apr 11, 8:09 am, Paul McGuire  wrote:
> On Apr 11, 2:41 am, Aaron Brady  wrote:
>
>
>
> > Why do I get the feeling that the authors of 'pyparsing' are out of
> > breath?
>
> What kind of breathlessness do you mean?  I'm still breathing, last
> time I checked.
>
> The-rumors-of-my-demise-have-been-greatly-exaggerated'ly yours,
> -- Paul

Gasping, not panting.
--
http://mail.python.org/mailman/listinfo/python-list


Re: Can't create list of dictionaries

2009-04-11 Thread Gabriel Genellina

On Apr 10, 12:36 pm, sophie_newbie  wrote:


I've got a function that returns a dictionary, I need to loop and
return 1000 dictionaries and append them to a list, but the thing is
that when I do the list.append(funtThatReturnsDict()) the resulting
only ever has 1 dictionary attached to it, even after running the
append function 1000 times!

I've tried using dict.copy() on the dictionary that was returned from
the function but this didn't work either.


En Fri, 10 Apr 2009 08:49:12 -0300, sophie_newbie   
escribió:



Scratch everything I said, copy() does work. Made a wee mistake
somewhere else.


If the function is supposed to return a *different* dictionary each time,  
ensure that, don't "fix" the result value after it has returned.


--
Gabriel Genellina

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


Re: ANN: PyGUI 2.0

2009-04-11 Thread member thudfoo
Line 25 of setup.py should be:

packages.append("GUI.Gtk")
--
http://mail.python.org/mailman/listinfo/python-list


Re: Writing a "Raw" Image to a File (Win, PIL)

2009-04-11 Thread MRAB

W. eWatson wrote:

I have an image of described as:
Img Info:  {}
 size:  (640, 480)
 format:  None
 mode:  P
 palette:  
 bands:  ('P',)
 type:  

I'd like to write it to a file. Apparently, I need to convert it to a 
string first. How do I do that? Pickle?


Have you tried the .tostring() method?
--
http://mail.python.org/mailman/listinfo/python-list


Re: Async serial communication/threads sharing data

2009-04-11 Thread Jean-Paul Calderone

On Wed, 25 Mar 2009 22:23:25 -0700, John Nagle  wrote:

Jean-Paul Calderone wrote:

On Tue, 24 Mar 2009 22:20:49 -0700, John Nagle  wrote:

Jean-Paul Calderone wrote:
On Mon, 23 Mar 2009 05:30:04 -0500, Nick Craig-Wood  
wrote:

Jean-Paul Calderone  wrote:
[snip]

   After bringing in all the heavy machinery of Twisted,
you're still polling at 10Hz.  That's disappointing.


Hmm, no?  There's no polling in the latest version of the code I saw.

Jean-Paul


if too_full:
   reactor.callLater(0.1, self.throttle)



Hi John,

You're looking at a different version of the code than what I was talking
about.  After discussing things with Nick, we came up with a version which
includes no polling.  See Nick's message of March 23, at around 1:30 PM for
the code I was referring to - a version of the app which does no polling.

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


Re: Pathological regular expression

2009-04-11 Thread Aaron Brady
On Apr 11, 10:07 am, Steven D'Aprano  wrote:
> On Thu, 09 Apr 2009 02:56:00 -0700, David Liang wrote:
> > Hi all,
> > I'm having a weird problem with a regular expression (tested in 2.6 and
> > 3.0):
>
> > Basically, any of these:
> > _re_comments = re.compile(r'^(([^\\]+|\\.|"([^"\\]+|\\.)*")*)#.*$')
> > _re_comments = re.compile(r'^(([^#]+|\\.|"([^"\\]+|\\.)*")*)#.*$')
> > _re_comments = re.compile(r'^(([^"]+|\\.|"([^"\\]+|\\.)*")*)#.*$')
>
> > followed by for example,
> > line = r'~/.[m]ozilla/firefox/*.default/chrome'
> > print(_re_comments.sub(r'\1', line))
>
> > ...hangs the interpreter.
>
> I can confirm the first one hangs the interpreter in Python 2.5 as well.
> I haven't tested the other two.
>
> To my mind, this is a bug in the RE engine. Is there any reason to not
> treat it as a bug?
>
> --
> Steven

How long does it take on a short example?
--
http://mail.python.org/mailman/listinfo/python-list


Re: Writing a "Raw" Image to a File (Win, PIL)

2009-04-11 Thread W. eWatson

MRAB wrote:

W. eWatson wrote:

I have an image of described as:
Img Info:  {}
 size:  (640, 480)
 format:  None
 mode:  P
 palette:  
 bands:  ('P',)
 type:  

I'd like to write it to a file. Apparently, I need to convert it to a 
string first. How do I do that? Pickle?


Have you tried the .tostring() method?
Perfect. Works exactly like I had hoped, 640x480 bytes--nothing more. 
Thanks. I had noticed it, but didn't get the connection. Then I wandered 
around nearby in the PIL description, and noticed something about decoding 
and write. Unknown territory to me. However, if I had followed up there was 
a connection between tostring and "raw" decoding.


--
   W. eWatson

 (121.015 Deg. W, 39.262 Deg. N) GMT-8 hr std. time)
  Obz Site:  39° 15' 7" N, 121° 2' 32" W, 2700 feet

Web Page: 

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


Re: Multithreading / multiprocess

2009-04-11 Thread Aahz
In article <57065c62-2024-47b5-a07e-1d60ff85b...@y10g2000prc.googlegroups.com>,
[email protected]  wrote:
>
>Is there anyway to begin a thread and execute a finite number of lines
>of code, or a finite amount of time within it?
>
>For example, say I create three child threads and I want to guarantee
>equal timeshare between them, can I specify a quanta (say 400 LOC
>although I know that is pretty small) to execute in each one in turn?

You have extremely coarse-grained control with sys.setcheckinterval().
However, there is no guarantee which thread will pick up control after
each context switch, so one thread might get more than its share.
-- 
Aahz ([email protected])   <*> http://www.pythoncraft.com/

Why is this newsgroup different from all other newsgroups?
--
http://mail.python.org/mailman/listinfo/python-list


Re: How to create a virtual serial port?

2009-04-11 Thread Grant Edwards
On 2009-04-11, Scott David Daniels  wrote:

 ... I'm on a OS X, python 2.5 Then the GPS program I have
 running on my MAC, RouteBuddy, can read the data from that
 serial port as standard.

> I'ms confused by this statement.  What physical connector does
> your "serial port" use to get the serial data to the Mac? 

The data comes in on the TCP/IP network.

The OP wants to send that data to a program called RouteBuddy.

RouteBuddy will only read data from a serial port.

The phrase "that serial port" in the text which you quote
refers to the virtual serial port that the elided text says the
OP wants to create.

The OP wants what is called a "serial port redirector" in
Windows-speak.  It's a program that routes data between a
network connection (often a TCP/IP connection that implements
RFC2217) and what appears to other programs to be a normal
serial port device API.

You can write a port redirector in user-space in MS-Windows,
but you can't in Linux/Unix.  On Unix systems you have to write
a kernel module that sits below the tty layer.  The tty layer
is what user applications talk to with open/close/read/write/ioctl
calls.

-- 
Grant

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


Re: sharing/swapping items between lists

2009-04-11 Thread Aahz
In article <4fd78ac3-ba83-456b-b768-3a0043548...@f19g2000vbf.googlegroups.com>,
Ross   wrote:
>
>I'm trying to design an iterator that produces two lists. The first
>list will be a list of unique pairings and the second will be a list
>of items that weren't used in the first list. After each round, the
>items that weren't used in the round before will get put back in and
>the second list will be populated with unique items. 

How do you specify what goes into the first list?  Based on your
description, I would have expected that the output from the first
iteration would be

( [(1,2),(3,4),(5,6)], [7,8] )

Regardless of the actual algorithm, if you are returning items one at a
time and maintaining state in a computation, you probably want to use a
generator.
-- 
Aahz ([email protected])   <*> http://www.pythoncraft.com/

Why is this newsgroup different from all other newsgroups?
--
http://mail.python.org/mailman/listinfo/python-list


Re: Pathological regular expression

2009-04-11 Thread Steven D'Aprano
On Sat, 11 Apr 2009 08:40:03 -0700, John Machin wrote:

>> To my mind, this is a bug in the RE engine. Is there any reason to not
>> treat it as a bug?
> 
> IMHO it's not a bug -- s/hang/takes a long time to compute/
> 
> Just look at it: 2 + operators and 3 * operators ... It's one of those
> "come back after lunch" REs.

Well, it's been running now for about two and a half hours, that's a 
rather long lunch. And despite MRAB's assertion, it *cannot* be 
interrupted by ctrl-C. That means that to all intents and purposes, the 
interpreter has locked up for the duration of the calculation, which may 
be days or weeks for all I know.



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


Re: Reading 3 objects at a time from list

2009-04-11 Thread Aahz
In article <[email protected]>,
Francesco Bochicchio   wrote:
>> On Sat, Apr 11, 2009 at 1:44 AM, Matteo  wrote:
>>>
>>> I need to pass the numbers to a function, but three at a time, until
>>> the string ends. The strings are of variable length, but always a
>>> multiple of three.
>
>I would do that with a generator:
>
> >>> def groups(l,n) :
>...   while l: yield l[:n]; l=l[n:]
>...

Unfortunately, that's O(N**2) albeit with an extremely small constant
factor, because popping off the head of the list requires a full list
copy.  You're probably okay with this algorithm unless the string could
be megabytes.
-- 
Aahz ([email protected])   <*> http://www.pythoncraft.com/

Why is this newsgroup different from all other newsgroups?
--
http://mail.python.org/mailman/listinfo/python-list


Re: Pathological regular expression

2009-04-11 Thread Dotan Cohen
> Well, it's been running now for about two and a half hours, that's a
> rather long lunch.

I'd also like a pony!

-- 
Dotan Cohen

http://what-is-what.com
http://gibberish.co.il
--
http://mail.python.org/mailman/listinfo/python-list


Re: How to create a virtual serial port?

2009-04-11 Thread Grant Edwards
On 2009-04-11, Grant Edwards  wrote:

> You can write a port redirector in user-space in MS-Windows,
> but you can't in Linux/Unix.  On Unix systems you have to
> write a kernel module that sits below the tty layer.

Perhaps I should elucidate further.

That's what the "pty" driver on Unix is: a kernel module that
sits underneath the tty layer where the "normal" serial-port
UART drivers sit.

However, Unix pty drivers only handles a subset of the normal
serial-port API.  [I'm not sure why pty drivers have never been
"finished" so that they fully emulate a serial port, but it's
been that way for 20+ years].

If RouteBuddy doesn't try to do things like get/set modem
control/status lines, then the OP might be able to use a pty.

Each pty consists of two devices: a master end and a slave end.
RouteBuddy would be told to use the slave end, and the OP would
write a program that would transfer data between a network
connection and the master end.

A pty devices is what is used by programs like xterm to run
programs like bash.  Xterm transfers data between a network
connection and the master end of a pty.  Bash is connected to
the slave end of that pty and "thinks" it's connected to a
serial port.  Bash uses little of the serial port API, so it's
happy with the limited API provided by a pty slave-end.

-- 
Grant

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


Re: Definition of Pythonic?

2009-04-11 Thread Emmanuel Surleau
On Saturday 11 April 2009 18:00:58 John Yeung wrote:
> On Apr 11, 10:08 am, Emmanuel Surleau 
>
> wrote:
> > Having written a few trivial scripts in Python, I'm curious as
> > to how you would sum up the Pythonic philosophy of development.
>
> A couple of others have already mentioned the Zen of Python, available
> at the Python command prompt.  I would agree with that, but also add
> the caveat that none of the principles expressed there are hard-and-
> fast rules.  Hopefully that is clear from the quasi-contradictory
> nature of the principles, but inevitably there will be some people who
> complain that Python breaks this or that "rule" from the Zen.

Thank you all for the nice and prompt replies to what is a typical newbie 
question, which is bound to come back regularly. And no, you can't make 
everybody happy, per definition.

> The fact is, it's impossible to satisfy every principle in every
> situation.  To me, Python distinguishes itself for how well it
> balances all of them.  "Compromise" is a word that comes up a lot when
> talking about the design of Python.  To some, that has a negative
> connotation; to me, it's an inevitable consequence of being practical.


That's fine with me: after all, you can't do software engineering without 
trade-offs.

Cheers,

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


design question, metaclasses?

2009-04-11 Thread Darren Dale
I am working on a project that provides a high level interface to hdf5
files by implementing a thin wrapper around h5py. I would like to
generalize the project so the same API can be used with other formats,
like netcdf or ascii files. The format specific code exists in File,
Group and Dataset classes, which I could reimplement for each format.
But there are other classes deriving from Group and Dataset which do
not contain any format-specific code, and I would like to find a way
to implement the functionality once and apply uniformly across
supported formats. This is really abstract, but I was thinking of
something along the lines of:

format1.Group # implementation of group in format1
format2.Group # ...
Base.DerivedGroup # base implementation of DerivedGroup, not directly
useful
format1.DerivedGroup = Base.DerivedGroup(format1.Group) # useful
format2.DerivedGroup = Base.DerivedGroup(format2.Group) # useful

Could anyone please offer a comment, is this an appropriate use of
metaclassing, or is there maybe an easier/better alternative?
--
http://mail.python.org/mailman/listinfo/python-list


Re: Pathological regular expression

2009-04-11 Thread Aaron Brady
On Apr 11, 12:40 pm, Steven D'Aprano  wrote:
> On Sat, 11 Apr 2009 08:40:03 -0700, John Machin wrote:
> >> To my mind, this is a bug in the RE engine. Is there any reason to not
> >> treat it as a bug?
>
> > IMHO it's not a bug -- s/hang/takes a long time to compute/
>
> > Just look at it: 2 + operators and 3 * operators ... It's one of those
> > "come back after lunch" REs.
>
> Well, it's been running now for about two and a half hours, that's a
> rather long lunch. And despite MRAB's assertion, it *cannot* be
> interrupted by ctrl-C. That means that to all intents and purposes, the
> interpreter has locked up for the duration of the calculation, which may
> be days or weeks for all I know.

While beyond the limits of a long lunch, that would make a nice
vacation?
--
http://mail.python.org/mailman/listinfo/python-list


Re: How to create a virtual serial port?

2009-04-11 Thread Scott David Daniels

Grant Edwards wrote:

On 2009-04-11, Grant Edwards  wrote:


You can write a port redirector in user-space in MS-Windows,
but you can't in Linux/Unix.  On Unix systems you have to
write a kernel module that sits below the tty layer.


Perhaps I should elucidate further.


This part I actually understand.  The OP has a program named
"RouteBuddy" that talks to a device over a serial port, and he
want to repalce the data stream coming from that device.  My
question is, "where does the device that he wants to replace
plug in?"  I don't see anywhere on my laptop I could plug in
anything but a USB connector, an ethernet connector, a firewire
connector, headphones, speakers, a display connector, or a
power cord.

--Scott David Daniels
[email protected]
--
http://mail.python.org/mailman/listinfo/python-list


Regex similar to "^(?u)\w$", but without digits?

2009-04-11 Thread Andreas
Hello,

I'd like to create a regex that captures any unicode character, but
not the underscore and the digits 0-9. "^(?u)\w$" captures them also.
Is there a possibility to restrict an expression like "\w" to "\w
without [0-9_]"?
I'm using python 2.5.4

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


Re: Unsupported operand types in if/else list comprehension

2009-04-11 Thread Mike H
Sigh. One more. And again, thank you for all of the help.

I realized that the last version that I posted took care of an SQL
injection problem for the values, but not for the fields. So, I went
ahead and modified the code:

def new_insert_cmd(myTable, myFields, myValues):
"""Imports given fields and values into a given table, returns the
Autoincrement value."""
SQLcmd="INSERT INTO " + myTable + " ( " +
create_input_string(myFields) + " ) VALUES ( " \
+ create_input_string(myValues) +" );"
allArguments=myFields+myValues
cursor.execute(SQLcmd, (allArguments))

create_input_strings() is just a function that creates the necessary
number of %s's for a given list (and which I'm sure there's a faster
way to code):

def create_input_string(myList):
sOut=""
for var in myList:
sOut=sOut+"%s, "
return sOut[:-2]

However, now the cursor.execute statement won't work. I've looked at
the content of SQLcmd and the values of allArguments and they seem
fine.

I've even tried running this at the IDLE command line:

cursor.execute("INSERT INTO plan (%s, %s, %s) VALUES (%s, %s, %s);",
(["name", "fileno", "size", "Test", "AAA-000", 7])) and I get this
error:

File "C:\Python25\lib\site-packages\MySQLdb\cursors.py", line 166, in execute
self.errorhandler(self, exc, value)
  File "C:\Python25\lib\site-packages\MySQLdb\connections.py", line
35, in defaulterrorhandler
raise errorclass, errorvalue
ProgrammingError: (1064, "You have an error in your SQL syntax; check
the manual that corresponds to your MySQL server version for the right
syntax to use near ''name', 'fileno', 'size') VALUES ('Test',
'AAA-000', 7)' at line 1")

Can I not use the cursor.execute command to pass variables that aren't
immediately next to each other? If so, is there a better way to go
about solving this problem?

Again, thanks for your patience and help with a newbie.

Michael


On Sat, Apr 11, 2009 at 11:53 AM, Mike H  wrote:
> Well, I'm an idiot. Obviously, the line "VALUES (%s, %s, %s);" needs
> to be modified to adapt for the number of arguments in the list. But
> otherwise
>
> On Sat, Apr 11, 2009 at 11:28 AM, Mike H  wrote:
>> Ok, thanks again to everyone for their suggestions, even if it appears
>> I was going down the wrong path at the start. I'm a grad student
>> creating this database to hold some of my own research on an isolated
>> server, so security, etc. isn't my biggest concern -- but I would like
>> to do this right. Here's the code that I've come up with now. Although
>> it's gotten away from the original question, those that have commented
>> on this seem to have some SQL knowledge, so I'd like to run it by them
>> to see if this is better in theory. (I've tried it and it works in
>> practice!)
>>
>> FYI, I'm using MySQLdb to connect with the Database.
>>
>> Also, I realize I should probably add in some try/catch statements and
>> other error handling... but this is what I have at the moment.
>>
>>
>>
>> def insert_cmd(myTable, myFields, myValues, myReturnKey):
>>    """Imports given fields and values into a given table, returns an
>> SQL variable holding the Autoincrement key"""
>>
>>    #tests to see if myParentKey is valid in mySQL.
>>    if not myReturnKey.startswith("@"): print "Error, myReturnKey must
>> start with '@'"; sys.exit()
>>
>>    SQLcmd="INSERT INTO " + myTable + " (%s) " % ", ".join(myFields)
>>    SQLcmd=SQLcmd + "VALUES (%s,%s,%s);"
>>    cursor.execute(SQLcmd, (myValues))
>>
>>    #sets and returns SQL variable.
>>    SQLcmd="select " + myReturnKey + ":=last_insert_id();"
>>    cursor.execute(SQLcmd)
>>    return myReturnKey
>>
>>
>> On Sat, Apr 11, 2009 at 7:38 AM, Diez B. Roggisch  
>> wrote:
>>> Mike H schrieb:

 Thanks to all of you.

 FYI, I'm doing this because I'm working on creating some insert
 statements in SQL, where string values need to be quoted, and integer
 values need to be unquoted.

 I wanted to be sure that I could pass these values to the list in a
 normal way e.g. ['test', 1, 'two'] and have a function correct the
 list for me, rather than calling the function with a strangely quoted
 list e.g. ['"'test'"', 1, '"'two'"'].>
>>>
>>> Don't do that yourself. This is error-prone. Instead, use the parametrized
>>> verison of the cursor.execute-method. It will perform the necessary
>>> escaping, and depending on the database and database adapter you use better
>>> performance.
>>>
>>> Diez
>>> --
>>> http://mail.python.org/mailman/listinfo/python-list
>>>
>>
>
--
http://mail.python.org/mailman/listinfo/python-list


Re: Definition of Pythonic?

2009-04-11 Thread Mark Wooding
John Yeung  writes:

> A couple of others have already mentioned the Zen of Python, available
> at the Python command prompt.  I would agree with that, but also add
> the caveat that none of the principles expressed there are hard-and-
> fast rules.

Indeed, I'd suggest that the very lack of hard-and-fast rules is typical
of the Python approach.

> The fact is, it's impossible to satisfy every principle in every
> situation.  To me, Python distinguishes itself for how well it
> balances all of them.  "Compromise" is a word that comes up a lot when
> talking about the design of Python.  To some, that has a negative
> connotation; to me, it's an inevitable consequence of being practical.

Agreed.  Then again, all language design is a compromise, between
factors like runtime efficiency, simplicity of implementation,
tractability of semantics (for various classes of users), supportability
by tools, and expressive power.  Python inhabits what seems to me to be
a particularly sweet spot on this rather complex landscape.

-- [mdw]
--
http://mail.python.org/mailman/listinfo/python-list


Re: How to create a virtual serial port?

2009-04-11 Thread Ned Deily
In article <[email protected]>,
 Scott David Daniels  wrote:
> This part I actually understand.  The OP has a program named
> "RouteBuddy" that talks to a device over a serial port, and he
> want to repalce the data stream coming from that device.  My
> question is, "where does the device that he wants to replace
> plug in?"  I don't see anywhere on my laptop I could plug in
> anything but a USB connector, an ethernet connector, a firewire
> connector, headphones, speakers, a display connector, or a
> power cord.

A number of vendors (Keyspan, Belkin) make USB serial ports.  FWIW, I 
use one here on this iMac and OS X with screen(1) and a null modem cable 
to act as a serial console for a headless Linux box.

-- 
 Ned Deily,
 [email protected]

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


Re: Using percent signs with SafeConfigParser

2009-04-11 Thread Peter Otten
Márcio Faustino wrote:

> Does the SafeConfigParser class correctly detects lone percent signs?
> For example, shouldn't the string "100%%" be accepted as a valid
> value? Executing the code below should only print one error, instead
> it prints 2. (I've tested this with version 2.6.1 on Windows XP.)
> 
> It seems the "_badpercent_re" regular expression (an attribute of
> SafeConfigParser, with value "%[^%]|%$") is flawed:
> - The first alternative "%[^%]" fails with the string "%%_", because
> it matches "%_".
> - The second alternative "%$" fails with the string "%%", because it
> matches "%".

I think you are right. Please file a bug report .

> The correct regular expression is "(? previous tests.

IMO this doesn't fix the problem because 

(1) it allows "%%%" which is also incorrect
(2) _interpvar_re has already butchered values like "%%(alpha)s" to "%"

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


Re: Reading 3 objects at a time from list

2009-04-11 Thread Vito De Tullio
Matteo wrote:

> it works and I like slices, but I was wondering if there was another
> way of doing the same thing, maybe reading the numbers in groups of
> arbitrary length n...

from http://docs.python.org/library/itertools.html#recipes

def grouper(n, iterable, fillvalue=None):
"grouper(3, 'ABCDEFG', 'x') --> ABC DEF Gxx"
args = [iter(iterable)] * n
return izip_longest(fillvalue=fillvalue, *args)


and it's *damned* fast!


-- 
By ZeD

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


Re: How to create a virtual serial port?

2009-04-11 Thread Grant Edwards
On 2009-04-11, Scott David Daniels  wrote:
> Grant Edwards wrote:
>> On 2009-04-11, Grant Edwards  wrote:
>> 
>>> You can write a port redirector in user-space in MS-Windows,
>>> but you can't in Linux/Unix.  On Unix systems you have to
>>> write a kernel module that sits below the tty layer.
>> 
>> Perhaps I should elucidate further.
>
> This part I actually understand.  The OP has a program named
> "RouteBuddy" that talks to a device over a serial port, and he
> want to repalce the data stream coming from that device.  My
> question is, "where does the device that he wants to replace
> plug in?"

To some other machine/device on the network that does have a
serial port.  You can buy dedicated devices that do
serial-ethernet, or you can use any old computer that does
happen to have a serial port.

If he was going to plug the device into a real serial port on
the machine inquestion, then he wouldn't need a virtual serial
port.

> I don't see anywhere on my laptop I could plug in anything but
> a USB connector, an ethernet connector, a firewire connector,
> headphones, speakers, a display connector, or a power cord.

He's not trying to plug the device into your laptop.

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


Re: How to create a virtual serial port?

2009-04-11 Thread Grant Edwards
On 2009-04-11, Ned Deily  wrote:
> In article <[email protected]>,
>  Scott David Daniels  wrote:
>> This part I actually understand.  The OP has a program named
>> "RouteBuddy" that talks to a device over a serial port, and he
>> want to repalce the data stream coming from that device.  My
>> question is, "where does the device that he wants to replace
>> plug in?"  I don't see anywhere on my laptop I could plug in
>> anything but a USB connector, an ethernet connector, a firewire
>> connector, headphones, speakers, a display connector, or a
>> power cord.
>
> A number of vendors (Keyspan, Belkin) make USB serial ports.  FWIW, I 
> use one here on this iMac and OS X with screen(1) and a null modem cable 
> to act as a serial console for a headless Linux box.

True, but that doesn't help the OP, where the data is coming
into the OS-X machine via a network connection.

-- 
Grant

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


Re: How to create a virtual serial port?

2009-04-11 Thread alex goretoy
A number of vendors (Keyspan, Belkin) make USB serial ports.  FWIW, I
use one here on this iMac and OS X with screen(1) and a null modem cable
to act as a serial console for a headless Linux box.

+1

-Alex Goretoy
http://www.goretoy.com

Norman Mailer
- "Writing books is the closest men ever come to childbearing."
--
http://mail.python.org/mailman/listinfo/python-list


Q:Pythonic way to create list of lists

2009-04-11 Thread grkuntzmd
I am just learning Python.

I am trying to create a list of empty lists: [[], [], [], ...] (10
items total).

What is the most Pythonic way to do this?

If I use a list comprehension (as in myList = [[] for item in xrange
(0, 10)]), Netbeans warns me that 'item' is never used.

If I use a for-loop (as in for item in myList = []; for item in xrange
(0, 10): myList.append([])), Netbeans still warns me of the same
thing.

If I use '*' (as myList = [[]] * 10), all of the empty lists refer to
the same object; changing one changes them all.

Do I have to live with the warning, or is there a "better" way?

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


absolute newbie: divide a list into sublists (nested lists?) of fixed length

2009-04-11 Thread ergconcepts
Hi,
I have a list looking like

[ 0.84971586,  0.05786009,  0.9645675,  0.84971586,  0.05786009,
0.9645675, 0.84971586,  0.05786009,  0.9645675,  0.84971586,
0.05786009,  0.9645675]

and I would like to break this list into subsets of fixed length (say,
three elements), i.e. to convert the list into a form such as the one
generated by the following example code which I have found:

>>>import numpy
>>>s = numpy.random.random((3,3))
>>>s
array([[ 0.11916176,  0.96409475,  0.72602155],
   [ 0.84971586,  0.05786009,  0.96456754],
   [ 0.81617437,  0.845342  ,  0.09109779]])

How can I create such a 2d array (i.e., something like a symmetric
matrix) from my data?

Thanks in advance,

Bernard

PS: Note that the numpy import is not important here, it is just the
structure of the data that matters..
--
http://mail.python.org/mailman/listinfo/python-list


Re: Unsupported operand types in if/else list comprehension

2009-04-11 Thread George Sakkis
On Apr 11, 3:03 pm, Mike H  wrote:

> Can I not use the cursor.execute command to pass variables that aren't
> immediately next to each other? If so, is there a better way to go
> about solving this problem?

Yes, there is. Use one of the several production quality python SQL
toolkits built for exactly this purpose instead of putting together an
ad-hoc, informally-specified bug-ridden slow implementation of half of
their feature set.

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


Re: Q:Pythonic way to create list of lists

2009-04-11 Thread Tim Chase

I am trying to create a list of empty lists: [[], [], [], ...] (10
items total).

What is the most Pythonic way to do this?

If I use a list comprehension (as in myList = [[] for item in xrange
(0, 10)]), Netbeans warns me that 'item' is never used.


Not using Netbeans, I can't verify that the below will stop the 
warning.  However the variable name "_" is used conventionally to 
mean "this doesn't matter".  Additionally, the starting point for 
xrange defaults to 0, so you can omit it.  That would translate to


 my_list = [[] for _ in xrange(10)]

-tkc


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


  1   2   >