Re: remote multiprocessing, shared object

2010-04-08 Thread Norm Matloff
Thanks very much, Kushal.

But it seems to me that it doesn't quite work.  After your first client
below creates l and calls append() on it, it would seem that one could
not then assign to it, e.g. do

   l[1] = 8

What I'd like is to write remote multiprocessing code just like threads
code (or for that matter, just like shared-memory multiprocessing code),
i.e. reading and writing shared globals.  Is this even possible?

Norm

On 2010-04-08, Kushal Kumaran kushal.kumaran+pyt...@gmail.com wrote:
 On Thu, Apr 8, 2010 at 3:04 AM, Norm Matloff matl...@doe.com wrote:
 Should be a simple question, but I can't seem to make it work from my
 understanding of the docs.

 I want to use the multiprocessing module with remote clients, accessing
 shared lists.  I gather one is supposed to use register(), but I don't
 see exactly how.  I'd like to have the clients read and write the shared
 list directly, not via some kind of get() and set() functions.  It's
 clear how to do this in a shared-memory setting, but how can one do it
 across a network, i.e. with serve_forever(), connect() etc.?

 Any help, especially with a concrete example, would be much appreciated.
 Thanks.


 There's an example in the multiprocessing documentation.
 http://docs.python.org/library/multiprocessing.html#using-a-remote-manager

 It creates a shared queue, but it's easy to modify for lists.

 For example, here's your shared list server:
 from multiprocessing.managers import BaseManager
 shared_list = []
 class ListManager(BaseManager): pass
 ListManager.register('get_list', callable=lambda:shared_list)
 m = ListManager(address=('', 5), authkey='abracadabra')
 s = m.get_server()
 s.serve_forever()

 A client that adds an element to your shared list:
 import random
 from multiprocessing.managers import BaseManager
 class ListManager(BaseManager): pass
 ListManager.register('get_list')
 m = ListManager(address=('localhost', 5), authkey='abracadabra')
 m.connect()
 l = m.get_list()
 l.append(random.random())

 And a client that prints out the shared list:
 from multiprocessing.managers import BaseManager
 class ListManager(BaseManager): pass
 ListManager.register('get_list')
 m = ListManager(address=('localhost', 5), authkey='abracadabra')
 m.connect()
 l = m.get_list()
 print str(l)

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


Re: remote multiprocessing, shared object

2010-04-08 Thread Kushal Kumaran
On Thu, Apr 8, 2010 at 11:30 AM, Norm Matloff matl...@doe.com wrote:
 Thanks very much, Kushal.

 But it seems to me that it doesn't quite work.  After your first client
 below creates l and calls append() on it, it would seem that one could
 not then assign to it, e.g. do

   l[1] = 8

 What I'd like is to write remote multiprocessing code just like threads
 code (or for that matter, just like shared-memory multiprocessing code),
 i.e. reading and writing shared globals.  Is this even possible?


Try this server:
from multiprocessing.managers import BaseManager, ListProxy
shared_list = []
class ListManager(BaseManager): pass
ListManager.register('get_list', callable=lambda:shared_list,
proxytype=ListProxy)
m = ListManager(address=('', 5), authkey='abracadabra')
s = m.get_server()
s.serve_forever()

Just changed the proxy type appropriately.  See the managers.py file
in the multiprocessing source for details.

 snipped

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


Re: Regex driving me crazy...

2010-04-08 Thread Steven D'Aprano
On Wed, 07 Apr 2010 21:57:31 -0700, Patrick Maupin wrote:

 On Apr 7, 9:51 pm, Steven D'Aprano
 ste...@remove.this.cybersource.com.au wrote:
 
 BTW, I don't know how you got 'True' here.
 
  re.split(' {2,}', s) == [x for x in s.split('  ') if x.strip()]
 True


It was a copy and paste from the interactive interpreter. Here it is, in 
a fresh session:

[st...@wow-wow ~]$ python
Python 2.5 (r25:51908, Nov  6 2007, 16:54:01)
[GCC 4.1.2 20070925 (Red Hat 4.1.2-27)] on linux2
Type help, copyright, credits or license for more information.
 import re
 s = '# 1  Short offline       Completed without error       00%'
 re.split(' {2,}', s) == [x for x in s.split('  ') if x.strip()]
True



Now I copy-and-paste from your latest post to do it again:

 s = '# 1  Short offline       Completed without error       00%'
 re.split(' {2,}', s) == [x for x in s.split('  ') if x.strip()]
False


Weird, huh?

And here's the answer: somewhere along the line, something changed the 
whitespace in the string into non-spaces:

 s
'# 1 \xc2\xa0Short offline \xc2\xa0 \xc2\xa0 \xc2\xa0 Completed without 
error \xc2\xa0 \xc2\xa0 \xc2\xa0 00%'


I blame Google. I don't know how they did it, but I'm sure it was them!
*wink*


By the way, let's not forget that the string could be fixed-width fields 
padded with spaces, in which case the right solution almost certainly 
will be:

s = '# 1  Short offline       Completed without error       00%'
result = s[25:55].rstrip()

Even in 2010, there are plenty of programs that export data using fixed 
width fields.


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


Re: Performance of list vs. set equality operations

2010-04-08 Thread Steven D'Aprano
On Wed, 07 Apr 2010 20:14:23 -0700, Raymond Hettinger wrote:

 [Raymond Hettinger]
  If the two collections have unequal sizes, then both ways immediately
  return unequal.
 
 [Steven D'Aprano]
 Perhaps I'm misinterpreting what you are saying, but I can't confirm
 that behaviour, at least not for subclasses of list:
 
 For doubters, see list_richcompare() in
 http://svn.python.org/view/python/trunk/Objects/listobject.c?
revision=78522view=markup

So what happens in my example with a subclass that (falsely) reports a 
different length even when the lists are the same?

I can guess that perhaps Py_SIZE does not call the subclass __len__ 
method, and therefore is not fooled by it lying. Is that the case?


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


Re: Simple Cookie Script: Not recognising Cookie

2010-04-08 Thread Pierre Quentel
On 8 avr, 07:03, Jimbo nill...@yahoo.com wrote:
 Hi I have a simple Python program that assigns a cookie to a web user
 when they open the script the 1st time(in an internet browser). If
 they open the script a second time the script should display the line
  You have been here 2 times. , if they open the script agai it
 should show on the webpage You have been here 3 times and so on.

 But for some reason, my program is not assigning or recognising an
 assigned cookie  outputing the line You have been here x times. I
 have gone over my code for like 2 hours now I cant figure out what is
 going wrong??

 Can you help me figure out whats wrong? I have my own cgi server that
 just runs on my machine so its not that its the code to recognise/
 assign a cookie

 [code]#!/usr/bin/env python

 import Cookie
 import cgi
 import os

 HTML_template = 
 html
   head

   /head
   body
     p %s /p
   /body
 /html
 

 def main():

     # Web Client is new to the site so we need to assign a cookie to
 them
     cookie = Cookie.SimpleCookie()
     cookie['SESSIONID'] = '1'
     code = No cookie exists. Welcome, this is your first visit.

     if 'HTTP_COOKIE' in os.environ:
         cookie = Cookie.SimpleCookie(os.environ['HTTP_COOKIE'])
         # If Web client has been here before
         if cookie.has_key('SESSIONID'):
             cookie['SESSIONID'].value = int(cookie['SESSIONID'].value)
 +1
             code = You have been here %s times. %
 cookie['SESSIONID'].value
         else:
             cookie = Cookie.SimpleCookie()
             cookie['SESSIONID'] = '1'
             code = I Have a cookie, but SESSIONID does not exist

     print Content-Type: text/html\n
     print HTML_template % code

 if __name__ == __main__:
     main()
 [/code]

Hi,

You are confusing the cookie sent by the browser to the server - you
get it by os.environ['HTTP_COOKIE'] - and the one sent by the server
to the browser : it it sent in the response headers

You can change method main() like this :

def main():

# defaut cookie to send if web Client is new to the site
set_cookie = Cookie.SimpleCookie()
set_cookie['SESSIONID'] = 1
code = No cookie exists. Welcome, this is your first visit.

if 'HTTP_COOKIE' in os.environ:
cookie = Cookie.SimpleCookie(os.environ['HTTP_COOKIE'])
if cookie.has_key('SESSIONID'):
# web client has been here before : increment number of
visits
set_cookie['SESSIONID'] = int(cookie['SESSIONID'].value)
+1
code = You have been here %s times. %
cookie['SESSIONID'].value
else:
code = I Have a cookie, but SESSIONID does not exist

print Content-Type: text/html
print set_cookie.output()   # send cookie to web client
print
print HTML_template % code

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


Re: pass object or use self.object?

2010-04-08 Thread Bruno Desthuilliers

Lie Ryan a écrit :

On 04/07/10 18:34, Bruno Desthuilliers wrote:

Lie Ryan a écrit :
(snip)


Since in function in python is a first-class object, you can instead do
something like:

def process(document):
# note: document should encapsulate its own logic
document.do_one_thing()

Obvious case of encapsulation abuse here. Should a file object
encapsulate all the csv parsing logic ? (and the html parsing, xml
parsing, image manipulation etc...) ? Should a model object
encapsulate the presentation logic ? I could go on for hours here...


Yes, but no; you're taking it out of context. Is {csv|html|xml|image}
parsing logic a document's logic? Is presentation a document's logic? If
they're not, then they do not belong in document.


Is len() a list logic ? If yes, it should belong to list !-)

There are two points here : the first is that we (that is, at least, you 
and me) just don't know enough about the OP's project to tell whether 
something should belong to the document or not. period. The second point 
is that objects don't live in a splendid isolation, and it's perfectly 
ok to have code outside an object's method working on the object.


wrt/ these two points, your document should encapsulate its own logic 
note seems a bit dogmatic (and not necessarily right) to me - hence my 
answer.


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


Re: Loading an imported module (C API)

