Re: decorator issue with modules dbus & gobject

2011-08-22 Thread Makiavelik
On 21 août, 18:31, Emile van Sebille  wrote:
> On 8/18/2011 5:02 AM Makiavelik said...
>
> > Hi,
> > Here is a sample code that reproduces the issue :
>
> Not really 'sample' enough to allow others to investigate...
>
> ImportError: No module namedgobject
> ImportError: No module nameddbus
> ImportError: No module nameddbus.mainloop.glib
>
> Try to eliminate the non-standard dependencies.  I wanted to check the
> default value set in timeout but there's too much crap to clean up.
>
> Emile

Yes that's right there is a dependency with both modules dbus and
gobject (as mentionned in the subject of the thread).

The fact is I am not able to quit the looping method of the
gobject.Mainloop object without errors ...
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Order of addresses returned by socket.gethostbyname_ex()

2011-08-22 Thread Steven D'Aprano
On Mon, 22 Aug 2011 04:37 pm Tomas Lidén wrote:

> In what order are the addresses returned by socket.gethostbyname_ex()?
> 
> We know that gethostbyname() is indeterministic but hope that
> gethostbyname_ex() has a specified order.

Did you want a particular order, or just any deterministic order?

Have you considered sorting the addresses yourself?



-- 
Steven

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


Re: Order of addresses returned by socket.gethostbyname_ex()

2011-08-22 Thread Tomas Lidén
On 22 Aug, 10:15, Steven D'Aprano  wrote:
> On Mon, 22 Aug 2011 04:37 pm Tomas Lidén wrote:
>
> > In what order are the addresses returned by socket.gethostbyname_ex()?
>
> > We know that gethostbyname() is indeterministic but hope that
> > gethostbyname_ex() has a specified order.
>
> Did you want a particular order, or just any deterministic order?
>
> Have you considered sorting the addresses yourself?
>
> --
> Steven

In this particular case we have a host with several connections (LAN,
WIFI, VmWare adapters etc). When using gethostbyname() we got a VmWare
adapter but we wanted to get the LAN (or the "best" connection to our
server). With gethostbyname_ex() the ordering seemed to become LAN,
Wifi, etc and that's good for us. But we don't know if this holds on
other platforms (we're running on Windows 7).

A cross-platform deterministic order would be excellent for us.

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


Optimizing Text Similarity Algorithm

2011-08-22 Thread Yaşar Arabacı
Hi,

I originally posted this question on stackoverflow, you can find it here:
http://stackoverflow.com/q/7133350/886669

I just want people check what I am doing and express their opinion about the
thing I am doing is acceptable, or are there some expects of it that could
change.
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Adding modified methods from another class without subclassing

2011-08-22 Thread Peter Otten
John O'Hagan wrote:

> I have a class like this:
> 
> class MySeq():
> def __init__(self, *seq, c=12):
> self.__c = c
> self.__pc = sorted(set([i % __c for i in seq]))
> self.order = ([[self.__pc.index(i % __c), i // __c] for i in seq])
> #other calculated attributes
> 
> @property
> def pitches(self):
> return [self.__pc[i[0]] + i[1] * self.__c for i in self.order]
> 
> #other methods

That makes me dizzy. Are there any maxims in the Zen of Python that this 
piece doesn't violate?

> The "pitches" attribute initially reconstructs the "seq" arguments but can
> be modified by writing to the "order" attribute.
> 
> The "pitches" attribute represents the instances and as such I found
> myself adding a lot of methods like:
> 
> 
> def __getitem__(self, index):
> return self.pitches[index]
> 
> def __len__(self):
> return len(self.pitches)
> 
> def __iter__(self):
> return iter(self.pitches)
> 
> def __repr__(self):
> return str(self.pitches)
> 
> and so on, and calling a lot of list methods on the "pitches" attribute of
> MySeq instances elsewhere. I thought of making MySeq a subclass of list
> with "pitches" as its contents, but then I would have had to override a
> lot of methods case-by-case, for example to ensure that any alterations to
> "pitches" were reflected in the other calculated attributes.
> 
> So I wrote this function which takes a method, modifies it to apply to an
> instance attribute, and takes care of any quirks:
> 
> def listmeth_to_attribute(meth, attr):
> def new_meth(inst, *args):
> #ensure comparison operators work:
> args = [getattr(i, attr)  if isinstance(i, inst.__class__)
> else i for i in args]
> reference = getattr(inst, attr)
> test = reference[:]
> result = meth(test, *args)
> #ensure instance is reinitialised
> #if attribute has been changed:
> if test != reference:
> inst.__init__(*test)
> #ensure slices are of same class
> if isinstance(result, meth.__objclass__):
> result = inst.__class__(*result)
> return result
> return new_meth
> 
> and this decorator to apply this function to all the list methods and add
> them to MySeq:
> 
> def add_mod_methods(source_cls, modfunc, modfunc_args, *overrides):
> """Overides = any methods in target to override from source"""
> def decorator(target_cls):
> for name, meth in vars(source_cls).items():
> if name not in dir(target_cls) or name in overrides:
> setattr(target_cls, name, modfunc(meth, *modfunc_args))
> return target_cls
> return decorator
> 
> a kind of DIY single inheritance, used like this:
> 
> @add_mod_methods(list, listmeth_to_attribute, ('pitches',), '__repr__')
> class MySeq():
> .
> 
> Now I can call list methods transparently on MySeq instances, like
> subclassing but without all the overriding. If this works it will simplify
> a lot of code in my project.
> 
> But the fact that I haven't seen this approach before increases the
> likelihood it may not be a good idea. I can almost hear the screams of
> "No, don't do that!" or the sound of me slapping my forehead when someone
> says "Why don't you just...". So before I put it in, I'd appreciate any
> comments, warnings, criticisms, alternatives etc..

In the standard library functools.total_ordering uses that technique and I 
find the implications hard to understand:

http://bugs.python.org/issue10042

Your decorator looks even more complex; I'd only recommend using it if 
you're absolutely sure you're clever enough to debug it ;)

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


locale.format without trailing zeros

2011-08-22 Thread przemolicc
Hello,

>>> import locale
>>> locale.setlocale(locale.LC_ALL, "pl_PL")
'pl_PL'
>>> i=0.20
>>> j=0.25
>>> locale.format('%f', i)
'0,20'
>>> locale.format('%f', j)
'0,25'

I need to print the numbers in the following format:
'0,2'   (i)
'0,25'  (j)
So the last trailing zeros are not printed.

Regards
Przemyslaw Bak (przemol)




















































Najwieksza baza najtanszych ofert mieszkaniowych
http://linkint.pl/f2a0e
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: locale.format without trailing zeros

2011-08-22 Thread Peter Otten
przemol...@poczta.fm wrote:

 import locale
 locale.setlocale(locale.LC_ALL, "pl_PL")
> 'pl_PL'
 i=0.20
 j=0.25
 locale.format('%f', i)
> '0,20'
 locale.format('%f', j)
> '0,25'
> 
> I need to print the numbers in the following format:
> '0,2' (i)
> '0,25'(j)
> So the last trailing zeros are not printed.

>>> print locale.format("%g", 1.23)
1,23
>>> print locale.format("%g", 1.2345678)
1,23457
>>> print locale.format("%.10g", 1.2345678)
1,2345678
>>> print locale.format("%.15g", 0.1)
0,1
>>> print locale.format("%.17g", 0.1)
0,10001

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


Re: relative speed of incremention syntaxes (or "i=i+1" VS "i+=1")

2011-08-22 Thread Richard D. Moores
On Sun, Aug 21, 2011 at 18:12, Steven D'Aprano
 wrote:

> But really, we're talking about tiny differences in speed. Such trivial
> differences are at, or beyond, the limit of what can realistically be
> measured on a noisy PC running multiple processes (pretty much all PCs
> these days). Here are three runs of each on my computer:
>
>
> [steve@sylar python]$ python2.5 -m timeit -s 'n=0' 'n = n+1'
> 100 loops, best of 3: 0.508 usec per loop
> [steve@sylar python]$ python2.5 -m timeit -s 'n=0' 'n = n+1'
> 100 loops, best of 3: 0.587 usec per loop
> [steve@sylar python]$ python2.5 -m timeit -s 'n=0' 'n = n+1'
> 100 loops, best of 3: 0.251 usec per loop
>
>
> [steve@sylar python]$ python2.5 -m timeit -s 'n=0' 'n += 1'
> 100 loops, best of 3: 0.226 usec per loop
> [steve@sylar python]$ python2.5 -m timeit -s 'n=0' 'n += 1'
> 100 loops, best of 3: 0.494 usec per loop
> [steve@sylar python]$ python2.5 -m timeit -s 'n=0' 'n += 1'
> 100 loops, best of 3: 0.53 usec per loop
>
> Look at the variation between runs! About 130% variation between the fastest
> and slowest for each expression. And there's no reason to think that the
> fastest results shown is as fast as it can get. The time is dominated by
> noise, not the addition.
>
>
> For what it's worth, if I try it with a more recent Python:
>
> [steve@sylar python]$ python3.2 -m timeit -s 'n=0' 'n = n+1'
> 100 loops, best of 3: 0.221 usec per loop
> [steve@sylar python]$ python3.2 -m timeit -s 'n=0' 'n = n+1'
> 100 loops, best of 3: 0.202 usec per loop
> [steve@sylar python]$ python3.2 -m timeit -s 'n=0' 'n = n+1'
> 100 loops, best of 3: 0.244 usec per loop
>
> [steve@sylar python]$ python3.2 -m timeit -s 'n=0' 'n += 1'
> 100 loops, best of 3: 0.49 usec per loop
> [steve@sylar python]$ python3.2 -m timeit -s 'n=0' 'n += 1'
> 100 loops, best of 3: 0.176 usec per loop
> [steve@sylar python]$ python3.2 -m timeit -s 'n=0' 'n += 1'
> 100 loops, best of 3: 0.49 usec per loop
>
>
> I simply do not believe that we can justify making *any* claim about the
> relative speeds of n=n+1 and n+=1 other than "they are about the same". Any
> result you get, faster or slower, will depend more on chance than on any
> real or significant difference in the code.

I couldn't resist giving it a try. Using Python 3.2.1 on a 64-bit
Windows 7 machine with a 2.60 gigahertz AMD Athlon II X4 620
processor, I did 18 tests, alternating between n=n+1 and n+=1 (so 9
each).

The fastest for n+=1 was
C:\Windows\System32> python -m timeit  -r 3 -s "n=0" "n += 1"
1000 loops, best of 3: 0.0879 usec per loop

The slowest for n+=1 was
C:\Windows\System32> python -m timeit  -r 3 -s "n=0" "n += 1"
1000 loops, best of 3: 0.0902 usec per loop

The fastest for n = n + 1 was
C:\Windows\System32> python -m timeit  -r 3 -s "n=0" "n=n+1"
1000 loops, best of 3: 0.0831 usec per loop

The slowest for n = n + 1 was
C:\Windows\System32> python -m timeit  -r 3 -s "n=0" "n=n+1"
1000 loops, best of 3: 0.0842 usec per loop

Coincidence?

All the data are pasted at http://pastebin.com/jArPSe56

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


questions (& answers) about object, type, builtin types, class, metaclass and __getattribute__

2011-08-22 Thread Amirouche B.
I'm learning a bit of python internals lately and I'm trying to figure
out the
relationship between type, objects, class, callables and
__getattribute__ resolution.

While understanding Python mechanics/concepts, I'm trying to figure
how it
translates in CPython. This post is Python centric. Questions about
the
implementation of this concepts might be the subject of a future post
[1].

I will proceed this way: I will write statements about each subject.
Don't hesitate
to pick one and provide more information or better phrasing, or
explaining why it's
not true.

Be aware that I'm considering only new-style class.


A) type vs object
-

1) object is the base object, it has no bases : len(object.__bases__)
== 0
2) every object in python inherit object :
any_object_except_object.__bases__[-1] is object
3) object's type is type : object.__class__ is type
4) type parent object is object : type.__bases__ == (object,)


