Re: Questions on XML

2009-08-22 Thread Emmanuel Surleau
On Saturday 22 August 2009 08:13:33 joy99 wrote:
> On Aug 22, 10:53 am, Stefan Behnel  wrote:
> > Rami Chowdhury wrote:
> > >> I am using primarily UTF-8 based strings, like Hindi or Bengali. Can I
> > >> use Python to help me in this regard?
> > >
> > > I can say from experience that Python on Windows (at least, Python 2.5
> > > on 32-bit Vista) works perfectly well with UTF-8 files containing
> > > Bangla. I have had trouble with working with the data in IDLE, however,
> > > which seems to prefer ASCII by default.
> >
> > Defaults almost never work for encodings. You have to be explicit: add an
> > encoding declaration to the top of your source file if you use encoded
> > literal strings in your code; use the codecs module with a suitable
> > encoding to read encoded text files, and use an XML parser when reading
> > XML.
> >
> > Stefan
>
> Dear Group,
> Thanx for your reply. Python works perfectly for Hindi and Bangla with
> Win XP. I never had a trouble.
> Best Regards,
> Subhabrata.

You might also want to have a look at lxml. It can much more than the XML 
module in the default distribution, uses ElementTree as well, and is backed by 
the kickass, fast libxml library (http://codespeak.net/lxml/). It will allow 
you to use XSLs, for instance. Regardless of whether you use lxml or not, have 
a look at etree.iterparse, it is invaluable when processing huge XML 
documents.

Cheers,

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


Re: convert date time

2009-08-22 Thread Kushal Kumaran
On Fri, Aug 21, 2009 at 11:44 PM, Ronn Ross wrote:
> I'm new to python and I'm getting a date time from a field in the database
> that looks like this:
> 8/2/2009 8:36:16 AM (UTC)
>
> I want to split it into two fields one with the date formatted like this:
> -MM-DD  2009-08-02
>
> and the time to be 24 hour or military time. How every you call it. Similar
> to this:
> 15:22:00
>
> I found it easy to truncate off the (UTC), but having trouble converting the
> date. Can someone point me in the right direction?
>

datetime.datetime.strptime() will give you a datetime object, which
you can then format in a wide variety of ways using its strftime()
method.  Doesn't work with dates earlier than 1900, I believe.

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


Re: Using 'apply' as a decorator, to define constants

2009-08-22 Thread Jonathan Fine

Jonathan Gardner wrote:

On Aug 21, 9:09 am, alex23  wrote:

On Aug 21, 11:36 pm, Jonathan Fine  wrote:

class ColourThing(object):
@apply
def rgb():
def fset(self, rgb):
self.r, self.g, self.b = rgb
def fget(self):
return (self.r, self.g, self.b)
return property(**locals())



This is brilliant. I am going to use this more often. I've all but
given up on property() since defining "get_foo", "get_bar", etc... has
been a pain and polluted the namespace.



I think we can do better, with a little work.  And also solve the 
problem that 'apply' is no longer a builtin in Python3.


Here's my suggestion:
===
import wibble # Informs reader we're doing something special here.

class ColourThing(object):

@wibble.property
def rgb():
'''This is the docstring for the property.'''
def fset(self, rgb):
self.r, self.g, self.b = rgb
def fget(self):
return (self.r, self.g, self.b)
return locals()
===

And here's wibble.property()
===
_property = property  # Avoid collision.
def property(fn):
return _property(doc=fn.__doc__, **fn())
===

We can add apply() to the wibble module.  By the way, 'wibble' is a 
placeholder to the real name.  Any suggestions?


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


Re: Questions on XML

2009-08-22 Thread Rami Chowdhury

encoding declaration to the top of your source file if you use encoded
literal strings in your code


Any tips for how to set the encoding in IDLE? printing the Unicode  
strings works -- trying to repr() the variable chokes with a  
UnicodeDecodeError, and trying to enter the literals inside IDLE just  
gets me '?' characters instead.


(this is Python 2.5 + IDLE installed from the Python-2.5.msi on  
python.org)


-
Rami Chowdhury
"Never assume malice when stupidity will suffice." -- Hanlon's Razor
408-597-7068 (US) / 07875-841-046 (UK) / 0189-245544 (BD)




On Aug 21, 2009, at 22:53 , Stefan Behnel wrote:


Rami Chowdhury wrote:
I am using primarily UTF-8 based strings, like Hindi or Bengali.  
Can I

use Python to help me in this regard?


I can say from experience that Python on Windows (at least, Python  
2.5

on 32-bit Vista) works perfectly well with UTF-8 files containing
Bangla. I have had trouble with working with the data in IDLE,  
however,

which seems to prefer ASCII by default.


Defaults almost never work for encodings. You have to be explicit:  
add an

encoding declaration to the top of your source file if you use encoded
literal strings in your code; use the codecs module with a suitable
encoding to read encoded text files, and use an XML parser when  
reading XML.


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


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


debugger

2009-08-22 Thread flagmino
To get familiar with the debugger, I have loaded this program:

import math

def s1(x, y):
   a = (x + y)
   print("Answer from s1"), a
   return

def s2(x, y):
   b = (x - y)
   print("This comes from s2"), b
   #print z
   print("call from s2: "), s1(x, y)
   return

I am trying to debug:
I press shift-F9 and F7. I end up in the interpreter where I enter s2
(1, 2).

>From that point if I press F7, the program restart all over.
If I press Enter, the program gets out of debug mode.

Please help me figuring out how I can use the dbugger. You are welcome
to send a sound file if this is easier for you.

Thanks

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


How to 'de-slashify' a string?

2009-08-22 Thread AK
Hi, if I have a string '\\303\\266', how can I convert it to '\303\266' 
in a general way?


The problem I'm running into is that I'm connecting with pygresql to a 
postgres database and when I get fields that are of 'char' type, I get 
them in unicode, but when I get fields of 'byte' type, I get the text 
with quoted slashes, e.g. '\303' becomes '\\303' and so on.


I saw posts online to do

cursor.execute("set client-encoding to unicode")

before running queries but this command causes an error.

So I think I need a way to de-quote the slashes or alternatively
some way to tell pygresql not to quote slashes for 'byte' fields.

Any help, hints, etc appreciated..

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


Re: debugger

2009-08-22 Thread Xavier Ho
On Sat, Aug 22, 2009 at 6:17 PM, flagmino  wrote:

> To get familiar with the debugger, I have loaded this program:
>
> import math
>
> def s1(x, y):
>   a = (x + y)
>   print("Answer from s1"), a
>   return
>
> def s2(x, y):
>   b = (x - y)
>   print("This comes from s2"), b
>   #print z
>   print("call from s2: "), s1(x, y)
>   return
>
> I am trying to debug:
> I press shift-F9 and F7. I end up in the interpreter where I enter s2
> (1, 2).
>
> >From that point if I press F7, the program restart all over.
> If I press Enter, the program gets out of debug mode.
>
> Please help me figuring out how I can use the dbugger. You are welcome
> to send a sound file if this is easier for you.
>
> Thanks
>
> ray
> --
> http://mail.python.org/mailman/listinfo/python-list


Sorry, but which debugger are you referring to?

Also, note that you're printing s1(x,y) which is returning nothing. Not sure
why you want to do that, but just mentioning it.
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Using 'apply' as a decorator, to define constants

2009-08-22 Thread Leonhard Vogt



Why I've personally stopped using it: I've always had the impression
that decorators were intended to provide a convenient and obvious way
of augmenting functions. Having one that automatically executes the
function at definition just runs counter to the behaviour I expect
from a decorator. Especially when direct assignment... foo = foo
() ...is a far more direct and clear way of expressing exactly what is
happening.


if you have to define it yourself, you could call it store_result or
something else instead of apply.

@store_result
def tags():
return result

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


Re: Using 'apply' as a decorator, to define constants

2009-08-22 Thread Steven D'Aprano
On Sat, 22 Aug 2009 08:09:52 +0100, Jonathan Fine wrote:

> Jonathan Gardner wrote:
>> On Aug 21, 9:09 am, alex23  wrote:
>>> On Aug 21, 11:36 pm, Jonathan Fine  wrote:
>>>
>>> class ColourThing(object):
>>> @apply
>>> def rgb():
>>> def fset(self, rgb):
>>> self.r, self.g, self.b = rgb
>>> def fget(self):
>>> return (self.r, self.g, self.b)
>>> return property(**locals())
>>>
>>>
>> This is brilliant. I am going to use this more often. I've all but
>> given up on property() since defining "get_foo", "get_bar", etc... has
>> been a pain and polluted the namespace.
> 
> 
> I think we can do better, with a little work.  And also solve the
> problem that 'apply' is no longer a builtin in Python3.

There's a standard idiom for that, using the property() built-in, for 
Python 2.6 or better.

Here's an example including a getter, setter, deleter and doc string, 
with no namespace pollution, imports, or helper functions or deprecated 
built-ins:

class ColourThing(object):
@property
def rgb(self):
"""Get and set the (red, green, blue) colours."""
return (self.r, self.g, self.b)
@rgb.setter
def rgb(self, rgb):
self.r, self.g, self.b = rgb
@rgb.deleter
def rgb(self):
del self.r, self.g, self.b


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


Re: Annoying octal notation

2009-08-22 Thread David
Il Fri, 21 Aug 2009 16:52:29 -0700 (PDT), James Harris ha scritto:


> 
>   0xff & 0x0e | 0b1101
>   16rff & 16r0e | 2r1101
> 
> Hmm. Maybe a symbol would be better than a letter.

What about 2_1011, 8_7621, 16_c26h or 2;1011, 8;7621, 16;c26h ?

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


Re: convert date time

2009-08-22 Thread John Machin
On Aug 22, 5:11 pm, Kushal Kumaran 
wrote:
> On Fri, Aug 21, 2009 at 11:44 PM, Ronn Ross wrote:
> > I'm new to python and I'm getting a date time from a field in the database
> > that looks like this:
> > 8/2/2009 8:36:16 AM (UTC)

> datetime.datetime.strptime() will give you a datetime object, which
> you can then format in a wide variety of ways using its strftime()
> method.  Doesn't work with dates earlier than 1900, I believe.

The datetime module works from 0001-01-01 onwards; see
http://docs.python.org/library/datetime.html#datetime.MINYEAR

Other things worth mentioning:

(1) datetime.datetime.strptime() was introduced in Python 2.5; for
Python 2.3 and 2.4, use time.strptime() [which may not like years
before 1970] followed by datetime.datetime()

(2) as the input has 12-hour clock plus AM/PM, ensure that you use %I
instead of %H for the hour component; %H won't do what you expect if
your expectation is not based on reading the docs :-)
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Annoying octal notation

2009-08-22 Thread David
Il Fri, 21 Aug 2009 10:36:35 -0700 (PDT), Mensanator ha scritto:

> Aha! Then I WAS right after all. Switch to 3.1 and you'll
> soon be cured of that bad habit:
> 
 012 + 012
> SyntaxError: invalid token (, line 1)

I have tre (four) problems:

1) I am forced to use 2.5 since the production server has 2.5 installed.

2) Quite often I have to enter many zero-leading numbers and now my fingers
put a leading zero almost everywhere

3) I don't understand why useless but *harmless* things like algebrically
insignificant leading zeros should be *forbidden* and promoted to errors.

4) I still don't like the '0o..' notation because 0 (zero) and o (lowercase
O) glyphs appear very similar in many character sets. I'd prefer something
like '0c..' so it resembles the word 'oc' for 'octal'.

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


Re: How to 'de-slashify' a string?

2009-08-22 Thread Steven D'Aprano
On Sat, 22 Aug 2009 04:20:23 -0400, AK wrote:

> Hi, if I have a string '\\303\\266', how can I convert it to '\303\266'
> in a general way?

It's not clear what you mean.

Do you mean you have a string '\\303\\266', that is:

backslash backslash three zero three backslash backslash two six six

If so, then the simplest way is:

>>> s = r'\\303\\266'  # note the raw string
>>> len(s)
10
>>> print s
\\303\\266
>>> print s.replace('', '\\')
\303\266


Another possibility:

>>> s = '\\303\\266'  # this is not a raw string
>>> len(s)
8
>>> print s
\303\266

So s is:
backslash three zero three backslash two six six

and you don't need to do any more.


> The problem I'm running into is that I'm connecting with pygresql to a
> postgres database and when I get fields that are of 'char' type, I get
> them in unicode, but when I get fields of 'byte' type, I get the text
> with quoted slashes, e.g. '\303' becomes '\\303' and so on.

Is pygresql quoting the backslash, or do you just think it is quoting the 
backslashes? How do you know? E.g. if you have '\\303', what is the 
length of that? 4 or 5?


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


Re: Annoying octal notation

2009-08-22 Thread Steven D'Aprano
On Sat, 22 Aug 2009 11:15:16 +0200, David wrote:

> 3) I don't understand why useless but *harmless* things like
> algebrically insignificant leading zeros should be *forbidden* and
> promoted to errors.

The PEP covering this change says:

"There are still some strong feelings that '0123' should be allowed as a 
literal decimal in Python 3.0. If this is the right thing to do, this can 
easily be covered in an additional PEP. This proposal only takes the 
first step of making '0123' not be a valid octal number, for reasons 
covered in the rationale."

http://www.python.org/dev/peps/pep-3127/



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


Re: Using 'apply' as a decorator, to define constants

2009-08-22 Thread Jonathan Fine

Steven D'Aprano wrote:

There's a standard idiom for that, using the property() built-in, for 
Python 2.6 or better.


Here's an example including a getter, setter, deleter and doc string, 
with no namespace pollution, imports, or helper functions or deprecated 
built-ins:


class ColourThing(object):
@property
def rgb(self):
"""Get and set the (red, green, blue) colours."""
return (self.r, self.g, self.b)
@rgb.setter
def rgb(self, rgb):
self.r, self.g, self.b = rgb
@rgb.deleter
def rgb(self):
del self.r, self.g, self.b



Sorry, Steve, but I don't understand this.  In fact, I don't even see 
how it can be made to work.


Unless an exception is raised,
@wibble
def wobble():
pass
will make an assignment to wobble, namely the return value of wibble. 
So in your example above, there will be /three/ assignments to rgb. 
Unless you do some complicated introspection (and perhaps not even then) 
surely they will clobber each other.


I still prefer:
@wibble.property
def rgb():
'''Red Green Blue color settings (property)'''

def fset(rgb):
self.r, self.g, self.b = rgb
return locals()

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


Re: Dictionary from a list

2009-08-22 Thread EK
On Aug 20, 2:10 pm, Peter Otten <[email protected]> wrote:
> Jan Kaliszewski wrote:
> > 20-08-2009 o 02:05:57 Jan Kaliszewski  wrote:
>
> >> Or probably better:
>
> >>      from itertools import islice, izip
> >>      dict(izip(islice(li, 0, None, 2), islice(li, 1, None, 2)))
>
> > Or similarly, perhaps more readable:
>
> >      iterator = iter(li)
> >      dict((iterator.next(), iterator.next()) for i in xrange(len(li)/2))
>
> I just can't stop posting this one:
>
> >>> from itertools import izip
> >>> it = iter([1,2,3,4,5,6])
> >>> dict(izip(it, it))
>
> {1: 2, 3: 4, 5: 6}
>
> I really tried, but yours drove me over the edge.
>
> Peter