2010-04-08 Thread CHEN Guang
PythoidC (http://pythoidc.sourceforge.net/) provides an easy way for developing 
and importing C modules into Python environment. There are some examples, may 
be of some help to you. 
With PythoidC, Python programmers can write, debug, commpile, C codes all in 
Python IDE, and do not have to tackel the boring braces {} and line-end 
semicolons ; 
PythoidC automatically parses C header files, make corresponding Python 
objects, thus Python programmer can essily introspect into C ... ...
CHEN Guang
- Original Message - 
From: booncw chiaw...@gmail.com
To: python-list@python.org
Sent: Tuesday, April 06, 2010 9:58 PM
Subject: Loading an imported module (C API)


 Hi,
 
 I am running a simulation where the python module has already been
 imported.   Could you please tell me how to load it?
 
 I've been doing this (importing everytime), but it is too slow:
 pModule = PyImport_Import(pName);
 
 
 Yours,
 
 Boon
 
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Translation docstrings with gettext

2010-04-08 Thread sapient
Thank you for your help.

Solution with decorators looks well, will try it in near future.
I will report here if I solve this problem.
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Python and Regular Expressions

2010-04-08 Thread Nobody
On Wed, 07 Apr 2010 18:25:36 -0700, Patrick Maupin wrote:

 Regular expressions != Parsers
 
 True, but lots of parsers *use* regular expressions in their
 tokenizers.  In fact, if you have a pure Python parser, you can often
 get huge performance gains by rearranging your code slightly so that
 you can use regular expressions in your tokenizer, because that
 effectively gives you access to a fast, specialized C library that is
 built into practically every Python interpreter on the planet.

Unfortunately, a typical regexp library (including Python's) doesn't allow
you to match against a set of regexps, returning the index of which one
matched. Which is what you really want for a tokeniser.

 Every time someone tries to parse nested structures using regular
 expressions, Jamie Zawinski kills a puppy.
 
 And yet, if you are parsing stuff in Python, and your parser doesn't
 use some specialized C code for tokenization (which will probably be
 regular expressions unless you are using mxtexttools or some other
 specialized C tokenizer code), your nested structure parser will be
 dog slow.

The point is that you *cannot* match arbitrarily-nested expressions using
regexps. You could, in theory, write a regexp which will match any valid
syntax up to N levels of nesting, for any finite N. But in practice, the
regexp is going to look horrible (and is probably going to be quite
inefficient if the regexp library uses backtracking rather than a DFA).

Even tokenising with Python's regexp interface is inefficient if the
number of token types is large, as you have to test against each regexp
sequentially.

Ultimately, if you want an efficient parser, you need something with a C
component, e.g. Plex.

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


Re: Loading an imported module (C API)

2010-04-08 Thread Christian Heimes

On 06.04.2010 15:58, booncw wrote:

Hi,

I am running a simulation where the python module has already been
imported.   Could you please tell me how to load it?

I've been doing this (importing everytime), but it is too slow:
pModule = PyImport_Import(pName);


You can cache the module object in a static variable:

static PyObject *module = NULL;
if (module == NULL   (module = PyImport_ImportModuleNoBlock(module)) 
== NULL) {

return NULL;
}
}

Christian

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


Re: Performance of list vs. set equality operations

2010-04-08 Thread Terry Reedy

On 4/8/2010 3:07 AM, Steven D'Aprano wrote:

On Wed, 07 Apr 2010 20:14:23 -0700, Raymond Hettinger wrote:


[Raymond Hettinger]

If the two collections have unequal sizes, then both ways immediately
return unequal.


[Steven D'Aprano]

Perhaps I'm misinterpreting what you are saying, but I can't confirm
that behaviour, at least not for subclasses of list:


For doubters, see list_richcompare() in
http://svn.python.org/view/python/trunk/Objects/listobject.c?

revision=78522view=markup

So what happens in my example with a subclass that (falsely) reports a
different length even when the lists are the same?

I can guess that perhaps Py_SIZE does not call the subclass __len__
method, and therefore is not fooled by it lying. Is that the case?


Adding a print call within __len__ should determine that.



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


python grep

2010-04-08 Thread Mag Gam
I am in the process of reading a zipped file which is about 6gb.

I would like to know if there is a command similar to grep in python
because I would like to emulate, -A -B option of GNU grep.

Lets say I have this,

083828.441,AA
093828.441,AA
094028.441,AA
094058.441,CC
094828.441,AA
103828.441,AA
123828.441,AA


if I do grep -A2 -B2 CC

I get 2 lines before and 2 lines after C

Is there an easy way to do this in python?

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


Re: Translation docstrings with gettext

2010-04-08 Thread Peter Otten
sapient wrote:

 I found several discussions where this question was asked, but was not
 answered.
 
 Now I am creating Python-API for my application, and want create it
 with translation support, including documentation strings for modules,
 classes, methods etc.
 
 It is simple to translate special-marked strings with gettext, but it
 is problem with docstrings: if I mark them for translation like
 _(Some documentation string) then it is not recognized as
 docstring. If I omit _() markup, then string is not translated.
 
 Script pygettext.py has key --docstrings that forces extraction
 docstrings from module, so I suppose, that it must be way to use thier
 translations.

 So, question is: How to translate docstrings in my example?

You could leave the docstrings alone and monkey-patch inspect.getdoc() to do 
the translation when help() is invoked:

#! /usr/bin/python2.6
# -*- coding: utf-8 -*-
import os, gettext

localedir = os.path.join( os.path.dirname(__file__), locale/ )

t = gettext.translation( 'testmodule', localedir=localedir, 
languages=['ru'], codeset=utf-8 )
t.install()

import testmodule

import inspect
def getdoc(object):
try:
doc = object.__doc__
except AttributeError:
return None
if not isinstance(doc, basestring):
return None

return inspect.cleandoc(_(doc))

inspect.getdoc = getdoc


help( testmodule )
testmodule.TestClass().testClassMethod()

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


Re: python grep

2010-04-08 Thread Stefan Behnel

Mag Gam, 08.04.2010 13:21:

I am in the process of reading a zipped file which is about 6gb.

I would like to know if there is a command similar to grep in python
because I would like to emulate, -A -B option of GNU grep.

Lets say I have this,

083828.441,AA
093828.441,AA
094028.441,AA
094058.441,CC
094828.441,AA
103828.441,AA
123828.441,AA


if I do grep -A2 -B2 CC

I get 2 lines before and 2 lines after C

Is there an easy way to do this in python?


Sure, just use a sliding window.

However, for a 6BG file, you won't really like the performance. It's 
basically impossible to beat the speed of (f)grep.


I'd use the subprocess module to run zfgrep over the file and parse the 
output in Python.


Stefan

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


Re: Python and Regular Expressions

2010-04-08 Thread Richard Lamboj

At the moment i have less time, so its painful to read about parsing, but it 
is quite interessting.

I have taken a look at the different Parsing Modules and i'am reading the 
Source Code to understand how they Work. Since Yesterday i'am writing on my 
own small Engine - Just for Fun and understanding how i can get what i need.

It seems that this is hard to code, becouse the logic is complex and sometimes 
confussing. Its not easy to find a perfect solution.

If someone knows good links to this thema, or can explain how parsers 
should/could work, please post it, or explain it.

Thanks for the Informations and the Help!

Kind Regards

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


Re: Python and Regular Expressions

2010-04-08 Thread Charles

Nobody nob...@nowhere.com wrote in message 
news:pan.2010.04.08.10.12.59.594...@nowhere.com...
 On Wed, 07 Apr 2010 18:25:36 -0700, Patrick Maupin wrote:

 Regular expressions != Parsers

 True, but lots of parsers *use* regular expressions in their
 tokenizers.  In fact, if you have a pure Python parser, you can often
 get huge performance gains by rearranging your code slightly so that
 you can use regular expressions in your tokenizer, because that
 effectively gives you access to a fast, specialized C library that is
 built into practically every Python interpreter on the planet.

 Unfortunately, a typical regexp library (including Python's) doesn't allow
 you to match against a set of regexps, returning the index of which one
 matched. Which is what you really want for a tokeniser.

[snip]

Really !,
 I am only a python newbie, but what about ...

import re
rr = [
  ( id,'([a-zA-Z][a-zA-Z0-9]*)' ),
  ( int,   '([+-]?[0-9]+)' ),
  ( float, '([+-]?[0-9]+\.[0-9]*)' ),
  ( float, '([+-]?[0-9]+\.[0-9]*[eE][+-]?[0-9]+)' )
]
tlist = [ t[0] for t in rr ]
pat = '^ *(' + '|'.join([ t[1] for t in rr ]) + ') *$'
p = re.compile(pat)

ss = [ ' annc', '1234', 'abcd', '  234sz ', '-1.24e3', '5.' ]
for s in ss:
  m = p.match(s)
  if m:
ix = [ i-2 for i in range(2,6) if m.group(i) ]
print '+s+' matches and has type, tlist[ix[0]]
  else:
print '+s+' does not match

output:
' annc' matches and has type id
'1234' matches and has type int
'abcd' matches and has type id
'  234sz ' does not match
'-1.24e3' matches and has type float
'5.' matches and has type float

seems to me to match a (small) set of regular expressions and
indirectly return the index of the matched expression, without
doing a sequential loop over the regular expressions.

Of course there is a loop over the reults of the match to determine
which sub-expression matched, but a good regexp library (which
I presume Python has) should match the sub-expressions without
looping over them. The techniques to do this were well known in
the 1970's when the first versons of lex were written.

Not that I would recommend tricks like this. The regular
expression would quickly get out of hand for any non-trivial
list of regular expresssions to match.

Charles



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


daemon.DaemonContext

2010-04-08 Thread Rebelo

i get : IOError: [Errno 9] Bad file descriptor
when i have logging and using daemon.DaemonContext()
i tried passing :
fh = logging.handlers.TimedRotatingFileHandler(LOG_FILENAME, 'midnight', 
encoding='utf-8')

with :
context = daemon.DaemonContext()
context.files_preserve=[fh]

but no good
what am i doing wrong?
--
http://mail.python.org/mailman/listinfo/python-list


Re: python grep

2010-04-08 Thread Mag Gam
Oh, thats nice to know!

But I use the CSV module with gzip module. Is it still possible to do
it with the subprocess?



On Thu, Apr 8, 2010 at 7:31 AM, Stefan Behnel stefan...@behnel.de wrote:
 Mag Gam, 08.04.2010 13:21:

 I am in the process of reading a zipped file which is about 6gb.

 I would like to know if there is a command similar to grep in python
 because I would like to emulate, -A -B option of GNU grep.

 Lets say I have this,

 083828.441,AA
 093828.441,AA
 094028.441,AA
 094058.441,CC
 094828.441,AA
 103828.441,AA
 123828.441,AA


 if I do grep -A2 -B2 CC

 I get 2 lines before and 2 lines after C

 Is there an easy way to do this in python?

 Sure, just use a sliding window.

 However, for a 6BG file, you won't really like the performance. It's
 basically impossible to beat the speed of (f)grep.

 I'd use the subprocess module to run zfgrep over the file and parse the
 output in Python.

 Stefan

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

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


Re: daemon.DaemonContext

2010-04-08 Thread Rebelo
when i use this :
 context = daemon.DaemonContext(stdin=sys.stdin, stdout=sys.stdout,
files_preserve=[fh], signal_map = {signal.SIGTERM:
'terminate',signal.SIGHUP: 'terminate'})

i don't get error but i still can't write to log file
what am i doing wrong?
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: daemon.DaemonContext

2010-04-08 Thread Vinay Sajip
On Apr 8, 1:28 pm, Rebelo puntabl...@gmail.com wrote:
 when i use this :
  context = daemon.DaemonContext(stdin=sys.stdin, stdout=sys.stdout,
 files_preserve=[fh], signal_map = {signal.SIGTERM:
 'terminate',signal.SIGHUP: 'terminate'})

 i don't get error but i still can't write to log file
 what am i doing wrong?

Not sure, offhand.

Change to a script which just writes to the file (rather than going
via logging) - does that work? What platform are you on, what version
etc?

It's unlikely to be a logging-related problem: more likely it's to do
with file descriptors.

Regards,

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


Re: daemon.DaemonContext

2010-04-08 Thread Vinay Sajip
On Apr 8, 1:28 pm, Rebelo puntabl...@gmail.com wrote:
 when i use this :
  context = daemon.DaemonContext(stdin=sys.stdin, stdout=sys.stdout,
 files_preserve=[fh], signal_map = {signal.SIGTERM:
 'terminate',signal.SIGHUP: 'terminate'})

 i don't get error but i still can't write to log file
 what am i doing wrong?

My guess is - files_preserve needs to be passed a file handle and not
a logging handler.

Regards,

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


Re: daemon.DaemonContext

2010-04-08 Thread Rebelo

Vinay Sajip wrote:

On Apr 8, 1:28 pm, Rebelo puntabl...@gmail.com wrote:

when i use this :
 context = daemon.DaemonContext(stdin=sys.stdin, stdout=sys.stdout,
files_preserve=[fh], signal_map = {signal.SIGTERM:
'terminate',signal.SIGHUP: 'terminate'})

i don't get error but i still can't write to log file
what am i doing wrong?


My guess is - files_preserve needs to be passed a file handle and not
a logging handler.

Regards,

Vinay Sajip


thnx for help.
writing to a file works, but i need logging.
do you know where can i find good documnetation for python-daemon?
--
http://mail.python.org/mailman/listinfo/python-list


Re: Profiling: Interpreting tottime

2010-04-08 Thread Nikolaus Rath
Gabriel Genellina gagsl-...@yahoo.com.ar writes:
 En Wed, 07 Apr 2010 18:44:39 -0300, Nikolaus Rath nikol...@rath.org
 escribió:

 def check_s3_refcounts():
 Check s3 object reference counts

 global found_errors
 log.info('Checking S3 object reference counts...')

 for (key, refcount) in conn.query(SELECT id, refcount FROM
 s3_objects):

 refcount2 = conn.get_val(SELECT COUNT(inode) FROM blocks
 WHERE s3key=?,
  (key,))
 if refcount != refcount2:
 log_error(S3 object %s has invalid refcount, setting
 from %d to %d,
   key, refcount, refcount2)
 found_errors = True
 if refcount2 != 0:
 conn.execute(UPDATE s3_objects SET refcount=? WHERE
 id=?,
  (refcount2, key))
 else:
 # Orphaned object will be picked up by check_keylist
 conn.execute('DELETE FROM s3_objects WHERE id=?', (key,))

 When I ran cProfile.Profile().runcall() on it, I got the following
 result:

ncalls  tottime  percall  cumtime  percall filename:lineno(function)
 1 7639.962 7639.962 7640.269 7640.269
 fsck.py:270(check_s3_refcounts)

 So according to the profiler, the entire 7639 seconds where spent
 executing the function itself.

 How is this possible? I really don't see how the above function can
 consume any CPU time without spending it in one of the called
 sub-functions.

 Is the conn object implemented as a C extension?

No, it's pure Python.

 The profiler does not
 detect calls to C functions, I think.

Hmm. Isn't this a C function?

   262.3170.0892.3170.089 {method 'execute' of 
'apsw.Cursor' objects}

 You may be interested in this package by Robert Kern:
 http://pypi.python.org/pypi/line_profiler
 Line-by-line profiler.
 line_profiler will profile the time individual lines of code take to
 execute.

That looks interesting nevertheless, thanks!


   -Nikolaus

-- 
 »Time flies like an arrow, fruit flies like a Banana.«

  PGP fingerprint: 5B93 61F8 4EA2 E279 ABF6  02CF A9AD B7F8 AE4E 425C
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: ftp and python

2010-04-08 Thread Simon
You could user FTP.voidcmd()
E.G.
ftp.voidcmd('RNFT filename.txt')ftp.voidcmd('RNTO newdir/filename.txt')
From the rfc:

RENAME FROM (RNFR)

   This command specifies the old pathname of the file which is
   to be renamed.  This command must be immediately followed by
   a rename to command specifying the new file pathname.

RENAME TO (RNTO)

   This command specifies the new pathname of the file
   specified in the immediately preceding rename from
   command.  Together the two commands cause a file to be
   renamed.

P.S. Sorry about emailing that direct to you John :(


On 8 April 2010 07:24, John Nagle na...@animats.com wrote:

 Tim Chase wrote:

 Matjaz Pfefferer wrote:

 What would be the easiest way to copy files from one ftp
 folder to another without downloading them to local system?


 As best I can tell, this isn't well-supported by FTP[1] which doesn't seem
 to have a native copy this file from server-location to server-location
 bypassing the client. There's a pair of RNFR/RNTO commands that allow you
 to rename (or perhaps move as well) a file which ftplib.FTP.rename()
 supports but it sounds like you want too copies.


   In theory, the FTP spec supports three-way transfers, where the
 source, destination, and control can all be on different machines.
 But no modern implementation supports that.

John Nagle

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

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


get objects from image

2010-04-08 Thread Jannis Syntychakis
Hallo Everybody,

Maybe you can help me with this:

i have a picture. The picture is black, with some white objects.

Is there any way i can detect the automatically? Something like:
if there white objects bigger than 3 pixels draw a box there or get
their
position!

getting their position is more important for me.

One more question:

can i let the user tell the software there the white object is? like a
square
this moves with the mouse and the user will be able to click on the
white objects.

Maybe somebosy could help? with an example maybe?

Thank you very much in advance

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


Re: daemon.DaemonContext

2010-04-08 Thread Vinay Sajip
On Apr 8, 1:58 pm, Rebelo puntabl...@gmail.com wrote:
 Vinay Sajip wrote:
  On Apr 8, 1:28 pm, Rebelo puntabl...@gmail.com wrote:
  when i use this :
   context = daemon.DaemonContext(stdin=sys.stdin, stdout=sys.stdout,
  files_preserve=[fh], signal_map = {signal.SIGTERM:
  'terminate',signal.SIGHUP: 'terminate'})

  i don't get error but i still can't write to log file
  what am i doing wrong?

  My guess is - files_preserve needs to be passed a file handle and not
  alogginghandler.

  Regards,

  Vinay Sajip

 thnx for help.
 writing to a file works, but i needlogging.
 do you know where can i find good documnetation for python-daemon?

No,

but see this post:

http://groups.google.com/group/comp.lang.python/msg/851ce78e53812ade

It may lead you to more information. The thread shows that Sean
DiZazzo got logging working with the package.

I think you just have to pass the file object used by the handler
(fh.stream) in the files_preserve array.

Regards,

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


Re: daemon.DaemonContext

2010-04-08 Thread Rebelo

i found a crude workaround:
i wrote a function in which i start logging after deamon starts
--
http://mail.python.org/mailman/listinfo/python-list


Re: ftp and python

2010-04-08 Thread Tim Chase

Simon wrote:

You could user FTP.voidcmd()
E.G.
ftp.voidcmd('RNFT filename.txt')ftp.voidcmd('RNTO newdir/filename.txt')

From the rfc:


RENAME FROM (RNFR)

   This command specifies the old pathname of the file which is
   to be renamed.  This command must be immediately followed by
   a rename to command specifying the new file pathname.

RENAME TO (RNTO)

   This command specifies the new pathname of the file
   specified in the immediately preceding rename from
   command.  Together the two commands cause a file to be
   renamed.


As mentioned in my original reply, that should be what 
ftplib.FTP.rename() does under the covers[1].  However, the OP 
was asking about copying a file, not renaming a file.


John mentioned the poorly-supported server-to-server copy, but 
from my understanding, I think that still slurps locally and then 
pushes it back up elsewhere.


-tkc

[1]
taken from ftplib.py:

def rename(self, fromname, toname):
'''Rename a file.'''
resp = self.sendcmd('RNFR ' + fromname)
if resp[0] != '3':
raise error_reply, resp
return self.voidcmd('RNTO ' + toname)




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


Re: daemon.DaemonContext

2010-04-08 Thread Rebelo

Vinay Sajip wrote:

On Apr 8, 1:58 pm, Rebelo puntabl...@gmail.com wrote:

Vinay Sajip wrote:

On Apr 8, 1:28 pm, Rebelo puntabl...@gmail.com wrote:

when i use this :
 context = daemon.DaemonContext(stdin=sys.stdin, stdout=sys.stdout,
files_preserve=[fh], signal_map = {signal.SIGTERM:
'terminate',signal.SIGHUP: 'terminate'})
i don't get error but i still can't write to log file
what am i doing wrong?

My guess is - files_preserve needs to be passed a file handle and not
alogginghandler.
Regards,
Vinay Sajip

thnx for help.
writing to a file works, but i needlogging.
do you know where can i find good documnetation for python-daemon?


No,

but see this post:

http://groups.google.com/group/comp.lang.python/msg/851ce78e53812ade

It may lead you to more information. The thread shows that Sean
DiZazzo got logging working with the package.

I think you just have to pass the file object used by the handler
(fh.stream) in the files_preserve array.

Regards,

Vinay Sajip

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


Re: Regex driving me crazy...

2010-04-08 Thread J
On Thu, Apr 8, 2010 at 01:16, Kushal Kumaran
kushal.kumaran+pyt...@gmail.com wrote:

 Is there any particular reason you absolutely must extract the status
 message?  If you already have a list of possible status messages, you
 could just test which one of those is present in the line...

Yes and no...

Mostly, it's for the future.  Right now, this particular test script
(and I mean test script in the sense it's part of a testing framework,
not in the sense that I'm being tested on it ;-)  ) is fully
automated.

Once the self-test on the HDD is complete, the script will return
either a 0 or 1 for PASS or FAIL respectively.

However, in the future, it may need to be changed to or also handled
manually instead of automatically. And if we end up wanting it to be
automatic, then having that phrase would be important for logging or
problem determination.  We don't so much care about the rest of the
string I want to parse as the data it gives is mostly meaningless, but
having it pull things like:

Completed: Electrical error

or

Completed: Bad Sectors Found

could as useful as

Completed without error

or

Aborted by user

So that's why I was focusing on just extracting that phrase from the
output.  I could just pull the entire string and do a search for the
phrases in question, and that's probably the simplest thing to do:

re.search(Search Phrase,outputString)

but I do have a tendency to overthink things some times and besides
which, having just that phrase for the logs, or for use in a future
change would be cool, and this way, I've already got that much of it
done for later on.
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: get objects from image

2010-04-08 Thread Chris Hulan
On Apr 8, 9:17 am, Jannis Syntychakis ioan...@live.nl wrote:
 Hallo Everybody,

 Maybe you can help me with this:

 i have a picture. The picture is black, with some white objects.

 Is there any way i can detect the automatically? Something like:
 if there white objects bigger than 3 pixels draw a box there or get
 their
 position!

 getting their position is more important for me.

 One more question:

 can i let the user tell the software there the white object is? like a
 square
 this moves with the mouse and the user will be able to click on the
 white objects.

 Maybe somebosy could help? with an example maybe?

 Thank you very much in advance

 Ioannis

Sounds like a job for an image lib, like say Camellia (http://
camellia.sourceforge.net/index.html)
Their info Mentions a Ruby interface, so a python interface should be
fairly easy

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


How to call application in the background with subprocess.call

2010-04-08 Thread jorma kala
Hi,

I'd like to call an external application (firefox) from a python program (a
PyQT GUI), but I want the external application to run in the background, I
mean I do not want my python calling program to wait till the external
subprocess terminates.
I've tried this:

call([firefox, http://www.python.org;])

but my PyQT interface freezes until I terminate Firefox.
Is there any parameter I need to use with call so that the python calling
program doesn't wait for the termination of the subprocess?

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


Re: The Regex Story

2010-04-08 Thread Tim Chase

Lie Ryan wrote:

Why am I seeing a lot of this pattern lately:

OP: Got problem with string
+- A: Suggested a regex-based solution
   +- B: Quoted Some people ... regex ... two problems.

or

OP: Writes some regex, found problem
+- A: Quoted Some people ... regex ... two problems.
   +- B: Supplied regex-based solution, clean one
  +- A: Suggested PyParsing (or similar)


There's a spectrum of parsing solutions:

- string.split() or string[slice] notations handle simple cases 
and are built-in


- regexps handle more complex parsing tasks and are also built in

- pyparsing handles far more complex parsing tasks (nesting, etc) 
but isn't built-in



The above dialog tends to appear when the task isn't in the 
sweet-spot of regexps.  Either it's sufficiently simple that 
simple split/slice notation will do, or (at the other end of the 
spectrum) the effort to get it working with a regexp is hairy and 
convoluted, worthy of a more readable solution implemented with 
pyparsing.  The problem comes from people thinking that regexps 
are the right solution to *every* problem...often demonstrated by 
the OP writing how do I write a regexp to solve this 
non-regexp-optimal problem assuming regexps are the right tool 
for everything.


There are some problem-classes for which regexps are the *right* 
solution, and I don't see as much of your example dialog in those 
cases.


-tkc


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


Re: How to call application in the background with subprocess.call

2010-04-08 Thread Kushal Kumaran
On Thu, Apr 8, 2010 at 7:39 PM, jorma kala jjk...@gmail.com wrote:
 Hi,

 I'd like to call an external application (firefox) from a python program (a
 PyQT GUI), but I want the external application to run in the background, I
 mean I do not want my python calling program to wait till the external
 subprocess terminates.
 I've tried this:

 call([firefox, http://www.python.org;])

 but my PyQT interface freezes until I terminate Firefox.
 Is there any parameter I need to use with call so that the python calling
 program doesn't wait for the termination of the subprocess?


Use subprocess.Popen instead of directly using subprocess.call.

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


Re: Regex driving me crazy...

2010-04-08 Thread Grant Edwards
On 2010-04-08, Steven D'Aprano ste...@remove.this.cybersource.com.au wrote:

 Even in 2010, there are plenty of programs that export data using fixed 
 width fields.

If you want the columns to line up as the data changes, that's pretty
much the only way to go.

-- 
Grant Edwards   grant.b.edwardsYow! But was he mature
  at   enough last night at the
  gmail.comlesbian masquerade?
-- 
http://mail.python.org/mailman/listinfo/python-list


Py3: Read file with Unicode characters

2010-04-08 Thread Gnarlodious
Attempting to read a file containing Unicode characters such as ±:
UnicodeDecodeError: 'ascii' codec can't decode byte 0xc2 in position
5007: ordinal not in range(128)

I did succeed by converting all the characters to HTML entities such
as #177;, but I want the characters to be the actual font in the
source file. What am I doing wrong? My understanding is that ALL
strings in Py3 are unicode so... confused.

-- Gnarlie

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


Re: get objects from image

2010-04-08 Thread Chris Colbert
lookup connected component labeling in a machine vision book.

On Thu, Apr 8, 2010 at 9:57 AM, Chris Hulan chris.hu...@gmail.com wrote:
 On Apr 8, 9:17 am, Jannis Syntychakis ioan...@live.nl wrote:
 Hallo Everybody,

 Maybe you can help me with this:

 i have a picture. The picture is black, with some white objects.

 Is there any way i can detect the automatically? Something like:
 if there white objects bigger than 3 pixels draw a box there or get
 their
 position!

 getting their position is more important for me.

 One more question:

 can i let the user tell the software there the white object is? like a
 square
 this moves with the mouse and the user will be able to click on the
 white objects.

 Maybe somebosy could help? with an example maybe?

 Thank you very much in advance

 Ioannis

 Sounds like a job for an image lib, like say Camellia (http://
 camellia.sourceforge.net/index.html)
 Their info Mentions a Ruby interface, so a python interface should be
 fairly easy

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

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


Re: Py3: Read file with Unicode characters

2010-04-08 Thread Martin v. Loewis
Gnarlodious wrote:
 Attempting to read a file containing Unicode characters such as ±:
 UnicodeDecodeError: 'ascii' codec can't decode byte 0xc2 in position
 5007: ordinal not in range(128)
 
 I did succeed by converting all the characters to HTML entities such
 as #177;, but I want the characters to be the actual font in the
 source file. What am I doing wrong? My understanding is that ALL
 strings in Py3 are unicode so... confused.

When opening the file, you need to specify the file encoding. If you
don't, it defaults to ASCII (in your situation; the specific default
depends on the environment).

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


Re: Performance of list vs. set equality operations

2010-04-08 Thread Raymond Hettinger
[Steven D'Aprano]
 So what happens in my example with a subclass that (falsely) reports a
 different length even when the lists are the same?

 I can guess that perhaps Py_SIZE does not call the subclass __len__
 method, and therefore is not fooled by it lying. Is that the case?

Yes.  Py_SIZE() gets the actual size of the underlying list.

The methods for most builtin containers typically access the
underlying structure directly.  That makes them fast and allows
them to maintain their internal invariants.


Raymond

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


Replacing Periods with Backspaces

2010-04-08 Thread Booter
All,

I am trying to replace a series of periods in a sting with backspaces
that way I can easily parse information from a Windows command.  the
current statement I have for this is

***Statement
capture = re.sub('\.*', '\b', capture)

===Out Put===
str: \bi\bp\bc\bo\bn\bf\bi\bg\b \b/\ba\bl\bl\b\n\b\n\bW\bi\bn\bd\bo\bw
\bs\b \bI\bP\b \bC\bo\bn\bf\bi\bg\bu\br\ba\bt\bi\bo\bn\b\n\b\n\b \b \b
\bH\bo\bs\bt\b \bN\ba\bm\be\b \b \b \b \b \b \b \b \b \b \b \b \b \b:
\b \bT\br\bi\bt\bo\bn\b\n\b \b \b \bP\br\bi\bm\ba\br\by\b \bD\bn\bs\b
\bS\bu\bf\bf\bi\bx\b \b \b \b \b \b \b \b \b \b:\b \be\bn\bg\br\bC\bo
\bl\bo\bS\bt\ba\bt\be\bE\bD\bU\b\n\b \b \b \bN\bo\bd\be\b \bT\by\bp\be
\b \b \b \b \b \b \b \b \b \b \b \b \b \b:\b \bH\by\bb\br\bi\bd\b\n\b
\b \b \bI\bP\b \bR\bo\bu\bt\bi\bn\bg\b \bE\bn\ba\bb\bl\be\bd\b \b \b
\b \b \b \b \b \b:\b \bN\bo\b\n\b \b \b \bW\bI\bN\bS\b \bP\br\bo\bx\by
\b \bE\bn\ba\bb\bl\be\bd\b \b \b \b \b \b \b \b \b:\b \bN\bo\b\n\b \b
\b \bD\bN\bS\b \bS\bu\bf\bf\bi\bx\b \bS\be\ba\br\bc\bh\b \bL\bi\bs\bt
\b \b \b \b \b \b \b:\b \be\bn\bg\br\bC\bo\bl\bo\bS\bt\ba\bt\be\bE\bD
\bU\b\n\b \b \b \b \b \b \b \b \b \b \b \b \b \b \b \b \b \b \b \b \b
\b \b \b \b \b \b \b \b \b \b \b \b \b \b \b \b \b \b \bC\bo\bl\bo\bS
\bt\ba\bt\be\bE\bD\bU\b\n\b\n\bE\bt\bh\be\br\bn\be\bt\b \ba\bd\ba\bp\bt
\be\br\b \bL\bo\bc\ba\bl\b \bA\br\be\ba\b \bC\bo\bn\bn\be\bc\bt\bi\bo
\bn\b:\b\n\b\n\b \b \b \bC\bo\bn\bn\be\bc\bt\bi\bo\bn\b-\bs\bp\be\bc\bi
\bf\bi\bc\b \bD\bN\bS\b \bS\bu\bf\bf\bi\bx\b \b \b \b:\b \b\n\b \b \b
\bD\be\bs\bc\br\bi\bp\bt\bi\bo\bn\b \b \b \b \b \b \b \b \b \b \b \b
\b:\b \bR\be\ba\bl\bt\be\bk\b \bP\bC\bI\be\b \bG\bB\bE\b \bF\ba\bm\bi
\bl\by\b \bC\bo\bn\bt\br\bo\bl\bl\be\br\b\n\b \b \b \bP\bh\by\bs\bi\bc
\ba\bl\b \bA\bd\bd\br\be\bs\bs\b \b \b \b \b \b \b...
==END

which paces a bunch of '\b' strings throughout the string (to me at
random).  It sort of works if I use this command but I don't want
there to be whitespace...

**Statement***
capture = re.sub('\.*', '\b', capture)

===Out Put===
str: ipconfig /all\n\nWindows IP Configuration\n\n   Host
Name : Triton\n   Primary Dns Suffix :
engrColoStateEDU\n   Node Type : Hybrid\n   IP Routing
Enabled: No\n   WINS Proxy Enabled: No\n   DNS Suffix
Search List  : engrColoStateEDU
\n   ColoStateEDU\n\nEthernet
adapter Local Area Connection:\n\n   Connection-specific DNS
Suffix   : \n   Description: Realtek PCIe GBE Family
Controller\n   Physical Address : 00-24-1D-16-FF-28\n   DHCP
Enabled   : No\n   Autoconfiguration Enabled : Yes\n
IPv4 Address   : 12982227254(Preferred) \n   Subnet
Mask: 2552552480\n   Default Gateway  :
129822241\n   DNS Servers:
1298210378\n   1298210379\n
NetBIOS over Tcpip: Enabled\n\nTunnel adapter
isatap{04FB4DF5-4B41-4058-A641-6965D13CCC06}:\n\n   Media
State: Media disconnected\n   Connection-specific DNS
Suffix   ...
==END


If anyone can help it would be greatly appreciated.

Thanks,

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


How to open and read an unknown extension file

2010-04-08 Thread varnikat t
I am trying to do this
if os.path.exists(*.*.txt):
file=open(*.*.txt)
self.text_view.get_buffer().set_text(file.read())
else:
file=open(*.*.html)
self.text_view.get_buffer().set_text(file.read())

It gives error *.*.txt not existingThere are two files in the folder
testing.pnm.txt
and testing.pnm.html
How to make it open any name and extension file and read it?

Regards
Varnika Tewari
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: python grep

2010-04-08 Thread Stefan Behnel

Mag Gam, 08.04.2010 14:21:

On Thu, Apr 8, 2010 at 7:31 AM, Stefan Behnel wrote:

Mag Gam, 08.04.2010 13:21:


I am in the process of reading a zipped file which is about 6gb.

I would like to know if there is a command similar to grep in python
because I would like to emulate, -A -B option of GNU grep.

Lets say I have this,

083828.441,AA
093828.441,AA
094028.441,AA
094058.441,CC
094828.441,AA
103828.441,AA
123828.441,AA


if I do grep -A2 -B2 CC

I get 2 lines before and 2 lines after C

Is there an easy way to do this in python?


Sure, just use a sliding window.

However, for a 6BG file, you won't really like the performance. It's
basically impossible to beat the speed of (f)grep.

I'd use the subprocess module to run zfgrep over the file and parse the
output in Python.


Oh, thats nice to know!

But I use the CSV module with gzip module. Is it still possible to do
it with the subprocess?


Depends on what you do with the csv module and how it interacts with the 
search above. Giving more detail may allow us to answer your question and 
to provide better advice.


Stefan

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


Re: How to open and read an unknown extension file

2010-04-08 Thread Kushal Kumaran
On Thu, Apr 8, 2010 at 9:00 PM, varnikat t varnika...@gmail.com wrote:
 I am trying to do this
 if os.path.exists(*.*.txt):
             file=open(*.*.txt)
             self.text_view.get_buffer().set_text(file.read())
 else:
             file=open(*.*.html)
             self.text_view.get_buffer().set_text(file.read())

 It gives error *.*.txt not existingThere are two files in the folder
 testing.pnm.txt
 and testing.pnm.html
 How to make it open any name and extension file and read it?


os.path.exists does not do pattern matching like that.  Take a look at
the glob module.

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


Re: Py3: Read file with Unicode characters

2010-04-08 Thread Gnarlodious
On Apr 8, 9:14 am, Martin v. Loewis wrote:

 When opening the file, you need to specify the file encoding.

OK, I had tried this:

open(path, 'r').read().encode('utf-8')

however I get error

TypeError: Can't convert 'bytes' object to str implicitly

I had assumed a Unicode string was a Unicode string, so why is it a
bytes string?

Sorry, doing Unicode in Py3 has really been a challenge.

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


Re: fcntl, serial ports and serial signals on RS232.

2010-04-08 Thread Anssi Saari
Max Kotasek mawr...@gmail.com writes:

 Hello to all out there,

 I'm trying to figure out how to parse the responses from fcntl.ioctl()
 calls that modify the serial lines in a way that asserts that the line
 is now changed.  For example I may want to drop RTS explicitly, and
 assert that the line has been dropped before returning.

 Here is a brief snippet of code that I've been using to do that, but
 not sure what to do with the returned response:

 def set_RTS(self, state=True):
   if self.fd is None:
 return 0

   p = struct.pack('I', termios.TIOCM_RTS)
   if state:
 return fcntl.ioctl(self.fd, termios.TIOCMBIS, p)
   else:
 return fcntl.ioctl(self.fd, termios.TIOCMBIC, p)

 The problem is I get responses like '\x01\x00\x00\x00', or
 '\x02\x00\x00\x00'  and I'm not sure what they mean.

I'm not an expert in this by any means. However, I don't think that
fcntl call actually returns the port status after the bit setting. But
why not check it explicitly with termios.TIOCMGET? At least then I
seem to be able to toggle the RTS bit (bit 2) in the register. Here
are the trivial functions I used:

def set_rts(fd):
print Setting RTS.
p = struct.pack('I', termios.TIOCM_RTS) 
fcntl.ioctl(fd, termios.TIOCMBIS, p)

def clear_rts(fd):
print Clearing RTS.
p = struct.pack('I', termios.TIOCM_RTS) 
fcntl.ioctl(fd, termios.TIOCMBIC, p)

def get_status(fd):
print Querying RTS state.
stat = struct.pack('I', 0)
rc = fcntl.ioctl(fd, termios.TIOCMGET, stat)
if struct.unpack('I', rc)[0]  termios.TIOCM_RTS:
print RTS bit is on.
else:
print RTS bit is off.


It seems to me also that RTS is always on after the port has been
opened. I didn't dig out my voltmeter or anything to check this,
though.
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: How to open and read an unknown extension file

2010-04-08 Thread Chris Colbert
On Thu, Apr 8, 2010 at 11:42 AM, Kushal Kumaran
kushal.kumaran+pyt...@gmail.com wrote:
 On Thu, Apr 8, 2010 at 9:00 PM, varnikat t varnika...@gmail.com wrote:
 I am trying to do this
 if os.path.exists(*.*.txt):
             file=open(*.*.txt)
             self.text_view.get_buffer().set_text(file.read())
 else:
             file=open(*.*.html)
             self.text_view.get_buffer().set_text(file.read())

 It gives error *.*.txt not existingThere are two files in the folder
 testing.pnm.txt
 and testing.pnm.html
 How to make it open any name and extension file and read it?


 os.path.exists does not do pattern matching like that.  Take a look at
 the glob module.

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


In [4]: files = [f for f in os.listdir(os.getcwd()) if
os.path.splitext(f)[-1]=='.txt']

In [5]: files
Out[5]:
['pip-log.txt',
 'extended_abstract.txt',
 'testlog.txt',
 'pymazon_error_log.txt',
 'hny.txt']
-- 
http://mail.python.org/mailman/listinfo/python-list


catch clipboard status in gnome

2010-04-08 Thread Tracubik
Hi all,
i'ld like to create an gnome applet in python that, if left-clicked, 
perform a particular operation using the text of the clipboard.

i've found this example:

import pygtk
pygtk.require('2.0')
import gtk

# get the clipboard
clipboard = gtk.clipboard_get()

# read the clipboard text data. you can also read image and
# rich text clipboard data with the
# wait_for_image and wait_for_rich_text methods.
text = clipboard.wait_for_text()

this work with cut and paste direct operation, but it doesn't catch a 
selected text (that can be pasted for example in a document via the middle 
clik of the mouse)

Any hints?
Nico
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: ftp and python

2010-04-08 Thread Anssi Saari
John Nagle na...@animats.com writes:

 In theory, the FTP spec supports three-way transfers, where the
 source, destination, and control can all be on different machines.
 But no modern implementation supports that.

I remember even using that way back when, Unix machines in the 1990s. 

But, server to server transfers are supported even today, since it's
part of the RFC. RFC959 explains how it's done in chapter 5.2. Usually
this is called FXP now. 
http://en.wikipedia.org/wiki/Comparison_of_FTP_client_software lists a
bunch of clients with FXP support. I don't know about doing this with
ftplib, though.
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: fcntl, serial ports and serial signals on RS232.

2010-04-08 Thread Grant Edwards
On 2010-04-07, Max Kotasek mawr...@gmail.com wrote:

 I'm trying to figure out how to parse the responses from fcntl.ioctl()
 calls that modify the serial lines in a way that asserts that the line
 is now changed.

Two comments:

  1) None of the Linux serial drivers I've worked on return line states
 except when you call TIOCMGET.

  2) If the TIOCMBI[S|C] call returned a 'success' value, then the
 line was set to what you requested.

If you want to read back the state that you just wrote, you can call
TIOCMGET, but for the output pins it's always going to return the 
last value that was written.

 For example I may want to drop RTS explicitly, and
 assert that the line has been dropped before returning.

Call TIOCMSET.  If it doesn't return an error, then you're done.

 Here is a brief snippet of code that I've been using to do that, but
 not sure what to do with the returned response:

What returned response?

The only thing that is returned by TIOCMBIS/TIOCMBIC is a status value
of 0 for success and 0 for failure. IIRC, that value is checked by
Python's fcntl.ioctl wrapper and it will raise an exception on
failure.

 Is someone familiar with manipulating serial signals like this in
 python?

Yes.

 Am I even taking the right approach by using the fcntl.ioctl call?

Yes.  When you set/clear RTS or DTR do they not go up/down?

Even if you can't use pyserial, it's a good source for example code.

-- 
Grant Edwards   grant.b.edwardsYow! TONY RANDALL!  Is YOUR
  at   life a PATIO of FUN??
  gmail.com
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: fcntl, serial ports and serial signals on RS232.

2010-04-08 Thread Grant Edwards
On 2010-04-08, Anssi Saari a...@sci.fi wrote:

 It seems to me also that RTS is always on after the port has been
 opened. I didn't dig out my voltmeter or anything to check this,
 though.

IIRC, that's generally true: RTS and DTR are both set to on by the
tty layer's open() handler _if_ the device's useage count was 0.  If
you open an already open port, then the RTS and DTR lines are left
alone.

-- 
Grant Edwards   grant.b.edwardsYow! Someone in DAYTON,
  at   Ohio is selling USED
  gmail.comCARPETS to a SERBO-CROATIAN
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: How to open and read an unknown extension file

2010-04-08 Thread Steven Howe

On 04/08/2010 08:57 AM, Chris Colbert wrote:

On Thu, Apr 8, 2010 at 11:42 AM, Kushal Kumaran
kushal.kumaran+pyt...@gmail.com  wrote:
   

On Thu, Apr 8, 2010 at 9:00 PM, varnikat tvarnika...@gmail.com  wrote:
 

I am trying to do this
if os.path.exists(*.*.txt):
 file=open(*.*.txt)
 self.text_view.get_buffer().set_text(file.read())
else:
 file=open(*.*.html)
 self.text_view.get_buffer().set_text(file.read())

It gives error *.*.txt not existingThere are two files in the folder
testing.pnm.txt
and testing.pnm.html
How to make it open any name and extension file and read it?

   

os.path.exists does not do pattern matching like that.  Take a look at
the glob module.

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

 

In [4]: files = [f for f in os.listdir(os.getcwd()) if
os.path.splitext(f)[-1]=='.txt']

In [5]: files
Out[5]:
['pip-log.txt',
  'extended_abstract.txt',
  'testlog.txt',
  'pymazon_error_log.txt',
  'hny.txt']
   

seems like a lot of work

from glob import glob

mylist = glob( *.txt )
for item in mylist:
print item

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


Generating a rainbow?

2010-04-08 Thread Tobiah
I'm having a difficult time with this.  I want
to display a continuous range of hues using HTML
hex representation (#RRGGBB).  How would I go 
about scanning through the hues in order to
make a rainbow?

Thanks,

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


Re: Py3: Read file with Unicode characters

2010-04-08 Thread Martin v. Loewis
Gnarlodious wrote:
 On Apr 8, 9:14 am, Martin v. Loewis wrote:
 
 When opening the file, you need to specify the file encoding.
 
 OK, I had tried this:
 
 open(path, 'r').read().encode('utf-8')

No, when *opening* the file, you need to specify the encoding:

open(path, 'r', encoding='utf-8').read()

 Sorry, doing Unicode in Py3 has really been a challenge.

That's because you need to re-learn some things.

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


Re: Generating a rainbow?

2010-04-08 Thread Chris Colbert
On Thu, Apr 8, 2010 at 12:46 PM, Tobiah t...@rcsreg.com wrote:
 I'm having a difficult time with this.  I want
 to display a continuous range of hues using HTML
 hex representation (#RRGGBB).  How would I go
 about scanning through the hues in order to
 make a rainbow?

 Thanks,

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


In [43]: possible = []

In [44]: for i in range(2**8):
   : h = hex(i).lstrip('0x')
   : while len(h)  2:
   : h = '0' + h
   : possible.append(h)
   :
   :

In [45]: full = [r + g + b for r in possible for g in possible for b
in possible]

In [46]: len(full)
Out[46]: 16777216

In [47]: 2**24
Out[47]: 16777216
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Generating a rainbow?

2010-04-08 Thread Richard Thomas
On Apr 8, 5:46 pm, Tobiah t...@rcsreg.com wrote:
 I'm having a difficult time with this.  I want
 to display a continuous range of hues using HTML
 hex representation (#RRGGBB).  How would I go
 about scanning through the hues in order to
 make a rainbow?

 Thanks,

 Toby

Look at the colorsys module.

http://docs.python.org/library/colorsys.html?highlight=colorsys#module-colorsys
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Generating a rainbow?

2010-04-08 Thread Tobiah
 Look at the colorsys module.
 
 http://docs.python.org/library/colorsys.html?highlight=colorsys#module-
colorsys

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


How to open and read an unknown extension file

2010-04-08 Thread varnikat t
Hey,
Thanks for the help.it detects now using glob.glob(*.*.txt)
Can u suggest how to open and read file this way?

*if glob.glob(*.*.txt):
file=open(glob.glob(*.*.txt))

self.text_view.get_buffer().set_text(file.read())
else:
file=open(glob.glob(*.*.html))

self.text_view.get_buffer().set_text(file.read())
*

This gives error!!


On Thu, Apr 8, 2010 at 9:12 PM, Kushal Kumaran 
kushal.kumaran+pyt...@gmail.com kushal.kumaran%2bpyt...@gmail.com wrote:

 On Thu, Apr 8, 2010 at 9:00 PM, varnikat t varnika...@gmail.com wrote:
  I am trying to do this
  if os.path.exists(*.*.txt):
  file=open(*.*.txt)
  self.text_view.get_buffer().set_text(file.read())
  else:
  file=open(*.*.html)
  self.text_view.get_buffer().set_text(file.read())
 
  It gives error *.*.txt not existingThere are two files in the folder
  testing.pnm.txt
  and testing.pnm.html
  How to make it open any name and extension file and read it?
 

 os.path.exists does not do pattern matching like that.  Take a look at
 the glob module.

 --
 regards,
 kushal




-- 
Varnika Tewari



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


Re: How to open and read an unknown extension file

2010-04-08 Thread varnikat t
it gives me this error

TypeError: coercing to Unicode: need string or buffer, list found


On Thu, Apr 8, 2010 at 10:48 PM, varnikat t varnika...@gmail.com wrote:





 Hey,
 Thanks for the help.it detects now using glob.glob(*.*.txt)
 Can u suggest how to open and read file this way?

 *if glob.glob(*.*.txt):
 file=open(glob.glob(*.*.txt))

 self.text_view.get_buffer().set_text(file.read())
 else:
 file=open(glob.glob(*.*.html))

 self.text_view.get_buffer().set_text(file.read())
 *

 This gives error!!


 On Thu, Apr 8, 2010 at 9:12 PM, Kushal Kumaran 
 kushal.kumaran+pyt...@gmail.com kushal.kumaran%2bpyt...@gmail.comwrote:

 On Thu, Apr 8, 2010 at 9:00 PM, varnikat t varnika...@gmail.com wrote:
  I am trying to do this
  if os.path.exists(*.*.txt):
  file=open(*.*.txt)
  self.text_view.get_buffer().set_text(file.read())
  else:
  file=open(*.*.html)
  self.text_view.get_buffer().set_text(file.read())
 
  It gives error *.*.txt not existingThere are two files in the folder
  testing.pnm.txt
  and testing.pnm.html
  How to make it open any name and extension file and read it?
 

 os.path.exists does not do pattern matching like that.  Take a look at
 the glob module.

 --
 regards,
 kushal




 --
 Varnika Tewari



 --
 Varnika Tewari




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


Re: Generating a rainbow?

2010-04-08 Thread Gary Herron

Tobiah wrote:

I'm having a difficult time with this.  I want
to display a continuous range of hues using HTML
hex representation (#RRGGBB).  How would I go 
about scanning through the hues in order to

make a rainbow?

Thanks,

Toby
  


Use the hue-saturation-value color space, and call hsv_to_rgb from the 
standard Python library to convert to RGB.  Enjoy!


Gary Herron




from colorsys import hsv_to_rgb

for hue :
 rgb = hsv_to_rgb(hue, saturation, value)


Let 'hue' run from 0 (red) through 2/3 (blue)  Hues from 2/3 to 1 get 
into purples and magentas, which are not spectral (i.e., rainbow) colors.


Set 'saturation' to perhaps 0.5 (for a washed out effect) through 1.0 
(for pure color).  Even an intensely colorful rainbow has lots of white 
light mixed in with it;  a saturation of 0.5 is probably good.


Set 'value' to something in the range of 0 to 1 to control brightness.
--
http://mail.python.org/mailman/listinfo/python-list


Re: Generating a rainbow?

2010-04-08 Thread Chris Colbert
On Thu, Apr 8, 2010 at 1:14 PM, Tobiah t...@rcsreg.com wrote:
 Look at the colorsys module.

 http://docs.python.org/library/colorsys.html?highlight=colorsys#module-
 colorsys

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


How does that answer your original question?
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Py3: Read file with Unicode characters

2010-04-08 Thread Gnarlodious
On Apr 8, 11:04 am, Martin v. Loewis wrote:

 That's because you need to re-learn some things.

Apparently so, every little item is a lesson. Thank you.

-- Gnarlie

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


Re: Replacing Periods with Backspaces

2010-04-08 Thread Gabriel Genellina

En Thu, 08 Apr 2010 12:26:56 -0300, Booter colo.av...@gmail.com escribió:


I am trying to replace a series of periods in a sting with backspaces
that way I can easily parse information from a Windows command.  the
current statement I have for this is

***Statement
capture = re.sub('\.*', '\b', capture)


The * means ZERO or more occurrences - matching practically everywhere.
I think you want \.+ -- one or more occurrences of a literal period. To  
actually have a \ in the string, you have to write it as \\.+ or r\.+



===Out Put===
str: \bi\bp\bc\bo\bn\bf\bi\bg\b \b/\ba\bl\bl\b\n\b\n\bW\bi\bn\bd\bo\bw
\bs\b \bI\bP\b \bC\bo\bn\bf\bi\bg\bu\br\ba\bt\bi\bo\bn\b\n\b\n\b \b \b
==END

which paces a bunch of '\b' strings throughout the string (to me at
random).  It sort of works if I use this command but I don't want
there to be whitespace...


You want those \b, don't you? Just not everywhere...


===Out Put===
str: ipconfig /all\n\nWindows IP Configuration\n\n   Host
Name : Triton\n   Primary Dns Suffix :
engrColoStateEDU\n   Node Type : Hybrid\n   IP Routing
Enabled: No\n   WINS Proxy Enabled: No\n   DNS Suffix
Search List  : engrColoStateEDU
\n   ColoStateEDU\n\nEthernet
adapter Local Area Connection:\n\n   Connection-specific DNS
Suffix   : \n   Description: Realtek PCIe GBE Family
Controller\n   Physical Address : 00-24-1D-16-FF-28\n   DHCP
Enabled   : No\n   Autoconfiguration Enabled : Yes\n
IPv4 Address   : 12982227254(Preferred) \n   Subnet
Mask: 2552552480\n   Default Gateway  :
129822241\n   DNS Servers:
1298210378\n   1298210379\n
NetBIOS over Tcpip: Enabled\n\nTunnel adapter
isatap{04FB4DF5-4B41-4058-A641-6965D13CCC06}:\n\n   Media
State: Media disconnected\n   Connection-specific DNS
Suffix   ...
==END


Except for the last '...' I don't see any '.' in that string to be  
replaced...


--
Gabriel Genellina

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


Why these don't work??

2010-04-08 Thread M. Hamed
I'm trying the following statements that I found here and there on
Google, but none of them works on my Python 2.5, are they too old? or
newer?

abc.reverse()
import numpy
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: pass object or use self.object?

2010-04-08 Thread Tim Arnold
On Apr 8, 4:20 am, Bruno Desthuilliers bruno.
42.desthuilli...@websiteburo.invalid wrote:
 Lie Ryan a écrit :





  On 04/07/10 18:34, Bruno Desthuilliers wrote:
  Lie Ryan a écrit :
  (snip)

  Since in function in python is a first-class object, you can instead do
  something like:

  def process(document):
      # note: document should encapsulate its own logic
      document.do_one_thing()
  Obvious case of encapsulation abuse here. Should a file object
  encapsulate all the csv parsing logic ? (and the html parsing, xml
  parsing, image manipulation etc...) ? Should a model object
  encapsulate the presentation logic ? I could go on for hours here...

  Yes, but no; you're taking it out of context. Is {csv|html|xml|image}
  parsing logic a document's logic? Is presentation a document's logic? If
  they're not, then they do not belong in document.

 Is len() a list logic ? If yes, it should belong to list !-)

 There are two points here : the first is that we (that is, at least, you
 and me) just don't know enough about the OP's project to tell whether
 something should belong to the document or not. period. The second point
 is that objects don't live in a splendid isolation, and it's perfectly
 ok to have code outside an object's method working on the object.

 wrt/ these two points, your document should encapsulate its own logic
 note seems a bit dogmatic (and not necessarily right) to me - hence my
 answer.

The 'document' in this case is an lxml Elementtree, so I think it
makes sense to have code outside the object (e.g. static methods)
working on the object.
thanks,
--Tim
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: How to open and read an unknown extension file

2010-04-08 Thread Kushal Kumaran
On Thu, Apr 8, 2010 at 10:39 PM, varnikat t varnika...@gmail.com wrote:
 Hey,
 Thanks for the help.it detects now using glob.glob(*.*.txt)
 Can u suggest how to open and read file this way?

 if glob.glob(*.*.txt):
             file=open(glob.glob(*.*.txt))
             self.text_view.get_buffer().set_text(file.read())
         else:
             file=open(glob.glob(*.*.html))
             self.text_view.get_buffer().set_text(file.read())

 This gives error!!

glob.glob returns a list of filenames.  You need to loop over it and
pass individual elements to the open function.

for item in glob.glob('*.txt'):
# item is a filename.  pass it to open and process however you need

I don't know how the set_text method works, but it sounds like it
might not work right if you call it repeatedly with different
filenames.

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


Re: Why these don't work??

2010-04-08 Thread nn


M. Hamed wrote:
 I'm trying the following statements that I found here and there on
 Google, but none of them works on my Python 2.5, are they too old? or
 newer?

 abc.reverse()
 import numpy

reverse does not work on strings but does work on lists:
 x=list(abc)
 x.reverse()
 x
['c', 'b', 'a']

or maybe this:
 ''.join(reversed(abc))
'cba'

as far as numpy you need to install it first:

http://pypi.python.org/pypi/numpy/


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


Re: Why these don't work??

2010-04-08 Thread Mark Dickinson
On Apr 8, 7:10 pm, M. Hamed mohammed.elshou...@microchip.com
wrote:
 I'm trying the following statements that I found here and there on
 Google, but none of them works on my Python 2.5, are they too old? or
 newer?

 abc.reverse()

This isn't valid Python in any version that I'm aware of.  Where did
you see it?  It wouldn't make a lot of sense anyway, since by analogy
with list.reverse you'd expect it to reverse the given string in
place.  But that's not possible in Python, because strings are
immutable.

Maybe you're looking for something like:

 reversed(abc)
reversed object at 0x100582810

which works in versions of Python = 2.4.

 import numpy

For this to work, you need to have numpy installed.  numpy is a third-
party package that isn't part of the standard Python distribution;
for more information, see:

http://numpy.scipy.org/

The best method for installing numpy would depend on your system, and
on where you got Python from.  On OS X, the system Python comes with
numpy as standard, for example.  On Linux, there's probably a python26-
numpy package (or something with a similar name) that you can
install.  On Windows:  no idea.  :)

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


Re: Generating a rainbow?

2010-04-08 Thread Tobiah
 How does that answer your original question?

I was able to do this:

import colorsys

sat = 1
value = 1
length = 1000
for x in range(0, length + 1):
hue = x / float(length)
color = list(colorsys.hsv_to_rgb(hue, sat, value))
for x in range(3):
color[x] = int(color[x] * 255)
hexval = (#%02x%02x%02x % tuple(color)).upper()
print div style='height: 1; width: 500; background-color: %s' 
% hexval


http://tobiah.org/rainbow.html
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Why these don't work??

2010-04-08 Thread MRAB

M. Hamed wrote:

I'm trying the following statements that I found here and there on
Google, but none of them works on my Python 2.5, are they too old? or
newer?

abc.reverse()


Lists have a .reverse() method which reverses the list elements
in-place, but strings don't because they're immutable.

There's a built-in function reversed() which returns an iterator over an
iterable object, eg a string:

print reversed(abc)

for c in reversed(abc):
print c

It's all in the documentation.


import numpy


numpy isn't part of the standard library; you'd need to download and
install it.

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


Re: ftp and python

2010-04-08 Thread John Nagle

Anssi Saari wrote:

John Nagle na...@animats.com writes:


In theory, the FTP spec supports three-way transfers, where the
source, destination, and control can all be on different machines.
But no modern implementation supports that.


I remember even using that way back when, Unix machines in the 1990s. 


But, server to server transfers are supported even today, since it's
part of the RFC. RFC959 explains how it's done in chapter 5.2. Usually
this is called FXP now. 
http://en.wikipedia.org/wiki/Comparison_of_FTP_client_software lists a

bunch of clients with FXP support. I don't know about doing this with
ftplib, though.


   Although the protocol allows setting up a 3-way transfer, many
FTP servers disallow data connections to an IP address different
from the control address.  It's a security risk.

   It's useful when you want to move data between machines with high
bandwidth connections, as within a server farm, and the control machine
has less bandwidth.  But there are more modern approaches for that.

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


regex help: splitting string gets weird groups

2010-04-08 Thread gry
[ python3.1.1, re.__version__='2.2.1' ]
I'm trying to use re to split a string into (any number of) pieces of
these kinds:
1) contiguous runs of letters
2) contiguous runs of digits
3) single other characters

e.g.   555tHe-rain.in#=1234   should give:   [555, 'tHe', '-', 'rain',
'.', 'in', '#', '=', 1234]
I tried:
 re.match('^(([A-Za-z]+)|([0-9]+)|([-.#=]))+$', 
 '555tHe-rain.in#=1234').groups()
('1234', 'in', '1234', '=')

Why is 1234 repeated in two groups?  and why doesn't tHe appear as a
group?  Is my regexp illegal somehow and confusing the engine?

I *would* like to understand what's wrong with this regex, though if
someone has a neat other way to do the above task, I'm also interested
in suggestions.
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: regex help: splitting string gets weird groups

2010-04-08 Thread MRAB

gry wrote:

[ python3.1.1, re.__version__='2.2.1' ]
I'm trying to use re to split a string into (any number of) pieces of
these kinds:
1) contiguous runs of letters
2) contiguous runs of digits
3) single other characters

e.g.   555tHe-rain.in#=1234   should give:   [555, 'tHe', '-', 'rain',
'.', 'in', '#', '=', 1234]
I tried:

re.match('^(([A-Za-z]+)|([0-9]+)|([-.#=]))+$', '555tHe-rain.in#=1234').groups()

('1234', 'in', '1234', '=')

Why is 1234 repeated in two groups?  and why doesn't tHe appear as a
group?  Is my regexp illegal somehow and confusing the engine?

I *would* like to understand what's wrong with this regex, though if
someone has a neat other way to do the above task, I'm also interested
in suggestions.


If the regex was illegal then it would raise an exception. It's doing
exactly what you're asking it to do!

First of all, there are 4 groups, with group 1 containing groups 2..4 as
alternatives, so group 1 will match whatever groups 2..4 match:

Group 1: (([A-Za-z]+)|([0-9]+)|([-.#=]))
Group 2: ([A-Za-z]+)
Group 3: ([0-9]+)
Group 4: ([-.#=])

It matches like this:

Group 1 and group 3 match '555'.
Group 1 and group 2 match 'tHe'.
Group 1 and group 4 match '-'.
Group 1 and group 2 match 'rain'.
Group 1 and group 4 match '.'.
Group 1 and group 2 match 'in'.
Group 1 and group 4 match '#'.
Group 1 and group 4 match '='.
Group 1 and group 3 match '1234'.

If a group matches then any earlier match of that group is discarded,
so:

Group 1 finishes with '1234'.
Group 2 finishes with 'in'.
Group 3 finishes with '1234'.
Group 4 finishes with '='.

A solution is:

 re.findall('[A-Za-z]+|[0-9]+|[-.#=]', '555tHe-rain.in#=1234')
['555', 'tHe', '-', 'rain', '.', 'in', '#', '=', '1234']

Note: re.findall() returns a list of matches, so if the regex doesn't
contain any groups then it returns the matched substrings. Compare:

 re.findall(a(.), ax ay)
['x', 'y']
 re.findall(a., ax ay)
['ax', 'ay']
--
http://mail.python.org/mailman/listinfo/python-list


Re: regex help: splitting string gets weird groups

2010-04-08 Thread Jon Clements
On 8 Apr, 19:49, gry georgeryo...@gmail.com wrote:
 [ python3.1.1, re.__version__='2.2.1' ]
 I'm trying to use re to split a string into (any number of) pieces of
 these kinds:
 1) contiguous runs of letters
 2) contiguous runs of digits
 3) single other characters

 e.g.   555tHe-rain.in#=1234   should give:   [555, 'tHe', '-', 'rain',
 '.', 'in', '#', '=', 1234]
 I tried: re.match('^(([A-Za-z]+)|([0-9]+)|([-.#=]))+$', 
 '555tHe-rain.in#=1234').groups()

 ('1234', 'in', '1234', '=')

 Why is 1234 repeated in two groups?  and why doesn't tHe appear as a
 group?  Is my regexp illegal somehow and confusing the engine?

 I *would* like to understand what's wrong with this regex, though if
 someone has a neat other way to do the above task, I'm also interested
 in suggestions.

I would avoid .match and use .findall
(if you walk through them both together, it'll make sense what's
happening
with your match string).

 s = 555tHe-rain.in#=1234
 re.findall('[A-Za-z]+|[0-9]+|[-.#=]', s)
['555', 'tHe', '-', 'rain', '.', 'in', '#', '=', '1234']

hth,

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


Re: regex help: splitting string gets weird groups

2010-04-08 Thread Patrick Maupin
On Apr 8, 1:49 pm, gry georgeryo...@gmail.com wrote:
 [ python3.1.1, re.__version__='2.2.1' ]
 I'm trying to use re to split a string into (any number of) pieces of
 these kinds:
 1) contiguous runs of letters
 2) contiguous runs of digits
 3) single other characters

 e.g.   555tHe-rain.in#=1234   should give:   [555, 'tHe', '-', 'rain',
 '.', 'in', '#', '=', 1234]
 I tried: re.match('^(([A-Za-z]+)|([0-9]+)|([-.#=]))+$', 
 '555tHe-rain.in#=1234').groups()

 ('1234', 'in', '1234', '=')

 Why is 1234 repeated in two groups?  and why doesn't tHe appear as a
 group?  Is my regexp illegal somehow and confusing the engine?

 I *would* like to understand what's wrong with this regex, though if
 someone has a neat other way to do the above task, I'm also interested
 in suggestions.

