Re: Killing threads (was Re: Cancel or timeout a long running regular expression)

2011-09-20 Thread Martin v. Loewis
> Is it just that nobody's implemented it, or is there a good reason for
> avoiding offering this sort of thing?

I've been considering to implement killing threads several times for the
last 15 years (I think about it once every year), and every time
I give up because it's too complex and just not implementable.

To start with, a simple flag in the thread won't do any good. It will
not cancel blocking system calls, so people will complain that the
threads they meant to cancel continue to run forever. Instead, you
have to use some facility to interrupt blocking system calls. You
then have to convince callers of those blocking system calls not to
retry when they see that the first attempt to call it was interrupted.
And so on.

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


Re: About MAKE_FUNCTION opcode in Python 3

2011-09-20 Thread Terry Reedy

On 9/20/2011 5:56 PM, Eric Snow wrote:

On Tue, Sep 20, 2011 at 1:59 PM, Arnaud Delobelle  wrote:

Since Python 3.0 we have keyword only arguments in functions (see PEP
3102).  Looking at the documentation for the dis module (where opcodes
are documented), I see the following for MAKE_FUNCTION [1]

"""
MAKE_FUNCTION(argc)
Pushes a new function object on the stack. TOS is the code associated
with the function. The function object is defined to have argc default
parameters, which are found below TOS.
"""

No mention of default values for keyword only arguments.  Now let's
try (Python 3.2):


def foo():

...   def bar(x, y, *args, z=1, t=2, **kwargs): pass
...

dis.dis(foo)

  2   0 LOAD_CONST   1 ('z')
  3 LOAD_CONST   2 (1)
  6 LOAD_CONST   3 ('t')
  9 LOAD_CONST   4 (2)
 12 LOAD_CONST   5 (", line 2>)
 15 MAKE_FUNCTION  512
 18 STORE_FAST   0 (bar)
 21 LOAD_CONST   0 (None)
 24 RETURN_VALUE

MAKE_FUNCTION has an argc of 512.  So it seems that since Python 3.0:

* the number of default values for normal arguments is argc&  0xFF
* the number of default values for keyword only arguments is argc>>  8

Can anyone confirm this?  I can then open a ticket on bugs.python.org


You're mostly right.

http://hg.python.org/cpython/file/default/Python/ceval.c#l2684

   2684 int posdefaults = oparg&  0xff;
   2685 int kwdefaults = (oparg>>8)&  0xff;
   2686 int num_annotations = (oparg>>  16)&  0x7fff;



[1] http://docs.python.org/dev/library/dis.html#opcode-MAKE_FUNCTION


I agree that doc should be updated for 3.x. Please suggest a new working 
if you can, as well as copying the source snippet above. Add me 
terry.reedy as nosy.


--
Terry Jan Reedy

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


Re: Operator commutativity

2011-09-20 Thread Chris Angelico
On Wed, Sep 21, 2011 at 11:07 AM, Steven D'Aprano
 wrote:
> If the right-hand argument is a subclass of the left-hand argument, AND also
> defines __radd__ directly rather than inheriting it, then its __radd__
> method is called before the left-hand argument's __add__ method.
>
> which strikes me as a strangely specific and not very useful rule. I suspect
> it might be an accident of implementation rather than a deliberate feature.

It makes sense, but in a weird way. (Maybe I understand it because I'm
half Dutch? heh) It means that a subclass can override addition for
itself - presumably, it'll define __add__ and __radd__ both - and that
the only time you'd get a false positive (where a function thinks it
can handle the addition but actually there's a better one) is when
it's a subclass. So this is probably correct behaviour, but it's a
fairly weird and esoteric rule.

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


Re: Operator commutativity

2011-09-20 Thread Steven D'Aprano
Ethan Furman wrote:

> Peter Pearson wrote:
>> On Mon, 19 Sep 2011 05:48:07 -0700, Ethan Furman 
>> wrote:
>> [snip]
>>> Also, if the right-hand operand is a subclass of the left-hand operand
>>> then Python will try right-hand_operand.__radd__ first.
>> 
>> I don't think it works that way for me:
>> 
>> Python 2.6.5 (r265:79063, Apr 16 2010, 13:09:56)
> class C1():
>> ... def __add__(self, other):
>> ... print "C1.__add__()"
>> ...
> class C2(C1):
>> ... def __radd__(self, other):
>> ... print "C2.__radd__()"
>> ...
> C1() + C2()
>> C1.__add__()
> 
> Oh, it has to be a new-style class.  Sorry.

Even so, I still don't think it quite works the way you have suggested.



class C1(object):
def __add__(self, other):
return "__add__(%s, %s)" % (self, other)
def __radd__(self, other):
return "__radd__(%s, %s)" % (self, other)

class C2(C1):
pass


>>> C1() + C2()
'__add__(<__main__.C1 object at 0xb7f79b0c>, <__main__.C2 object at
0xb7f79b6c>)'


