Re: Extracting real-domain-name (without sub-domains) from a given URL

2009-01-13 Thread Chris Rebert
On Mon, Jan 12, 2009 at 11:46 PM, S.Selvam Siva s.selvams...@gmail.com wrote:
 Hi all,

   I need to extract the domain-name from a given url(without sub-domains).
 With urlparse, i am able to fetch only the domain-name(which includes the
 sub-domain also).

 eg:
   http://feeds.huffingtonpost.com/posts/ , http://www.huffingtonpost.de/,
  all must lead to huffingtonpost.com or huffingtonpost.de

 Please suggest me some ideas regarding this problem.

That would require (pardon the pun) domain-specific logic. For most
TLDs (e.g. .com, .org) the domain name is just blah.com, blah.org,
etc. But for ccTLDs, often only second-level registrations are
allowed, e.g. for www.bbc.co.uk, so the main domain name would be
bbc.co.uk  I think a few TLDs have even more complicated rules.

I doubt anyone's created a general ready-made solution for this, you'd
have to code it yourself.
To handle the common case, you can cheat and just .split() at the
periods and then slice and rejoin the list of domain parts, ex:
'.'.join(domain.split('.')[-2:])

Cheers,
Chris

-- 
Follow the path of the Iguana...
http://rebertia.com
--
http://mail.python.org/mailman/listinfo/python-list


Relative performance of comparable regular expressions

2009-01-13 Thread Barak, Ron
Hi,

I have a question about relative performance of comparable regular expressions.

I have large log files that start with three letters month names (non-unicode).

Which would give better performance, matching with  ^[a-zA-Z]{3}, or with 
^\S{3} ?
Also, which is better (if different at all): \d\d or \d{2} ?
Also, would matching . be different (performance-wise) than matching the 
actual character, e.g. matching : ?
And lastly, at the end of a line, is there any performance difference between 
(.+)$ and (.+)

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


Re: Extracting real-domain-name (without sub-domains) from a given URL

2009-01-13 Thread S.Selvam Siva
On Tue, Jan 13, 2009 at 1:50 PM, Chris Rebert c...@rebertia.com wrote:

 On Mon, Jan 12, 2009 at 11:46 PM, S.Selvam Siva s.selvams...@gmail.com 
 wrote:
  Hi all,
 
I need to extract the domain-name from a given url(without sub-domains).
  With urlparse, i am able to fetch only the domain-name(which includes the
  sub-domain also).
 
  eg:
http://feeds.huffingtonpost.com/posts/ , http://www.huffingtonpost.de/,
   all must lead to huffingtonpost.com or huffingtonpost.de
 
  Please suggest me some ideas regarding this problem.

 That would require (pardon the pun) domain-specific logic. For most
 TLDs (e.g. .com, .org) the domain name is just blah.com, blah.org,
 etc. But for ccTLDs, often only second-level registrations are
 allowed, e.g. for www.bbc.co.uk, so the main domain name would be
 bbc.co.uk  I think a few TLDs have even more complicated rules.

 I doubt anyone's created a general ready-made solution for this, you'd
 have to code it yourself.
 To handle the common case, you can cheat and just .split() at the
 periods and then slice and rejoin the list of domain parts, ex:
 '.'.join(domain.split('.')[-2:])

 Cheers,
 Chris


Thank you Chris Rebert,
  Actually i tried with domain specific logic.Having 200 TLD like
.com,co.in,co.uk and tried to extract the domain name.
  But my boss want more reliable solution than this method,any way i
will try to find some alternative solution.


--
Yours,
S.Selvam
--
http://mail.python.org/mailman/listinfo/python-list


Re: efficient interval containment lookup

2009-01-13 Thread Gabriel Genellina
En Tue, 13 Jan 2009 02:55:36 -0200, Per Freem perfr...@yahoo.com  
escribió:



On Jan 12, 10:58 pm, Steven D'Aprano
ste...@remove.this.cybersource.com.au wrote:

On Mon, 12 Jan 2009 14:49:43 -0800, Per Freem wrote:
 thanks for your replies -- a few clarifications and questions. the
 is_within operation is containment, i.e. (a,b) is within (c,d) iff a
= c and b = d. Note that I am not looking for intervals that
 overlap... this is why interval trees seem to me to not be relevant,



i found an implementation (which is exactly how i'd write it based on
the description) here:  
http://hackmap.blogspot.com/2008/11/python-interval-tree.html


when i use this however, it comes out either significantly slower or
equal to a naive search. my naive search just iterates through a
smallish list of intervals and for each one says whether they overlap
with each of a large set of intervals.


I think there is an algorithm by Baeza-Yates that handles efficiently what  
you want; but I can't find it. Perhaps Google works better for you. It  
might be only available in Spanish, though.


--
Gabriel Genellina

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


Re: Attaching a live interpreter to a script?

2009-01-13 Thread Julian Smith
On Mon, 12 Jan 2009 23:35:14 -0800 (PST)
Kannon neokan...@gmail.com wrote:

 What I'd like to do is attach an interpreter to a program running from
 a script (I.E, not something I typed into the live interpreter). It'd
 be an awesome way to debug programs, as well as tweak parameters and
 such at runtime. Ideally, I'd like it to be something in pure python
 so I could throw it into Jython and IronPython as well. (Though, I can
 actually code Java and C#, so implementing it is something I could do
 myself, if needed.) I was thinking maybe something I could throw into
 a tkinter window or similar, but I wasn't able to find anything on how
 to pass stuff directly to the interpreter.
 
 Any ideas, or if this is even possible would be nice. Thanks in
 advance.

I have a few lines of code that runs a debug-server in its own thread,
listening on a socket, and exec-ing text that is sent to the socket.
The exec happens in the debug-server thread.

It's very useful - you can get a backtrace for all threads (on
python-2.5 and later), look at globals, call functions, etc etc, all
while the main programme is running.

If anyone's interested, i'll put the code somewhere. It's all very
simple; some would say crude, too.

- Jules

-- 
http://op59.net/
http://undo-software.com/
--
http://mail.python.org/mailman/listinfo/python-list


Re: Implementing file reading in C/Python

2009-01-13 Thread Marc 'BlackJack' Rintsch
On Mon, 12 Jan 2009 21:26:27 -0500, Steve Holden wrote:

 The very idea of mapping part of a process's virtual address space onto
 an area in which low-level system code resides, so writing to this
 region may corrupt the system, with potentially catastrophic
 consequences seems to be asking for trouble to me.

That's why those regions are usually write protected and no execution 
allowed from the code in the user area of the virtual address space.

Ciao,
Marc 'BlackJack' Rintsch
--
http://mail.python.org/mailman/listinfo/python-list


Re: Relative performance of comparable regular expressions

2009-01-13 Thread John Machin
On Jan 13, 7:24 pm, Barak, Ron ron.ba...@lsi.com wrote:
 Hi,

 I have a question about relative performance of comparable regular 
 expressions.

 I have large log files that start with three letters month names 
 (non-unicode).

 Which would give better performance, matching with  ^[a-zA-Z]{3}, or with 
 ^\S{3} ?

(1) If you want to match at the start of a line, use re.match()
*without* the pointless ^. Don't use re.search with a pattern
starting with ^ -- it won't be any faster than and it could be a lot
worse; re.search doesn't know to stop if the first match fails:

command-prompt\python26\python -m timeit -simport re;rx=re.compile
('^AB')
;text='Z'*100 rx.match(text)
100 loops, best of 3: 1.15 usec per loop

command-prompt\python26\python -m timeit -simport re;rx=re.compile
('^AB')
;text='Z'*100 rx.search(text)
10 loops, best of 3: 4.47 usec per loop

command-prompt\python26\python -m timeit -simport re;rx=re.compile
('^AB')
;text='Z'*1000 rx.search(text)
1 loops, best of 3: 34.1 usec per loop

(2) I think you mean ^\s{3} not ^\S{3}

(3) Now that you've seen how to do timings, over to you :-)

 Also, which is better (if different at all): \d\d or \d{2} ?
 Also, would matching . be different (performance-wise) than matching the 
 actual character, e.g. matching : ?
 And lastly, at the end of a line, is there any performance difference between 
 (.+)$ and (.+)

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


Re: efficient interval containment lookup

2009-01-13 Thread Tim Chase

Per Freem wrote:

i forgot to add, my naive_find is:

def naive_find(intervals, start, stop):
  results = []
  for interval in intervals:
if interval.start = start and interval.stop = stop:
  results.append(interval)
  return results


I don't know if using a list-comprehension here is a better 
choice, but your code looks very much like


  def naive_find(intervals, start, stop):
return [interval for interval in intervals
  if interval.start = start and interval.stop = stop]

which may even be simple enough to include in-line rather than as 
a sub-function (with any associated call overhead).


I've found that usually Python's smart/efficient handling of 
list-comprehensions can make an appreciable difference within 
critical-loop constructs.


-tkc




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


Re: using PIL for PCA analysis

2009-01-13 Thread Jan Erik Solem



 if i want to do an array of PIL image data i can use
 img=Image.open(myimg.jpg) .convert(L)
 pixelarray=img.getdata()
 
convert(L) is a good way to make images grayscale. An option to using
getdata() is to try numpy's array:
pixelarray = numpy.array(img)
this gives lots of possibilities for working with the images numerically,
like for PCA. (see example code in the link below)



 thus i guess i can build a matrix of a set of  images
 is there something wrong in the way i do this above?may be i can use
 that to find covariance matrix for the set of images?
 
I wrote a short script for doing PCA on images using python, with some
explanations and example code 
http://jesolem.blogspot.com/2009/01/pca-for-images-using-python.html here .
Could be of help to you guys. 

-- 
View this message in context: 
http://www.nabble.com/using-PIL-for-PCA-analysis-tp15606311p21432675.html
Sent from the Python - python-list mailing list archive at Nabble.com.

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


Re: why cannot assign to function call

2009-01-13 Thread Steven D'Aprano
On Sat, 10 Jan 2009 15:46:35 +, Mark Wooding wrote:

 [Another tome.  I hope this contains sufficient new material to continue
 to be of interest to other readers.]

I found it interesting. I don't know about others. However, at 756 lines 
(including quoting) it was nearly double the size of my previous tome, so 
I think this discussion is clearly not going anyway. I think this 
conversation is reaching it's natural end. Frustration levels are rising. 
I could -- and in fact intended to -- write a point-by-point argument, 
but that would only send the size of this skyrocketing and probably 
nobody would read it. So I'm going to take a different tack in an attempt 
to reduce frustration levels: if I can't convince the other side they're 
wrong, can I at least get them to understand where I'm coming from a 
little better?

As I see it, this conversation is floundering on two radically different 
ideas about what it means to say a language uses pass-by-foo.

On the one hand, some people (me and possibly rurpy) consider this is 
pass-by-foo to be a statement about behaviour directly visible to the 
programmer. We have a set of behavioral traits in mind, and if a language 
exhibits those behaviours, then it is clearly and obviously pass-by-foo 
no matter how that behaviour is implemented. I'll call these the 
behaviorists.

On the other hand, others (I'm going to put Joe and Mark into this group) 
consider pass-by-foo to be a statement about mechanism, or if you 
prefer, implementation. If a compiler does X, then the language is pass-
by-foo, regardless of behaviour. I'll call these the mechanists.

To a behaviorist, Python simply can't be pass-by-value, because it 
doesn't behave like pass-by-value in other languages (particularly C and 
Pascal). The Python community has a tradition of duck-typing: if it 
quacks like a duck and swims like a duck, then it is a duck in every way 
that is important. If it can't swim and doesn't quack, it isn't a duck, 
no matter how much duck DNA is in it. If it flies, that's irrelevant to 
the question, because some ducks fly and some don't.

According to the behaviorist view, what makes an evaluation strategy call-
by-reference is whether or not it exhibits the following three behaviours:

(1) passing a value to a function does not copy the value;

(2) modifications to the value inside the function are visible to the 
caller;

(3) assignments to the value inside the function are visible to the 
caller.

where value means the thing the programmer manipulates symbolically in 
source code. Values are ints, strings, lists and so forth. Pointers or 
references are *only* values if the language allows you to write the 
equivalent of:

ptr = SomeReferenceTo(x)  # like Pascal's ptr := ^x;
x = Dereference(ptr)  # like Pascal's x := ptr^;


According to this viewpoint, Python clearly cannot be pass-by-reference 
because (3) is not true.


Similarly, what makes pass-by-value is:

(1) passing a value to a function makes a copy of the value, where value 
is an entity the programmer can symbolically manipulate in source code 
(lists, ints, floats etc.);

(2) modifications to the value inside the function are not visible to the 
caller;

(3) assignments to the value inside the function are not visible to the 
caller.

According to this viewpoint, Python clearly cannot be pass-by-value 
either because neither (1) nor (2) are true. The underlying mechanism is 
irrelevant.

Mechanists take a different view. A typical argument paraphrased from Joe 
in previous threads is:

Of course Python makes a copy of the value you pass to a function. The 
difference is that the value you pass is a reference.

Mechanists reject the restriction that values only include entities 
manipulated by the programmer. Consequently they're prepared to say that 
values in Python aren't the things which Python programmers symbolically 
manipulate (strings, ints etc.) but are references. Needless to say this 
claim strikes behaviorists as nonsensical and rather incoherent. If the 
value of 2 isn't 2, then value has no meaning. If mechanists see the 
behaviorists as willfully ignorant, the behaviorists see the mechanists 
as being blatantly obfuscatory, introducing irrelevant details and 
ignoring clear examples of the duck quacking.


And these two viewpoints are why this debate never ends, merely goes 
quite for a few weeks or months.



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


best site for hacking tricks , computer tweaks

2009-01-13 Thread debasish ray
 

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


Re: Python tricks

2009-01-13 Thread Nick Craig-Wood
Scott David Daniels scott.dani...@acm.org wrote:
  RajNewbie wrote:
  On Jan 12, 6:51 pm, Tim Chase python.l...@tim.thechases.com wrote:
  [a perfectly fine reply which is how I'd solve it]
   RajNewbie wrote:
  ... The solution that I had in mind is:
 while True:
   ...
   if condition: break
   if inifinte_loop(): raise infiinte_loop_exception
Wherein infinite_loop is a generator, which returns true if i  200
def infinite_loop():
   i = 0
   while i  200:
   i++
   yield False
   yield True
  Could somebody let me know whether this is a good option?
  ...
  But, I still feel it would be much more aesthetically pleasing if I
  can call a single procedure like
  if infinite_loop() - to do the same.
  Is it somehow possible? - say by using static variables, iterators --
  anything?
 
  Yes, it is possible.  After:
 
   def Fuse(count, exception):
   for i in range(count):
   yield None
   raise exception
 
  You can do your loop as:
   check_infinite = Fuse(200, ValueError('Infinite Loop')).next
   while True:
   ...
   check_infinite()

Or related to the above and the original proposal

class InfiniteLoopError(Exception):
An 'infinite' loop has been detected

def infinite_loop(max=200):
for i in xrange(max):
yield i
raise InfiniteLoopError()

Use it like this

for i in infinite_loop():
if i  10:
break
print iteration, i

or

for i in infinite_loop(10):
print iteration, i

  but I agree with Tim that a for ... else loop for the limit is
  clearer.

Probably yes

-- 
Nick Craig-Wood n...@craig-wood.com -- http://www.craig-wood.com/nick
--
http://mail.python.org/mailman/listinfo/python-list


Re: are there some special about '\x1a' symbol

2009-01-13 Thread sim.sim
On 12 янв, 16:00, John Machin sjmac...@lexicon.net wrote:
 On Jan 13, 12:45 am, sim.sim maksim.kasi...@gmail.com wrote:



  On 10 ÑÎ×, 23:40, John Machin sjmac...@lexicon.net wrote:

   On Jan 11, 2:45šam, sim.sim maksim.kasi...@gmail.com wrote:

Hi all!

I had touch with some different python behavior: I was tried to write
into a file a string with the '\x1a' symbol, and for FreeBSD system,
it gives expected result:

 open(test, w).write('before\x1aafter')
 open('test').read()

'before\x1aafter'

but for my WinXP box, it gives some strange:

 open(test, w).write('before\x1aafter')
 open('test').read()

'before'

Here I can write all symbols, but not read.
I've tested it with python 2.6, 2.5 and 2.2 and WinXP SP2.

Why is it so and is it possible to fix it?

   You've already got two good answers, but this might add a little more
   explanation:

   You will aware that in Windows Command Prompt, to exit the interactive
   mode of Python (among others), you need to type Ctrl-Z ...

   | C:\junkpython
   | Python 2.5.2 (r252:60911, Feb 21 2008, 13:11:45) [MSC v.1310 32 bit
   (Intel)] on
   win32
   | Type help, copyright, credits or license for more
   information.
   |  problem = '\x1a'
   |  ord(problem)
   | 26
   |  # What is the 26th letter of the English/ASCII alphabet?
   | ...
   |  ^Z
   |
   | C:\junk

   HTH,
   John

  Hi John,

  I agree - those two answers are really good. Thanks to Mel and Marc.
  I'm sorry if my stupid question was annoyed you.

 I didn't think your question was stupid. Stupid was (a) CP/M recording
 file size as number of 128-byte sectors, forcing the use of an in-band
 EOF marker for text files (b) MS continuing to regard Ctrl-Z as an EOF
 decades after people stopped writing Ctrl-Z at the end of text files.

 And I wasn't annoyed either ... I was merely adding the information
 that Ctrl-Z and '\x1a' were the same thing; many people don't make the
 connection.

 Cheers,
 John

Ah John, thank you for your explanations!
My first impression was that your comments does not relates to my
question,
but I've found new things where I used to think there was nothing.

Now it is interesting to me how one have to give reasons to use open
(.., 'r') instead of open(.., 'rb')?
There is confusing situation when we use open(.., 'r'), are there some
scenario when we might be confused when we'll use open(.., 'rb')?

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


Re: are there some special about '\x1a' symbol

2009-01-13 Thread John Machin
On Jan 13, 10:12 pm, sim.sim maksim.kasi...@gmail.com wrote:

 Ah John, thank you for your explanations!
 My first impression was that your comments does not relates to my
 question,
 but I've found new things where I used to think there was nothing.

 Now it is interesting to me how one have to give reasons to use open
 (.., 'r') instead of open(.., 'rb')?
 There is confusing situation when we use open(.., 'r'), are there some
 scenario when we might be confused when we'll use open(.., 'rb')?

Some general rules: if you regard a file as text, open it with rt --
the t is redundant but gives you and anyone else who reads your code
that assurance that you've actually thought about it. Otherwise you
regard the file as binary, and open it with rb. The distinction was
always important on Windows because of the special handling of
newlines and '\x1a') but largely unimportant on *x boxes. With Python
3.0, it is important for all users to specify the mode that they
really need: 'b' files read and write bytes objects whereas 't' files
read and write str objects, have the newline etc changes, and need an
encoding to decode the raw bytes into str (Unicode) objects -- and you
can't use bytes objects directly with a 't' file nor str objects
directly a 'b' file.

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