B) type vs metaclass


1) type is the first metaclass ?
2) type is its own metaclass : type(type) is type ?
3) object's metaclass is type ?
4) other metaclasses *MUST* inherit type ?
5) type(any_object) == last_metaclass_..., which is, most of the time,
type ?


C) type vs class


1) Type is the metaclass of most classes
2) The class statement::

class MyClass(object):
attribute = 1

def method(self):
pass

   translates to::

MyClass = type('MyClass', (object,), {'attribute': 1, 'method':
def method: pass })

3) Instantiation of any class ``MyClass(*args, **kwargs)`` translates
to::

type(MyClass).__call__(MyClass, *args, **kwargs)

   This is due to __getattribute__ algorithm (see E)

4) It's in type.__call__ that happens calls to __new__ and __init__

5) 3) => classes are instance of type

6) Since type.__call__ is used to instantiate instance of instance of
type
   (rephrased: __call__ is used to instantiate classes) where is the
code which
   is executed when we write ``type(myobject)`` or ``type('MyClass',
bases, attributes)``
   __getattribute__ resolution algorithm (see E) tells me that it
should be type.__call__
   but type.__call__ is already used to class instatiation.


C') class vs class instances aka. objects
-

1) A class type is a metaclass : issubclass(type(MyClass), type),
   MyClass.__class__ == type(MyClass)
2) An object type is a class : most of the time
isinstance(type(my_object), type)
   generally issubclass(type(type(my_object)), type)


D) builtin types


1) builtin types are their own metaclass ?
2) why function builtin type can not be subclassed ?
3) how does builtin function type relate to callable objects ?
4) int(1) is the same as int.__call__(1), since type(int) is type,
shouldn't int(1)
   translates to type.__call__(int, 1) ?


E) __getattribute__
---

1) ``my_object.attribute`` always translates to
``my_object.__getattribute__('attribute')``
2) Is the following algorithm describing __getattribute__ correct [2],
beware that I've
   added a comment:

a) If attrname is a special (i.e. Python-provided) attribute for
objectname,
   return it.  # what does it mean to be Python-provided ?

b ) Check objectname.__class__.__dict__ for attrname. If it exists
and is a
data-descriptor, return the descriptor result. Search all bases of
objectname.__class__
for the same case.

c) Check objectname.__dict__ for attrname, and return if found.

d) If it is a class and a descriptor exists in it or its bases,
return the
   descriptor result.

d) Check objectname.__class__.__dict__ for attrname. If it exists
and is a
   non-data descriptor, return the descriptor result. If it
exists, and is
   not a descriptor, just return it. If it exists and is a data
descriptor,
   we shouldn't be here because we would have returned at point 2.
Search all
   bases of objectname.__class__ for same case.

e) Raise AttributeError


Thanks in advance,

Regards,


Amirouche

[1] or maybe you can point me to the right direction before I ask
stupid questions.
I'm a bit familiar with Jython code, which seems to be easier than
PyPy and CPython
to read even if there is some magic due to the fact that Jython use
some JVM API
that hides somewhat how it works.
[2] this is a rewritten version of
http://www.cafepy.com/article/python_attributes_and_methods/python_attributes_and_methods.html#attribute-search-summary
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Order of addresses returned by socket.gethostbyname_ex()

2011-08-22 Thread Cameron Simpson
On 22Aug2011 02:06, Tomas Lid�n  wrote:
| On 22 Aug, 10:15, Steven D'Aprano  wrote:
| > On Mon, 22 Aug 2011 04:37 pm Tomas Lidén wrote:
| > > In what order are the addresses returned by socket.gethostbyname_ex()?
| >
| > > We know that gethostbyname() is indeterministic but hope that
| > > gethostbyname_ex() has a specified order.
| >
| > Did you want a particular order, or just any deterministic order?
| >
| > Have you considered sorting the addresses yourself?
| 
| In this particular case we have a host with several connections (LAN,
| WIFI, VmWare adapters etc). When using gethostbyname() we got a VmWare
| adapter but we wanted to get the LAN (or the "best" connection to our
| server). With gethostbyname_ex() the ordering seemed to become LAN,
| Wifi, etc and that's good for us. But we don't know if this holds on
| other platforms (we're running on Windows 7).
| 
| A cross-platform deterministic order would be excellent for us.

It would not surprise me if the order was related to the order a scan of
the system interfaces yields information, and I would imagine that may
be influenced by the order in which the interfaces were initialised.

So getting the LAN first may merely be fortuitous.
I wouldn't rely on it, especially if interfaces come and go.

What if you queried your routing table instead? Usually there's just one
default route, and hopefully it would be configured to use the "best"
interface.

Cheers,
-- 
Cameron Simpson  DoD#743
http://www.cskk.ezoshosting.com/cs/

If you give me six lines written by the most honest man, I will find
something in them to hang him.  - Cardinal Richilieu
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Order of addresses returned by socket.gethostbyname_ex()

2011-08-22 Thread Chris Angelico
On Mon, Aug 22, 2011 at 11:06 AM, Cameron Simpson  wrote:
> What if you queried your routing table instead? Usually there's just one
> default route, and hopefully it would be configured to use the "best"
> interface.
>

I wouldn't necessarily trust even this, on Windows. I've lately had
the most insane trouble getting my XP laptop to function properly as a
proxy - LAN connection has the default gateway, wifi has only
192.168.* - and Windows kept on trying to send DNS queries out on the
wireless connection, and then wondered why it didn't get a result.

Explicit is better than implicit. Instead of using the order, have a
config file that chooses the one(s) you want by name or IP address. Of
course, if you're on Unix/Linux, you can use the interface name (eth0,
eth1, etc) with a fair degree of reliability.

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


Re: locale.format without trailing zeros

2011-08-22 Thread Anssi Saari
przemol...@poczta.fm writes:

> Hello,
>
 import locale
 locale.setlocale(locale.LC_ALL, "pl_PL")
> 'pl_PL'
 i=0.20
 j=0.25
 locale.format('%f', i)
> '0,20'
 locale.format('%f', j)
> '0,25'
>
> I need to print the numbers in the following format:
> '0,2' (i)
> '0,25'(j)
> So the last trailing zeros are not printed.

That would be the %g conversion specifier.
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: locale.format without trailing zeros

2011-08-22 Thread przemolicc
On Mon, Aug 22, 2011 at 11:48:46AM +0200, Peter Otten wrote:
> przemol...@poczta.fm wrote:
> 
>  import locale
>  locale.setlocale(locale.LC_ALL, "pl_PL")
> > 'pl_PL'
>  i=0.20
>  j=0.25
>  locale.format('%f', i)
> > '0,20'
>  locale.format('%f', j)
> > '0,25'
> > 
> > I need to print the numbers in the following format:
> > '0,2'   (i)
> > '0,25'  (j)
> > So the last trailing zeros are not printed.
> 
> >>> print locale.format("%g", 1.23)
> 1,23
> >>> print locale.format("%g", 1.2345678)
> 1,23457
> >>> print locale.format("%.10g", 1.2345678)
> 1,2345678
> >>> print locale.format("%.15g", 0.1)
> 0,1
> >>> print locale.format("%.17g", 0.1)
> 0,10001

Thank you very much :-)

Regards
Przemyslaw Bak (przemol)




















































Znajdz samochod idealny dla siebie!
Szukaj >> http://linkint.pl/f2a0a
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Execute script from ipython

2011-08-22 Thread Johan Ekh
Thanks Chris!
I tried using "!" instead of "run". It works but with a significant
performance penalty.

Best regards,
Johan


On Fri, Aug 19, 2011 at 5:11 PM, Chris Rebert  wrote:

> On Fri, Aug 19, 2011 at 6:00 AM, Johan Ekh  wrote:
> > Hi all,
> > I have a script "myscript.py" located in "/usr/local/bin" on my linux
> box.
> > I can execute it in ipython with
> >
> > run /usr/local/bin/myscript.py
> >
> > but not with
> >
> > run myscript.py
> >
> > even though /usr/local/bin is in my $PATH and in my $PYTHONPATH.
> >
> > What should I do to correct this?
>
> Given that %run takes a filename and not a module name, I doubt
> PYTHONPATH matters. ipython's docs for %run don't seem to indicate
> that a search of any kind is performed. So, I'd say you have to either
> pass a valid absolute or relative path to myscript.py, or run
> myscript.py from bash instead of ipython.
>
> Changing your script's shebang line to ipython might also work
> (haven't tried it myself). Or you could try patching ipython's run()
> function to add this search feature you desire.
>
> Cheers,
> Chris
> --
> http://rebertia.com
>
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: locale.format without trailing zeros

2011-08-22 Thread przemolicc
On Mon, Aug 22, 2011 at 11:48:46AM +0200, Peter Otten wrote:
> przemol...@poczta.fm wrote:
> 
>  import locale
>  locale.setlocale(locale.LC_ALL, "pl_PL")
> > 'pl_PL'
>  i=0.20
>  j=0.25
>  locale.format('%f', i)
> > '0,20'
>  locale.format('%f', j)
> > '0,25'
> > 
> > I need to print the numbers in the following format:
> > '0,2'   (i)
> > '0,25'  (j)
> > So the last trailing zeros are not printed.
> 
> >>> print locale.format("%g", 1.23)
> 1,23
> >>> print locale.format("%g", 1.2345678)
> 1,23457
> >>> print locale.format("%.10g", 1.2345678)
> 1,2345678
> >>> print locale.format("%.15g", 0.1)
> 0,1
> >>> print locale.format("%.17g", 0.1)
> 0,10001

How about this format:
',1'
(the local zero is also not printed)

(I know this is strange but I need compatibility with local requirements)

Regards
Przemyslaw Bak (przemol)




















































Najwieksza baza najtanszych ofert mieszkaniowych
http://linkint.pl/f2a0e
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Order of addresses returned by socket.gethostbyname_ex()

2011-08-22 Thread Roy Smith
In article 
<356978ef-e9c1-48fd-bb87-849fe8e27...@p5g2000vbl.googlegroups.com>,
 Tomas Lidén  wrote:

> In what order are the addresses returned by socket.gethostbyname_ex()?
> 
> We know that gethostbyname() is indeterministic but hope that
> gethostbyname_ex() has a specified order.

Why would you hope that?  Or maybe a better question is, why would you 
expect that?  In general, all name resolution calls return results in 
arbitrary order.  In some cases, results are intentionally changed on 
every call (i.e. round-robin) in an attempt at load sharing.

What kind of ordering were you hoping for?
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Order of addresses returned by socket.gethostbyname_ex()

2011-08-22 Thread Tomas Lidén
On 22 Aug, 12:06, Cameron Simpson  wrote:
>
> It would not surprise me if the order was related to the order a scan of
> the system interfaces yields information, and I would imagine that may
> be influenced by the order in which the interfaces were initialised.
>
> So getting the LAN first may merely be fortuitous.
> I wouldn't rely on it, especially if interfaces come and go.
>

We did try to disable/enable the interfaces in different orders, but
always got the same return order from gethostbyname_ex(). So it looked
pretty stable (on that specific OS). There's been some testing on
Linux as well, but since this should be used in a general cross
platform tool we wanted to check the exact behaviour of the method.

> What if you queried your routing table instead? Usually there's just one
> default route, and hopefully it would be configured to use the "best"
> interface.
>

Hmm... perhaps. How would you do that?
/Tomas
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Order of addresses returned by socket.gethostbyname_ex()

2011-08-22 Thread Tomas Lidén
On 22 Aug, 12:36, Chris Angelico  wrote:
>
> Explicit is better than implicit. Instead of using the order, have a
> config file that chooses the one(s) you want by name or IP address. Of
> course, if you're on Unix/Linux, you can use the interface name (eth0,
> eth1, etc) with a fair degree of reliability.
>