I've tried that in both Python 2.6 and 3.2 and get the same result.

However, consider this slight variation:

class D1(object):
def __add__(self, other):
return "__add__(%s, %s)" % (self, other)

class D2(D1):
def __radd__(self, other):
return "__radd__(%s, %s)" % (self, other)


>>> D1() + D2()
'__radd__(<__main__.D2 object at 0xb7c2c36c>, <__main__.D1 object at
0xb7c2cb0c>)'



After playing around with various combinations of C1, C2, D1 and D2, it
seems to me that the rule is:

If the right-hand argument is a subclass of the left-hand argument, AND also
defines __radd__ directly rather than inheriting it, then its __radd__
method is called before the left-hand argument's __add__ method.


which strikes me as a strangely specific and not very useful rule. I suspect
it might be an accident of implementation rather than a deliberate feature.



-- 
Steven

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


Re: About MAKE_FUNCTION opcode in Python 3

2011-09-20 Thread Eric Snow
On Tue, Sep 20, 2011 at 1:59 PM, Arnaud Delobelle  wrote:
> Since Python 3.0 we have keyword only arguments in functions (see PEP
> 3102).  Looking at the documentation for the dis module (where opcodes
> are documented), I see the following for MAKE_FUNCTION [1]
>
> """
> MAKE_FUNCTION(argc)
> Pushes a new function object on the stack. TOS is the code associated
> with the function. The function object is defined to have argc default
> parameters, which are found below TOS.
> """
>
> No mention of default values for keyword only arguments.  Now let's
> try (Python 3.2):
>
 def foo():
> ...   def bar(x, y, *args, z=1, t=2, **kwargs): pass
> ...
 dis.dis(foo)
>  2           0 LOAD_CONST               1 ('z')
>              3 LOAD_CONST               2 (1)
>              6 LOAD_CONST               3 ('t')
>              9 LOAD_CONST               4 (2)
>             12 LOAD_CONST               5 ( 0x1005ec8b0, file "", line 2>)
>             15 MAKE_FUNCTION          512
>             18 STORE_FAST               0 (bar)
>             21 LOAD_CONST               0 (None)
>             24 RETURN_VALUE
>
> MAKE_FUNCTION has an argc of 512.  So it seems that since Python 3.0:
>
> * the number of default values for normal arguments is argc & 0xFF
> * the number of default values for keyword only arguments is argc >> 8
>
> Can anyone confirm this?  I can then open a ticket on bugs.python.org

You're mostly right.

http://hg.python.org/cpython/file/default/Python/ceval.c#l2684

  2684 int posdefaults = oparg & 0xff;
  2685 int kwdefaults = (oparg>>8) & 0xff;
  2686 int num_annotations = (oparg >> 16) & 0x7fff;

-eric

>
> [1] http://docs.python.org/dev/library/dis.html#opcode-MAKE_FUNCTION
>
> --
> Arnaud
> --
> http://mail.python.org/mailman/listinfo/python-list
>
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: HTMLParser and non-ascii html pages

2011-09-20 Thread Peter Otten
Yaşar Arabacı wrote:

> I am using a simple sublclass of HTMLParser like this:
> 
> class LinkCollector(HTMLParser):
> 
> def reset(self):
> self.links = []
> HTMLParser.reset(self)
> 
> def handle_starttag(self,tag,attr):
> if tag in ("a","link"):
> key = "href"
> elif tag in ("img","script"):
> key = "src"
> else:
> return
> self.links.extend([v for k,v in attr if k == key])
> 
> This gives following error:
> 
> Traceback (most recent call last):
>   File "downloader.py", line 209, in 
> if __name__ == "__main__": main()
>   File "downloader.py", line 201, in main
> link_collect.feed(response)
>   File "C:\Python27\lib\HTMLParser.py", line 108, in feed
> self.goahead(0)
>   File "C:\Python27\lib\HTMLParser.py", line 148, in goahead
> k = self.parse_starttag(i)
>   File "C:\Python27\lib\HTMLParser.py", line 252, in parse_starttag
> attrvalue = self.unescape(attrvalue)
>   File "C:\Python27\lib\HTMLParser.py", line 393, in unescape
> return re.sub(r"&(#?[xX]?(?:[0-9a-fA-F]+|\w{1,8}));", replaceEntities,
> s)
>   File "C:\Python27\lib\re.py", line 151, in sub
> return _compile(pattern, flags).sub(repl, string, count)
> UnicodeDecodeError: 'ascii' codec can't decode byte 0xc3 in position 13:
> ordinal not in range(128)