Re: Extracting real-domain-name (without sub-domains) from a given URL

2009-01-13 Thread Steve Holden
S.Selvam Siva wrote:
 On Tue, Jan 13, 2009 at 1:50 PM, Chris Rebert c...@rebertia.com wrote:
 On Mon, Jan 12, 2009 at 11:46 PM, S.Selvam Siva s.selvams...@gmail.com 
 wrote:
 Hi all,

   I need to extract the domain-name from a given url(without sub-domains).
 With urlparse, i am able to fetch only the domain-name(which includes the
 sub-domain also).

 eg:
   http://feeds.huffingtonpost.com/posts/ , http://www.huffingtonpost.de/,
  all must lead to huffingtonpost.com or huffingtonpost.de

 Please suggest me some ideas regarding this problem.
 That would require (pardon the pun) domain-specific logic. For most
 TLDs (e.g. .com, .org) the domain name is just blah.com, blah.org,
 etc. But for ccTLDs, often only second-level registrations are
 allowed, e.g. for www.bbc.co.uk, so the main domain name would be
 bbc.co.uk  I think a few TLDs have even more complicated rules.

 I doubt anyone's created a general ready-made solution for this, you'd
 have to code it yourself.
 To handle the common case, you can cheat and just .split() at the
 periods and then slice and rejoin the list of domain parts, ex:
 '.'.join(domain.split('.')[-2:])

 Cheers,
 Chris
 
 
 Thank you Chris Rebert,
   Actually i tried with domain specific logic.Having 200 TLD like
 .com,co.in,co.uk and tried to extract the domain name.
   But my boss want more reliable solution than this method,any way i
 will try to find some alternative solution.
 
If you post a good first try, opening the source, I would be surprised
if others do not join your effort to establish suitable rules. This is
somethjing that many people could doubtless use.

regards
 Steve
-- 
Steve Holden+1 571 484 6266   +1 800 494 3119
Holden Web LLC  http://www.holdenweb.com/

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


Re: Problems in Using C-API for Unicode handling

2009-01-13 Thread abhi
On Jan 13, 12:17 pm, Terry Reedy tjre...@udel.edu wrote:
 abhi wrote:
  Hi,
      I am trying to handle Unicode objects in C (Python 2.5.2). I am
  getting PyObjects from and want to coerce them to unicode objects. The
  documentation provides two APIs for that:

   PyUnicode_FromEncodedObject(PyObject *obj, const char *encoding,
  const char *errors)
   PyUnicode_FromObject(PyObject *obj)

  (http://www.python.org/doc/2.5.2/api/unicodeObjects.html)
  Now I want to utf-16 so I am trying to use the first one, but it is
  giving back NULL in case of PyObject is already Unicode type which is
  expected. What puzzles me is that PyUnicode_FromObject(PyObject *obj)
  is passing irrespective of type of PyObject. The API says it is
  Shortcut for PyUnicode_FromEncodedObject(obj, NULL, strict) but if I
  use that, it returns NULL where as PyUnicode_FromObject works.

  Is there any way by which I can take in any PyObject and convert it to
  utf-16 object? Any help is appreciated.

 Whether Unicode objects are utf-16 or utf=32 depends on your Python
 build.  You can always convert a byte string representation of an object
 to unicode.- Hide quoted text -

 - Show quoted text -

Hi,
   I agree with you. I have a Python unicode object in C (I don't know
which utf) and I want to convert this explicitely to utf-16. Is there
any way to do this?
PyUnicode_FromEncodedObject(PyObject *obj, const char *encoding, const
char *errors) says that obj can't be a unicode type so I guess I can't
use this one, does anybody knows any other method by which I can
achieve my goal?

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


Re: Relative performance of comparable regular expressions

2009-01-13 Thread Steve Holden
John Machin wrote:
 On Jan 13, 7:24 pm, Barak, Ron ron.ba...@lsi.com wrote:
 Hi,

 I have a question about relative performance of comparable regular 
 expressions.

 I have large log files that start with three letters month names 
 (non-unicode).

 Which would give better performance, matching with  ^[a-zA-Z]{3}, or with 
 ^\S{3} ?
 
 (1) If you want to match at the start of a line, use re.match()
 *without* the pointless ^. Don't use re.search with a pattern
 starting with ^ -- it won't be any faster than and it could be a lot
 worse; re.search doesn't know to stop if the first match fails:
 
 command-prompt\python26\python -m timeit -simport re;rx=re.compile
 ('^AB')
 ;text='Z'*100 rx.match(text)
 100 loops, best of 3: 1.15 usec per loop
 
 command-prompt\python26\python -m timeit -simport re;rx=re.compile
 ('^AB')
 ;text='Z'*100 rx.search(text)
 10 loops, best of 3: 4.47 usec per loop
 
 command-prompt\python26\python -m timeit -simport re;rx=re.compile
 ('^AB')
 ;text='Z'*1000 rx.search(text)
 1 loops, best of 3: 34.1 usec per loop
 
 (2) I think you mean ^\s{3} not ^\S{3}
 
 (3) Now that you've seen how to do timings, over to you :-)
 
 Also, which is better (if different at all): \d\d or \d{2} ?
 Also, would matching . be different (performance-wise) than matching the 
 actual character, e.g. matching : ?
 And lastly, at the end of a line, is there any performance difference 
 between (.+)$ and (.+)
 
Of course if the log strings all begin with a string like Dec 12 2009
 then you don't need regular expressions at all - just pull the
characters out using their positions and slicing. The month would be
string[0:3] and so on.

regards
 Steve
-- 
Steve Holden+1 571 484 6266   +1 800 494 3119
Holden Web LLC  http://www.holdenweb.com/

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


WebDAV client module

2009-01-13 Thread Vincent Gulinao
Kindly point me to a good WebDAV client module for Python. Looks like PyDav
is popular, but it seems some of the modules used within were already
deprecated.

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


Re: Problems in Using C-API for Unicode handling

2009-01-13 Thread Steve Holden
abhi wrote:
 On Jan 13, 12:17 pm, Terry Reedy tjre...@udel.edu wrote:
 abhi wrote:
 Hi,
 I am trying to handle Unicode objects in C (Python 2.5.2). I am
 getting PyObjects from and want to coerce them to unicode objects. The
 documentation provides two APIs for that:
  PyUnicode_FromEncodedObject(PyObject *obj, const char *encoding,
 const char *errors)
  PyUnicode_FromObject(PyObject *obj)
 (http://www.python.org/doc/2.5.2/api/unicodeObjects.html)
 Now I want to utf-16 so I am trying to use the first one, but it is
 giving back NULL in case of PyObject is already Unicode type which is
 expected. What puzzles me is that PyUnicode_FromObject(PyObject *obj)
 is passing irrespective of type of PyObject. The API says it is
 Shortcut for PyUnicode_FromEncodedObject(obj, NULL, strict) but if I
 use that, it returns NULL where as PyUnicode_FromObject works.
 Is there any way by which I can take in any PyObject and convert it to
 utf-16 object? Any help is appreciated.
 Whether Unicode objects are utf-16 or utf=32 depends on your Python
 build.  You can always convert a byte string representation of an object
 to unicode.- Hide quoted text -

 - Show quoted text -
 
 Hi,
I agree with you. I have a Python unicode object in C (I don't know
 which utf) and I want to convert this explicitely to utf-16. Is there
 any way to do this?
 PyUnicode_FromEncodedObject(PyObject *obj, const char *encoding, const
 char *errors) says that obj can't be a unicode type so I guess I can't
 use this one, does anybody knows any other method by which I can
 achieve my goal?
 
I suspect that a Python Unicode object in C will be using either UCS-2
or UCS-4 representation, depending on the options your interpreter was
built with. So whatever else it is, it won't be UTF-anything. Don't know
whether that helps or not.

regards
 Steve
-- 
Steve Holden+1 571 484 6266   +1 800 494 3119
Holden Web LLC  http://www.holdenweb.com/

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


Re: Attaching a live interpreter to a script?

2009-01-13 Thread Benjamin Kaplan
On Tue, Jan 13, 2009 at 2:35 AM, Kannon neokan...@gmail.com wrote:

 I'm sure this has come up before, but my google-fu is just not strong
 enough to find it out of 10,000~ posts, and apologies if this is
 obvious.

 What I'd like to do is attach an interpreter to a program running from
 a script (I.E, not something I typed into the live interpreter). It'd
 be an awesome way to debug programs, as well as tweak parameters and
 such at runtime. Ideally, I'd like it to be something in pure python
 so I could throw it into Jython and IronPython as well. (Though, I can
 actually code Java and C#, so implementing it is something I could do
 myself, if needed.) I was thinking maybe something I could throw into
 a tkinter window or similar, but I wasn't able to find anything on how
 to pass stuff directly to the interpreter.

 Any ideas, or if this is even possible would be nice. Thanks in
 advance.



I don't know exactly what they did, but wxPython has a widget inspection
tool that includes such a thing. It lets you manipulate whatever widget is
selected in the tree control above the shell. The widget inspection tool is
in wx/lib/inspection.py and the code for the shell they use is in
wx/py/shell.py. I think it's pure python except for the gui.


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

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


Re: Is there a better algorithm?

2009-01-13 Thread Lie
On Jan 3, 4:38 am, mr mario.rugg...@gmail.com wrote:
 As has been noted, the best is to fix the input to be regular-3-
 tuples. For the fun of it, here's another variation of a solution:

snip

Yet another solution:

for i in l:
k, u, v = i[0], None if len(i) == 2 else i[1], i[-1]

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


Re: Is there a better algorithm?

2009-01-13 Thread Lie
On Jan 3, 11:55 am, Kottiyath n.kottiy...@gmail.com wrote:
 On Jan 3, 2:38 am, mr mario.rugg...@gmail.com wrote:

snip
 It is a code to post some data to HTML server.
 Even though usually the POST values are of type(name, value), if file
 transfer is involved, then POST values change to (name, filename,
 value).
 My view was that since filename is a rare occurance and doesnt make
 sense in a usual POST, I had not kept it as a full 3 tuple.
 Since so many programmers (that too much more capable than me) are
 suggesting that it is code smell, I am reviewing my decision.

Is it possible to change (name, filename, value) into (name, value,
filename) instead?

In most cases optional arguments should be last. There are some very
exceptional case, where early optional argument might be better, like
python's range, which type signature is: [[start,] stop[, step]] (i.e.
with one argument, it is assigned to stop, instead of start)
--
http://mail.python.org/mailman/listinfo/python-list


Python-URL! - weekly Python news and links (Jan 13)

2009-01-13 Thread Gabriel Genellina
QOTW:  If Jack Valenti had been around at the time of Gutenberg he
would have organized the monks to come and burn down the printing
press' :-). - Information Technology Association of America president
Harris Miller


Dynamic creation of instance attributes may adversely affect code
readability:

http://groups.google.com/group/comp.lang.python/browse_thread/thread/68bc54bca830c46/

A detective story: getting meaningful data out of a mix of base64, UTF-16,
and gzip:

http://groups.google.com/group/comp.lang.python/browse_thread/thread/d84f42493fe81864/

http://groups.google.com/group/comp.lang.python/browse_thread/thread/d72d883409764559/

Analyzing existing alternatives to extend Python with C or C++ code:

http://groups.google.com/group/comp.lang.python/browse_thread/thread/4f9afb5b4bdfaac6/

This long post on PyObjC from late in 2004 is worth re-reading
for its remarks on Macintosh, architecture, and getting things
to work:
http://mail.python.org/pipermail/pythonmac-sig/2004-October/012023.html

Handling multiple child processes:

http://groups.google.com/group/comp.lang.python/browse_thread/thread/80a8edb06c3b7f1b/

How to create an instance of the right subclass:

http://groups.google.com/group/comp.lang.python/browse_thread/thread/636d0f516e9850c9/



Everything Python-related you want is probably one or two clicks away in
these pages:

Python.org's Python Language Website is the traditional
center of Pythonia
http://www.python.org
Notice especially the master FAQ
http://www.python.org/doc/FAQ.html

PythonWare complements the digest you're reading with the
marvelous daily python url
 http://www.pythonware.com/daily

Just beginning with Python?  This page is a great place to start:
http://wiki.python.org/moin/BeginnersGuide/Programmers

The Python Papers aims to publish the efforts of Python enthusiats:
http://pythonpapers.org/
The Python Magazine is a technical monthly devoted to Python:
http://pythonmagazine.com

Readers have recommended the Planet sites:
http://planetpython.org
http://planet.python.org

comp.lang.python.announce announces new Python software.  Be
sure to scan this newsgroup weekly.
http://groups.google.com/group/comp.lang.python.announce/topics

Python411 indexes podcasts ... to help people learn Python ...
Updates appear more-than-weekly:
http://www.awaretek.com/python/index.html

The Python Package Index catalogues packages.
http://www.python.org/pypi/

The somewhat older Vaults of Parnassus ambitiously collects references
to all sorts of Python resources.
http://www.vex.net/~x/parnassus/

Much of Python's real work takes place on Special-Interest Group
mailing lists
http://www.python.org/sigs/

Python Success Stories--from air-traffic control to on-line
match-making--can inspire you or decision-makers to whom you're
subject with a vision of what the language makes practical.
http://www.pythonology.com/success

The Python Software Foundation (PSF) has replaced the Python
Consortium as an independent nexus of activity.  It has official
responsibility for Python's development and maintenance.
http://www.python.org/psf/
Among the ways you can support PSF is with a donation.
http://www.python.org/psf/donations/

The Summary of Python Tracker Issues is an automatically generated
report summarizing new bugs, closed ones, and patch submissions. 

http://search.gmane.org/?author=status%40bugs.python.orggroup=gmane.comp.python.develsort=date

Although unmaintained since 2002, the Cetus collection of Python
hyperlinks retains a few gems.
http://www.cetus-links.org/oo_python.html

Python FAQTS
http://python.faqts.com/

The Cookbook is a collaborative effort to capture useful and
interesting recipes.
http://code.activestate.com/recipes/langs/python/

Many Python conferences around the world are in preparation.
Watch this space for links to them.

Among several Python-oriented RSS/RDF feeds available, see:
http://www.python.org/channews.rdf
For more, see:
http://www.syndic8.com/feedlist.php?ShowMatch=pythonShowStatus=all
The old Python To-Do List now lives principally in a
SourceForge reincarnation.
http://sourceforge.net/tracker/?atid=355470group_id=5470func=browse
http://www.python.org/dev/peps/pep-0042/

del.icio.us presents an intriguing approach to reference commentary.
It already aggregates quite a bit of Python intelligence.
http://del.icio.us/tag/python

*Py: the Journal of the Python Language*
http://www.pyzine.com

Dr.Dobb's 

RE: Relative performance of comparable regular expressions

2009-01-13 Thread Barak, Ron
Hi John,

Thanks for the below - teaching me how to fish  ( instead of just giving me 
a fish :-)
Now I could definitely get the answers for myself, and also be a bit more 
enlightened.

As for your (2) remark below (on my question: Which would give better 
performance, matching with ^[a-zA-Z]{3}, or with ^\S{3} ?):
(2) I think you mean ^\s{3} not ^\S{3},
I actually did meant to use \S, namely - a character that is not a white-space.

Bye,
Ron.

-Original Message-
From: John Machin []
Sent: Tuesday, January 13, 2009 11:15
To: python-list@python.org
Subject: Re: Relative performance of comparable regular expressions

On Jan 13, 7:24 pm, Barak, Ron ron.ba...@lsi.com wrote:
 Hi,

 I have a question about relative performance of comparable regular 
 expressions.

 I have large log files that start with three letters month names 
 (non-unicode).

 Which would give better performance, matching with  ^[a-zA-Z]{3}, or with 
 ^\S{3} ?

(1) If you want to match at the start of a line, use re.match()
*without* the pointless ^. Don't use re.search with a pattern starting with 
^ -- it won't be any faster than and it could be a lot worse; re.search 
doesn't know to stop if the first match fails:

command-prompt\python26\python -m timeit -simport re;rx=re.compile
('^AB')
;text='Z'*100 rx.match(text)
100 loops, best of 3: 1.15 usec per loop

command-prompt\python26\python -m timeit -simport re;rx=re.compile
('^AB')
;text='Z'*100 rx.search(text)
10 loops, best of 3: 4.47 usec per loop

command-prompt\python26\python -m timeit -simport re;rx=re.compile
('^AB')
;text='Z'*1000 rx.search(text)
1 loops, best of 3: 34.1 usec per loop

(2) I think you mean ^\s{3} not ^\S{3}

(3) Now that you've seen how to do timings, over to you :-)

 Also, which is better (if different at all): \d\d or \d{2} ?
 Also, would matching . be different (performance-wise) than matching the 
 actual character, e.g. matching : ?
 And lastly, at the end of a line, is there any performance difference between 
 (.+)$ and (.+)

Cheers,
John

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


Python threading

2009-01-13 Thread Brian Allen Vanderburg II
I'm doing some multi-threaded programming and before diving into the 
C/C++ code I though I'd do some in Python first.  I decided to read 
through the threading module and I understand some of it, but I don't 
understand this, though I'm sure it is easy:


The condition object has a method _is_owned, which is called if the lock 
doesn't have one.  The RLock does have one but a regular lock not.  It 
is supposed to return true if the lock is owned by the current thread:


   def _is_owned(self):
   # Return True if lock is owned by currentThread.
   # This method is called only if __lock doesn't have _is_owned().
   if self.__lock.acquire(0):
   self.__lock.release()
   return False
   else:
   return True

It seems that for a condition without an RLock but a Lock, 
self.__lock.acquire(0) will return False even if the lock is owned by 
another thread other than the current thread, so _is_owned would return 
True even if the lock is owned by another thread.


B. Vanderburg II
--
http://mail.python.org/mailman/listinfo/python-list


Re: urllib2 - 403 that _should_ not occur.

2009-01-13 Thread Philip Semanchuk


On Jan 13, 2009, at 1:22 AM, Steve Holden wrote:


Philip Semanchuk wrote:


On Jan 12, 2009, at 6:48 PM, ajaksu wrote:


On Jan 11, 11:59 pm, James Mills prolo...@shortcircuit.net.au
wrote:

Hey all,

The following fails for me:


from urllib2 import urlopen
f =
urlopen(http://groups.google.com/group/chromium-announce/feed/rss_v2_0_msgs.xml 
)




Traceback (most recent call last):

[...]

Any helpful ideas ?


Maybe raise a real bug @ bugs.python.org instead of just  
mentioning it

like I did: http://bugs.python.org/msg77889

I think at least some sites would be willing to add the new UA to
their whitelists.


I don't think I understand you clearly. Whether or not Google et al
whitelist the Python UA isn't a Python issue, is it?

I'd say it's an issue relevant to Python users, which woudl seem to  
put

it pretty much in the mainstream for c.l.py - especially as the code
causing concern was written in Python.


I didn't mean to imply that the conversation didn't belong here. I  
think that is perfectly appropriate. What I don't understand is the  
suggestion that Google's server config should be raised as a bug  
against Python. (i.e. raise a real bug @ bugs.python.org...)





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


Re: urllib2 - 403 that _should_ not occur.

2009-01-13 Thread Steve Holden
Philip Semanchuk wrote:
 
 On Jan 13, 2009, at 1:22 AM, Steve Holden wrote:
 
 Philip Semanchuk wrote:

 On Jan 12, 2009, at 6:48 PM, ajaksu wrote:

 On Jan 11, 11:59 pm, James Mills prolo...@shortcircuit.net.au
 wrote:
 Hey all,

 The following fails for me:

 from urllib2 import urlopen
 f =
 urlopen(http://groups.google.com/group/chromium-announce/feed/rss_v2_0_msgs.xml;)



 Traceback (most recent call last):
 [...]
 Any helpful ideas ?

 Maybe raise a real bug @ bugs.python.org instead of just mentioning it
 like I did: http://bugs.python.org/msg77889

 I think at least some sites would be willing to add the new UA to
 their whitelists.

 I don't think I understand you clearly. Whether or not Google et al
 whitelist the Python UA isn't a Python issue, is it?

 I'd say it's an issue relevant to Python users, which woudl seem to put
 it pretty much in the mainstream for c.l.py - especially as the code
 causing concern was written in Python.
 
 I didn't mean to imply that the conversation didn't belong here. I think
 that is perfectly appropriate. What I don't understand is the suggestion
 that Google's server config should be raised as a bug against Python.
 (i.e. raise a real bug @ bugs.python.org...)
 
Oh, I see! Yes, it's hard to know what actions anyone could take on such
a bug report. I suppose the documentation could be modified to describe
how some services require specific agents, but that wouldn't help a huge
amount.

regards
 Steve
-- 
Steve Holden+1 571 484 6266   +1 800 494 3119
Holden Web LLC  http://www.holdenweb.com/

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


exec in a nested function yields an error

2009-01-13 Thread TP
Hi everybody,

Try the following program:


def f():
def f_nested():
exec a=2
print a
f()


It yields an error.
$ python nested_exec.py
  File nested_exec.py, line 3
exec a=2
SyntaxError: unqualified exec is not allowed in function 'f_nested' it is a
nested function

What is the problem?
Why?

Thanks

-- 
python -c print ''.join([chr(154 - ord(c)) for c in '*9(9(18%.\
91+,\'Z4(55l4('])

When a distinguished but elderly scientist states that something is
possible, he is almost certainly right. When he states that something is
impossible, he is very probably wrong. (first law of AC Clarke)
--
http://mail.python.org/mailman/listinfo/python-list


Ethernet packet size python

2009-01-13 Thread K-man
Hi,
  I am sending data using the socket interface in python, but I want
to know how big the ethernet packet size is (in bytes).  I didn't
really see a way using the socket library of how to do this.  Any
suggestions?
--
http://mail.python.org/mailman/listinfo/python-list


Embedding Python with non-filesystem code source?

2009-01-13 Thread Ryan Oltman
Background:
I'm trying to develop some certification software in C/C++ that would
allow engineers/technicians to quickly develop scripts/functions in
Python to verify a product (there could be hundreds of functions per
product). I would like to use SQLite db to store the functions and the
necessary certification reference data.

Question:
I have successfully embedded python code in C/C++ using the
PyEval_CallObject when using a file as the module source,but is it
possible using the PyImport_ImportModule  PyObject_GetAttrString to
point to a non-filesystem source?  Is there another way to setup the
PyEval_CallObject that would allow this capability?

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


Converting from PyUnicodeObject to char * without calling C API

2009-01-13 Thread skip
I'm trying to convert Python's gdbinit file to Python 3.  One of the things
it does is print filenames and function names when displaying stack frames.
This worked fine in Python 2 because the type of such objects is
PyStringObject which uses NUL-terminated strings under the covers.  For
example:

set $__fn = (char *)((PyStringObject *)co-co_filename)-ob_sval

In Python 3 co-co_filename is a PyUnicodeObject pointer with the raw data
encoded as either UCS2 or UCS4.  This presents problems when displaying
strings:

(gdb) set $__f = (PyUnicodeObject *)(co-co_filename)
(gdb) p *$__f-s...@$__f-length
$14 = {47, 85, 115, 101, 114, 115, 47, 115, 107, 105, 112, 47, 115, 114, 
99, 47, 112, 121, 116, 104, 111, 110, 47, 112, 121, 51, 107, 45, 116, 47, 76, 
105, 98, 47, 95, 119, 101, 97, 107, 114, 101, 102, 115, 101, 116, 46, 112, 121}

(gdb) p *(char *)($__f-str)@$__f-length
$15 = 
/\000U\000s\000e\000r\000s\000/\000s\000k\000i\000p\000/\000s\000r\000c\000/\000p\000y\000t\000h\000o\000n\000/\000p

(gdb) p *(char *)($__f-str)@($__f-length*2)
$16 = 
/\000U\000s\000e\000r\000s\000/\000s\000k\000i\000p\000/\000s\000r\000c\000/\000p\000y\000t\000h\000o\000n\000/\000p\000y\0003\000k\000-\000t\000/\000L\000i\000b\000/\000_\000w\000e\000a\000k\000r\000e\000f\000s\000e\000t\000.\000p\000y


I'd like to get rid of those NULs when displaying names.  Making it more
difficult, I'd like to do it without calling any C API functions.  If at all
possible the user-defined commands should work even if there is no process
available.  (e.g., when working with core files).

Any suggestions?

-- 
Skip Montanaro - s...@pobox.com - http://smontanaro.dyndns.org/
--
http://mail.python.org/mailman/listinfo/python-list


Re: Converting from PyUnicodeObject to char * without calling C API

2009-01-13 Thread MRAB

s...@pobox.com wrote:

I'm trying to convert Python's gdbinit file to Python 3.  One of the things
it does is print filenames and function names when displaying stack frames.
This worked fine in Python 2 because the type of such objects is
PyStringObject which uses NUL-terminated strings under the covers.  For
example:

set $__fn = (char *)((PyStringObject *)co-co_filename)-ob_sval

In Python 3 co-co_filename is a PyUnicodeObject pointer with the raw data
encoded as either UCS2 or UCS4.  This presents problems when displaying
strings:

(gdb) set $__f = (PyUnicodeObject *)(co-co_filename)
(gdb) p *$__f-s...@$__f-length
$14 = {47, 85, 115, 101, 114, 115, 47, 115, 107, 105, 112, 47, 115, 114, 
99, 47, 112, 121, 116, 104, 111, 110, 47, 112, 121, 51, 107, 45, 116, 47, 76, 
105, 98, 47, 95, 119, 101, 97, 107, 114, 101, 102, 115, 101, 116, 46, 112, 121}

(gdb) p *(char *)($__f-str)@$__f-length
$15 = 
/\000U\000s\000e\000r\000s\000/\000s\000k\000i\000p\000/\000s\000r\000c\000/\000p\000y\000t\000h\000o\000n\000/\000p

(gdb) p *(char *)($__f-str)@($__f-length*2)
$16 = 
/\000U\000s\000e\000r\000s\000/\000s\000k\000i\000p\000/\000s\000r\000c\000/\000p\000y\000t\000h\000o\000n\000/\000p\000y\0003\000k\000-\000t\000/\000L\000i\000b\000/\000_\000w\000e\000a\000k\000r\000e\000f\000s\000e\000t\000.\000p\000y


I'd like to get rid of those NULs when displaying names.  Making it more
difficult, I'd like to do it without calling any C API functions.  If at all
possible the user-defined commands should work even if there is no process
available.  (e.g., when working with core files).

Should you be using char * when they aren't char? Is there a wide char 
type of some sort?

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


Re: Ethernet packet size python

2009-01-13 Thread Steve Holden
K-man wrote:
 Hi,
   I am sending data using the socket interface in python, but I want
 to know how big the ethernet packet size is (in bytes).  I didn't
 really see a way using the socket library of how to do this.  Any
 suggestions?

There is no way to know what size Ethernet packets will result from
specific traffic. Or do you want to know the MTU size (largest possible
Ethernet packet size)? This shouldn't really matter, since large TCP
messages will be split into a sequence of IP datagrams, and large IP
datagrams will be automatically fragmented and then reassembled at the
other end.

Is there a specific reason this is important to you?

regards
 Steve
-- 
Steve Holden+1 571 484 6266   +1 800 494 3119
Holden Web LLC  http://www.holdenweb.com/

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


Re: exec in a nested function yields an error

2009-01-13 Thread Albert Hopkins
On Tue, 2009-01-13 at 16:13 +0100, TP wrote:
 Hi everybody,
 
 Try the following program:
 
 
 def f():
 def f_nested():
 exec a=2
 print a
 f()
 
 
 It yields an error.
 $ python nested_exec.py
   File nested_exec.py, line 3
 exec a=2
 SyntaxError: unqualified exec is not allowed in function 'f_nested' it is a
 nested function
 
 What is the problem?
 Why?
 

I believe because in the above example what namespace is local for the
exec is ambiguous so you need to specify it explicitly.

Also, and I'm sure you know this, exec can be dangerous and should be
used wisely/rarely.

 

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


Re: Ternary operator and tuple unpacking -- What am I missing ?

2009-01-13 Thread Scott David Daniels

imageguy wrote:


1)  n = None
2)  c,d = n if n is not None else 0,0

