Re: Customizing class attribute access in classic classes

2011-10-30 Thread Geoff Bache
On Oct 30, 4:16 am, Ben Finney  wrote:
> Geoff Bache  writes:
> > I'm wondering if there is any way to customize class attribute access
> > on classic classes?
>
> Why do that? What is it you're hoping to achieve, and why limit it to
> classic classes only?
>

I'm building a mocking tool, CaptureMock, which works by intercepting
and capturing particular calls, recording and replaying them.
A user can just say "intercept httplib for me" and it will record all
the interactions with httplib and allow a test that can be run without
doing anything via http or writing any handcrafted "mock-code".

So I need to be able to intercept also static attribute access, say
httplib.HTTPConnection.request.

httplib.HTTPConnection is a classic class. I can make my intercepting
version of it into a new-style class but the risk is that that will
change its behaviour in subtle ways, negating the point of the tool.

As for limiting it to classic classes only, I obviously need to do it
on new-style classes also. But I know how to do that...

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


Re: Customizing class attribute access in classic classes

2011-10-30 Thread Steven D'Aprano
On Sat, 29 Oct 2011 14:06:12 -0700, Geoff Bache wrote:

> Hi,
> 
> I'm wondering if there is any way to customize class attribute access on
> classic classes?
> 
> So this works:
> 
> class Meta(type):
> def __getattr__(cls, name):
> return "Customized " + name
> 
> class A:
> __metaclass__ = Meta
> 
> print A.blah
> 
> but it turns A into a new-style class.

And why is this a problem?

In any case, metaclasses work for classic classes. Metaclasses go back to 
pre-Python 1.5, long before new-style classes and type unification.

http://www.python.org/doc/essays/metaclasses/

You just have to do a lot more work:


class Meta:
def __init__(self, name, bases, namespace):
self.__name__ = name
self.__bases__ = bases
self.__dict__ = namespace
def __str__(self):
return ""
__repr__ = __str__
def __getattr__(self, name):
return "Customized " + name
def __call__(self):
return self

(The purpose of the __str__ and __repr__ methods is to make it possible 
to experiment in the interactive interpreter, without a lot of mysterious 
and puzzling "str object is not callable" TypeErrors. Trust me on this.)

And in use:

>>> class Spam:
... __metaclass__ = Meta
... a = 1
... 
>>> Spam.b
'Customized b'


But note that using classic classes, there is no equivalent of 
__getattribute__ or descriptors, so there is no way to customize access 
of an attribute which actually does exist:

>>> Spam.a
1


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


Re: Customizing class attribute access in classic classes

2011-10-30 Thread Geoff Bache

Thanks for this Steven. I'm however gettings some pretty odd effects,
both on method access and inheritance. I expanded your example a
bit...

class Meta:
def __init__(self, name, bases, namespace):
self.__name__ = name
self.__bases__ = bases
self.__dict__ = namespace
def __str__(self):
return ""
__repr__ = __str__
def __getattr__(self, name):
return "Customized " + name
def __call__(self):
return self

class Base:
def basemethod(self):
return "base"


class A(Base):
__metaclass__ = Meta
def method(self):
return "answer"

The effect seems to be to make all methods of A into static methods,
and to ignore its base classes altogether:

>>> a = A()
>>> print a.blah2
Customized blah2
>>> print a.method()
Traceback (most recent call last):
  File "", line 1, in 
TypeError: method() takes exactly 1 argument (0 given)
>>> print a.method(1)
answer
>>> print A.method(1)
answer
>>> print a.basemethod()
Traceback (most recent call last):
  File "", line 1, in 
TypeError: 'str' object is not callable
>>> isinstance(a, Base)
False

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


Re: Need Windows user / developer to help with Pynguin

2011-10-30 Thread Mark Hammond

On 30/10/2011 1:43 AM, Lee Harr wrote:

For Windows users who want to just run Pyguin (not modify or tinker
with the source code), it would be best to bundle Pynguin up with
Py2exe


I considered that, but I agree that licensing issues would make it
problematic.


What licensing issues concern you?  The py2exe license shouldn't be a 
problem and py2exe or something like it is good advice.


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


Re: Dynamically creating properties?

2011-10-30 Thread DevPlayer
To be honest, I was hoping someone would have posted a link to a well
known and tested recipe. You'd think this function would be in the
standard library or a specific Exception tied directly with setattr()
and getattr() (and possibly __getattr__(), __getattribute__(),
__setattr__())

The main thing I wanted to point out though is when you start using
dynamically named references, there's more to it then just letting a
dynamic file define it.

If there's a way to reference a set of data, it really shouldn't be
with a "dynamically named reference" too often.

Databases are a good example. Perhaps this is a better way for
example: If you have a bunch of tables in your DB -is- it better to
get the table def and create a Python class with dynamically named
"fields"?

Or is it better to create a Table class with name attribute and a
Field class with a name attribute (named "name")