The config file solution is not suitable, I think. This code should
run in a general cross platform application (Texttest), and we do not
want each user of that system to be forced to edit a config file..

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


Re: Order of addresses returned by socket.gethostbyname_ex()

2011-08-22 Thread Tomas Lidén
On 22 Aug, 13:26, Roy Smith  wrote:
> In article
> <356978ef-e9c1-48fd-bb87-849fe8e27...@p5g2000vbl.googlegroups.com>,
>  Tomas Lidén  wrote:
>
> > In what order are the addresses returned by socket.gethostbyname_ex()?
>
> > We know that gethostbyname() is indeterministic but hope that
> > gethostbyname_ex() has a specified order.
>
> Why would you hope that?  Or maybe a better question is, why would you
> expect that?  In general, all name resolution calls return results in
> arbitrary order.  In some cases, results are intentionally changed on
> every call (i.e. round-robin) in an attempt at load sharing.
>
> What kind of ordering were you hoping for?

See previous posts.

Basically I was asking about the contract for this method.. hoping
that it is deterministic.
Our testing indicated that the interfaces are returned in a specific
order, but we want to know if this is really the case (on all
platforms).

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


Re: Order of addresses returned by socket.gethostbyname_ex()

2011-08-22 Thread Roy Smith
In article 
,
 Tomas Lidén  wrote:

> In this particular case we have a host with several connections (LAN,
> WIFI, VmWare adapters etc). When using gethostbyname() we got a VmWare
> adapter but we wanted to get the LAN (or the "best" connection to our
> server).

Figuring out which is the best connection is a decidedly non-trivial 
problem.  You could try some heuristic like "pick the address which has 
the same network number as me".  But, what if you're multi-homed as 
well, and the target has addresses on more than one of the networks 
you're connected to?  Not to mention that even enumerating all of your 
own connections is non-trivial.

> With gethostbyname_ex() the ordering seemed to become LAN,
> Wifi, etc and that's good for us. But we don't know if this holds on
> other platforms (we're running on Windows 7).

You can't count on the ordering being anything in particular.  It's a 
lost cause.  You may think you can find patterns, and then write your 
application to depend on those, and you will eventually get burned.

> A cross-platform deterministic order would be excellent for us.

"A cross-platform deterministic X would be excellent" is a true 
statement for almost any value of X.  Many people have wasted much of 
their lives trying to achieve that goal, for various Xs.
-- 
http://mail.python.org/mailman/listinfo/python-list


write()

2011-08-22 Thread Hyun-su
Hi,
I have a file which is 3D data ([pixel_x, pixel_y, pixel_z])

I want to plot and write into other file as 1 dimension ([:, y_cut,
z_cut])

How can I do it?

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


Re: questions (& answers) about object, type, builtin types, class, metaclass and __getattribute__

2011-08-22 Thread Steven D'Aprano
Amirouche B. wrote:

> A) type vs object
> -
> 
> 1) object is the base object, it has no bases : len(object.__bases__)
> == 0

Correct, but for reference, a more direct test is:

object.__bases__ == ()

(no need for len).


> 2) every object in python inherit object :
> any_object_except_object.__bases__[-1] is object

Excluding old-style objects, I believe you are correct.


> 3) object's type is type : object.__class__ is type
> 4) type parent object is object : type.__bases__ == (object,)

The relationship between type and object is somewhat special, and needs to
be bootstrapped by the CPython virtual machine.

Arbitrary types (classes) inherit from object. That means the type is a
subclass of object:

>>> class K(object):pass
...
>>> issubclass(K, object)
True


What's less obvious is that types are themselves objects, and therefore are
instances of object:

>>> isinstance(K, object)
True


Since classes are objects, they have a type, namely ``type``.


This includes ``type`` itself:

* type is an instance of object
* object is an instance of type
* type is a subclass of object
* but object is NOT a subclass of type



> B) type vs metaclass
> 
> 
> 1) type is the first metaclass ?

Excluding old-style classes, yes, all custom classes (those you create with
the class statement) have a default metaclass of type.


> 2) type is its own metaclass : type(type) is type ?

Yes. Another bit of bootstrapping that the compiler does.

>>> type(type) is type
True


> 3) object's metaclass is type ?

Yes.


> 4) other metaclasses *MUST* inherit type ?

No. Metaclasses can be anything that mimics type.


>>> def meta(name, bases, dict):
... class X(object):
... pass
... return X
...
>>> class K(object):
... __metaclass__ = meta
... a = 1
...
>>>
>>> K



They don't even need to return a type/class. Like decorators, they can
return anything.


>>> def meta(name, bases, dict):
... return "spam"
...
>>> class K(object):
... __metaclass__ = meta
...
>>> K
'spam'



> 5) type(any_object) == last_metaclass_..., which is, most of the time,
> type ?

I'm not sure what you mean by "last_metaclass". But no. The type of an
object is its class:

type(42) => int
type("spam") => str
type(1.23) => float

However, the type of a class is *usually* type.



> C) type vs class
> 
> 
> 1) Type is the metaclass of most classes

Yes.


> 2) The class statement::
> 
> class MyClass(object):
> attribute = 1
> 
> def method(self):
> pass
> 
>translates to::
> 
> MyClass = type('MyClass', (object,), {'attribute': 1, 'method':
> def method: pass })

Except that the syntax won't work, the idea is broadly correct.


> 3) Instantiation of any class ``MyClass(*args, **kwargs)`` translates
> to::
> 
> type(MyClass).__call__(MyClass, *args, **kwargs)

Like any function call, MyClass(...) becomes

type(MyClass).__call__(self, ...)

with self=MyClass. Since type(MyClass) is usually ``type``, that gives:

type.__call__(MyClass, ...)


>This is due to __getattribute__ algorithm (see E)
> 
> 4) It's in type.__call__ that happens calls to __new__ and __init__


If type were written in pure Python, it would probably look something like
this:

class Type(object):
# ...
# other methods
# ...
def __call__(cls, *args, **kwargs):
instance = cls.__new__(cls, *args, **kwargs)
if isinstance(instance, cls):
instance.__init__(*args, **kwargs)
return instance

But see further on, for more complication.

Note that __new__ is special-cased as a staticmethod, hence it needs the
first argument to be passed directly.


> 5) 3) => classes are instance of type
> 
> 6) Since type.__call__ is used to instantiate instance of instance of
> type
>(rephrased: __call__ is used to instantiate classes) where is the
> code which
>is executed when we write ``type(myobject)`` or ``type('MyClass',
> bases, attributes)``


You would need to check the C implementation of type, but if I were doing
this in pure Python, I'd have something like this:

class Type(object):
def __call__(self, *args, **kwargs):
if self is Type:
if kwargs:
raise TypeError('unexpected keyword arguments')
# calling type(...) directly
if len(args) == 1:
# Single argument call, like type(x)
return x.__class__
else:
# Like type(name, bases, dict)
name, bases, dict = *args
cls = Type.__new__(Type, name, bases, dict)
if isinstance(cls, Type):
cls.__init__(name, bases, dict)
else:
# called from MyClass(...)
# which becomes type(MyClass).__call__(MyClass, ...)
# self here equals MyClass, which is an instance of type but 
# not type itself
instance = self.__

Re: Order of addresses returned by socket.gethostbyname_ex()

2011-08-22 Thread Roy Smith
In article 
<034ff4bf-e3e4-47ff-9a6c-195412431...@s20g2000yql.googlegroups.com>,
 Tomas Lidén  wrote:

> Basically I was asking about the contract for this method.. hoping
> that it is deterministic.

The contract for socket.gethostbyname_ex() is described at 
http://docs.python.org/library/socket.html#socket.gethostbyname_ex.  It 
says:

"Translate a host name to IPv4 address format, extended interface. 
Return a triple (hostname, aliaslist, ipaddrlist) where hostname is the 
primary host name responding to the given ip_address, aliaslist is a 
(possibly empty) list of alternative host names for the same address, 
and ipaddrlist is a list of IPv4 addresses for the same interface on the 
same host (often but not always a single address). gethostbyname_ex() 
does not support IPv6 name resolution, and getaddrinfo() should be used 
instead for IPv4/v6 dual stack support."

That's it.  It says nothing about ordering, so nothing about ordering 
should be inferred.

> Our testing indicated that the interfaces are returned in a specific
> order, but we want to know if this is really the case (on all
> platforms).

No, it is not.
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Order of addresses returned by socket.gethostbyname_ex()

2011-08-22 Thread Paul Kölle

Am 22.08.2011 13:37, schrieb Roy Smith:

In article
,
  Tomas Lidén  wrote:

A cross-platform deterministic order would be excellent for us.


"A cross-platform deterministic X would be excellent" is a true
statement for almost any value of X.  Many people have wasted much of
their lives trying to achieve that goal, for various Xs.

So true..., QOTW +1


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


Re: Order of addresses returned by socket.gethostbyname_ex()

2011-08-22 Thread Tomas Lidén
OK - thank you all for your contributions.
/T
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Optimizing Text Similarity Algorithm

2011-08-22 Thread Peter Otten
Yaşar Arabacı wrote:

> I originally posted this question on stackoverflow, you can find it here:
> http://stackoverflow.com/q/7133350/886669
> 
> I just want people check what I am doing and express their opinion about
> the thing I am doing is acceptable, or are there some expects of it that
> could change.

You are using only word frequencies to calculate the similarity of the 
document pairs. You can calculate these frequencies before you enter the the 
expensive 

for documentA in ...:
for documentB in ...:
calculate_similarity(documentA, documentB)

loops and therefore bring that portion of your could from O(n*n) to O(n). 
Here's is a sample where I applied that technique blindly to your code. I 
also had to remove the django dependency, so I've changed it to operate on 
text files.

import sys
import math
import re

from collections import Counter
from itertools import combinations

def get_words(text):
# FIXME
regex = re.compile("\W+", flags=re.UNICODE)
return re.split(regex, text)

def pairs(files):
"""Generate (title, wordlist) pairs.

(filename is used as title)
"""
for filename in files:
with open(filename) as f:
text = f.read()
yield filename, get_words(text)

def process(pairs):
triples = []
total = Counter()
for title, words in pairs:
c = Counter(words)
total.update(c.iterkeys())
sigma = sum(math.log(freq, 1.6) for freq in c.itervalues())
triples.append((title, c, sigma))

for (title1, freq1, sum1), (title2, freq2, sum2) in combinations(
triples, 2):
upper_part = 0