dict(zip(*[iter(l)]*2))
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: How to 'de-slashify' a string?

2009-08-22 Thread AK

Steven D'Aprano wrote:

On Sat, 22 Aug 2009 04:20:23 -0400, AK wrote:


Hi, if I have a string '\\303\\266', how can I convert it to '\303\266'
in a general way?


It's not clear what you mean.

Do you mean you have a string '\\303\\266', that is:

backslash backslash three zero three backslash backslash two six six

If so, then the simplest way is:


s = r'\\303\\266'  # note the raw string
len(s)

10

print s

\\303\\266

print s.replace('', '\\')

\303\266


Another possibility:


s = '\\303\\266'  # this is not a raw string
len(s)

8

print s

\303\266

So s is:
backslash three zero three backslash two six six

and you don't need to do any more.


Well, I need the string itself to become '\303\266', not to print
that way. In other words, when I do 'print s', it should display
unicode characters if my term is set to show them, instead of
showing \303\266.





The problem I'm running into is that I'm connecting with pygresql to a
postgres database and when I get fields that are of 'char' type, I get
them in unicode, but when I get fields of 'byte' type, I get the text
with quoted slashes, e.g. '\303' becomes '\\303' and so on.


Is pygresql quoting the backslash, or do you just think it is quoting the 
backslashes? How do you know? E.g. if you have '\\303', what is the 
length of that? 4 or 5?


Length is 4, and I need it to be length of 1. E.g.:

>>> s = '\303'
>>> s
'\xc3'
>>> x = '\\303'
>>> x
'\\303'
>>> len(x)
4
>>> len(s)
1


What I get from pygresql is x, what I need is s. Either by asking 
pygresql to do this or convert it afterwards. I can't do 
replace('\\303', '\303') because it can be any unicode character.








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


Re: How to 'de-slashify' a string?

2009-08-22 Thread Vlastimil Brom
2009/8/22 AK :
> Steven D'Aprano wrote:
>>
>> On Sat, 22 Aug 2009 04:20:23 -0400, AK wrote:
>>
>>> Hi, if I have a string '\\303\\266', how can I convert it to '\303\266'
>>> in a general way?
>>
>> It's not clear what you mean.
>>
>> Do you mean you have a string '\\303\\266', that is:
>>
>> backslash backslash three zero three backslash backslash two six six
>>
>> If so, then the simplest way is:
>>
> s = r'\\303\\266'  # note the raw string
> len(s)
>>
>> 10
>
> print s
>>
>> \\303\\266
>
> print s.replace('', '\\')
>>
>> \303\266
>>
>>
>> Another possibility:
>>
> s = '\\303\\266'  # this is not a raw string
> len(s)
>>
>> 8
>
> print s
>>
>> \303\266
>>
>> So s is:
>> backslash three zero three backslash two six six
>>
>> and you don't need to do any more.
>
> Well, I need the string itself to become '\303\266', not to print
> that way. In other words, when I do 'print s', it should display
> unicode characters if my term is set to show them, instead of
> showing \303\266.
>
>>
>>
>>> The problem I'm running into is that I'm connecting with pygresql to a
>>> postgres database and when I get fields that are of 'char' type, I get
>>> them in unicode, but when I get fields of 'byte' type, I get the text
>>> with quoted slashes, e.g. '\303' becomes '\\303' and so on.
>>
>> Is pygresql quoting the backslash, or do you just think it is quoting the
>> backslashes? How do you know? E.g. if you have '\\303', what is the length
>> of that? 4 or 5?
>
> Length is 4, and I need it to be length of 1. E.g.:
>
 s = '\303'
 s
> '\xc3'
 x = '\\303'
 x
> '\\303'
 len(x)
> 4
 len(s)
> 1
>
>
> What I get from pygresql is x, what I need is s. Either by asking pygresql
> to do this or convert it afterwards. I can't do replace('\\303', '\303')
> because it can be any unicode character.
>
>>
>>
>
>
> --
> AK
> --
> http://mail.python.org/mailman/listinfo/python-list
>


Hi,
do you mean something like

>>> u"\u0303"
u'\u0303'
>>> print u"\u0303"
̃
̃ (dec.: 771)  (hex.: 0x303)   ̃ COMBINING TILDE (Mark, Nonspacing)
?

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


Re: Questions on XML

2009-08-22 Thread Stefan Behnel
Kee Nethery wrote:
> As a newbie, the thing that caused me trouble was importing a string
> into the XML parser.

That would be

root_element = et.fromstring(some_string)

The parse() function is meant to parse from a file.


> from xml.etree import ElementTree as et
> theXmlDataTree = et.parse(StringIO.StringIO(theXmlString))
> 
> from xml.etree import ElementTree as et
> theXmlDataTree = et.ElementTree(et.XML(theXmlString))

You can use both, but I suspect parsing from StringIO to be slower than
parsing from the string directly. That's the case for lxml, at least.

Note that fromstring() behaves the same as XML(), but it reads better when
parsing from a string variable. XML() reads better when parsing from a
literal string.

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


Re: Using 'apply' as a decorator, to define constants

2009-08-22 Thread Steven D'Aprano
On Sat, 22 Aug 2009 10:51:27 +0100, Jonathan Fine wrote:

> Steven D'Aprano wrote:
> 
>> There's a standard idiom for that, using the property() built-in, for
>> Python 2.6 or better.
>> 
>> Here's an example including a getter, setter, deleter and doc string,
>> with no namespace pollution, imports, or helper functions or deprecated
>> built-ins:
>> 
>> class ColourThing(object):
>> @property
>> def rgb(self):
>> """Get and set the (red, green, blue) colours.""" return
>> (self.r, self.g, self.b)
>> @rgb.setter
>> def rgb(self, rgb):
>> self.r, self.g, self.b = rgb
>> @rgb.deleter
>> def rgb(self):
>> del self.r, self.g, self.b
> 
> 
> Sorry, Steve, but I don't understand this.  In fact, I don't even see
> how it can be made to work.

Nevertheless, it does work, and it's not even magic. It's described (very 
briefly) in the docstring for property: help(property) will show it to 
you. More detail is here:

http://docs.python.org/library/functions.html#property

As for how it can work, it's not that difficult. All you need is for the 
setter and deleter methods to add an appropriate fset and fdel method to 
the property, then return it. Here's a toy example which may demonstrate 
the process (although unlike property, fget, fset and fdel don't have any 
special meanings):

class ToyProperty(object):
def __init__(self, fget, fset=None, fdel=None, fdoc=None):
if fdoc is None:
fdoc = fget.__doc__
self.fget = fget
self.fset = fset
self.fdel = fdel
def getter(self, fget):
self.fget = fget
return self
def setter(self, fset):
self.fset = fset
return self
def deleter(self, fdel):
self.fdel = fdel
return self


> Unless an exception is raised,
>  @wibble
>  def wobble():
>  pass
> will make an assignment to wobble, namely the return value of wibble. So
> in your example above, there will be /three/ assignments to rgb.

Yes. A tiny inefficiency, which only occurs when the class is created. If 
you care about that, then (1) use the full form of property() where you 
supply the fget, fset and fdel arguments all at once, and (2) you really 
need to get out into the fresh air more *wink*



> Unless
> you do some complicated introspection (and perhaps not even then) surely
> they will clobber each other.

So what? The process is no weirder than:

x = ['fget']
x = x + ['fset']
x = x + ['fdel']



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


Re: How to 'de-slashify' a string?

2009-08-22 Thread AK

Vlastimil Brom wrote:

2009/8/22 AK :

Steven D'Aprano wrote:

On Sat, 22 Aug 2009 04:20:23 -0400, AK wrote:


Hi, if I have a string '\\303\\266', how can I convert it to '\303\266'
in a general way?

It's not clear what you mean.

Do you mean you have a string '\\303\\266', that is:

backslash backslash three zero three backslash backslash two six six

If so, then the simplest way is:


s = r'\\303\\266'  # note the raw string
len(s)

10

print s

\\303\\266

print s.replace('', '\\')

\303\266


Another possibility:


s = '\\303\\266'  # this is not a raw string
len(s)

8

print s

\303\266

So s is:
backslash three zero three backslash two six six

and you don't need to do any more.

Well, I need the string itself to become '\303\266', not to print
that way. In other words, when I do 'print s', it should display
unicode characters if my term is set to show them, instead of
showing \303\266.




The problem I'm running into is that I'm connecting with pygresql to a
postgres database and when I get fields that are of 'char' type, I get
them in unicode, but when I get fields of 'byte' type, I get the text
with quoted slashes, e.g. '\303' becomes '\\303' and so on.

Is pygresql quoting the backslash, or do you just think it is quoting the
backslashes? How do you know? E.g. if you have '\\303', what is the length
of that? 4 or 5?

Length is 4, and I need it to be length of 1. E.g.:


s = '\303'
s

'\xc3'

x = '\\303'
x

'\\303'

len(x)

4

len(s)

1


What I get from pygresql is x, what I need is s. Either by asking pygresql
to do this or convert it afterwards. I can't do replace('\\303', '\303')
because it can be any unicode character.





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




Hi,
do you mean something like


u"\u0303"

u'\u0303'

print u"\u0303"

̃
̃ (dec.: 771)  (hex.: 0x303)   ̃ COMBINING TILDE (Mark, Nonspacing)
?

vbr


Yes, something like that except that it starts out as '\\303\\266', and 
it's good enough for me if it turns into '\303\266', in fact that's 
rendered as one unicode char. In other words, when you do:


>>> print "\\303\\266"
'\303\266'

I need that result to become a python string, i.e. the slashes need to
be converted from literal slashes to escape slashes.




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


Re: debugger

2009-08-22 Thread Albert Hopkins
On Sat, 2009-08-22 at 01:17 -0700, flagmino wrote:
[...]
> I am trying to debug:
> I press shift-F9 and F7. I end up in the interpreter where I enter s2
> (1, 2).
> 
> >From that point if I press F7, the program restart all over.
> If I press Enter, the program gets out of debug mode.

Umm.. which debugger is that?  My debugger doesn't have shift-F9...

-a


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


Re: How to 'de-slashify' a string?

2009-08-22 Thread Vlastimil Brom
2009/8/22 AK :
> Vlastimil Brom wrote:
>>
>> 2009/8/22 AK :
>>>
>>> Steven D'Aprano wrote:

 On Sat, 22 Aug 2009 04:20:23 -0400, AK wrote:

> Hi, if I have a string '\\303\\266', how can I convert it to '\303\266'
> in a general way?

 It's not clear what you mean.

 Do you mean you have a string '\\303\\266', that is:

 backslash backslash three zero three backslash backslash two six six

 If so, then the simplest way is:

>>> s = r'\\303\\266'  # note the raw string
>>> len(s)

 10
>>>
>>> print s

 \\303\\266
>>>
>>> print s.replace('', '\\')

 \303\266


 Another possibility:

>>> s = '\\303\\266'  # this is not a raw string
>>> len(s)

 8
>>>
>>> print s

 \303\266

 So s is:
 backslash three zero three backslash two six six

 and you don't need to do any more.
>>>
>>> Well, I need the string itself to become '\303\266', not to print
>>> that way. In other words, when I do 'print s', it should display
>>> unicode characters if my term is set to show them, instead of
>>> showing \303\266.
>>>

> The problem I'm running into is that I'm connecting with pygresql to a
> postgres database and when I get fields that are of 'char' type, I get
> them in unicode, but when I get fields of 'byte' type, I get the text
> with quoted slashes, e.g. '\303' becomes '\\303' and so on.

 Is pygresql quoting the backslash, or do you just think it is quoting
 the
 backslashes? How do you know? E.g. if you have '\\303', what is the
 length
 of that? 4 or 5?
>>>
>>> Length is 4, and I need it to be length of 1. E.g.:
>>>
>> s = '\303'
>> s
>>>
>>> '\xc3'
>>
>> x = '\\303'
>> x
>>>
>>> '\\303'
>>
>> len(x)
>>>
>>> 4
>>
>> len(s)
>>>
>>> 1
>>>
>>>
>>> What I get from pygresql is x, what I need is s. Either by asking
>>> pygresql
>>> to do this or convert it afterwards. I can't do replace('\\303', '\303')
>>> because it can be any unicode character.
>>>

>>>
>>> --
>>> AK
>>> --
>>> http://mail.python.org/mailman/listinfo/python-list
>>>
>>
>>
>> Hi,
>> do you mean something like
>>
> u"\u0303"
>>
>> u'\u0303'
>
> print u"\u0303"
>>
>> ̃
>>    ̃ (dec.: 771)  (hex.: 0x303)   ̃ COMBINING TILDE (Mark, Nonspacing)
>> ?
>>
>> vbr
>
> Yes, something like that except that it starts out as '\\303\\266', and it's
> good enough for me if it turns into '\303\266', in fact that's rendered as
> one unicode char. In other words, when you do:
>
 print "\\303\\266"
> '\303\266'
>
> I need that result to become a python string, i.e. the slashes need to
> be converted from literal slashes to escape slashes.
>
>
>
>
> --
> AK
> --
> http://mail.python.org/mailman/listinfo/python-list
>

Not sure, whether it is the right way of handling the such text data, but maybe:

>>> decoded = '\\303\\266'.decode("string_escape")
>>> decoded
'\xc3\xb6'
>>> print decoded
ö
>>> print '\303\266'
ö
>>>

It might be an IDLE issue, but it still isn't one unicode glyph.

I guess, you have to ensure, that the input data is valid and the
right encoding is used.

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


Re: How to 'de-slashify' a string?

2009-08-22 Thread Steven D'Aprano
On Sat, 22 Aug 2009 06:09:20 -0400, AK wrote:

>> Is pygresql quoting the backslash, or do you just think it is quoting
>> the backslashes? How do you know? E.g. if you have '\\303', what is the
>> length of that? 4 or 5?
> 
> Length is 4, and I need it to be length of 1. E.g.:
> 
>  >>> s = '\303'
>  >>> s
> '\xc3'
>  >>> x = '\\303'
>  >>> x
> '\\303'
>  >>> len(x)
> 4
>  >>> len(s)
> 1
> 
> 
> What I get from pygresql is x, what I need is s. Either by asking
> pygresql to do this or convert it afterwards. I can't do
> replace('\\303', '\303') because it can be any unicode character.


Use the 'unicode-escape' codec to decode the byte-string to Unicode.

>>> s = '\\303\\266'
>>> print s
\303\266
>>> s
'\\303\\266'
>>> len(s)
8
>>> u = s.decode('unicode-escape')
>>> print u
ö
>>> u
u'\xc3\xb6'
>>> len(u)
2



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


Re: Using 'apply' as a decorator, to define constants

2009-08-22 Thread Jonathan Fine

Steven D'Aprano wrote:

On Sat, 22 Aug 2009 10:51:27 +0100, Jonathan Fine wrote:


Steven D'Aprano wrote:


There's a standard idiom for that, using the property() built-in, for
Python 2.6 or better.

Here's an example including a getter, setter, deleter and doc string,
with no namespace pollution, imports, or helper functions or deprecated
built-ins:

class ColourThing(object):
@property
def rgb(self):
"""Get and set the (red, green, blue) colours.""" return
(self.r, self.g, self.b)
@rgb.setter
def rgb(self, rgb):
self.r, self.g, self.b = rgb
@rgb.deleter
def rgb(self):
del self.r, self.g, self.b


Sorry, Steve, but I don't understand this.  In fact, I don't even see
how it can be made to work.


Nevertheless, it does work, and it's not even magic. It's described (very 
briefly) in the docstring for property: help(property) will show it to 
you. More detail is here:


http://docs.python.org/library/functions.html#property


My apologies.  I wasn't up to date with my Python versions:

| Changed in version 2.6: The getter, setter, and deleter
| attributes were added.

I was still thinking Python2.5 (or perhaps earlier?).  I still don't 
like it.  All those repetitions of 'rgb'.


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


warnings.warn vs logging.warning

2009-08-22 Thread Gunter Henriksen
When I want to issue a warning, I am uncertain
about the distinction between warnings.warn() and
logging.warning().  My naive thought is to presume
"warning" means the same thing in both cases, and
so if I call logging.warning(), it should take
care of making sure something equivalent to my
calling warnings.warn() will happen... more than
just having a message go to wherever a message
to warnings.warn() would go.  If I want to utilize
the capabilities of both mechanisms (like having
an exception raised), should I call both functions?
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Annoying octal notation

2009-08-22 Thread David
Il Thu, 20 Aug 2009 16:59:14 -0700 (PDT), James Harris ha scritto:

> 
> It maybe made sense once but this relic of the past should have been
> consigned to the waste bin of history long ago.

I perfectly agree with you!

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


Re: Need cleanup advice for multiline string

2009-08-22 Thread Steven D'Aprano
On Wed, 19 Aug 2009 10:51:08 -0700, Simon Forman wrote:

> (FWIW, I've always admired Humpty Dumpty's attitude to words.

"When I use a word," Humpty Dumpty said in rather a scornful tone, "it 
means just what I choose it to mean -- neither more nor less."

When you say "admired", do you mean what the rest of us understand by 
"admired", or something completely different?

How about "always", "attitude", "to" and "words"?

For all I know, you're talking about baking a birthday cake for your cat, 
by which I mean shaving off all your hair, and by "hair" I mean "lunch" 
and by "shaving off" I mean "eating".


> Have you
> ever read R.A. Wilson's "Quantum Psychology"?)

Perhaps I have, perhaps I haven't, it depends on who asks first.



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


line completion

2009-08-22 Thread Steven Woody
Hi,
I wrote a program that takes some user input.  Many inputs are quit often
used by user, so when a user launch the program, and type in "The Sha",  he
wants to get "wshank Redemption" displayed automatically in reversed color
(black text on white background) along his cursor.  When he decided to use
"The Shawshank Redemption", he just press the enter key, otherwise, he type
in other words anyway.

My question is, is there any module that can easy the implementation of this
feature?  Thanks.



-- 
Life is the only flaw in an otherwise perfect nonexistence
   -- Schopenhauer

narke
public key at http://subkeys.pgp.net:11371 ([email protected])
-- 
http://mail.python.org/mailman/listinfo/python-list


Blank Line at Program Exit

2009-08-22 Thread Steven Woody
Hi,
Any python program, even that does absolutely nothing in the code, will
cause a blank line printed out when the program exit.  What's the reason?
 Thanks.


-- 
Life is the only flaw in an otherwise perfect nonexistence
   -- Schopenhauer

narke
public key at http://subkeys.pgp.net:11371 ([email protected])
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: generate keyboard/mouse event under windows

2009-08-22 Thread yaka
Read this and see if it helps:

http://kvance.livejournal.com/985732.html
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: list 'results' from maps.google then crawl

2009-08-22 Thread Stefan Behnel
Justin wrote:
> list 'results' from maps.google then crawl through the (engine of some
> sort) space to the 'results' website and look at it html to find the
> contact

Good idea. How do you know how to recognise the contact? He/she might come
disguised.

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


generate keyboard/mouse event under windows

2009-08-22 Thread Ray
Hi,

Anyone can give some help on how to generate keyboard mouse event
under windows? (python 2.5)

I tried pyhook, I only know how to monitor the keyboard/mouse events.
but don't know how to generate/send the the event.

thanks for any help.

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


list 'results' from maps.google then crawl

2009-08-22 Thread Justin
list 'results' from maps.google then crawl through the (engine of some
sort) space to the 'results' website and look at it html to find the
contact
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: grokproject and zope.security==3.4.1 error

2009-08-22 Thread Christian Heimes

Manuel A. Iglesias Abbatemarco schrieb:

I will apprreciate if someone could help me with the following error.

I a trying to create a grokproject application in my debian 5.0 box,
the following is the output after executing # grokproject Sample
command.

Getting distribution for 'zope.security==3.4.1'.

src/zope/security/_proxy.c:19:20: error: Python.h: No such file or directory



sudo apt-get install python2.4-dev

Have fun :)

Christian

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


Re: Annoying octal notation

2009-08-22 Thread MRAB

Dennis Lee Bieber wrote:

On Fri, 21 Aug 2009 16:52:29 -0700 (PDT), James Harris
 declaimed the following in
gmane.comp.python.general:


So you are saying that Smalltalk has r where
r is presumably for radix? That's maybe best of all. It preserves the
syntactic requirement of starting a number with a digit and seems to
have greatest flexibility. Not sure how good it looks but it's
certainly not bad.

  0xff & 0x0e | 0b1101
  16rff & 16r0e | 2r1101

Hmm. Maybe a symbol would be better than a letter.


Or Ada's16#FF#, 8#377#...


'#' starts a comment, so that's right out! :-)


I forget if DEC/VMS FORTRAN or Xerox Sigma FORTRAN used x'FF' or
'FF'x, and o'377' or '377'o


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


Re: Annoying octal notation

2009-08-22 Thread MRAB

David wrote:

Il Fri, 21 Aug 2009 16:52:29 -0700 (PDT), James Harris ha scritto:



  0xff & 0x0e | 0b1101
  16rff & 16r0e | 2r1101

Hmm. Maybe a symbol would be better than a letter.


What about 2_1011, 8_7621, 16_c26h or 2;1011, 8;7621, 16;c26h ?


'_': what if in the future we want to allow them in numbers for clarity?

';': used to separate multiple statements on a line (but not used that
often).
--
http://mail.python.org/mailman/listinfo/python-list


Re: PIL and Python

2009-08-22 Thread catafest
If I make it work, i will send the solution. Thank you !

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


Re: debugger

2009-08-22 Thread Stef Mientki

Albert Hopkins wrote:

On Sat, 2009-08-22 at 01:17 -0700, flagmino wrote:
[...]
  

I am trying to debug:
I press shift-F9 and F7. I end up in the interpreter where I enter s2
(1, 2).

>From that point if I press F7, the program restart all over.
If I press Enter, the program gets out of debug mode.



Umm.. which debugger is that?  My debugger doesn't have shift-F9...
  

then you probably not working under windows ...

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


Re: How to 'de-slashify' a string?

2009-08-22 Thread Piet van Oostrum
> Vlastimil Brom  (VB) wrote:

> decoded = '\\303\\266'.decode("string_escape")
> decoded
>VB> '\xc3\xb6'
> print decoded
>VB> ö
> print '\303\266'
>VB> ö
> 

>VB> It might be an IDLE issue, but it still isn't one unicode glyph.

>VB> I guess, you have to ensure, that the input data is valid and the
>VB> right encoding is used.

>>> decoded = '\\303\\266'.decode("string_escape").decode('utf-8')
>>> decoded
u'\xf6'
>>> print decoded
ö

-- 
Piet van Oostrum 
URL: http://pietvanoostrum.com [PGP 8DAE142BE17999C4]
Private email: [email protected]
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: debugger

2009-08-22 Thread Tim Chase

Stef Mientki wrote:

Albert Hopkins wrote:

On Sat, 2009-08-22 at 01:17 -0700, flagmino wrote:
[...]
  

I am trying to debug:
I press shift-F9 and F7. I end up in the interpreter where I enter s2
(1, 2).

>From that point if I press F7, the program restart all over.
If I press Enter, the program gets out of debug mode.


Umm.. which debugger is that?  My debugger doesn't have shift-F9...
  

then you probably not working under windows ...


Even under Windows, you still have to specify your environment. 
In both Win32 and *nix I use pdb as my debugger and shift+F9 does 
nothing ;-)


So the question is what is the debugger the OP is using?  Because 
it's not pdb. :)


-tkc



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


Re: Annoying octal notation

2009-08-22 Thread Grant Edwards
On 2009-08-22, Dennis Lee Bieber  wrote:
> On Fri, 21 Aug 2009 10:45:51 -0700, John Nagle 
> declaimed the following in gmane.comp.python.general:
>
>> 
>>  And it's over.  We can finally dispense with octal by default.
>>
>   I've not looked at modern Intel processor format, but if there are
> folks still using variants of 8080 (8051?) and Z-80, Octal still works
> nice for op-codes... I don't recall the exact values, but the MOV
> instruction was something like '1SD'o, where S and D are three bit
> register specifications (A, B, C, D, E, H, L, and Memory as I recall)

The Heathkit's terminal I have uses a Z80, and IIRC, the
assembly listings were in split-octal [a 16 bit word ranges
from 000 000 to 377 377]. Stuff for the PDP-11 (which also had
instruction fields 3 bits wide) was always in octal as well.
The PDP-11 is pretty much dead, but I think there are embedded
Z80 derivitives still in use.

-- 
Grant

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


your favorite debugging tool?

2009-08-22 Thread Esmail

Hi all,

What is your favorite tool to help you debug your
code? I've been getting along with 'print' statements
but that is getting old and somewhat cumbersome.

I'm primarily interested in utilities for Linux (but
if you have recommendations for Windows, I'll take
them too :)

I use emacs as my primary development environment, FWIW.
Someone mentioned winpdb .. anyone have experience/comments
on this? Others?

Thanks,
Esmail

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


Re: debugger

2009-08-22 Thread Scott David Daniels

flagmino wrote:

To get familiar with the debugger, I have loaded this program:

import math

def s1(x, y):
   a = (x + y)
   print("Answer from s1"), a
   return

def s2(x, y):
   b = (x - y)
   print("This comes from s2"), b
   #print z
   print("call from s2: "), s1(x, y)
   return

I am trying to debug:
I press shift-F9 and F7. I end up in the interpreter where I enter s2
(1, 2).

From that point if I press F7, the program restart all over.
If I press Enter, the program gets out of debug mode.

Please help me figuring out how I can use the dbugger. You are welcome
to send a sound file if this is easier for you.

Thanks

ray

You need to tell us:
Which Python version (e.g. 2.6.2)
Which "platform" (hardware & OS) (e.g. 64-bit AMD FreeBSD)
Which debugger (e.g. Idle)
What you expected to happen that did not, and why you expected it.
or What happened and why you did not expect it.

Often you can lots of this information by going to your debugger window 
and doing Help // About, and go to your Python environment and type:

import sys
print sys.version # cut the results and paste in your message as
"sys.version says, "'2.6.2 (r262:71605, ...'"  [don't do dots yourself]

To understand more of why we need this on every question, see:
http://www.mikeash.com/getting_answers.html
or google for "smart questions".

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


Re: your favorite debugging tool?

2009-08-22 Thread Aahz
In article ,
Esmail   wrote:
>
>What is your favorite tool to help you debug your code? I've been
>getting along with 'print' statements but that is getting old and
>somewhat cumbersome.

Despite the fact that I've been using Python for more than a decade,
print is still my mainstay (or possibly logging to a file).
-- 
Aahz ([email protected])   <*> http://www.pythoncraft.com/

"I support family values -- Addams family values" --www.nancybuttons.com
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Blank Line at Program Exit

2009-08-22 Thread Dave Angel

Steven Woody wrote:

Hi,
Any python program, even that does absolutely nothing in the code, will
cause a blank line printed out when the program exit.  What's the reason?
 Thanks.


  
I think the "blank line" is coming from your shell.  In Windows, I 
believe the shell emits a newline after running a program, probably to 
make sure the prompt starts in column 0.


Note that if your python program writes a partial line to stdout, this 
will "finish" the line before giving you the next prompt.  So the line 
isn't necessarily blank.


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


Re: TypeError while checking for permissions with os.access() on windows xp

2009-08-22 Thread ryniek90



First, some nitpicking: Include the whole traceback when posting about
errors please.  Don't write "if some_boolean_expression != True:"
instead prefer "if not some_boolean_expression:".  Also, in your code
"except (Except), ex: return ex" the parentheses are redundant,
there's no "Except" Exception (unless you created one yourself) and
why are you catching and returning the Exception object?  (Also,
what's that "sys.exit(0)" doing in there? And why exit with 0 if there
was a problem? Exit code 0 means "no problem".)


Second, from what you've said backup_dest must be None:

  

import os
os.access(None, os.W_OK)



Traceback (most recent call last):
  File "", line 1, in 
os.access(None, os.W_OK)
TypeError: coercing to Unicode: need string or buffer, NoneType found

  


No, backup_dest is destination for backup. It's this variable: 
*backup_dest = self.__backup_dest* .
*self.__backup_dest = os.getenv('HOME')*  -  as you can see, 
self.__backup_dest is a reference to the home directory which is a 
string. Then backup_dest is a reference to the variable 
self.__backup_dest. But that second reference seems to be recognised as 
a NoneType ??


I'm using optparse in my script (that code for checking for permission 
is a part of my backup script).


Maybe i post my complete code:
"
#!/usr/bin/env python
#-*- coding: utf-8 -*-
#MyBackUper

import os, sys, tarfile
import datetime
from optparse import OptionParser


class Except(Exception):
   pass