SO instead of :
field_name = xml_parse.get_next_field_name(xml_table_definition)
my_table = Table()
setattr(my_table, field_name,
empty_list_to_later_contain_field_data)

Perhaps:
field_name = xml_parse.get_next_field_name(xml_table_definition)
my_table = Table()
my_table.fields[field_name] =
empty_list_to_later_contain_field_data
# or
my_table.add_field( Field(field_name) )

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


[Help] python ctypes to process C pointer!

2011-10-30 Thread Korobase
A c code snippet,the c file compiled to a dll file named libxxx.dll:

typedef void* HND;
typedef unsigned char UCHAR;
typedef short int  SWORD;
...
int Connect(
   HND* hnd,
   UCHAR* ipaddr,
   SWORD port){
..
return 1;
}

then How to handle function Connect using python and ctypes. especially the
parameter hnd?

My python code:

from ctypes import *
xxx = cdll.libxxx
xxx.Connect.restype=c_int
xxx.Connect.argstype=[c_wchar_p,c_wchar_p,c_int]

hnd=c_char_p()

buf=create_string_buffer("127.0.0.1\0")
ipaddr=cast(buf,POINTER(c_char))

xxx.Connect(byref(hnd),ipaddr,8000)

But I always result a error:
WindowsError: exception: access violation writing 0x

How to fix this problem?

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


__init__ with multiple list values

2011-10-30 Thread Gnarlodious
Initializing a list of objects with one value:

class Order:
 def __init__(self, ratio):
  self.ratio=ratio
 def __call__(self):
  return self.ratio

ratio=[1, 2, 3, 4, 5]
Orders=[Order(x) for x in ratio]


But now I want to __init__ with 3 values:

class Order:
 def __init__(self, ratio, bias, locus):
  self.ratio=ratio
  self.bias=bias
  self.locus=locus
 def __call__(self):
  return self.ratio, self.bias, self.locus

ratio=[1, 2, 3, 4, 5]
bias=[True, False, True, False, True]
locus=['A', 'B', 'C', 'D', 'E']
Orders=[Order(x,y,z) for x,y,z in [ratio, bias, locus]]

>>> ValueError: too many values to unpack (expected 3)

How to do it?

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


Re: Review Python site with useful code snippets

2011-10-30 Thread Mehrzad Irani
Considering that the site is going to grow over time, putting the snippets in a 
drop-down menu isn't a wise idea in my opinion.

Snippets on a separate page like activestate python would make it more 
convenient.

Nice initiative, and would be very helpful when it grows over time.
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: __init__ with multiple list values

2011-10-30 Thread Chris Angelico
On Mon, Oct 31, 2011 at 2:02 AM, Gnarlodious  wrote:
> Orders=[Order(x,y,z) for x,y,z in [ratio, bias, locus]]
>

Assuming that you intend to take the first element of each list, then
the second, and so on, you'll want to use zip():

Orders=[Order(x,y,z) for x,y,z in zip(ratio, bias, locus)]

With your syntax, Python iterates over a three-element list. The first
iteration, it looks at 'ratio' and tries to unpack that into x,y,z;
this doesn't work, because ratio has five elements. The second
iteration would try to unpack 'bias', and the third would go for
'locus'.

The zip function returns tuples of (ratio[N], bias[N], locus[N]) for
successive Ns:

>>> list(zip(ratio,bias,locus))
[(1, True, 'A'), (2, False, 'B'), (3, True, 'C'), (4, False, 'D'), (5,
True, 'E')]

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


[ANN] Pyrolite 1.3 - native Pyro and Pickle library for Java and .NET

2011-10-30 Thread Irmen de Jong
Hello,

I'd like to announce Pyrolite 1.3,
a tiny (~50k) Pyro client library for Java and .NET.

Q: "what is a java/.net library doing in this newsgroup?"

A.1: This library is meant to connect a Java or .NET program to Python in a 
very simple
way, using the Pyro protocol. Pyro is my remote object library or Python.
A.2: Pyrolite contains an almost feature complete native implementation of 
Python's
pickle protocol. This can be useful by itself to read/write pickles from Java 
or .NET
programs.

Download Pyrolite here:   http://irmen.home.xs4all.nl/pyrolite/
More info on Pyrolite:  http://irmen.home.xs4all.nl/pyrolite/README.txt
More info on Pyro:  http://irmen.home.xs4all.nl/pyro/


Enjoy,

Irmen de Jong

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


Re: __init__ with multiple list values

2011-10-30 Thread Gnarlodious
On Oct 30, 9:15 am, Chris Angelico wrote:

> Orders=[Order(x,y,z) for x,y,z in zip(ratio, bias, locus)]

Brilliant, thanks!

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


RE: Need Windows user / developer to help with Pynguin

2011-10-30 Thread Lee Harr

