proposal: give delattr ability to ignore missing attribute

2008-06-10 Thread Gary Wilson
I would like to propose that functionality be added to delattr to
handle the case when the attribute does not exist.

First off, getattr handles this nicely with the default parameter:

value = getattr(obj, 'foo', False)

instead of:

try:
value = getattr(obj, 'foo')
except AttributeError:
value = False

or:

if hasattr(obj, 'foo'):
value = getattr(obj, 'foo')
else:
value = False


And I think it makes sense to have something similar for delattr (name
the argument as you wish):

delattr(obj, 'foo', allow_missing=True)

instead of:

try:
delattr(obj, 'foo')
except AttributeError:
pass

or:

try:
del obj.foo
except AttributeError:
pass

or:

if hasattr(obj, 'foo')
delattr(obj, 'foo')

For backwards compatibility, allow_missing would default to False.

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


supported versions policy?

2009-07-31 Thread Gary Wilson
Does Python have a formal policy on the support lifetime (bug fixes,
security fixes, etc.) for major and minor versions?  I did a bit of
searching on the Python web site and this group, but didn't find
anything.  If there is a policy posted somewhere (and I just didn't
dig deep enough), would someone please point me in the right
direction.  If there is no posted policy, would someone be kind enough
to describe the practiced policy here.

I'm looking for is something like this:
http://docs.djangoproject.com/en/dev/internals/release-process/#supported-versions

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


Re: Set/Get attribute syntatic sugar

2005-06-28 Thread Gary Wilson Jr
Заур Шибзухов wrote:
> There is a syntactic sugar for item access in
> dictionaries and sequences:
> 
> o[e] = v <-> o.__setitem__(e, v)
> o[e] <-> o.__getitem__(e)
> 
> where e is an expression.
> 
> There is no similar way for set/get attribute for objects.
> If e is a given name, then 
>  
> o.e = v <-> o.__setattr__(e, v)
> o.e <-> o.__getattr__(e)
> 
> Anybody thought about this issue?

How about inheriting the dict class, something like this...

>>> class C(dict):
... pass
...
>>> e = 'myAttribute'
>>> v = 'syntactic sugar'
>>> o = C()
>>> o[e] = v
>>> o[e]
'syntactic sugar'

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

Re: Cleaning strings with Regular Expressions

2005-09-08 Thread Gary Wilson Jr
sheffdog wrote:
> Using regular expressions, the best I can do so far is using the re.sub
> command but it still takes two lines. Can I do this in one line? Or
> should I be approaching this differently? All I want to end up with is
> the file name "ppbhat.tga".

A regular expression to do what you want:
 >>> s = 'setAttr ".ftn" -type "string" 
 >>> /assets/chars/boya/geo/textures/lod1/ppbhat.tga";'
 >>> s = re.sub(r".*/(.*\.tga).*", r"\1", s)
 >>> s
'ppbhat.tga'

Is a regular expression the best solution?
That depends on what else you need to do with your data file.
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: My First Python Script

2005-09-16 Thread Gary Wilson Jr
Ed Hotchkiss wrote:
> def ZeroThrough255():
>   x = 0
>   while x <= 255:
>   if len(x) == 1:
>   mySet = '00' + str(x)
>   elif len(x) == 2:
>   mySet = '0' + str(x)
>   else:
>   mySet = x
>   print mySet
>   x +=1   
> 
> ZeroThrough255()

Instead of using the while loop and a counter, you can use the range() 
function.  Using range() and string formatting you could to something like:

def ZeroThrough255():
for num in range(256):
print "%03d" % num

which, using a list comprehension and the string join() function, could also 
be written as:

def ZeroThrough255():
print "\n".join(["%03d" % num for num in range(256)])
-- 
http://mail.python.org/mailman/listinfo/python-list


trying to use swig for the first time

2006-01-23 Thread Gary Wilson Jr
...I have some C code (foo.c and foo.h) that I would like to be able to access 
using python.

I've written my interface file (foo.i) like so:
%module foo
%{
#include "foo.h"
%}
%include "foo.h"

I then do the following on the command line:
$ swig -python foo.i
$ gcc -c foo.c foo_wrap.c -I /usr/include/python2.4
$ ld -shared foo.o foo_wrap.o -o _foo.so