...


This is more easily expressed as:
c, d = n or (0, 0)
--
http://mail.python.org/mailman/listinfo/python-list


Re: Simple CGI-XMLRPC failure

2009-01-13 Thread Mike MacHenry
I figured it was some kind of bug. Must be either a bug with my
version of either the library (most likely) or perhaps some weird
environment setting that I have set incorrectly (also likely). How can
I figure out which version of SimpleXMLRPCServer I'm running? Do you
run Ubuntu by any chance? If you which version?

Does anyone know of any environment settings I could look into on
Apache or Python?

-mike

On Mon, Jan 12, 2009 at 9:02 PM, Jeff McNeil j...@jmcneil.net wrote:
 On Jan 12, 12:40 pm, Mike MacHenry dski...@ccs.neu.edu wrote:
 I am having a difficult time understanding why my very simple
 CGI-XMLRPC test isn't working. I created a server to export two
 functions, the built-in function pow and my own identity function
 i. I run a script to call both of them and the pow work fine but
 the i gives me an error that says my XMLRPC server doesn't support
 than name. Here is the code for both files and the output:

 #!/usr/bin/env python
 #This file is /usr/lib/cgi-bin/roundwarerpc.py
 from SimpleXMLRPCServer import CGIXMLRPCRequestHandler
 def i(x):
 return x
 server = CGIXMLRPCRequestHandler()
 server.register_function(pow)
 server.register_function(i)
 server.handle_request()

 #!/usr/bin/env python
 #This file is ~/test.py
 import xmlrpclib
 server = xmlrpclib.ServerProxy(http://localhost/cgi-bin/roundwarerpc.py;)
 print server.pow(2,3)
 print server.i(10)

 #This is the STDOUT and STDERR when running ~/test.py
 dski...@dskippy-laptop:$ python test.py 8
 Traceback (most recent call last):
   File test.py, line 4, in module
 print server.test(10)
   File /usr/lib/python2.5/xmlrpclib.py, line 1147, in __call__
 return self.__send(self.__name, args)
   File /usr/lib/python2.5/xmlrpclib.py, line 1437, in __request
 verbose=self.__verbose
   File /usr/lib/python2.5/xmlrpclib.py, line 1201, in request
 return self._parse_response(h.getfile(), sock)
   File /usr/lib/python2.5/xmlrpclib.py, line 1340, in _parse_response
 return u.close()
   File /usr/lib/python2.5/xmlrpclib.py, line 787, in close
 raise Fault(**self._stack[0])
 xmlrpclib.Fault: Fault 1: 'type \'exceptions.Exception\':method i
 is not supported'

 Does anyone know what might be wrong with this?

 Thanks for the help,
 -mike

 p.s.
 Python 2.5.2 (r252:60911, Jul 31 2008, 17:28:52)
 [GCC 4.2.3 (Ubuntu 4.2.3-2ubuntu7)] on linux2
 Server version Apache/2.2.8 (Ubuntu)
 Server built: Jun 25 2008 13:54:13

 I copied your code verbatim and I don't have any issues with it at
 all. Same version of Python, same version of Apache.

 In SimpleXMLRPCServer.py, register_function adds directly to a
 self.funcs dictionary, so an instance variable of the same name
 shouldn't hurt anything.  That exception is only raised when a
 self.funcs lookup raises a KeyError unless you're registering an
 instance.

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

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


Could you suggest optimisations ?

2009-01-13 Thread Barak, Ron
Hi,

In the attached script, the longest time is spent in the following functions 
(verified by psyco log):

def match_generator(self,regex):

Generate the next line of self.input_file that
matches regex.

generator_ = self.line_generator()
while True:
self.file_pointer = self.input_file.tell()
if self.file_pointer != 0:
self.file_pointer -= 1
if (self.file_pointer + 2) = self.last_line_offset:
break
line_ = generator_.next()
print %.2f%%   \r % (((self.last_line_offset - 
self.input_file.tell()) / (self.last_line_offset * 1.0)) * 100.0),
if not line_:
break
else:
match_ = regex.match(line_)
groups_ = re.findall(regex,line_)
if match_:
yield line_.strip(\n), groups_

def get_matching_records_by_regex_extremes(self,regex_array):

Function will:
Find the record matching the first item of regex_array.
Will save all records until the last item of regex_array.
Will save the last line.
Will remember the position of the beginning of the next line in
self.input_file.

start_regex = regex_array[0]
end_regex = regex_array[len(regex_array) - 1]

all_recs = []
generator_ = self.match_generator

try:
match_start,groups_ = generator_(start_regex).next()
except StopIteration:
return(None)

if match_start != None:
all_recs.append([match_start,groups_])

line_ = self.line_generator().next()
while line_:
match_ = end_regex.match(line_)
groups_ = re.findall(end_regex,line_)
if match_ != None:
all_recs.append([line_,groups_])
return(all_recs)
else:
all_recs.append([line_,[]])
line_ = self.line_generator().next()

def line_generator(self):

Generate the next line of self.input_file, and update
self.file_pointer to the beginning of that line.

while self.input_file.tell() = self.last_line_offset:
self.file_pointer = self.input_file.tell()
line_ = self.input_file.readline()
if not line_:
break
yield line_.strip(\n)

I was trying to think of optimisations, so I could cut down on processing time, 
but got no inspiration.
(I need the print %.2f%%   \r ... line for user's feedback).

Could you suggest any optimisations ?
Thanks,
Ron.


P.S.: Examples of processing times are:

 *   2m42.782s  on two files with combined size of792544 bytes (no matches 
found).
 *   28m39.497s on two files with combined size of 4139320 bytes (783 matches 
found).

These times are quite unacceptable, as a normal input to the program would be 
ten files with combined size of ~17MB.


_failover_multiple_files_client.py
Description: _failover_multiple_files_client.py
--
http://mail.python.org/mailman/listinfo/python-list


Re: Ethernet packet size python

2009-01-13 Thread K-man
On Jan 13, 10:35 am, Steve Holden st...@holdenweb.com wrote:
 K-man wrote:
  Hi,
    I am sending data using the socket interface in python, but I want
  to know how big the ethernet packet size is (in bytes).  I didn't
  really see a way using the socket library of how to do this.  Any
  suggestions?

 There is no way to know what size Ethernet packets will result from
 specific traffic. Or do you want to know the MTU size (largest possible
 Ethernet packet size)? This shouldn't really matter, since large TCP
 messages will be split into a sequence of IP datagrams, and large IP
 datagrams will be automatically fragmented and then reassembled at the
 other end.

 Is there a specific reason this is important to you?

 regards
  Steve
 --
 Steve Holden        +1 571 484 6266   +1 800 494 3119
 Holden Web LLC              http://www.holdenweb.com/

I am trying to determine the actual network link speed.
--
http://mail.python.org/mailman/listinfo/python-list


Re: Ternary operator and tuple unpacking -- What am I missing ?

2009-01-13 Thread Paul Rubin
imageguy imageguy1...@gmail.com writes:
 Using py2.5.4 and entering the following lines in IDLE, I don't really
 understand why I get the result shown in line 8.
 
 Note the difference between lines 7 and 10 is that 'else' clause
 result enclosed in brackets, however, in line 2, both the 'c,d'
 variables are assign correctly without the brackets being required.

c,d = n if n is not None else 0,0

parses as:

c,d = (n if n is not None else 0), 0

In the case where n is None, c and d are both set to 0.

In the case where n is a tuple, c is set to the tuple and d is set to 0.

Does that help?
--
http://mail.python.org/mailman/listinfo/python-list


Re: Relative performance of comparable regular expressions

2009-01-13 Thread Chris Rebert
On Tue, Jan 13, 2009 at 6:16 AM, Barak, Ron ron.ba...@lsi.com wrote:
 Hi John,

 Thanks for the below - teaching me how to fish  ( instead of just giving
 me a fish :-)
 Now I could definitely get the answers for myself, and also be a bit more
 enlightened.

 As for your (2) remark below (on my question: Which would give better
 performance, matching with ^[a-zA-Z]{3}, or with ^\S{3} ?):
 (2) I think you mean ^\s{3} not ^\S{3},
 I actually did meant to use \S, namely - a character that is not a
 white-space.

(A) Please don't top-post, it makes replying to you more awkward and
makes it harder for readers to follow the conversation.

(B) But ^[a-zA-Z]{3}, and ^\S{3} aren't even equivalent! \S allows
*digits* and *punctuation* too, whereas the former *only* matches
letters.

Cheers,
Chris
-- 
Follow the path of the Iguana...
http://rebertia.com


 -Original Message-
 From: John Machin [
 ]
 Sent: Tuesday, January 13, 2009 11:15
 To: python-list@python.org
 Subject: Re: Relative performance of comparable regular expressions

 On Jan 13, 7:24 pm, Barak, Ron ron.ba...@lsi.com wrote:
 Hi,

 I have a question about relative performance of comparable regular
 expressions.

 I have large log files that start with three letters month names
 (non-unicode).

 Which would give better performance, matching with  ^[a-zA-Z]{3}, or
 with ^\S{3} ?

 (1) If you want to match at the start of a line, use re.match()
 *without* the pointless ^. Don't use re.search with a pattern starting
 with ^ -- it won't be any faster than and it could be a lot worse;
 re.search doesn't know to stop if the first match fails:

 command-prompt\python26\python -m timeit -simport re;rx=re.compile
 ('^AB')
 ;text='Z'*100 rx.match(text)
 100 loops, best of 3: 1.15 usec per loop

 command-prompt\python26\python -m timeit -simport re;rx=re.compile
 ('^AB')
 ;text='Z'*100 rx.search(text)
 10 loops, best of 3: 4.47 usec per loop

 command-prompt\python26\python -m timeit -simport re;rx=re.compile
 ('^AB')
 ;text='Z'*1000 rx.search(text)
 1 loops, best of 3: 34.1 usec per loop

 (2) I think you mean ^\s{3} not ^\S{3}

 (3) Now that you've seen how to do timings, over to you :-)

 Also, which is better (if different at all): \d\d or \d{2} ?
 Also, would matching . be different (performance-wise) than matching the
 actual character, e.g. matching : ?
 And lastly, at the end of a line, is there any performance difference
 between (.+)$ and (.+)

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


ctype problem

2009-01-13 Thread Grimson
hello out there,
I have a problem with c-types.
I made a c-library, which expects a pointer to a self defined structure.

let the funtion call myfunction(struct interface* iface)

and the struct:
struct interface
{
int a;
int b;
char *c;
}

the Python ctype port of this structur would be:

class INTERFACE(Structure):
_fields_ = [(a c_int),
  (b,c_int),
  (c, c_char)]

in my python-struct a create a instance of INTERFACE

myiface = INTERFACE()
myiface.a = ctypes.c_int(80)
myiface.b = ctypes.c_int(22)
...
than I make a pointer onto it.
p_iface = ctypes.pointer(myiface)
and I tried it also with a reference
r_iface = ctypes.byref(myiface)

but neither myclib.myfunction(p_iface) nor myclib.myfunction(r_iface)
works properly. The function is been called but it reads only zeros (0)
for each parameter (member in the struct).

Where is my fault?

Thank you..

sincerely chris


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


Standard IPC for Python?

2009-01-13 Thread Laszlo Nagy
I would like to develop some module for Python for IPC. Socket 
programming howto recommends that for local communication, and I 
personally experienced problems with TCP (see my previous post: Slow 
network).


I was looking for semaphores and shared memory, but it is not in the 
standard lib. I was also thinking about unix domain sockets, but it is 
not available under windows.


I was looking for non-standard modules as well, but only found a few 
references with insufficient information. For example:


http://code.activestate.com/recipes/519626/

The question is: what is the standard way to implement fast and portable 
IPC with Python? Are there tools in the standard lib that can do this?



Thanks,

  Laszlo

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


Re: Standard IPC for Python?

2009-01-13 Thread Philip Semanchuk


On Jan 13, 2009, at 11:25 AM, Laszlo Nagy wrote:

I would like to develop some module for Python for IPC. Socket  
programming howto recommends that for local communication, and I  
personally experienced problems with TCP (see my previous post:  
Slow network).


I was looking for semaphores and shared memory, but it is not in the  
standard lib. I was also thinking about unix domain sockets, but it  
is not available under windows.


I was looking for non-standard modules as well, but only found a few  
references with insufficient information. For example:


http://code.activestate.com/recipes/519626/

The question is: what is the standard way to implement fast and  
portable IPC with Python? Are there tools in the standard lib that  
can do this?


They're not in the standard lib, but I have 3 modules for Python IPC.  
They're all Unix-only.


posix_ipc gives you semaphores, shared memory and messages queues:
http://semanchuk.com/philip/posix_ipc/

sysv_ipc gives you semaphores and shared memory:
http://semanchuk.com/philip/sysv_ipc/

shm also gives access to SysV semaphores and shared memory:
http://nikitathespider.com/python/shm/

The only reason to use shm over the sysv_ipc module is that shm  
supports versions of Python  2.5. I'm not developing shm any further,  
so avoid using it if possible.


Here's an overview that compares the three:
http://semanchuk.com/philip/PythonIpc/


The overview of the overview is that posix_ipc is the one to use if  
you can because it is the simplest. sysv_ipc is more broadly supported  
but is more complicated for programmer (me) and user (you) alike.


Bugs reports are of course welcome.


Enjoy!

Philip


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


Re: Standard IPC for Python?

2009-01-13 Thread Diez B. Roggisch
Laszlo Nagy wrote:

 I would like to develop some module for Python for IPC. Socket
 programming howto recommends that for local communication, and I
 personally experienced problems with TCP (see my previous post: Slow
 network).
 
 I was looking for semaphores and shared memory, but it is not in the
 standard lib. I was also thinking about unix domain sockets, but it is
 not available under windows.
 
 I was looking for non-standard modules as well, but only found a few
 references with insufficient information. For example:
 
 http://code.activestate.com/recipes/519626/
 
 The question is: what is the standard way to implement fast and portable
 IPC with Python? Are there tools in the standard lib that can do this?

I use Pyro. Has always been fast enough for me. It spares you the troubles
of bloated XML-documents other RPC-mechanisms use.

Of course it is RPC, not only IPC - so it comes with a tradeoff. But so
far, it has been always fast enough for me.

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


Re: urllib2 - 403 that _should_ not occur.

2009-01-13 Thread Falcolas
On Jan 11, 6:59 pm, James Mills prolo...@shortcircuit.net.au
wrote:
 Hey all,

 The following fails for me:

  from urllib2 import urlopen
  f = 
  urlopen(http://groups.google.com/group/chromium-announce/feed/rss_v2_0_msgs.xml;)

For what it's worth, I've had a similar problem with the urlopen as
well. Using the library default urlopen results in an error, but if I
build an opener with the basic handlers, it works just fine.

 import urllib2
 f = urllib2.urlopen(http://localhost:8000;)
Traceback (most recent call last):
  File pyshell#1, line 1, in module
f = urllib2.urlopen(http://localhost:8000;)
  File C:\Python25\lib\urllib2.py, line 121, in urlopen
return _opener.open(url, data)
  File C:\Python25\lib\urllib2.py, line 380, in open
response = meth(req, response)
  File C:\Python25\lib\urllib2.py, line 491, in http_response
'http', request, response, code, msg, hdrs)
  File C:\Python25\lib\urllib2.py, line 418, in error
return self._call_chain(*args)
  File C:\Python25\lib\urllib2.py, line 353, in _call_chain
result = func(*args)
  File C:\Python25\lib\urllib2.py, line 499, in http_error_default
raise HTTPError(req.get_full_url(), code, msg, hdrs, fp)
HTTPError: HTTP Error 403: Forbidden
 opener = urllib2.OpenerDirector()
 opener.add_handler(urllib2.HTTPHandler())
 opener.add_handler(urllib2.HTTPDefaultErrorHandler())
 f = opener.open(http://localhost:8000;)
 f.read()
'something relevant'
--
http://mail.python.org/mailman/listinfo/python-list


Re: Problems in Using C-API for Unicode handling

2009-01-13 Thread Stefan Behnel
abhi wrote:
 Now I want to utf-16 so I am trying to use the first one, but it is
 giving back NULL in case of PyObject is already Unicode type which is
 expected. What puzzles me is that PyUnicode_FromObject(PyObject *obj)
 is passing irrespective of type of PyObject. The API says it is
 Shortcut for PyUnicode_FromEncodedObject(obj, NULL, strict) but if I
 use that, it returns NULL where as PyUnicode_FromObject works.
 
 Is there any way by which I can take in any PyObject and convert it to
 utf-16 object? Any help is appreciated.

Use PyUnicode_FromObject() to convert the (non-string) object to a unicode
object, then encode the unicode object as UTF-16 using the respecive
functions in the codecs API (see the bottom of the C-API docs page for the
unicode object).

Note, however, that you will not succeed to convert a byte string to the
corresponding unicode string using PyUnicode_FromObject(), except in the
simple case where the string is ASCII encoded. Doing this right requires
explicit decoding using a byte encoding that you must specify (again, see
the codecs API).

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


basic python list/dict/key question/issues..

2009-01-13 Thread bruce
Hi..

quite new to python, and have a couple of basic question:

i have 
 (term:[1,2,3])

as i understand it, this is a list, yes/no?

how can i represent this as a dict/list?

i've got a few of these that i'm trying to deal with..

thanks


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


Re: Problems in Using C-API for Unicode handling

2009-01-13 Thread Scott David Daniels

abhi wrote:

On Jan 13, 12:17 pm, Terry Reedy tjre...@udel.edu wrote:

abhi wrote:

Hi,
I am trying to handle Unicode objects in C (Python 2.5.2)

... I want to convert this explicitely to utf-16

You are trying to get Unicode and UTF-16, whereas you should think
of those two as distinct.  UTF-16 is an encoded form (bytes) from
the abstract Unicode is characters.  What you want in Python is:
u'abc'.encode('UTF-16')
So look for something returning a string by invoking the decode method.

--Scott David Daniels
scott.dani...@acm.org
--
http://mail.python.org/mailman/listinfo/python-list


Re: basic python list/dict/key question/issues..

2009-01-13 Thread Roger
On Jan 13, 11:59 am, bruce bedoug...@earthlink.net wrote:
 Hi..

 quite new to python, and have a couple of basic question:

 i have
  (term:[1,2,3])

 as i understand it, this is a list, yes/no?

 how can i represent this as a dict/list?

 i've got a few of these that i'm trying to deal with..

 thanks
x = {term:[1,2,3]} is a dictionary and the value of the key
term is a list.
--
http://mail.python.org/mailman/listinfo/python-list


Re: basic python list/dict/key question/issues..

2009-01-13 Thread Albert Hopkins
On Tue, 2009-01-13 at 08:59 -0800, bruce wrote:
 Hi..
 
 quite new to python, and have a couple of basic question:
 
 i have 
  (term:[1,2,3])
 
 as i understand it, this is a list, yes/no?
 
No, that's invalid syntax:

 (term:[1,2,3])
  File stdin, line 1
(term:[1,2,3])
   ^
SyntaxError: invalid syntax

 how can i represent this as a dict/list?

[this,is,a,list]
{this:1, is:2, a: 3, dict: 3}
 
 i've got a few of these that i'm trying to deal with..

If you are new to python you might to read up on a tutorial.

http://docs.python.org/tutorial/




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


Re: Standard IPC for Python?

2009-01-13 Thread Laszlo Nagy


The only reason to use shm over the sysv_ipc module is that shm 
supports versions of Python  2.5. I'm not developing shm any further, 
so avoid using it if possible.

Hmm, we are using FreeBSD, Ubuntu and Windows. Unfortunately

- posix_ipc is broken under FreeBSD
- sysv_ipc does not support message queues at all
- shm is not maintained
- windows is not supported by any of these modules

It is so interesting that there is no standard implementation for IPC in 
Python. I would think it is a very common task for programmers. I might 
find myself writting a new IPC module that works under Windows as well.


Can anyone tell me if select.select works under OS X?

Thanks,

  Laszlo

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


Re: Standard IPC for Python?

2009-01-13 Thread Laszlo Nagy



I use Pyro. Has always been fast enough for me. It spares you the troubles
of bloated XML-documents other RPC-mechanisms use.

Of course it is RPC, not only IPC - so it comes with a tradeoff. But so
far, it has been always fast enough for me.
  
Unfortunately, I'm developing an ORB, and using another type of ORB to 
transport data would be silly. :-) I need the lower level stuff.


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


Re: i want to join developer group

2009-01-13 Thread gert
On Jan 13, 1:36 pm, gkc...@gmail.com wrote:
 Hello,
       I want to contribute to the open source projects.

Open source is about finding where you are good at. Contributing is a
product of the skills you learn.
--
http://mail.python.org/mailman/listinfo/python-list


Re: Standard IPC for Python?

2009-01-13 Thread Philip Semanchuk


On Jan 13, 2009, at 12:40 PM, Laszlo Nagy wrote:



The only reason to use shm over the sysv_ipc module is that shm  
supports versions of Python  2.5. I'm not developing shm any  
further, so avoid using it if possible.

Hmm, we are using FreeBSD, Ubuntu and Windows. Unfortunately

- posix_ipc is broken under FreeBSD


A clarification: the module posix_ipc is *not* broken. It exposes  
FreeBSD's implementation of POSIX IPC which has broken semaphores  
(based on my experiments, anyway). The practical result for you is the  
same but the difference is very important to me as the module author.  
Fixing the posix_ipc module is under my control, fixing FreeBSD's  
POSIX IPC is not.



- sysv_ipc does not support message queues at all


That's true, but in your original email you said you were looking for  
semaphores and shared memory. There was no mention of message queues.


I'm working on message queue support, but the Sys V IPC API is a  
headache and takes longer to code against than the POSIX API.




- shm is not maintained


SHM *is* maintained. As I said in my first email and as it says on the  
Web page, I'm not developing it any further. No further development !=  
unmaintained.


Also, it offers only a subset of the features in sysv_ipc, so its  
status is not of great concern unless you're using Python  2.5.




- windows is not supported by any of these modules


Windows uses a different API, but I think there are some packages that  
can make Windows look like some flavor of Unix. For instance, Cygwin  
seems to expose some relevant APIs like sem_open(), sem_post(), etc:

http://cygwin.com/cygwin-api/compatibility.html#std-susv3

It might be that posix_ipc would work with this package with just a  
little tweaking. I have no idea. As I said on the posix_ipc page, if  
anyone wants to tackle this one and send me bug reports, please do!



It is so interesting that there is no standard implementation for  
IPC in Python. I would think it is a very common task for  
programmers. I might find myself writting a new IPC module that  
works under Windows as well.


Feel free to reinvent the wheel. Or, you could pitch in and help with  
what's already out there.



bye
Philip




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


Re: exec in a nested function yields an error

2009-01-13 Thread Scott David Daniels

TP wrote:

...

 def f():

def f_nested():
exec a=2
print a
f()
... What is the problem? Why?


What it wants is you to provide the in context portion of the
exec statement.  I expect the reason it fails is that there is no
dictionary that is available as locals that encompasses the locals
of both f and f_nested, so you'll have to be explicit about what
you mean.

So, the code wants you to say something like:
def f():
def f_nested():
exec a=2 in globals(), locals()
print a
return f_nested
f()

--Scott David Daniels
scott.dani...@acm.org
--
http://mail.python.org/mailman/listinfo/python-list


Re: Standard IPC for Python?

2009-01-13 Thread Laszlo Nagy




- posix_ipc is broken under FreeBSD


A clarification: the module posix_ipc is *not* broken. It exposes 
FreeBSD's implementation of POSIX IPC which has broken semaphores 
(based on my experiments, anyway). The practical result for you is the 
same but the difference is very important to me as the module author. 
Fixing the posix_ipc module is under my control, fixing FreeBSD's 
POSIX IPC is not.

Mea culpa. :-) I did not want to offend you.

- sysv_ipc does not support message queues at all


That's true, but in your original email you said you were looking for 
semaphores and shared memory. There was no mention of message queues.
You are right again. :-) I would like to use IPC beacuse I want to send 
messages between processes.

- shm is not maintained


SHM *is* maintained. As I said in my first email and as it says on the 
Web page, I'm not developing it any further. No further development != 
unmaintained.

My bad again. :-( This is not my day.
It is so interesting that there is no standard implementation for IPC 
in Python. I would think it is a very common task for programmers. I 
might find myself writting a new IPC module that works under Windows 
as well.


Feel free to reinvent the wheel. Or, you could pitch in and help with 
what's already out there.


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


Re: Ethernet packet size python

2009-01-13 Thread Grant Edwards
On 2009-01-13, Steve Holden st...@holdenweb.com wrote:
 K-man wrote:

 I am sending data using the socket interface in python, but I
 want to know how big the ethernet packet size is (in bytes).
 I didn't really see a way using the socket library of how to
 do this.  Any suggestions?

 There is no way to know what size Ethernet packets will result
 from specific traffic.

Unless he's using a low level API such as AF_PACKET/SOCK_RAW.
If that's the case then the packets will be exactly as big as
he makes them. And he wouldn't be asking this question. :)

 Or do you want to know the MTU size (largest possible Ethernet
 packet size)? This shouldn't really matter, since large TCP
 messages will be split into a sequence of IP datagrams, and
 large IP datagrams will be automatically fragmented and then
 reassembled at the other end.

 Is there a specific reason this is important to you?

If it's just idle curiosity, then wireshark or tcpdump can show
one exactly what's going on on the wire.  A reading of 
http://en.wikipedia.org/wiki/Transmission_Control_Protocol
should also allow one to predict pretty accurately what's going
to happen when you call send().

-- 
Grant Edwards   grante Yow! I want EARS!  I want
  at   two ROUND BLACK EARS
   visi.comto make me feel warm
   'n secure!!
--
http://mail.python.org/mailman/listinfo/python-list


Re: Simple CGI-XMLRPC failure

2009-01-13 Thread Jeff McNeil
I don't have the version in front of me now as that was on my home
machine, but Python was the same right down to the revision number.
Unless you've mucked with it, it's the same file that I've got on my
box.

Jeff

On Jan 13, 10:51 am, Mike MacHenry dski...@ccs.neu.edu wrote:
 I figured it was some kind of bug. Must be either a bug with my
 version of either the library (most likely) or perhaps some weird
 environment setting that I have set incorrectly (also likely). How can
 I figure out which version of SimpleXMLRPCServer I'm running? Do you
 run Ubuntu by any chance? If you which version?

 Does anyone know of any environment settings I could look into on
 Apache or Python?

 -mike

 On Mon, Jan 12, 2009 at 9:02 PM, Jeff McNeil j...@jmcneil.net wrote:
  On Jan 12, 12:40 pm, Mike MacHenry dski...@ccs.neu.edu wrote:
  I am having a difficult time understanding why my very simple
  CGI-XMLRPC test isn't working. I created a server to export two
  functions, the built-in function pow and my own identity function
  i. I run a script to call both of them and the pow work fine but
  the i gives me an error that says my XMLRPC server doesn't support
  than name. Here is the code for both files and the output:

  #!/usr/bin/env python
  #This file is /usr/lib/cgi-bin/roundwarerpc.py
  from SimpleXMLRPCServer import CGIXMLRPCRequestHandler
  def i(x):
  return x
  server = CGIXMLRPCRequestHandler()
  server.register_function(pow)
  server.register_function(i)
  server.handle_request()

  #!/usr/bin/env python
  #This file is ~/test.py
  import xmlrpclib
  server = xmlrpclib.ServerProxy(http://localhost/cgi-bin/roundwarerpc.py;)
  print server.pow(2,3)
  print server.i(10)

  #This is the STDOUT and STDERR when running ~/test.py
  dski...@dskippy-laptop:$ python test.py 8
  Traceback (most recent call last):
File test.py, line 4, in module
  print server.test(10)
File /usr/lib/python2.5/xmlrpclib.py, line 1147, in __call__
  return self.__send(self.__name, args)
File /usr/lib/python2.5/xmlrpclib.py, line 1437, in __request
  verbose=self.__verbose
File /usr/lib/python2.5/xmlrpclib.py, line 1201, in request
  return self._parse_response(h.getfile(), sock)
File /usr/lib/python2.5/xmlrpclib.py, line 1340, in _parse_response
  return u.close()
File /usr/lib/python2.5/xmlrpclib.py, line 787, in close
  raise Fault(**self._stack[0])
  xmlrpclib.Fault: Fault 1: 'type \'exceptions.Exception\':method i
  is not supported'

  Does anyone know what might be wrong with this?

  Thanks for the help,
  -mike

  p.s.
  Python 2.5.2 (r252:60911, Jul 31 2008, 17:28:52)
  [GCC 4.2.3 (Ubuntu 4.2.3-2ubuntu7)] on linux2
  Server version Apache/2.2.8 (Ubuntu)
  Server built: Jun 25 2008 13:54:13

  I copied your code verbatim and I don't have any issues with it at
  all. Same version of Python, same version of Apache.

  In SimpleXMLRPCServer.py, register_function adds directly to a
  self.funcs dictionary, so an instance variable of the same name
  shouldn't hurt anything.  That exception is only raised when a
  self.funcs lookup raises a KeyError unless you're registering an
  instance.

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

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


Re: Converting from PyUnicodeObject to char * without calling C API

2009-01-13 Thread skip
MRAB Should you be using char * when they aren't char? Is there a
MRAB wide char type of some sort?

No, I shouldn't.  The storage is wchar_t *, what you get with my first
printed expression:

   (gdb) set $__f = (PyUnicodeObject *)(co-co_filename)
   (gdb) p *$__f-s...@$__f-length
   $14 = {47, 85, 115, 101, 114, 115, 47, 115, 107, 105, 112, 47, 115, 114, 99, 
47, 112, 121, 116, 104, 111, 110, 47, 112, 121, 51, 107, 45, 116, 47, 76, 105, 
98, 47, 95, 119, 101, 97, 107, 114, 101, 102, 115, 101, 116, 46, 112, 121}

That's not too readable.  I'm not sure I can do much with that without
having a process to use.  Ideally:

print .join([ chr(x) for x in *$__f-s...@$__f-length])

The question is, how do you do that in gdb-speak???

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


Re: Does Python really follow its philosophy of Readability counts?

2009-01-13 Thread Bruno Desthuilliers

Steven D'Aprano a écrit :

On Mon, 12 Jan 2009 13:36:07 -0800, Paul Rubin wrote:


Bruno Desthuilliers bdesth.quelquech...@free.quelquepart.fr writes:

Why on earth are you using Python if you don't like the way it work ???

Why on earth keep releasing new versions of Python if the old ones are
already perfect?


That's a fallacious argument. Nobody is arguing that any specific version 
of Python is perfect, but clearly many people do like the general design 
choices of the language, that is, the way it works.


Thanks for making my point clear.

*If* you don't like the way it works, and you have a choice in the 
matter, perhaps you should find another language that works more the way 
you would prefer.


On the other hand... Bruno's question is unfair. It is perfectly 
reasonable to (hypothetically) consider Python to be the best *existing* 
language while still wanting it to be improved (for some definition of 
improvement).


And that's the problem : what Paul suggests are not improvements but 
radical design changes. The resulting language - whatever it may be 
worth, I'm not making any judgement call here - would not be Python 
anymore.



Just because somebody has criticisms of Python, or a wish-
list of features, doesn't mean they hate the language. 


There's probably a whole range of nuances between not liking and 
hating. And Paul is of course perfectly right to think that a language 
having this and that features from Python, but not this other one, would 
be a better language (at least according to it's own definition of 
better). Where I totally disagree is that it would make *Python* better.


Also, my question was not that unfair (even if a bit provocative). I 
really wonder why peoples that seems to dislike one of the central 
features of Python - it's dynamism - still use it (assuming of course 
they are free to choose another language). And FWIW, I at least had a 
partial answer on this.

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


Re: Standard IPC for Python?

2009-01-13 Thread Philip Semanchuk


On Jan 13, 2009, at 1:22 PM, Laszlo Nagy wrote:





- posix_ipc is broken under FreeBSD


A clarification: the module posix_ipc is *not* broken. It exposes  
FreeBSD's implementation of POSIX IPC which has broken semaphores  
(based on my experiments, anyway). The practical result for you is  
the same but the difference is very important to me as the module  
author. Fixing the posix_ipc module is under my control, fixing  
FreeBSD's POSIX IPC is not.

Mea culpa. :-) I did not want to offend you.


Thank you, Laszlo, I appreciate that.




- sysv_ipc does not support message queues at all


That's true, but in your original email you said you were looking  
for semaphores and shared memory. There was no mention of message  
queues.
You are right again. :-) I would like to use IPC beacuse I want to  
send messages between processes.


If you can wait a bit and Windows is not crucial, sysv_ipc should have  
message queue support before too long.




- shm is not maintained


SHM *is* maintained. As I said in my first email and as it says on  
the Web page, I'm not developing it any further. No further  
development != unmaintained.

My bad again. :-( This is not my day.
It is so interesting that there is no standard implementation for  
IPC in Python. I would think it is a very common task for  
programmers. I might find myself writting a new IPC module that  
works under Windows as well.


Feel free to reinvent the wheel. Or, you could pitch in and help  
with what's already out there.


I realize that lack of Windows support is a big minus for both of  
these modules. As I said, any help getting either posix_ipc or  
sysv_ipc working under Windows would be much appreciated. It sounds  
like you have access to the platform and incentive to see it working,  
so dig in if you like.


Once I get message queues working for SysV, I want to support unnamed  
semaphores in the POSIX module and then I'll consider these modules  
mainly feature-complete. After that I can think about adding more  
features like:

- Windows support
- Python 2.6/3.0 support
- buffer-style access to shared memory (slicing, etc.)
- Merging these two into a generic ipc module that's agnostic about  
POSIX versus Sys V. That might not be realistic.


If I have time in the future, I'll tackle these items myself. But  
these projects are just educational for me and paying work will take  
priority.



bye
Philip




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


Re: Standard IPC for Python?

2009-01-13 Thread Laszlo Nagy




I realize that lack of Windows support is a big minus for both of 
these modules. As I said, any help getting either posix_ipc or 
sysv_ipc working under Windows would be much appreciated. It sounds 
like you have access to the platform and incentive to see it working, 
so dig in if you like.
Maybe I can help with windows. I just need to figure out what to use: 
pipes or windows sockets?


http://msdn.microsoft.com/en-us/library/aa365574(VS.85).aspx


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


Re: Standard IPC for Python?

2009-01-13 Thread Philip Semanchuk


On Jan 13, 2009, at 2:01 PM, Laszlo Nagy wrote:





I realize that lack of Windows support is a big minus for both of  
these modules. As I said, any help getting either posix_ipc or  
sysv_ipc working under Windows would be much appreciated. It sounds  
like you have access to the platform and incentive to see it  
working, so dig in if you like.
Maybe I can help with windows. I just need to figure out what to  
use: pipes or windows sockets?


http://msdn.microsoft.com/en-us/library/aa365574(VS.85).aspx


I was suggesting getting posix_ipc or sysv_ipc to compile against a  
compatibility library (Cygwin?) under Windows. It sounds like you're  
proposing something totally different, no?



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


Python 3.0 urllib.parse.parse_qs results in TypeError

2009-01-13 Thread ag73
Hi,

I am trying to parse data posted to a Python class that extends
http.server.BaseHTTPRequestHandler. Here is the code I am using:

def do_POST(self):
ctype, pdict = cgi.parse_header(self.headers['Content-Type'])
length = int(self.headers['Content-Length'])
if ctype == 'application/x-www-form-urlencoded':
qs = self.rfile.read(length)
print(qs=+str(qs))
form = urllib.parse.parse_qs(qs, keep_blank_values=1)

The print statement shows the following output, so it looks like the
data is being posted correctly:

qs=b'file_data=b
%27IyEvdXNyL2Jpbi9lbnYgcHl0aG9uCiMgZW5jb2Rpbmc6IHV0Zi04CiIiIgp1bnRpdGxlZC5weQoK
%5CnQ3JlYXRlZCBieSBBbmR5IEdyb3ZlIG9uIDIwMDgtMTItMDIuCkNvcHlyaWdodCAoYykgMjAwOCBf
%5CnX015Q29tcGFueU5hbWVfXy4gQWxsIHJpZ2h0cyByZXNlcnZlZC4KIiIiCgppbXBvcnQgc3lzCmlt
%5CncG9ydCBvcwoKCmRlZiBtYWluKCk6CglwcmludCAibmFtZTE9dmFsdWUxIgoJcHJpbnQgIm5hbWUy
%5CnPXZhbHVlMiIKCgppZiBfX25hbWVfXyA9PSAnX19tYWluX18nOgoJbWFpbigpCgo%3D
%5Cn%27filename=test.py'

However, the last line of code that calls parse_qs causes the
following exception to be thrown:

class 'TypeError'
Type str doesn't support the buffer API

I haven't been able to find any information on the web about this. Any
pointers would be appreciated. I am using ActivePython 3.0 and have
tried this on Linux and Max OS X with the same outcome.

Thanks,

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


Re: Standard IPC for Python?

2009-01-13 Thread Laszlo Nagy




I was suggesting getting posix_ipc or sysv_ipc to compile against a 
compatibility library (Cygwin?) under Windows. It sounds like you're 
proposing something totally different, no?
OK I see. But probably I do not want to use Cygwin because that would 
create another dependency. I understand that posix_ipc/sysv is not 
natively supported under windows. What about this:


- create a wrapper, using ctypes, /windll / cdll/ to access API functions
- use CreateFileMapping on the page file to create shared memory (a la 
windows: http://msdn.microsoft.com/en-us/library/aa366537.aspx)
- use CreateEvent/WaitForSingleObject for signaling 
(http://msdn.microsoft.com/en-us/library/ms682396(VS.85).aspx)
- these should be enough to implement shared memory functions and 
message queues under windows, and that might be a quick solution at 
least for me.
- it might also be used to emulate the same posix_ipc interface, without 
any external dependency added (cygwin).


All I care about is to create a working message queue. But if you think 
that this ctypes hack would be useful for other users, then I can try to 
implement it.


I must tell you that I'm not very familiar with C programming (it was a 
long time ago...) and I do not own MSVC.


(Hmm, can I compile this with mingw?)

Laszlo

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


Read binary file and dump data in

2009-01-13 Thread Santiago Romero

 Hi.

 Until now, all my python programs worked with text files. But now I'm
porting an small old C program I wrote lot of years ago to python and
I'm having problems with datatypes (I think).

 some C code:

 fp = fopen( file, rb);
 while !feof(fp)
 {
value = fgetc(fp);
printf(%d, value );
 }

 I started writing:

 fp = open(file, rb)
 data = fp.read()
 for i in data:
   print %d,  % (int(i))

 But it complains about i not being an integer... . len(data) shows
exactly the file size, so maybe is a type cast problem... :-?

 What's the right way to work with the binary data (read 1 byte values
and work with them, dumping them as an integer in this case)?

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


Re: Read binary file and dump data in

2009-01-13 Thread Albert Hopkins
On Tue, 2009-01-13 at 12:02 -0800, Santiago Romero wrote:
 Hi.
 
  Until now, all my python programs worked with text files. But now I'm
 porting an small old C program I wrote lot of years ago to python and
 I'm having problems with datatypes (I think).
 
  some C code:
 
  fp = fopen( file, rb);
  while !feof(fp)
  {
 value = fgetc(fp);
 printf(%d, value );
  }
 
  I started writing:
 
  fp = open(file, rb)
  data = fp.read()
  for i in data:
print %d,  % (int(i))
 
  But it complains about i not being an integer... . len(data) shows
 exactly the file size, so maybe is a type cast problem... :-?
 

int() expects something that looks like an integer.  E.g.

int(2)   = 2
int(2.0) = 2
int('2') = 2
int('c') = ValueError

If you are reading arbitrary bytes then it will likely not always look
like integers. What you probably meant is:

for i in data:
   print %d,  % ord(i)

But if you are really dealing with C-like data structures then you might
be better off using the struct module.

-a


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


Re: Ternary operator and tuple unpacking -- What am I missing ?

2009-01-13 Thread imageguy
On Jan 13, 1:01 am, Miles semantic...@gmail.com wrote:
 On Tue, Jan 13, 2009 at 12:02 AM, imageguy imageguy1...@gmail.com wrote:
  Using py2.5.4 and entering the following lines in IDLE, I don't really
  understand why I get the result shown in line 8.

  Note the difference between lines 7 and 10 is that 'else' clause
  result enclosed in brackets, however, in line 2, both the 'c,d'
  variables are assign correctly without the brackets being required.

  1)  n = None
  2)  c,d = n if n is not None else 0,0
  3)  print c,d, type(c), type(d)
  4) 0 0 type 'int' type 'int'

 The ternary expression has higher precedence than the comma, so the
 actual effect of line 2 (and 8) is:

  c, d = (n if n is not None else 0), 0

 Or written more explicitly:

  c = n if n is not None else 0
  d = 0

 So the only correct way to write the expression, for the result you
 want, is to use your line 10:

  10)   c,d = n if n is not None else (0,0)

 But if you're struggling with the precedence issues, I'd recommend
 ditching ternary expressions altogether and using full conditional
 blocks.

 -Miles