class Packer(Except):

   def __init__(self):
   self.__backup_dest = os.getenv('HOME')
   __current_time = datetime.datetime.today().strftime('%Y-%m-%d 
%H:%M:%S')

   Except.__init__(self)

   def doc_note(self):

   print """

   DOCUMENTATION

   'Backuper' is a script for doing backups.
   You can backup with it single files or whole directories.
   For available commands type 'backuper.py -h' or 'backuper.py 
--help'.


   Using it is simple:

   - to backup single file, type:
   'backuper.py -f FILE_PATH BCKP_NAME [DEST_PATH]',
   where 'FILE_PATH' is exact path to prefered file which you want 
to backup and 'BCKP_NAME' is the name for backup output file. Default 
'DEST_PATH' is placed in home directory, it can't be changed. 
'BCKP_NAME' must be given;


   - to backup whole directory, type:
   'backuper.py -d DIR_PATH BCKP_NAME [DEST_PATH]', where 
'DIR_PATH' is exact path to prefered directory, which you want to backup 
and 'BCKP_NAME' is the name for backup output file. Default 'DEST_PATH' 
is placed in home directory, it can't be changed. 'BCKP_NAME' must be given;


   """

   def __check_set_perm(self, rd_obj_path, backup_dest):

   try:

   if os.path.exists(rd_obj_path):
   if not os.access(rd_obj_path, os.R_OK):
   print "Have no permissions on [%s] for reading 
operation.\nTrying to set them..." % os.path.split(rd_obj_path)[1]

   if not os.path.isdir(rd_obj_path):
   os.chmod(rd_obj_path, stat.S_IREAD)
   else:
   for root, dirs, files in os.walk(rd_obj_path):
   for f in files:
   os.chmod(os.path.join(root, f), 
stat.S_IREAD)
   print "Get permissions for reading on [%s] 
successfully." % os.path.split(rd_obj_path)[1]

   else:
   print "Have permissions on [%s] for reading." % 
os.path.split(rd_obj_path)[1]


   if not os.access(backup_dest, os.W_OK):
   print "Have no permissions on [%s] for writing 
operation.\nTrying to set them..." % os.path.split(backup_dest)[1]

   os.chmod(backup_dest, stat.S_IWRITE)
   print "Get permissions for reading on [%s] 
successfully." % os.path.split(backup_dest)[1]

   else:
   print "Have permissions on [%s] for writing." % 
os.path.split(backup_dest)[1]

   else:
   return "Can't find specified path - [%s]." % rd_obj_path
   sys.exit(1)

   except (Except), ex:
   return ex


   def backup_file(self, rd_obj_path, bkp_obj_name):

   try:

   if os.name == "nt":
   rd_obj_path = rd_obj_path.replace('~/', '%s\\' % 
os.getenv('HOME')).replace('\\', '')

   backup_dest = self.__backup_dest
   print "Checking permissions for reading and writing..."
   self.__check_set_perm(rd_obj_path, backup_dest)
   backup_obj = tarfile.open("%s\\%s(%s).tar.bz2" % 
(backup_dest, bkp_obj_name, __current_time), 'w:bz2')


   else:
   rd_obj_path = rd_obj_path.replace('~/', '%s/' % 
os.getenv('HOME'))

   backup_dest = self.__backup_dest
   print "Checking permissions for reading and writing..."
   self.__check_set_perm(rd_obj_path, backup_dest)
   backup_obj = tarfile.open("%s/%s(%s).

Re: Decompressing gzip over FTP

2009-08-22 Thread Albert Hopkins
On Fri, 2009-08-21 at 18:15 -0700, SeanMon wrote:
> Is there a way to decompress a large (2GB) gzipped file being
> retrieved over FTP on the fly?
> 
> I'm using ftplib.FTP to open a connection to a remote server, and I
> have had no success connecting retrbinary to gzip without using an
> intermediate file.
> 
> Is there any way to get a file-like object describing the remote file,
> or a way to utilize gzip's decompression without providing it a file-
> like object?

I tried to solve your problem with StringIO and .truncate().  I also
tried to solve it with us.pipe(), but was unsuccessful with either.  The
problem is the gzip module uses .seek() which you can't do with pipes...

You could probably get away with just using the compress module (as zlib
does) but you will have to deal with handling the headers, footers, CRC
checks, etc. on your own.

The cheap alternative is to just have your script open a pipe to "gunzip
-c" and read/write from that pipe.

hth,
-a


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


Re: debugger

2009-08-22 Thread Dave Angel

Stef Mientki wrote:
Albert 
Hopkins wrote:

On Sat, 2009-08-22 at 01:17 -0700, flagmino wrote:
[...]
 

I am trying to debug:
I press shift-F9 and F7. I end up in the interpreter where I enter s2
(1, 2).

>From that point if I press F7, the program restart all over.
If I press Enter, the program gets out of debug mode.



Umm.. which debugger is that?  My debugger doesn't have shift-F9...
  

then you probably not working under windows ...

Stef



That doesn't help.  If you want help, describe the environment.  Python 
version, OS, and what command you did to start this "debugger."  Or at 
least its name.


The Windows debugger is called Debug.exe, and it doesn't accept 
Shift-F9.  But I doubt the OP was talking about that one.  Komodo 
doesn't seem to do anything with Shift-F9 either.


PythonWin doesn't seem to, nor does Idle.   Most of these could be 
customized, but that's besides the point.


In version:  2.6.2 (r262:71600, Apr 21 2009, 15:05:37) [MSC v.1500 32 
bit (Intel)],  running under XPsp3, with win32 extensions loaded, there 
are a bunch of files called debug or debugger.  Which one might be meant 
here?


Or perhaps the debugger was part of NetBeans, or Eclipse, or some other IDE?

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


Re: Blank Line at Program Exit

2009-08-22 Thread Nitebirdz
On Thu, Aug 20, 2009 at 01:31:14PM +0800, Steven Woody wrote:
> Hi,
> Any python program, even that does absolutely nothing in the code, will
> cause a blank line printed out when the program exit.  What's the reason?
>  Thanks.
> 

Chances are it is related to whichever operating system and/or shell you
are using.  For instance, this is taken from a system running Ubuntu
Linux 8.04:

-
laptop:$ cat hello.py
#!/usr/bin/env python

print "Hello, there!"
laptop:$ ./hello.py
Hello, there!
laptop:$ cat nothing.py 
#!/usr/bin/env python

laptop:$ ./nothing.py 
laptop:$ 
-


As you can see, no blank line was printed out. 

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


Re: debugger

2009-08-22 Thread MRAB

Dave Angel wrote:

Stef Mientki wrote:
Albert 
Hopkins wrote:

On Sat, 2009-08-22 at 01:17 -0700, flagmino wrote:
[...]
 

I am trying to debug:
I press shift-F9 and F7. I end up in the interpreter where I enter s2
(1, 2).

>From that point if I press F7, the program restart all over.
If I press Enter, the program gets out of debug mode.



Umm.. which debugger is that?  My debugger doesn't have shift-F9...
  

then you probably not working under windows ...

Stef



That doesn't help.  If you want help, describe the environment.  Python 
version, OS, and what command you did to start this "debugger."  Or at 
least its name.


The Windows debugger is called Debug.exe, and it doesn't accept 
Shift-F9.  But I doubt the OP was talking about that one.  Komodo 
doesn't seem to do anything with Shift-F9 either.


PythonWin doesn't seem to, nor does Idle.   Most of these could be 
customized, but that's besides the point.


In version:  2.6.2 (r262:71600, Apr 21 2009, 15:05:37) [MSC v.1500 32 
bit (Intel)],  running under XPsp3, with win32 extensions loaded, there 
are a bunch of files called debug or debugger.  Which one might be meant 
here?


Or perhaps the debugger was part of NetBeans, or Eclipse, or some other 
IDE?



It might be winpdb, although I haven't been able to confirm it.
--
http://mail.python.org/mailman/listinfo/python-list


Re: TypeError while checking for permissions with os.access() on windows xp

2009-08-22 Thread Dave Angel

ryniek90 wrote:



First, some nitpicking: Include the whole traceback when posting about
errors please.  Don't write "if some_boolean_expression != True:"
instead prefer "if not some_boolean_expression:".  Also, in your code
"except (Except), ex: return ex" the parentheses are redundant,
there's no "Except" Exception (unless you created one yourself) and
why are you catching and returning the Exception object?  (Also,
what's that "sys.exit(0)" doing in there? And why exit with 0 if there
was a problem? Exit code 0 means "no problem".)


Second, from what you've said backup_dest must be None:

 

import os
os.access(None, os.W_OK)



Traceback (most recent call last):
  File "", line 1, in 
os.access(None, os.W_OK)
TypeError: coercing to Unicode: need string or buffer, NoneType found

  


No, backup_dest is destination for backup. It's this variable: 
*backup_dest = self.__backup_dest* .
*self.__backup_dest = os.getenv('HOME')*  -  as you can see, 
self.__backup_dest is a reference to the home directory which is a 
string. Then backup_dest is a reference to the variable 
self.__backup_dest. But that second reference seems to be recognised 
as a NoneType ??


I'm using optparse in my script (that code for checking for permission 
is a part of my backup script).


Maybe i post my complete code:
"
#!/usr/bin/env python
#-*- coding: utf-8 -*-
#MyBackUper

import os, sys, tarfile
import datetime
from optparse import OptionParser


class Except(Exception):
   pass


class Packer(Except):

   def __init__(self):
   self.__backup_dest = os.getenv('HOME')
   __current_time = datetime.datetime.today().strftime('%Y-%m-%d 
%H:%M:%S')

   Except.__init__(self)

   def doc_note(self):

   print """

   DOCUMENTATION

   'Backuper' is a script for doing backups.
   You can backup with it single files or whole directories.
   For available commands type 'backuper.py -h' or 'backuper.py 
--help'.


   Using it is simple:

   - to backup single file, type:
   'backuper.py -f FILE_PATH BCKP_NAME [DEST_PATH]',
   where 'FILE_PATH' is exact path to prefered file which you want 
to backup and 'BCKP_NAME' is the name for backup output file. Default 
'DEST_PATH' is placed in home directory, it can't be changed. 
'BCKP_NAME' must be given;


   - to backup whole directory, type:
   'backuper.py -d DIR_PATH BCKP_NAME [DEST_PATH]', where 
'DIR_PATH' is exact path to prefered directory, which you want to 
backup and 'BCKP_NAME' is the name for backup output file. Default 
'DEST_PATH' is placed in home directory, it can't be changed. 
'BCKP_NAME' must be given;


   """

   def __check_set_perm(self, rd_obj_path, backup_dest):

   try:

   if os.path.exists(rd_obj_path):
   if not os.access(rd_obj_path, os.R_OK):
   print "Have no permissions on [%s] for reading 
operation.\nTrying to set them..." % os.path.split(rd_obj_path)[1]

   if not os.path.isdir(rd_obj_path):
   os.chmod(rd_obj_path, stat.S_IREAD)
   else:
   for root, dirs, files in os.walk(rd_obj_path):
   for f in files:
   os.chmod(os.path.join(root, f), 
stat.S_IREAD)
   print "Get permissions for reading on [%s] 
successfully." % os.path.split(rd_obj_path)[1]

   else:
   print "Have permissions on [%s] for reading." % 
os.path.split(rd_obj_path)[1]


   if not os.access(backup_dest, os.W_OK):
   print "Have no permissions on [%s] for writing 
operation.\nTrying to set them..." % os.path.split(backup_dest)[1]

   os.chmod(backup_dest, stat.S_IWRITE)
   print "Get permissions for reading on [%s] 
successfully." % os.path.split(backup_dest)[1]

   else:
   print "Have permissions on [%s] for writing." % 
os.path.split(backup_dest)[1]

   else:
   return "Can't find specified path - [%s]." % rd_obj_path
   sys.exit(1)

   except (Except), ex:
   return ex


   def backup_file(self, rd_obj_path, bkp_obj_name):

   try:

   if os.name == "nt":
   rd_obj_path = rd_obj_path.replace('~/', '%s\\' % 
os.getenv('HOME')).replace('\\', '')

   backup_dest = self.__backup_dest
   print "Checking permissions for reading and writing..."
   self.__check_set_perm(rd_obj_path, backup_dest)
   backup_obj = tarfile.open("%s\\%s(%s).tar.bz2" % 
(backup_dest, bkp_obj_name, __current_time), 'w:bz2')


   else:
   rd_obj_path = rd_obj_path.replace('~/', '%s/' % 
os.getenv('HOME'))

   backup_dest = self.__backup_dest
   print "Checking permissions for reading and writing..."
   self.__check_set_perm(rd_obj_path, backup_dest)
   backup_obj = tarfile

string literal vs string variable

2009-08-22 Thread Kee Nethery


On Aug 22, 2009, at 3:32 AM, Stefan Behnel wrote:



You can use both, but I suspect parsing from StringIO to be slower  
than

parsing from the string directly. That's the case for lxml, at least.

Note that fromstring() behaves the same as XML(), but it reads  
better when

parsing from a string variable. XML() reads better when parsing from a
literal string.


I'm not sure I know the difference between a string variable and a  
literal string. Is the difference as simple as:


somestring = u'hello world'
fromstring(somestring)  <-- string variable
vs
XML(u'hello world')  <-- literal string

Kee


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


Urllib2 not working

2009-08-22 Thread Carlos Fabian Ramirez
Hello,
When I try to open a URL using urllib2.urlopen it returns Name or service
not known. It is not a problem with my Internet I believe, since I have
Internet access on my computer, and I have verified it is not a syntax, or
spelling, error on my part. I have also tried accessing the site (google.com)
by IP like so:
urllib2.urlopen('http://74.125.67.100/')
but this does not work either.
This is a traceback of an attempt to access it through its URL (
http://www.google.com/)
http://dpaste.com/84016/
Any help would be very appreciated.
-- 
http://mail.python.org/mailman/listinfo/python-list


sed/awk/perl: How to replace all spaces each with an underscore that occur before a specific string ?

2009-08-22 Thread bolega
sed/awk/perl:

How to replace all spaces each with an underscore that occur before a
specific string ?

I really prefer a sed one liner.

Example
Input :  This is my book. It is too  thick to read. The author gets
little royalty but the publisher makes a lot.
Output: This_is_my_book._It_is_too__thick_to read. The author gets
little royalty but the publisher makes a lot.

We replaced all the spaces with underscores before the first occurence
of the string "to ".

Thanks
Gnuist



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


Re: sed/awk/perl: How to replace all spaces each with an underscore that occur before a specific string ?

2009-08-22 Thread w_a_x_man
On Aug 22, 1:11 pm, bolega  wrote:
> sed/awk/perl:
>
> How to replace all spaces each with an underscore that occur before a
> specific string ?
>
> I really prefer a sed one liner.
>
> Example
> Input :  This is my book. It is too  thick to read. The author gets
> little royalty but the publisher makes a lot.
> Output: This_is_my_book._It_is_too__thick_to read. The author gets
> little royalty but the publisher makes a lot.
>
> We replaced all the spaces with underscores before the first occurence
> of the string "to ".
>
> Thanks
> Gnuist

awk 'BEGIN{FS=OFS="to "}{gsub(/ /,"_",$1);print}' myfile
-- 
http://mail.python.org/mailman/listinfo/python-list


Python - insert image / pdf / blob files to MySQL, MSSQL

2009-08-22 Thread MacRules

Where to find code examples?

Or someone can post sample codes here.

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


Re: Annoying octal notation

2009-08-22 Thread Derek Martin
On Sat, Aug 22, 2009 at 10:03:35AM +1000, Ben Finney wrote:
> > and the former is virtually indistinguishable from 00012, O0012, or
> > many other combinations that someone might accidentally type (or
> > intentionally type, having to do this in dozens of other programming
> > languages).
> 
> Only if you type the letter in uppercase. The lower-case ‘o’ is much
> easier to distinguish.

It is easier, but I dispute that it is much easier.
 
> Whether or not you find ‘0o012’ easily distinguishable as a non-decimal
> notation, it's undoubtedly easier to distinguish than ‘012’.

012 has meant  decimal 10 in octal to me for so long, from its use in
MANY other programming languages, that I disagree completely.  

> > I can see how 012 can be confusing to new programmers, but at least
> > it's legible, and the great thing about humans is that they can be
> > taught (usually). I for one think this change is completely misguided.
> 
> These human programmers, whether newbies or long-experienced, also deal
> with decimal numbers every day, many of which are presented as a
> sequence of digits with leading zeros — and we continue to think of them
> as decimal numbers regardless. Having the language syntax opposed to
> that is 
 
...consistent with virtually every other popular programming language.

-- 
Derek D. Martin
http://www.pizzashack.org/
GPG Key ID: 0x81CFE75D



pgpaLprG9uUPz.pgp
Description: PGP signature
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: TypeError while checking for permissions with os.access() on windows xp

2009-08-22 Thread ryniek90



First, some nitpicking: Include the whole traceback when posting about
errors please.  Don't write "if some_boolean_expression != True:"
instead prefer "if not some_boolean_expression:".  Also, in your code
"except (Except), ex: return ex" the parentheses are redundant,
there's no "Except" Exception (unless you created one yourself) and
why are you catching and returning the Exception object?  (Also,
what's that "sys.exit(0)" doing in there? And why exit with 0 if there
was a problem? Exit code 0 means "no problem".)


Second, from what you've said backup_dest must be None:

 

import os
os.access(None, os.W_OK)



Traceback (most recent call last):
  File "", line 1, in 
os.access(None, os.W_OK)
TypeError: coercing to Unicode: need string or buffer, NoneType found

  


No, backup_dest is destination for backup. It's this variable: 
*backup_dest = self.__backup_dest* .
*self.__backup_dest = os.getenv('HOME')*  -  as you can see, 
self.__backup_dest is a reference to the home directory which is a 
string. Then backup_dest is a reference to the variable 
self.__backup_dest. But that second reference seems to be recognised 
as a NoneType ??


I'm using optparse in my script (that code for checking for 
permission is a part of my backup script).


Maybe i post my complete code:
"
#!/usr/bin/env python
#-*- coding: utf-8 -*-
#MyBackUper

import os, sys, tarfile
import datetime
from optparse import OptionParser


class Except(Exception):
   pass


class Packer(Except):

   def __init__(self):
   self.__backup_dest = os.getenv('HOME')
   __current_time = datetime.datetime.today().strftime('%Y-%m-%d 
%H:%M:%S')

   Except.__init__(self)

   def doc_note(self):

   print """

   DOCUMENTATION

   'Backuper' is a script for doing backups.
   You can backup with it single files or whole directories.
   For available commands type 'backuper.py -h' or 'backuper.py 
--help'.


   Using it is simple:

   - to backup single file, type:
   'backuper.py -f FILE_PATH BCKP_NAME [DEST_PATH]',
   where 'FILE_PATH' is exact path to prefered file which you 
want to backup and 'BCKP_NAME' is the name for backup output file. 
Default 'DEST_PATH' is placed in home directory, it can't be changed. 
'BCKP_NAME' must be given;


   - to backup whole directory, type:
   'backuper.py -d DIR_PATH BCKP_NAME [DEST_PATH]', where 
'DIR_PATH' is exact path to prefered directory, which you want to 
backup and 'BCKP_NAME' is the name for backup output file. Default 
'DEST_PATH' is placed in home directory, it can't be changed. 
'BCKP_NAME' must be given;


   """

   def __check_set_perm(self, rd_obj_path, backup_dest):

   try:

   if os.path.exists(rd_obj_path):
   if not os.access(rd_obj_path, os.R_OK):
   print "Have no permissions on [%s] for reading 
operation.\nTrying to set them..." % os.path.split(rd_obj_path)[1]

   if not os.path.isdir(rd_obj_path):
   os.chmod(rd_obj_path, stat.S_IREAD)
   else:
   for root, dirs, files in os.walk(rd_obj_path):
   for f in files:
   os.chmod(os.path.join(root, f), 
stat.S_IREAD)
   print "Get permissions for reading on [%s] 
successfully." % os.path.split(rd_obj_path)[1]

   else:
   print "Have permissions on [%s] for reading." % 
os.path.split(rd_obj_path)[1]


   if not os.access(backup_dest, os.W_OK):
   print "Have no permissions on [%s] for writing 
operation.\nTrying to set them..." % os.path.split(backup_dest)[1]

   os.chmod(backup_dest, stat.S_IWRITE)
   print "Get permissions for reading on [%s] 
successfully." % os.path.split(backup_dest)[1]

   else:
   print "Have permissions on [%s] for writing." % 
os.path.split(backup_dest)[1]

   else:
   return "Can't find specified path - [%s]." % rd_obj_path
   sys.exit(1)

   except (Except), ex:
   return ex


   def backup_file(self, rd_obj_path, bkp_obj_name):

   try:

   if os.name == "nt":
   rd_obj_path = rd_obj_path.replace('~/', '%s\\' % 
os.getenv('HOME')).replace('\\', '')

   backup_dest = self.__backup_dest
   print "Checking permissions for reading and writing..."
   self.__check_set_perm(rd_obj_path, backup_dest)
   backup_obj = tarfile.open("%s\\%s(%s).tar.bz2" % 
(backup_dest, bkp_obj_name, __current_time), 'w:bz2')


   else:
   rd_obj_path = rd_obj_path.replace('~/', '%s/' % 
os.getenv('HOME'))

   backup_dest = self.__backup_dest
   print "Checking permissions for reading and writing..."
   self.__check_set_perm(rd_obj_path, backup_dest)
   backup_obj = tarfile.open("%s/%s(%s).

Re: Urllib2 not working

2009-08-22 Thread Simon Forman
On Sat, Aug 22, 2009 at 2:12 PM, Carlos Fabian
Ramirez wrote:
> Hello,
> When I try to open a URL using urllib2.urlopen it returns Name or service
> not known. It is not a problem with my Internet I believe, since I have
> Internet access on my computer, and I have verified it is not a syntax, or
> spelling, error on my part. I have also tried accessing the site
> (google.com) by IP like so:
> urllib2.urlopen('http://74.125.67.100/')
> but this does not work either.
> This is a traceback of an attempt to access it through its URL
> (http://www.google.com/)
> http://dpaste.com/84016/
> Any help would be very appreciated.
>

Try googling the error message "Name or service not known" and urllib2.

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


Re: TypeError while checking for permissions with os.access() on windows xp

2009-08-22 Thread Stephen Hansen
>
>
> WTF??
> Why on IDLE it works, but when i run this script in cmd.exe, the
> os.getenv('HOME') goes NoneType?
> I'm to newbie yet to understand this  :/
>

HOME is simply not a standard environment variable that Windows provides.
Any program can set/add environment variables as it deems fit; in this case
it seems IDLE is setting one, but you can't rely on it outside of IDLE.

If you do os.environ["HOMEDRIVE"] + os.environ["HOMEPATH"] it may be what
you want.

Start up cmd.exe, type 'set' -- that's the default/standard environment
variables you have to work with. That's it.

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


Re: difference between raw_input() and input()

2009-08-22 Thread Juraj G.
if you prefix number with zero, it will turn into octal number... I
too wasn't aware of it... at least in python :/
http://en.wikipedia.org/wiki/Octal
It seems like bad practice to put zeroes before any decimal number in
any language :)
Juraj
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: sed/awk/perl: How to replace all spaces each with an underscore that occur before a specific string ?

2009-08-22 Thread Tim Chase

bolega wrote:

sed/awk/perl:


Better to post in the "sed" or "perl" mailing lists rather than a
Python list.  I saw an awk solution flew by.


How to replace all spaces each with an underscore that occur
before a specific string ?

I really prefer a sed one liner.


Here's a one-liner sed solution:

 sed '/to /{s//\n&/;h;s/.*\n//;x;s/\n.*//;s/ /_/g;G;s/\n//}'

There's a reason I prefer Python for these sorts of things: 
readability!  You win 5 free internets (as a stand-in for the 
"real life" you clearly don't have) if you can decipher that in 
under 20 seconds ;-)


expecting-to-see-NO CARRIER-after-typing-that'ly yers,

-tkc
+++ATH0











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


Re: Using 'apply' as a decorator, to define constants

2009-08-22 Thread Jan Kaliszewski

21-08-2009 o 18:09:02 alex23  wrote:


Unfortunately, apply() has been removed as a built-in in 3.x.


You can always implement it yourself :)

def apply(function, args=(), keywords={}):
return function(*args, **keywords)

--
Jan Kaliszewski (zuo) 
--
http://mail.python.org/mailman/listinfo/python-list


Re: Questions on XML

2009-08-22 Thread SUBHABRATA BANERJEE
Should I help you? If you answered my questions I am differing from your
view I do not get any problem in processing Hindi or Bangla or any Indian
language in Python it is perfectly fine.
Best Regards,
Subhabrata.

On Sat, Aug 22, 2009 at 9:48 AM, Rami Chowdhury wrote:

>
> I am using primarily UTF-8 based strings, like Hindi or Bengali. Can I
>> use Python to help me in this regard?
>>
>
> I can say from experience that Python on Windows (at least, Python 2.5 on
> 32-bit Vista) works perfectly well with UTF-8 files containing Bangla. I
> have had trouble with working with the data in IDLE, however, which seems to
> prefer ASCII by default.
>
> -
> Rami Chowdhury
> "Never assume malice when stupidity will suffice." -- Hanlon's Razor
> 408-597-7068 (US) / 07875-841-046 (UK) / 0189-245544 (BD)
>
>
>
>
>
> On Aug 21, 2009, at 19:15 , joy99 wrote:
>
>   Dear Group,
>>
>> I like to convert some simple strings of natural language to XML. May
>> I use Python to do this? If any one can help me, on this.
>>
>> I am using primarily UTF-8 based strings, like Hindi or Bengali. Can I
>> use Python to help me in this regard?
>>
>> How can I learn good XML aspects of Python. If any one can kindly name
>> me a book or URL.
>>
>> I am using Python2.6 on Windows XP with IDLE as GUI.
>>
>> Best Regards,
>> Subhabrata.
>> --
>> http://mail.python.org/mailman/listinfo/python-list
>>
>
>
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Annoying octal notation

2009-08-22 Thread Jan Kaliszewski

22-08-2009 o 21:04:17 Derek Martin  wrote:


On Sat, Aug 22, 2009 at 10:03:35AM +1000, Ben Finney wrote:



These human programmers, whether newbies or long-experienced, also deal
with decimal numbers every day, many of which are presented as a
sequence of digits with leading zeros — and we continue to think of them
as decimal numbers regardless. Having the language syntax opposed to
that is



...consistent with virtually every other popular programming language.


Probably not every other...

Anyway -- being (as it was said) inconsistent with every-day-convention --
it'd be also inconsistent with *Python* conventions, i.e.:

0x <- hex prefix
0b <- bin prefix

Cheers,
*j

--
Jan Kaliszewski (zuo) 
--
http://mail.python.org/mailman/listinfo/python-list


Re: string literal vs string variable

2009-08-22 Thread Piet van Oostrum
> Kee Nethery  (KN) wrote:

>KN> On Aug 22, 2009, at 3:32 AM, Stefan Behnel wrote:

>>> 
>>> You can use both, but I suspect parsing from StringIO to be slower  than
>>> parsing from the string directly. That's the case for lxml, at least.
>>> 
>>> Note that fromstring() behaves the same as XML(), but it reads  better
>>> when
>>> parsing from a string variable. XML() reads better when parsing from a
>>> literal string.

>KN> I'm not sure I know the difference between a string variable and a  literal
>KN> string. Is the difference as simple as:

>KN> somestring = u'hello world'
>KN> fromstring(somestring)  <-- string variable
>KN> vs
>KN> XML(u'hello world')  <-- literal string

Yes.

Stefan probably means `looks better for the human reader' when he says
`reads better'. XML and fromstring are just different names for the same
function. 
-- 
Piet van Oostrum 
URL: http://pietvanoostrum.com [PGP 8DAE142BE17999C4]
Private email: [email protected]
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: sed/awk/perl: How to replace all spaces each with an underscore that occur before a specific string ?

2009-08-22 Thread Jan Kaliszewski

22-08-2009 o 20:11:32 bolega  wrote:


sed/awk/perl:

How to replace all spaces each with an underscore that occur before a
specific string ?


$ rm -rf /home/bolega ; python -c 'for i in xrange(1000): print "I will  
never crosspost senselessly."'


;~]

--
Jan Kaliszewski (zuo) 
--
http://mail.python.org/mailman/listinfo/python-list


Re: Questions on XML

2009-08-22 Thread joy99
On Aug 22, 12:16 pm, Rami Chowdhury  wrote:
> > encoding declaration to the top of your source file if you use encoded
> > literal strings in your code
>
> Any tips for how to set the encoding in IDLE? printing the Unicode  
> strings works -- trying to repr() the variable chokes with a  
> UnicodeDecodeError, and trying to enter the literals inside IDLE just  
> gets me '?' characters instead.
>
> (this is Python 2.5 + IDLE installed from the Python-2.5.msi on  
> python.org)
>
> -
> Rami Chowdhury
> "Never assume malice when stupidity will suffice." -- Hanlon's Razor
> 408-597-7068 (US) / 07875-841-046 (UK) / 0189-245544 (BD)
>
> On Aug 21, 2009, at 22:53 , Stefan Behnel wrote:
>
>
>
> > Rami Chowdhury wrote:
> >>> I am using primarily UTF-8 based strings, like Hindi or Bengali.  
> >>> Can I
> >>> use Python to help me in this regard?
>
> >> I can say from experience that Python on Windows (at least, Python  
> >> 2.5
> >> on 32-bit Vista) works perfectly well with UTF-8 files containing
> >> Bangla. I have had trouble with working with the data in IDLE,  
> >> however,
> >> which seems to prefer ASCII by default.
>
> > Defaults almost never work for encodings. You have to be explicit:  
> > add an
> > encoding declaration to the top of your source file if you use encoded
> > literal strings in your code; use the codecs module with a suitable
> > encoding to read encoded text files, and use an XML parser when  
> > reading XML.
>
> > Stefan
> > --
> >http://mail.python.org/mailman/listinfo/python-list- Hide quoted text -
>
> - Show quoted text -

Dear Sir,

There is no big issue for this. I simply downloaded Python2.5 and
using IDLE on Windows XP with service pack 2. I used Python 2.5
earlier and presently using Python 2.6 and it is also perfectly fine.
I am not using it for small programs but machine learning programs,
where code itself runs few million lines over some terabytes of Hindi
and Bengali data, and I never found any problem. What is the exact
nature of problem you are getting? If you can kindly specify. If
possible, with some sample codes. And printing is also never been a
problem, all good printers like HP,Epson,Xerox,Canon are printing
Hindi or Bengali data finely.
If you can kindly specify the problem.

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


Re: string literal vs string variable

2009-08-22 Thread Jan Kaliszewski

22-08-2009 o 19:46:51 Kee Nethery  wrote:

I'm not sure I know the difference between a string variable and a  
literal string. Is the difference as simple as:


somestring = u'hello world'
fromstring(somestring)  <-- string variable
vs
XML(u'hello world')  <-- literal string


Yes, simply:

s = 'hello world'
#^
#it is a *string literal*

s  # <- it is a *string object*
   #(or rather a name referring to it :-))

(In Python we have rather 'names' than 'variables', though -- as
a mental leap -- they are commonly referred to as 'variables',
regarding other languages' terminology).

Cheers,
*j

--
Jan Kaliszewski (zuo) 
--
http://mail.python.org/mailman/listinfo/python-list


ANN: mock 0.6.0, Python mocking and testing library

2009-08-22 Thread Fuzzyman
mock is a Python mock object library for testing, with additional
support for runtime monkey patching.

Most mocking libraries follow the ‘record -> replay’ pattern of
mocking. I prefer the ‘action -> assertion’ pattern, which is more
readable and intuitive; particularly when working with the Python
unittest module.

This release, version 0.6.0, is a minor release but with a few new
features:

* mock homepage http://www.voidspace.org.uk/python/mock/
* download http://www.voidspace.org.uk/downloads/mock-0.6.0.zip
* PDF documentation http://www.voidspace.org.uk/downloads/mock.pdf
* Google code project and SVN repository http://code.google.com/p/mock/

New in 0.6.0:

* New test layout compatible with test discovery
* Descriptors (static methods / class methods etc) can now be patched
and restored correctly
* Mocks can raise exceptions when called by setting side_effect to an
exception class or instance
* Mocks that wrap objects will not pass on calls to the underlying
object if an explicit return_value is set

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


Re: logging SMTPhandler Error

2009-08-22 Thread Bev in TX
On Aug 21, 8:34 am, Bev in TX  wrote:
> Hi,
>
> I've done some Python programming, but I still consider myself a
> Python newbie.  I have a Mac Pro OS X 10.5.8 system and I installed
> Python 2.6.2 (the latest package available for the Mac) yesterday.
>
> I was working through Matt Wilson's article on using the logging
> module:http://blog.tplus1.com/index.php/2007/09/28/the-python-logging-module...
> (If that does not work, then try:http://tinyurl.com/5v2lcy)
>
> Everything worked great until his last example.  My ISP does not
> provide e-mail, so I tried using gmail in the line that sets h2.  I
> substituted "mailid" for my actual e-mail address in the following
> examples; in my test I used my actual e-mail ID.  Also, I used the
> full path to the newly installed Python 2.6.2; otherwise it picked up
> the older Python 2.5:
> #!/Library/Frameworks/Python.framework/Versions/2.6/bin/python
>
> First attempt:
> h2 = logging.handlers.SMTPHandler('smtp.gmail.com', '[email protected]',
> ['[email protected]'],'ERROR log')
> However, that caused the following error to be issued:
>
> Traceback (most recent call last):
>   File "/Library/Frameworks/Python.framework/Versions/2.6/lib/
> python2.6/logging/handlers.py", line 868, in emit
>     smtp.sendmail(self.fromaddr, self.toaddrs, msg)
>   File "/Library/Frameworks/Python.framework/Versions/2.6/lib/
> python2.6/smtplib.py", line 698, in sendmail
>     raise SMTPSenderRefused(code, resp, from_addr)
> SMTPSenderRefused: (530, '5.7.0 Must issue a STARTTLS command first.
> 7sm3867994qwf.47', '[email protected]')
>
> I also tried providing my gmail userid/password, I tried adding a 5th,
> credential, argument, which is a tupple, (username,password) (new in
> 2.6).
>
> Second attempt:
> h2 = logging.handlers.SMTPHandler('smtp.gmail.com', '[email protected]',
> ['[email protected]'],'ERROR log',('[email protected]','gmail-
> password'))
> However, that caused the following error message:
>
> Traceback (most recent call last):
>   File "/Library/Frameworks/Python.framework/Versions/2.6/lib/
> python2.6/logging/handlers.py", line 867, in emit
>     smtp.login(self.username, self.password)
>   File "/Library/Frameworks/Python.framework/Versions/2.6/lib/
> python2.6/smtplib.py", line 552, in login
>     raise SMTPException("SMTP AUTH extension not supported by
> server.")
> SMTPException: SMTP AUTH extension not supported by server.
>
> I am able access gmail via Mac's Mail, in which it says that outgoing
> mail is going to:
>       smtp.gmail.com:mailid
> I tried using that as the server in the Python script, but it could
> not find that server.
>
> Is this possible?  If I am doing something incorrectly, would someone
> please indicate what it is?
>
> Thanks,
> Bev in TX

Today I tried this with both Python 2.6.2 and 3.1.1 on an XP system,
with the same error.  For the 3.1.1 test, I had to change the syntax
of the exception from "except exc, var" to "except exc as var".  Here
is the modified script, with my email ID changed to "mailid":

import logging
import logging.handlers

#Make a global logging object
x = logging.getLogger("logfun")
x.setLevel(logging.DEBUG)

#This handler writes out everything to stdout
h1 = logging.StreamHandler()
f = logging.Formatter("%(levelname)s %(asctime)s %(funcName)s %(lineno)
d %(message)s")
h1.setFormatter(f)
h1.setLevel(logging.DEBUG)
x.addHandler(h1)

#This handler emails me anything that is an error or worse
h2 = logging.handlers.SMTPHandler('smtp.gmail.com', '[email protected]',
['[email protected]'],'ERROR log')
f = logging.Formatter("%(levelname)s %(asctime)s %(funcName)s %(lineno)
d %(message)s")
h2.setLevel(logging.ERROR)
h2.setFormatter(f)
x.addHandler(h2)

def g():
  1 / 0

def f():
  logfun = logging.getLogger("logfun")
  logfun.debug("inside f!")
  try:
g()
  except Exception as ex:
logfun.exception("Something awful happened!")
  logfun.debug("Finishing f!")

if __name__ == "__main__":
  f()

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


Re: TypeError while checking for permissions with os.access() on windows xp

2009-08-22 Thread ryniek90




WTF??
Why on IDLE it works, but when i run this script in cmd.exe, the
os.getenv('HOME') goes NoneType?
I'm to newbie yet to understand this  :/


HOME is simply not a standard environment variable that Windows 
provides. Any program can set/add environment variables as it deems 
fit; in this case it seems IDLE is setting one, but you can't rely on 
it outside of IDLE.


If you do os.environ["HOMEDRIVE"] + os.environ["HOMEPATH"] it may be 
what you want.


Start up cmd.exe, type 'set' -- that's the default/standard 
environment variables you have to work with. That's it.


--S


Yep, now that variable works fine. Thanks.
--
http://mail.python.org/mailman/listinfo/python-list


Numeric literals in other than base 10 - was Annoying octal notation

2009-08-22 Thread James Harris
On 22 Aug, 10:27, David <[email protected]> wrote:

... (snipped a discussion on languages and other systems interpreting
numbers with a leading zero as octal)

> > Either hexadecimal should have been 0h or octal should
> > have been 0t :=)
>
>
> I have seen the use of Q/q instead in order to make it clearer. I still
> prefer Smalltalk's 16rFF and 8r377.
>
>
> Two interesting options. In a project I have on I have also considered
> using 0q as indicating octal. I maybe saw it used once somewhere else
> but I have no idea where. 0t was a second choice and 0c third choice
> (the other letters of oct). 0o should NOT be used for obvious reasons.
>
> So you are saying that Smalltalk has r where
> r is presumably for radix? That's maybe best of all. It preserves the
> syntactic requirement of starting a number with a digit and seems to
> have greatest flexibility. Not sure how good it looks but it's
> certainly not bad.
>
>
> >   0xff & 0x0e | 0b1101
> >   16rff & 16r0e | 2r1101
>
> > Hmm. Maybe a symbol would be better than a letter.

...

> >Or Ada's16#FF#, 8#377#...

> >I forget if DEC/VMS FORTRAN or Xerox Sigma FORTRAN used x'FF' or
> >   'FF'x, and o'377' or '377'o

...

>
> What about 2_1011, 8_7621, 16_c26h or 2;1011, 8;7621, 16;c26h ?

They look good - which is important. The trouble (for me) is that I
want the notation for a new programming language and already use these
characters. I have underscore as an optional separator for groups of
digits - 123000 and 123_000 mean the same. The semicolon terminates a
statement. Based on your second idea, though, maybe a colon could be
used instead as in

  2:1011, 8:7621, 16:c26b

I don't (yet) use it as a range operator.

I could also use a hash sign as although I allow hash to begin
comments it cannot be preceded by anything other than whitespace so
these would be usable

  2#1011, 8#7621, 16#c26b

I have no idea why Ada which uses the # also apparently uses it to end
a number

  2#1011#, 8#7621#, 16#c26b#

Copying this post also to comp.lang.misc. Folks there may either be
interested in the discussion or have comments to add.

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


Items inheriting attributes from its container?

2009-08-22 Thread Kreso
I would like to create a list-like container class so that, additionally
to usual list methods, I could attach attributes to the container instances.
However, I would like it so that the items contained in the particular
instance of container somehow 'inherit' those attributes i.e.

cont = Container()
cont.color = 'blue'
cont.append(item)
print item.color
'blue'

The example appended below does that, but with the restriction that container
attributes must be set in the instantiation phase. This is actually fine
for me at the moment because my objects are "read only", but I would
like to hear about better solutions, with more flexibility, please.


#-8<
class Player:
"""Class for items"""

def __init__(self, playerdata, team):
self.data = playerdata
for key in team.__dict__:
setattr(self, key, team.__dict__[key])
return


class Team(list):
"""Class for containers"""

def __init__(self, teamdata, playerdata):
for key in teamdata:
setattr(self, key, teamdata[key])
for item in playerdata:
self.append(Player(item, self))
return


lakersdata = {'name' : 'Lakers', 'kitcolor' : 'yellow'}
lakersplayers = [['Kobe', 'PG', 12, 123], ['Kareem', 'FW', 23, 345]]

lakers = Team(lakersdata, lakersplayers)

# This is fine:
p1 = lakers[1]
print p1.kitcolor

# However the following doesn't work:
lakers.kitcolor = 'blue'
print p1.kitcolor

#-8<
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: line completion

2009-08-22 Thread Chris Rebert
On Thu, Aug 20, 2009 at 9:06 AM, Steven Woody wrote:
> Hi,
> I wrote a program that takes some user input.  Many inputs are quit often
> used by user, so when a user launch the program, and type in "The Sha",  he
> wants to get "wshank Redemption" displayed automatically in reversed color
> (black text on white background) along his cursor.  When he decided to use
> "The Shawshank Redemption", he just press the enter key, otherwise, he type
> in other words anyway.
> My question is, is there any module that can easy the implementation of this
> feature?  Thanks.

The `readline` module in the standard library:
http://docs.python.org/library/readline.html

Although I don't know how to get it to colorize the interface.

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


Re: sed/awk/perl: How to replace all spaces each with an underscore that occur before a specific string ?

2009-08-22 Thread Ed Morton
On Aug 22, 1:11 pm, bolega  wrote:
> sed/awk/perl:
>
> How to replace all spaces each with an underscore that occur before a
> specific string ?
>
> I really prefer a sed one liner.

Why?

> Example
> Input :  This is my book. It is too  thick to read. The author gets
> little royalty but the publisher makes a lot.
> Output: This_is_my_book._It_is_too__thick_to read. The author gets
> little royalty but the publisher makes a lot.
>
> We replaced all the spaces with underscores before the first occurence
> of the string "to ".

No, you replaced all ... the string "to " (note the space).

awk '{idx=index($0,"to "); tgt=substr($0,1,idx-1); gsub(/ /,"_",tgt);
print tgt substr($0,idx)}' file

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


Re: Items inheriting attributes from its container?

2009-08-22 Thread Christian Heimes

Kreso schrieb:

I would like to create a list-like container class so that, additionally
to usual list methods, I could attach attributes to the container instances.
However, I would like it so that the items contained in the particular
instance of container somehow 'inherit' those attributes i.e.


The Python web application framework Zope implements the feature under 
the name "acquisition". Happy Googling!


Christian

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


Re: Numeric literals in other than base 10 - was Annoying octal notation

2009-08-22 Thread Mel
James Harris wrote:

> I have no idea why Ada which uses the # also apparently uses it to end
> a number
> 
>   2#1011#, 8#7621#, 16#c26b#

Interesting.  They do it because of this example from 
:

2#1#E8-- an integer literal of value 256

where the E prefixes a power-of-2 exponent, and can't be taken as a digit of 
the radix.  That is to say

16#1#E2

would also equal 256, since it's 1*16**2 .


Mel.

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


Re: Annoying octal notation

2009-08-22 Thread Carl Banks
On Aug 22, 12:04 pm, Derek Martin  wrote:
> On Sat, Aug 22, 2009 at 10:03:35AM +1000, Ben Finney wrote:
> > These human programmers, whether newbies or long-experienced, also deal
> > with decimal numbers every day, many of which are presented as a
> > sequence of digits with leading zeros — and we continue to think of them
> > as decimal numbers regardless. Having the language syntax opposed to
> > that is
>
> ...consistent with virtually every other popular programming language.


If you know anything about Python, you should know that "consistent
with virtually every other programming langauge" is, at most, a polite
suggestion for how Python should do it.


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


Re: Items inheriting attributes from its container?

2009-08-22 Thread Stephen Fairchild
Kreso wrote:

> I would like to create a list-like container class so that, additionally
> to usual list methods, I could attach attributes to the container
> instances. However, I would like it so that the items contained in the
> particular instance of container somehow 'inherit' those attributes i.e.
> 
> cont = Container()
> cont.color = 'blue'
> cont.append(item)
> print item.color
> 'blue'
> 
> The example appended below does that, but with the restriction that
> container attributes must be set in the instantiation phase. This is
> actually fine for me at the moment because my objects are "read only", but
> I would like to hear about better solutions, with more flexibility,
> please.
> 
> 
> #-8<
> class Player:
> """Class for items"""
> 
> def __init__(self, playerdata, team):
> self.data = playerdata
> for key in team.__dict__:
> setattr(self, key, team.__dict__[key])
> return
> 
> 
> class Team(list):
> """Class for containers"""
> 
> def __init__(self, teamdata, playerdata):
> for key in teamdata:
> setattr(self, key, teamdata[key])
> for item in playerdata:
> self.append(Player(item, self))
> return
> 
> 
> lakersdata = {'name' : 'Lakers', 'kitcolor' : 'yellow'}
> lakersplayers = [['Kobe', 'PG', 12, 123], ['Kareem', 'FW', 23, 345]]
> 
> lakers = Team(lakersdata, lakersplayers)
> 
> # This is fine:
> p1 = lakers[1]
> print p1.kitcolor
> 
> # However the following doesn't work:
> lakers.kitcolor = 'blue'
> print p1.kitcolor
> 
> #-8<

I hope this gives you some good ideas.

http://en.wikipedia.org/wiki/Join_(SQL)

I suspect you will be finding a use for the special __getattr__ method,
which is called when an attribute is not found. This can be used to search
on your set of joined objects. Your list of joined objects should be a
set() to prevent duplicates.
-- 
Stephen Fairchild
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Annoying octal notation

2009-08-22 Thread Carl Banks
On Aug 21, 12:48 pm, Derek Martin  wrote:
> John Nagle wrote:
> > Yes, and making lead zeros an error as suggested in PEP 3127 is a
> > good idea.  It will be interesting to see what bugs that flushes
> > out.
> James Harris wrote:
> > It maybe made sense once but this relic of the past should have been
> > consigned to the waste bin of history long ago.
>
> Sigh.  Nonsense.  I use octal notation *every day*, for two extremely
> prevalent purposes: file creation umask, and Unix file permissions
> (i.e. the chmod() function/command).

Unix file permissions maybe made sense once but this relic of the past
should have been consigned to the waste bin of history long ago.  :)


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


Re: logging SMTPhandler Error

2009-08-22 Thread Ned Deily
In article 
,
 Bev in TX  wrote:
> On Aug 21, 8:34 am, Bev in TX  wrote:
> > Hi,
> >
> > I've done some Python programming, but I still consider myself a
> > Python newbie.  I have a Mac Pro OS X 10.5.8 system and I installed
> > Python 2.6.2 (the latest package available for the Mac) yesterday.
> >
> > I was working through Matt Wilson's article on using the logging
> > module:http://blog.tplus1.com/index.php/2007/09/28/the-python-logging-module
> > ...
> > (If that does not work, then try:http://tinyurl.com/5v2lcy)
> >
> > Everything worked great until his last example.  My ISP does not
> > provide e-mail, so I tried using gmail in the line that sets h2.  I
> > substituted "mailid" for my actual e-mail address in the following
> > examples; in my test I used my actual e-mail ID.  Also, I used the
> > full path to the newly installed Python 2.6.2; otherwise it picked up
> > the older Python 2.5:
> > #!/Library/Frameworks/Python.framework/Versions/2.6/bin/python
> >
> > First attempt:
> > h2 = logging.handlers.SMTPHandler('smtp.gmail.com', '[email protected]',
> > ['[email protected]'],'ERROR log')
> > However, that caused the following error to be issued:
> >
> > Traceback (most recent call last):
> >   File "/Library/Frameworks/Python.framework/Versions/2.6/lib/
> > python2.6/logging/handlers.py", line 868, in emit
> >     smtp.sendmail(self.fromaddr, self.toaddrs, msg)
> >   File "/Library/Frameworks/Python.framework/Versions/2.6/lib/
> > python2.6/smtplib.py", line 698, in sendmail
> >     raise SMTPSenderRefused(code, resp, from_addr)
> > SMTPSenderRefused: (530, '5.7.0 Must issue a STARTTLS command first.
> > 7sm3867994qwf.47', '[email protected]')

The problem here is that gmail, like most modern mail services, requires 
the use of an encrypted (SSL or TLS) connection for mail relaying so 
that the required user name and password are not sent in the clear.  The 
logging SMTP handler uses the smtplib module from the standard module to 
send mail and, when built properly, smtplib does support TLS.  However, 
the caller of smtplib does need to do some extra work in that case, i.e. 
it needs to call the server object's starttls method at the right point 
in the protocol handshaking. It's currently not done automatically in 
smtplib and, unfortunately, there is no code in the logging smtp handler 
to detect the need for and to call starttls (in response to a 
250-STARTTLS response to an EHLO).

To make this work, either the logging module or, perhaps better, the 
smptlib module needs to be smarter about this case.  I didn't see an 
open issue on the Python bug tracker about this; you might want to open 
one.  In the meantime, some options would be to find an SMTP mail host 
that doesn't require TLS.  Or write a custom logger.  Or on OS X it's 
not *too* difficult to set up a local host mailer using the 
Apple-supplied prefix that would accept mail locally and forward it to a 
more sophisticated remote mailer.

-- 
 Ned Deily,
 [email protected]

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


Re: sed/awk/perl: How to replace all spaces each with an underscore that occur before a specific string ?

2009-08-22 Thread John W. Krahn

bolega wrote:

sed/awk/perl:

How to replace all spaces each with an underscore that occur before a
specific string ?

I really prefer a sed one liner.

Example
Input :  This is my book. It is too  thick to read. The author gets
little royalty but the publisher makes a lot.
Output: This_is_my_book._It_is_too__thick_to read. The author gets
little royalty but the publisher makes a lot.

We replaced all the spaces with underscores before the first occurence
of the string "to ".


$ perl -le'
$x = "This is my book. It is too  thick to read. The author gets little 
royalty but the publisher makes a lot.";

print $x;
$x =~ /to / && substr( $x, 0, $-[0] ) =~ tr/ /_/;
print $x;
'
This is my book. It is too  thick to read. The author gets little 
royalty but the publisher makes a lot.
This_is_my_book._It_is_too__thick_to read. The author gets little 
royalty but the publisher makes a lot.





John
--
Those people who think they know everything are a great
annoyance to those of us who do.-- Isaac Asimov
--
http://mail.python.org/mailman/listinfo/python-list


Re: sed/awk/perl: How to replace all spaces each with an underscore that occur before a specific string ?

2009-08-22 Thread MRAB

John W. Krahn wrote:

bolega wrote:

sed/awk/perl:

How to replace all spaces each with an underscore that occur before a
specific string ?

I really prefer a sed one liner.

Example
Input :  This is my book. It is too  thick to read. The author gets
little royalty but the publisher makes a lot.
Output: This_is_my_book._It_is_too__thick_to read. The author gets
little royalty but the publisher makes a lot.

We replaced all the spaces with underscores before the first occurence
of the string "to ".


$ perl -le'
$x = "This is my book. It is too  thick to read. The author gets little 
royalty but the publisher makes a lot.";

print $x;
$x =~ /to / && substr( $x, 0, $-[0] ) =~ tr/ /_/;
print $x;
'
This is my book. It is too  thick to read. The author gets little 
royalty but the publisher makes a lot.
This_is_my_book._It_is_too__thick_to read. The author gets little 
royalty but the publisher makes a lot.



If you're interested in a Python regex solution:

s = "This is my book. It is too  thick to read. The author gets little 
royalty but the publisher makes a lot."

s = re.sub(".*?(?=to )", lambda m: m.group().replace(" ", "_"), s)

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


Re: logging SMTPhandler Error

2009-08-22 Thread Bev in TX
On Aug 22, 7:07 pm, Ned Deily  wrote:
>
> The problem here is that gmail, like most modern mail services, requires
> the use of an encrypted (SSL or TLS) connection for mail relaying so
> that the required user name and password are not sent in the clear.  The
> logging SMTP handler uses the smtplib module from the standard module to
> send mail and, when built properly, smtplib does support TLS.  However,
> the caller of smtplib does need to do some extra work in that case, i.e.
> it needs to call the server object's starttls method at the right point
> in the protocol handshaking. It's currently not done automatically in
> smtplib and, unfortunately, there is no code in the logging smtp handler
> to detect the need for and to call starttls (in response to a
> 250-STARTTLS response to an EHLO).
>
> To make this work, either the logging module or, perhaps better, the
> smptlib module needs to be smarter about this case.  I didn't see an
> open issue on the Python bug tracker about this; you might want to open
> one.  In the meantime, some options would be to find an SMTP mail host
> that doesn't require TLS.  Or write a custom logger.  Or on OS X it's
> not *too* difficult to set up a local host mailer using the
> Apple-supplied prefix that would accept mail locally and forward it to a
> more sophisticated remote mailer.
>
> --
>  Ned Deily,
>  [email protected] Hide quoted text -
>
> - Show quoted text -

Thanks for the excellent and informative response :-).  I'll
investigate further, as you suggested, now that I understand what is
happening.

Bev in TX
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Items inheriting attributes from its container?

2009-08-22 Thread Jan Kaliszewski

23-08-2009 Kreso  wrote:


I would like to create a list-like container class so that, additionally
to usual list methods, I could attach attributes to the container  
instances.

However, I would like it so that the items contained in the particular
instance of container somehow 'inherit' those attributes i.e.

cont = Container()
cont.color = 'blue'
cont.append(item)
print item.color
'blue'


[snip]


class Player:
"""Class for items"""

def __init__(self, playerdata, team):
self.data = playerdata
for key in team.__dict__:
setattr(self, key, team.__dict__[key])
return


class Team(list):
"""Class for containers"""
   def __init__(self, teamdata, playerdata):
for key in teamdata:
setattr(self, key, teamdata[key])
for item in playerdata:
self.append(Player(item, self))
return

lakersdata = {'name' : 'Lakers', 'kitcolor' : 'yellow'}
lakersplayers = [['Kobe', 'PG', 12, 123], ['Kareem', 'FW', 23, 345]]

lakers = Team(lakersdata, lakersplayers)


[snip]


p1 = lakers[1]
print p1.kitcolor


[snip]


lakers.kitcolor = 'blue'
print p1.kitcolor


[Not tested. I believe that the idea is clear]


  class RecruitmentError(ValueError):
  """Raised when a player cannot be recruited."""


  class Player(object):
  """A potential item of a Team() instance."""

  def __init__(self, playerdata, team=None):
  self.data = playerdata
  self.team = None
  if team:
  team.append(self)

  # (called when the usual attribute lookup didn't succeed)
  def __getattr__(self, name):
  return getattr(self.team, name)

  # ^ the last method may not be necessary -- you can always
  #   access to team data via 'team' attribute
  #   (and 'explicit is better than implicit')


  class Team(list):
  """A container for Player() instances."""

  def __init__(self, teamdata, playerdata=None):
  for key in teamdata:
  setattr(self, key, teamdata[key])
  for data in playerdata:
  self.append(Player(data))

  def _validate_and_get(self, index=None, player=None):
  if player is not None and not isinstance(player, Player):
  raise TypeError('A Player instance is required')
  if index is not None:
  if isinstance(index, slice):
  raise TypeError('Slicing is not allowed')
  return self[index]  # (raise IndexError for bad index)

  _validate = _validate_and_get

  def _recruit(self, player):
  if player.team is None:
  player.team = self
  else:
  raise RecruitmentError('Player %s has already recruited')

  def _dismiss(self, player):
  player.team = None

  def __setitem__(self, index, player):
  current = self._validate_and_get(index, player)
  if player is not current:
  self._recruit(player)
  self._dismiss(current)
  list.__setitem__(self, index, player)

  def __delitem__(self, index):
  player = self._validate_and_get(index)
  self._dismiss(player)
  list.__delitem__(self, index)

  def append(self, player):
  self._validate(player=player)
  self._recruit(player)
  list.append(self, player)

  # and similarly:
  # * def extend...
  # * def insert...
  # * def pop...
  # * def remove...

...if you really need ordered container (reflecting functions/hierarchy
of players in a team?).

Otherwise, as Stephen noted, you should subclass set rather than list.

Cheers,
*j

--
Jan Kaliszewski (zuo) 
--
http://mail.python.org/mailman/listinfo/python-list


Re: Items inheriting attributes from its container?

2009-08-22 Thread Jan Kaliszewski

PS. Erratum:


   class Team(list):
   """A container for Player() instances."""

   def __init__(self, teamdata, playerdata=None):


*** Here should be added: list.__init__(self) ***


   for key in teamdata:
   setattr(self, key, teamdata[key])
   for data in playerdata:
   self.append(Player(data))


--
Jan Kaliszewski (zuo) 
--
http://mail.python.org/mailman/listinfo/python-list


Re: your favorite debugging tool?

2009-08-22 Thread Ben Finney
Esmail  writes:

> What is your favorite tool to help you debug your code?

A “print” statement (or equivalent, like logging output).

> I've been getting along with 'print' statements but that is getting
> old and somewhat cumbersome.

Whenever a simple output statement is too cumbersome for debugging, I
take it as a sign that the program is too cumbersome to follow.

My debugging questions at that point are best taken to the unit test
suite: record the questions and the expected answers, so that in the
future anyone can get the benefit of them in a repeatable and automated
fashion.

-- 
 \ “The power of accurate observation is frequently called |
  `\cynicism by those who don't have it.” —George Bernard Shaw |
_o__)  |
Ben Finney
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: logging SMTPhandler Error

2009-08-22 Thread Ned Deily
In article 
<[email protected]>,
 Bev in TX  wrote:
> On Aug 22, 7:07 pm, Ned Deily  wrote:
> > [...] Or on OS X it's
> > not *too* difficult to set up a local host mailer using the
> > Apple-supplied prefix that would accept mail locally and forward it to a
> > more sophisticated remote mailer.

Um, notation fail:  s/prefix/Postfix/

-- 
 Ned Deily,
 [email protected]

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


Re: Annoying octal notation

2009-08-22 Thread Derek Martin
On Sat, Aug 22, 2009 at 02:55:51AM +, Steven D'Aprano wrote:
> > I can see how 012 can
> > be confusing to new programmers, but at least it's legible, and the
> > great thing about humans is that they can be taught (usually).
> 
> And the great thing is that now you get to teach yourself to stop writing 
> octal numbers implicitly and be write them explicitly with a leading 0o 
> instead :)

Sorry, I don't write them implicitly.  A leading zero explicitly
states that the numeric constant that follows is octal.  It is so in 6
out of 7 computer languages I have more than a passing familiarity
with (the 7th being scheme, which is a thing unto itself), including
Python.  It's that way on Bourne-compatible and POSIX-compatible Unix
shells (though it requires a leading backslash before the leading zero
there).  I'm quite certain it can not be the case on only those 6
languages that I happen to be familiar with...

While it may be true that people commonly write decimal numbers with
leading zeros (I dispute even this, having to my recollection only
recently seen it as part of some serial number, which in practice is
really more of a string identifier than a number, often containing
characters other than numbers), it's also true that in the context of
computer programming languages, for the last 40+ years, a number
represented with a leading zero is most often an octal number.  This
has been true even in Python for nearly *twenty years*.  Why the
sudden need to change it?

So no, I don't get to teach myself to stop writing octal numbers with
a leading zero.  Instead, I have to remember an exception to the rule.

Also I don't think it's exactly uncommon for computer languages to do
things differently than they are done in non-CS circles.  A couple of
easy examples: we do not write x+=y except in computer languages.  The
POSIX system call to create a file is called creat().  If you think
about it, I'm sure you can come up with lots of examples where even
Python takes liberties.  Is this a bad thing?  Not inherently, no.
Will it be confusing to people who aren't familiar with the usage?
Quite possibly, but that is not inherently bad either.  It's all about
context.

> Use of octal isn't common. 

It's common enough.  Peruse the include files for your C libraries, or
the source for your operating system's kernel, or system libraries,
and I bet you'll find plenty of octal.  I did.  [Note that it is
irrelevant that these are C/C++ files; here we are only concerned with
whether they use octal, not how it is represented therein.]  I'd guess
there's a fair chance that any math or scientific software package
uses octal.  Octal is a convenient way to represent bit fields that
naturally occur in groups of 3, of which there are potentially
limitless cases.  

> You've given two cases were octal notation is useful, but for every
> coder who frequently writes umasks on Unix systems, there are a
> thousand who don't.

I gave two cases that I use *daily*, or very nearly daily.  My hats
currently include system admin, SQA, and software development, and I
find it convenient to use octal in each of those.  But those are
hardly the only places where octal is useful.  Have a look at the
ncurses library, for example.  Given that Python has an ncurses
interface, I'm guessing it's used there too.  In fact if the Python
source had no octal in it, I would find that very surprising.

> It's no hardship to write 0o12 instead of 012.

Computer languages are not write-only, excepting maybe Perl. ;-)
Writing 0o12 presents no hardship; but I assert, with at least some
support from others here, that *reading* it does.

-- 
Derek D. Martin
http://www.pizzashack.org/
GPG Key ID: 0x81CFE75D



pgp2DtXpzfNjd.pgp
Description: PGP signature
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Annoying octal notation

2009-08-22 Thread Derek Martin
On Fri, Aug 21, 2009 at 04:23:57PM -0700, James Harris wrote:
> You misunderstand. I was saying that taking a leading zero as
> indicating octal is archaic. Octal itself is fine where appropriate.

I don't see that the leading zero is any more archaic than the use of
octal itself...  Both originate from around the same time period, and
are used in the same cases.  We should just prohibit octal entirely
then.

But I suppose it depends on which definition of "archaic" you use.  In
the other common sense of the word, the leading zero is no more
archaic than the C programming language.  Let's ban the use of all
three. :)  (I believe C is still the language in which the largest
number of lines of new code are written, but if not, it's way up
there.)
 
> The chmod command doesn't require a leading zero.

No, but it doesn't need any indicator that the number given to it is
in octal; in the case of the command line tool, octal is *required*,
and the argument is *text*.  However, the chmod() system call, and the
interfaces to it in every language I'm familiar with that has one, do
require the leading zero (because that's how you represent octal).
Including Python, for some 20 years or so.

-- 
Derek D. Martin
http://www.pizzashack.org/
GPG Key ID: 0x81CFE75D



pgpf8DJDYdSjx.pgp
Description: PGP signature
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Numeric literals in other than base 10 - was Annoying octal notation

2009-08-22 Thread Richard Harter
On Sat, 22 Aug 2009 14:54:41 -0700 (PDT), James Harris
 wrote:

>On 22 Aug, 10:27, David <[email protected]> wrote:
>
>... (snipped a discussion on languages and other systems interpreting
>numbers with a leading zero as octal)
>
>> > Either hexadecimal should have been 0h or octal should
>> > have been 0t :=3D)
>>
>>
>> I have seen the use of Q/q instead in order to make it clearer. I still
>> prefer Smalltalk's 16rFF and 8r377.
>>
>>
>> Two interesting options. In a project I have on I have also considered
>> using 0q as indicating octal. I maybe saw it used once somewhere else
>> but I have no idea where. 0t was a second choice and 0c third choice
>> (the other letters of oct). 0o should NOT be used for obvious reasons.
>>
>> So you are saying that Smalltalk has r where
>> r is presumably for radix? That's maybe best of all. It preserves the
>> syntactic requirement of starting a number with a digit and seems to
>> have greatest flexibility. Not sure how good it looks but it's
>> certainly not bad.

I opine that a letter is better; special characters are a
valuable piece of real estate.  However for floating point you
need at least three letters because a floating point number has
three parts: the fixed point point, the exponent base, and the
exponent.  Now we can represent the radices of the individual
parts with the 'r'scheme, e.g., 2r101001, but we need separate
letters to designate the exponent base and the exponent.  B and E
are the obvious choices, though we want to be careful about a
confusion with 'b' in hex.  For example, using 'R',

3R20.1B2E16Rac

is 20.1 in trinary (6 1/3) times 2**172 (hex ac).

I grant that this example looks a bit gobbledegookish, but normal
usage would be much simpler.  The notation doesn't handle
balanced trinary; however I opine that balanced trinary requires
special notation.

   
Richard Harter, [email protected]
http://home.tiac.net/~cri, http://www.varinoma.com
No one asks if a tree falls in the forest 
if there is no one there to see it fall.
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Annoying octal notation

2009-08-22 Thread Steven D'Aprano
On Sat, 22 Aug 2009 14:04:17 -0500, Derek Martin wrote:

>> These human programmers, whether newbies or long-experienced, also deal
>> with decimal numbers every day, many of which are presented as a
>> sequence of digits with leading zeros — and we continue to think of
>> them as decimal numbers regardless. Having the language syntax opposed
>> to that is
>  
> ...consistent with virtually every other popular programming language.

A mistake is still a mistake even if it shared with others.

Treating its with a lead zero as octal was a design error when it was 
first thought up (possibly in C?) and it remains a design error no matter 
how many languages copy it. I feel your pain of having to unlearn 
something you have learned, but just because you have been lead astray by 
the languages you use doesn't mean we should compound the error by 
leading the next generation of coders astray too.

Octal is of little importance today, as near as I can tell it only has 
two common uses in high level languages: file umasks and permissions on 
Unix systems. It simply isn't special enough to justify implicit notation 
that surprises people, leads to silent errors, and is inconsistent with 
standard mathematical notation and treatment of floats:

>>> 123.2000 # insignificant trailing zeroes don't matter
123.2
>>> 000123.2 # neither do insignificant leading zeroes
123.2
>>> 001.23e0023  # not even if it is an integer
1.23e+23
>>> 000123  # but here is matters
83


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


Idle does not recognize PYTHONPATH

2009-08-22 Thread goldtech
Hi,

Idle does not recognize PYTHONPATH set in .bashrc. Starting Python
shell in the terminal sys.path shows the PYTHONPATH, but again not
shown in IDLE. This is a common problem but I could not find a fix.
Using Ubuntu 9.04. Python 2.6.

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


Re: How to 'de-slashify' a string?

2009-08-22 Thread AK

Vlastimil Brom wrote:

2009/8/22 AK :

Vlastimil Brom wrote:

2009/8/22 AK :

Steven D'Aprano wrote:

On Sat, 22 Aug 2009 04:20:23 -0400, AK wrote:


Hi, if I have a string '\\303\\266', how can I convert it to '\303\266'
in a general way?

It's not clear what you mean.

Do you mean you have a string '\\303\\266', that is:

backslash backslash three zero three backslash backslash two six six

If so, then the simplest way is:


s = r'\\303\\266'  # note the raw string
len(s)

10

print s

\\303\\266

print s.replace('', '\\')

\303\266


Another possibility:


s = '\\303\\266'  # this is not a raw string
len(s)

8

print s

\303\266

So s is:
backslash three zero three backslash two six six

and you don't need to do any more.

Well, I need the string itself to become '\303\266', not to print
that way. In other words, when I do 'print s', it should display
unicode characters if my term is set to show them, instead of
showing \303\266.


The problem I'm running into is that I'm connecting with pygresql to a
postgres database and when I get fields that are of 'char' type, I get
them in unicode, but when I get fields of 'byte' type, I get the text
with quoted slashes, e.g. '\303' becomes '\\303' and so on.

Is pygresql quoting the backslash, or do you just think it is quoting
the
backslashes? How do you know? E.g. if you have '\\303', what is the
length
of that? 4 or 5?

Length is 4, and I need it to be length of 1. E.g.:


s = '\303'
s

'\xc3'

x = '\\303'
x

'\\303'

len(x)

4

len(s)

1


What I get from pygresql is x, what I need is s. Either by asking
pygresql
to do this or convert it afterwards. I can't do replace('\\303', '\303')
because it can be any unicode character.

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



Hi,
do you mean something like


u"\u0303"

u'\u0303'

print u"\u0303"

̃
   ̃ (dec.: 771)  (hex.: 0x303)   ̃ COMBINING TILDE (Mark, Nonspacing)
?

vbr

Yes, something like that except that it starts out as '\\303\\266', and it's
good enough for me if it turns into '\303\266', in fact that's rendered as
one unicode char. In other words, when you do:


print "\\303\\266"

'\303\266'

I need that result to become a python string, i.e. the slashes need to
be converted from literal slashes to escape slashes.




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



Not sure, whether it is the right way of handling the such text data, but maybe:


decoded = '\\303\\266'.decode("string_escape")
decoded

'\xc3\xb6'

print decoded

ö

print '\303\266'

ö

It might be an IDLE issue, but it still isn't one unicode glyph.

I guess, you have to ensure, that the input data is valid and the
right encoding is used.

hth
  vbr


Actually, this works perfectly for me. It prints out as one character in
gnome-terminal and also when I write it to a text file, and open it as
utf-8 format in gnumeric, it also shows up properly.

Thanks to all who helped! -AK
--
http://mail.python.org/mailman/listinfo/python-list


Re: How to create functors?

2009-08-22 Thread Paul Rubin
Steven D'Aprano  writes:
> According to Wikipedia, functor can be used as a synonym for "function 
> object": ... http://en.wikipedia.org/wiki/Function_object

Hmm, I hadn't seen that usage before.  I guess there's no law against
it, but it seems a bit bogus to me.

> I find the Haskell page entirely opaque and unintelligible... The
> Wikipedia page is a little better

I thought it was the other way around, but I've been banging my head
against Haskell on and off for the past year or so, so it could be
that the Haskell page is more intelligible if the reader already has
some familiarity with Haskell and its type system.  

Here's another attempt of my own, from the mathematical perspective:
"categories" generalize the concept of structured collections of
objects.  For example, groups, rings, and sets are each a type of
structured collection, so there is a category of groups (called "Grp"
since for some reason categorists like to use capitalized, abbreviated
names), a category of rings ("Rng"), and a category of sets ("Set").
Each category has a collection of objects and a collection of "arrows"
(sometimes called morphisms) which are associative relations between
objects in the category.  So in the category of sets, the objects are
sets and the arrows are functions mapping one set to another.  In the
category of groups, the objects are groups and the arrows are the
homeomorphisms between groups.

Functors are mappings from one category to another, that map both the
objects and the arrows.  That is they are arrows on the categories of
categories.

The concepts of categories and functors came from the loftier reaches
of algebraic geometry (further in math than I ever got) in the 1950's
or so.  These days they turn out to be useful in studying type systems
of programming languages, which is where most of my exposure to them
has come from.

> But let me try an example to see if I've got it right:
> class Int2StrFunctor:
> def map1(self, n):
> if type(n) is not int:
> raise TypeError('argument must be an int')

I don't think this is right.  A functor (in the PL sense that I've
been describing) acts on types, not on members of types.  That is,
your example turns "3" into "---", i.e. it turns a member of type
"int" into a member of type "str".  A functor would be something that
acts on arbitrary types, e.g. one might turn "int" into "array of
int", and "str" into "array of str" and so forth.  Another might turn
"int" into "red-black tree containing a pair of ints at each node" and
doing something similar with str.  The map1 and map2 functions (I
guess it is ok to call them that) are supposed to be generic.

I guess you could have a "String" functor whose map1 function is repr
and whose map2 functor is (lambda f: lambda x,f=f: repr(f(x))) or
something like that.  It would transport every type to the str type.

> There are some technical restrictions on functors, relating to the sorts 
> of functions and types (strictly "categories") they can accept, 
> presumably to make them mathematically well-behaved.

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


Re: logging SMTPhandler Error

2009-08-22 Thread Aahz
In article ,
Ned Deily   wrote:
>
>Or on OS X it's not *too* difficult to set up a local host mailer using
>the Apple-supplied prefix that would accept mail locally and forward it
>to a more sophisticated remote mailer.

It's also not too difficult to do the moral equivalent with dnspython and
find the MX host for the domain you're trying to send e-mail to; that
usually does not require an encrypted connection.
-- 
Aahz ([email protected])   <*> http://www.pythoncraft.com/

"I support family values -- Addams family values" --www.nancybuttons.com
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Annoying octal notation

2009-08-22 Thread Steven D'Aprano
On Sat, 22 Aug 2009 22:19:01 -0500, Derek Martin wrote:

> On Sat, Aug 22, 2009 at 02:55:51AM +, Steven D'Aprano wrote:
>> > I can see how 012 can
>> > be confusing to new programmers, but at least it's legible, and the
>> > great thing about humans is that they can be taught (usually).
>> 
>> And the great thing is that now you get to teach yourself to stop
>> writing octal numbers implicitly and be write them explicitly with a
>> leading 0o instead :)
> 
> Sorry, I don't write them implicitly.  A leading zero explicitly states
> that the numeric constant that follows is octal.

That is incorrect.

Decimal numbers implicitly use base 10, because there's nothing in the 
literal 12340 (say) to indicate the base is ten, rather than 16 or 9 or 
23. Although implicit is usually bad, when it's as common and expected as 
decimal notation, it's acceptable.

Hex decimals explicitly use base 16, because the leading 0x is defined to 
mean "base 16". 0x is otherwise not a legal decimal number, or hex number 
for that matter. (It would be legal in base 34 or greater, but that's 
rare enough that we can ignore this.) For the bases we care about, a 
leading 0x can't have any other meaning -- there's no ambiguity, so we 
can treat it as a synonym for "base 16".

(Explicitness isn't a binary state, and it would be even more explicit if 
the base was stated in full, as in e.g. Ada where 16#FF# = decimal 255.)

However, octal numbers are defined implicitly: 012 is a legal base 10 
number, or base 3, or base 9, or base 16. There's nothing about a leading 
zero that says "base 8" apart from familiarity. We can see the difference 
between leading 0x and leading 0 if you repeat it: repeating an explicit 
0x, as in 0x0xFF, is a syntax error, while repeating an implicit 0 
silently does nothing different:

>>> 0x0xFF
  File "", line 1
0x0xFF
 ^
SyntaxError: invalid syntax
>>> 0077
63


> It is so in 6 out of 7
> computer languages I have more than a passing familiarity with (the 7th
> being scheme, which is a thing unto itself), including Python.  It's
> that way on Bourne-compatible and POSIX-compatible Unix shells (though
> it requires a leading backslash before the leading zero there).  I'm
> quite certain it can not be the case on only those 6 languages that I
> happen to be familiar with...

No, of course not. There are a bunch of languages, pretty much all 
heavily influenced by C, which treat integer literals with leading 0s as 
oct: C++, Javascript, Python 2.x, Ruby, Perl, Java. As so often is the 
case, C's design mistakes become common practice. Sigh.

However, there are many, many languages that don't, or otherwise do 
things differently to C. Even some modern C-derived languages reject the 
convention:

C# doesn't have octal literals at all.

As far as I can tell, Objective-C and Cocoa requires you to explicitly 
enable support for octal literals before you use them.

In D, at least some people want to follow Python's lead and either drop 
support for oct literals completely, or require a 0o prefix:
http://d.puremagic.com/issues/show_bug.cgi?id=2656

E makes a leading 0 a syntax error.


As far as other, non-C languages go, leading 0 = octal seems to be rare 
or non-existent:

Basic and VB use a leading &O for octal.

FORTRAN 90 uses a leading O (uppercase o) for octal, and surrounds the 
literal in quotation marks: O"12" would be ten in octal. 012 would be 
decimal 12.

As far as I can tell, COBOL also ignores leading zeroes.

Forth interprets literals according to the current value of BASE (which 
defaults to 10). There's no special syntax for it.To enter ten in octal, 
you might say:

8 BASE ! 12

or if your system provides it:

OCT 12

Standard Pascal ignores leading 0s in integers, and doesn't support octal 
at all. A leading $ is used for hex. At least one non-standard Pascal 
uses leading zero for octal.

Haskell requires an explicit 0o:
http://www.haskell.org/onlinereport/lexemes.html#lexemes-numeric

So does OCaml.

Ada uses decimal unless you explicitly give the base:
http://archive.adaic.com/standards/83lrm/html/lrm-02-04.html

Leading zeroes are insignificant in bc:

[st...@sylar ~]$ bc
bc 1.06
Copyright 1991-1994, 1997, 1998, 2000 Free Software Foundation, Inc.
This is free software with ABSOLUTELY NO WARRANTY.
For details type `warranty'.
012 + 011
23

Leading zeroes are also insignificant in Hewlett-Packard RPN language 
(e.g. HP-48GX calculators), Hypertalk and languages derived from it.

I'm not sure, but it looks to me like Boo doesn't support octal literals, 
although it supports hex with 0x and binary with 0b.

Algol uses an explicit base: 8r12 to indicate octal 10.

Common Lisp and Scheme use a #o prefix.

As far as *languages* go, 0-based octal literals are in the tiny 
minority. As far as *programmers* go, it may be in a plurality, perhaps 
even a small minority, but remember there are still millions of VB 
programmers out there who are just as unfamiliar with C conventions.

> W