Then when I try importing the module into python:
$ python -c "import foo"
Traceback (most recent call last):
   File "", line 1, in ?
   File "foo.py", line 5, in ?
 import _foo
ImportError: ./_foo.so: undefined symbol: EVP_DecodeBlock

Now, EVP_DecodeBlock can be found in /usr/include/openssl/evp.h:
int EVP_DecodeBlock(unsigned char *t, const unsigned char *f, int n);

And evp.h in included in foo.h:
#include 

What am I doing wrong here?
Do I need to include more in the interface file?

I tried adding the EVP_DecodeBlock declaration to the interface file like so:
%module foo
%{
#include "foo.h"
extern int EVP_DecodeBlock(unsigned char *t, const unsigned char *f, int n);
%}
%include "foo.h"
extern int EVP_DecodeBlock(unsigned char *t, const unsigned char *f, int n);

But this led to the exact same error when trying to import the python module.
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Convert from numbers to letters

2005-05-19 Thread Gary Wilson Jr
Bill Mill wrote:
> On 5/19/05, Peter Otten <[EMAIL PROTECTED]> wrote:
> 
>>Bill Mill wrote:
>>
>>
Traceback (most recent call last):
File"",line1,in?
NameError: name 'sorted' is not defined

I think you're probably using 2.4 ??
>>>
>>>Yes, sorted() is new in python 2.4 .You could use a very lightly
>>>tested pure-python partial replacement:
>>
>>By the way, sorted() can be removed from your original post.
>>
>>Code has no effect :-)
> 
> 
> I'm gonna go ahead and disagree with you:

Me too, although I would forgo the sort altogether (while making things
a little more readable IMO):

> alpha = 'abcdefghijklmnopqrstuvwxyz'.upper()
> pairs = [''.join((x,y)) for x in alpha for y in [''] + [z for z in alpha]]
> pairs = sorted(pairs, key=len)

alpha = 'abcdefghijklmnopqrstuvwxyz'.upper()
pairs = [x for x in alpha] + [''.join((x,y)) for x in alpha for y in alpha]



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


Re: Convert from numbers to letters

2005-05-19 Thread Gary Wilson Jr
Gary Wilson Jr wrote:
> alpha = 'abcdefghijklmnopqrstuvwxyz'.upper()
> pairs = [x for x in alpha] + [''.join((x,y)) for x in alpha for y in alpha]

I forget, is string concatenation with '+' just as fast as join()
now (because that would look even nicer)?
-- 
http://mail.python.org/mailman/listinfo/python-list


PAM authentication?

2005-05-24 Thread Gary Wilson Jr
I would like my application to be able to authenticate through PAM. Is
there any code out there that implements this? All I could find was PyPAM
(http://www.pangalactic.org/PyPAM/), which doesn't look like it has been
touched in almost 6 years and requires python1.5.

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


__init__.py in packages

2005-06-08 Thread Gary Wilson Jr
I'm creating a python package foo.

What is intended use for __init__.py files?
Well, I found this: http://www.python.org/doc/essays/packages.html
>From what I can gather it is for initialization of the package when doing an
import, but I would really like to see an example or situation that makes good
use of the __init__.py file.

Looking at the distutils __init__.py file, it only defines things like
__version__.  However, looking at the logging __init__.py file, it is a
1196-line monster with functions and classes defined throughout.

What is the RightThing?

Should I only define things in __init__.py that need to be defined when
importing a subpackage and/or module of package foo?

Is it ok for me to define classes in foo/__init__.py?
Whereby I could do something like:

from foo import MyClass

Or is is better if I were to put these classes and/or functions in foo/core.py?
Whereby I would do something like:

from foo.core import MyClass
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Perl s/ To Python?

2005-06-10 Thread Gary Wilson Jr
John Abel wrote:
> Does anyone know of a quick way of performing this:
> 
> $testVar =~ s#/mail/.*$##g

Use the re (regular expression) module.  Since you are iterating over a lot of
entries, it is good to compile the regular expression outside of the loop.

>>> import re
>>> mailRE = re.compile('/mail/.*$')
>>>
>>> myList = ['/var/mail/joe', '/var/spool/mail/bob']
>>> remainderList = []
>>>
>>> for testVar in myList:
... remainderList.append(mailRE.sub('', testVar))
...
>>> print '\n'.join(remainderList)
/var
/var/spool


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