for word in freq1 & freq2:
adjusted1 = math.log(freq1[word], 1.6)
adjusted2 = math.log(freq2[word], 1.6)
upper_part += (
adjusted1 * adjusted2 * math.log(len(triples)/total[word]))

lower_part = math.sqrt(sum1 * sum2)

title1, title2 = sorted([title1, title2])
yield title1, title2, upper_part/lower_part


def main():
files = sys.argv[1:]

results = process(pairs(files))
results = sorted(results, key=lambda x: x[:2])
results.sort(key=lambda x: x[2], reverse=True)
print "\n".join("%s and %s => %f" % xcv for xcv in results)

if __name__ == "__main__":
main()


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


Re: locale.format without trailing zeros

2011-08-22 Thread Peter Otten
przemol...@poczta.fm wrote:

> How about this format:
> ',1'
> (the local zero is also not printed)
> 
> (I know this is strange but I need compatibility with local requirements)

I believe you have to do it yourself:

>>> locale.format("%f", 0.123)
'0,123000'
>>> locale.format("%f", 0.123).strip("0")
',123'

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


Re: Python Windows Extensions for Mac

2011-08-22 Thread johnny.venter
Chris, thank you for the information.  Focusing on Active Directory, I reviewed 
the info at the following site: 
http://technet.microsoft.com/en-us/library/cc961766.aspx

Based on this, I need to find a module that implements the LDAP APIs.  By 
default, it does not appear that Python can speak this language, I am using 
version 2.6.1.  The module I found is Python-LDAP (http://www.python-ldap.org/).

Does anyone have experience using this?

 On Sun, 21 Aug 2011 00:30:07 -0700 Chris Angelico  wrote  

>On Sun, Aug 21, 2011 at 6:38 AM, Johnny Venter  wrote: 
>> Yes, I want to make my queries from a remote non-Windows computer. Here is 
>> the scenario: 
>> 
>> From my mac, I want to use python to access and read objects from a remote  
>> Windows computer joined to a Windows 2003 functional level domain. Given 
>> this, what is the best way to accomplish this? 
>> 
> 
>Then the "use Python" part is relatively immaterial; what you need to 
>know is: What network protocol are you using to "access and read 
>objects"? Start by researching that; once you know the details (is it 
>even TCP/IP-based?), you can look into whether Python has facilities 
>for speaking that protocol. 
> 
>ChrisA 
>-- 
>http://mail.python.org/mailman/listinfo/python-list 
>

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


Re: Adding modified methods from another class without subclassing

2011-08-22 Thread John O'Hagan
On Mon, 22 Aug 2011 15:27:36 +1000
Steven D'Aprano  wrote:

> On Mon, 22 Aug 2011 03:04 pm John O'Hagan wrote:
> 
> > The "pitches" attribute represents the instances and as such I found
> > myself adding a lot of methods like:
> > 
> > def __getitem__(self, index):
> > return self.pitches[index]
> > 
> > def __len__(self):
> > return len(self.pitches)
> 
> 
> Looks like a call for (semi-)automatic delegation!
> 
> Try something like this:
> 
> 
> # Untested
> class MySeq(object):
> methods_to_delegate = ('__getitem__', '__len__', ...)
> pitches = ...  # make sure pitches is defined
> def __getattr__(self, name):
> if name in self.__class__.methods_to_delegate:
> return getattr(self.pitches, name)
> return super(MySeq, object).__getattr__(self, name)
> # will likely raise AttributeError

Thanks, this looks promising. I didn't know about __getattr__ or delegation. 
This example doesn't seem to work as is for special methods beginning with "__" 
(e.g.: "TypeError: object of type 'MyList' has no len()"). It seems that 
__getattr__ is not called for special methods. Also, it doesn't immediately 
suggest to me a way of modifying method calls (maybe __setattr__?). But it's 
certainly a neater way to get methods to operate on the attribute. I'm looking 
into it, and delegation generally.

However, I don't understand what the super call is doing. If the method isn't 
delegated, shouldn't it just fall back to getattr(self, name)?

Thanks and regards,

John


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


Re: Optimizing Text Similarity Algorithm

2011-08-22 Thread Neil Cerutti
On 2011-08-22, Peter Otten <__pete...@web.de> wrote:
> Ya??ar Arabac?? wrote:
>> I originally posted this question on stackoverflow, you can find it here:
>> http://stackoverflow.com/q/7133350/886669
>> 
>> I just want people check what I am doing and express their opinion about
>> the thing I am doing is acceptable, or are there some expects of it that
>> could change.

Perhaps check out difflib.SequenceMatcher.ratio to see if the
library function is good enough.

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


Re: write()

2011-08-22 Thread Ulrich Eckhardt
Hyun-su wrote:
> I have a file which is 3D data ([pixel_x, pixel_y, pixel_z])
> 
> I want to plot and write into other file as 1 dimension ([:, y_cut,
> z_cut])
> 
> How can I do it?

I'd write a loop along these lines:

  with open(outputfile, 'w') as o:
  for pixel_x, pixel_y, pixel_z in input_data:
  o.write(...)