Trying to reproduce the error:

>>> from HTMLParser import HTMLParser
>>> class P(HTMLParser):
... def handle_starttag(self, tag, attrs):
... key, value = attrs[0]
... print tag, key, "=", value
...
>>> def feed(s):
... P().feed(s)
...
>>> feed("")
a href = yadda
>>> feed("")
a href = ä yadda
>>> feed("")
Traceback (most recent call last):
  File "", line 1, in 
  File "", line 2, in feed
  File "/usr/local/lib/python2.7/HTMLParser.py", line 108, in feed
self.goahead(0)
  File "/usr/local/lib/python2.7/HTMLParser.py", line 148, in goahead
k = self.parse_starttag(i)
  File "/usr/local/lib/python2.7/HTMLParser.py", line 252, in parse_starttag
attrvalue = self.unescape(attrvalue)
  File "/usr/local/lib/python2.7/HTMLParser.py", line 390, in unescape
return re.sub(r"&(#?[xX]?(?:[0-9a-fA-F]+|\w{1,8}));", replaceEntities, 
s)
  File "/usr/local/lib/python2.7/re.py", line 151, in sub
return _compile(pattern, flags).sub(repl, string, count)
UnicodeDecodeError: 'ascii' codec can't decode byte 0xc3 in position 1: 
ordinal not in range(128)

It seems that the exception is triggered by an attribute value that contains 
both entities and non-ascii bytes.

>>> feed(u"")
a href = ä ä

> Rest of the code available as attachment. Does anyone know how to solve
> this?

The documentation doesn't mention unicode, but it seems to work anyway:

>>> feed(u"")
a href = ä ä

So one fix might be to convert the data to unicode before passing it to the 
HTMLParser. 

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


About MAKE_FUNCTION opcode in Python 3

2011-09-20 Thread Arnaud Delobelle
Since Python 3.0 we have keyword only arguments in functions (see PEP
3102).  Looking at the documentation for the dis module (where opcodes
are documented), I see the following for MAKE_FUNCTION [1]

"""
MAKE_FUNCTION(argc)
Pushes a new function object on the stack. TOS is the code associated
with the function. The function object is defined to have argc default
parameters, which are found below TOS.
"""

No mention of default values for keyword only arguments.  Now let's
try (Python 3.2):