Thanks.
Hadn't thought through the operator precedence and the affect of the
comma.
This was the first time I tried to use with tuples, so will be more
careful next time
or stick to control blocks.

g.

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


Re: Read binary file and dump data in

2009-01-13 Thread Chris Rebert
On Tue, Jan 13, 2009 at 12:02 PM, Santiago Romero srom...@gmail.com wrote:

  Hi.

  Until now, all my python programs worked with text files. But now I'm
 porting an small old C program I wrote lot of years ago to python and
 I'm having problems with datatypes (I think).

  some C code:

  fp = fopen( file, rb);
  while !feof(fp)
  {
value = fgetc(fp);
printf(%d, value );
  }

  I started writing:

  fp = open(file, rb)
  data = fp.read()
  for i in data:
   print %d,  % (int(i))

  But it complains about i not being an integer... . len(data) shows
 exactly the file size, so maybe is a type cast problem... :-?

  What's the right way to work with the binary data (read 1 byte values
 and work with them, dumping them as an integer in this case)?

Albert already pointed out the problem with using int(), so I'll just
say that you might be interested in the `struct` module:
http://docs.python.org/library/struct.html

Cheers,
Chris

-- 
Follow the path of the Iguana...
http://rebertia.com
--
http://mail.python.org/mailman/listinfo/python-list


