Re: isinstance(False, int)

2010-03-08 Thread Bruno Desthuilliers

Rolando Espinoza La Fuente a écrit :

On Fri, Mar 5, 2010 at 2:32 PM, mk  wrote:

Arnaud Delobelle wrote:


1 == True

True

0 == False

True

So what's your question?

Well nothing I'm just kind of bewildered: I'd expect smth like that in Perl,
but not in Python.. Although I can understand the rationale after skimming
PEP 285, I still don't like it very much.



So, the pythonic way to check for True/False should be:


1 is True

False


0 is False

False

instead of ==, right?


Nope. The pythonic way is to check for truth value - not for True or 
False -, and to only use the identity test when wanting to test for 
identity.


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


libc Sleep api performs a busy waiting

2010-03-08 Thread Mahesh
Hi,

 I am having a problem while using sleep function from libc , the
thread in which i am calling it is getting struck and not allowing
other threads to execute.  Here is a simple code that i am trying to
exeute

import threading
import time
import dl


def dummy1():
a=dl.open('/lib/libc.so.6')
print "in thread 1 Start"
a.call('sleep',2)
#time.sleep(2)
print "in thread 1 End"

def dummy2():
print "in thread 2 Start"
time.sleep(1)
print "in thread 2 End"
newThread1=threading.Thread(None,dummy1)
newThread2=threading.Thread(None,dummy2)
newThread1.start()
newThread2.start()

print "in main"



The out put of this program is  (In this case thread 1 even though i
am calling a sleep function its not allowing other threads to execute,
other threads execute only after the completion of first thread)

in thread 1 Start
in thread 1 End
in thread 2 Start
in main
in thread 2 End


where as if i use time.sleep instead of a.call(sleep) the out put is
(which i guess is right behaviour, because it start the threads and
suspends them because the have sleep , and continue executing the main
thread)
in thread 1 Start
in thread 2 Start
in main
in thread 2 End
in thread 1 End


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


Re: The C language like the Python, by the Python and for the Python--- PythoidC

2010-03-08 Thread CHEN Guang
Dear Stefan, 
Thanks a lot for your interest in PythoidC.
Yes, PythoidC takes regular expression to parse C header files, 
but there is no danger at all, because, the parsed result is only used for 
introspecting and auto-completion, has noting to do with syntax converting and 
compiling.
I just improved PythoidC to a satisfying version for me, up to now, all the 
projects
with PythoidC are experimental or educational.
PythoidC is mainly used to deal with simple but time consuming tasks in Python 
project, 
as you see, PythoidC file can be imported the same any Python files.
Oliver ( ChenGuang )
?
CHEN Guang, 08.03.2010 06:08:
> Hi, if you are interested in C-Python mixed programming, please take a look 
> at:
> http://pythoidc.googlecode.com
> PythoidC is the C language like the Python, by the Python and for the Python

It looks a bit dangerous to me to parse C header files only with regular 
expressions. For Cython, some people seem to be using gcc-xml instead, and 
some have reported that clang is really nice to play with. However, none of 
those has made its way into Cython yet, mostly due to the dependency it 
would add. I don't know if tcc (which you seem to depend on) has anything 
comparable, but I doubt it.

Just out of curiosity, is this syntax converter actually used for any real 
projects?

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


Re: running a program on many processors

2010-03-08 Thread Processor-Dev1l
On Mar 8, 1:18 am, Paweł Banyś 
wrote:
> Hello,
>
> I have already read about Python and multiprocessing which allows using
> many processors. The idea is to split a program into separate tasks and
> run each of them on a separate processor. However I want to run a Python
> program doing a single simple task on many processors so that their
> cumulative power is available to the program as if there was one huge
> CPU instead of many separate ones. Is it possible? How can it be achieved?
>
> Best regards,
>
> Paweł

I can suggest you to try some .NET language (like c#, Boo <-- python-
like, or maybe even IronPython). Reason is that .NET languages do
analyze the code and split them into logical parts that run across
threads.
-- 
http://mail.python.org/mailman/listinfo/python-list


libc Sleep api performs a busy waiting

2010-03-08 Thread Joe Fox
Hi ,

 I am having a problem while using sleep function from libc , the
thread in which i am calling it is getting struck and not allowing
other threads to execute.  Here is a simple code that i am trying to
exeute

import threading
import time
import dl

def dummy1():
a=dl.open('/lib/libc.so.6')
print "in thread 1 Start"
a.call('sleep',2)
#time.sleep(2)
print "in thread 1 End"

def dummy2():
print "in thread 2 Start"
time.sleep(1)
print "in thread 2 End"
newThread1=threading.Thread(None,dummy1)
newThread2=threading.Thread(None,dummy2)
newThread1.start()
newThread2.start()

print "in main"

The out put of this program is  (In this case thread 1 even though i
am calling a sleep function its not allowing other threads to execute,
other threads execute only after the completion of first thread)

in thread 1 Start
in thread 1 End
in thread 2 Start
in main
in thread 2 End
where as if i use time.sleep instead of a.call(sleep) the out put is
(which i guess is right behaviour, because it start the threads and
suspends them because the have sleep , and continue executing the main
thread)
in thread 1 Start
in thread 2 Start
in main
in thread 2 End
in thread 1 End


Can any of you point me how to over come this.?
Thanks
Mahesh
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Reverse engineering CRC?

2010-03-08 Thread Dave Angel

Steven D'Aprano wrote:

On Mon, 08 Mar 2010 16:09:12 +1300, Gregory Ewing wrote:

  

Given some known data/crc pairs, how feasible is it to figure out the
polynomial being used to generate the crc?



Google is your friend:

http://www.woodmann.com/fravia/crctut1.htm

  
That page was interesting to read, especially since I've implemented the 
three algorithms - CRC16, CRC32, and the reversed version of CRC16, all 
in the long-distant past.


However, if there's anything in there about how to derive the polynomial 
algorithm from (a few) samples I missed it entirely. Instead, what it 
calls reverse engineering is figuring out how to modify a message to 
force it to have a desired CRC value (when the CRC polynomial is already 
known).


DaveA

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


Re: Best practice way to detect internet connectivity under Python 2.6/Windows

2010-03-08 Thread Tim Golden

On 08/03/2010 02:41, pyt...@bdurham.com wrote:

Is the best pratice way to detect internet connectivity under
Windows (using Python 2.6) simply to attempt to access a known
internet website using urllib or urlib2 wrapped in a try/except
construct?


Well, in theory you could use the Internet API:

  http://msdn.microsoft.com/en-us/library/aa383996%28VS.85%29.aspx

which is exposed via the win32inet module:

  http://timgolden.me.uk/pywin32-docs/win32inet.html

but I'd just do a socket connection to port 80 (or some known port
on some known server)  with a reasonable timeout.

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


Re: Curiosity stirkes me on 'import this'.

2010-03-08 Thread Daniel Fetchinson
> I took a look at the 'this' module to see where the file is stored. This is
> probably old news to some people, but was new to me.
>
> print this.s
> Gur Mra bs Clguba, ol Gvz Crgref
>
> Ornhgvshy vf orggre guna htyl.
> Rkcyvpvg vf orggre guna vzcyvpvg.
> Fvzcyr vf orggre guna pbzcyrk.
> Pbzcyrk vf orggre guna pbzcyvpngrq.
> Syng vf orggre guna arfgrq.
> Fcnefr vf orggre guna qrafr.
> Ernqnovyvgl pbhagf.
> Fcrpvny pnfrf nera'g fcrpvny rabhtu gb oernx gur ehyrf.
> Nygubhtu cenpgvpnyvgl orngf chevgl.
> Reebef fubhyq arire cnff fvyragyl.
> Hayrff rkcyvpvgyl fvyraprq.
> Va gur snpr bs nzovthvgl, ershfr gur grzcgngvba gb thrff.
> Gurer fubhyq or bar-- naq cersrenoyl bayl bar --boivbhf jnl gb qb vg.
> Nygubhtu gung jnl znl abg or boivbhf ng svefg hayrff lbh'er Qhgpu.
> Abj vf orggre guna arire.
> Nygubhtu arire vf bsgra orggre guna *evtug* abj.
> Vs gur vzcyrzragngvba vf uneq gb rkcynva, vg'f n onq vqrn.
> Vs gur vzcyrzragngvba vf rnfl gb rkcynva, vg znl or n tbbq vqrn.
> Anzrfcnprf ner bar ubaxvat terng vqrn -- yrg'f qb zber bs gubfr!
>
> And some other attributes in 'this' module as well, that decodes the string.
> I'm curious. Was this encoded purely for fun?

Yes.

Cheers,
Daniel




-- 
Psss, psss, put it down! - http://www.cafepress.com/putitdown
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Reverse engineering CRC?

2010-03-08 Thread Gregory Ewing

Steven D'Aprano wrote:

Can you just ask the application developer what CRC is being used? Or 
look at the source code? Disassemble the binary?


There's no source, and the binary is enormous. I could ask,
but I wouldn't hold out much hope of them being willing to
tell me.


it appears that the crc size may be at least
24 bits, so just trying all possible polynomials probably isn't doable.


"At least"? Can't you tell by looking at them?


It's not entirely clear exactly which bytes are part of the
CRC. There are 3 adjacent bytes in the header of the file
that change when I modify the contents, which led me to
think it was a 24-bit CRC. But I now believe that one of
them is not part of the CRC, and it's actually 16 bits.

Using pycrc, I've now tried all possible 16-bit polynomials,
with various combinations of bit and byte reversal, but I
haven't found one that works consistently, so I'm wondering
whether it's using some non-standard algorithm.

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


Re: negative "counts" in collections.Counter?

2010-03-08 Thread Vlastimil Brom
2010/3/8 Raymond Hettinger :
> On Mar 7, 5:46 pm, Steven D'Aprano  cybersource.com.au> wrote:
>> Given that Counter supports negative counts, it looks to me that the
>> behaviour of __add__ and __sub__ is fundamentally flawed. You should
>> raise a bug report (feature enhancement) on the bug tracker.
>
> It isn't a bug.  I designed it that way.
> There were several possible design choices,
> each benefitting different use cases.
>...
> One possible choice (the one preferred by the OP) was to
> has addition and subtraction be straight adds and subtracts
> without respect to sign and to not support __and__ and __or__.
> Straight addition was already supported via the update() method.
> But no direct support was provided for straight subtractions
> that leave negative values.  Sorry about that.
>
> Instead the choice was to implement the four methods as
> multiset operations.  As such, they need to correspond
> to regular set operations.  For example, with sets:
>
>...
> Hopes this gives you some insight into the design choices.
>
>
> Raymond Hettinger
>
Thank you very much for the exhaustive explanation Raymond!
I very much suspected, there would be some exact reasoning behind it,
as these negative counts are explicitly handled in the code of these
methods.
I just happened to expect the straightforward addition and subtraction
and possibly the zero truncation or an exception in incompatible cases
(like elements() with floats, currently). This way also the negative
part would be available and the truncation possible.
I am by far not able to follow all of the mathematical background, but
even for zero-truncating multiset, I would expect the truncation on
input rather than on output of some operations. E.g. the following
seems surprising to me in context of the discarded negatives by
addition or subtraction:

>>> c1=collections.Counter({'a': -11, 'b': -8})
>>> c2=collections.Counter({'a': 3})
>>> c2-c1
Counter({'a': 14, 'b': 8})
>>>

Probably a kind of negative_update()  or some better named method will
be handy, like the one you supplied or simply the current module code
without the newcount > 0: ... condition. Or would it be an option to
have a keyword argument like zero_truncate=False which would influence
this behaviour? Or is some other method than elements() incompatible
with negative counts?
(I actually can imagine comparing negative counts on both sides. e.g.
in a secondary comparison of two wordlists with specific removed items
- comparing to the "master" list.)
Additionally, were issubset and issuperset considered for this
interface (not sure whether symmetric_difference would be applicable)?

Anyway, I recognise, that I can easily use a custom class for these
tasks, if these usecases are rare or non-standard for this general
collection object.
Thanks again,
  vbr
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Curiosity stirkes me on 'import this'.

2010-03-08 Thread Ben Finney
Daniel Fetchinson  writes:

> > I'm curious. Was this encoded purely for fun?
>
> Yes.

For some more fun, try measuring the ‘this’ module source code against
the principles in the Zen.

-- 
 \ “Leave nothing to chance. Overlook nothing. Combine |
  `\  contradictory observations. Allow yourself enough time.” |
_o__) —Hippocrates |
Ben Finney
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: negative "counts" in collections.Counter?

2010-03-08 Thread Steven D'Aprano
On Sun, 07 Mar 2010 22:31:00 -0800, Raymond Hettinger wrote:

> On Mar 7, 5:46 pm, Steven D'Aprano  cybersource.com.au> wrote:
>> Given that Counter supports negative counts, it looks to me that the
>> behaviour of __add__ and __sub__ is fundamentally flawed. You should
>> raise a bug report (feature enhancement) on the bug tracker.
> 
> It isn't a bug.  I designed it that way. There were several possible
> design choices, each benefitting different use cases.

Thanks for the explanation Raymond. A few comments follow:


> FWIW, here is the reasoning behind the design.
> 
> The basic approach to Counter() is to be a dict subclass that supplies
> zero for missing values.   This approach places almost no restrictions
> on what can be stored in it. You can store floats, decimals, fractions,
> etc. Numbers can be positive, negative, or zero.

Another way of using default values in a dict. That's five that I know 
of: dict.get, dict.setdefault, dict.pop, collections.defaultdict, and 
collections.Counter. And the Perl people criticise Python for having 
"only one way to do it" *wink*

(That's not meant as a criticism, merely an observation.)


[...]
> One possible choice (the one preferred by the OP) was to has addition
> and subtraction be straight adds and subtracts without respect to sign
> and to not support __and__ and __or__. Straight addition was already
> supported via the update() method. But no direct support was provided
> for straight subtractions that leave negative values.  Sorry about that.

Would you consider a feature enhancement adding an additional method, 
analogous to update(), to perform subtractions? I recognise that it's 
easy to subclass and do it yourself, but there does seem to be some 
demand for it, and it is an obvious feature given that Counter does 
support negative counts.


> Instead the choice was to implement the four methods as multiset
> operations.  As such, they need to correspond to regular set operations.

Personally, I think the behaviour of + and - would be far less surprising 
if the class was called Multiset. Intuitively, one would expect counters 
to be limited to ints, and to support negative counts when adding and 
subtracting. In hindsight, do you think that Multiset would have been a 
better name?



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


Re: A "scopeguard" for Python

2010-03-08 Thread Steve Holden
Alf P. Steinbach wrote:
> * Gabriel Genellina:
>> En Thu, 04 Mar 2010 20:52:04 -0300, Alf P. Steinbach 
>> escribió:
>>
>>> Sorry, as with the places noted above, I can't understand what you're
>>> trying to say here.
>>
>> Regarding your posts, neither can I. All the time. Sorry, deciphering
>> your posts would force me to spend much more time than available and I
>> can't afford that - so I'm blocking your messages from now on.
> 
> 
> You could just ask if there was anything you didn't understand.
> 
> Even with as little you quote readers can see my approach to that
> problem: asking.
> 
> But your response, both the out of context quoting and your comment,
> seems solely designed to convey a negative impression instead of
> addressing any technical issue.
> 
This isn't an isolated case, Alf. Physician, heal thyself.

regards
 Steve
-- 
Steve Holden   +1 571 484 6266   +1 800 494 3119
PyCon is coming! Atlanta, Feb 2010  http://us.pycon.org/
Holden Web LLC http://www.holdenweb.com/
UPCOMING EVENTS:http://holdenweb.eventbrite.com/

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


Re: libc Sleep api performs a busy waiting

2010-03-08 Thread Steve Holden
Mahesh wrote:
> Hi,
> 
>  I am having a problem while using sleep function from libc , the
> thread in which i am calling it is getting struck and not allowing
> other threads to execute.  Here is a simple code that i am trying to
> exeute
> 
> import threading
> import time
> import dl
> 
> 
> def dummy1():
> a=dl.open('/lib/libc.so.6')
> print "in thread 1 Start"
> a.call('sleep',2)
> #time.sleep(2)
> print "in thread 1 End"
> 
> def dummy2():
> print "in thread 2 Start"
> time.sleep(1)
> print "in thread 2 End"
> newThread1=threading.Thread(None,dummy1)
> newThread2=threading.Thread(None,dummy2)
> newThread1.start()
> newThread2.start()
> 
> print "in main"
> 
> 
> 
> The out put of this program is  (In this case thread 1 even though i
> am calling a sleep function its not allowing other threads to execute,
> other threads execute only after the completion of first thread)
> 
> in thread 1 Start
> in thread 1 End
> in thread 2 Start
> in main
> in thread 2 End
> 
> 
> where as if i use time.sleep instead of a.call(sleep) the out put is
> (which i guess is right behaviour, because it start the threads and
> suspends them because the have sleep , and continue executing the main
> thread)
> in thread 1 Start
> in thread 2 Start
> in main
> in thread 2 End
> in thread 1 End
> 
> 
Why not just use the time module's sleep function? Unlike the libc code
it releases Python's GIL (global interpreter lock), thus allowing other
threads to run.

regards
 Steve
-- 
Steve Holden   +1 571 484 6266   +1 800 494 3119
PyCon is coming! Atlanta, Feb 2010  http://us.pycon.org/
Holden Web LLC http://www.holdenweb.com/
UPCOMING EVENTS:http://holdenweb.eventbrite.com/

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


Re: Conditional based on whether or not a module is being used

2010-03-08 Thread Jean-Michel Pichavant

Pete Emerson wrote:

On Mar 5, 1:14 pm, Chris Rebert  wrote:
  

On Fri, Mar 5, 2010 at 12:25 PM, Pete Emerson  wrote:


On Fri, Mar 5, 2010 at 12:17 PM, Chris Rebert  wrote:
  

On 3/5/10, Pete Emerson  wrote:


In a module, how do I create a conditional that will do something
based on whether or not another module has been loaded?
  




If someone is using foo module, I want to take advantage of its
features and use it in foobar, otherwise, I want to do something else.
In other words, I don't want to create a dependency of foobar on foo.
  
My failed search for solving this makes me wonder if I'm approaching

this all wrong.
  

Just try importing foo, and then catch the exception if it's not installed.





Except I want to use the module only if the main program is using it
too, not just if it's available for use. I think that I found a way in
my follow-up post to my own message, but not sure it's the best way or
conventional.
  

What is your use case for this behavior exactly? You've piqued my curiosity.

Cheers,
Chris
--http://blog.rebertia.com



I have written my first module called "logger" that logs to syslog via
the syslog module but also allows for logging to STDOUT in debug mode
at multiple levels (to increase verbosity depending on one's need), or
both. I've looked at the logging module and while it might suit my
needs, it's overkill for me right now (I'm still *very* much a python
newbie).

I want to write other modules, and my thinking is that it makes sense
for those modules to use the "logger" module to do the logging, if and
only if the parent using the other modules is also using the logger
module.

In other words, I don't want to force someone to use the "logger"
module just so they can use my other modules, even if the "logger"
module is installed ... but I also want to take advantage of it if I'm
using it.

Now that I've written that, I'm not sure that makes a whole lot of
sense. It seems like I could say, "hey, this person has the 'logger'
module available, let's use it!".

Thoughts?
  

The logging module is not overkill, use it.
It can be simple if you want it to be, and can be more complex if you 
need advanced logging features.


All the "what if the application is not using my logger module" is dealt 
with by the logging module.
And I'm not its designer in any way, so my advice is completely 
objective :-)


It's definitely worth spending some time ramping up with it.

JM

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


Re: Reverse engineering CRC?

2010-03-08 Thread Dave Angel

Gregory Ewing wrote:

Steven D'Aprano wrote:

Can you just ask the application developer what CRC is being used? Or 
look at the source code? Disassemble the binary?


There's no source, and the binary is enormous. I could ask,
but I wouldn't hold out much hope of them being willing to
tell me.


it appears that the crc size may be at least
24 bits, so just trying all possible polynomials probably isn't doable.


"At least"? Can't you tell by looking at them?


It's not entirely clear exactly which bytes are part of the
CRC. There are 3 adjacent bytes in the header of the file
that change when I modify the contents, which led me to
think it was a 24-bit CRC. But I now believe that one of
them is not part of the CRC, and it's actually 16 bits.

Using pycrc, I've now tried all possible 16-bit polynomials,
with various combinations of bit and byte reversal, but I
haven't found one that works consistently, so I'm wondering
whether it's using some non-standard algorithm.

Or even some other standard algorithm.  If you know so little about the 
value, how do you even know it's a CRC ?  Could it be a ones-complement 
sum, such as used in Ethernet?


Is the problem really worth it?  The possibilities are practically 
unbounded.  And if the developer is really determined to make it 
difficult, they could be doing multiple passes over the data, in which 
case probably disassembly (or subtle debug tracing) may be your best bet.


DaveA

Dave

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


Re: Import problem

2010-03-08 Thread Jean-Michel Pichavant

Johny wrote:

I have this directory structure

C:
  \A
 __init__.py
 amodule.py

 \B
  __init__.py
  bmodule.py

   \D
__init__.py
dmodule.py

and  I want to import  bmodule.py
C:\>cd \

C:\>python
Python 2.5 (r25:51908, Sep 19 2006, 09:52:17) [MSC v.1310 32 bit
(Intel)] on win
32
Type "help", "copyright", "credits" or "license" for more information.
  

from A.B import  bmodule


I am bmodule
  
C:\>


so far so good. Now I would like to import bmodule but if the current
directory is \D subdirectory.

C:> cd \A\B\D
C:\A\B\D>
C:\A\B\D>python
Python 2.5 (r25:51908, Sep 19 2006, 09:52:17) [MSC v.1310 32 bit
(Intel)] on win
32
Type "help", "copyright", "credits" or "license" for more information.
  

import sys
sys.path.append('C:\\A')
from A.B import bmodule


Traceback (most recent call last):
  File "", line 1, in 
ImportError: No module named A.B

C:\>

so I can not import a module from the parent directory? Or where did I
make an error?
Thanks for help

L.
  

try

import sys
sys.path.append('C:\\')
from A.B import bmodule


JM

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


What to do if anything bites.

2010-03-08 Thread Google Adsense
What to do if anything bites.

Check our bites treatment at

http://108ambulance.blogspot.com/2010/03/108-ambulance-home-page.html
-- 
http://mail.python.org/mailman/listinfo/python-list


ANNOUNCE: Major Feature Release - NHI1-0.7, PLMK-1.6 and libmsgque-4.5

2010-03-08 Thread Andreas Otto
Dear User,


ANNOUNCE:Major Feature Release


  libmsgque: Application-Server-Toolkit for
 C, C++, JAVA, C#, TCL, PERL, PYTHON, VB.NET
  PLMK:  Programming-Language-Microkernel
  NHI1:  Non-Human-Intelligence #1


SUMMARY
===

This is a 'major-feature-release' to add 'longtime-transaction' support.

A 'longtime-transaction' is a transaction with a possible infinite time
period between the start and the end. A 'longtime-transaction' is always
a 'persistent-transaction' too. The 'persistent' support is available as
an in-memory solution on the 'application-server' or in an additional
'bidirectional-filter' available for the next release.

A typical scenario for a 'longtime-transaction' is:

* A user with a client connect to the server and start a
'longtime-transaction' job. After a while the user shutdown the client
and leave the office. At the next morning the user start the client
again and the results are send from the server to the client.
* On a space mission the time-overhead between transaction start and end
is large. It is usually not possible to keep the
client-server-connection open all the time.
* On a military action-system the enemy try to interrupted the
client-server-connection to disable data communication.

To use a 'longtime-transaction' the results are send to a
'client-service' registered with MqServiceCreate. This service and
addtional data (I call this data 'local-context-data') have to be added
to the 'longterm-transaction' package to be available in the result. On the
'link-target' the 'local-context-data' is initial stripped from the
'data-package' and is later added to the results again.

The 'local-context-data' is added to the 'data-package' with
MqSendT_START and MqSendT_END.
The 'local-context-data' is read from the 'data-package' with
MqReadT_START and MqReadT_END.


LINKS
=

  libmsgque:
   > http://nhi1.berlios.de/theLink/index.htm
  NHI1:
   > http://nhi1.berlios.de/
  DOWNLOAD:
   > http://developer.berlios.de/projects/nhi1/



mfg

  Andreas Otto (aotto1968)
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: A "scopeguard" for Python

2010-03-08 Thread sstein...@gmail.com
On Mar 8, 2010, at 7:28 AM, Steve Holden wrote:

>> 
>> But your response, both the out of context quoting and your comment,
>> seems solely designed to convey a negative impression instead of
>> addressing any technical issue.
>> 
> This isn't an isolated case, Alf. Physician, heal thyself.

As far as I can tell, this happens on every thread Alf is involved in at one 
point or another.  I'm sure it's "everyone else."

S

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


Re: NoSQL Movement?

2010-03-08 Thread Xah Lee
many people mentioned scalibility... though i think it is fruitful to
talk about at what size is the NoSQL databases offer better
scalability than SQL databases.

For example, consider, if you are within world's top 100th user of
database in terms of database size, such as Google, then it may be
that the off-the-shelf tools may be limiting. But how many users
really have such massive size of data?

note that google's need for database today isn't just a seach engine.
It's db size for google search is probably larger than all the rest of
search engine company's sizes combined. Plus, there's youtube (vid
hosting), gmail, google code (source code hosting), google blog, orkut
(social networking), picasa (photo hosting), etc, each are all ranked
within top 5 or so with respective competitors in terms of number of
accounts... so, google's datasize is probably number one among the
world's user of databases, probably double or triple than the second
user with the most large datasize. At that point, it seems logical
that they need their own db, relational or not.

  Xah
∑ http://xahlee.org/

☄

On Mar 4, 10:35 pm, John Nagle  wrote:
> Xah Lee wrote:
> > recently i wrote a blog article on The NoSQL Movement
> > athttp://xahlee.org/comp/nosql.html
>
> > i'd like to post it somewhere public to solicit opinions, but in the
> > 20 min or so, i couldn't find a proper newsgroup, nor private list
> > that my somewhat anti-NoSQL Movement article is fitting.
>
>     Too much rant, not enough information.
>
>     There is an argument against using full relational databases for
> some large-scale applications, ones where the database is spread over
> many machines.  If the database can be organized so that each transaction
> only needs to talk to one database machine, the locking problems become
> much simpler.  That's what BigTable is really about.
>
>     For many web applications, each user has more or less their own data,
> and most database activity is related to a single user.  Such
> applications can easily be scaled up with a system that doesn't
> have inter-user links.  There can still be inter-user references,
> but without a consistency guarantee.  They may lead to dead data,
> like Unix/Linux symbolic links.  This is a mechanism adequate
> for most "social networking" sites.
>
>     There are also some "consistent-eventually" systems, where a query
> can see old data.  For non-critical applications, those can be
> very useful.  This isn't a SQL/NoSQL thing; MySQL asynchronous
> replication is a "consistent-eventually" system.  Wikipedia uses
> that for the "special" pages which require database lookups.
>
>     If you allow general joins across any tables, you have to have all
> the very elaborate interlocking mechanisms of a distributed database.
> The serious database systems (MySQL Cluster and Oracle, for example)
> do offer that, but there are usually
> substantial complexity penalties, and the databases have to be carefully
> organized to avoid excessive cross-machine locking.  If you don't need
> general joins, a system which doesn't support them is far simpler.
>
>                                         John Nagle

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


Converting HTML to PDF using Python?

2010-03-08 Thread Oltmans
Hi Python superstars,

Guys, any ideas on how to convert HTML files to PDF files? Or as an
alternative, how to convert HTML files to an image file(jpeg/png/etc)?
Ideally, the converted PDF/Image file should like exactly like the way
HTML file looks in the browser.

I really have no idea about this so I will really appreciate all the
help. Thanks in advance and kindly pardon my ignorance.

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


Re: negative "counts" in collections.Counter?

2010-03-08 Thread Chris Rebert
On Mon, Mar 8, 2010 at 4:21 AM, Steven D'Aprano <
st...@remove-this-cybersource.com.au> wrote:
> On Sun, 07 Mar 2010 22:31:00 -0800, Raymond Hettinger wrote:
>> On Mar 7, 5:46 pm, Steven D'Aprano > cybersource.com.au> wrote:
>>> Given that Counter supports negative counts, it looks to me that the
>>> behaviour of __add__ and __sub__ is fundamentally flawed. You should
>>> raise a bug report (feature enhancement) on the bug tracker.
>>
>> It isn't a bug.  I designed it that way. There were several possible
>> design choices, each benefitting different use cases.

>> Instead the choice was to implement the four methods as multiset
>> operations.  As such, they need to correspond to regular set operations.
>
> Personally, I think the behaviour of + and - would be far less surprising
> if the class was called Multiset. Intuitively, one would expect counters
> to be limited to ints, and to support negative counts when adding and
> subtracting. In hindsight, do you think that Multiset would have been a
> better name?

Of course I'm not Raymond, but I'd encourage reading the original
announcement thread where that and related issues were discussed:
http://mail.python.org/pipermail/python-list/2009-January/1189466.html

To summarize the arguments against "Multiset":
* the API is primarily dict-like, not set-like
** related to this, len() and .items() don't behave in what is arguably an
"intuitive" way for multisets
* the API allows for negative counts; sets don't normally allow negative
multiplicities

The same pretty much also goes for why "Bag" wouldn't be an appropriate
name.

Finally, it leaves the names available for when a proper Bag/Multiset is
added. :)

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


Trouble with quotes

2010-03-08 Thread Stephen Nelson-Smith
Hi,

I've written some (primitive) code to parse some apache logfies and
establish if apache has appended a session cookie to the end.  We're
finding that some browsers don't and apache doesn't just append a "-"
- it just omits it.

It's working fine, but for an edge case:

Couldn't match  192.168.1.107 - - [24/Feb/2010:20:30:44 +0100] "GET
http://sekrit.com/node/175523 HTTP/1.1" 200 -
"http://sekrit.com/search/results/"3%2B2%20course""; "Mozilla/4.0
(compatible; MSIE 6.0; Windows NT 5.1; SV1; GTB6.4)"
Couldn't match  192.168.1.107 - - [24/Feb/2010:20:31:15 +0100] "GET
http://sekrit.com/node/175521 HTTP/1.1" 200 -
"http://sekrit.com/search/results/"3%2B2%20course""; "Mozilla/4.0
(compatible; MSIE 6.0; Windows NT 5.1; SV1; GTB6.4)"
Couldn't match  192.168.1.107 - - [24/Feb/2010:20:32:07 +0100] "GET
http://sekrit.com/node/175520 HTTP/1.1" 200 -
"http://sekrit.com/search/results/"3%2B2%20course""; "Mozilla/4.0
(compatible; MSIE 6.0; Windows NT 5.1; SV1; GTB6.4)"
Couldn't match  192.168.1.107 - - [24/Feb/2010:20:32:33 +0100] "GET
http://sekrit.com/node/175522 HTTP/1.1" 200 -
"http://sekrit.com/search/results/"3%2B2%20course""; "Mozilla/4.0
(compatible; MSIE 6.0; Windows NT 5.1; SV1; GTB6.4)"
Couldn't match  192.168.1.107 - - [24/Feb/2010:20:33:01 +0100] "GET
http://sekrit.com/node/175527 HTTP/1.1" 200 -
"http://sekrit.com/search/results/"3%2B2%20course""; "Mozilla/4.0
(compatible; MSIE 6.0; Windows NT 5.1; SV1; GTB6.4)"
Couldn't match  192.168.1.107 - - [25/Feb/2010:17:01:54 +0100] "GET
http://sekrit.com/search/results/ HTTP/1.0" 200 -
"http://sekrit.com/search/results/"guideline%20grids"&page=1";
"Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1; .NET CLR
1.1.4322; .NET CLR 2.0.50727)"
Couldn't match  192.168.1.107 - - [25/Feb/2010:17:02:15 +0100] "GET
http://sekrit.com/search/results/ HTTP/1.0" 200 -
"http://sekrit.com/search/results/"guideline%20grids"&page=1";
"Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1; .NET CLR
1.1.4322; .NET CLR 2.0.50727)"

If there are " " inside the request string, my regex breaks.

Here's the code:

#!/usr/bin/env python
import re

pattern = 
r'(?P^(-|[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}(,
[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3})*){1})
(?P(\S*)) (?P(\S*))
(?P(\[[^\]]+\]))
(?P(\"([^"\\]*(?:\\.[^"\\]*)*)\")?)
(?P(\S*)) (?P(\S*))
(?P(\"([^"\\]*(?:\\.[^"\\]*)*)\")?)
(?P(\"([^"\\]*(?:\\.[^"\\]*)*)\")?)(
)?(?P(\"([^"\\]*(?:\\.[^"\\]*)*)\")?)'

regex = re.compile(pattern)

lines = 0
no_cookies = 0
unmatched = 0

for line in open('/home/stephen/scratch/test-data.txt'):
  lines +=1
  line = line.strip()
  match = regex.match(line)

  if match:
data = match.groupdict()
if data['SiteIntelligenceCookie'] == '':
  no_cookies +=1
  else:
print "Couldn't match ", line
unmatched +=1

print "I analysed %s lines." % (lines,)
print "There were %s lines with missing Site Intelligence cookies." %
(no_cookies,)
print "I was unable to process %s lines." % (unmatched,)

How can I make the regex a bit more resilient so it doesn't break when
" " is embedded?

-- 
Stephen Nelson-Smith
Technical Director
Atalanta Systems Ltd
www.atalanta-systems.com
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Trouble with quotes

2010-03-08 Thread Martin P. Hellwig

On 03/08/10 17:06, Stephen Nelson-Smith wrote:

Hi,

I've written some (primitive) code to parse some apache logfies and
establish if apache has appended a session cookie to the end.  We're
finding that some browsers don't and apache doesn't just append a "-"
- it just omits it.

It's working fine, but for an edge case:

Couldn't match  192.168.1.107 - - [24/Feb/2010:20:30:44 +0100] "GET
http://sekrit.com/node/175523 HTTP/1.1" 200 -
"http://sekrit.com/search/results/"3%2B2%20course""; "Mozilla/4.0
(compatible; MSIE 6.0; Windows NT 5.1; SV1; GTB6.4)"


I didn't try to mentally parse the regex pattern (I like to keep 
reasonably sane). However from the sounds of it the script barfs when 
there is a quoted part in the second URL part. So how about doing a 
simple string.replace('/"','') & string.replace('" ','') before doing 
your re foo?


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


Re: Converting HTML to PDF using Python?

2010-03-08 Thread CM
On Mar 8, 10:36 am, Oltmans  wrote:
> Hi Python superstars,
>
> Guys, any ideas on how to convert HTML files to PDF files? Or as an
> alternative, how to convert HTML files to an image file(jpeg/png/etc)?
> Ideally, the converted PDF/Image file should like exactly like the way
> HTML file looks in the browser.
>
> I really have no idea about this so I will really appreciate all the
> help. Thanks in advance and kindly pardon my ignorance.
>
> Thanks,
> Oltmans

http://lmgtfy.com/?q=python+html+to+pdf
-- 
http://mail.python.org/mailman/listinfo/python-list


ssl, v23 client, v3 server...

2010-03-08 Thread Dino Viehland
In the ssl module docs (and in the tests) it says that if you have a client 
specifying PROTOCOL_SSLv23 (so it'll use v2 or v3) and a server specifying 
PROTOCOL_SSLv3 (so it'll only use v3) that you cannot connect between the two.  
Why doesn't this end up using SSL v3 for the communication?


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


Re: Converting HTML to PDF using Python?

2010-03-08 Thread Terry Reedy

On 3/8/2010 12:51 PM, CM wrote:

On Mar 8, 10:36 am, Oltmans  wrote:

Hi Python superstars,

Guys, any ideas on how to convert HTML files to PDF files? Or as an
alternative, how to convert HTML files to an image file(jpeg/png/etc)?
Ideally, the converted PDF/Image file should like exactly like the way
HTML file looks in the browser.


How an HTML file looks in *a* (not *the*) browser depends on the 
browser, its settings, and window size. That is both a virtue and a 
reason for the invention of pdf format.



I really have no idea about this so I will really appreciate all the
help. Thanks in advance and kindly pardon my ignorance.

Thanks,
Oltmans


http://lmgtfy.com/?q=python+html+to+pdf



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


Re: Calculating very large exponents in python

2010-03-08 Thread geremy condra
On Mon, Mar 8, 2010 at 2:15 AM, Fahad Ahmad  wrote:
> Thanks Geremy,
>
> That has been an absolute bump... GOD i cant sit on my chair, it has
> worked even on 512 bit number and with no time..
> superb i would say.
>
> lastly, i am using the code below to calculate Largest Prime factor of a
> number:
>
> print
> ('''==='''
>    '''  CALCULATE  HIGHEST PRIME
> FACTOR  '''
>
> '''===''')
>
> #!/usr/bin/env python
> def highest_prime_factor(n):
>    if isprime(n):
>   return n
>    for x in xrange(2,n ** 0.5 + 1):
>   if not n % x:
>  return highest_prime_factor(n/x)
> def isprime(n):
>    for x in xrange(2,n ** 0.5 + 1):
>   if not n % x:
>  return False
>    return True
> if  __name__ == "__main__":
>    import time
>    start = time.time()
>    print highest_prime_factor(1238162376372637826)
>    print time.time() - start
>
> the code works with a bit of delay on the number : "1238162376372637826" but
> extending it to
> (109026109913291424366305511581086089650628117463925776754560048454991130443047109026109913291424366305511581086089650628117463925776754560048454991130443047)
>  makes python go crazy. Is there any way just like above, i can have it
> calculated it in no time.
>
>
> thanks for the support.

If you're just looking for the largest prime factor I would suggest using
a fermat factorization attack. In the example you gave, it returns
nearly immediately.

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


Re: Calculating very large exponents in python

2010-03-08 Thread geremy condra
On Mon, Mar 8, 2010 at 2:05 PM, geremy condra  wrote:
> On Mon, Mar 8, 2010 at 2:15 AM, Fahad Ahmad  wrote:
>> Thanks Geremy,
>>
>> That has been an absolute bump... GOD i cant sit on my chair, it has
>> worked even on 512 bit number and with no time..
>> superb i would say.
>>
>> lastly, i am using the code below to calculate Largest Prime factor of a
>> number:
>>
>> print
>> ('''==='''
>>    '''  CALCULATE  HIGHEST PRIME
>> FACTOR  '''
>>
>> '''===''')
>>
>> #!/usr/bin/env python
>> def highest_prime_factor(n):
>>    if isprime(n):
>>   return n
>>    for x in xrange(2,n ** 0.5 + 1):
>>   if not n % x:
>>  return highest_prime_factor(n/x)
>> def isprime(n):
>>    for x in xrange(2,n ** 0.5 + 1):
>>   if not n % x:
>>  return False
>>    return True
>> if  __name__ == "__main__":
>>    import time
>>    start = time.time()
>>    print highest_prime_factor(1238162376372637826)
>>    print time.time() - start
>>
>> the code works with a bit of delay on the number : "1238162376372637826" but
>> extending it to
>> (109026109913291424366305511581086089650628117463925776754560048454991130443047109026109913291424366305511581086089650628117463925776754560048454991130443047)
>>  makes python go crazy. Is there any way just like above, i can have it
>> calculated it in no time.
>>
>>
>> thanks for the support.
>
> If you're just looking for the largest prime factor I would suggest using
> a fermat factorization attack. In the example you gave, it returns
> nearly immediately.
>
> Geremy Condra
>

Allow me to add a very important caveat to my previous statement:
a fermat factorization is primarily useful if you know that your number
is a large semiprime, such as an RSA modulus, which I assume you
are. Otherwise, make sure and test for primality.

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


Re: NoSQL Movement?

2010-03-08 Thread Duncan Booth
Xah Lee  wrote:

> For example, consider, if you are within world's top 100th user of
> database in terms of database size, such as Google, then it may be
> that the off-the-shelf tools may be limiting. But how many users
> really have such massive size of data?

You've totally missed the point. It isn't the size of the data you have 
today that matters, it's the size of data you could have in several years' 
time.

Maybe today you've got 10 users each with 10 megabytes of data, but you're 
aspiring to become the next twitter/facebook or whatever. It's a bit late 
as you approach 100 million users (and a petabyte of data) to discover that 
your system isn't scalable: scalability needs to be built in from day one.
-- 
http://mail.python.org/mailman/listinfo/python-list


Newbie question: python versions differ per user?

2010-03-08 Thread BobAalsma
I'm on Mac OS X 10.5.8 and downloaded 2.6.4 Mac Installer Disk Image
as/in(?) the sys admin user. For this user Pyhton 2.6.4 is now the
current version.
I want to use Python outside the sys asdmin user. However, all other
users still use Python 2.5.1 (Apple delivered).

The sys admin user looks in /Library/Frameworks/Python.framework/
Versions/2.6/lib/...
The other users look in/System/Library/Frameworks/
Python.framework/Version/2.5/lib/...

I could not find any questions on this matter, so am I the only one?
Did I do something wrong?
I assumed the paths for all users would be modified - too easy?
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Calculating very large exponents in python

2010-03-08 Thread Mark Dickinson
[Replying to Geremy's reply because the OP's post didn't show up in my
newsreader.]
On Mar 7, 8:40 pm, geremy condra  wrote:
> On Sun, Mar 7, 2010 at 1:55 PM, Fahad Ahmad  wrote:
> > Dear All,
>
> > i am writing my crytographic scheme in python, i am just a new user to it.
> > I have written the complete code, the only point i am stuck it is that i am
> > using 256 exponentiation which is normal in crytography but python just
> > hangs on it.
>
> > g**x  [where both g and x are 256 bit numbers , in decimal they are around
> > 77]

No library can solve this problem.  If g and x are both 256-bit
numbers then the result of g**x will have on the order of 10**79 bits,
which matches estimates of the number of particles in the universe.  I
can only imagine that you actually want g**x % m for some m, in which
case three-argument pow is your friend, as Geremy pointed out.

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


Re: negative "counts" in collections.Counter?

2010-03-08 Thread Raymond Hettinger
[Steven D'Aprano]
> Thanks for the explanation Raymond. A few comments follow:

You're welcome :-)


> Would you consider a feature enhancement adding an additional method,
> analogous to update(), to perform subtractions? I recognise that it's
> easy to subclass and do it yourself, but there does seem to be some
> demand for it, and it is an obvious feature given that Counter does
> support negative counts.

Will continue to mull it over.

Instinct says that conflating two models can be worse for usability
than just picking one of the models and excluding the other.

If I had it to do over, there is a reasonable case that elementwise
vector methods (__add__, __sub__, and __mul__) may have been a more
useful choice than multiset methods (__add__, __sub__, __and__,
__or__).

That being said, the multiset approach was the one that was chosen.
It was indicated for people who have experience with bags or
multisets in other languages.   It was also consistent with the naming
of the class as tool for counting things (i.e. it handles counting
numbers right out of the box).  No explicit support is provided
for negative values, but it isn't actively hindered either.

For applications needing elementwise vector operations and signed
arithmetic, arguably they should be using a more powerful toolset,
perhaps supporting a full-range of elementwise binary and unary
operations and a dotproduct() method.  Someone should write that
class and post it to the ASPN Cookbook to see if there is any uptake.


> Personally, I think the behaviour of + and - would be far less surprising
> if the class was called Multiset. Intuitively, one would expect counters
> to be limited to ints, and to support negative counts when adding and
> subtracting. In hindsight, do you think that Multiset would have been a
> better name?

The primary use case for Counter() is to count things (using the
counting numbers).
The term Multiset is more obscure and only applies
to the four operations that eliminate non-positive results.
So, I'm somewhat happy with the current name.

FWIW, the notion of "what is surprising" often depends on the
observer's
background and on the problem they are currently trying to solve.
If you need negative counts, then Counter.__sub__() is surprising.
If your app has no notion of a negative count, then it isn't.
The docs, examples, and docstrings are very clear about the behavior,
so the "surprise" is really about wanting it to do something other
than what it currently does ;-)


Raymond



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


Re: negative "counts" in collections.Counter?

2010-03-08 Thread Daniel Stutzbach
On Mon, Mar 8, 2010 at 1:24 PM, Raymond Hettinger  wrote:

> Instinct says that conflating two models can be worse for usability
> than just picking one of the models and excluding the other.
>

Toward that end, shouldn't Counter do one (and only one) of the follow?
1) Disallow non-positive counts (like a Multiset/Bag)
2) Allow them when subtracting

In other words, to avoid conflating two models the following two examples
should produce the same output:

>>> c = Counter({'red': 1})
>>> c['red'] -= 2
>>> c
Counter({'red': -1})

>>> c = Counter({'red': 1})
>>> c2 = Counter({'red': 2})
>>> c - c2
Counter()

--
Daniel Stutzbach, Ph.D.
President, Stutzbach Enterprises, LLC 
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: write to remote ile

2010-03-08 Thread monkeys paw

On 3/7/2010 9:53 PM, Martin P. Hellwig wrote:

On 03/08/10 02:51, monkeys paw wrote:

On 3/7/2010 9:20 PM, Martin P. Hellwig wrote:

On 03/08/10 02:10, monkeys paw wrote:

I can xfer a file from a remote server using:

import urllib2 as u
x=u.urlopen('http://tycho.usno.navy.mil/cgi-bin/timer.pl')

for line in x:
print line


How can i write a file to the remote server?

I tried:

x = u.url.open('http://joemoney.net/somefile.txt', 'w')

but that does not work


How do you normally (i.e. without python) put files on a remote server?


Using FTP, i'm really having a brain cramp here, but i'm new to python


We all been there, in some for or other ;-), anyway you might want to
have a look at this:
http://docs.python.org/library/ftplib.html



Tried this, the storlines doesn't work, everything else OK

from ftplib import FTP
ftp = FTP('joemoney.net')
ftp.login('secret','pw')
ftp.retrlines('LIST') # list directory contents
ftp.storlines('STOR', 'sf.xml')   # Is this incorrect, it throws error>

  File "ftp.py", line 5, in 
ftp.storlines('STOR', 'sf.xml')
  File "C:\Python26\lib\ftplib.py", line 470, in storlines
conn = self.transfercmd(cmd)
  File "C:\Python26\lib\ftplib.py", line 356, in transfercmd
return self.ntransfercmd(cmd, rest)[0]
  File "C:\Python26\lib\ftplib.py", line 327, in ntransfercmd
resp = self.sendcmd(cmd)
  File "C:\Python26\lib\ftplib.py", line 243, in sendcmd
return self.getresp()
  File "C:\Python26\lib\ftplib.py", line 218, in getresp
raise error_perm, resp
ftplib.error_perm: 501 No file name


The file exists in the same directory as ftp.py
--
http://mail.python.org/mailman/listinfo/python-list


Re: negative "counts" in collections.Counter?

2010-03-08 Thread Raymond Hettinger
[Vlastimil Brom]
> Thank you very much for the exhaustive explanation Raymond!

You're welcome.


> I am by far not able to follow all of the mathematical background, but
> even for zero-truncating multiset, I would expect the truncation on
> input rather than on output of some operations.

I debated about this and opted for be-loose-in-receiving-and-strict-on-
output.
One thought is that use cases for multisets would have real multisets
as inputs (no negative counts) and as outputs.  The user controls
the inputs, and the method only has a say in what its outputs are.

Also, truncating input would complicate the mathematical definition
of
what is happening.  Compare:

r = a[x] - b[x]
if r > 0:
emit(r)

vs.

r = max(0, a[x]) - max(0, b[x])
if r > 0:
emit(r)

Also, the design parallels what is done in the decimal module
where rounding is applied only to the results of operations,
not to the inputs.


> Probably a kind of negative_update()  or some better named method will
> be handy, like the one you supplied or simply the current module code
> without the newcount > 0: ... condition.

See my other post on this subject.  There is no doubt that
such a method would be handy for signed arithmetic.
The question is whether conflating two different models hurts
the API more than it helps.  Right now, the Counter() class
has no explicit support for negative values.  It is
designed around natural numbers and counting numbers.


> Or would it be an option to
> have a keyword argument like zero_truncate=False which would influence
> this behaviour?

Guido's thoughts on behavior flags is that they are usually a signal
that you need two different classes.  That is why itertools has
ifilter() and ifilterfalse() or izip() and izip_longest() instead
of having behavior flags.

In this case, we have an indication that what you really want is
a separate class supporting elementwise binary and unary operations
on vectors (where the vector fields are accessed by a dictionary
key instead of a positional value).


> Additionally, were issubset and issuperset considered for this
> interface (not sure whether symmetric_difference would be applicable)?

If the need arises, these could be included.  Right now, you
can get the same result with:  "if a - b: ..."

FWIW, I never liked those two method names.  Can't remember whether
a.issubset(b) means "a is a subset of b" or "b issubset of a'.


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


Re: Reverse engineering CRC?

2010-03-08 Thread Gregory Ewing

Dave Angel wrote:
If you know so little about the 
value, how do you even know it's a CRC ?  Could it be a ones-complement 
sum, such as used in Ethernet?


I'm going by the fact that the application reports a
"CRC mismatch" when it's wrong. I can't be sure that what
it calls a "CRC" is really a true CRC, but it's more than
a simple sum, because changing one bit in the file results
in a completely different value.


Is the problem really worth it?


Probably not -- it looks like fixing the problem I'm trying
to fix by hand will be faster in the long run. I just thought
it might turn out to be a solved problem and someone could
point me to an algorithm for it, but it seems not.

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


Re: Import problem

2010-03-08 Thread News123
Hi Steven,

Steven D'Aprano wrote:
> On Sat, 06 Mar 2010 03:53:53 -0800, Johny wrote:
>
> import sys
> sys.path.append('C:\\A')
> from A.B import bmodule
>> Traceback (most recent call last):
>>   File "", line 1, in 
>> ImportError: No module named A.B
>
> The current directory is irrelevant, except that it is automatically
> added to the PYTHONPATH. That's why you can import A.B when the current
> directory is C.

Minor currection:

It doesn't seem to be the current directory, but the directory, where
the script is located in, which is auto-appended to the pythonpath

Please see following example:

$ python -V
Python 2.6.4

$ mkdir A

$ touch A/__init__

$ # create A/blla.py an A/blo.py

$ cat A/bla.py
print "I am bla"
import A.blo
print "and I found blo",dir(A.blo)
$ cat A/blo.py
avar = 3
print "I am blo"

$ python A/bla.py
I am bla
Traceback (most recent call last):
  File "A/bla.py", line 2, in 
import A.blo
ImportError: No module named A.blo



However:
$ cat alternative_bla.py
import sys
sys.path.append(".")
print "I am bla"
import A.blo
print "and I found blo",dir(A.blo)

$ python A/alternativ_bla.py
I am bla
I am blo
and I found blo ['__builtins__', '__doc__', '__file__', '__name__',
'__package__', 'avar']


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


Re: negative "counts" in collections.Counter?

2010-03-08 Thread Gregory Ewing

Raymond Hettinger wrote:


Instead the choice was to implement the four methods as
multiset operations.  As such, they need to correspond
to regular set operations.


Seems to me you're trying to make one data type do the
work of two, and ending up with something inconsistent.

I think you should be providing two types: one is a
multiset, which disallows negative counts altogether;
the other behaves like a sparse vector with appropriate
arithmetic operations.

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


Re: negative "counts" in collections.Counter?

2010-03-08 Thread Vlastimil Brom
2010/3/8 Raymond Hettinger :
...
[snip detailed explanations]
>...
> In this case, we have an indication that what you really want is
> a separate class supporting elementwise binary and unary operations
> on vectors (where the vector fields are accessed by a dictionary
> key instead of a positional value).
>
>
>> Additionally, were issubset and issuperset considered for this
>> interface (not sure whether symmetric_difference would be applicable)?
>
> If the need arises, these could be included.  Right now, you
> can get the same result with:  "if a - b: ..."
>
> FWIW, I never liked those two method names.  Can't remember whether
> a.issubset(b) means "a is a subset of b" or "b issubset of a'.
>
>
> Raymond
> --
>
Thanks for the further remarks Raymond,
initially I thought while investigating new features of python 3, this
would be a case for replacing the "home made" solutions with the
standard module functionality.
Now I can see, it probably wouldn't be an appropriate decision in this
case, as the expected usage of Counter with its native methods is
different.

As for the issubset, issuperset method names, I am glad, a far more
skilled person has the same problem like me :-)   In this case the
operators appear to be clearer than the method names...

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


Re: Import problem

2010-03-08 Thread News123
Jean-Michel Pichavant wrote:
> Johny wrote:
>> I have this directory structure
>>
>> C:
>>   \A
>>  __init__.py
>>  amodule.py
>>
>>  \B
>>   __init__.py
>>   bmodule.py
>>
>>\D
>> __init__.py
>> dmodule.py
>>
>> and  I want to import  bmodule.py
>> C:\>cd \
>>
>> C:\>python
>> Python 2.5 (r25:51908, Sep 19 2006, 09:52:17) [MSC v.1310 32 bit
>> (Intel)] on win
>> 32
>> Type "help", "copyright", "credits" or "license" for more information.
>>  
> from A.B import  bmodule
> 
>> I am bmodule
>>   C:\>
>>
>> so far so good. Now I would like to import bmodule but if the current
>> directory is \D subdirectory.
>>
>> C:> cd \A\B\D
>> C:\A\B\D>
>> C:\A\B\D>python
>> Python 2.5 (r25:51908, Sep 19 2006, 09:52:17) [MSC v.1310 32 bit
>> (Intel)] on win
>> 32
>> Type "help", "copyright", "credits" or "license" for more information.
>>  
> import sys
> sys.path.append('C:\\A')
> from A.B import bmodule
> 
>> Traceback (most recent call last):
>>   File "", line 1, in 
>> ImportError: No module named A.B
>>
>> C:\>
>>
>> so I can not import a module from the parent directory? Or where did I
>> make an error?
>> Thanks for help
>>
>> L.
>>   
> try
> 
> import sys
> sys.path.append('C:\\')
> from A.B import bmodule
> 
is there any 'automatic' way of finding the top level
directory?basically the 'top level directory is the first directory
going upwards, that doesn't contain a __init__.py file.

of course you could do this 'manually' by
doing:

# assume, that this module is A.amodule
import sys
import os

# I'd love to have a similiar automatic construct
if __name__ == "__main__":
level = 1 # or function locating how far to go up before
  # finding a dir, whcih does not contain a __init__.py
mydir = os.path.split(__file__)[0]
topdir = os.path.join( mydir,*(("..",)*level))
abstop = os.path.abspath(topdir)
sys.path.append(abstop)

## now you can import with the normal module paths

import A.blo
print "and I found blo",dir(A.blo)


bye N



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


Re: Import problem

2010-03-08 Thread News123
Jean-Michel Pichavant wrote:
> Johny wrote:
>> I have this directory structure
>>
>> C:
>>   \A
>>  __init__.py
>>  amodule.py
>>
>>  \B
>>   __init__.py
>>   bmodule.py
>>
>>\D
>> __init__.py
>> dmodule.py
>>
>> and  I want to import  bmodule.py
>> C:\>cd \
>>
>> C:\>python
>> Python 2.5 (r25:51908, Sep 19 2006, 09:52:17) [MSC v.1310 32 bit
>> (Intel)] on win
>> 32
>> Type "help", "copyright", "credits" or "license" for more information.
>>  
> from A.B import  bmodule
> 
>> I am bmodule
>>   C:\>
>>
>> so far so good. Now I would like to import bmodule but if the current
>> directory is \D subdirectory.
>>
>> C:> cd \A\B\D
>> C:\A\B\D>
>> C:\A\B\D>python
>> Python 2.5 (r25:51908, Sep 19 2006, 09:52:17) [MSC v.1310 32 bit
>> (Intel)] on win
>> 32
>> Type "help", "copyright", "credits" or "license" for more information.
>>  
> import sys
> sys.path.append('C:\\A')
> from A.B import bmodule
> 
>> Traceback (most recent call last):
>>   File "", line 1, in 
>> ImportError: No module named A.B
>>
>> C:\>
>>
>> so I can not import a module from the parent directory? Or where did I
>> make an error?
>> Thanks for help
>>
>> L.
>>   
> try
> 
> import sys
> sys.path.append('C:\\')
> from A.B import bmodule
> 
is there any 'automatic' way of finding the top level
directory?basically the 'top level directory is the first directory
going upwards, that doesn't contain a __init__.py file.

of course you could do this 'manually' by
doing:

# assume, that this module is A.amodule
import sys
import os

# I'd love to have a similiar automatic construct
if __name__ == "__main__":
level = 1 # or function locating how far to go up before
  # finding a dir, whcih does not contain a __init__.py
mydir = os.path.split(__file__)[0]
topdir = os.path.join( mydir,*(("..",)*level))
abstop = os.path.abspath(topdir)
sys.path.append(abstop)

## now you can import with the normal module paths

import A.blo
print "and I found blo",dir(A.blo)


bye N



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


Re: libc Sleep api performs a busy waiting

2010-03-08 Thread News123
Hi Steve,

Steve Holden wrote:
> Mahesh wrote:
>> Hi,
>>
>>  I am having a problem while using sleep function from libc , the
>> thread in which i am calling it is getting struck and not allowing
>> other threads to execute.  Here is a simple code that i am trying to
>> exeute
>>
>> import threading
>> import time
>> import dl
>>
>>
>> def dummy1():
>> a=dl.open('/lib/libc.so.6')
>> print "in thread 1 Start"
>> a.call('sleep',2)
>> #time.sleep(2)
>> print "in thread 1 End"
>>
>> def dummy2():
>> print "in thread 2 Start"
>> time.sleep(1)
>> print "in thread 2 End"
>> newThread1=threading.Thread(None,dummy1)
>> newThread2=threading.Thread(None,dummy2)
>> newThread1.start()
>> newThread2.start()
>>
>> print "in main"
>>
>>
>>
>> The out put of this program is  (In this case thread 1 even though i
>> am calling a sleep function its not allowing other threads to execute,
>> other threads execute only after the completion of first thread)
>>
>> in thread 1 Start
>> in thread 1 End
>> in thread 2 Start
>> in main
>> in thread 2 End
>>
>>
>> where as if i use time.sleep instead of a.call(sleep) the out put is
>> (which i guess is right behaviour, because it start the threads and
>> suspends them because the have sleep , and continue executing the main
>> thread)
>> in thread 1 Start
>> in thread 2 Start
>> in main
>> in thread 2 End
>> in thread 1 End
>>
>>
> Why not just use the time module's sleep function? Unlike the libc code
> it releases Python's GIL (global interpreter lock), thus allowing other
> threads to run.
> 
I fully agree for the given example.
Especcially time.sleep allows even sub second sleeps.

Out of curiousity though:

Is there any way to perform a call(), that releases the GIL before
calling  the function?

It would avoid writing a wrapper?


bye


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


Re: What to do if anything bites.

2010-03-08 Thread News123
Google Adsense wrote:
> What to do if anything bites.
> 
> Check our bites treatment at
> 
> http://108ambulance.blogspot.com/2010/03/108-ambulance-home-page.html

Pythons don't bite
-- 
http://mail.python.org/mailman/listinfo/python-list


related lists mean value

2010-03-08 Thread dimitri pater - serpia
Hi,

I have two related lists:
x = [1 ,2, 8, 5, 0, 7]
y = ['a', 'a', 'b', 'c', 'c', 'c' ]

what I need is a list representing the mean value of 'a', 'b' and 'c'
while maintaining the number of items (len):
w = [1.5, 1.5, 8, 4, 4, 4]

I have looked at iter(tools) and next(), but that did not help me. I'm
a bit stuck here, so your help is appreciated!

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


Re: negative "counts" in collections.Counter?

2010-03-08 Thread Raymond Hettinger
[Gregory Ewing]
> I think you should be providing two types: one is a
> multiset, which disallows negative counts altogether;
> the other behaves like a sparse vector with appropriate
> arithmetic operations.


That is pretty close to what I said above:

"""
In this case, we have an indication that what you really want is a
separate class supporting elementwise binary and unary operations on
vectors (where the vector fields are accessed by a dictionary key
instead of a positional value).
"""

The first and foremost goal of the class was to provide a simple
counting capability along the lines of:

class Counter(dict):
   def __init__(self, iterable=()):
for elem in iterable:
self[elem] += 1
   def __missing__(self, key):
return 0

Note, there is no "disallowing negatives" or even any insistence
that the "counts" are actually numbers.  In that respect, it was
a consenting adults class from the start.  It is short, simple,
and fast.

The other methods were mostly just "extras" to make the tool
more useful.  Each adds its own minimal restrictions:

   elements() requires integer counts
   most_common() requires that counts be orderable
   multiset operations are defined for non-negative values

We could have kept this same design and wrapped every dictionary
method so that it would raise an exception for any count that was not
a positive integer.  That would have slowed down the class, increased
the code volume, and precluded some uses that are currently possible.
IMO, that would not have been a win and it would not have helped the
OP who was seeking a more vector-like tool for signed-number
operations.

Anyway, it is what it is.  The tool is released and most
users seem to be happy with what we have.


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


Re: write to remote ile

2010-03-08 Thread MRAB

monkeys paw wrote:

On 3/7/2010 9:53 PM, Martin P. Hellwig wrote:

On 03/08/10 02:51, monkeys paw wrote:

On 3/7/2010 9:20 PM, Martin P. Hellwig wrote:

On 03/08/10 02:10, monkeys paw wrote:

I can xfer a file from a remote server using:

import urllib2 as u
x=u.urlopen('http://tycho.usno.navy.mil/cgi-bin/timer.pl')

for line in x:
print line


How can i write a file to the remote server?

I tried:

x = u.url.open('http://joemoney.net/somefile.txt', 'w')

but that does not work


How do you normally (i.e. without python) put files on a remote server?


Using FTP, i'm really having a brain cramp here, but i'm new to python


We all been there, in some for or other ;-), anyway you might want to
have a look at this:
http://docs.python.org/library/ftplib.html



Tried this, the storlines doesn't work, everything else OK

from ftplib import FTP
ftp = FTP('joemoney.net')
ftp.login('secret','pw')
ftp.retrlines('LIST') # list directory contents
ftp.storlines('STOR', 'sf.xml')   # Is this incorrect, it throws error>


The documentation says:

FTP.storlines(cmd, file, callback=None)

where 'cmd' is the FTP command, 'STOR sf.xml' ('sf.xl' is the path of
the file on the server) and 'file' is the open (local) source file.
--
http://mail.python.org/mailman/listinfo/python-list


Re: Converting HTML to PDF using Python?

2010-03-08 Thread Grant Edwards
On 2010-03-08, Oltmans  wrote:

> Guys, any ideas on how to convert HTML files to PDF files?

os.system("w3m -dump %s | a2ps -B --borders=no -o - | ps2pdf - %s" % 
(infile,outfile))

-- 
Grant Edwards   grant.b.edwardsYow! Am I accompanied by a
  at   PARENT or GUARDIAN?
  gmail.com
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: related lists mean value

2010-03-08 Thread MRAB

dimitri pater - serpia wrote:

Hi,

I have two related lists:
x = [1 ,2, 8, 5, 0, 7]
y = ['a', 'a', 'b', 'c', 'c', 'c' ]

what I need is a list representing the mean value of 'a', 'b' and 'c'
while maintaining the number of items (len):
w = [1.5, 1.5, 8, 4, 4, 4]

I have looked at iter(tools) and next(), but that did not help me. I'm
a bit stuck here, so your help is appreciated!


Try doing it in 2 passes.

First pass: count the number of times each string occurs in 'y' and the
total for each (zip/izip and defaultdict are useful for these).

Second pass: create the result list containing the mean values.
--
http://mail.python.org/mailman/listinfo/python-list


Re: related lists mean value

2010-03-08 Thread Chris Rebert
On Mon, Mar 8, 2010 at 2:34 PM, dimitri pater - serpia
 wrote:
> Hi,
>
> I have two related lists:
> x = [1 ,2, 8, 5, 0, 7]
> y = ['a', 'a', 'b', 'c', 'c', 'c' ]
>
> what I need is a list representing the mean value of 'a', 'b' and 'c'
> while maintaining the number of items (len):
> w = [1.5, 1.5, 8, 4, 4, 4]
>
> I have looked at iter(tools) and next(), but that did not help me. I'm
> a bit stuck here, so your help is appreciated!

from __future__ import division

def group(keys, values):
#requires None not in keys
groups = []
cur_key = None
cur_vals = None
for key, val in zip(keys, values):
if key != cur_key:
if cur_key is not None:
groups.append((cur_key, cur_vals))
cur_vals = [val]
cur_key = key
else:
cur_vals.append(val)
groups.append((cur_key, cur_vals))
return groups

def average(lst):
return sum(lst) / len(lst)

def process(x, y):
result = []
for key, vals in group(y, x):
avg = average(vals)
for i in xrange(len(vals)):
result.append(avg)
return result

x = [1 ,2, 8, 5, 0, 7]
y = ['a', 'a', 'b', 'c', 'c', 'c' ]

print process(x, y)
#=> [1.5, 1.5, 8.0, 4.0, 4.0, 4.0]

It could be tweaked to use itertools.groupby(), but it would probably
be less efficient/clear.

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


Re: Reverse engineering CRC?

2010-03-08 Thread Dave Angel

Gregory Ewing wrote:
Dave 
Angel wrote:
If you know so little about the value, how do you even know it's a 
CRC ?  Could it be a ones-complement sum, such as used in Ethernet?


I'm going by the fact that the application reports a
"CRC mismatch" when it's wrong. I can't be sure that what
it calls a "CRC" is really a true CRC, but it's more than
a simple sum, because changing one bit in the file results
in a completely different value.


Is the problem really worth it?


Probably not -- it looks like fixing the problem I'm trying
to fix by hand will be faster in the long run. I just thought
it might turn out to be a solved problem and someone could
point me to an algorithm for it, but it seems not.

If you assume it's done in a single pass, and you know which byte is the 
end of the buffer, I'd think you could learn a lot by just tweaking that 
last byte.  But I still think you'd want to automate your testing, 
presumably by some keystroke-stuffer.


DaveA

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


Re: Python 3.1.2 release candidate

2010-03-08 Thread Mensanator
On Mar 6, 4:13 pm, Benjamin Peterson  wrote:
> On behalf of the Python development team, I'm pleased to announce a release
> candidate for the second bugfix release of the Python 3.1 series, Python 
> 3.1.2.
>
> This bug fix release fixes numerous issues found in 3.1.1.  This release
> candidate has been released to solicit testing and feedback over an possible
> regressions from 3.1.1.  Please consider testing it with your library or
> application and reporting an bugs you encounter.  This will help make the 
> final
> 3.1.2 release, planned in 2 weeks time, all the more stable.
>
> The Python 3.1 version series focuses on the stabilization and optimization of
> the features and changes that Python 3.0 introduced.  For example, the new I/O
> system has been rewritten in C for speed.  File system APIs that use unicode
> strings now handle paths with undecodable bytes in them. Other features 
> include
> an ordered dictionary implementation, a condensed syntax for nested with
> statements, and support for ttk Tile in Tkinter.  For a more extensive list of
> changes in 3.1, seehttp://doc.python.org/3.1/whatsnew/3.1.htmlor Misc/NEWS in
> the Python distribution.
>
> To download Python 3.1.2rc1 visit:
>
>      http://www.python.org/download/releases/3.1.2/
>
> A list of changes in 3.1.2rc1 can be found here:
>
>      http://svn.python.org/projects/python/tags/r312rc1/Misc/NEWS
>
> The 3.1 documentation can be found at:
>
>      http://docs.python.org/3.1
>
> Bugs can always be reported to:
>
>      http://bugs.python.org

Hey, it finally let me register. Turned out the e-mail
to confirm registry was getting marked Spam. I pulled it
out of the Spam box and completed the registration.

Of course, the reason I needed to register is because
IDLE will *still* not end a process properly in Windows.

see issue 8093

>
> Enjoy!
>
> --
> Benjamin Peterson
> Release Manager
> benjamin at python.org
> (on behalf of the entire python-dev team and 3.1.2's contributors)

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


Re: related lists mean value

2010-03-08 Thread dimitri pater - serpia
thanks Chris and MRAB!
Looks good, I'll try it out

On Tue, Mar 9, 2010 at 12:22 AM, Chris Rebert  wrote:
> On Mon, Mar 8, 2010 at 2:34 PM, dimitri pater - serpia
>  wrote:
>> Hi,
>>
>> I have two related lists:
>> x = [1 ,2, 8, 5, 0, 7]
>> y = ['a', 'a', 'b', 'c', 'c', 'c' ]
>>
>> what I need is a list representing the mean value of 'a', 'b' and 'c'
>> while maintaining the number of items (len):
>> w = [1.5, 1.5, 8, 4, 4, 4]
>>
>> I have looked at iter(tools) and next(), but that did not help me. I'm
>> a bit stuck here, so your help is appreciated!
>
> from __future__ import division
>
> def group(keys, values):
>    #requires None not in keys
>    groups = []
>    cur_key = None
>    cur_vals = None
>    for key, val in zip(keys, values):
>        if key != cur_key:
>            if cur_key is not None:
>                groups.append((cur_key, cur_vals))
>            cur_vals = [val]
>            cur_key = key
>        else:
>            cur_vals.append(val)
>    groups.append((cur_key, cur_vals))
>    return groups
>
> def average(lst):
>    return sum(lst) / len(lst)
>
> def process(x, y):
>    result = []
>    for key, vals in group(y, x):
>        avg = average(vals)
>        for i in xrange(len(vals)):
>            result.append(avg)
>    return result
>
> x = [1 ,2, 8, 5, 0, 7]
> y = ['a', 'a', 'b', 'c', 'c', 'c' ]
>
> print process(x, y)
> #=> [1.5, 1.5, 8.0, 4.0, 4.0, 4.0]
>
> It could be tweaked to use itertools.groupby(), but it would probably
> be less efficient/clear.
>
> Cheers,
> Chris
> --
> http://blog.rebertia.com
>



-- 
---
You can't have everything. Where would you put it? -- Steven Wright
---
please visit www.serpia.org
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Queue peek?

2010-03-08 Thread Aahz
In article <58f61382-ac79-46fb-8612-a3c9fde29...@c16g2000yqd.googlegroups.com>,
Veloz   wrote:
>
>The "peek" parts comes in when the user comes back later to see if
>their report has done. That is, in my page controller logic, I'd like
>to look through the complete queue and see if the specific report has
>been finished (I could tell by matching up the ID of the original
>request to the ID in the completed queue). If there was an item in the
>queue matching the ID, it would be removed.

Here's the question: what happens when the user refreshes the "report
done" page?  The problem with the way you're doing it is that checking
for report done is a one-shot operation.  You probably want to use a
database to cache this and have some kind of cache expiration.
-- 
Aahz (a...@pythoncraft.com)   <*> http://www.pythoncraft.com/

"Many customs in this life persist because they ease friction and promote
productivity as a result of universal agreement, and whether they are
precisely the optimal choices is much less important." --Henry Spencer
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: related lists mean value

2010-03-08 Thread John Posner

On 3/8/2010 5:34 PM, dimitri pater - serpia wrote:

Hi,

I have two related lists:
x = [1 ,2, 8, 5, 0, 7]
y = ['a', 'a', 'b', 'c', 'c', 'c' ]

what I need is a list representing the mean value of 'a', 'b' and 'c'
while maintaining the number of items (len):
w = [1.5, 1.5, 8, 4, 4, 4]

I have looked at iter(tools) and next(), but that did not help me. I'm
a bit stuck here, so your help is appreciated!


Nobody expects object-orientation (or the Spanish Inquisition):

#-
from collections import defaultdict

class Tally:
def __init__(self, id=None):
self.id = id
self.total = 0
self.count = 0

x = [1 ,2, 8, 5, 0, 7]
y = ['a', 'a', 'b', 'c', 'c', 'c']

# gather data
tally_dict = defaultdict(Tally)
for i in range(len(x)):
obj = tally_dict[y[i]]
obj.id = y[i]
obj.total += x[i]
obj.count += 1

# process data
result_list = []
for key in sorted(tally_dict):
obj = tally_dict[key]
mean = 1.0 * obj.total / obj.count
result_list.extend([mean] * obj.count)
print result_list
#-

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


Re: related lists mean value

2010-03-08 Thread John Posner

On 3/8/2010 9:39 PM, John Posner wrote:




# gather data
tally_dict = defaultdict(Tally)
for i in range(len(x)):
obj = tally_dict[y[i]]
obj.id = y[i] <--- statement redundant, remove it
obj.total += x[i]
obj.count += 1


-John


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


Re: related lists mean value

2010-03-08 Thread John Posner

On 3/8/2010 9:43 PM, John Posner wrote:

On 3/8/2010 9:39 PM, John Posner wrote:





obj.id = y[i] <--- statement redundant, remove it


Sorry for the thrashing! It's more correct to say that the Tally class 
doesn't require an "id" attribute at all. So the code becomes:


#-
from collections import defaultdict

class Tally:
def __init__(self):
self.total = 0
self.count = 0

x = [1 ,2, 8, 5, 0, 7]
y = ['a', 'a', 'b', 'c', 'c', 'c']

# gather data
tally_dict = defaultdict(Tally)
for i in range(len(x)):
obj = tally_dict[y[i]]
obj.total += x[i]
obj.count += 1

# process data
result_list = []
for key in sorted(tally_dict):
obj = tally_dict[key]
mean = 1.0 * obj.total / obj.count
result_list.extend([mean] * obj.count)
print result_list
#-

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


remove element with ElementTree

2010-03-08 Thread tdan
I have been using ElementTree to write an app, and would like to
simply remove an element.
But in ElementTree, you must know both the parent and the child
element to do this.
There is no getparent() function, so I am stuck if I only have an
element.

I am iterating over a table and getting all  tags, checking their
text, and conditionally deleting them:

def RemoveElementWithText( topEl, subEl, text ):
for el in topEl.getiterator( subEl ):
if el.text = text:
break
else:
el = None
return el

RemoveElementWithText( table, 'td', 'sometext' )

My table is like so:


 ...
 ...

Is there any way to do this in ElementTree?  I see lxml.etree does
this nicely, but I want to use python's standard library.
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: libc Sleep api performs a busy waiting

2010-03-08 Thread Joe Fox
Hi,

 actually i have simplified my scenario a lot here ,

In my actual case , i have to call a C-api which blocks on  c select , in a
separate thread.

my thread is getting struck in that api , and thus blocking all the other
threads.
Can you point to something which will help me call this blocking C-api call
without my thread getting struck.


Thanks
Mahesh

On Mon, Mar 8, 2010 at 6:03 PM, Steve Holden  wrote:

> Mahesh wrote:
> > Hi,
> >
> >  I am having a problem while using sleep function from libc , the
> > thread in which i am calling it is getting struck and not allowing
> > other threads to execute.  Here is a simple code that i am trying to
> > exeute
> >
> > import threading
> > import time
> > import dl
> >
> >
> > def dummy1():
> > a=dl.open('/lib/libc.so.6')
> > print "in thread 1 Start"
> > a.call('sleep',2)
> > #time.sleep(2)
> > print "in thread 1 End"
> >
> > def dummy2():
> > print "in thread 2 Start"
> > time.sleep(1)
> > print "in thread 2 End"
> > newThread1=threading.Thread(None,dummy1)
> > newThread2=threading.Thread(None,dummy2)
> > newThread1.start()
> > newThread2.start()
> >
> > print "in main"
> >
> >
> >
> > The out put of this program is  (In this case thread 1 even though i
> > am calling a sleep function its not allowing other threads to execute,
> > other threads execute only after the completion of first thread)
> >
> > in thread 1 Start
> > in thread 1 End
> > in thread 2 Start
> > in main
> > in thread 2 End
> >
> >
> > where as if i use time.sleep instead of a.call(sleep) the out put is
> > (which i guess is right behaviour, because it start the threads and
> > suspends them because the have sleep , and continue executing the main
> > thread)
> > in thread 1 Start
> > in thread 2 Start
> > in main
> > in thread 2 End
> > in thread 1 End
> >
> >
> Why not just use the time module's sleep function? Unlike the libc code
> it releases Python's GIL (global interpreter lock), thus allowing other
> threads to run.
>
> regards
>  Steve
> --
> Steve Holden   +1 571 484 6266   +1 800 494 3119
> PyCon is coming! Atlanta, Feb 2010  http://us.pycon.org/
> Holden Web LLC http://www.holdenweb.com/
> UPCOMING EVENTS:http://holdenweb.eventbrite.com/
>
> --
> http://mail.python.org/mailman/listinfo/python-list
>
-- 
http://mail.python.org/mailman/listinfo/python-list


imported var not being updated

2010-03-08 Thread Alex Hall
Hello all:
I have a project with many pyw files. One file holds a variable and a
function that I find myself using across many other pyw files, so I
called it helpers.pyw. The variable is "mostRecent", which is a string
and is meant to hold a string so I know what the program most recently
output; in all the other pyw files, every time the program outputs
something, I include the line
helpers.mostRecent=output
where output is a string and helpers has been imported.

The problem is that mostRecent does not seem to be updated at all, and
I cannot figure out why. I declare it with empty quotes initially, but
the program should keep updating it. Everytime I go to see what its
value is, though, it is still apparently empty. Here is a basic
sample:

randomFile.pyw
---
import helpers

def something():
  output="I am the most recently output string!"
  helpers.mostRecent=output
#end def

file2.pyw
---
from helpers import mostRecent

print(mostRecent) #prints nothing

Any thoughts on what I am doing wrong? Thanks!!

-- 
Have a great day,
Alex (msg sent from GMail website)
mehg...@gmail.com; http://www.facebook.com/mehgcap
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: imported var not being updated

2010-03-08 Thread Gary Herron

Alex Hall wrote:

Hello all:
I have a project with many pyw files. One file holds a variable and a
function that I find myself using across many other pyw files, so I
called it helpers.pyw. The variable is "mostRecent", which is a string
and is meant to hold a string so I know what the program most recently
output; in all the other pyw files, every time the program outputs
something, I include the line
helpers.mostRecent=output
where output is a string and helpers has been imported.

The problem is that mostRecent does not seem to be updated at all, and
I cannot figure out why. I declare it with empty quotes initially, but
the program should keep updating it. Everytime I go to see what its
value is, though, it is still apparently empty. Here is a basic
sample:

randomFile.pyw
---
import helpers

def something():
  output="I am the most recently output string!"
  helpers.mostRecent=output
#end def

file2.pyw
---
from helpers import mostRecent

print(mostRecent) #prints nothing

Any thoughts on what I am doing wrong? Thanks!!
  


The form of import you are using
   from helpers import mostRecent
makes a *new* binding to the value in the module that's doing the 
import.  Rebinding the var in a different module does not affect this.  
It's similar to this:


   a = 'string'
   b = a
   a = 'another string'

won't affect b's value.

What you can do, is not make a separate binding, but reach into the 
helpers module to get the value there.  Like this:


   import helpers
   print helpers.mostRecent

That will work as you hope.

Gary Herron


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


Re: imported var not being updated

2010-03-08 Thread Alex Hall
Thanks, it worked as expected. I guess I figured that Python would
read my mind and realize that I wanted mostRecent to act globally for
the program, imported as a copy or accessed in its own namespace
(right term?) Oh well, the day computers can read thoughts like that
is the day programmers are out of a job... Thanks again.

On 3/8/10, Gary Herron  wrote:
> Alex Hall wrote:
>> Hello all:
>> I have a project with many pyw files. One file holds a variable and a
>> function that I find myself using across many other pyw files, so I
>> called it helpers.pyw. The variable is "mostRecent", which is a string
>> and is meant to hold a string so I know what the program most recently
>> output; in all the other pyw files, every time the program outputs
>> something, I include the line
>> helpers.mostRecent=output
>> where output is a string and helpers has been imported.
>>
>> The problem is that mostRecent does not seem to be updated at all, and
>> I cannot figure out why. I declare it with empty quotes initially, but
>> the program should keep updating it. Everytime I go to see what its
>> value is, though, it is still apparently empty. Here is a basic
>> sample:
>>
>> randomFile.pyw
>> ---
>> import helpers
>>
>> def something():
>>   output="I am the most recently output string!"
>>   helpers.mostRecent=output
>> #end def
>>
>> file2.pyw
>> ---
>> from helpers import mostRecent
>>
>> print(mostRecent) #prints nothing
>>
>> Any thoughts on what I am doing wrong? Thanks!!
>>
>
> The form of import you are using
> from helpers import mostRecent
> makes a *new* binding to the value in the module that's doing the
> import.  Rebinding the var in a different module does not affect this.
> It's similar to this:
>
> a = 'string'
> b = a
> a = 'another string'
>
> won't affect b's value.
>
> What you can do, is not make a separate binding, but reach into the
> helpers module to get the value there.  Like this:
>
> import helpers
> print helpers.mostRecent
>
> That will work as you hope.
>
> Gary Herron
>
>
> --
> http://mail.python.org/mailman/listinfo/python-list
>


-- 
Have a great day,
Alex (msg sent from GMail website)
mehg...@gmail.com; http://www.facebook.com/mehgcap
-- 
http://mail.python.org/mailman/listinfo/python-list


scipy sparse matrix question

2010-03-08 Thread Victor Eijkhout
I can't find any detailed information about scipy.sparse.

My specific question: what does "for x in A" give me when A is a sparse
matrix? It seems to yield all nonzero locations, but in what kind of
form? Very specifically: how do I get the (i,j) coordinates and the
value from x?

Victor.

-- 
Victor Eijkhout -- eijkhout at tacc utexas edu
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Calculating very large exponents in python

2010-03-08 Thread casevh
[also replying to Geremy since the OP's message doesn't appear...]

On Mar 8, 11:05 am, geremy condra  wrote:
> On Mon, Mar 8, 2010 at 2:15 AM, Fahad Ahmad  wrote:
> > Thanks Geremy,
>
> > That has been an absolute bump... GOD i cant sit on my chair, it has
> > worked even on 512 bit number and with no time..
> > superb i would say.
>
> > lastly, i am using the code below to calculate Largest Prime factor of a
> > number:
>
> > print
> > ('''===­'''
> >    '''  CALCULATE  HIGHEST PRIME
> > FACTOR  '''
>
> > '''­===''')
>
> > #!/usr/bin/env python
> > def highest_prime_factor(n):
> >    if isprime(n):
> >   return n
> >    for x in xrange(2,n ** 0.5 + 1):
> >   if not n % x:
> >  return highest_prime_factor(n/x)
> > def isprime(n):
> >    for x in xrange(2,n ** 0.5 + 1):
> >   if not n % x:
> >  return False
> >    return True
> > if  __name__ == "__main__":
> >    import time
> >    start = time.time()
> >    print highest_prime_factor(1238162376372637826)
> >    print time.time() - start
>
> > the code works with a bit of delay on the number : "1238162376372637826" but
> > extending it to
> > (10902610991329142436630551158108608965062811746392577675456004845499113044­304710902610991329142436630551158108608965062811746392577675456004845499113­0443047)
> >  makes python go crazy. Is there any way just like above, i can have it
> > calculated it in no time.
>
> > thanks for the support.
>
> If you're just looking for the largest prime factor I would suggest using
> a fermat factorization attack. In the example you gave, it returns
> nearly immediately.
>
> Geremy Condra- Hide quoted text -
>
> - Show quoted text -

For a Python-based solution, you might want to look at pyecm (http://
sourceforge.net/projects/pyecm/)

On a system with gmpy installed also, pyecm found the following
factors:

101, 521, 3121, 9901, 36479, 300623, 53397071018461,
1900381976777332243781

There still is a 98 digit unfactored composite:

60252507174568243758911151187828438446814447653986842279796823262165159406500174226172705680274911

Factoring this remaining composite using ECM may not be practical.

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


How to detect C Function block using python

2010-03-08 Thread Faheem

Hey All,
I'm new in this community. I am writing a static analyzer for validating C Code 
using python and for that I'm looking for a python module/API that will detect 
Function block of a given C-File. I know simple function can be detected using 
push "{" and poping it if character "}" is found. This solution is actually 
done already, I want a robust API that can do more then that like detecting 
Function name parameters its return types etc.
Thanks!Faheem  




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