If you want a better suggestion, I'd suggest that you provide more info. 
Also, and that is very important, you should indicate which efforts you 
already too. In particular, I'd suggest the introductory tutorial on Python 
(see http://docs.python.org) and E.S. Raymonds essay on smart questions 
(search the web for it). If you don't show effort, people will assume you 
are lazy and just need someone to do your homework for you.

Good luck!

Uli

-- 
Domino Laser GmbH
Geschäftsführer: Thorsten Föcking, Amtsgericht Hamburg HR B62 932

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


To cmd.exe

2011-08-22 Thread aba ca
Hi all!

How can I send to cmd.exe "netstat -an"?

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


Re: To cmd.exe

2011-08-22 Thread Tim Golden

On 22/08/2011 14:21, aba ca wrote:

How can I send to cmd.exe "netstat -an"?


Look at the subprocess module, and especially the .check_output
convenience function:

  http://docs.python.org/library/subprocess.html#subprocess.check_output

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


Re: Help with regular expression in python

2011-08-22 Thread Vlastimil Brom
Sorry, if I missed some further specification in the earlier thread or
if the following is oversimplification of the original problem (using
3 numbers instead of 32),
would something like the following work for your data?

>>> import re
>>> data = """2.201000e+01 2.15e+01 2.199000e+01 : (instance: 0)   :
>>>some description
... 2.201000e+01 2.15e+01 2.199000e+01 : (instance: 0)   :
  some description
... 2.201000e+01 2.15e+01 2.199000e+01 : (instance: 0)   :
  some description
... 2.201000e+01 2.15e+01 2.199000e+01 : (instance: 0)   :
  some description"""
>>> for res in 
>>> re.findall(r"(?m)^(?:(?:[-+]?(?:\d+(?:\.\d*)?|\.\d+)(?:[eE][-+]?\d+))?\s+){3}(?:.+)$",
>>>  data): print res
...
2.201000e+01 2.15e+01 2.199000e+01 : (instance: 0)   :
some description
2.201000e+01 2.15e+01 2.199000e+01 : (instance: 0)   :
some description
2.201000e+01 2.15e+01 2.199000e+01 : (instance: 0)   :
some description
2.201000e+01 2.15e+01 2.199000e+01 : (instance: 0)   :
some description
>>>

i.e. all parentheses are non-capturing (?:...) and there are extra
anchors for line begining and end ^...$ with the multiline flag set
via (?m)
Each result is one matching line in this sample (if you need to acces
single numbers, you could process these matches further or use the new
regex implementation mentioned earlier by mrab (its developer) with
the new match method captures() - using an appropriate pattern with
the needed groupings).

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


Re: Adding modified methods from another class without subclassing

2011-08-22 Thread John O'Hagan
On Mon, 22 Aug 2011 11:32:18 +0200
Peter Otten <__pete...@web.de> wrote:

> John O'Hagan wrote:
> 
> > I have a class like this:
> > 
> > class MySeq():
> > def __init__(self, *seq, c=12):
> > self.__c = c
> > self.__pc = sorted(set([i % __c for i in seq]))
> > self.order = ([[self.__pc.index(i % __c), i // __c] for i in seq])
> > #other calculated attributes
> > 
> > @property
> > def pitches(self):
> > return [self.__pc[i[0]] + i[1] * self.__c for i in self.order]
> > 
> > #other methods
> 
> That makes me dizzy. Are there any maxims in the Zen of Python that this 
> piece doesn't violate?

"Now is better than never"?

I know it looks crazy to take something apart and put it back together, as in 
this simplified example, but it does have a meaningful use: reducing a series 
of musical notes to a unique irreducible form "__pc", ("prime form" in 
pitch-class set theory), but keeping track of the actual current manifestation 
of that form via the writeable "order" attribute. That's why "pitches" must be 
called with each reference, because it may change, unlike "__pc", which can 
only change if the instance is reinitialised.

I am open to more elegant suggestions, of course. :)
[...]

> > 
> > So I wrote this function which takes a method, modifies it to apply to an
> > instance attribute, and takes care of any quirks:
> > 
> > def listmeth_to_attribute(meth, attr):
> > def new_meth(inst, *args):
> > #ensure comparison operators work:
> > args = [getattr(i, attr)  if isinstance(i, inst.__class__)
> > else i for i in args]
> > reference = getattr(inst, attr)
> > test = reference[:]
> > result = meth(test, *args)
> > #ensure instance is reinitialised
> > #if attribute has been changed:
> > if test != reference:
> > inst.__init__(*test)
> > #ensure slices are of same class
> > if isinstance(result, meth.__objclass__):
> > result = inst.__class__(*result)
> > return result
> > return new_meth
> > 
> > and this decorator to apply this function to all the list methods and add
> > them to MySeq:
> > 
> > def add_mod_methods(source_cls, modfunc, modfunc_args, *overrides):
> > """Overides = any methods in target to override from source"""
> > def decorator(target_cls):
> > for name, meth in vars(source_cls).items():
> > if name not in dir(target_cls) or name in overrides:
> > setattr(target_cls, name, modfunc(meth, *modfunc_args))
> > return target_cls
> > return decorator
> > 
> > a kind of DIY single inheritance, used like this:
> > 
> > @add_mod_methods(list, listmeth_to_attribute, ('pitches',), '__repr__')
> > class MySeq():
> > .

[...]

>
> In the standard library functools.total_ordering uses that technique and I 
> find the implications hard to understand:
> 
> http://bugs.python.org/issue10042
> 
> Your decorator looks even more complex; I'd only recommend using it if 
> you're absolutely sure you're clever enough to debug it ;)


It looks to me like that total_ordering bug is a result of using comparison 
operators on objects which may not implement them; I'm pretty sure the 
listmeth_to_attribute function above can only allow comparing lists with lists 
or raising an exception. Although I must admit that this creates an unexpected 
bug/feature whereby lists and MySeqs can be successfully compared, so I take 
your point! 

Thanks for your reply,

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


Re: How to make statements running in strictly sequential fashion like in an interactive shell

2011-08-22 Thread lzlu123
On Aug 19, 6:38 pm, aspineux  wrote:
> On Aug 19, 5:00 pm, lzlu123  wrote:
>
>
>
>
>
>
>
>
>
> > I have an instrument that has a RS232 type serial comm port and I need
> > to connect to and control. I use Python 3.2 in Windows XP, plus
> > pySerial module. I have a problem when I execute a script consisting
> > of statements that open the comm port, configure it, write strings to
> > and receive strings from it. Thoese strings aer either commands
> > pertinent to the instrument (control) or responses from the instrument
> > (response).
>
> > When those statements are executed in a python interpreter
> > interactively (at >>>), I get what I expect and the results are good
> > and correct. However, when I execute the script, either being invoked
> > within the interpreter or run file, I don’t get what I want. The
> > statements in the script is the same as what I use in the interactive
> > interpreter.
>
> > Why do I get the strange behavior and how can I change the script to
> > make it to behave like in interactive interpreter?
>
> > --script---
> > def read(comport):
>
> >     wrt_str=b'movt 3000'+b'\r\n'
> >     ret_str=comport.write(wrt_str)
>
> >     wrt_str=b'scan'+b'\r\n'
> >     ret_str=comport.write(wrt_str)
>
> >     rsp_str=comport.readlines() #1
>
> You use readlines() with a s at the end !
>
> * Note that when the serial port was opened with no timeout, that
> readline()
> * blocks until it sees a newline (or the specified size is reached)
> * and that readlines() would never return and therefore refuses to
> work
> * (it raises an exception in this case)!
>
>
>
>
>
>
>
>
>
>
>
> >     wrt_str=b'hllo'+b'\r\n'
> >     ret_str=comport.write(wrt_str)
>
> >     rsp_str=comport.readlines()#2
> > --
>
> > The problem is with the lines above with ###. In interactive mode,
> > there is about 1 second delay at #1, and 9 seonds delay at #2. I get
> > correct responses there. However, if I execute the script above, there
> > is no delay at all and I get incorrect results (garbage). I set the
> > read timeout to 0 in comm port set up, as
>
> > comport.timeout=0
> > So the comport should be in blocking mode if it waits for the end of
> > line or end of file.
>
> Wrong :
>
>     timeout = None: wait forever
>     timeout = 0: non-blocking mode (return immediately on read)
>     timeout = x: set timeout to x seconds (float allowed)
>
>
>
>
>
>
>
>
>
>
>
> > I tried many things, like exec (execfile in 2.7), but at no avail.
>
> > I have an update to the original post I made a few days ago. I think I
> > know what the problem is and want to know if anyone has a solution:
>
> > After putting "print" and "time.sleep(delay)" after every statement, I
> > found when the script is running, it appears going around the pyserial
> > statement, such as "comport.write(..)" or "comport.readlines(...)"
> > while the pyserial command is executing (appearing as waiting and
> > busying doing some thing, you know serial port is slow). So for
> > example, when I exec all statements in a python interactive shell, I
> > am not able to type and run a new statement if the previous one is not
> > returned. Let's ay, if comport.readlines() is not returning, I can't
> > type and run the next comport.write(...) statemtn. However, in a
> > script that is running, if the comport.readlines() is busy reading,
> > the next statement is running, if the next statement happens to be a
> > comport.write() which will abort the reading.
>
> > Is there any way to force the python script to behave like running
> > exactly sequentially?
>
> You have some new things to try

I tried to set the timeout to different values other than 0, but still
got same result -- ruuning script directly  behaves differently from
executing statements in interactive shell.  I even tried using read
instead of readline(s).
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: How to make statements running in strictly sequential fashion like in an interactive shell

2011-08-22 Thread lzlu123
On Aug 19, 11:06 pm, Javier  wrote:
> Never used it, but I think you can try this:
>
> Pexpect - a Pure Python Expect-like module
> Pexpect is a pure Python Expect-like module. Pexpect makes 
> Python...www.noah.org/python/pexpect/
>
>
>
>
>
>
>
> lzlu123  wrote:
> > I have an instrument that has a RS232 type serial comm port and I need
> > to connect to and control. I use Python 3.2 in Windows XP, plus
> > pySerial module. I have a problem when I execute a script consisting
> > of statements that open the comm port, configure it, write strings to
> > and receive strings from it. Thoese strings aer either commands
> > pertinent to the instrument (control) or responses from the instrument
> > (response).
>
> > When those statements are executed in a python interpreter
> > interactively (at >>>), I get what I expect and the results are good
> > and correct. However, when I execute the script, either being invoked
> > within the interpreter or run file, I don???t get what I want. The
> > statements in the script is the same as what I use in the interactive
> > interpreter.
>
> > Why do I get the strange behavior and how can I change the script to
> > make it to behave like in interactive interpreter?
>
> > --script---
> > def read(comport):
>
> >    wrt_str=b'movt 3000'+b'\r\n'
> >    ret_str=comport.write(wrt_str)
>
> >    wrt_str=b'scan'+b'\r\n'
> >    ret_str=comport.write(wrt_str)
>
> >    rsp_str=comport.readlines() #1
>
> >    wrt_str=b'hllo'+b'\r\n'
> >    ret_str=comport.write(wrt_str)
>
> >    rsp_str=comport.readlines()#2
> > --
>
> > The problem is with the lines above with ###. In interactive mode,
> > there is about 1 second delay at #1, and 9 seonds delay at #2. I get
> > correct responses there. However, if I execute the script above, there
> > is no delay at all and I get incorrect results (garbage). I set the
> > read timeout to 0 in comm port set up, as
>
> > comport.timeout=0
> > So the comport should be in blocking mode if it waits for the end of
> > line or end of file.
>
> > I tried many things, like exec (execfile in 2.7), but at no avail.
>
> > I have an update to the original post I made a few days ago. I think I
> > know what the problem is and want to know if anyone has a solution:
>
> > After putting "print" and "time.sleep(delay)" after every statement, I
> > found when the script is running, it appears going around the pyserial
> > statement, such as "comport.write(..)" or "comport.readlines(...)"
> > while the pyserial command is executing (appearing as waiting and
> > busying doing some thing, you know serial port is slow). So for
> > example, when I exec all statements in a python interactive shell, I
> > am not able to type and run a new statement if the previous one is not
> > returned. Let's ay, if comport.readlines() is not returning, I can't
> > type and run the next comport.write(...) statemtn. However, in a
> > script that is running, if the comport.readlines() is busy reading,
> > the next statement is running, if the next statement happens to be a
> > comport.write() which will abort the reading.
>
> > Is there any way to force the python script to behave like running
> > exactly sequentially?

I am using Python 3.2 in Windows in which Pexpect appeared
problematic.
-- 
http://mail.python.org/mailman/listinfo/python-list


CGI input: Filter dict.update() unwanted variables

2011-08-22 Thread Gnarlodious
In my last post I learned of the necessity of filtering CGI input, so
what I want to do is set a dict of allowable variable names:

allowedVariables = {'eeny':None, 'meeny':None, 'miny':None, 'mo':None}

# Set up a FieldStorage object:
import cgi
inputVariables = cgi.FieldStorage()
for name, value in {"eeny" : "value1",  "meeny" : "value2",  "miny" :
"value3",  "mofo" : "value4"}.items():
inputVariables.list.append(cgi.MiniFieldStorage(name, value))

allowedVariables.update(((key, inputVariables[key].value) for key in
inputVariables))
allowedVariables

As you can see, the variable 'mofo' gets added to allowedVariables,
which is normal behavior. Is there an easy way to limit updates to
ONLY variables in the allowedVariables dict?

And in addition, maybe return an error so the attacker can be blocked?

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


Re: CGI input: Filter dict.update() unwanted variables

2011-08-22 Thread Miki Tebeka
> Is there an easy way to limit updates to
> ONLY variables in the allowedVariables dict?

allowedVariables = ['eeny', 'meeny', 'miny', 'mo']
form = cgi.FieldStorage()
safe_input = dict((key, form.getvalue(key)) for key in allowedVariables) 
 
> And in addition, maybe return an error so the attacker can be blocked?
You can check if there is a "non-allowed variable" and then return HTTP error.
if set(form) - set(allowedVariables):
print('Status: 406\n\n')
raise SystemExit()

HTH
--
Miki Tebeka 
http://pythonwise.blogspot.com

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


Re: questions (& answers) about object, type, builtin types, class, metaclass and __getattribute__

2011-08-22 Thread Stephen Hansen
On 8/22/11 3:02 AM, Amirouche B. wrote:
> A) type vs object
> -
> 
> 1) object is the base object, it has no bases : len(object.__bases__)
> == 0
> 2) every object in python inherit object :
> any_object_except_object.__bases__[-1] is object

Not exactly. Python has two somewhat different object models, "old style
classes" and "new style classes", with slightly different behavior and
internal structure.

   class Foo: pass

is an "old-style class", dated back to Python's ancient past. This all
relates to the fact that 'type' and 'class' used to be two pretty
different things, where one was something you mostly did only in C, and
one was something you did (mostly) only in Python. They are largely the
same now.

> 3) object's type is type : object.__class__ is type
> 4) type parent object is object : type.__bases__ == (object,)

Saying "type" and "parent" and the like for new-style classes is
something of a misnomer. For "type" and "object", these things aren't
constructed like this.

What you have here is technically true if you go poke at it in the
interpreter, but it doesn't really /mean/ anything because its not how
these objects came to be and is circular and a bit confusing. These
fundamental objects are created special.


> B) type vs metaclass
> 
> 
> 1) type is the first metaclass ?

Type is the basic, default "metaclass", yes. A metaclass is a callable
that constructs class objects.

> 2) type is its own metaclass : type(type) is type ?

Only in a purely theoretical way. It doesn't actually mean anything;
moreover, type(something) is NOT how you determine somethings metaclass.
Its how you determine somethings type.

The two concepts may be very distinct. Lots of things don't have
metaclasses.

> 3) object's metaclass is type ?

Again, only theoretically.

> 4) other metaclasses *MUST* inherit type ?

Absolutely not. Any callable can be a metaclasss. Despite its name, it
doesn't have to be itself a class or anything. Just something you can
call with er, 3 (I forget exactly) arguments, and which returns a
constructed class object.

> 5) type(any_object) == last_metaclass_..., which is, most of the time,
> type ?

Not necessarily at all. In fact, there is no way I'm aware of to
determine if a metaclass was involved in a classes construction unless
said metaclass wants to provide such a mechanism.

Metaclasses are kind of a  hack. They are a way to hook into the class
construction that's normally done and do something, anything you want,
(even hijack the whole procedure and NOT construct a class at all, but
play a song if you want) before its all finished.

For example, this is a metaclass I've used:

PageTypes = {}

class _PageRegistration(type):
def __new__(cls, name, bases, dct):
klass = type.__new__(cls, name, bases, dct)
typename = name[:-9].lower()
if not typename:
typename = None

PageTypes[typename] = klass
klass.Type = typename

return klass

class QueuePage(sc.SizedPanel):
__metaclass__ = _PageRegistration

Note, the fact that my _PageRegistration metaclass inherits is itself a
class which inherits from type is just one convenient way to write
metaclasses. It could as simply have been just a function.

Metaclasses are somewhat poorly named in that they are really, "creation
hooks".

> C) type vs class
> 
> 
> 1) Type is the metaclass of most classes

Yes and no. Yes, in that most classes are created using the default
mechanism inside CPython. The class body is executed in a scope, the
resulting dictionary is bound to a new class object, bases and the like
are set, and such.

No in that it really just, IIUC, skips the whole "metaclass" part of the
process because this 'default mechanism' doesn't need to call out into
other code to do its job. At least, I think-- May be wrong here,
metaclasses are something of a dark voodoo and I'm not 100% entirely
familiar with the internal workings of CPython.

But functionally, a metaclass is the chunk of code responsible for the
actual physical construction of the class object.

> 2) The class statement::
> 
> class MyClass(object):
> attribute = 1
> 
> def method(self):
> pass
> 
>translates to::
> 
> MyClass = type('MyClass', (object,), {'attribute': 1, 'method':
> def method: pass })