>>> def foo():
...   def bar(x, y, *args, z=1, t=2, **kwargs): pass
...
>>> dis.dis(foo)
  2   0 LOAD_CONST   1 ('z')
  3 LOAD_CONST   2 (1)
  6 LOAD_CONST   3 ('t')
  9 LOAD_CONST   4 (2)
 12 LOAD_CONST   5 (", line 2>)
 15 MAKE_FUNCTION  512
 18 STORE_FAST   0 (bar)
 21 LOAD_CONST   0 (None)
 24 RETURN_VALUE

MAKE_FUNCTION has an argc of 512.  So it seems that since Python 3.0:

* the number of default values for normal arguments is argc & 0xFF
* the number of default values for keyword only arguments is argc >> 8

Can anyone confirm this?  I can then open a ticket on bugs.python.org

[1] http://docs.python.org/dev/library/dis.html#opcode-MAKE_FUNCTION

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


Re: Why are my queues empty even though there are items in it?

2011-09-20 Thread Kayode Odeyemi
On Tue, Sep 20, 2011 at 7:42 PM, MRAB  wrote:

> On 20/09/2011 19:13, Kayode Odeyemi wrote:
>
> def transaction_queue(queue, item, timeout, block=False):
>> queue = multiprocessing.Queue()
>>
>
> This line creates a new empty queue, hiding the one which was passed in.


Removed.

>
>
>  if item is not {} and timeout is not 0:
>>
>
> `not {}` has the value True, so `item is not {}` means `item is True`.
> The `is` checks for identity, not equality, so this is true only if `item`
> actually has the value True or 1 (and this is an implementation-dependent
> behaviour).


changed to:

if item != {} and timeout != None

>
>
>  print "Items are {0}".format(item)
>> for i in range(len(item)):
>>
>
> You're trying to get as many items from the queue as there are items in
> the dict `item`, which looks wrong to me.
>
>
>  try:
>> d = queue.get(block)
>> print d
>> except Empty:
>> print 'Fees queue empty at %s' % (i)
>> else:
>> return process(q=queue, i=d, t=timeout)
>> else:
>> print 'No item in the queue to get'
>>
>> Effected this. d has a result in the output (output below). So obviously,
it is not empty.

What I'm trying to do in the initial code is to have all items in the queue
evaluated at the
same time such that each leaves the queue for processing when its timeout
period has elapse.

>
>> Q: Why are my queues empty even though there are items in it?
>>
>>  What makes you think there are items in it? You print out `item`, which
> is not the queue.
>

I've made these changes but the queue still appears empty

None put to queue
{u'status': u'pending', u'timeout': 3}
empty queue
None put to queue
{u'status': u'pending', u'timeout': 5}
empty queue

Why 'empty queue' in process()? Also, why "None put to queue" since d
confirms
the items in the queue?


-- 
Odeyemi 'Kayode O.
http://www.sinati.com. t: @charyorde
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Why are my queues empty even though there are items in it?

2011-09-20 Thread MRAB

On 20/09/2011 20:09, Zero Piraeus wrote:

:

On 20 September 2011 14:42, MRAB  wrote:

On 20/09/2011 19:13, Kayode Odeyemi wrote:


 if item is not {} and timeout is not 0:


`not {}` has the value True, so `item is not {}` means `item is True`.
The `is` checks for identity, not equality, so this is true only if `item`
actually has the value True or 1 (and this is an implementation-dependent
behaviour).


Nitpick: "is not" is an operator:


"foo" is not {}

True

"foo" is (not {})

False


Oops! True.

However, it still doesn't do what the OP intended:

>>> {} is {}
False
>>> {} is not {}
True
--
http://mail.python.org/mailman/listinfo/python-list


Re: Operator commutativity

2011-09-20 Thread Ethan Furman

Peter Pearson wrote:

On Mon, 19 Sep 2011 05:48:07 -0700, Ethan Furman  wrote:
[snip]
Also, if the right-hand operand is a subclass of the left-hand operand 
then Python will try right-hand_operand.__radd__ first.


I don't think it works that way for me:

Python 2.6.5 (r265:79063, Apr 16 2010, 13:09:56) 

class C1():

... def __add__(self, other):
... print "C1.__add__()"
... 

class C2(C1):

... def __radd__(self, other):
... print "C2.__radd__()"
... 

C1() + C2()

C1.__add__()


Oh, it has to be a new-style class.  Sorry.

~Ethan~

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


Re: Why are my queues empty even though there are items in it?

2011-09-20 Thread Zero Piraeus
:

On 20 September 2011 14:42, MRAB  wrote:
> On 20/09/2011 19:13, Kayode Odeyemi wrote:
>>
>>     if item is not {} and timeout is not 0:
>
> `not {}` has the value True, so `item is not {}` means `item is True`.
> The `is` checks for identity, not equality, so this is true only if `item`
> actually has the value True or 1 (and this is an implementation-dependent
> behaviour).

Nitpick: "is not" is an operator:

>>> "foo" is not {}
True
>>> "foo" is (not {})
False

 -[]z.
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Dynamically Cause A Function To Return

2011-09-20 Thread Arnaud Delobelle
On 20 September 2011 00:13, Jordan Evans  wrote:
> I want dynamically place the 'return' statement in a function via user input
> or achieve the same through some other means.  If some other means, the user
> must be able initiate this at runtime during a raw_input().  This is what I
> have so far, this would be an awesome command line debugging tool if I can
> get it to work.
>
> def b(label="", *args):
>     """Used to create breaks for debugging.  Will break the function or
> continue the function it is place within based on user input.  Has as a
> input loop for querying variables and executing code before a break or a
> continue."""
>     print label+":",
>     for arg in args:
>         print str(arg),
>     if len(args):
>         print
>     x = ""
>     while x != ".":
>         command = raw_input()
>         try:
>             exec command
>         except:
>             pass
>

I don't really understand your dynamic return idea, but this reminds
me of some debugging function I wrote some time ago.  It pauses
execution and you can evaluate expression in the current stack frame
and any of its parents using the following syntax:

 executes  in the stack frame where pause() was inserted
. executes it in the parent of this stack frame
.. in the grandparent (etc...)

? shows all accessible stack frames

def pause():
import sys, inspect, re
f = None
print "\n*** Entering pause mode (EOF to resume)"
try:
while True:
try:
c = raw_input('pause> ')
if c == '?':
for i, fi in enumerate(inspect.stack()[1:]):
print ('%s File "%s", line %s, in %s' %
   ('.'*i, fi[1], fi[2], fi[3]))
continue
dots = re.match(r'\.*', c)
back = 0
if dots:
back = len(dots.group())
f = sys._getframe(back+1)
code_name = f.f_code.co_name
cmd = c[back:]
val = cmd and eval(cmd, f.f_globals, f.f_locals)
print "(%s) %r" % (code_name, val)
except Exception, e:
if isinstance(e, EOFError):
del f
raise
print "%s: %s" % (type(e).__name__, e)
except EOFError:
pass
finally:
print "\n*** Leaving pause mode"


# Simple example of 'pause' in action:

>>> def g(x):
... b = 5
... pause()
...
>>> def f(x, y):
... z = 2
... g(x)
... print "And we carry on..."
...
>>> f('spam', [4, 2])

*** Entering pause mode (EOF to resume)
pause> ?
 File "", line 3, in g
. File "", line 3, in f
.. File "", line 1, in 
pause> b
(g) 5
pause> b+1
(g) 6
pause> .z
(f) 2
pause> .y
(f) [4, 2]
pause> .x
(f) 'spam'
pause> ..len
() 
pause> ^D
*** Leaving pause mode
And we carry on...
>>>

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


Re: Why are my queues empty even though there are items in it?

2011-09-20 Thread MRAB

On 20/09/2011 19:13, Kayode Odeyemi wrote:

Hello friends,

I'm writing some Python app that makes use of the multiprocessing and
Queue api to perform
transaction jobs.

My problem is that, the queue always report empty even though I can
confirm that there are items in it.

Below is the code I'm working with:


[snip]


def transaction_queue(queue, item, timeout, block=False):
 queue = multiprocessing.Queue()


This line creates a new empty queue, hiding the one which was passed in.


 if item is not {} and timeout is not 0:


`not {}` has the value True, so `item is not {}` means `item is True`.
The `is` checks for identity, not equality, so this is true only if `item`
actually has the value True or 1 (and this is an implementation-dependent
behaviour).


 print "Items are {0}".format(item)
 for i in range(len(item)):


You're trying to get as many items from the queue as there are items in
the dict `item`, which looks wrong to me.


 try:
 d = queue.get(block)
 print d
 except Empty:
 print 'Fees queue empty at %s' % (i)
 else:
 return process(q=queue, i=d, t=timeout)
 else:
 print 'No item in the queue to get'

A JSON POST from CURL calls put_in_queue callback:

  curl -v -H "Content-Type: application/json" -X POST --data
'fees={"fees":{"status":"pending","timeout":5},
"hostel":{"status":"pending","timeout": 3}}'
http://127.0.0.1:8000/transaction/add/ > post_data.txt