IMO, for most purposes, for people who don't want to become re
experts, the easiest, fastest, best, most predictable way to use re is
re.split.  You can either call re.split directly, or, if you are going
to be splitting on the same pattern over and over, compile the pattern
and grab its split method.  Use a *single* capture group in the
pattern, that covers the *whole* pattern.  In the case of your example
data:

 import re
 splitter=re.compile('([A-Za-z]+|[0-9]+|[-.#=])').split
 s='555tHe-rain.in#=1234'
 [x for x in splitter(s) if x]
['555', 'tHe', '-', 'rain', '.', 'in', '#', '=', '1234']

The reason for the list comprehension is that re.split will always
return a non-matching string between matches.  Sometimes this is
useful even when it is a null string (see recent discussion in the
group about splitting digits out of a string), but if you don't care
to see null (empty) strings, this comprehension will remove them.

The reason for a single capture group that covers the whole pattern is
that it is much easier to reason about the output.  The split will
give you all your data, in order, e.g.

 ''.join(splitter(s)) == s
True

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


Re: regex help: splitting string gets weird groups

2010-04-08 Thread Tim Chase

gry wrote:

[ python3.1.1, re.__version__='2.2.1' ]
I'm trying to use re to split a string into (any number of) pieces of
these kinds:
1) contiguous runs of letters
2) contiguous runs of digits
3) single other characters

