Re: f python?

2012-04-09 Thread Alex Mizrahi

Ok no problem. My sloppiness. After all, my implementation wasn't
portable. So, let's fix it. After a while, discovered there's the
os.sep. Ok, replace "/" to os.sep, done. Then, bang, all hell
went lose. Because, the backslash is used as escape in string, so any
regex that manipulate path got fucked majorly. So, now you need to
find a quoting mechanism.


if os.altsep is not None:
sep_re = '[%s%s]' % (os.sep, os.altsep)
else:
sep_re = '[%s]' % os.sep

But really, you should be ranting about regexps rather than Python.
They're convenient if you know exactly what you want to match, but a
nuisance if you need to generate the expression based upon data which is
only available at run-time (and re.escape() only solves one very specific
problem).


It isn't a problem of regular expressions, but a problem of syntax for 
specification of regular expressions (i.e. them being specified as a 
string).


Common Lisp regex library cl-ppcre allows to specify regex via a parse 
tree. E.g. "(foo[/\\]bar)" becomes


(:REGISTER (:SEQUENCE "foo" (:CHAR-CLASS #\/ #\\) "bar"))

This is more verbose, but totally unambiguous and requires no escaping.

So this definitely is a problem of Python's regex library, and a problem 
of lack of support for nice parse tree representation in code.


cl-ppcre supports both textual perl-compatible regex specification and 
parse tree. I would start with a simple string specification, then when 
shit hits fan I can call cl-ppcre::parse-string to get those parse trees 
and replaces forward slash with back slash. Moreover, I can 
automatically convert regexes:


(defun scan-auto/ (regex target-string)
   (let ((fixed-parse-tree (subst '(:char-class #\/ #\\) '(:char-class #\/)
  (cl-ppcre::parse-string regex)
  :test 'equal)))
(cl-ppcre:scan-to-strings fixed-parse-tree target-string)))


CL-USER> (scan-auto/ "foo[/]bar" "foo\\bar")
"foo\\bar"
#()
--
http://mail.python.org/mailman/listinfo/python-list


Re: f python?

2012-04-09 Thread Andrew Berg
On 4/9/2012 1:52 AM, Chris Angelico wrote:
> I think this will be a real winner, and you
> should team up with Ranting Rick to produce a new operating system and
> Python with this new specification and RULE THE WORLD!
But only after going back to the cage to plan for tomorrow night.

-- 
CPython 3.2.2 | Windows NT 6.1.7601.17640
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: How to resolve circular reference in python C extension?

2012-04-09 Thread Stefan Behnel
罗勇刚(Yonggang Luo) , 09.04.2012 04:28:
> static PyObject *
> Repository_get_index(Repository *self, void *closure)
> {
> int err;
> git_index *index;
> Index *py_index;
> 
> assert(self->repo);
> 
> if (self->index == NULL) {
> err = git_repository_index(&index, self->repo);
> if (err < 0)
> return Error_set(err);
> 
> py_index = PyObject_GC_New(Index, &IndexType);
> if (!py_index) {
> git_index_free(index);
> return NULL;
> }
> 
> Py_INCREF(self);
> py_index->repo = self;
> py_index->index = index;
> PyObject_GC_Track(py_index);
> self->index = (PyObject*)py_index;
> }
> 
> Py_INCREF(self->index);
> return self->index;
> }

It would have been nice if you had added some explanatory text to this code
dump.

What do you mean by "resolve"?

Stefan

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


Re: pygame.Rect question

2012-04-09 Thread Pekka Karjalainen
On Mon, Apr 9, 2012 at 3:29 AM, Dave Angel  wrote:
> I don't know about pygame, but almost everywhere in the standard
> library, ranges are closed at the begin and open at the end.  For
> example, if you have range(30, 50), there are 20 items, numbered 30
> through 49.  I expect the same will be true for rect.right() and
> rect.bottom().

Your expectation is correct. I'll just point out the part in the
Pygame docs that spells this out.

http://www.pygame.org/docs/ref/rect.html

"The area covered by a Rect does not include the right- and
bottom-most edge of pixels. If one Rect's bottom border is another
Rect's top border (i.e., rect1.bottom=rect2.top), the two meet exactly
on the screen but do not overlap, and rect1.colliderect(rect2) returns
false."

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


Python randomly exits with Linux OS error -9 or -15

2012-04-09 Thread Janis
Hello!

I have this problem with my script exiting randomly with Linux OS
status code -9 (most often) or -15 (also sometimes, but much more
rarely). As far as I understand -9 corresponds to Bad file descriptor
and -15 Block device required.

1) Is there a way how I could find out what exactly causes Python
process to exit?
2) What could be the reason of Python exiting with these status code?

The script is a crawler that crawls several web sites to download web
pages and extract information. Most often it exits after having run
for 2 hours and having downloaded ~24 000 files. Some specific web
sites are more affected than others, i.e., there are other instances
of the script running in parallel that download more pages and
complete normally. That could be related to the speed each page is
returned etc.

I have a try-catch block in the root of the script which works very
well to catch any kind of exceptions, but in these cases either Python
does not catch the exception or fails to log it into MySQL error log
table.

I can not use debugger because the code exits after it has run for an
hour or more. In order to try to catch the exact place I have put
logging after each line in some places. The logging prints to stdout,
to a file and logs the line into MySQL DB. This has revealed that code
may exit upon a random simple lines such as: time.sleep(0.1) - most
often, f = opener.open(request) - also often, but sometimes also such
simple statements as list.add('string').

It can also be that the problem occues and then the script exits upon
attempt to do any output - stdout (i.e. regular print '1'), writing to
file and logging into MySQL. I have changed either of these to be the
first ones in the debug log function, but each time the script did not
fail in between these lines.

The main libraries that are in use: MySQLDB, urllib and urllib2, but
none of them seems to be the direct cause of the problem, there is no
direct call of any of these upon exit. I suspect that this could be
related to some garbadge collecting process.

Versions tried with: Python 2.6, Python 2.7 (and I think it happened
also in Python 2.5, but I'm not sure)

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


Re: Python randomly exits with Linux OS error -9 or -15

2012-04-09 Thread Andrew Berg
On 4/9/2012 5:01 AM, Janis wrote:
> I have this problem with my script exiting randomly with Linux OS
> status code -9 (most often) or -15 (also sometimes, but much more
> rarely). As far as I understand -9 corresponds to Bad file descriptor
> and -15 Block device required.
> 
> 1) Is there a way how I could find out what exactly causes Python
> process to exit?
> 2) What could be the reason of Python exiting with these status code?
> 
> The script is a crawler that crawls several web sites to download web
> pages and extract information. Most often it exits after having run
> for 2 hours and having downloaded ~24 000 files. Some specific web
> sites are more affected than others, i.e., there are other instances
> of the script running in parallel that download more pages and
> complete normally. That could be related to the speed each page is
> returned etc.
Sounds like an issue with either the filesystem (perhaps you ran out of
inodes - entirely possible when running scripts that create tens of
thousands of files) or the disk itself (maybe it's dying).

-- 
CPython 3.2.2 | Windows NT 6.1.7601.17640
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Python randomly exits with Linux OS error -9 or -15

2012-04-09 Thread Alain Ketterlin
Janis  writes:

> I have this problem with my script exiting randomly with Linux OS
> status code -9 (most often) or -15 (also sometimes, but much more
> rarely). As far as I understand -9 corresponds to Bad file descriptor
> and -15 Block device required.

How do you get -9 and -15? Exit status is supposed to be between 0 and
127. With bash, 128+N is also used for processes that terminate on a
signal. At the python level, subprocess.wait() uses negative numbers for
signal-terminated processes. And 9 is SIGKILL and 15 is SIGTERM.

My guess is that your script hits a limit, e.g., number of open files,
or stack-size, or... But of course it's only a guess.

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


Re: How to resolve circular reference in python C extension?

2012-04-09 Thread Yonggang Luo
I means remove the circular references without hurt the code, that is,
these two object will be destroyed at the same time. Even there
creation time is differ

2012/4/9, Stefan Behnel :
> 罗勇刚(Yonggang Luo) , 09.04.2012 04:28:
>> static PyObject *
>> Repository_get_index(Repository *self, void *closure)
>> {
>> int err;
>> git_index *index;
>> Index *py_index;
>>
>> assert(self->repo);
>>
>> if (self->index == NULL) {
>> err = git_repository_index(&index, self->repo);
>> if (err < 0)
>> return Error_set(err);
>>
>> py_index = PyObject_GC_New(Index, &IndexType);
>> if (!py_index) {
>> git_index_free(index);
>> return NULL;
>> }
>>
>> Py_INCREF(self);
>> py_index->repo = self;
>> py_index->index = index;
>> PyObject_GC_Track(py_index);
>> self->index = (PyObject*)py_index;
>> }
>>
>> Py_INCREF(self->index);
>> return self->index;
>> }
>
> It would have been nice if you had added some explanatory text to this code
> dump.
>
> What do you mean by "resolve"?
>
> Stefan
>
> --
> http://mail.python.org/mailman/listinfo/python-list
>

-- 
从我的移动设备发送

 此致
礼
罗勇刚
Yours
sincerely,
Yonggang Luo
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: ordering with duck typing in 3.1

2012-04-09 Thread Neil Cerutti
On 2012-04-07, Jon Clements  wrote:
> Any reason you can't derive from int instead of object? You may
> also want to check out functions.total_ordering on 2.7+

functools.total_ordering

I was temporarily tripped up by the aforementioned documentation,
myself.

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


ast.parse

2012-04-09 Thread Kiuhnm
Is it a known fact that ast.parse doesn't handle line continuations and 
some multi-line expressions?

For instance, he doesn't like
for (x,
 y) in each([1,
 2]):
print(1)
at all.
Is there a workaround besides "repairing" the code on the fly?

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


Re: ast.parse

2012-04-09 Thread Chris Rebert
On Mon, Apr 9, 2012 at 4:53 AM, Kiuhnm
 wrote:
> Is it a known fact that ast.parse doesn't handle line continuations and some
> multi-line expressions?
> For instance, he doesn't like
>    for (x,
>         y) in each([1,
>                     2]):
>        print(1)
> at all.
> Is there a workaround besides "repairing" the code on the fly?

What version of Python are you using? What's the exact error you're
getting? I'm unable to reproduce:

$ python3
Python 3.2.2 (default, Jan 13 2012, 00:07:49)
>>> c = """for (x,
...  y) in each([1,
...  2]):
... print(1)"""
>>>
>>> from ast import parse
>>> parse(c)
<_ast.Module object at 0x101f38350>
>>>

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


Re: Question on Python 3 shell restarting

2012-04-09 Thread Albert W. Hopkins
On Sun, 2012-04-08 at 20:09 +0200, Franck Ditter wrote:
> How may I get a fresh Python shell with Idle 3.2 ?
> I have to run the same modules several times with all
> variables cleared.

Why don't you write your module as a script and pass the variables via
command line like most human beings?


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


Re: Python Gotcha's?

2012-04-09 Thread Neil Cerutti
On 2012-04-08, John Nagle  wrote:
> 6.  Multiple inheritance is a mess.  Especially "super".

Python allows you to get dirty. Super solves a messy problem.

> 10. Python 3 isn't upward compatible with Python 2.

Even minor versions of Python are usually not forward compatible.
In the case of 2 to 3, more help and support than usual is
available: http://docs.python.org/dev/howto/pyporting.html

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


Re: f python?

2012-04-09 Thread Seymour J.
In <20120408114313...@kylheku.com>, on 04/08/2012
   at 07:14 PM, Kaz Kylheku  said:

>Null-terminated strings are infinitely better than the ridiculous
>encapsulation of length + data.

ROTF,LMAO!

>For one thing, if s is a non-empty null terminated string then,
>cdr(s) is also a string representing the rest of that string 
>without the first character,

Are you really too clueless to differentiate between C and LISP?

>Null terminated strings have simplified all kids of text
>manipulation, lexical scanning, and data storage/communication 
>code resulting in immeasurable savings over the years.

Yeah, especially code that needs to deal with lengths and nulls. It's
great for buffer overruns too.

-- 
Shmuel (Seymour J.) Metz, SysProg and JOAT  

Unsolicited bulk E-mail subject to legal action.  I reserve the
right to publicly post or ridicule any abusive E-mail.  Reply to
domain Patriot dot net user shmuel+news to contact me.  Do not
reply to spamt...@library.lspace.org

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


Re: ast.parse

2012-04-09 Thread Irmen de Jong
On 9-4-2012 13:53, Kiuhnm wrote:
> Is it a known fact that ast.parse doesn't handle line continuations and some 
> multi-line
> expressions?
> For instance, he doesn't like
> for (x,
>  y) in each([1,
>  2]):
> print(1)
> at all.
> Is there a workaround besides "repairing" the code on the fly?
> 
> Kiuhnm

What Python version are you using and what is the exact error? It works fine 
here
(Python 2.7.2):

>>> import ast
>>> code="""for (x,
...  y) in each([1,
...  2]):
... print(1)"""
>>> ast.parse(code)
<_ast.Module object at 0x02418A10>


Irmen

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


Re: f python?

2012-04-09 Thread Roy Smith
In article <4f82d3e2$1$fuzhry+tra$mr2...@news.patriot.net>,
 Shmuel (Seymour J.) Metz  wrote:

> >Null terminated strings have simplified all kids of text
> >manipulation, lexical scanning, and data storage/communication 
> >code resulting in immeasurable savings over the years.
> 
> Yeah, especially code that needs to deal with lengths and nulls. It's
> great for buffer overruns too.

I once worked on a C++ project that used a string class which kept a 
length count, but also allocated one extra byte and stuck a null at the 
end of every string.
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Python randomly exits with Linux OS error -9 or -15

2012-04-09 Thread Emile van Sebille

On 4/9/2012 3:47 AM Alain Ketterlin said...

Janis  writes:


I have this problem with my script exiting randomly with Linux OS
status code -9 (most often) or -15 (also sometimes, but much more
rarely).



My guess is that your script hits a limit, e.g., number of open files,
or stack-size, or... But of course it's only a guess.


You might try monitoring /proc/[pid]/fd and friends to see if it looks 
like you'd expect.  I'd suspect you're holding on to file refs 
preventing their being garbage collected...


Emile


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


Re: Python randomly exits with Linux OS error -9 or -15

2012-04-09 Thread Adam Skutt
On Apr 9, 6:47 am, Alain Ketterlin 
wrote:
> Janis  writes:
> > I have this problem with my script exiting randomly with Linux OS
> > status code -9 (most often) or -15 (also sometimes, but much more
> > rarely). As far as I understand -9 corresponds to Bad file descriptor
> > and -15 Block device required.
>
> How do you get -9 and -15? Exit status is supposed to be between 0 and
> 127.

0-255 are perfectly legal in UNIX.  Chances are that something is
interpreting the unsigned integer as a signed integer accidentally.
Of course, without any output, there's no way to know for sure.

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


Re: Python randomly exits with Linux OS error -9 or -15

2012-04-09 Thread Adam Skutt
On Apr 9, 6:01 am, Janis  wrote:
> Hello!
>
> I have this problem with my script exiting randomly with Linux OS
> status code -9 (most often) or -15 (also sometimes, but much more
> rarely). As far as I understand -9 corresponds to Bad file descriptor
> and -15 Block device required.
>

As Alain already said, you're probably hitting a resource limit of
some sort or another and your program is being forcibly terminated as
a result.  You may just need to increase ulimits or system-wide
limits, or you may have a resource leak.  There may be output in the
kernel logs (dmesg) or some /var/log file if this is the case.

> 1) Is there a way how I could find out what exactly causes Python
> process to exit?

Use a debugger or at least turn on core dumps, so you have something
to examine after the crash.  Tracking through the output to figure out
where you crashed in the Python code is difficult, but possible.

> I have a try-catch block in the root of the script which works very
> well to catch any kind of exceptions, but in these cases either Python
> does not catch the exception or fails to log it into MySQL error log
> table.

You need to fix your handler to log the exception to stderr or
similiar, as it will make fixing your application much easier.  It's
virtually impossible for anyone to help you if you don't know exactly
what's going on.

It's also highly likely that your current handler is throwing an
exception while running and masking the original error.  I would
seriously advise taking it out, at least temporarily.  This is why
catch-all handlers tend to be a poor idea, as they're rarely robust in
the cases you didn't consider.

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


Re: ast.parse

2012-04-09 Thread Kiuhnm

On 4/9/2012 14:43, Irmen de Jong wrote:

On 9-4-2012 13:53, Kiuhnm wrote:

Is it a known fact that ast.parse doesn't handle line continuations and some 
multi-line
expressions?
For instance, he doesn't like
 for (x,
  y) in each([1,
  2]):
 print(1)
at all.
Is there a workaround besides "repairing" the code on the fly?

Kiuhnm


What Python version are you using and what is the exact error? It works fine 
here
(Python 2.7.2):


import ast
code="""for (x,

...  y) in each([1,
...  2]):
... print(1)"""

ast.parse(code)

<_ast.Module object at 0x02418A10>


Yes, it works. I was too hasty in blaming ast. Sorry.

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


Re: Question on Python 3 shell restarting

2012-04-09 Thread Miki Tebeka
> How may I get a fresh Python shell with Idle 3.2 ?
Open the configuration panel (Options -> Configure IDLE). Look in the "Keys" 
tab for the shortcut to "restart-shell"

HTH
--
Miki Tebeka 
http://pythonwise.blogspot.com
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Interrupting a blocking function frolm another thread.

2012-04-09 Thread superhac...@gmail.com
On Apr 8, 8:09 pm, Adam Skutt  wrote:
> On Apr 8, 5:52 pm, superhac...@gmail.com wrote:
>
>
>
>
>
>
>
>
>
> > On Sunday, April 8, 2012 3:55:41 PM UTC-5, Adam Skutt wrote:
> > > On Apr 8, 2:45 pm, "superhac...@gmail.com" 
> > > wrote:
> > > > I am using the python module nfqueue-bindings which is a nfqueue
> > > > packet intercepting module.  It uses the following snippet of code to
> > > > start the process:
>
> > > > print "trying to run"
> > > > try:
> > > >      q.try_run()
> > > >      except KeyboardInterrupt, e:
> > > >      print "interrupted"
>
> > > > The q.try_run() method blocks.   I would like to be able to interrupt
> > > > this the same way you can hit control-c to unblock it, but from
> > > > another thread.  Does anyone have any idea on how to do this?  Is
> > > > there some sort of Exception I can generate from another thread that
> > > > would do this?
>
> > > The simplest and most reliable way will be to modify the asynchronous
> > > I/O example 
> > > athttps://www.wzdftpd.net/redmine/projects/nfqueue-bindings/repository/...
> > > to do what you want.  The classical way to accomplish this would be to
> > > create a second I/O descriptor (via os.pipe or similiar) and wait for
> > > that descriptor in the same async I/O loop.  When you want to stop the
> > > loop, send a message (could be a single byte) to the second descriptor
> > > and then respond appropriately in your asynchronous I/O handler.
>
> > > However, simply ignoring the nfqueue socket while doing other
> > > processing might be acceptable too.  I would read the documentation
> > > and ask questions carefully before taking that approach, as it may
> > > lead to lost data or other problems.  The viability of this approach
> > > depends heavily on your application.
>
> > > Adam
>
> > Thanks for the reply Adam.  I am still confused with the example with the 
> > example you linked to.  I have only been working with Python for a couple 
> > of weeks, but I am fluent with C.  Given the example you linked to, are you 
> > saying that this is the run loop for asynchronous comm that a end user of 
> > the module could use?  Or is this something with the nfqueue-bindings 
> > module that I would I have to modify.  I don't recognize the the main loop 
> > in the example.
>
> asyncore is a standard python module for handling asynchronous I/O
> operations (i.e., select/poll operations).  The main loop in that
> example is handled by the 'asyncore.loop()' call, which just calls
> select() in a loop until all channels are closed.  You're looking at
> the standard Python library technique for an asynchronous I/O event
> loop.
>
> asyncore.file_dispatcher enables use of asyncore.loop() with standard
> UNIX FDs.  nfqueue.queue objects provide access to their underlying
> FDs so you can wait on them.  asyncore.loop() is the main loop of the
> application, performing endless select/poll waits and calling
> 'handle_read' whenever the netfilter FD has data for reading.  You can
> find more details about the module in the Python library docs.
>
> Since your code is Linux specific, you can always call os.select() or
> os.poll() if it makes you feel better, but I don't think that's going
> to simplify anything here.
>
> Hopefully that helps you.
>
> Adam

Thanks for the through explanation.  I now know what I need to do.


Thanks again!

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


Re: pygame.Rect question

2012-04-09 Thread Peter Pearson
On Sun, 8 Apr 2012 16:58:01 -0700 (PDT), Scott Siegler wrote:
[snip]
> I set rect.left to 30, rect.top to 30 and rect.width = 20
>
> This works fine.  However, when looking at rect.right() it
> shows that it is equal to 50. I suppose this is equal to
> 30+20.  However, since the first pixel is on location 30,
> wouldn't the 20th pixel be on 49 (not 50)?
>
> Am I missing something here?  It is really confusing me
> when I am doing some collision algorithms.

I'm not a pygame expert, but . . . it would be consistent
with Python's conventions for ranges of indices if
rect.right() were the first pixel *outside* the rectangle.
Of course, one would expect rect.bottom() to work similarly.

If this is the right explanation, it will be useful to
picture indices pointing between pixels, rather than at
pixels.

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


Re: ordering with duck typing in 3.1

2012-04-09 Thread Jon Clements
On Monday, 9 April 2012 12:33:25 UTC+1, Neil Cerutti  wrote:
> On 2012-04-07, Jon Clements  wrote:
> > Any reason you can't derive from int instead of object? You may
> > also want to check out functions.total_ordering on 2.7+
> 
> functools.total_ordering
> 
> I was temporarily tripped up by the aforementioned documentation,
> myself.
> 
> -- 
> Neil Cerutti

Oops. I sent it from a mobile tablet device - I got auto-corrected. But yes, it 
is functools.total_ordering - TY you Neil.

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


Re: f python?

2012-04-09 Thread Kaz Kylheku
On 2012-04-09, Shmuel Metz  wrote:
> In <20120408114313...@kylheku.com>, on 04/08/2012
>at 07:14 PM, Kaz Kylheku  said:
>
>>Null-terminated strings are infinitely better than the ridiculous
>>encapsulation of length + data.
>
> ROTF,LMAO!
>
>>For one thing, if s is a non-empty null terminated string then,
>>cdr(s) is also a string representing the rest of that string 
>>without the first character,
>
> Are you really too clueless to differentiate between C and LISP?

In Lisp we can burn a list literal like '(a b c) into ROM, and compute (b c)
without allocating any memory.

Null-terminated C strings do the same thing.

In some Lisp systems, in fact, "CDR coding" was used to save space when
allocating a list all at once. This created something very similar to
a C string: a vector-like object of all the CARs, with a terminating
convention marking the end.

It's logically very similar.

I need not repeat the elegant recursion example for walking a C string.

That example is not possible with the length + data representation.
(Not without breaking the encapsulation and passing the length as a separate
recursion parameter to a recursive routine that works with the raw data part of
the string.)

>>Null terminated strings have simplified all kids of text
>>manipulation, lexical scanning, and data storage/communication 
>>code resulting in immeasurable savings over the years.
>
> Yeah, especially code that needs to deal with lengths and nulls.

To get the length of a string, you call a function, in either representation,
so it is not any more complicated from a coding point of view. The function is,
of course, more expensive if the string is null terminated, but you can code
with awareness of this and not call length wastefully.

If all else was equal (so that the expense of the length operation were
the /only/ issue) then of course the length + data would be better.

However, all else is not equal.

One thing that is darn useful, for instance, is that
p + strlen(p) still points to a string which is length zero, and this
sort of thing is widely exploited in text processing code. e.g.

   size_t digit_prefix_len = strspn(input_string, "0123456789");
   const char *after_digits = input-string + digit_prefix_len;

   if (*after_digits == 0) {
 /* string consists only of digits: nothing after digits */
   } else {
 /* process part after digits */
   }

It's nice that after_digits is a bona-fide string just like input_string,
without any memory allocation being required.

We can lexically analyze a string without ever asking it what its length is,
and as we march down the string, the remaining suffix of that string is always
a string so we can treat it as one, recurse on it, whatever.

Code that needs to deal with null "characters" is manipulating binary data, not
text, and should use a suitable data structure for that.

> It's great for buffer overruns too.

If we scan for a null terminator which is not there, we have a buffer overrun.

If a length field in front of string data is incorrect, we also have a buffer
overrrun.

A pattern quickly emerges here: invalid, corrupt data produced by buggy code
leads to incorrect results, and behavior that is not well-defined!
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: f python?

2012-04-09 Thread Kaz Kylheku
On 2012-04-09, Roy Smith  wrote:
> In article <4f82d3e2$1$fuzhry+tra$mr2...@news.patriot.net>,
>  Shmuel (Seymour J.) Metz  wrote:
>
>> >Null terminated strings have simplified all kids of text
>> >manipulation, lexical scanning, and data storage/communication 
>> >code resulting in immeasurable savings over the years.
>> 
>> Yeah, especially code that needs to deal with lengths and nulls. It's
>> great for buffer overruns too.
>
> I once worked on a C++ project that used a string class which kept a 
> length count, but also allocated one extra byte and stuck a null at the 
> end of every string.

Me too! I worked on numerous C++ projects with such a string template
class.

It was usually called 

  std::basic_string

and came from this header called:

  #include 

which also instantiated it into two flavors under two nicknames:
std::basic_string being introduced as std::string, and
std::basic_string as std::wstring.

This class had a c_str() function which retrieved a null-terminated
string and so most implementations just stored the data that way, but
some of the versions of that class cached the length of the string
to avoid doing a strlen or wcslen operation on the data.
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Python randomly exits with Linux OS error -9 or -15

2012-04-09 Thread Janis
Thank you all for the help! I will need to think a bit about the other
suggestions.

But, Alan, as to this:

> > How do you get -9 and -15? Exit status is supposed to be between 0 and
> > 127.

I have the following code that has caught these:

p = subprocess.Popen([Config.PYTHON_EXE,'Load.py',"%s" % (row[1],)],
bufsize=0, executable=None, stdin=None, stdout=None,
stderr=subprocess.PIPE, preexec_fn=None, close_fds=False, shell=False,
cwd='../Loader/')
stdout, stderr = p.communicate()
if p.returncode != 0:
...

So you say it's SIGKILL and SIGTERM? Then I guess these could be
misleading statuses from those cases when I have terminated the
sessions myself, and when there is the real problem apparently the
caller detected nothing here. SIGKILL and SIGTERM would probably also
explain why there was nothing in stderr.

Thanks,
Janis
P.S. the problem seems to occure also when I call the process directly
and not via Popen from another script.
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Question on Python 3 shell restarting

2012-04-09 Thread Terry Reedy

On 4/9/2012 8:09 AM, Albert W. Hopkins wrote:

On Sun, 2012-04-08 at 20:09 +0200, Franck Ditter wrote:

How may I get a fresh Python shell with Idle 3.2 ?
I have to run the same modules several times with all
variables cleared.


If you have the module in an idle edit window, F5-run restarts after 
saving any changes. On the menu, Shell/Restart Shell does just that and 
gives the shortcut -- Cntl-F6 for me.



Why don't you write your module as a script and pass the variables via
command  line


Perhaps variables are not passed on the command line. Perhaps there are 
no variables to be passed. Perhaps he did not write the modules. Perhaps 
he is running on Windows, where command line usage is discouraged. 
(Command Prompt is hidden away and its operation once found is faulty.) 
Perhaps he is doing this at work where he does not control things. 
Perhaps he is editing and re-running to test, for which Idle is 
excellent, and which I do all the time.


> like most human beings?

Snarkiness not helpful.

--
Terry Jan Reedy

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


ASTRONOMY IN THE QUR'AN

2012-04-09 Thread BV BV
THE QUR'AN AND MODERN SCIENCE
Extracted from the Book
 The Bible, The Qur'an and Science
Maurice Bucaille

ASTRONOMY IN THE QUR'AN

The Qur'an is full of reflections on the heavens. In the preceding
chapter on the Creation, we saw how the plurality of the heavens and
earths was referred to, as well as what the Qur'an calls an
intermediary creation 'between the heavens and the earth': modern
science has verified the latter. The verses referring to the Creation
already contain a broad idea of what is to be found in the heavens,
i.e. of everything outside the earth.

Apart from the verses that specifically describe the Creation, there
are roughly another forty verses in the Qur'an which provide
information on astronomy complementing what has already been given.
Some of them are not much more than reflections on the glory of the
Creator, the Organizer of all the stellar and planetary systems. These
we know to be arranged according to balancing positions whose
stability Newton explained in his law of the mutual attraction of
bodies.

The first verses to be quoted here hardly furnish much material for
scientific analysis: the aim is simply to draw attention to God's
Omnipotence. They must be mentioned however to give a realistic idea
of the way the Qur'anic text described the organization of the
Universe fourteen centuries ago.

These references constitute a new fact of divine Revelation. The
organization of the world is treated in neither the Gospels nor the
Old Testament (except for a few notions whose general inaccuracy we
have already seen in the Biblical description of the Creation). The
Qur'an however deals with this subject in depth. What it describes is
important, but so is what it does not contain. It does not in fact
provide an account of the theories prevalent at the time of the
Revelation that deal with the organization of the celestial world
theories that science was later to show were inaccurate. An example of
this will be given later. This negative consideration must however be
pointed out.

A. General Reflections Concerning the Sky
- Surah 50, verse 6.
Do they not look at the sky above them, how We have built it and
adorned it, and there are no rifts in it
- Surah 31, verse 10:
(God) created the heavens without any pillars that you can see...
- Surah 13, verse 2:
God is the One Who raised the heavens without any pillars that you can
see, then He firmly established Himself on the throne and He subjected
the sun and moon...
These last two verses refute the belief that the vault of the heavens
was held up by pillars, the only things preventing the former from
crushing the earth.
- Surah 55, verse 7:
. the sky (God) raised it
- Surah 22, verse 65:
. (God) holds back the sky from falling on the earth unless by His
leave

It is known how the remoteness of celestial masses at great distance
and in proportion to the magnitude of their mass itself constitutes
the foundation of their equilibrium. The more remote the masses are
the weaker the force is that attracts one to the other. The nearer
they are, the stronger the attraction is that one has to the other:
this is true for the Moon, which is near to the Earth (astronomically
speaking) and exercises an influence by laws of attraction on the
position occupied by the waters of the sea, hence the phenomenon of
the tides. If two celestial bodies come too close to one another,
collision is inevitable. The fact that they are subjected to an order
is the sine qua non for the absence of disturbances.

The subjection of the heavens to divine order is often referred to as
well:
- Surah 23, verse 86: God is speaking to the Prophet.
. Say: Who is lord of the seven heavens and Lord of the tremendous
throne?.
We have already seen how by 'seven heavens' what is meant is not 7,
but an indefinite number of heavens.
- Surah 45, verse 31:
. For you (God) subjected all that is in the heavens and on the earth,
all from Him. Behold! In that are signs for people who reflect.
- Surah 55, verse 5:
. the sun and moon (are subjected) to calculations.
- Surah 6, verse 96:
. (God) appointed the night for rest and the sun and the moon for
reckoning.
- Surah 14, verse 33:
. For you (God) subjected the sun and the moon, both diligently
pursuing their courses. And for you He subjected the night and the
day.

Here one verse completes another: the calculations referred to result
in the regularity of the course described by the heavenly bodies in
question, this is expressed by the word da'ib, the present participle
of a verb whose original meaning was 'to work eagerly and assiduously
at something'. Here it is given the meaning of 'to apply oneself to
something with care in a perseverant, invariable manner, in accordance
with set habits'.

Surah 36, verse 39: God is speaking:
. And for the moon We have appointed mansions till she returns like an
old shriveled palm branch.

This is a reference to the curled form of the-palm branch which, as it
shrivels up,

[newbie] help with pygame-tutorial

2012-04-09 Thread aapeetnootjes
I'm trying out the pygame tutorial at 
http://www.pygame.org/docs/tut/intro/intro.html
If I try out the code I'm facing an error:
./game.py: line 4: syntax error  at unexpected symbol 'size'
./game.py: line 4: `size = width, height = 320, 240'

can anyone here tell me what's going wrong?

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


Re: f python?

2012-04-09 Thread Rainer Weikusat
Shmuel (Seymour J.) Metz  writes:

[...]

>>For one thing, if s is a non-empty null terminated string then,
>>cdr(s) is also a string representing the rest of that string 
>>without the first character,
>
> Are you really too clueless to differentiate between C and LISP?

In LISP, a list is a set of conses (pairs) whose car (first element of
the pair) contains a value and whose cdr (second element of the pair)
links to the next cons that's part of the list. The end of a list is
marked by a cdr whose value is nil. A so-called 'C string' is a
sequentially allocated sequence of memory locations which contain the
characters making up the string and the end of it is marked by a
memory location holding the value 0. This is logically very similar
to the LISP list and it shouldn't be to difficult to understand that
'cdr(s) is also a string representing the rest of the string' means
'given that s points to a non-empty C string, s + 1 points to a
possibly empty C string which is identical with s with the first
character removed'. 

>>Null terminated strings have simplified all kids of text
>>manipulation, lexical scanning, and data storage/communication 
>>code resulting in immeasurable savings over the years.
>
> Yeah, especially code that needs to deal with lengths and nulls. It's
> great for buffer overruns too.

This is, I think, a case where the opinions of people who have used C
strings and the opinions of people who haven't differ greatly. A nice
German proverb applicable to situations like that would be 'Was der
Bauer nicht kennt das frisst er nicht' ...
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: functions which take functions

2012-04-09 Thread Terry Reedy

On 4/9/2012 2:57 PM, Kiuhnm wrote:

Do you have some real or realistic (but easy and self-contained)
examples when you had to define a (multi-statement) function and pass it
to another function?


This is so common in Python that it is hardly worth sneezing about.

map(f, iterable)
filter(f, iterable)
functools.wraps(f, args)

---
Sometimes it is a bit hidden.

@deco
def f(): pass

is equivalent to

def f(): pass # replace with multiple statements
f = deco(f)

In fact, one reason for moving the wrapper above the def line is that 
function bodies can be arbitrarily large, moving the explicit call 
arbitrarily far away.


---
class C():
def test(self): print(self, '.test called')

c = C()
c.test()
import types
types.MethodType(c.__class__.test, c)()
# This *is* the internal implementation of c.test

#prints
<__main__.C object at 0x0363E278> .test called
<__main__.C object at 0x0363E278> .test called


In numerical analysis, functions that numerically integrate, 
differentiate, regress, fit, or plot functions take functions as arguments.


---
The most common, so common you do not notice it, is passing functions to 
other functions via the global namespace that is the hidden argument to 
all functions. (Every function has a readonly .__globals__ attribute 
that is used to resolve global accesses.)


def g(): pass
def h(): pass
def j(a): return g(h(a))

Be glad you do not have to *always* do something like

def j(a, funcs): return funcs.g(funcs.h(a))
print(j(a, locals()))

--
Terry Jan Reedy

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


Re: [newbie] help with pygame-tutorial

2012-04-09 Thread MRAB

On 09/04/2012 21:20, aapeetnootjes wrote:

I'm trying out the pygame tutorial at 
http://www.pygame.org/docs/tut/intro/intro.html
If I try out the code I'm facing an error:
./game.py: line 4: syntax error  at unexpected symbol 'size'
./game.py: line 4: `size = width, height = 320, 240'

can anyone here tell me what's going wrong?

thanks


That code looks OK to me.

Please copy and paste the first few lines of what you actually have in
your script, in case it's subtly different.
--
http://mail.python.org/mailman/listinfo/python-list


Re: f python?

2012-04-09 Thread Rainer Weikusat
Rainer Weikusat  writes:
> Shmuel (Seymour J.) Metz  writes:
>
> [...]
>
>>>For one thing, if s is a non-empty null terminated string then,
>>>cdr(s) is also a string representing the rest of that string 
>>>without the first character,
>>
>> Are you really too clueless to differentiate between C and LISP?
>
> In LISP, a list is a set of conses (pairs) whose car (first element of
> the pair) contains a value and whose cdr (second element of the pair)
> links to the next cons that's part of the list. The end of a list is
> marked by a cdr whose value is nil.

Addition: This can also be implemented very neatly in Perl by using
two element array references as 'cons cells', toy example

---
sub car
{
return $_[0][0];
}

sub cdr
{
return $_[0][1];
}

sub list
{
@_ && [shift, &list];
}

$l = list(0 .. 100);
while ($l) {
print(car($l), ' ');
$l = cdr($l);
}
print("\n");
---

and for algorithms which are well-suited for linked lists, this can
even outperform (when suitably implemented) an equivalent algorithm
using arrays.
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Python Gotcha's?

2012-04-09 Thread Stefan Schwarzer
Hi Miki,

On 2012-04-05 00:34, Miki Tebeka wrote:
> I'm going to give a "Python Gotcha's" talk at work.
> If you have an interesting/common "Gotcha" (warts/dark corners ...) please 
> share.
> 
> (Note that I want over http://wiki.python.org/moin/PythonWarts already).

I gave a somewhat similar talk a while ago:

http://sschwarzer.com/download/robust_python_programs_europython2010.pdf

The following is a German version of the talk slides, but
covers a bit more since there was a longer time slot. Even
if you don't know German, you'll most likely understand what
I'm talking about by reading the code. :-)

http://sschwarzer.com/download/robustere_python_programme_clt2010_print.pdf

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


Re: [newbie] help with pygame-tutorial

2012-04-09 Thread Roy Smith
In article 
<1a558398-3984-4b20-8d67-a0807871b...@v1g2000yqm.googlegroups.com>,
 aapeetnootjes  wrote:

> I'm trying out the pygame tutorial at 
> http://www.pygame.org/docs/tut/intro/intro.html
> If I try out the code I'm facing an error:
> ./game.py: line 4: syntax error  at unexpected symbol 'size'
> ./game.py: line 4: `size = width, height = 320, 240'
> 
> can anyone here tell me what's going wrong?
> 
> thanks

How did you run the code?  Did you do "python game.py", or did you just 
make game.py executable and run ./game.py?  If the later, my guess is 
it's being interpreted by the shell because there's no #! line.
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: functions which take functions

2012-04-09 Thread Emile van Sebille

On 4/9/2012 11:57 AM Kiuhnm said...

Do you have some real or realistic


... yes


(but easy and self-contained)


 aah, no.


examples when you had to define a (multi-statement) function and pass it
to another function?


This weekend I added functionality to a subsystem that allows users to 
write simple get functions stored in [funcname].py files in a specified 
directory that are read, compliled, and stored in a dictionary to be 
executed dynamically.  So, when a properly crafted command is recieved 
the corresponding function object is retrieved from the dictionary and 
exectued.


... hang on ...  ok -- here's a simple self contained example - HTH!

Emile



STACK=[]

def push(this):
STACK.append(int(this))

def plus(this):
tosa,tosb = STACK.pop(),STACK.pop()
push(tosa+tosb)

def minus(this):
tosa,tosb = STACK.pop(),STACK.pop()
push(tosa-tosb)

def times(this):
tosa,tosb = STACK.pop(),STACK.pop()
push(tosa*tosb)

def div(this):
tosa,tosb = STACK.pop(),STACK.pop()
push(tosb/tosa)

def equals(this):
return STACK.pop()

funcs = { "+" : plus,
  "-" : minus,
  "*" : times,
  "/" : div,
  "=" : equals
  }


def calculate (text):
for part in text.split():
retval = funcs.get(part,push)(part)
return retval


assert calculate ("4 3 + =") == 7
assert calculate ("4 3 * =") == 12
assert calculate ("12 3 / =") == 4
assert calculate ("1 2 3 4 + + + =") == 10

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


Re: Python randomly exits with Linux OS error -9 or -15

2012-04-09 Thread Martin P. Hellwig

On 09/04/2012 11:01, Janis wrote:

My experience is that these kind of behaviors are observed when (from 
most to least likeliness):

- Your kernel barfs on a limit, e.g. space/inodes/processes/memory/etc.
- You have a linked library mismatch
- You have bit rot on your system
- You have a faulty linked library
- You have a faulty kernel

The last two are academic for me as I never have seen it in real life, 
but could be possible and the bit rot one only bit me once in the last 
15 years (well I since then use RAID on all but convenience systems).


hth

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


Re: Python randomly exits with Linux OS error -9 or -15

2012-04-09 Thread Dan Stromberg
You might try running your Python process with:

  strace -f -s 1024 -o /tmp/script.strace python /path/to/script.py

Then you (perhaps with a C programmer) can likely track down what happened
right before the crash by examining the system call tracer near the end of
the file.

http://stromberg.dnsalias.org/~strombrg/debugging-with-syscall-tracers.html

On Mon, Apr 9, 2012 at 3:01 AM, Janis  wrote:

> Hello!
>
> I have this problem with my script exiting randomly with Linux OS
> status code -9 (most often) or -15 (also sometimes, but much more
> rarely). As far as I understand -9 corresponds to Bad file descriptor
> and -15 Block device required.
>
> 1) Is there a way how I could find out what exactly causes Python
> process to exit?
> 2) What could be the reason of Python exiting with these status code?
>
> The script is a crawler that crawls several web sites to download web
> pages and extract information. Most often it exits after having run
> for 2 hours and having downloaded ~24 000 files. Some specific web
> sites are more affected than others, i.e., there are other instances
> of the script running in parallel that download more pages and
> complete normally. That could be related to the speed each page is
> returned etc.
>
> I have a try-catch block in the root of the script which works very
> well to catch any kind of exceptions, but in these cases either Python
> does not catch the exception or fails to log it into MySQL error log
> table.
>
> I can not use debugger because the code exits after it has run for an
> hour or more. In order to try to catch the exact place I have put
> logging after each line in some places. The logging prints to stdout,
> to a file and logs the line into MySQL DB. This has revealed that code
> may exit upon a random simple lines such as: time.sleep(0.1) - most
> often, f = opener.open(request) - also often, but sometimes also such
> simple statements as list.add('string').
>
> It can also be that the problem occues and then the script exits upon
> attempt to do any output - stdout (i.e. regular print '1'), writing to
> file and logging into MySQL. I have changed either of these to be the
> first ones in the debug log function, but each time the script did not
> fail in between these lines.
>
> The main libraries that are in use: MySQLDB, urllib and urllib2, but
> none of them seems to be the direct cause of the problem, there is no
> direct call of any of these upon exit. I suspect that this could be
> related to some garbadge collecting process.
>
> Versions tried with: Python 2.6, Python 2.7 (and I think it happened
> also in Python 2.5, but I'm not sure)
>
> Thanks,
> Janis
> --
> http://mail.python.org/mailman/listinfo/python-list
>
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Python randomly exits with Linux OS error -9 or -15

2012-04-09 Thread Cameron Simpson
On 09Apr2012 12:02, Janis  wrote:
| Thank you all for the help! I will need to think a bit about the other
| suggestions.
| 
| But, Alan, as to this:
| > > How do you get -9 and -15? Exit status is supposed to be between 0 and
| > > 127.
| 
| I have the following code that has caught these:
| 
| p = subprocess.Popen([Config.PYTHON_EXE,'Load.py',"%s" % (row[1],)],
| bufsize=0, executable=None, stdin=None, stdout=None,
| stderr=subprocess.PIPE, preexec_fn=None, close_fds=False, shell=False,
| cwd='../Loader/')
| stdout, stderr = p.communicate()
| if p.returncode != 0:
| ...
| 
| So you say it's SIGKILL and SIGTERM? Then I guess these could be
| misleading statuses from those cases when I have terminated the
| sessions myself, and when there is the real problem apparently the
| caller detected nothing here. SIGKILL and SIGTERM would probably also
| explain why there was nothing in stderr.

It is certainly SIGKILL and SIGTERM. See citation from docs below.

Background: wait() and its modern counterpart waitpid() put the process exit
status into an int, which is signed.

The exit status seen _in_the_shell_ is as described (0-255) but the raw
status from the OS is encoded in an int, and will generally be negative
is the process terminated from a signal. You're meant (in C) to inspect
it via a set of macros which understand this stuff.

If you read the documentation for the subprocess module it says:

  Popen.returncode

The child return code, set by poll() and wait() (and indirectly by
communicate()). A None value indicates that the process hasn’t terminated
yet.
A negative value -N indicates that the child was terminated by signal N
(Unix only).

So there you go. SIGKILL and SIGTERM, definitely.

RTFM. It is your friend.

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

Gabriel Genellina: See PEP 234 http://www.python.org/dev/peps/pep-0234/
Angus Rodgers:
  You've got to love a language whose documentation contains sentences
  beginning like this:
"Among its chief virtues are the following four -- no, five -- no,
six -- points: [...]"
from python-list@python.org
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Interrupting a blocking function frolm another thread.

2012-04-09 Thread superhac007
On Monday, April 9, 2012 9:39:54 AM UTC-5, super...@gmail.com wrote:
> On Apr 8, 8:09 pm, Adam Skutt  wrote:
> > On Apr 8, 5:52 pm, superhac...@gmail.com wrote:
> >
> >
> >
> >
> >
> >
> >
> >
> >
> > > On Sunday, April 8, 2012 3:55:41 PM UTC-5, Adam Skutt wrote:
> > > > On Apr 8, 2:45 pm, "superhac...@gmail.com" 
> > > > wrote:
> > > > > I am using the python module nfqueue-bindings which is a nfqueue
> > > > > packet intercepting module.  It uses the following snippet of code to
> > > > > start the process:
> >
> > > > > print "trying to run"
> > > > > try:
> > > > >      q.try_run()
> > > > >      except KeyboardInterrupt, e:
> > > > >      print "interrupted"
> >
> > > > > The q.try_run() method blocks.   I would like to be able to interrupt
> > > > > this the same way you can hit control-c to unblock it, but from
> > > > > another thread.  Does anyone have any idea on how to do this?  Is
> > > > > there some sort of Exception I can generate from another thread that
> > > > > would do this?
> >
> > > > The simplest and most reliable way will be to modify the asynchronous
> > > > I/O example 
> > > > athttps://www.wzdftpd.net/redmine/projects/nfqueue-bindings/repository/...
> > > > to do what you want.  The classical way to accomplish this would be to
> > > > create a second I/O descriptor (via os.pipe or similiar) and wait for
> > > > that descriptor in the same async I/O loop.  When you want to stop the
> > > > loop, send a message (could be a single byte) to the second descriptor
> > > > and then respond appropriately in your asynchronous I/O handler.
> >
> > > > However, simply ignoring the nfqueue socket while doing other
> > > > processing might be acceptable too.  I would read the documentation
> > > > and ask questions carefully before taking that approach, as it may
> > > > lead to lost data or other problems.  The viability of this approach
> > > > depends heavily on your application.
> >
> > > > Adam
> >
> > > Thanks for the reply Adam.  I am still confused with the example with the 
> > > example you linked to.  I have only been working with Python for a couple 
> > > of weeks, but I am fluent with C.  Given the example you linked to, are 
> > > you saying that this is the run loop for asynchronous comm that a end 
> > > user of the module could use?  Or is this something with the 
> > > nfqueue-bindings module that I would I have to modify.  I don't recognize 
> > > the the main loop in the example.
> >
> > asyncore is a standard python module for handling asynchronous I/O
> > operations (i.e., select/poll operations).  The main loop in that
> > example is handled by the 'asyncore.loop()' call, which just calls
> > select() in a loop until all channels are closed.  You're looking at
> > the standard Python library technique for an asynchronous I/O event
> > loop.
> >
> > asyncore.file_dispatcher enables use of asyncore.loop() with standard
> > UNIX FDs.  nfqueue.queue objects provide access to their underlying
> > FDs so you can wait on them.  asyncore.loop() is the main loop of the
> > application, performing endless select/poll waits and calling
> > 'handle_read' whenever the netfilter FD has data for reading.  You can
> > find more details about the module in the Python library docs.
> >
> > Since your code is Linux specific, you can always call os.select() or
> > os.poll() if it makes you feel better, but I don't think that's going
> > to simplify anything here.
> >
> > Hopefully that helps you.
> >
> > Adam
> 
> Thanks for the through explanation.  I now know what I need to do.
> 
> 
> Thanks again!

Adam,

I got everything working today asynchronously with your help.   I really 
appreciate the information that you provided.  Since I am very new to Python 
development your input and direction greatly expedited what I needed.  Its 
people like you that make the world a better place.   If you ever need anything 
don't hesitate to email me personally.

In a nut a shell I ended up with a thread that had the following:

def startAsyncQ():
global stop
while stop != True:
asyncore.loop(count=1)

I could then could control the starting and stopping from another thread.  It 
worked like a champ.

Again, Thanks for your help!


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


use Python to post image to Facebook

2012-04-09 Thread CM
Shot in the dark here:  has any who reads this group been successful
with getting Python to programmatically post an image to Facebook?

I've tried using fbconsole[1] and facepy[2], both of which apparently
work fine for their authors and others and although I have an
authorization code, publish permissions, a Facebook app, I get back
these unhelpful errors when I try this (like "an unknown  error
occurred").

If anyone has been able to do this, maybe you can help me figure out
what I am doing wrong.
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: use Python to post image to Facebook

2012-04-09 Thread CM
> I've tried using fbconsole[1] and facepy[2], both of which apparently

Forgot the refs:

[1]https://github.com/facebook/fbconsole;
http://blog.carduner.net/2011/09/06/easy-facebook-scripting-in-python/

[2]https://github.com/jgorset/facepy
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: use Python to post image to Facebook

2012-04-09 Thread Shashank Singh
I wrote something like this a little while ago, may be this is what you are
looking for:
http://rationalpie.wordpress.com/2011/02/12/posting-photo-to-wall-using-facebook-graph-api/

On Mon, Apr 9, 2012 at 8:46 PM, CM  wrote:

> > I've tried using fbconsole[1] and facepy[2], both of which apparently
>
> Forgot the refs:
>
> [1]https://github.com/facebook/fbconsole;
> http://blog.carduner.net/2011/09/06/easy-facebook-scripting-in-python/
>
> [2]https://github.com/jgorset/facepy
> --
> http://mail.python.org/mailman/listinfo/python-list
>



-- 
Regards
Shashank Singh
 http://www.flipora.com
http://r ationalpie.wordpress.com
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: How to filter a dictionary ?

2012-04-09 Thread Shashank Singh
On Mon, Apr 9, 2012 at 10:49 PM, Nikhil Verma wrote:

>
> for_patient_type = {37: u'Test', 79: u'Real', 80: u'Real', 81: u'Real',
> 83: u'Real', 84: u'Real', 91: u'Real', 93: u'Real'}
>
> I want if the values are 'Real' give me the keys that have values 'Real'
> like this.
>
> {79:'Real'}
> {80:'Real'}
> {81:'Real'}
> {83:'Real'}
> {84:'Real'}
> {91:'Real'}
> {93:'Real'}
>

if you want the dict filtered

Python 2.6.1 (r261:67515, Jun 24 2010, 21:47:49)
[GCC 4.2.1 (Apple Inc. build 5646)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> for_patient_type = {37: u'Test', 79: u'Real', 80: u'Real', 81: u'Real',
83: u'Real', 84: u'Real', 91: u'Real', 93: u'Real'}
>>> dict((k, for_patient_type[k]) for k in for_patient_type if
for_patient_type[k] == 'Real')
{79: u'Real', 80: u'Real', 81: u'Real', 83: u'Real', 84: u'Real', 91:
u'Real', 93: u'Real'}
>>>

If you just want the keys

>>> [k for k in for_patient_type if for_patient_type[k] == 'Real']
[80, 81, 83, 84, 91, 93, 79]
>>>


>
> I am trying this but its giving me a generator object.
>
> In [9]: (k for k,v in for_patient_type.iteritems() if v == 'Real')
>

Iterating over a dict gives you all the keys, not the key value pairs



-- 
Regards
Shashank Singh
  http://www.flipora.com
http://r ationalpie.wordpress.com
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: How to filter a dictionary ?

2012-04-09 Thread Nikhil Verma
Thanks Shashank . It worked.

On Tue, Apr 10, 2012 at 11:34 AM, Shashank Singh <
shashank.sunny.si...@gmail.com> wrote:

>
>
> On Mon, Apr 9, 2012 at 10:49 PM, Nikhil Verma wrote:
>
>>
>> for_patient_type = {37: u'Test', 79: u'Real', 80: u'Real', 81: u'Real',
>> 83: u'Real', 84: u'Real', 91: u'Real', 93: u'Real'}
>>
>> I want if the values are 'Real' give me the keys that have values 'Real'
>> like this.
>>
>> {79:'Real'}
>> {80:'Real'}
>> {81:'Real'}
>> {83:'Real'}
>> {84:'Real'}
>> {91:'Real'}
>> {93:'Real'}
>>
>
> if you want the dict filtered
>
> Python 2.6.1 (r261:67515, Jun 24 2010, 21:47:49)
> [GCC 4.2.1 (Apple Inc. build 5646)] on darwin
> Type "help", "copyright", "credits" or "license" for more information.
> >>> for_patient_type = {37: u'Test', 79: u'Real', 80: u'Real', 81:
> u'Real', 83: u'Real', 84: u'Real', 91: u'Real', 93: u'Real'}
> >>> dict((k, for_patient_type[k]) for k in for_patient_type if
> for_patient_type[k] == 'Real')
> {79: u'Real', 80: u'Real', 81: u'Real', 83: u'Real', 84: u'Real', 91:
> u'Real', 93: u'Real'}
> >>>
>
> If you just want the keys
>
> >>> [k for k in for_patient_type if for_patient_type[k] == 'Real']
> [80, 81, 83, 84, 91, 93, 79]
> >>>
>
>
>>
>> I am trying this but its giving me a generator object.
>>
>> In [9]: (k for k,v in for_patient_type.iteritems() if v == 'Real')
>>
>
> Iterating over a dict gives you all the keys, not the key value pairs
>
>
>
> --
> Regards
> Shashank Singh
>   http://www.flipora.com
> http://r 
> ationalpie.wordpress.com
>
>


-- 
Regards
Nikhil Verma
+91-958-273-3156
-- 
http://mail.python.org/mailman/listinfo/python-list