At code run, the output is:

{u'status': u'pending', u'timeout': 3} put to queue
Items are {u'status': u'pending', u'timeout': 3}
Fees queue empty at 0
Fees queue empty at 1
{u'status': u'pending', u'timeout': 5} put to queue
Items are {u'status': u'pending', u'timeout': 5}
Fees queue empty at 0
Fees queue empty at 1

Q: Why are my queues empty even though there are items in it?


What makes you think there are items in it? You print out `item`, which
is not the queue.


Thanks you for the suggestions and answers.


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


Re: Dynamically Cause A Function To Return

2011-09-20 Thread Kayode Odeyemi
On Tue, Sep 20, 2011 at 12:13 AM, Jordan Evans wrote:

> I want dynamically place the 'return' statement in a function via user
> input or achieve the same through some other means.  If some other means,
> the user must be able initiate this at runtime during a raw_input().  This
> is what I have so far, this would be an awesome command line debugging tool
> if I can get it to work.
>
> def b(label="", *args):
> """Used to create breaks for debugging.  Will break the function or
> continue the function it is place within based on user input.  Has as a
> input loop for querying variables and executing code before a break or a
> continue."""
> print label+":",
> for arg in args:
> print str(arg),
> if len(args):
> print
> x = ""
> while x != ".":
> command = raw_input()
> try:
> exec command
> except:
> pass
>

It is also possible to create the function in your loop.

for arg in args:
   def f(args):
   return args # You can make this a callback function if you want


-- 
Odeyemi 'Kayode O.
http://www.sinati.com. t: @charyorde
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Dynamically Cause A Function To Return