Re: Does Python really follow its philosophy of Readability counts?

2009-01-13 Thread Russ P.
On Jan 13, 9:47 am, Bruno Desthuilliers
bdesth.quelquech...@free.quelquepart.fr wrote:
 Steven D'Aprano a écrit :

  On Mon, 12 Jan 2009 13:36:07 -0800, Paul Rubin wrote:

  Bruno Desthuilliers bdesth.quelquech...@free.quelquepart.fr writes:
  Why on earth are you using Python if you don't like the way it work ???
  Why on earth keep releasing new versions of Python if the old ones are
  already perfect?

  That's a fallacious argument. Nobody is arguing that any specific version
  of Python is perfect, but clearly many people do like the general design
  choices of the language, that is, the way it works.

 Thanks for making my point clear.

  *If* you don't like the way it works, and you have a choice in the
  matter, perhaps you should find another language that works more the way
  you would prefer.

  On the other hand... Bruno's question is unfair. It is perfectly
  reasonable to (hypothetically) consider Python to be the best *existing*
  language while still wanting it to be improved (for some definition of
  improvement).

 And that's the problem : what Paul suggests are not improvements but
 radical design changes. The resulting language - whatever it may be
 worth, I'm not making any judgement call here - would not be Python
 anymore.

  Just because somebody has criticisms of Python, or a wish-
  list of features, doesn't mean they hate the language.

 There's probably a whole range of nuances between not liking and
 hating. And Paul is of course perfectly right to think that a language
 having this and that features from Python, but not this other one, would
 be a better language (at least according to it's own definition of
 better). Where I totally disagree is that it would make *Python* better.

 Also, my question was not that unfair (even if a bit provocative). I
 really wonder why peoples that seems to dislike one of the central
 features of Python - it's dynamism - still use it (assuming of course
 they are free to choose another language). And FWIW, I at least had a
 partial answer on this.