e.g.   555tHe-rain.in#=1234   should give:   [555, 'tHe', '-', 'rain',
'.', 'in', '#', '=', 1234]
I tried:

re.match('^(([A-Za-z]+)|([0-9]+)|([-.#=]))+$', '555tHe-rain.in#=1234').groups()

('1234', 'in', '1234', '=')

Why is 1234 repeated in two groups?  and why doesn't tHe appear as a
group?  Is my regexp illegal somehow and confusing the engine?


well, I'm not sure what it thinks its finding but nested capture-groups 
always produce somewhat weird results for me (I suspect that's what's 
triggering the duplication).  Additionally, you're only searching for 
one match (.match() returns a single match-object or None; not all 
possible matches within the repeated super-group).



I *would* like to understand what's wrong with this regex, though if
someone has a neat other way to do the above task, I'm also interested
in suggestions.


Tweaking your original, I used

   s='555tHe-rain.in#=1234'
   import re
   r=re.compile(r'([a-zA-Z]+|\d+|.)')
   r.findall(s)
  ['555', 'tHe', '-', 'rain', '.', 'in', '#', '=', '1234']

The only difference between my results and your results is that the 555 
and 1234 come back as strings, not ints.


-tkc




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


Re: Why these don't work??

2010-04-08 Thread M. Hamed
Thanks All. That clears alot of confusion. It seems I assumed that
everything that works for lists works for strings (the immutable vs
mutable hasn't sunken in yet).

On the other hand (other than installing NumPy) is there a built-in
way to do an array full of zeros or one just like the numpy.zeros()? I
know I can do it with list comprehension (like [0 for i in
range(0,20)] but these are too many keystrokes for python :) I was
wondering if there is a simpler way.

I had another question about arrays but I should probably start
another thread.

Regards,

On Apr 8, 11:43 am, MRAB pyt...@mrabarnett.plus.com wrote:
 M. Hamed wrote:
  I'm trying the following statements that I found here and there on
  Google, but none of them works on my Python 2.5, are they too old? or
  newer?

  abc.reverse()

 Lists have a .reverse() method which reverses the list elements
 in-place, but strings don't because they're immutable.

 There's a built-in function reversed() which returns an iterator over an
 iterable object, eg a string:

      print reversed(abc)

      for c in reversed(abc):
          print c

 It's all in the documentation.

  import numpy

 numpy isn't part of the standard library; you'd need to download and
 install it.

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


Dynamically growing an array to implement a stack

2010-04-08 Thread M. Hamed
I have trouble with some Python concept. The fact that you can not
assign to a non-existent index in an array. For example:

a = [0,1]
a[2] = Generates an error

I can use a.append(2) but that always appends to the end. Sometimes I
want to use this array as a stack and hence my indexing logic would be
something like:

If you are already at the end (based on your stack pointer):
  use append() then index (and inc your pointer)
if not:
  index directly (and inc your stack pointer)

If feel that doing this everytime I want to add an element that I have
to check whether it exists or not is too much. Is there any simpler
way to do this?

I know I can do something like this:

a = numpy.zeros(MAX_STACK_SIZE)

but I don't want to predetermine the stack size.

Any help?

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


Re: Why these don't work??

2010-04-08 Thread Emile van Sebille

On 4/8/2010 1:08 PM M. Hamed said...

On the other hand (other than installing NumPy) is there a built-in
way to do an array full of zeros or one just like the numpy.zeros()? I
know I can do it with list comprehension (like [0 for i in
range(0,20)] but these are too many keystrokes for python :) I was
wondering if there is a simpler way.


map(lambda _:0, range(20))

Emile

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


Re: Dynamically growing an array to implement a stack

2010-04-08 Thread Paul McGuire
On Apr 8, 3:21 pm, M. Hamed mhels...@hotmail.com wrote:
 I have trouble with some Python concept. The fact that you can not
 assign to a non-existent index in an array. For example:

 a = [0,1]
 a[2] = Generates an error

 I can use a.append(2) but that always appends to the end. Sometimes I
 want to use this array as a stack and hence my indexing logic would be
 something like:

 If you are already at the end (based on your stack pointer):
       use append() then index (and inc your pointer)
 if not:
       index directly (and inc your stack pointer)

??? The stack pointer is *always* at the end, except don't actually
keep a real stack pointer, let list do it for you.  Call append to
push a value onto the end, and pop to pull it off.  Or if you are
really stuck on push/pop commands for a stack, do this:

 class stack(list):
...   push = list.append
...
 ss = stack()
 ss.push(x)
 ss.push(Y)
 ss
['x', 'Y']
 ss.pop()
'Y'
 ss.pop()
'x'
 ss.pop()
Traceback (most recent call last):
  File stdin, line 1, in module
IndexError: pop from empty list

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


Re: Dynamically growing an array to implement a stack

2010-04-08 Thread Patrick Maupin
On Apr 8, 3:21 pm, M. Hamed mhels...@hotmail.com wrote:
 I have trouble with some Python concept. The fact that you can not
 assign to a non-existent index in an array. For example:

 a = [0,1]
 a[2] = Generates an error

 I can use a.append(2) but that always appends to the end. Sometimes I
 want to use this array as a stack and hence my indexing logic would be
 something like:

 If you are already at the end (based on your stack pointer):
       use append() then index (and inc your pointer)
 if not:
       index directly (and inc your stack pointer)

 If feel that doing this everytime I want to add an element that I have
 to check whether it exists or not is too much. Is there any simpler
 way to do this?

 I know I can do something like this:

 a = numpy.zeros(MAX_STACK_SIZE)

 but I don't want to predetermine the stack size.

 Any help?

 Thanks

Well, if you never want to add intermediate data between your new
element and the stack, you can just do:

stack[index:index + 1] = [newelement]

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


Re: Recommend Commercial graphing library

2010-04-08 Thread Giacomo Boffi
Grant Edwards inva...@invalid.invalid writes:

 If it's 2D data, you don't need to use a 3D graph.

if it's tabular data, you don't need an uber-histogram
-- 
giampippetto, coso, come si chiama? ah si` MMAX ha scritto:
 Tra il trascendente e l'interpretazione prevalente del dato come
 assioma ne passa...
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Why these don't work??

2010-04-08 Thread Robert Kern

On 2010-04-08 15:08 PM, M. Hamed wrote:


On the other hand (other than installing NumPy) is there a built-in
way to do an array full of zeros or one just like the numpy.zeros()? I
know I can do it with list comprehension (like [0 for i in
range(0,20)] but these are too many keystrokes for python :) I was
wondering if there is a simpler way.


[0] * n

Of course, you should keep in mind that you shouldn't always be looking for 
concise built-in expressions to do things. Or rather, you shouldn't be 
disappointed if you don't find them. Almost always, the best solution is to wrap 
up the ugly code into a function that you can then call everywhere. So even if 
you were stuck with the list comprehension, you should have just defined your 
own zeros() function that did the job and use it everywhere.


--
Robert Kern

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

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


Re: regex help: splitting string gets weird groups

2010-04-08 Thread gry
On Apr 8, 3:40 pm, MRAB pyt...@mrabarnett.plus.com wrote:

...
 Group 1 and group 4 match '='.
 Group 1 and group 3 match '1234'.

 If a group matches then any earlier match of that group is discarded,
Wow, that makes this much clearer!  I wonder if this behaviour
shouldn't be mentioned in some form in the python docs?
Thanks much!

 so:

 Group 1 finishes with '1234'.
 Group 2 finishes with 'in'.
 Group 3 finishes with '1234'.

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


Re: regex help: splitting string gets weird groups

2010-04-08 Thread Jon Clements
On 8 Apr, 19:49, gry georgeryo...@gmail.com wrote:
 [ python3.1.1, re.__version__='2.2.1' ]
 I'm trying to use re to split a string into (any number of) pieces of
 these kinds:
 1) contiguous runs of letters
 2) contiguous runs of digits
 3) single other characters

 e.g.   555tHe-rain.in#=1234   should give:   [555, 'tHe', '-', 'rain',
 '.', 'in', '#', '=', 1234]
 I tried: re.match('^(([A-Za-z]+)|([0-9]+)|([-.#=]))+$', 
 '555tHe-rain.in#=1234').groups()

 ('1234', 'in', '1234', '=')

 Why is 1234 repeated in two groups?  and why doesn't tHe appear as a
 group?  Is my regexp illegal somehow and confusing the engine?

 I *would* like to understand what's wrong with this regex, though if
 someone has a neat other way to do the above task, I'm also interested
 in suggestions.

Avoiding re's (for a bit of fun):
(no good for unicode obviously)

import string
from itertools import groupby, chain, repeat, count, izip

s = 555tHe-rain.in#=1234

unique_group = count()
lookup = dict(
chain(
izip(string.ascii_letters, repeat('L')),
izip(string.digits, repeat('D')),
izip(string.punctuation, unique_group)
)
)
parse = dict(D=int, L=str.capitalize)


print [ parse.get(key, lambda L: L)(''.join(items)) for key, items in
groupby(s, lambda L: lookup[L]) ]
[555, 'The', '-', 'Rain', '.', 'In', '#', '=', 1234]

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


Re: Why these don't work??

2010-04-08 Thread Joaquin Abian
On Apr 8, 10:08 pm, M. Hamed mohammed.elshou...@microchip.com
wrote:
 Thanks All. That clears alot of confusion. It seems I assumed that
 everything that works for lists works for strings (the immutable vs
 mutable hasn't sunken in yet).

 On the other hand (other than installing NumPy) is there a built-in
 way to do an array full of zeros or one just like the numpy.zeros()? I
 know I can do it with list comprehension (like [0 for i in
 range(0,20)] but these are too many keystrokes for python :) I was
 wondering if there is a simpler way.

 I had another question about arrays but I should probably start
 another thread.

 Regards,

 On Apr 8, 11:43 am, MRAB pyt...@mrabarnett.plus.com wrote:

  M. Hamed wrote:
   I'm trying the following statements that I found here and there on
   Google, but none of them works on my Python 2.5, are they too old? or
   newer?

   abc.reverse()

  Lists have a .reverse() method which reverses the list elements
  in-place, but strings don't because they're immutable.

  There's a built-in function reversed() which returns an iterator over an
  iterable object, eg a string:

       print reversed(abc)

       for c in reversed(abc):
           print c

  It's all in the documentation.

   import numpy

  numpy isn't part of the standard library; you'd need to download and
  install it.



if you want an array you can get it from module array

 import array
 array.array('i', [0]*100)
array('i', [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0])

if you want simply  a list:
 [0] * 100
yields a list of hundred zeros

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


Re: python as pen and paper substitute

2010-04-08 Thread Giacomo Boffi
Manuel Graune manuel.gra...@koeln.de writes:

 Hello everyone,

 I am looking for ways to use a python file as a substitute for simple
 pen and paper calculations.

search(embedded calc mode) if manuel in emacs_fellows_set or sys.exit(1)
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Generating a rainbow?

2010-04-08 Thread Peter Parker

Tobiah wrote:

How does that answer your original question?


I was able to do this:

import colorsys

sat = 1
value = 1
length = 1000
for x in range(0, length + 1):
hue = x / float(length)
color = list(colorsys.hsv_to_rgb(hue, sat, value))
for x in range(3):
color[x] = int(color[x] * 255)
hexval = (#%02x%02x%02x % tuple(color)).upper()
print div style='height: 1; width: 500; background-color: %s' 
% hexval



http://tobiah.org/rainbow.html


Roy G. Biv would like you to order the colors according to their
wavelength. He would also like to see Orange and Yellow appear in
your rainbow.
--
http://mail.python.org/mailman/listinfo/python-list


Re: regex help: splitting string gets weird groups

2010-04-08 Thread gry
     s='555tHe-rain.in#=1234'
     import re
     r=re.compile(r'([a-zA-Z]+|\d+|.)')
     r.findall(s)
    ['555', 'tHe', '-', 'rain', '.', 'in', '#', '=', '1234']
This is nice and simple and has the invertible property that Patrick
mentioned above.  Thanks much!
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: python as pen and paper substitute

2010-04-08 Thread Manuel Graune
Giacomo Boffi giacomo.bo...@polimi.it writes:

 Manuel Graune manuel.gra...@koeln.de writes:

 Hello everyone,

 I am looking for ways to use a python file as a substitute for simple
 pen and paper calculations.

 search(embedded calc mode) if manuel in emacs_fellows_set or sys.exit(1)

Well, the subject does say python and not elisp, but I'm a vim-user
anyways. 

*duckandrun*


-- 
A hundred men did the rational thing. The sum of those rational choices was
called panic. Neal Stephenson -- System of the world
http://www.graune.org/GnuPG_pubkey.asc
Key fingerprint = 1E44 9CBD DEE4 9E07 5E0A  5828 5476 7E92 2DB4 3C99
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Dynamically growing an array to implement a stack

2010-04-08 Thread M. Hamed
Thanks Patrick, that is what I was exactly looking for.

Paul, thanks for your example. wasn't familiar with the stack class. I
feel Patrick's method is a lot simpler for my purpose.

Regards.

On Apr 8, 1:29 pm, Patrick Maupin pmau...@gmail.com wrote:
 On Apr 8, 3:21 pm, M. Hamed mhels...@hotmail.com wrote:



  I have trouble with some Python concept. The fact that you can not
  assign to a non-existent index in an array. For example:

  a = [0,1]
  a[2] = Generates an error

  I can use a.append(2) but that always appends to the end. Sometimes I
  want to use this array as a stack and hence my indexing logic would be
  something like:

  If you are already at the end (based on your stack pointer):
        use append() then index (and inc your pointer)
  if not:
        index directly (and inc your stack pointer)

  If feel that doing this everytime I want to add an element that I have
  to check whether it exists or not is too much. Is there any simpler
  way to do this?

  I know I can do something like this:

  a = numpy.zeros(MAX_STACK_SIZE)

  but I don't want to predetermine the stack size.

  Any help?

  Thanks

 Well, if you never want to add intermediate data between your new
 element and the stack, you can just do:

 stack[index:index + 1] = [newelement]

 Regards,
 Pat

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


Re: Why these don't work??

2010-04-08 Thread M. Hamed
OMG! That's beautiful! I loved the [0]*n how simple and how it never
occurred to me!

Robert I agree with your comment. I feel though that since I'm not
very experienced yet with Python, it's useful to learn about all those
simple yet powerful methods so I can use them when I really need them.
Plus it gives me more justification for the time I invested learning a
new language (and glad I did), and more reasons to dump Perl forever!

Thanks for all the suggestions.

On Apr 8, 1:37 pm, Joaquin Abian gatoyga...@gmail.com wrote:
 On Apr 8, 10:08 pm, M. Hamed mohammed.elshou...@microchip.com
 wrote:



  Thanks All. That clears alot of confusion. It seems I assumed that
  everything that works for lists works for strings (the immutable vs
  mutable hasn't sunken in yet).

  On the other hand (other than installing NumPy) is there a built-in
  way to do an array full of zeros or one just like the numpy.zeros()? I
  know I can do it with list comprehension (like [0 for i in
  range(0,20)] but these are too many keystrokes for python :) I was
  wondering if there is a simpler way.

  I had another question about arrays but I should probably start
  another thread.

  Regards,

  On Apr 8, 11:43 am, MRAB pyt...@mrabarnett.plus.com wrote:

   M. Hamed wrote:
I'm trying the following statements that I found here and there on
Google, but none of them works on my Python 2.5, are they too old? or
newer?

abc.reverse()

   Lists have a .reverse() method which reverses the list elements
   in-place, but strings don't because they're immutable.

   There's a built-in function reversed() which returns an iterator over an
   iterable object, eg a string:

        print reversed(abc)

        for c in reversed(abc):
            print c

   It's all in the documentation.

import numpy

   numpy isn't part of the standard library; you'd need to download and
   install it.

 if you want an array you can get it from module array

  import array
  array.array('i', [0]*100)

 array('i', [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0])

 if you want simply  a list: [0] * 100

 yields a list of hundred zeros

 cheers
 joaquin

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


Re: regex help: splitting string gets weird groups

2010-04-08 Thread Patrick Maupin
On Apr 8, 3:40 pm, gry georgeryo...@gmail.com wrote:
      s='555tHe-rain.in#=1234'
      import re
      r=re.compile(r'([a-zA-Z]+|\d+|.)')
      r.findall(s)
     ['555', 'tHe', '-', 'rain', '.', 'in', '#', '=', '1234']

 This is nice and simple and has the invertible property that Patrick
 mentioned above.  Thanks much!

Yes, like using split(), this is invertible.  But you will see a
difference (and for a given task, you might prefer one way or the
other) if, for example, you put a few consecutive spaces in the middle
of your string, where this pattern and findall() will return each
space individually, and split() will return them all together.

You *can* fix up the pattern for findall() where it will have the same
properties as the split(), but it will almost always be a more
complicated pattern than for the equivalent split().

Another thing you can do with split(): if you *think* you have a
pattern that fully covers every string you expect to throw at it, but
would like to verify this, you can make use of the fact that split()
returns a string between each match (and before the first match and
after the last match).  So if you expect that every character in your
entire string should be a part of a match, you can do something like:

strings = splitter(s)
tokens = strings[1::2]
assert not ''.join(strings[::2])

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


Re: The Regex Story

2010-04-08 Thread Lie Ryan
On 4/9/10, Tim Chase python.l...@tim.thechases.com wrote:
 Lie Ryan wrote:
 Why am I seeing a lot of this pattern lately:

 OP: Got problem with string
 +- A: Suggested a regex-based solution
+- B: Quoted Some people ... regex ... two problems.

 or

 OP: Writes some regex, found problem
 +- A: Quoted Some people ... regex ... two problems.
+- B: Supplied regex-based solution, clean one
   +- A: Suggested PyParsing (or similar)

 There's a spectrum of parsing solutions:

snip

 The above dialog tends to appear when the task isn't in the
 sweet-spot of regexps.  Either it's sufficiently simple that
 simple split/slice notation will do, or (at the other end of the
 spectrum) the effort to get it working with a regexp is hairy and
 convoluted, worthy of a more readable solution implemented with
 pyparsing.  The problem comes from people thinking that regexps
 are the right solution to *every* problem...often demonstrated by
 the OP writing how do I write a regexp to solve this
 non-regexp-optimal problem assuming regexps are the right tool
 for everything.

 There are some problem-classes for which regexps are the *right*
 solution, and I don't see as much of your example dialog in those
 cases.

I would have agreed with you if someone were to make the statement
until a few weeks ago; somehow in the last week or so, the mood about
regex seems to has shifted to regex is not suitable for anything
type of mood. As soon as someone (OP or not) proposed a regex
solution, someone else would retort with don't use regex use
string-builtins or pyparsing. It appears that the group has developed
some sense of regexphobia; some people pushes using string builtins
for moderately complex requirement and suggested pyparsing for not-so
complex need and that keeps shrinking regex sweet spot. But that's
just my inherently subjective observation.
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Generating a rainbow?

2010-04-08 Thread Neil Hodgson
Tobiah:

 for x in range(0, length + 1):
 ...
 for x in range(3):

   You should use different variables for the two loops.

   Neil

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


Re: Generating a rainbow?

2010-04-08 Thread Neil Hodgson
Me:
You should use different variables for the two loops.

   Actually it is closing the divs that makes it work in FireFox:

import colorsys

sat = 1
value = 1
length = 1000
for h in range(0, length + 1):
hue = h / float(length)
color = list(colorsys.hsv_to_rgb(hue, sat, value))
for x in range(3):
color[x] = int(color[x] * 255)
hexval = (#%02x%02x%02x % tuple(color)).upper()
print(
div style='height: 1; width: 500; background-color: %s'
/div % hexval)

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


Re: Generating a rainbow?

2010-04-08 Thread Chris Colbert
On Thu, Apr 8, 2010 at 2:34 PM, Tobiah t...@rcsreg.com wrote:
 How does that answer your original question?

 I was able to do this:

 import colorsys

 sat = 1
 value = 1
 length = 1000
 for x in range(0, length + 1):
        hue = x / float(length)
        color = list(colorsys.hsv_to_rgb(hue, sat, value))
        for x in range(3):
                color[x] = int(color[x] * 255)
        hexval = (#%02x%02x%02x % tuple(color)).upper()
        print div style='height: 1; width: 500; background-color: %s'
 % hexval


 http://tobiah.org/rainbow.html
 --
 http://mail.python.org/mailman/listinfo/python-list


I see. It appears I misunderstood the question.

The link doesn't work in Chrome btw.
-- 
http://mail.python.org/mailman/listinfo/python-list


Pythonic list reordering

2010-04-08 Thread Ben Racine
I have a list...

['dir_0_error.dat', 'dir_120_error.dat', 'dir_30_error.dat', 
'dir_330_error.dat']

I want to sort it based upon the numerical value only.

Does someone have an elegant solution to this? 

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


Re: Pythonic list reordering

2010-04-08 Thread Chris Rebert
On Thu, Apr 8, 2010 at 3:52 PM, Ben Racine i3enha...@gmail.com wrote:
 I have a list...

 ['dir_0_error.dat', 'dir_120_error.dat', 'dir_30_error.dat', 
 'dir_330_error.dat']

 I want to sort it based upon the numerical value only.

a = ['dir_0_error.dat', 'dir_120_error.dat', 'dir_30_error.dat',
'dir_330_error.dat']
def key(item):
return int(item.split('_')[1])
a.sort(key=key)

Cheers,
Chris
--
http://blog.rebertia.com
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Pythonic list reordering

2010-04-08 Thread Gary Herron

Ben Racine wrote:

I have a list...

['dir_0_error.dat', 'dir_120_error.dat', 'dir_30_error.dat', 
'dir_330_error.dat']

I want to sort it based upon the numerical value only.

Does someone have an elegant solution to this? 


Thanks,
Ben R.
  


How about a one liner?

   L.sort(key=lambda s: int(s.split('_')[1]))


(Which is not necessarily elegant, but it is short.)

Gary Herron

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


Re: Pythonic list reordering

2010-04-08 Thread Joaquin Abian
On Apr 9, 12:52 am, Ben Racine i3enha...@gmail.com wrote:
 I have a list...

 ['dir_0_error.dat', 'dir_120_error.dat', 'dir_30_error.dat', 
 'dir_330_error.dat']

 I want to sort it based upon the numerical value only.

 Does someone have an elegant solution to this?

 Thanks,
 Ben R.

not sure about elegance, but my two cents:

 mylist = ['dir_0_error.dat', 'dir_120_error.dat', 'dir_30_error.dat', 
 'dir_330_error.dat']
 mylist = [(int(item.split('_')[1]), item) for item in mylist]
 mylist.sort()
 mylist = [item for idx, item in mylist]
 mylist

['dir_0_error.dat', 'dir_30_error.dat', 'dir_120_error.dat',
'dir_330_error.dat']

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


  1   2   3   >