Translates to, I don't know about that. Is functionally equivalent, yes.
It is more or less what happens.

> 3) Instantiation of any class ``MyClass(*args, **kwargs)`` translates
> to::
> 
> type(MyClass).__call__(MyClass, *args, **kwargs)
> 
>This is due to __getattribute__ algorithm (see E)
> 4) It's in type.__call__ that happens calls to __new__ and __init__

Again, "translates to" is suggesting "this is what happens when you do
X", which I don't know if is strictly true. CPython inside may be
optimizing this whole process. Especially when it comes to "magic

Re: questions (& answers) about object, type, builtin types, class, metaclass and __getattribute__

2011-08-22 Thread Chris Angelico
On Mon, Aug 22, 2011 at 4:41 PM, Stephen Hansen
 wrote:
> Not exactly. Python has two somewhat different object models, "old style
> classes" and "new style classes", with slightly different behavior and
> internal structure.
>
>   class Foo: pass
>
> is an "old-style class", dated back to Python's ancient past. This all
> relates to the fact that 'type' and 'class' used to be two pretty
> different things, where one was something you mostly did only in C, and
> one was something you did (mostly) only in Python. They are largely the
> same now.

And "now" includes everything in Python 3, where a class implicitly
derives from object if no other subclassing is given.

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


Re: Order of addresses returned by socket.gethostbyname_ex()

2011-08-22 Thread Steven D'Aprano
Tomas Lidén wrote:


> In this particular case we have a host with several connections (LAN,
> WIFI, VmWare adapters etc). When using gethostbyname() we got a VmWare
> adapter but we wanted to get the LAN (or the "best" connection to our
> server). 

Define "best" connection.

If I tell you that my server has the following 6 connections:

Wifi1, LAN4, LAN1, LAN2, Wifi2, LAN5

which one is "best"?

Once you have an algorithm for deciding which connection is "best" for
everybody, then you can check whether gethostbyname_ex uses that algorithm,
or some other one.



-- 
Steven

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


Re: relative speed of incremention syntaxes (or "i=i+1" VS "i+=1")

2011-08-22 Thread Emile van Sebille

On 8/22/2011 2:55 AM Richard D. Moores said...

I couldn't resist giving it a try. Using Python 3.2.1 on a 64-bit
Windows 7 machine with a 2.60 gigahertz AMD Athlon II X4 620
processor, I did 18 tests, alternating between n=n+1 and n+=1 (so 9
each).

The fastest for n+=1 was
C:\Windows\System32>  python -m timeit  -r 3 -s "n=0" "n += 1"
1000 loops, best of 3: 0.0879 usec per loop

The slowest for n+=1 was
C:\Windows\System32>  python -m timeit  -r 3 -s "n=0" "n += 1"
1000 loops, best of 3: 0.0902 usec per loop

The fastest for n = n + 1 was
C:\Windows\System32>  python -m timeit  -r 3 -s "n=0" "n=n+1"
1000 loops, best of 3: 0.0831 usec per loop

The slowest for n = n + 1 was
C:\Windows\System32>  python -m timeit  -r 3 -s "n=0" "n=n+1"
1000 loops, best of 3: 0.0842 usec per loop

Coincidence?



Naaa.. I just ran it twice -- once per ... _this_ is coincidence.  :)

Microsoft Windows XP [Version 5.1.2600]
(C) Copyright 1985-2001 Microsoft Corp.

C:\Documents and Settings\Emile>python -m timeit  -r 3 -s "n=0" "n=n+1"
1000 loops, best of 3: 0.108 usec per loop

C:\Documents and Settings\Emile>python -m timeit  -r 3 -s "n=0" "n += 1"
1000 loops, best of 3: 0.108 usec per loop

C:\Documents and Settings\Emile>


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


Setting the time in Win7

2011-08-22 Thread Bob Greschke

Permissions!

We're running in an account as an administrator (the only account on 
the laptops) and the program just calls system(time ) and 
system(date ) after reading it from a connected GPS receiver.  I've 
fiddled with everything I could find in the registry and with the 
secpol.msc program and there doesn't seem to be any reason that it 
can't set the time, but it can't.  Any ideas?


Thanks!

Bob

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


Re: relative speed of incremention syntaxes (or "i=i+1" VS "i+=1")

2011-08-22 Thread Emile van Sebille

On 8/22/2011 9:35 AM Emile van Sebille said...

On 8/22/2011 2:55 AM Richard D. Moores said...

Coincidence?



Naaa.. I just ran it twice -- once per ... _this_ is coincidence. :)

Microsoft Windows XP [Version 5.1.2600]
(C) Copyright 1985-2001 Microsoft Corp.

C:\Documents and Settings\Emile>python -m timeit -r 3 -s "n=0" "n=n+1"
1000 loops, best of 3: 0.108 usec per loop

C:\Documents and Settings\Emile>python -m timeit -r 3 -s "n=0" "n += 1"
1000 loops, best of 3: 0.108 usec per loop


Actually, it's more likely I hit a minimum resolution issue -- I ran it 
twenty more times and never got a different result.


Emile


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


Re: Order of addresses returned by socket.gethostbyname_ex()

2011-08-22 Thread Terry Reedy

On 8/22/2011 7:39 AM, Tomas Lidén wrote:

On 22 Aug, 13:26, Roy Smith  wrote:

In article
<356978ef-e9c1-48fd-bb87-849fe8e27...@p5g2000vbl.googlegroups.com>,
  Tomas Lidén  wrote:


In what order are the addresses returned by socket.gethostbyname_ex()?



We know that gethostbyname() is indeterministic but hope that
gethostbyname_ex() has a specified order.


Why would you hope that?  Or maybe a better question is, why would you
expect that?  In general, all name resolution calls return results in
arbitrary order.  In some cases, results are intentionally changed on
every call (i.e. round-robin) in an attempt at load sharing.

What kind of ordering were you hoping for?


See previous posts.

Basically I was asking about the contract for this method..


The doc "Return a triple (hostname, aliaslist, ipaddrlist) where 
hostname is the primary host name responding to the given ip_address, 
aliaslist is a (possibly empty) list of alternative host names for the 
same address, and ipaddrlist is a list of IPv4/v6 addresses for the same 
interface on the same host (most likely containing only a single address)."



hoping that it is deterministic.


As far as I can see, that is not in the contract.


Our testing indicated that the interfaces are returned in a specific
order, but we want to know if this is really the case (on all
platforms).


Even if it were so now, a patch could change things.

--
Terry Jan Reedy


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


Re: Setting the time in Win7

2011-08-22 Thread Tim Golden

If memory serves, you need to enable a specific privilege to
set the time in Vista+. Just a moment...

Have a look here:

  http://support.microsoft.com/kb/300022

and look for SeSystemtimePrivilege generally. Sorry; I'm
a bit rushed at the moment. Feel free to post back if
that isn't clear

TJG

On 22/08/2011 17:35, Bob Greschke wrote:

Permissions!

We're running in an account as an administrator (the only account on the
laptops) and the program just calls system(time ) and system(date
) after reading it from a connected GPS receiver. I've fiddled with
everything I could find in the registry and with the secpol.msc program
and there doesn't seem to be any reason that it can't set the time, but
it can't. Any ideas?

Thanks!

Bob



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


Re: Help on PyQt4 QProcess

2011-08-22 Thread Edgar Fuentes
On Aug 20, 4:36 am, Phil Thompson  wrote:
> On Fri, 19 Aug 2011 14:32:12 -0700 (PDT), Edgar Fuentes
>
>
>
>
>
>
>
>
>
>  wrote:
> > On Aug 19, 4:21 pm, Carl Banks  wrote:
> >> On Friday, August 19, 2011 12:55:40 PM UTC-7, Edgar Fuentes wrote:
> >> > On Aug 19, 1:56 pm, Phil Thompson
> >> >  wrote:
> >> > > On Fri, 19 Aug 2011 10:15:20 -0700 (PDT), Edgar Fuentes
> >> > >  wrote:
> >> > > > Dear friends,
>
> >> > > > I need execute an external program from a gui using PyQt4, to
> avoid
> >> > > > that hang the main thread, i must connect the signal
> >> > > > "finished(int)"
> >> > > > of a QProcess to work properly.
>
> >> > > > for example, why this program don't work?
>
> >> > > >    from PyQt4.QtCore import QProcess
> >> > > >    pro = QProcess() # create QProcess object
> >> > > >    pro.connect(pro, SIGNAL('started()'), lambda
> >> > > > x="started":print(x))        # connect
> >> > > >    pro.connect(pro, SIGNAL("finished(int)"), lambda
> >> > > > x="finished":print(x))
> >> > > >    pro.start('python',['hello.py'])        # star hello.py
> program
> >> > > > (contain print("hello world!"))
> >> > > >    timeout = -1
> >> > > >    pro.waitForFinished(timeout)
> >> > > >    print(pro.readAllStandardOutput().data())
>
> >> > > > output:
>
> >> > > >    started
> >> > > >    0
> >> > > >    b'hello world!\n'
>
> >> > > > see that not emit the signal finished(int)
>
> >> > > Yes it is, and your lambda slot is printing "0" which is the return
> >> > > code
> >> > > of the process.
>
> >> > > Phil
>
> >> > Ok, but the output should be:
>
> >> >     started
> >> >     b'hello world!\n'
> >> >     finished
>
> >> > no?.
>
> >> > thanks Phil
>
> >> Two issues.  First of all, your slot for the finished function does not
> >> have the correct prototype, and it's accidentally not throwing an
> >> exception because of your unnecessary use of default arguments.
>  Anyway,
> >> to fix that, try this:
>
> >> pro.connect(pro, SIGNAL("finished(int)"), lambda v,
> >> x="finished":print(x))
>
> >> Notice that it adds an argument to the lambda (v) that accepts the int
> >> argument of the signal.  If you don't have that argument there, the int
> >> argument goes into x, which is why Python prints 0 instead of
> "finished".
>
> >> Second, processess run asynchrously, and because of line-buffering, IO
> >> can output asynchronously, and so there's no guarantee what order
> output
> >> occurs.  You might try calling the python subprocess with the '-u'
> switch
> >> to force unbuffered IO, which might be enough to force synchronous
> output
> >> (depending on how signal/slot and subprocess semantics are
> implemented).
>
> >> Carl Banks
>
> > Thanks Carl, your intervention was very helpful for me, this solve my
> > semantic error. I need to study more about signal/slots and process.
>
> In which case you should look at the modern, Pythonic connection syntax
> rather than the old one...
>
>     pro.started.connect(lambda: print("started"))
>     pro.finished.connect(lambda: print("finished"))
>
> Phil

Pythonic, great!, more straightforward.

Thanks again Phil and Carl.

best regards,

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


python apt help

2011-08-22 Thread Smaine Kahlouch

Hi,

I'm trying to use the apt_pkg module but it seems to me really hard and 
i didn't find samples for what i'm trying to do.

Actually it should be simple :

- Installing a package like 'aptitude install mypackage'
- test if a package is already installed

Thank you all for your help,
Sam
--
http://mail.python.org/mailman/listinfo/python-list


Re: Adding modified methods from another class without subclassing

2011-08-22 Thread Peter Otten
John O'Hagan wrote:

> On Mon, 22 Aug 2011 11:32:18 +0200
> Peter Otten <__pete...@web.de> wrote:
> 
>> John O'Hagan wrote:
>> 
>> > I have a class like this:
>> > 
>> > class MySeq():
>> > def __init__(self, *seq, c=12):
>> > self.__c = c
>> > self.__pc = sorted(set([i % __c for i in seq]))
>> > self.order = ([[self.__pc.index(i % __c), i // __c] for i in
>> > seq])
>> > #other calculated attributes
>> > 
>> > @property
>> > def pitches(self):
>> > return [self.__pc[i[0]] + i[1] * self.__c for i in self.order]
>> > 
>> > #other methods
>> 
>> That makes me dizzy. Are there any maxims in the Zen of Python that this
>> piece doesn't violate?
> 
> "Now is better than never"?
> 
> I know it looks crazy to take something apart and put it back together, as
> in this *simplified* *example*

emphasis mine ;)

> , but it does have a meaningful use: reducing a
> series of musical notes to a unique irreducible form "__pc", ("prime form"
> in pitch-class set theory), but keeping track of the actual current
> manifestation of that form via the writeable "order" attribute. That's why
> "pitches" must be called with each reference, because it may change,
> unlike "__pc", which can only change if the instance is reinitialised.
> 
> I am open to more elegant suggestions, of course. :)
> [...]

Dunno. Maybe you could inherit from collections.(Mutable)Sequence and 
somewhat reduce the number of methods you'd have to implement. 
Or you introduce a Pitch (Python) class that knows about its pitch class, 
and put that into a normal list:

>>> class Pitch(int):
... @property
... def pitch_class(self):
... return self % 12
... def __repr__(self):
... return "Pitch(%s, pitch_class=%s)" % (self, 
self.pitch_class)
...
>>> map(Pitch, [1,0,42])
[Pitch(1, pitch_class=1), Pitch(0, pitch_class=0), Pitch(42, pitch_class=6)]


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


Re: Setting the time in Win7

2011-08-22 Thread Bob Greschke

Hi!

Yup.  I've been from one end of that article to the other with no luck. 
It must be something to do with the shell that the system() call 
creates not having permission to set the time, but I can't figure out 
how to get around it.  Just using the GUI the account where the user is 
running the program from has permission to set the time.


On 2011-08-22 11:41:45 -0600, Tim Golden  said:


If memory serves, you need to enable a specific privilege to
set the time in Vista+. Just a moment...

Have a look here:

   http://support.microsoft.com/kb/300022

and look for SeSystemtimePrivilege generally. Sorry; I'm
a bit rushed at the moment. Feel free to post back if
that isn't clear

TJG

On 22/08/2011 17:35, Bob Greschke wrote:

Permissions!

We're running in an account as an administrator (the only account on the
laptops) and the program just calls system(time ) and system(date
) after reading it from a connected GPS receiver. I've fiddled with
everything I could find in the registry and with the secpol.msc program
and there doesn't seem to be any reason that it can't set the time, but
it can't. Any ideas?

Thanks!

Bob



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


Error when deleting and reimporting subpackages

2011-08-22 Thread Matthew Brett
Hi,

I recently ran into this behavior:

>>> import sys
>>> import apkg.subpkg
>>> del sys.modules['apkg']
>>> import apkg.subpkg as subpkg
Traceback (most recent call last):
  File "", line 1, in 
AttributeError: 'module' object has no attribute 'subpkg'

where 'apkg' and 'subpkg' comprise empty __init__.py files to simplify the 
example.

It appears then, that importing a subpackage, then deleting the containing 
package from sys.modules, orphans the subpackage in an unfixable state. 

I ran into this because the nose testing framework does exactly this kind of 
thing when loading test modules, causing some very confusing errors and 
failures.

Is this behavior expected?

Best,

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


Re: Error when deleting and reimporting subpackages

2011-08-22 Thread Stephen Hansen
On 8/22/11 11:51 AM, Matthew Brett wrote:
> Hi,
> 
> I recently ran into this behavior:
> 
 import sys
 import apkg.subpkg
 del sys.modules['apkg']
 import apkg.subpkg as subpkg
> Traceback (most recent call last):
>   File "", line 1, in 
> AttributeError: 'module' object has no attribute 'subpkg'
> 
> where 'apkg' and 'subpkg' comprise empty __init__.py files to simplify the 
> example.
> 
> It appears then, that importing a subpackage, then deleting the containing 
> package from sys.modules, orphans the subpackage in an unfixable state. 
> 
> I ran into this because the nose testing framework does exactly this kind of 
> thing when loading test modules, causing some very confusing errors and 
> failures.
> 
> Is this behavior expected?

Yes. Doing an import of "apkg.subpkg" results in more then just "test1"
being cached in sys.modules, and you're removing half of that so leaving
Python in a weird state.

You also want to del sys.modules["apkg.subpkg"], then you'll be able to
re-import apkg.subpkg. I.e:

Python 2.7.1 (r271:86882M, Nov 30 2010, 10:35:34)
[GCC 4.2.1 (Apple Inc. build 5664)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> import sys
>>> import test1.test2
>>> del sys.modules['test1']
>>> del sys.modules['test1.test2']
>>> import test1.test2 as test2
>>>

-- 

   Stephen Hansen
   ... Also: Ixokai
   ... Mail: me+list/python (AT) ixokai (DOT) io
   ... Blog: http://meh.ixokai.io/



signature.asc
Description: OpenPGP digital signature
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Order of addresses returned by socket.gethostbyname_ex()

2011-08-22 Thread Nobody
On Sun, 21 Aug 2011 23:37:42 -0700, Tomas Lidén wrote:

> In what order are the addresses returned by socket.gethostbyname_ex()?
> 
> We know that gethostbyname() is indeterministic but hope that
> gethostbyname_ex() has a specified order.

It doesn't. In fact, the order of the IP addresses may have been
deliberately randomised (by the resolver and/or DNS server) in order to
provide a simple form of load balancing.

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


Re: Error when deleting and reimporting subpackages

2011-08-22 Thread Matthew Brett
On Monday, August 22, 2011 12:06:44 PM UTC-7, Stephen Hansen wrote:
> On 8/22/11 11:51 AM, Matthew Brett wrote:
> > Hi,
> > 
> > I recently ran into this behavior:
> > 
>  import sys
>  import apkg.subpkg
>  del sys.modules['apkg']
>  import apkg.subpkg as subpkg
> > Traceback (most recent call last):
> >   File "", line 1, in 
> > AttributeError: 'module' object has no attribute 'subpkg'
> > 
> > where 'apkg' and 'subpkg' comprise empty __init__.py files to simplify the 
> > example.
> > 
> > It appears then, that importing a subpackage, then deleting the containing 
> > package from sys.modules, orphans the subpackage in an unfixable state. 
> > 
> > I ran into this because the nose testing framework does exactly this kind 
> > of thing when loading test modules, causing some very confusing errors and 
> > failures.
> > 
> > Is this behavior expected?
> 
> Yes. Doing an import of "apkg.subpkg" results in more then just "test1"
> being cached in sys.modules, and you're removing half of that so leaving
> Python in a weird state.
> 
> You also want to del sys.modules["apkg.subpkg"], then you'll be able to
> re-import apkg.subpkg. I.e:
> 
> Python 2.7.1 (r271:86882M, Nov 30 2010, 10:35:34)
> [GCC 4.2.1 (Apple Inc. build 5664)] on darwin
> Type "help", "copyright", "credits" or "license" for more information.
> >>> import sys
> >>> import test1.test2
> >>> del sys.modules['test1']
> >>> del sys.modules['test1.test2']
> >>> import test1.test2 as test2
> >>>

Yes, sorry, I should have mentioned that I explored these kind of variations.

I think I see that there isn't an obvious way for del sys.modules['apkg'] to 
know to delete or modify 'apkg.subpkg', because sys.modules is just a dict.

However, I could imagine the import machinery being able to recognize that 
'apkg.subpkg' is broken, and re-import it without error.

Is that reasonable?

Best,

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


Re: Setting the time in Win7

2011-08-22 Thread Bob Greschke
Several people have been hacking away on this computer we are testing 
on, so I'm not sure what settings -- other than all of them -- have 
been messed with, but popen("time ...") seems to work, but system("time 
...") does not.  I'm going to restore the machine to its original state 
and see what happens.


Bob

On 2011-08-22 11:41:45 -0600, Tim Golden  said:


If memory serves, you need to enable a specific privilege to
set the time in Vista+. Just a moment...

Have a look here:

   http://support.microsoft.com/kb/300022

and look for SeSystemtimePrivilege generally. Sorry; I'm
a bit rushed at the moment. Feel free to post back if
that isn't clear

TJG

On 22/08/2011 17:35, Bob Greschke wrote:

Permissions!

We're running in an account as an administrator (the only account on the
laptops) and the program just calls system(time ) and system(date
) after reading it from a connected GPS receiver. I've fiddled with
everything I could find in the registry and with the secpol.msc program
and there doesn't seem to be any reason that it can't set the time, but
it can't. Any ideas?

Thanks!

Bob



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


Re: MailingLogger 3.4.0 Released!

2011-08-22 Thread Chris Withers
Heh, of course, I forgot the setuptools-git extension to make 
"include_package_data=True" work, so this release was pretty useless, 
other than the docs on packages.python.org/testfixtures ;-)


Anyway, 3.4.1 has now been released which fixes this!

cheers,

Chris

On 17/08/2011 23:37, Chris Withers wrote:

I'm pleased to announce a new release of Mailinglogger.

Mailinglogger provides two handlers for the standard python
logging framework that enable log entries to be emailed either as the
entries are logged or as a summary at the end of the running process.

The handlers have the following features:

- customisable and dynamic subject lines for emails sent

- emails sent with a configurable headers for easy filtering

- flood protection to ensure the number of emails sent is not excessive

- support for SMTP servers that require authentication

- fully documented and tested

This release has no functional changes but finally ships with a full new
set of Sphinx docs:

http://packages.python.org/mailinglogger/

For more information, please see:
http://www.simplistix.co.uk/software/python/mailinglogger
or
http://pypi.python.org/pypi/mailinglogger

cheers,

Chris



--
Simplistix - Content Management, Batch Processing & Python Consulting
- http://www.simplistix.co.uk
--
http://mail.python.org/mailman/listinfo/python-list


Re: Error when deleting and reimporting subpackages

2011-08-22 Thread Stephen Hansen
On Mon, Aug 22, 2011 at 12:14 PM, Matthew Brett wrote:

> Yes, sorry, I should have mentioned that I explored these kind of
> variations.
>
> I think I see that there isn't an obvious way for del sys.modules['apkg']
> to know to delete or modify 'apkg.subpkg', because sys.modules is just a
> dict.
>
> However, I could imagine the import machinery being able to recognize that
> 'apkg.subpkg' is broken, and re-import it without error.


> Is that reasonable?
>

Not really, no. Despite the fact that you can sorta do it, and that there's
this "reload" function, Python doesn't really support reloading of modules /
code. If you want to do it, you basically have to go out and _do_ it
yourself.

If you muck about in sys.modules, you need to do so completely. Python and
the import stuff won't really do anything to help you (though it won't go
out of its way to hinder you, either).

Something like:

def remove_module(name):
for mod in sys.modules.keys():
if mod == name or name.startswith(name + "."):
del sys.modules[mod]

Then remove_module("apkg") may work for you. (Code above not tested at all,
not even kinda)

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


Re: Order of addresses returned by socket.gethostbyname_ex()