>>> Py2exe
>>
>> I considered that, but I agree that licensing issues would make it
>> problematic.
>
> What licensing issues concern you? The py2exe license shouldn't be a
> problem and py2exe or something like it is good advice.

I think PyQt 4 would be the biggest issue. It is GPL 2 / 3.


I think if this were just for my own use, or just for use in
getting the program on to computers in my own school's
labs then py2exe would be fine.

I don't think I could post a py2exe'd version on the google
code site for general distribution. Of course this is only my
own personal (non-legal) opinion.

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


Re: Need Windows user / developer to help with Pynguin

2011-10-30 Thread Alec Taylor
Maybe push something onto pip or easy_install?

On Fri, Oct 28, 2011 at 6:38 AM, Lee Harr  wrote:
>
> I develop the free python-based turtle graphics application pynguin.
>
> http://pynguin.googlecode.com/
>
>
> Lately I have been getting a lot of positive comments from people
> who use the program, but I am also getting a lot of feedback from
> people on Windows (mostly beginners) who are having trouble
> getting the program running.
>
> I don't use Windows myself, though I have found access occasionally
> to fix bugs. I just don't know enough about Windows culture to be
> able to make a reliable method for installing or running the program.
>
>
> The method that I wrote for Linux also works on Windows, but it has
> to be run from the command prompt.
>
>
> I am hoping someone can look at what is there and come up with a
> reliable method or a simple set of steps that people can follow to get
> up and running. Hopefully without having to resort to the command
> prompt.
>
> I started a wiki page here:
> http://code.google.com/p/pynguin/wiki/InstallingPynguinOnWindows
> but I can't even test if it actually works
>
>
> Thanks for any help.
>
>
> --
> http://mail.python.org/mailman/listinfo/python-list
>
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: __init__ with multiple list values

2011-10-30 Thread MRAB

On 30/10/2011 15:02, Gnarlodious wrote:

Initializing a list of objects with one value:

class Order:
  def __init__(self, ratio):
   self.ratio=ratio
  def __call__(self):
   return self.ratio

ratio=[1, 2, 3, 4, 5]
Orders=[Order(x) for x in ratio]


But now I want to __init__ with 3 values:

class Order:
  def __init__(self, ratio, bias, locus):
   self.ratio=ratio
   self.bias=bias
   self.locus=locus
  def __call__(self):
   return self.ratio, self.bias, self.locus

ratio=[1, 2, 3, 4, 5]
bias=[True, False, True, False, True]
locus=['A', 'B', 'C', 'D', 'E']
Orders=[Order(x,y,z) for x,y,z in [ratio, bias, locus]]


ValueError: too many values to unpack (expected 3)


How to do it?


Use 'zip':

Orders=[Order(x,y,z) for x,y,z in zip(ratio, bias, locus)]
--
http://mail.python.org/mailman/listinfo/python-list


Calling JavaScript inside the webbrowser module

2011-10-30 Thread Anders Gunnarsson
Hi!

Is there anyway to communicate with JavaScript inside a website opened via the 
webbrowser module?

| import webbrowser
| webbrowser.open('http://python.org')

Here I'd like to do something like webbrowser.call('alert(1)')
and I'd like to be able to call the python app from javascript too.

I've looked at pywebkitgtk, but it's messy running on win32.

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


Appending to sys.path during module install with distutils

2011-10-30 Thread Darren Hart
I'm trying to use distutils to install a collection of modules in
/usr/local/lib/python2.7/site-packages. My distribution (Fedora 15)
doesn't include any /usr/local paths in sys.path, so the import fails
when running the program. The distutils documentation suggests adding
a $NAME.pth file to an existing site-packages directory in sys.path.
Is there a preferred/accepted method of doing this?

I considered just adding some code to my setup.py to generate a
braindump.pth file containing something like:

PREFIX/lib/pythonMAJOR.MINOR/site-packages

and then  walking the existing sys.path and picking one of those
site-packages directories to install braindump.pth to. I'm not sure
how to determine which is the appropriate path.

Maybe I'm going about this completely wrong as well - anyone care to
help steer me in the right direction?

The project is located here: http://braindump.dvhart.com in case
anyone wants it for reference.

Thanks,

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


SSE4a with ctypes in python? (gcc __builtin_popcount)

2011-10-30 Thread est
Hi guys,

Here is the sample code

http://stackoverflow.com/questions/6389841/efficiently-find-binary-strings-with-low-hamming-distance-in-large-set/6390606#6390606

static inline int distance(unsigned x, unsigned y)
{
return __builtin_popcount(x^y);
}

Is it possible to rewrite the above gcc code in python using ctypes
(preferably Win/*nix compatible)?

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


ttk Listbox

2011-10-30 Thread Ric

What would be an equivalent widget in ttk like a Listbox and if
possible a small example? I tried to look here
http://docs.python.org/library/ttk.html but did not see anything.

Maybe I did not look in the right place?

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