2011-09-20 Thread Chris Angelico
On Tue, Sep 20, 2011 at 9:13 AM, Jordan Evans  wrote:
> I want dynamically place the 'return' statement in a function via user input
> or achieve the same through some other means.  If some other means, the user
> must be able initiate this at runtime during a raw_input().  This is what I
> have so far, this would be an awesome command line debugging tool if I can
> get it to work.

Not entirely sure what you're trying to accomplish here. You want to
pepper your code with calls to b() and then have the user be able to
abort the function from there? If so, I can think of two ways:

1) Have b() raise an exception, which you catch at a high level.
That'd unwind the stack all the way to the top, which may or may not
be what you want. Pretty easy to do though.
2) Pepper your code with:
if b(): return
and then have b return 0 normally and 1 when the user asks to abort.

This isn't truly an interactive debugger, though it does potentially
have much value.

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


Re: Dynamically Cause A Function To Return

2011-09-20 Thread Kayode Odeyemi
On Tue, Sep 20, 2011 at 12:13 AM, Jordan Evans wrote:

> I want dynamically place the 'return' statement in a function via user
> input or achieve the same through some other means.  If some other means,
> the user must be able initiate this at runtime during a raw_input().  This
> is what I have so far, this would be an awesome command line debugging tool
> if I can get it to work.
>
> def b(label="", *args):
> """Used to create breaks for debugging.  Will break the function or
> continue the function it is place within based on user input.  Has as a
> input loop for querying variables and executing code before a break or a
> continue."""
> print label+":",
> for arg in args:
> print str(arg),
> if len(args):
> print
> x = ""
> while x != ".":
> command = raw_input()
> try:
> exec command
> except:
> pass
>

Isn't this "call by reference" ?

see
http://docs.python.org/faq/programming.html?highlight=kwargs#how-do-i-write-a-function-with-output-parameters-call-by-reference



-- 
Odeyemi 'Kayode O.
http://www.sinati.com. t: @charyorde
-- 
http://mail.python.org/mailman/listinfo/python-list


Why are my queues empty even though there are items in it?

2011-09-20 Thread Kayode Odeyemi
Hello friends,

I'm writing some Python app that makes use of the multiprocessing and Queue
api to perform
transaction jobs.

My problem is that, the queue always report empty even though I can confirm
that there are items in it.

Below is the code I'm working with:

import multiprocessing
from multiprocessing import Process, Queue
from Queue import Empty

def process(**kwargs):
q = kwargs['q']
# Don't start the process if there's no item in queue
if q.empty():
print 'empty queue'
multiprocessing.Process() # Process defaults to None
else:
p = multiprocessing.Process(target=transaction_queue, args=(kwargs))
p.start()

def put_in_queue(items={}):
print items

q = multiprocessing.Queue()

try:
for k,v in items.iteritems():
data = v #data is a dict
timeout = data.get(u'timeout')

# Put the transaction item in the queue at a specific timeout
# period
q.put(data, False, timeout)
print "{0} put to queue".format(data)
transaction_queue(q, data, timeout, False)

except (KeyError, AttributeError, ValueError):
print 'Incorrect data format'

def transaction_queue(queue, item, timeout, block=False):
queue = multiprocessing.Queue()
if item is not {} and timeout is not 0:
print "Items are {0}".format(item)
for i in range(len(item)):
try:
d = queue.get(block)
print d
except Empty:
print 'Fees queue empty at %s' % (i)
else:
return process(q=queue, i=d, t=timeout)
else:
print 'No item in the queue to get'

A JSON POST from CURL calls put_in_queue callback:

 curl -v -H "Content-Type: application/json" -X POST --data
'fees={"fees":{"status":"pending","timeout":5},
"hostel":{"status":"pending","timeout": 3}}'
http://127.0.0.1:8000/transaction/add/ > post_data.txt

At code run, the output is:

{u'status': u'pending', u'timeout': 3} put to queue
Items are {u'status': u'pending', u'timeout': 3}
Fees queue empty at 0
Fees queue empty at 1
{u'status': u'pending', u'timeout': 5} put to queue
Items are {u'status': u'pending', u'timeout': 5}
Fees queue empty at 0
Fees queue empty at 1

Q: Why are my queues empty even though there are items in it?

Thanks you for the suggestions and answers.

-- 
Odeyemi 'Kayode O.
http://www.sinati.com. t: @charyorde
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Operator commutativity

2011-09-20 Thread Peter Pearson
On Mon, 19 Sep 2011 05:48:07 -0700, Ethan Furman  wrote:
[snip]
> Also, if the right-hand operand is a subclass of the left-hand operand 
> then Python will try right-hand_operand.__radd__ first.