2011-08-22 Thread Cameron Simpson
On 22Aug2011 04:29, Tomas Lid�n  wrote:
| On 22 Aug, 12:06, Cameron Simpson  wrote:
| > It would not surprise me if the order was related to the order a scan of
| > the system interfaces yields information, and I would imagine that may
| > be influenced by the order in which the interfaces were initialised.
| >
| > So getting the LAN first may merely be fortuitous.
| > I wouldn't rely on it, especially if interfaces come and go.
| 
| We did try to disable/enable the interfaces in different orders, but
| always got the same return order from gethostbyname_ex(). So it looked
| pretty stable (on that specific OS). There's been some testing on
| Linux as well, but since this should be used in a general cross
| platform tool we wanted to check the exact behaviour of the method.
| 
| > What if you queried your routing table instead? Usually there's just one
| > default route, and hopefully it would be configured to use the "best"
| > interface.
| 
| Hmm... perhaps. How would you do that?

On UNIX systems (and, I think, also Windows systems) you'd run the command:

  netstat -rn

or use the "route" command. (This avoids using operating system calls
directly - the command will know what to do itself.)

I seem to recall that Windows has the netstat command as well (possibly
because, IIRC, they pulled in the BSD UNIX IP stack for their first IP
implementation).

Anyway, by popening the netstat command you can pull the "default" (or
"0.0.0.0") route from its output. Then see if the interface listed there
corresponds to what you need to know.

Cheers,
-- 
Cameron Simpson  DoD#743
http://www.cskk.ezoshosting.com/cs/

Then again, it's probably really because your average Australian doesn't give
a shit about anything that doesn't increase the price of beer.
- Murray Chapman 
-- 
http://mail.python.org/mailman/listinfo/python-list


Thoughts about documentation for non-python resource files

2011-08-22 Thread Tim Johnson
I'm looking for comments and/or URLs to discussions on this topic.

I use my own MVC system. A component of this system generates
documentation from python docstrings. Of course this system also
comprises many non-python filetypes : css, html, txt, js, xml etc.
Views, which are txt or html files can be handled by my system's
built-in relationships between controllers and views.

For other non-python files which are not found directly to
controllers, css, js, html, xml all support comments and
documentation can be (and is) build into the comments.

Before I proceded and 'roll my own' protocol for generating
documentations for these above-mention filetypes, I'd like to know
if there is any such protocols being used already.

Example : A pythonic way to generate documentations on a javascript
file as well as its functions, objects etc.

Any comments welcome.

thanks
-- 
Tim 
tim at johnsons-web dot com or akwebsoft dot com
http://www.akwebsoft.com
-- 
http://mail.python.org/mailman/listinfo/python-list


Help on instance of closeable_response in module Mechanize

2011-08-22 Thread Yingjie Lin
Hi Python users,

I have a question about the instance of closeable_response in module Mechanize.

from mechanize import ParseResponse, urlopen
url = "http://wwwsearch.sourceforge.net/mechanize/example.html";
r = urlopen(url)
forms = ParseResponse(r, backwards_compat=False)
html_lines = r.read()

If I call ParseResponse() before r.read(), then lforms would be a list 
containing one form 
instance, and html_lines would be an empty string. If I call r.read() first, 
then html_lines 
would be the HTML source code of the page, but forms would be an empty list. 

Therefore, I have to open the url twice, once for each function, like this:

r = urlopen(url)
forms = ParseResponse(r, backwards_compat=False)
r = urlopen(url)
html_lines = r.read()

I believe this shouldn't be necessary. What is the proper way of doing it? 
Thank you.

- Yingjie







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


Re: Help on instance of closeable_response in module Mechanize

2011-08-22 Thread Chris Rebert
On Mon, Aug 22, 2011 at 5:17 PM, Yingjie Lin  wrote:
> Hi Python users,
>
> I have a question about the instance of closeable_response in module 
> Mechanize.
>
>        from mechanize import ParseResponse, urlopen
>        url = "http://wwwsearch.sourceforge.net/mechanize/example.html";
>        r = urlopen(url)
>        forms = ParseResponse(r, backwards_compat=False)
>        html_lines = r.read()
>
> If I call ParseResponse() before r.read(), then lforms would be a list 
> containing one form
> instance, and html_lines would be an empty string. If I call r.read() first, 
> then html_lines
> would be the HTML source code of the page, but forms would be an empty list.
>
> Therefore, I have to open the url twice, once for each function, like this:
>
>        r = urlopen(url)
>        forms = ParseResponse(r, backwards_compat=False)
>        r = urlopen(url)
>        html_lines = r.read()
>
> I believe this shouldn't be necessary. What is the proper way of doing it? 
> Thank you.

Untested speculation:

from StringIO import StringIO
r = urlopen(url)
html = r.read()
s = StringIO(html)
forms = ParseResponse(s, backwards_compat=False)

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


Re: Adding modified methods from another class without subclassing

2011-08-22 Thread John O'Hagan
On Mon, 22 Aug 2011 23:08:50 +1000
John O'Hagan  wrote:

> On Mon, 22 Aug 2011 15:27:36 +1000
> Steven D'Aprano  wrote:
[..]
> > Looks like a call for (semi-)automatic delegation!
> > 
> > Try something like this:
> > 
> > 
> > # Untested
> > class MySeq(object):
> > methods_to_delegate = ('__getitem__', '__len__', ...)
> > pitches = ...  # make sure pitches is defined
> > def __getattr__(self, name):
> > if name in self.__class__.methods_to_delegate:
> > return getattr(self.pitches, name)
> > return super(MySeq, object).__getattr__(self, name)
> > # will likely raise AttributeError
> 
[..]
> 
> However, I don't understand what the super call is doing. If the method isn't 
> delegated, shouldn't it just fall back to getattr(self, name)?

On reading the __getattr__ docs properly, I see that AttributeError is what 
should generally happen.
 
> Thanks and regards,
> 
> John
> 

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


Re: Adding modified methods from another class without subclassing

2011-08-22 Thread Steven D'Aprano
On Tue, 23 Aug 2011 10:55 am John O'Hagan wrote:

>> > # Untested
>> > class MySeq(object):
>> > methods_to_delegate = ('__getitem__', '__len__', ...)
>> > pitches = ...  # make sure pitches is defined
>> > def __getattr__(self, name):
>> > if name in self.__class__.methods_to_delegate:
>> > return getattr(self.pitches, name)
>> > return super(MySeq, object).__getattr__(self, name)
>> > # will likely raise AttributeError
>> 
> [..]
>> 
>> However, I don't understand what the super call is doing. If the method
>> isn't delegated, shouldn't it just fall back to getattr(self, name)?
> 
> On reading the __getattr__ docs properly, I see that AttributeError is
> what should generally happen.

Which is what the call to super will accomplish, but if the behaviour ever
changes (including the error message given), you won't have to change your
class.



-- 
Steven

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


Re: CGI input: Filter dict.update() unwanted variables

2011-08-22 Thread Gnarlodious
On Aug 22, 9:39 am, Miki Tebeka wrote:

> HTH

Yes it helps, thank you!

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


Re: Error when deleting and reimporting subpackages

2011-08-22 Thread John Nagle

On 8/22/2011 11:51 AM, Matthew Brett wrote:

Hi,

I recently ran into this behavior:


import sys import apkg.subpkg del sys.modules['apkg'] import
apkg.subpkg as subpkg

Traceback (most recent call last): File "", line 1,
in AttributeError: 'module' object has no attribute 'subpkg'

where 'apkg' and 'subpkg' comprise empty __init__.py files to
simplify the example.

It appears then, that importing a subpackage, then deleting the
containing package from sys.modules, orphans the subpackage in an
unfixable state.

I ran into this because the nose testing framework does exactly this
kind of thing when loading test modules, causing some very confusing
errors and failures.

Is this behavior expected?


   It's undefined behavior.  You're dealing with CPython implementation
semantics, not Python language semantics.

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


how to write the lambda expression in my tkinter ?

2011-08-22 Thread 守株待兔
from Tkinter import *

 fields = 'Name', 'Job', 'Pay'

 

 def fetch(event,entries):

for entry in entries:

print 'Input => "%s"' % entry.get()   # get text

print  event.widget 

 

 

 def makeform(root, fields):

entries = []

for field in fields:

row = Frame(root)   # make a new row

lab = Label(row, width=5, text=field)   # add label, entry

ent = Entry(row)

row.pack(side=TOP, fill=X)  # pack row on top

lab.pack(side=LEFT)

ent.pack(side=RIGHT, expand=YES, fill=X)# grow horizontal

entries.append(ent)

return entries

 

 if __name__ == '__main__':

root = Tk()

ents = makeform(root, fields)

root.bind('', lambda event,entries=ents: fetch(event,entries))  
 

Button(root, text='Fetch', command= lambda 
event:fetch(event,entries)).pack(side=LEFT)


root.mainloop()
when you run it ,press enter ,you can get the value in the entry;when you  
click the  Button(Fetch),there is a wrong output ,i can't revise it,i know it 
is the  26  can't run ,how to fix it ?

Button(root, text='Fetch', command= lambda 
event:fetch(event,entries)).pack(side=LEFT)-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Word Perfect integration

2011-08-22 Thread Michael Torrie
On 08/18/2011 01:24 PM, Ethan Furman wrote:
> Alec Taylor wrote:
>> wow, people still use WordPerfect?
> 
> Them's fightin' words right there!  :)
> 
> Yes, we still use Word Perfect, and will as long as it is available. 
> The ability to see the codes in use (bold, margins, columns, etc) has so 
> far been unequaled in anything else I have looked at.

It's too bad LaTeX is so complicated and LyX just never quite worked out
for me.
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: how to write the lambda expression in my tkinter ?

2011-08-22 Thread Chris Rebert
2011/8/22 守株待兔 <1248283...@qq.com>:
> from Tkinter import *
> fields = 'Name', 'Job', 'Pay'
>
> def fetch(event,entries):
>    for entry in entries:
>    print 'Input => "%s"' % entry.get()   # get text
>    print  event.widget
>
>
> def makeform(root, fields):
>    entries = []
>    for field in fields:
>    row = Frame(root)   # make a new row
>    lab = Label(row, width=5, text=field)   # add label, entry
>    ent = Entry(row)
>    row.pack(side=TOP, fill=X)  # pack row on top
>    lab.pack(side=LEFT)
>    ent.pack(side=RIGHT, expand=YES, fill=X)# grow horizontal
>    entries.append(ent)
>    return entries
>
> if __name__ == '__main__':
>    root = Tk()
>    ents = makeform(root, fields)
>    root.bind('', lambda event,entries=ents: fetch(event,entries))
>    Button(root, text='Fetch', command= lambda event:fetch(event,entries)).pack(side=LEFT)
>    root.mainloop()
>
> when you run it ,press enter ,you can get the value in the entry;when you
> click the  Button(Fetch),there is a wrong output ,i can't revise it,i know
> it is the  26  can't run ,how to fix it ?
>
> Button(root, text='Fetch', command= lambda event:fetch(event,entries)).pack(side=LEFT)

Problem 1: `entries` is undefined within the scope of the lambda; it's
not a parameter of the lambda, nor is it defined in any outer scope
that encloses the lambda. This will lead to a NameError. `ents` is /
would be within scope however...(*wink*)

Problem 2: Based on quick experimentation, Tkinter does not pass
`command` any arguments, yet your lambda has 1 required argument
(namely, `event`). This will cause a run-time error when the lambda is
called.

That should be enough to get you started.

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