I think the issue here is the distinction between hacking and software
engineering. I may be misusing the term hacking, but I do not mean
it in a pejoritive sense. I just mean getting things done fast without
a lot of concern for safety, security, and long-term maintainability
and scalability. I'm not a computer scientist, but it seems to me that
Python is great for hacking and good for software engineering, but it
is not ideal for software engineering.

What Paul is suggesting, I think, is that Python should move in the
direction of software engineering. Whether that can be done without
compromising its hacking versatility is certainly a valid question,
but if it can be done, then why not do it?

Certainly one basic principle of software engineering is data
encapsulation. Tacking new attributes onto class instances all over
the place may be convenient and useful in many cases, but it is not
consistent with good software engineering. If the programmer could
somehow disallow it in certain classes, that could be useful,
providing that those who wish to continue doing it would be free to do
so. If class attributes could somehow be declared private, that would
be useful too. Optional explicit type declarations could also be
useful -- and I believe Python does have that now, so no need to argue
about that.

Why do I continue to use Python when I have so many complaints about
it? As everyone here knows, it has many good qualities. My work falls
somewhere in the middle between hacking and software engineering. I
am developing a research prototype of a safety-critical system. A
research prototype is not safety-critical itself, and it needs to be
flexible enough to try new ideas quickly, but it also needs to serve
as a model for a well-engineered system. Is Python the right choice? I
think so, but I don't know for sure.
--
http://mail.python.org/mailman/listinfo/python-list


Reminder: Calgary Python User Group - 1st Meeting tomorrow - Wed Jan 14

2009-01-13 Thread Greg
Our first meeting is tomorrow night at:

Good Earth Cafe, 1502 11 Street SW, Calgary, AB
Wed Jan 14, 7pm - 8pm

Topic: Google App Engine

http://www.google.com/calendar/event?eid=Z2Q0cDdpYmJobzVzbzZobXJxbTc2OHUxYW9fMjAwOTAxMTVUMDIwMDAwWiBhZG1pbkBweXRob25jYWxnYXJ5LmNvbQctz=America/Edmonton

Google Group / mailing list:
http://groups.google.ca/group/pythoncalgary

Website:
http://www.pythoncalgary.com/

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


Re: Standard IPC for Python?

2009-01-13 Thread drobi...@gmail.com
On Jan 13, 2:37 pm, Philip Semanchuk phi...@semanchuk.com wrote:
 On Jan 13, 2009, at 2:01 PM, Laszlo Nagy wrote:



  I realize that lack of Windows support is a big minus for both of  
  these modules. As I said, any help getting either posix_ipc or  
  sysv_ipc working under Windows would be much appreciated. It sounds  
  like you have access to the platform and incentive to see it  
  working, so dig in if you like.
  Maybe I can help with windows. I just need to figure out what to  
  use: pipes or windows sockets?

 http://msdn.microsoft.com/en-us/library/aa365574(VS.85).aspx

 I was suggesting getting posix_ipc or sysv_ipc to compile against a  
 compatibility library (Cygwin?) under Windows. It sounds like you're  
 proposing something totally different, no?

It's not really correct to call Cygwin a compatibility library. It's
more of a separate system.
In any case, the current version (1.5.25) does not support sem_unlink
or shm_unlink so posix_ipc does not build. Cygwin 1.7, currently under
test, will support these.  I haven't tried it yet. I expect it will
work OOTB.
--
http://mail.python.org/mailman/listinfo/python-list


Re: Standard IPC for Python?

2009-01-13 Thread Aaron Brady
On Jan 13, 2:04 pm, Laszlo Nagy gand...@shopzeus.com wrote:
 - create a wrapper, using ctypes, /windll / cdll/ to access API functions
 - use CreateFileMapping on the page file to create shared memory (a la
 windows:http://msdn.microsoft.com/en-us/library/aa366537.aspx)
 - use CreateEvent/WaitForSingleObject for signaling
 (http://msdn.microsoft.com/en-us/library/ms682396(VS.85).aspx)
 - these should be enough to implement shared memory functions and
 message queues under windows, and that might be a quick solution at
 least for me.
 - it might also be used to emulate the same posix_ipc interface, without
 any external dependency added (cygwin).

 All I care about is to create a working message queue. But if you think
 that this ctypes hack would be useful for other users, then I can try to
 implement it.

 I must tell you that I'm not very familiar with C programming (it was a
 long time ago...) and I do not own MSVC.

 (Hmm, can I compile this with mingw?)

 Laszlo

Yes.  The flags in the Extending/Embedding C++ section work with
mingw.

You can create inheritable pipes with the CreatePipe API.  'mmap'
provides shared memory, and it's a standard module.  You may also like
the 'multiprocessing' module, which comes with Python 2.6.
--
http://mail.python.org/mailman/listinfo/python-list


Re: Ternary operator and tuple unpacking -- What am I missing ?

2009-01-13 Thread John Machin
On Jan 13, 5:36 pm, Steve Holden st...@holdenweb.com wrote:
 Miles wrote:
  On Tue, Jan 13, 2009 at 12:02 AM, imageguy imageguy1...@gmail.com wrote:
  Using py2.5.4 and entering the following lines in IDLE, I don't really
  understand why I get the result shown in line 8.

  Note the difference between lines 7 and 10 is that 'else' clause
  result enclosed in brackets, however, in line 2, both the 'c,d'
  variables are assign correctly without the brackets being required.

  1)  n = None
  2)  c,d = n if n is not None else 0,0
  3)  print c,d, type(c), type(d)
  4) 0 0 type 'int' type 'int'

  The ternary expression has higher precedence than the comma, so the
  actual effect of line 2 (and 8) is:

  c, d = (n if n is not None else 0), 0

  Or written more explicitly:

  c = n if n is not None else 0
  d = 0

  So the only correct way to write the expression, for the result you
  want, is to use your line 10:

  10)   c,d = n if n is not None else (0,0)

  But if you're struggling with the precedence issues, I'd recommend
  ditching ternary expressions altogether and using full conditional
  blocks.

 Yet another great example of why Guido was right to resist putting
 conditional expressions into Python for so long (and wrong to succumb to
 the demand).

I thought I said Nobody mention the war! 

IMO this is just an example of why (1) in general people who are
unsure of operator precedence should use parentheses and (2) in
particular it's not a good idea to try to write tuples without
parentheses in any but the simpler cases like a, b = b, a

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


Re: ctype problem

2009-01-13 Thread Aaron Brady
On Jan 13, 10:22 am, Grimson grim...@gmx.de wrote:
 hello out there,
 I have a problem with c-types.
 I made a c-library, which expects a pointer to a self defined structure.

 let the funtion call myfunction(struct interface* iface)

 and the struct:
 struct interface
 {
     int a;
     int b;
     char *c;

 }

 the Python ctype port of this structur would be:

 class INTERFACE(Structure):
     _fields_ = [(a         c_int),
                       (b,        c_int),
                       (c,         c_char)]

 in my python-struct a create a instance of INTERFACE

 myiface = INTERFACE()
 myiface.a = ctypes.c_int(80)
 myiface.b = ctypes.c_int(22)
 ...
 than I make a pointer onto it.
 p_iface = ctypes.pointer(myiface)
 and I tried it also with a reference
 r_iface = ctypes.byref(myiface)

 but neither myclib.myfunction(p_iface) nor myclib.myfunction(r_iface)
 works properly. The function is been called but it reads only zeros (0)
 for each parameter (member in the struct).

 Where is my fault?

 Thank you..

 sincerely chris

Did you remember to define myclib.myfunction.argtypes= [ INTERFACE ] ?
--
http://mail.python.org/mailman/listinfo/python-list


Re: Does Python really follow its philosophy of Readability counts?

2009-01-13 Thread Paul Rubin
Bruno Desthuilliers bdesth.quelquech...@free.quelquepart.fr writes:
 And that's the problem : what Paul suggests are not improvements but
 radical design changes.

Eh?  I think of them as moderate and incremental improvements, in a
direction that Python is already moving in.  Radical would be
something like a full-scale static type system.

 I really wonder why peoples that seems to dislike one of the central
 features of Python - it's dynamism - still use it (assuming of
 course they are free to choose another language).

I certainly don't think dynamism is central to Python.  In what I see
as well-developed Python programming style, it's something that is
only rarely used in any important way.  I'd spend much less time
debugging if I got compiler warnings whenever I used dynamism without
a suitable annotation.  The 1% of the time where I really want to use
dynamism I don't see any problem with putting in an appropriate
decorator, superclass, or whatever.
--
http://mail.python.org/mailman/listinfo/python-list


Re: Standard IPC for Python?

2009-01-13 Thread Philip Semanchuk


On Jan 13, 2009, at 3:04 PM, Laszlo Nagy wrote:





I was suggesting getting posix_ipc or sysv_ipc to compile against a  
compatibility library (Cygwin?) under Windows. It sounds like  
you're proposing something totally different, no?
OK I see. But probably I do not want to use Cygwin because that  
would create another dependency. I understand that posix_ipc/sysv is  
not natively supported under windows. What about this:


- create a wrapper, using ctypes, /windll / cdll/ to access API  
functions
- use CreateFileMapping on the page file to create shared memory (a  
la windows: http://msdn.microsoft.com/en-us/library/aa366537.aspx)
- use CreateEvent/WaitForSingleObject for signaling (http://msdn.microsoft.com/en-us/library/ms682396(VS.85).aspx 
)
- these should be enough to implement shared memory functions and  
message queues under windows, and that might be a quick solution at  
least for me.


Python has the mmap module which might work a lot like shared memory.  
I'm not clear on the differences, honestly.


Named pipes might be a cleaner way to implement message queues:
http://msdn.microsoft.com/en-us/library/aa365590(VS.85).aspx


- it might also be used to emulate the same posix_ipc interface,  
without any external dependency added (cygwin).


It'd be nice to have message queues/named pipes working under Windows.

If I were you, I'd steer clear of calling them something specific like  
SysV or POSIX messages queues. That will create an expectation of  
certain semantics, and you might find it difficult to fulfill that  
promise in certain cases (e.g. implementing the POSIX function  
mq_notify() ). If you call them Nagy message queues then no one will  
be disappointed or surprised as long as your code implements FIFO IPC.



Bye
Philip


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


Re: Standard IPC for Python?

2009-01-13 Thread Philip Semanchuk


On Jan 13, 2009, at 4:31 PM, drobi...@gmail.com wrote:


On Jan 13, 2:37 pm, Philip Semanchuk phi...@semanchuk.com wrote:

I was suggesting getting posix_ipc or sysv_ipc to compile against a
compatibility library (Cygwin?) under Windows. It sounds like you're
proposing something totally different, no?


It's not really correct to call Cygwin a compatibility library. It's
more of a separate system.


Thanks for the education; I'm obviously not very familiar with it.


In any case, the current version (1.5.25) does not support sem_unlink
or shm_unlink so posix_ipc does not build. Cygwin 1.7, currently under
test, will support these.  I haven't tried it yet. I expect it will
work OOTB.


Thanks for the report. Strange that it supports the functions to open  
but not close semaphores. IN any case, I'd be very happy if posix_ipc  
or sysv_ipc would work with few or no modifications under Cygwin.



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


Re: Python 3.0 urllib.parse.parse_qs results in TypeError

2009-01-13 Thread John Machin
On Jan 14, 6:54 am, ag73 andygrov...@gmail.com wrote:
 Hi,

 I am trying to parse data posted to a Python class that extends
 http.server.BaseHTTPRequestHandler. Here is the code I am using:

         def do_POST(self):
                 ctype, pdict = cgi.parse_header(self.headers['Content-Type'])
                 length = int(self.headers['Content-Length'])
                 if ctype == 'application/x-www-form-urlencoded':
                         qs = self.rfile.read(length)
                         print(qs=+str(qs))
                         form = urllib.parse.parse_qs(qs, keep_blank_values=1)

 The print statement shows the following output, so it looks like the
 data is being posted correctly:

 qs=b'file_data=b
 %27IyEvdXNyL2Jpbi9lbnYgcHl0aG9uCiMgZW5jb2Rpbmc6IHV0Zi04CiIiIgp1bnRpdGxlZC5w­eQoK
 %5CnQ3JlYXRlZCBieSBBbmR5IEdyb3ZlIG9uIDIwMDgtMTItMDIuCkNvcHlyaWdodCAoYykgMjA­wOCBf
 %5CnX015Q29tcGFueU5hbWVfXy4gQWxsIHJpZ2h0cyByZXNlcnZlZC4KIiIiCgppbXBvcnQgc3l­zCmlt
 %5CncG9ydCBvcwoKCmRlZiBtYWluKCk6CglwcmludCAibmFtZTE9dmFsdWUxIgoJcHJpbnQgIm5­hbWUy
 %5CnPXZhbHVlMiIKCgppZiBfX25hbWVfXyA9PSAnX19tYWluX18nOgoJbWFpbigpCgo%3D
 %5Cn%27filename=test.py'

 However, the last line of code that calls parse_qs causes the
 following exception to be thrown:

 class 'TypeError'
 Type str doesn't support the buffer API

Please show the full traceback.
--
http://mail.python.org/mailman/listinfo/python-list


Pydev 1.4.2 Released

2009-01-13 Thread Fabio Zadrozny
Hi All,

Pydev and Pydev Extensions 1.4.2 have been released

Details on Pydev Extensions: http://www.fabioz.com/pydev
Details on Pydev: http://pydev.sf.net
Details on its development: http://pydev.blogspot.com

Release Highlights in Pydev Extensions:
-

* Context insensitive code-completion working with multiple interpreters
* Fixed code analysis problem on staticmethod
* Giving proper warning on version mismatch
* Remote debugger fix


Release Highlights in Pydev:
--

* Interpreter can be configured on a per-project basis
* Jython 2.5b0 properly supported
* Find definition working for Jython builtins
* Run: can be python/jython even if it doesn't match the interpreter
configured for the project
* Fixed problem on find definition if one of the interpreters was not configured
* Fixed halting condition that could occur on code-completion
* __file__ available in code-completion
* Reorganized preferences (removed editor preferences from the root)
* Preferences for showing hover info
* Fixed problem when formatting binary operator that was in a new line
* When converting spaces to tabs (and vice-versa), the number of
spaces for each tab is asked
* Debugger
  o When finishing the user code debugging, it doesn't step into
the debugger code anymore
  o Fixes for working with Jython
  o Fix for Python 3.0 integration (could not resolve variables)



What is PyDev?
---

PyDev is a plugin that enables users to use Eclipse for Python and
Jython development -- making Eclipse a first class Python IDE -- It
comes with many goodies such as code completion, syntax highlighting,
syntax analysis, refactor, debug and many others.


Cheers,

-- 
Fabio Zadrozny
--
Software Developer

Aptana
http://aptana.com/python

Pydev Extensions
http://www.fabioz.com/pydev

Pydev - Python Development Enviroment for Eclipse
http://pydev.sf.net
http://pydev.blogspot.com
--
http://mail.python.org/mailman/listinfo/python-list


Re: Scheduled Tasks - SetFlags

2009-01-13 Thread Roger Upole

kj7ny wrote:
 How do I enable/disable a scheduled task using Python?

 I can get to a task:

self.ts=pythoncom.CoCreateInstance
 (taskscheduler.CLSID_CTaskScheduler,None,pythoncom.CLSCTX_INPROC_SERVER,taskscheduler.IID_ITaskScheduler)
self.ts.SetTargetComputer(u'SomeServer')
self.tasks=self.ts.Enum()
for task in self.tasks:
self.t=self.ts.Activate(task)
if self.t.GetAccountInformation().lower().find('SomeUser')
=0:
print self.t.GetFlags()

 I can find if a task is enabled or not and toggle it:

disabled=self.t.GetFlags() 
 taskscheduler.TASK_FLAG_DISABLED == taskscheduler.TASK_FLAG_DISABLED
print 'Originally Disabled: %s'%(disabled,)
if disabled:
self.t.SetFlags(self.t.GetFlags() 
 ~taskscheduler.TASK_FLAG_DISABLED)
else:
self.t.SetFlags(self.t.GetFlags() |
 taskscheduler.TASK_FLAG_DISABLED)
disabled=self.t.GetFlags() 
 taskscheduler.TASK_FLAG_DISABLED == taskscheduler.TASK_FLAG_DISABLED
print 'Recheck Disabled: %s'%(disabled,)

 ... Recheck Disabled shows that the value says it has been
 disabled.

 The problem is that if I run it again and again, Originally Disabled
 is always the same.  In other words, Recheck Disabled does NOT
 stick.  It's like I'm missing a .commit()


Your missing commit is spelled

pf=t.QueryInterface(pythoncom.IID_IPersistFile)
pf.Save(None,1)

   Roger




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


Weird behaviour re: Python on Windows

2009-01-13 Thread Kevin Jing Qiu
I've been experiencing weird behavior of Python's os module on Windows:

Here's the environment:
Box1: Running Windows 2003 Server with Apache+mod_python
Box2: Running Windows 2003 Server with Zope/Plone and Z:\ mapped to D:\
on Box1

It appears any os calls that deals with file/dir on the mapped drive is
problematic. More specifically, os.stat(path), os.path.exists(path)
gives 'File not found' when path is on the mapped drive. Wait, this gets
better: when I'm in the debug mode (or Python interactive shell),
everything works fine! Os.stat, os.path.exists, os.fstat, etc, worked as
expected. I tried PyWin32 extension's GetFileSize() method and the same
thing happens: debug or interactive mode works fine, but when Plone is
run in non-debug mode, the problems begin.

I swear this isn't a Plone problem either. I switched the configuration.
Now Box1 has a network drive mapped to Box2's D:\. The custom Apache
filter we have (which is written in python executed by mod_python)
complains that it can't find the file which is on the mapped drive.

So there you go. I'm wondering if anyone else experienced such problem.
Any suggestions or input is appreciated!

Thanks,

Kevin

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


Re: Python 3.0 urllib.parse.parse_qs results in TypeError

2009-01-13 Thread Andy Grove
On Jan 13, 3:08 pm, John Machin sjmac...@lexicon.net wrote:

 Please show the full traceback.

John,

Thanks. Here it is:

  File /Library/Frameworks/Python.framework/Versions/3.0/lib/
python3.0/socketserver.py, line 281, in _handle_request_noblock
self.process_request(request, client_address)
  File /Library/Frameworks/Python.framework/Versions/3.0/lib/
python3.0/socketserver.py, line 307, in process_request
self.finish_request(request, client_address)
  File /Library/Frameworks/Python.framework/Versions/3.0/lib/
python3.0/socketserver.py, line 320, in finish_request
self.RequestHandlerClass(request, client_address, self)
  File /Library/Frameworks/Python.framework/Versions/3.0/lib/
python3.0/socketserver.py, line 614, in __init__
self.handle()
  File /Library/Frameworks/Python.framework/Versions/3.0/lib/
python3.0/http/server.py, line 363, in handle
self.handle_one_request()
  File /Library/Frameworks/Python.framework/Versions/3.0/lib/
python3.0/http/server.py, line 357, in handle_one_request
method()
  File /Users/andy/Development/EclipseWorkspace/dbsManage/kernel.py,
line 178, in do_POST
form = urllib.parse.parse_qs(qs, keep_blank_values=1)
  File /Library/Frameworks/Python.framework/Versions/3.0/lib/
python3.0/urllib/parse.py, line 351, in parse_qs

for name, value in parse_qsl(qs, keep_blank_values,
strict_parsing):
  File /Library/Frameworks/Python.framework/Versions/3.0/lib/
python3.0/urllib/parse.py, line 377, in parse_qsl
pairs = [s2 for s1 in qs.split('') for s2 in s1.split(';')]
TypeError: Type str doesn't support the buffer API
--
http://mail.python.org/mailman/listinfo/python-list


Re: python3.0 MySQLdb

2009-01-13 Thread Martin v. Löwis
Steve Holden wrote:
 Daniel Fetchinson wrote:
 I need something to connect to a database, preferably mysql, that
 works in python3.0 please.
 And your question is?


 Surely it's fairly obvious that the question is does such a thing
 exist, and if so where can I find it?. 

Interestingly enough, the question was slightly (but importantly)
different, though: the question really was Does anybody has a patch for
MySQLdb?; as my reference to the existing interface to PostgreSQL
was not sufficient for the OP.

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


'Import sys' succeeds in C++ embedded code, but module is not fully visible

2009-01-13 Thread Ben Sizer
I have the following C++ code and am attempting to embed Python 2.5,
but although the import sys statement works, attempting to reference
sys.path from inside a function after that point fails. It's as if
it's not treating it as a normal module but as any other global
variable which I'd have to explicitly qualify.


Py_InitializeEx(0); // the zero skips registration of signal
handlers.
PyObject* ourNamespace_ = PyDict_New();
PyDict_SetItemString(ourNamespace_, __builtins__,
PyEval_GetBuiltins());
PyObject* locals = PyDict_New();

const char* scriptStr =
print '1'\n
import sys\n
print sys.path\n
def debug_path_info():\n
print 'These are the directories Python looks into for
modules and source files:'\n
print '2'\n
for folder in sys.path:\n
print folder\n
print '--'\n
print 'This would be your present working folder/
directory:'\n
print '3'\n
print sys.path[0]\n
debug_path_info()\n;

PyObject* scriptResult = PyRun_String(
scriptStr,  // Python code to execute
Py_file_input,
ourNamespace_,   // globals dictionary
locals);// locals dictionary

if (!scriptResult)
{
std::cerr  Python error:   Unhandled Python exception 
from
script.  std::endl;
PyErr_Print();
}
else
{
Py_DECREF(scriptResult); // don't need result any more
}

Py_DECREF(locals);
Py_DECREF(ourNamespace_);
Py_Finalize();


And the output is like this:

1
['E:\\code\\Python25\\lib\\site-packages\\turbokid-1.0.4-py2.5.egg',
'E:\\code\\
Python25\\lib\\site-packages\\turbocheetah-1.0-py2.5.egg', 'E:\\code\
\Python25\\
lib\\site-packages\\simplejson-1.8.1-py2.5-win32.egg', 'E:\\code\
\Python25\\lib\
\site-packages\\ruledispatch-0.5a0.dev_r2306-py2.5-win32.egg', 'E:\
\code\\Python
25\\lib\\site-packages\\pastescript-1.6.2-py2.5.egg', 'E:\\code\
\Python25\\lib\\
site-packages\\formencode-1.0.1-py2.5.egg', 'E:\\code\\Python25\\lib\
\site-packa
ges\\decoratortools-1.7-py2.5.egg', 'E:\\code\\Python25\\lib\\site-
packages\\con
figobj-4.5.2-py2.5.egg', 'E:\\code\\Python25\\lib\\site-packages\
\cherrypy-2.3.0
-py2.5.egg', 'E:\\code\\Python25\\lib\\site-packages\\kid-0.9.6-
py2.5.egg', 'E:\
\code\\Python25\\lib\\site-packages\\cheetah-2.0.1-py2.5-win32.egg',
'E:\\code\\
Python25\\lib\\site-packages\\pyprotocols-1.0a0-py2.5-win32.egg', 'E:\
\code\\Pyt
hon25\\lib\\site-packages\\pastedeploy-1.3.1-py2.5.egg', 'E:\\code\
\Python25\\li
b\\site-packages\\paste-1.6-py2.5.egg', 'E:\\code\\Python25\\lib\\site-
packages\
\sqlobject-0.10.0-py2.5.egg', 'E:\\code\\Python25\\lib\\site-packages\
\tgfastdat
a-0.9a7-py2.5.egg', 'E:\\code\\Python25\\lib\\site-packages\
\webhelpers-0.6-py2.
5.egg', 'E:\\code\\Python25\\lib\\site-packages\\shove-0.1.3-
py2.5.egg', 'E:\\co
de\\Python25\\lib\\site-packages\\boto-1.3a-py2.5.egg', 'E:\\code\
\Python25\\lib
\\site-packages\\sqlalchemy-0.5.0beta3-py2.5.egg', 'E:\\code\\Python25\
\lib\\sit
e-packages\\turbojson-1.1.4-py2.5.egg', 'E:\\code\\Python25\\lib\\site-
packages\
\setuptools-0.6c9-py2.5.egg', 'E:\\code\\Python25\\lib\\site-packages\
\turbogear
s-1.0.8-py2.5.egg', 'C:\\WINDOWS\\system32\\python25_d.zip', 'E:\\code\
\Python25
\\Lib', 'E:\\code\\Python25\\DLLs', 'E:\\code\\Python25\\Lib\\lib-tk',
'e:\\Visu
al Studio 2008\\Projects\\StacklessEmbed\\StacklessEmbed', 'e:\\Visual
Studio 20
08\\Projects\\StacklessEmbed\\Debug', 'E:\\code\\Python25', 'E:\\code\
\Python25\
\lib\\site-packages', 'E:\\code\\Python25\\lib\\site-packages\\PIL',
'E:\\code\\
Python25\\lib\\site-packages\\wx-2.8-msw-unicode']
These are the directories Python looks into for modules and source
files:
2
Python error: Unhandled Python exception from script.
Traceback (most recent call last):
  File string, line 13, in module
  File string, line 7, in debug_path_info
NameError: global name 'sys' is not defined
[12532 refs]


(Incidentally, the Stackless references are because I was originally
trying to embed Stackless, but I reverted to vanilla 2.5 to see if it
was a Stackless specific issue, which it appears not.)

Another interesting thing is that sys.path[0] doesn't appear to be the
current working directory, despite several sources online suggesting
it should be.

What am I doing wrong?

--
Ben Sizer












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


Re: Does Python really follow its philosophy of Readability counts?

2009-01-13 Thread Carl Banks
On Jan 13, 4:03 pm, Paul Rubin http://phr...@nospam.invalid wrote:
 Bruno Desthuilliers bdesth.quelquech...@free.quelquepart.fr writes:
  And that's the problem : what Paul suggests are not improvements but
  radical design changes.

 Eh?  I think of them as moderate and incremental improvements, in a
 direction that Python is already moving in.

I've seen no evidence that any Python project is moving even remotely
toward data encapsulation.  That would be a drastic change.  Even if
it were only a minor change in the implementation (and it would not
be), it would be a major stroke in the Python community.  It would
basically cause a wholescale power shift from the user to the
implementor.  As a user it'd be like the difference between living in
a free democracy and a fascist dictatorship.


  Radical would be
 something like a full-scale static type system.

  I really wonder why peoples that seems to dislike one of the central
  features of Python - it's dynamism - still use it (assuming of
  course they are free to choose another language).

 I certainly don't think dynamism is central to Python. In what I see
 as well-developed Python programming style, it's something that is
 only rarely used in any important way.

You're in the minority, then.


 I'd spend much less time
 debugging if I got compiler warnings whenever I used dynamism without
 a suitable annotation.  The 1% of the time where I really want to use
 dynamism I don't see any problem with putting in an appropriate
 decorator, superclass, or whatever.

Well, I guess you are the sacrifical lamb so that everyone else can
take advantage of the dynamicism.


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


Re: Python 3.0 urllib.parse.parse_qs results in TypeError

2009-01-13 Thread Andy Grove
I don't fully understand this but if I pass in str(qs) instead of
qs then the call works. However, qs is returned from file.read()
operation so shouldn't that be a string already?

In case it's not already obvious, I am new to Python :-) .. so I'm
probably missing something here.
--
http://mail.python.org/mailman/listinfo/python-list


Re: why cannot assign to function call

2009-01-13 Thread Mark Wooding
Steven D'Aprano ste...@remove.this.cybersource.com.au wrote:

 I found it interesting. 

Well, that's something, at any rate.

 I think this conversation is reaching it's natural end. Frustration
 levels are rising. 

I think you may be right.  That said...

 So I'm going to take a different tack in an attempt to reduce
 frustration levels: if I can't convince the other side they're wrong,
 can I at least get them to understand where I'm coming from a little
 better?

Maybe...

 As I see it, this conversation is floundering on two radically different 
 ideas about what it means to say a language uses pass-by-foo.

You might be right, but I'm unconvinced.

 On the one hand, some people (me and possibly rurpy) consider this is 
 pass-by-foo to be a statement about behaviour directly visible to the 
 programmer. We have a set of behavioral traits in mind, and if a language 
 exhibits those behaviours, then it is clearly and obviously pass-by-foo 
 no matter how that behaviour is implemented. I'll call these the 
 behaviorists.

Here's the problem.  I think I'm in that camp too!

I'm going to move away from the formal semantics stuff and try a
different tack.  Here's what I think is the defining property of
pass-by-value (distilled from the formal approach I described earlier,
but shorn of the symbolism):

  The callee's parameters are /new variables/, initialized /as if by
  assignment/ from the values of caller's argument expressions.

I'd just like to examine that for a bit.  Firstly, let's expand it from
the soundbite: basically what it says is that you should be able to
replace

  function mumble(a, b, c) { stuff in terms of a, b, and c }
  ...
  mumble(1 + 2, xyz, whatever)

with

  ...
  fresh_a = 1 + 2
  fresh_b = xyz
  fresh_c = whatever
  stuff in terms of fresh_a, fresh_b, and fresh_c

with no observable difference (here, fresh_a and so on are a variable
names not appearing in the rest of the program).  So:

  * It captures C's behaviour (at least if you don't count arrays --
let's not open that one again), and Pascal's normal behaviour.
Assigning to the parameters doesn't affect the caller's argument
variables because the parameters are fresh variables.

  * It /doesn't/ capture Fortran's behaviour, or Pascal's `var'
parameters, because obviously assignment to parameters in Fortran
/can/ affect the caller's argument variables

  * It also doesn't capture exotic things like Algol's call by name, and
lazy evaluation, because there's an evaluation step in there.

My soundbite definition for pass-by-reference is this:

  The callee's parameters are merely /new names/ for the caller's
  argument variables -- as far as that makes sense.

There's a caveat there for argument expressions which don't correspond
directly to variables -- and I've glossed over the issue of lvalue
expressions which designate locations and all of that.  The idea is that
you can replace

  function mumble(a, b) { stuff in terms of a and b }
  ...
  mumble(xyz, whatever)

by

  ...
  stuff in terms of xyz and whatever

This does indeed capture Fortran, and Pascal's `var', while excluding C
and Pascal non-`var'.  Good!

So... obviously I'm going to claim that Python is pass-by-value.  Why?
Because its argument passing works the same way as its assignment.

But! (you claim) ...

 Python simply can't be pass-by-value, because it doesn't behave like
 pass-by-value in other languages (particularly C and Pascal).

Ah! (say I) but assignment in C and Pascal looks different from the way
it looks in C -- and in exactly the same way that argument passing looks
different.  And there, I think, I'm going to rest my case.

I'm sorry I took so long to distill these thoughts.  Thank you for
putting up with my theoretical meanderings on the way.

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


cgi.FieldStorage hanging with Python 3.0 (but works with 2.5.1)

2009-01-13 Thread Andy Grove
I'm trying to get a Python web server running that I can upload files
to. I actually have the code running with the version of Python pre-
installed on Mac OS X but it doesn't work with ActivePython 3.0 - I
have not been able to compile Python from source myself to see if the
issue is specific to the ActivePython distribution.

Here is the relevant code:

class MyHandler(http.server.BaseHTTPRequestHandler):
  def do_POST(self):
try:
print( Calling cgi.FieldStorage() )
form = cgi.FieldStorage(
fp=self.rfile,
headers=self.headers,
environ={'REQUEST_METHOD':'POST',
 'CONTENT_TYPE':self.headers['Content-
Type'],
 })
print( Calling cgi.FieldStorage() )

The client is the following HTML form being submitted with Firefox
3.0.5 running on the same machine. The browser also hangs, waiting for
a response from the server.

form action=http://localhost:8090/deploy; method=POST
enctype=multipart/form-data
table
trtdFile:/tdtdinput type=file 
name=filename/td/tr
trtdnbsp;/tdtdinput type=submit value=Deploy/tr
/table
/form

As I said, this all works fine with Python 2.5.1 pre-installed.

Any suggestions?

Thanks,

Andy.

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


Re: Standard IPC for Python?

2009-01-13 Thread Mel
Philip Semanchuk wrote:

 I'm working on message queue support, but the Sys V IPC API is a
 headache and takes longer to code against than the POSIX API.

I hadn't found it that bad.  I have a C extension I should perhaps clean up
and make public.

Mel.

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


Re: Standard IPC for Python?

2009-01-13 Thread Terry Reedy

Laszlo Nagy wrote:




I was suggesting getting posix_ipc or sysv_ipc to compile against a 
compatibility library (Cygwin?) under Windows. It sounds like you're 
proposing something totally different, no?
OK I see. But probably I do not want to use Cygwin because that would 
create another dependency. I understand that posix_ipc/sysv is not 
natively supported under windows. What about this:




A few comments: First, the issue of cross-platform IPC, and its 
difficulties, has come up occasionally, but most with the knowledge of 
writing stuff for one system (esp. Windows) tend to stick with that system.



- create a wrapper, using ctypes, /windll / cdll/ to access API functions


ctypes was only added in 2.5.  It should make some things easier than 
they would have been before.


- use CreateFileMapping on the page file to create shared memory (a la 
windows: http://msdn.microsoft.com/en-us/library/aa366537.aspx)
- use CreateEvent/WaitForSingleObject for signaling 
(http://msdn.microsoft.com/en-us/library/ms682396(VS.85).aspx)
- these should be enough to implement shared memory functions and 
message queues under windows, and that might be a quick solution at 
least for me.
- it might also be used to emulate the same posix_ipc interface, without 
any external dependency added (cygwin).


That would be good.



All I care about is to create a working message queue. But if you think 
that this ctypes hack would be useful for other users, then I can try to 
implement it.


I must tell you that I'm not very familiar with C programming (it was a 
long time ago...) and I do not own MSVC.


Python compiles, I believe, with the free VCExpress.  But I would start 
with ctypes.


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


Re: Could you suggest optimisations ?

2009-01-13 Thread Terry Reedy

Barak, Ron wrote:

Hi,

In the attached script, the longest time is spent in the following 
functions (verified by psyco log):


I cannot help but wonder why and if you really need all the rigamorole 
with file pointers, offsets, and tells instead of


for line in open(...):
  do your processing.




def match_generator(self,regex):

Generate the next line of self.input_file that
matches regex.

generator_ = self.line_generator()
while True:
self.file_pointer = self.input_file.tell()
if self.file_pointer != 0:
self.file_pointer -= 1
if (self.file_pointer + 2) = self.last_line_offset:
break
line_ = generator_.next()
print %.2f%%   \r % (((self.last_line_offset - 
self.input_file.tell()) / (self.last_line_offset * 1.0)) * 100.0),

if not line_:
break
else:
match_ = regex.match(line_)
groups_ = re.findall(regex,line_)
if match_:
yield line_.strip(\n), groups_
 
def get_matching_records_by_regex_extremes(self,regex_array):


Function will:
Find the record matching the first item of regex_array.
Will save all records until the last item of regex_array.
Will save the last line.
Will remember the position of the beginning of the next line in
self.input_file.

start_regex = regex_array[0]
end_regex = regex_array[len(regex_array) - 1]
 
all_recs = []

generator_ = self.match_generator
 
try:

match_start,groups_ = generator_(start_regex).next()
except StopIteration:
return(None)
 
if match_start != None:

all_recs.append([match_start,groups_])
 
line_ = self.line_generator().next()

while line_:
match_ = end_regex.match(line_)
groups_ = re.findall(end_regex,line_)
if match_ != None:
all_recs.append([line_,groups_])
return(all_recs)
else:
all_recs.append([line_,[]])
line_ = self.line_generator().next()
 
def line_generator(self):


Generate the next line of self.input_file, and update
self.file_pointer to the beginning of that line.

while self.input_file.tell() = self.last_line_offset:
self.file_pointer = self.input_file.tell()
line_ = self.input_file.readline()
if not line_:
break
yield line_.strip(\n)

I was trying to think of optimisations, so I could cut down on 
processing time, but got no inspiration.

(I need the print %.2f%%   \r ... line for user's feedback).

Could you suggest any optimisations ?
Thanks,
Ron.
 
 
P.S.: Examples of processing times are:


* 2m42.782s  on two files with combined size of792544 bytes
  (no matches found).
* 28m39.497s on two files with combined size of 4139320 bytes
  (783 matches found). 


These times are quite unacceptable, as a normal input to the program
would be ten files with combined size of ~17MB.




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


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


Re: [OT] Re: are there some special about '\x1a' symbol

2009-01-13 Thread Terry Reedy

Gabriel Genellina wrote:
En Mon, 12 Jan 2009 12:00:16 -0200, John Machin sjmac...@lexicon.net 
escribió:



I didn't think your question was stupid. Stupid was (a) CP/M recording
file size as number of 128-byte sectors, forcing the use of an in-band
EOF marker for text files (b) MS continuing to regard Ctrl-Z as an EOF
decades after people stopped writing Ctrl-Z at the end of text files.


This is called backwards compatibility and it's a good thing :)


But it does not have to be the default or only behavior to be available.

Consider the Atucha II nuclear plant, started in 1980, based on a design 
from 1965, and still unfinished. People require access to the complete 
design, plans, specifications, CAD drawings... decades after they were 
initially written.
I actually do use (and maintain! -- ugh!) some DOS programs. Some people 
would have a hard time if they could not read their old data with new 
programs.
Even Python has a print statement decades after nobody uses a teletype 
terminal anymore...




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


Re: Python 3.0 urllib.parse.parse_qs results in TypeError

2009-01-13 Thread John Machin
On Jan 14, 9:56 am, Andy Grove andygrov...@gmail.com wrote:
 On Jan 13, 3:08 pm, John Machin sjmac...@lexicon.net wrote:

  Please show the full traceback.

 John,

 Thanks. Here it is:

   File /Library/Frameworks/Python.framework/Versions/3.0/lib/
 python3.0/socketserver.py, line 281, in _handle_request_noblock
     self.process_request(request, client_address)
   File /Library/Frameworks/Python.framework/Versions/3.0/lib/
 python3.0/socketserver.py, line 307, in process_request
     self.finish_request(request, client_address)
   File /Library/Frameworks/Python.framework/Versions/3.0/lib/
 python3.0/socketserver.py, line 320, in finish_request
     self.RequestHandlerClass(request, client_address, self)
   File /Library/Frameworks/Python.framework/Versions/3.0/lib/
 python3.0/socketserver.py, line 614, in __init__
     self.handle()
   File /Library/Frameworks/Python.framework/Versions/3.0/lib/
 python3.0/http/server.py, line 363, in handle
     self.handle_one_request()
   File /Library/Frameworks/Python.framework/Versions/3.0/lib/
 python3.0/http/server.py, line 357, in handle_one_request
     method()
   File /Users/andy/Development/EclipseWorkspace/dbsManage/kernel.py,
 line 178, in do_POST
     form = urllib.parse.parse_qs(qs, keep_blank_values=1)
   File /Library/Frameworks/Python.framework/Versions/3.0/lib/
 python3.0/urllib/parse.py, line 351, in parse_qs
 
     for name, value in parse_qsl(qs, keep_blank_values,
 strict_parsing):
   File /Library/Frameworks/Python.framework/Versions/3.0/lib/
 python3.0/urllib/parse.py, line 377, in parse_qsl
     pairs = [s2 for s1 in qs.split('') for s2 in s1.split(';')]
 TypeError: Type str doesn't support the buffer API


| Python 3.0 (r30:67507, Dec  3 2008, 20:14:27) [MSC v.1500 32 bit
(Intel)] on win32
| Type help, copyright, credits or license for more
information.
|  qs_bytes = b'a;bc;d'
|  qs_str = 'a;bc;d'
|  pairs = [s2 for s1 in qs_bytes.split('') for s2 in s1.split
(';')]
| Traceback (most recent call last):
|   File stdin, line 1, in module
| TypeError: Type str doesn't support the buffer API
|  pairs = [s2 for s1 in qs_str.split('') for s2 in s1.split(';')]
|  pairs
| ['a', 'b', 'c', 'd']
|  b'xy'.split('')
| Traceback (most recent call last):
|   File stdin, line 1, in module
| TypeError: Type str doesn't support the buffer API
|  b'xy'.split(b'')
| [b'x', b'y']
|  'xy'.split('')
| ['x', 'y']
| 

The immediate cause is that as expected mixing str and bytes raises an
exception -- this one however qualifies as not very informative and
possibly wrong [not having inspected the code for whatever.split() I'm
left wondering what is the relevance of the buffer API].

The docs for urllib.parse.parse_qs() and .parse_qsl() are a bit vague:
query string given as a string argument (data of type application/x-
www-form-urlencoded) ... does string mean str only or str or
bytes?

Until someone can give an authoritative answer [*], you might like to
try decoding your data (presuming you know what it is or how to dig it
out like you found the type and length) and feeding the result to
the .parse_qs().

[*] I know next to zilch about cgi and urllib -- I'm just trying to
give you some clues to see if you can get yourself back on the road.

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


Re: Extracting real-domain-name (without sub-domains) from a given URL

2009-01-13 Thread Terry Reedy

S.Selvam Siva wrote:


I doubt anyone's created a general ready-made solution for this, you'd
have to code it yourself.
To handle the common case, you can cheat and just .split() at the
periods and then slice and rejoin the list of domain parts, ex:
'.'.join(domain.split('.')[-2:])

Cheers,
Chris



Thank you Chris Rebert,
  Actually i tried with domain specific logic.Having 200 TLD like
.com,co.in,co.uk and tried to extract the domain name.
  But my boss want more reliable solution than this method,any way i
will try to find some alternative solution.


I make a dict mapping TLDs to number of parts to strip off
parts = {
'com':1,
'in':2,
'org':1,
'uk':2,
}
etc

If certain TLDs need a special function, define the function first and 
map that TLD to the function and then switch on the type of value (int 
or function) when you look it up.


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


Re: python3.0 MySQLdb

2009-01-13 Thread Daniel Fetchinson
 I need something to connect to a database, preferably mysql, that
 works in python3.0 please.
 And your question is?


 Surely it's fairly obvious that the question is does such a thing
 exist, and if so where can I find it?.

 Interestingly enough, the question was slightly (but importantly)
 different, though: the question really was Does anybody has a patch for
 MySQLdb?; as my reference to the existing interface to PostgreSQL
 was not sufficient for the OP.

Exactly. One could think about 3-4 different potentially useful
answers to the OP but when one sees 3-4 immediately right after
reading the post then probably there are a couple more still after
some thinking. So if the OP specifies exactly what he/she wants,
he/she will get more signal than noise.

Cheers,
Daniel


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


Re: Standard IPC for Python?

2009-01-13 Thread James Mills
On Wed, Jan 14, 2009 at 2:25 AM, Laszlo Nagy gand...@shopzeus.com wrote:
 The question is: what is the standard way to implement fast and portable IPC
 with Python? Are there tools in the standard lib that can do this?

Certainly not standard by any means, but I use
circuits (1). Two or more processes can communicate
via  Bridge by propagating events.

Example: http://trac.softcircuit.com.au/circuits/browser/examples/remotepy.py

cheers
James

1. http://trac.softcircuit.com.au/circuits/
--
http://mail.python.org/mailman/listinfo/python-list


Re: Standard IPC for Python?

2009-01-13 Thread James Mills
On Wed, Jan 14, 2009 at 3:40 AM, Laszlo Nagy gand...@shopzeus.com wrote:
 Can anyone tell me if select.select works under OS X?

Yes it does.

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


Re: Does Python really follow its philosophy of Readability counts?

2009-01-13 Thread Russ P.
On Jan 13, 3:07 pm, Carl Banks pavlovevide...@gmail.com wrote:

 I've seen no evidence that any Python project is moving even remotely
 toward data encapsulation.  That would be a drastic change.  Even if
 it were only a minor change in the implementation (and it would not
 be), it would be a major stroke in the Python community.  It would
 basically cause a wholescale power shift from the user to the
 implementor.  As a user it'd be like the difference between living in
 a free democracy and a fascist dictatorship.

I just googled object oriented principles. The first site that came
up lists four princicples:

- Encapsulation
- Abstraction
- Inheritance
- Polymorphism

The Wikipedia entry for object-oriented programming also lists
encapsulation as a fundamental concept.

The first line on the python.org site says:

Python is a dynamic object-oriented programming language that can be
used for many kinds of software development.

How can that possibly be true if you see no evidence that any Python
project is moving even remotely toward data encapsulation?

Semantics aside, I fail to understand your hostility toward a
fundamental concept of object-oriented programming. The difference
between a free democracy and a fascist dictatorship? Give me a
break!
--
http://mail.python.org/mailman/listinfo/python-list


executing multiple functions in background simultaneously

2009-01-13 Thread Catherine Moroney

Hello everybody,

I know how to spawn a sub-process and then wait until it
completes.  I'm wondering if I can do the same thing with
a Python function.

I would like to spawn off multiple instances of a function
and run them simultaneously and then wait until they all complete.
Currently I'm doing this by calling them as sub-processes
executable from the command-line.  Is there a way of accomplishing
the same thing without having to make command-line executables
of the function call?

I'm primarily concerned about code readability and ease of
programming.  The code would look a lot prettier and be shorter
to boot if I could spawn off function calls rather than
subprocesses.

Thanks for any advice,

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


  1   2   3   >