I don't think it works that way for me:

Python 2.6.5 (r265:79063, Apr 16 2010, 13:09:56) 
>>> class C1():
... def __add__(self, other):
... print "C1.__add__()"
... 
>>> class C2(C1):
... def __radd__(self, other):
... print "C2.__radd__()"
... 
>>> C1() + C2()
C1.__add__()
>>> 


-- 
To email me, substitute nowhere->spamcop, invalid->net.
-- 
http://mail.python.org/mailman/listinfo/python-list


HTMLParser and non-ascii html pages

2011-09-20 Thread Yaşar Arabacı
Hi,

I am using a simple sublclass of HTMLParser like this:

class LinkCollector(HTMLParser):

def reset(self):
self.links = []
HTMLParser.reset(self)

def handle_starttag(self,tag,attr):
if tag in ("a","link"):
key = "href"
elif tag in ("img","script"):
key = "src"
else:
return
self.links.extend([v for k,v in attr if k == key])

This gives following error:

Traceback (most recent call last):
  File "downloader.py", line 209, in 
if __name__ == "__main__": main()
  File "downloader.py", line 201, in main
link_collect.feed(response)
  File "C:\Python27\lib\HTMLParser.py", line 108, in feed
self.goahead(0)
  File "C:\Python27\lib\HTMLParser.py", line 148, in goahead
k = self.parse_starttag(i)
  File "C:\Python27\lib\HTMLParser.py", line 252, in parse_starttag
attrvalue = self.unescape(attrvalue)
  File "C:\Python27\lib\HTMLParser.py", line 393, in unescape
return re.sub(r"&(#?[xX]?(?:[0-9a-fA-F]+|\w{1,8}));", replaceEntities,
s)
  File "C:\Python27\lib\re.py", line 151, in sub
return _compile(pattern, flags).sub(repl, string, count)
UnicodeDecodeError: 'ascii' codec can't decode byte 0xc3 in position 13:
ordinal not in range(128)

Rest of the code available as attachment. Does anyone know how to solve
this?

-- 
http://yasar.serveblog.net/


downloader.py
Description: Binary data
-- 
http://mail.python.org/mailman/listinfo/python-list


techology

2011-09-20 Thread jasmine jasmine
http://123maza.com/48/doll789/
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: PyEval_EvalCodeEx return value

2011-09-20 Thread John Pinner
On Sep 20, 11:34 am, Mateusz Loskot  wrote:
> Hi,
>
> I'm trying to dig out details about what exactly is the return
> value the of PyEval_EvalCodeEx function in Python 3.x
> The documentation is sparse, unfortunately.
>
> Perhaps I'm looking at wrong function.
> My aim is simple, I need to execute Python code using Python interpreter
> embedded in my C++ application.
> The Python code is a simple script that always returns single value.
> For example:
>
> #! /usr/bin/env python
> def foo(a, b):
>     return a + b
> f = foo(2, 3)
>
> But, f can be of different type for different script: one returns
> numeric value, another returns a sequence, so the type is not
> possible to be determined in advance.
>
> I know how to capture Python stdout/stderr.
>
> I also know how to access the "f" attribute using
> PyObject_GetAttrString and then I can convert "f" value to C++ type
> depending on PyObject type.
>
> However, I guess there shall be a way to access "f" value
> directly from PyEval_EvalCode return object:
>
> PyObject* evalRet = ::PyEval_EvalCode(...);
>
> But, I can't find any details what the "evalRet" actually is.

I assume that you have read the documentation at 
http://docs.python.org/c-api/veryhigh.html,
and I agree that it's sparse, but the entry for the next entry,
PyEval_EvalCodeEx, tells you a little more, as does that for
PyEval_EvalFrameEx.

Obviously, it's returning a pointer to a PyObject, and Looking at the
source code, that may be NULL, to throw an exception, or an execution
frame, or a generator,etc, etc, depending on the code it's been given.
I guess that you should be able to inspect the PyObject to see what it
is.

What I'm wondering is, why are you eval-ing code ? A favourite device
of C programmers coming to Python (and mea culpa in the past), it's
fraught with potential problems and even security risks, and is
difficult to debug, and maybe you should be using introspection
instead.

And maybe you should be working in Python, and not C++ at this point,
do the dynamic stuff in the language best suited, and then return to C+
+ when needed.

Best wishes,

John
--

> Any pointers would be helpful.
>
> Best regards,
> --
> Mateusz Loskot,http://mateusz.loskot.net
> Charter Member of OSGeo,http://osgeo.org
> Member of ACCU,http://accu.org

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


