Re: Behavior of re.split on empty strings is unexpected

2010-08-05 Thread jhermann
On Aug 2, 7:34 pm, John Nagle na...@animats.com wrote:
   s2 =    HELLO   THERE  
   kresplit4 = re.compile(r'\W+', re.UNICODE)
   kresplit4.split(s2)
 ['', 'HELLO', 'THERE', '']

 I still get empty strings.

 re.findall(r\w+,  a b c )
['a', 'b', 'c']

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


Re: Your beloved python features

2010-02-11 Thread jhermann
$ python -c import this
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Create object from variable indirect reference?

2009-11-17 Thread jhermann
On 10 Nov., 17:03, NickC reply...@works.fine.invalid wrote:
 Many thanks for the replies.  getattr() works great:

You can get a little more versatile and even specify the location of
the name (i.e. the module / package name) without pre-importing it,
like this...

def importName(modulename, name=None):
 Import identifier C{name} from module C{modulename}.

If name is omitted, modulename must contain the name after the
module path, delimited by a colon.

@param modulename: Fully qualified module name, e.g. C{x.y.z}.
@param name: Name to import from C{modulename}.
@return: Requested object.
@rtype: object

if name is None:
modulename, name = modulename.split(':', 1)
module = __import__(modulename, globals(), {}, [name])
return getattr(module, name)

print importName(socket:gethostname)()


This is especially useful if you want to specify factory classes or
the like in a non-python config file. The syntax is the same as that
of setuptools entry points.

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


Re: a splitting headache

2009-10-26 Thread jhermann
On 16 Okt., 02:18, Mensanator mensana...@aol.com wrote:
 All I wanted to do is split a binary number into two lists,
 a list of blocks of consecutive ones and another list of
 blocks of consecutive zeroes.

Back to the OP's problem, the obvious (if you know the std lib) and
easy solution is:

 c = '00101000101'
 filter(None, re.split((1+), c))
['00', '1', '0', '1', '0', '', '00', '1', '0', '1']

In production code, you compile the regex once, of course.
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Q on explicitly calling file.close

2009-09-23 Thread jhermann
     except:
         return 0

So wrong on so many levels...
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Python 3.0 crashes displaying Unicode at interactive prompt

2008-12-17 Thread jhermann
Assuming those survived the switch to 3.0, you can use site.py und
sys.displayhook to customize to the old behaviour (i.e. change it to a
version using ascii instead of repr). Since this only affects
interactive use, it's also no problem for portability of code, unlike
solutions like forcing the defaultencoding etc.
--
http://mail.python.org/mailman/listinfo/python-list


Re: Problem with sqlite3 cursor and imbricated for loop

2008-11-19 Thread jhermann
c.execute(select * from stocks)
for s in list(c):
   print s[0]
   c.execute(select * from stocks where price20)
   for sp in c:
  print '  '+sp[0]
c.close()

The simple addition of list() should do away with the dependency on
mysql's implementation, since it forces the instant fetch of all data.
Whether that is better than to use a 2nd cursor is probably a matter
of taste.

Using * in selects is always bad, though. ;)
--
http://mail.python.org/mailman/listinfo/python-list


Re: Identifying unicode punctuation characters with Python regex

2008-11-19 Thread jhermann
  P=P.replace('\\','').replace(']','\\]')   # escape both of them.

re.escape() does this w/o any assumptions by your code about the regex
implementation.
--
http://mail.python.org/mailman/listinfo/python-list


Re: unittest exits

2008-11-19 Thread jhermann
On 13 Nov., 20:20, Chris Rebert [EMAIL PROTECTED] wrote:
 try:
     unittest.main()
 except SystemExit:
     pass

You most probably want this instead:

try:
unittest.main()
except SystemExit, exc:
# only exit if tests failed
if exc.code:
raise
--
http://mail.python.org/mailman/listinfo/python-list


Re: closures and dynamic binding

2008-10-01 Thread jhermann
I didn't see this mentioned in the thread yet: the double-lambda is
unnecessary (and a hack). What you should do when you need early
binding is... early binding. ;)

Namely:

f = [lambda n=n: n for n in range(10)]
print f[0]()
print f[1]()

Note the n=n, this prints 0 and 1 instead of 9/9.
--
http://mail.python.org/mailman/listinfo/python-list


Re: Eggs, VirtualEnv, and Apt - best practices?

2008-10-01 Thread jhermann
Our solution consists of:
 * our own base python distribution, decoupled from the OS one (for
various reasons, one being version independency)
 * distutils / setuptools / virtualenv is included in that python
installation, no other eggs installed in site-packages
 * virtualenv + Paver to manage build environments
 * Paver plugins containing standard (continuous) build targets
 * a hand-crafted tool that builds an RPM (.deb would be easy, too)
from an egg URL / filename, packaging a ready-to-run virtualenv
environment into the RPM; it's a rather shallow shell above virtualenv
and rpmbuild, automating the process and enforcing company standards.
--
http://mail.python.org/mailman/listinfo/python-list


Re: An idiom for code generation with exec

2008-06-25 Thread jhermann
Since nobody mentioned textwrap.dedent yet as an alternative to the
old if 1: trick, I thought I should do so. :)
--
http://mail.python.org/mailman/listinfo/python-list