Re: Python bug in Windows 8--report now, or later?

2011-09-20 Thread Kevin Walzer

On 9/20/11 8:32 AM, Alec Taylor wrote:

I can confirm that os.mkdir('C:\\h') and os.path.exists('C:\\h') work
on Windows 8 Dev x64.


OK--looks like I will need to do a bit more digging into my own code. 
Thanks for clarifying.


--
Kevin Walzer
Code by Kevin
http://www.codebykevin.com
--
http://mail.python.org/mailman/listinfo/python-list


Re: Python bug in Windows 8--report now, or later?

2011-09-20 Thread Alec Taylor
I can confirm that os.mkdir('C:\\h') and os.path.exists('C:\\h') work
on Windows 8 Dev x64.

On Tue, Sep 20, 2011 at 6:40 AM, Andrew Berg  wrote:
> On 2011.09.19 09:00 AM, Brian Curtin wrote:
>> You said "the application does not create an app folder in the user's
>> 'application data' directory" -- what does this mean, or rather, what
>> is the specific folder you're expecting to have? If Python can't
>> create the directory but you can do it manually, there may be some
>> permission or access differences new to Windows 8. What does "echo
>> %APPDATA%" give you?
> I booted up the Win8 dev preview in a VM and os.environ['appdata'] gives
> me the same result it would for Windows 7. Perhaps the problem lies in
> the os.path.exists() call (that is, Win8 and Win7 react differently to
> this call). I personally would try to create the directory and then
> catch either a WindowsError (error 183) or an OSError (error 17) if the
> directory already exists. I might play around with this later and post
> some results.
>
> --
> CPython 3.2.2 | Windows NT 6.1.7601.17640 | Thunderbird 6.0.2
> PGP/GPG Public Key ID: 0xF88E034060A78FCB
> --
> http://mail.python.org/mailman/listinfo/python-list
>
-- 
http://mail.python.org/mailman/listinfo/python-list


PyEval_EvalCodeEx return value

2011-09-20 Thread Mateusz Loskot

Hi,

I'm trying to dig out details about what exactly is the return
value the of PyEval_EvalCodeEx function in Python 3.x
The documentation is sparse, unfortunately.

Perhaps I'm looking at wrong function.
My aim is simple, I need to execute Python code using Python interpreter 
embedded in my C++ application.

The Python code is a simple script that always returns single value.
For example:

#! /usr/bin/env python
def foo(a, b):
   return a + b
f = foo(2, 3)

But, f can be of different type for different script: one returns 
numeric value, another returns a sequence, so the type is not

possible to be determined in advance.

I know how to capture Python stdout/stderr.

I also know how to access the "f" attribute using
PyObject_GetAttrString and then I can convert "f" value to C++ type
depending on PyObject type.

However, I guess there shall be a way to access "f" value
directly from PyEval_EvalCode return object:

PyObject* evalRet = ::PyEval_EvalCode(...);

But, I can't find any details what the "evalRet" actually is.

Any pointers would be helpful.

Best regards,
--
Mateusz Loskot, http://mateusz.loskot.net
Charter Member of OSGeo, http://osgeo.org
Member of ACCU, http://accu.org
--
http://mail.python.org/mailman/listinfo/python-list


red bull hats of http:www.discounthats.net

2011-09-20 Thread Bertha Jonanna
Welcome to the http:www.discounthats.net.The http:www.discounthats.net
is a promotional shop from the red bull hats, monster energy hats,
snapback hats.red bull hats: http:www.discounthats.net red bull
hats: http://www.discounthats.net/category-2-b0-Red-Bull-Hats.html
monster energy hats: 
http://www.discounthats.net/category-3-b0-Monster-Energy-Hats.html
snapback hats: http://www.discounthats.net/category-79-b0-Snapback-Hats.html
our site: www.discounthats.net
-- 
http://mail.python.org/mailman/listinfo/python-list


red bull hats of http:www.discounthats.net

2011-09-20 Thread Bertha Jonanna
Welcome to the http:www.discounthats.net.The http:www.discounthats.net
is a promotional shop from the red bull hats, monster energy hats,
snapback hats.red bull hats: http:www.discounthats.net red bull
hats: http://www.discounthats.net/category-2-b0-Red-Bull-Hats.html
monster energy hats: 
http://www.discounthats.net/category-3-b0-Monster-Energy-Hats.html
snapback hats: http://www.discounthats.net/category-79-b0-Snapback-Hats.html
our site: www.discounthats.net
-- 
http://mail.python.org/mailman/listinfo/python-list