Re: Help needed installing easy_install and lxml2

2012-08-16 Thread Stefan Behnel
Ian, 15.08.2012 21:39:
> On a reasonably fresh (3 day old) install of 64 bit windows 7, I have
> installed the 64 bit Python 2.7.3 from http://www.python.org/download/
> 
> Then I installed  the 64 bit version of easy_install using
> http://pypi.python.org/pypi/setuptools
> 
> And then install lxml 2.3.5, for which I use the instructions at
> http://lxml.de/installation.html#installation
> 
> This ends with:
> Processing lxml-2.3.5.tgz
> Running lxml-2.3.5\setup.py -q bdist_egg --dist-dir
> c:\users\ian\appdata\local\temp\easy_install-9__rq7\lxml-2.3.5\egg-dist-tmp-uj_v_2
> 
> Building lxml version 2.3.5.
> Building without Cython.
> ERROR: 'xslt-config' is not recognized as an internal or external command,
> operable program or batch file.
> 
> ** make sure the development packages of libxml2 and libxslt are installed **
> 
> Using build configuration of libxslt
> error: Setup script exited with error: Unable to find vcvarsall.bat
> 
> C:\Users\ian>
> 
> Now what? I thought I was installing the binary package do I would not need
> any development versions.
> 
> What do these errors mean, and how can I overcome them?

http://lxml.de/FAQ.html#where-are-the-binary-builds

Stefan


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


Re: how to call perl script from html using python

2012-08-16 Thread Pervez Mulla
On Tuesday, August 14, 2012 10:42:48 AM UTC+5:30, Pervez Mulla wrote:
> Hi,
> 
> 
> 
> I wanna call perl script in HTML form n store that data in DB using Python.
> 
> 
> 
> How can i do this...??
> 
> 
> 
> Please help me
> 
> 
> 
> Thank you
> 
> Pervez



On Tuesday, August 14, 2012 10:42:48 AM UTC+5:30, Pervez Mulla wrote:
> Hi,
> 
> 
> 
> I wanna call perl script in HTML form n store that data in DB using Python.
> 
> 
> 
> How can i do this...??
> 
> 
> 
> Please help me
> 
> 
> 
> Thank you
> 
> Pervez



On Tuesday, August 14, 2012 10:42:48 AM UTC+5:30, Pervez Mulla wrote:
> Hi,
> 
> 
> 
> I wanna call perl script in HTML form n store that data in DB using Python.
> 
> 
> 
> How can i do this...??
> 
> 
> 
> Please help me
> 
> 
> 
> Thank you
> 
> Pervez



On Tuesday, August 14, 2012 10:42:48 AM UTC+5:30, Pervez Mulla wrote:
> Hi,
> 
> 
> 
> I wanna call perl script in HTML form n store that data in DB using Python.
> 
> 
> 
> How can i do this...??
> 
> 
> 
> Please help me
> 
> 
> 
> Thank you
> 
> Pervez

Hey Steven ,

Thank you for your response,

I will in detail now about my project,

Actually the project entire backend in PERL language , Am using Django 
framework for my front end .

I have written code for signup page in python , which is working perfectly .

In HTml when user submit POST method, it calling Python code Instead of 
this I wanna call perl script for sign up ..
 
below in form for sign up page in python 

form.py
from django import forms
from django.contrib.auth.models import User
from django.forms import ModelForm
from user_profile.models import Profile

class RegistrationForm(ModelForm):
username= forms.CharField(label=(u'User Name'))
email   = forms.EmailField(label=(u'E-mail'))
password= 
forms.CharField(label=(u'Password'),widget=forms.PasswordInput(render_value=False))
password1   = forms.CharField(label=(u'Verify 
Password'),widget=forms.PasswordInput(render_value=False))

class Meta:
model = Profile
exclude = ('user',)

def clean_username(self):
username = self.cleaned_data['username']
try:
User.objects.get(username==username)
except User.DoesNotExist:
return username
raise forms.ValidationError("That username is already taken. Please 
select another.")

def clean_password(self):
if self.cleaned_data['password'] != self.cleaned_data['password1']:
raise forms.ValidationError("Password didnt match... Please try 
again")
return self.cleaned_data
-

view.py


def ProfileRegistration(request):
if request.user.is_authenticated():
return HttpResponseRedirect('/profile/')
if request.method =="POST":
form = RegistrationForm(request.POST)
if form.is_valid():
user = User.objects.create_user(username = 
form.cleaned_data['username'],
email = form.cleaned_data['email'],
password = 
form.cleaned_data['password'])
user.save()
profile = Profile(user=user, 
firstname=form.cleaned_data['firstname'],
 lastname=form.cleaned_data['lastname'],
 phone=form.cleaned_data['phone'],
 title=form.cleaned_data['title'],
 
companyname=form.cleaned_data['companyname'])
profile.save()
return HttpResponseRedirect('/profile/')
else:
return render_to_response 
('register.html',{'form':form},context_instance=RequestContext(request))
else: 
form = RegistrationForm()
context = {'form':form}
return render_to_response('register.html', context, 
context_instance=RequestContext(request))


In view instead invoking python script I wanna invoke Perl script .for the 
signup page 


Thank You


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


Re: Help needed installing easy_install and lxml2

2012-08-16 Thread Ian

On 16/08/2012 08:01, Stefan Behnel wrote:
http://lxml.de/FAQ.html#where-are-the-binary-builds Stefan 


Hi Stefan,

Thanks Stefan,  everything is working fine now.

Regards

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


Re: Sharing code between different projects?

2012-08-16 Thread Jean-Michel Pichavant

andrea crotti wrote:

2012/8/14 Cameron Simpson :
  

Having just skimmed this thread, one thing I haven't quite seen suggested is
this:

Really do make a third "utilities" project, and treat "the project" and
"deploy" as separate notions. So to actually run/deploy project A's code
you'd have a short script that copied project A and the utilities project
code into a tree and ran off that. Or even a simple process/script to
update the copy of "utilities" in "project A"'s area.

So you don't "share" code on an even handed basis but import the
"utilities" library into each project as needed.

I do this (one my own very small scale) in one of two ways:

  - as needed, copy the desired revision of utilities into the project's
library space and do perforce's equivalent of Mercurial's addremove
on that library tree (comment "update utilities to revision X").

  - keep a perforce work area for the utilities in your project A area,
where your working project A can hook into it with a symlink or some
deploy/copy procedure as suggested above.
With this latter one you can push back into the utilities library
from your "live" project, because you have a real checkout. So:

  projectAdir
projectA-perforce-checkout
utilities-perforce-checkout
  projectBdir
projectB-perforce-checkout
utilities-perforce-checkout




Thanks, is more or less what I was going to do..  But I would not use
symlinks and similar things, because then every user should set it up
accordingly.

Potentially we could instead use the perforce API to change the
workspace mappings at run-time, and thus "force" perforce to checkout
the files in the right place..

There is still the problem that people should checkout things from two
places all the time instead of one..

  
SVN allows to define external dependencies, where one repository will 
actually checkout another one at a specific version. If SVN does it, I 
guess any decent SCM also provide such feature.


Assuming our project is named 'common', and you have 2 projects A and B :

A
   - common@rev1

B
   - common@rev2

Project A references the lib as "A.common", B as "B.common". You need to 
be extra carefull to never reference common as 'common' in any place.


JM

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


Re: OT: Monty Python in Syria

2012-08-16 Thread PythonAB

On 16 aug 2012, at 02:56, Andrew Cooper wrote:

> On 16/08/2012 01:52, Andrew Cooper wrote:
>> On 16/08/2012 01:01, Terry Reedy wrote:
>>> On 8/15/2012 6:07 PM, Mark Lawrence wrote:
 On 15/08/2012 20:15, Tamer Higazi wrote:
> Exactly!
> NOT PROGRAMMING related has NOTHING TODO HERE!
> 
 
 Please don't shout, please don't top post
>>> 
>>> agreed.
>>> 
 and what gives you the right
 to determine what is or is not on topic here?
>>> 
>>> The same right as anyone.
>>> 
 The subject is also
 clearly marked OT or did that escape your attention?
>>> 
>>> But it has nothing to do with Monty Python either, that I can see.
>> 
>> Then I humbly suggest you re-watch The Holy Grail.
>> 
>> ~Andrew
>> 
>>> Nor is there a video to see the context of (OP said "For context, start
>>> the video at 1:00.") Perhaps link is erroneous.
> 
> P.S. - There is certainly a video.  It just takes an obnoxiously long
> time to load.
> 
>>> 
>>> Marking something OT does not excuse it. It should still be related. We
>>> cannot allow 1000s of OT posts a day, marked OT or not.
>>> 
>> 
> 

I agree, adding "OT" shouldn't be an excuse to just post anything people want,
should it?

The URL has absolutely nothing to do with python whatsoever. At least not here
as I can view it in The Netherlands.
It's a news item about the 'rebels' in Syria who supposedly shot down this 
fighterplane.
How someone can link that to Monty Python is worth a oscar for storytelling i 
think...

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


Re: [ANNC] pybotwar-0.8

2012-08-16 Thread Jean-Michel Pichavant

Lee Harr wrote:

pybotwar is a fun and educational game where players
write computer programs to control simulated robots.

http://pybotwar.googlecode.com/


The focus of this release is making all functionality
available from the PyQt interface and making PyQt
the default interface.

pybotwar uses pybox2d for the physical simulation.
It can be run in text-only mode -- useful for longer
tournaments -- or use pyqt or pygame for a graphical
interface and visualization of the action.

pybotwar is released under GPLv3.


Changes in pybotwar-0.8:
- pybox2d-2.0.2b2 is now the required version

API
- Robot.turret() now takes turret speed instead of angle
- PING sensor now differentiates own bullets from enemy bullets
- PING differentiates dead robots when remove_dead_robots=False
- made internal Robot state variables less likely to conflict with user code

Settings
- added "robots dir" setting to make it easier to run pybotwar
from an installed copy, rather than from the unpacked folder
- logs, lineups, and db all go in robots dir
- robot modules are now loaded by full path
- If using Qt settings, remembers most recently used set of robots

PyQt4 Interface
- PyQt4 view mode is now the default
- (text mode and pygame/pygsear still available)
- all settings are available from PyQt interface
- remembers most recently used set of robots
- added debug window showing sensor values, commands, and logs
- can now start tournaments (and multiple battles) from pyqt
- can also start tournaments to run in background
- editor automatically adds .py to robot filename if needed

Other changes
- made template.py a valid robot program
- single battles are now run as 1-battle tournaments
- renamed kill stat to outlasted
- started tracking kills as dealing final damage to other robot
- commandline can now take a list of robots to load
- added "Super Tournaments"
- runs tournaments with all possible combinations of robots

Fixes:
- made POSition sensor scale same as PING sensor
- fixed editor backspace between left edge and start of text
- fixed inability to re-open file once its window was closed
- fixed crash when cancelling file-open dialog
- limited motor speed on turret
- log messages in testmode when using qt4 view
- fall back to :memory: database if unable to open db file

 		 	   		  
  

I have some suggestions:

- rename pybotwar into pybotcontest
- replace "bullet" by "missile of passion"
- replace "dead" by "overflood with love"
- "PING sensor" into "pheromone sensor"
... and so on :p

JM

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


Installing Python 3.2.3 on Win 7

2012-08-16 Thread Johan van Zyl
Hi

I installed Python 3.2.3 successfully on my work laptop (XP) but
cannot seem to do it on my home PC (Win7)
I click the button to install and the window just disappears o the screen.
So how do I in fact install Python 3.2.3 on Win 7?

-- 
Johan van Zyl
PMB - Box 21673, Mayors Walk, 3208
Pretoria - Box 2667, Brooklyn Square, 0075
FAX: 086 622 9554
The answer my friend is blowin in the wind...
If you forward messages, Please use the BCC area, and Please REMOVE
all email addresses and all the useless info at the bottom of the
message, before you send it on. ONLY send out CLEAN and TIDY emails.
Thank you.
-- 
http://mail.python.org/mailman/listinfo/python-list


RE: Installing Python 3.2.3 on Win 7

2012-08-16 Thread Tommi Helander
Hi Johan,

-Are you trying to install 32 or 64-bit Python?
-Is your Win7 32 or 64-bits?
-Have you tried running the Python installer from the command line to see if it 
generates any helpful output?

-Tommi Helander

> Date: Thu, 16 Aug 2012 09:17:43 +0200
> Subject: Installing Python 3.2.3 on Win 7
> From: jo...@jvz.co.za
> To: python-list@python.org
> 
> Hi
> 
> I installed Python 3.2.3 successfully on my work laptop (XP) but
> cannot seem to do it on my home PC (Win7)
> I click the button to install and the window just disappears o the screen.
> So how do I in fact install Python 3.2.3 on Win 7?
> 
> -- 
> Johan van Zyl
> PMB - Box 21673, Mayors Walk, 3208
> Pretoria - Box 2667, Brooklyn Square, 0075
> FAX: 086 622 9554
> The answer my friend is blowin in the wind...
> If you forward messages, Please use the BCC area, and Please REMOVE
> all email addresses and all the useless info at the bottom of the
> message, before you send it on. ONLY send out CLEAN and TIDY emails.
> Thank you.
> -- 
> http://mail.python.org/mailman/listinfo/python-list
  -- 
http://mail.python.org/mailman/listinfo/python-list


Re: dbf.py API question concerning Index.index_search()

2012-08-16 Thread Hans Mulder
On 16/08/12 01:26:09, Ethan Furman wrote:
> Indexes have a new method (rebirth of an old one, really):
> 
>   .index_search(
>  match,
>  start=None,
>  stop=None,
>  nearest=False,
>  partial=False )
> 
> The defaults are to search the entire index for exact matches and raise
> NotFoundError if it can't find anything.
> 
> match is the search criteria
> start and stop is the range to search in
> nearest returns where the match should be instead of raising an error
> partial will find partial matches
> 
> The question is what should the return value be?
> 
> I don't like the usual pattern of -1 meaning not found (as in
> 'nothere'.find('a')), so I thought a fun and interesting way would be to
> subclass long and override the __nonzero__ method to return True/False
> based on whether the (partial) match was found.  The main problems I see
> here is that the special return value reverts to a normal int/long if
> anything is done to it (adding, subtracting, etc), and the found status
> is lost.
> 
> The other option is returning a (number, bool) tuple -- safer, yet more
> boring... ;)

I think you should go for the safe boring option, because in many use
cases the caller will need to known whether the number you're returning
is the index of a match or just the nearest non-match.  The caller could
redo the match to find out.  But you have already done the match, so you
might as well tell them the result.


Hope this helps,

-- HansM

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


Re: Installing Python 3.2.3 on Win 7

2012-08-16 Thread Johan van Zyl
Python 32 bit on Win 7 32 bit.
Thx!

On 16 August 2012 12:32, Tommi Helander  wrote:
> Hi Johan,
>
> -Are you trying to install 32 or 64-bit Python?
> -Is your Win7 32 or 64-bits?
> -Have you tried running the Python installer from the command line to see if
> it generates any helpful output?
>
> -Tommi Helander
>
>> Date: Thu, 16 Aug 2012 09:17:43 +0200
>> Subject: Installing Python 3.2.3 on Win 7
>> From: jo...@jvz.co.za
>> To: python-list@python.org
>
>>
>> Hi
>>
>> I installed Python 3.2.3 successfully on my work laptop (XP) but
>> cannot seem to do it on my home PC (Win7)
>> I click the button to install and the window just disappears o the screen.
>> So how do I in fact install Python 3.2.3 on Win 7?
>>
>> --
>> Johan van Zyl
>> PMB - Box 21673, Mayors Walk, 3208
>> Pretoria - Box 2667, Brooklyn Square, 0075
>> FAX: 086 622 9554
>> The answer my friend is blowin in the wind...
>> If you forward messages, Please use the BCC area, and Please REMOVE
>> all email addresses and all the useless info at the bottom of the
>> message, before you send it on. ONLY send out CLEAN and TIDY emails.
>> Thank you.
>> --
>> http://mail.python.org/mailman/listinfo/python-list



-- 
Johan van Zyl
PMB - Box 21673, Mayors Walk, 3208
Pretoria - Box 2667, Brooklyn Square, 0075
FAX: 086 622 9554
The answer my friend is blowin in the wind...
If you forward messages, Please use the BCC area, and Please REMOVE
all email addresses and all the useless info at the bottom of the
message, before you send it on. ONLY send out CLEAN and TIDY emails.
Thank you.
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Sharing code between different projects?

2012-08-16 Thread andrea crotti
2012/8/16 Jean-Michel Pichavant :
>
> SVN allows to define external dependencies, where one repository will
> actually checkout another one at a specific version. If SVN does it, I guess
> any decent SCM also provide such feature.
>
> Assuming our project is named 'common', and you have 2 projects A and B :
>
> A
>- common@rev1
>
> B
>- common@rev2
>
> Project A references the lib as "A.common", B as "B.common". You need to be
> extra carefull to never reference common as 'common' in any place.
>
> JM
>


Unfortunately I think you guess wrong
http://forums.perforce.com/index.php?/topic/553-perforce-svnexternals-equivalent/
Anyway with views and similar things is not that hard to implement the
same thing..
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: how to call perl script from html using python

2012-08-16 Thread andrea crotti
2012/8/16 Pervez Mulla :
>
> Hey Steven ,
>
> Thank you for your response,
>
> I will in detail now about my project,
>
> Actually the project entire backend in PERL language , Am using Django 
> framework for my front end .

> I have written code for signup page in python , which is working perfectly .
>
> In HTml when user submit POST method, it calling Python code Instead of 
> this I wanna call perl script for sign up ..
>
> below in form for sign up page in python 

Good that's finally an explanation, so the question you can ask google
was "how do I call an external process from Python",
which has absolutely nothing to with HTML, and is very easy to find
out (hint: subprocess).
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Installing Python 3.2.3 on Win 7

2012-08-16 Thread Ulrich Eckhardt

Am 16.08.2012 09:17, schrieb Johan van Zyl:

I installed Python 3.2.3 successfully on my work laptop (XP) but
cannot seem to do it on my home PC (Win7)
I click the button to install and the window just disappears o the screen.
So how do I in fact install Python 3.2.3 on Win 7?


I used some MSI files, IIRC. What exactly did you download and what 
"button to install" are you talking about? Lastly, just in case, are you 
running a system with 32 or 64 bits?


Uli

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


Re: how to call perl script from html using python

2012-08-16 Thread Rob Day
On 16 August 2012 08:23, Pervez Mulla  wrote:

>
>
> In HTml when user submit POST method, it calling Python code Instead
> of this I wanna call perl script for sign up ..
>
>
Can you not just change the action= attribute in your  HTML attribute
to point to your Perl CGI script?

-- 
Robert K. Day
robert@merton.oxon.org
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Strange behavior

2012-08-16 Thread Virgil Stokes

On 15-Aug-2012 02:19, Steven D'Aprano wrote:

On Tue, 14 Aug 2012 21:40:10 +0200, Virgil Stokes wrote:


You might find the following useful:

def testFunc(startingList):
  xOnlyList = []; j = -1
  for xl in startingList:
  if (xl[0] == 'x'):

That's going to fail in the starting list contains an empty string. Use
xl.startswith('x') instead.
Yes, but this was by design (tacitly assumed that startingList was both a list 
and non-empty).




  xOnlyList.append(xl)
  else:
  j += 1
  startingList[j] = xl

Very cunning, but I have to say that your algorithm fails the "is this
obviously correct without needing to study it?" test. Sometimes that is
unavoidable, but for something like this, there are simpler ways to solve
the same problem.

Sorry, but I do not sure what you mean here.




  if j == -1:
  startingList = []
  else:
  del startingList[j:-1]
  return(xOnlyList)



And here is another version using list comprehension that I prefer
def testFunc2(startingList):
  return([x for x in startingList if x[0] == 'x'], [x for x in
startingList if x[0] != 'x'])

This walks over the starting list twice, doing essentially the same thing
both times. It also fails to meet the stated requirement that
startingList is modified in place, by returning a new list instead.
This can meet the requirement that startingList is modified in place via the 
call to this function (see the attached code).

Here's an example of what I mean:

py> mylist = mylist2 = ['a', 'x', 'b', 'xx', 'cx']  # two names for one
list
py> result, mylist = testFunc2(mylist)
py> mylist
['a', 'b', 'cx']
py> mylist2  # should be same as mylist
['a', 'x', 'b', 'xx', 'cx']

Yes, I had a typo in my original posting --- sorry about that!


Here is the obvious algorithm for extracting and removing words starting
with 'x'. It walks the starting list only once, and modifies it in place.
The only trick needed is list slice assignment at the end.

def extract_x_words(words):
 words_with_x = []
 words_without_x = []
 for word in words:
 if word.startswith('x'):
 words_with_x.append(word)
 else:
 words_without_x.append(word)
 words[:] = words_without_x  # slice assignment
 return words_with_x

Suppose words was not a list --- you have tacitly assumed that words is a list.


The only downside of this is that if the list of words is so enormous
that you can fit it in memory *once* but not *twice*, this may fail. But
the same applies to the list comprehension solution.
But, this is not the only downside if speed is important --- it is slower than 
the list comprehension method (see results that follows).


Here is a summary of three algorithms (algorithm-1, algorithm-2, algorithm-2A) 
that I tested (see attached code). Note, algorithm-2A was obtained by removing 
the slice assignment in the above code and modifying the return as follows


def extract_x_words(words):
words_with_x = []
words_without_x = []
for word in words:
if word.startswith('x'):
words_with_x.append(word)
else:
words_without_x.append(word)
#words[:] = words_without_x  # slice assignment
return words_with_x, words_without_x

Of course, one needs to modify the call for "in-place" update of startingList as 
follows:


   xOnlyList,startingList = extract_x_words(startingList)

Here is a summary of my timing results obtained for 3 different algorithms for 
lists with 100,000 strings of length 4 in each list:


Method
average (sd) time in seconds
algorithm-1 (list comprehension)
0.11630 (0.0014)
algorithm-2 (S. D'Aprano)
0.17594 (0.0014)
algorithm-2A (modified S. D'Aprano)
0.18217 (0.0023)


These values  were obtained from 100 independent runs (MC simulations) on lists 
that contain 100,000 strings. Approximately 50% of these strings contained a 
leading 'x'. Note, that the results show that algorithm-2 (suggested by S. 
D'Aprano) is approximately 51% slower than algorithm-1 (list comprehensions) and 
algorithm-2A (simple modification of algorithm-2) is approximately 57% slower 
than algorithm-1. Why is algorithm-2A slower than algorithm-2?


I would be interested in seeing code that is faster than algorithm-1 --- any 
suggestions are welcomed.  And of course, if there are any errors in my attached 
code please inform me of them and I will try to correct them as soon as 
possible. Note, some of the code is actually irrelevant for the original 
"Strange behavior" post.


Have a good day!

'''
  Purpose: Time three different algorithms for the same task
  
  Author: V. Stokes (v...@it.uu.se, 2012-08-16)
  Refs:
   python-list@python.org list
* Strange behavior, 14-Aug-2012 17:38, light1qu...@gmail.com
* Re: Strange behavior, 14-Aug-2012 21:40, Stokes, Virgil
* Re: Strange behavior, 15-Aug-2012 02:19, Steven D'Aprano
  
  Note:
   1. The mean and standard d

Re: SSLSocket.getpeercert() doesn't return issuer, serial number, etc

2012-08-16 Thread Antoine Pitrou

Hello,

Gustavo Baratto  gmail.com> writes:
> 
> SSL.Socket.getpeercert() doesn't return essential information present in the
> client certificate (issuer, serial number, not before, etc), and it looks it 
> is
> by design:

It does, in Python 3.2:
http://docs.python.org/py3k/library/ssl.html#client-side-operation

(although the getpeercert() doc should be updated to reflect this)

If some information is still lacking from the returned value, please open an 
issue at http://bugs.python.org

Regards

Antoine.


-- 
Software development and contracting: http://pro.pitrou.net


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


type(None)()

2012-08-16 Thread Hans Mulder
On 8/08/12 04:14:01, Steven D'Aprano wrote:
> NoneType raises an error if you try to create a second instance. bool
> just returns one of the two singletons (doubletons?) again.
>
> py> type(None)()
> Traceback (most recent call last):
>   File "", line 1, in 
> TypeError: cannot create 'NoneType' instances

Why is that?

Why doesn't it just return an existing instance of the type,
like bool, int, str and other built-in non-mutable types do?

> py> type(False)() is False
> True


Just wondering,

-- HansM

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


Re: Dynamically determine base classes on instantiation

2012-08-16 Thread Thomas Bach
On Thu, Aug 16, 2012 at 12:16:03AM +, Steven D'Aprano wrote:
> Some comments:
> 
> 1) What you show are not "use cases", but "examples". A use-case is a 
> description of an actual real-world problem that needs to be solved. A 
> couple of asserts is not a use-case.

Thanks for the clarification on that one. So, here's the use-case: I'm
querying the crunchbase API which returns JSON data and is rather
poorly documented. I want to create a data model for the companies
listed on Crunchbase in order to be able to put the queried data in a
data-base. As I am too lazy to examine all the data by hand I thought
I automatize this. I thought that it would be nice to be able to pass
a function a parsed JSON object (AFAIK these are lists, dicts,
strings, ints, floats, strs in Python) and it returns me the type of
these objects. For the simple classes (str, int, float) this is quite
trivial: F('foo') should return `str' and F(8) should return `int'.

For a compound object like dict I would like it to return the data
fields with their type. Hence, F({'foo': 8}) should return 
{'foo': int}, and given that f = F({'foo': {'bar': 80}}) I would like
f to equal to {'foo': dict}, with the option to query the type of
'foo' via f.foo, where the latter should equal to {'bar': int}. So
far, this is not a complicated case. But, sometimes a data field on
returned data set is simply None. Thus, I want to extract the types from
another data set and merge the two.

So, my question (as far as I can see it, please correct me if I am
wrong) is less of the "How do I achieve this?"-kind, but more of the
"What is a clean design for this?"-kind. My intuitive thought was that
the `merge' function should be a part of the object returned from `F'.

> How about you tell us the problem, and we'll suggest a solution?

I can see your point. On the other hand, by expressing my thoughts you
can at least tell me that these are completely wrong and correct my
way of thinking this way.

> Consider your two examples:
> 
> a = Foo(['a', 'list'])
> b = Foo({'blah': 8})
> 
> According to your design:
> 
> a is a Foo
> b is a Foo

I actually never said that. I simply wanted `a' and `b' to share the
same function (the `merge' function), I thought that the easiest way
to achieve this is by letting them share the same name-space. But, as
you show: …

> therefore a and b are the same type
> 
> So far so good: this is perfectly normal object-oriented design.
> 
> But you also have 
> 
> a is a list, but not a dict
> b is a dict, but not a listn
> therefore a and b are different types
> 
> So you contradict yourself: at the same time, a and b are both the same 
> and different types.

… I already made a mistake on the logical level.

> Instead, Foo should implement only the shared operations, and everything 
> else should be delegated to _obj.
> 
> If you inherit from builtins, you cannot use automatic delegation on the 
> magic "double-underscore" (dunder) methods like __eq__, __len__, etc.
> 
> See this thread here for one possible solution:
> 
> http://www.velocityreviews.com/forums/t732798-automatic-delegation-in-python-3-a.html
> 

OK, thanks for the hint. I will see how I'm going to put all this
stuff together.

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


Re: Strange behavior

2012-08-16 Thread Peter Otten
Virgil Stokes wrote:

>>> def testFunc(startingList):
>>>xOnlyList = []; j = -1
>>>for xl in startingList:
>>>if (xl[0] == 'x'):
>> That's going to fail in the starting list contains an empty string. Use
>> xl.startswith('x') instead.
> Yes, but this was by design (tacitly assumed that startingList was both a
> list and non-empty).

You missunderstood it will fail if the list contains an empty string, not if 
the list itself is empty: 

>>> words = ["alpha", "", "xgamma"]
>>> [word for word in words if word[0] == "x"]
Traceback (most recent call last):
  File "", line 1, in 
IndexError: string index out of range

The startswith() version:

>>> [word for word in words if word.startswith("x")]
['xgamma']

Also possible:

>>> [word for word in words if word[:1] == "x"]
['xgamma']

> def testFunc1(startingList): 
>  ''' 
>Algorithm-1 
>Note: 
>  One should check for an empty startingList before 
>  calling testFunc1 -- If this possibility exists! 
>  ''' 
>  return([x for x in startingList if x[0] == 'x'], 
> [x for x in startingList if x[0] != 'x']) 
>  
> 
> I would be interested in seeing code that is faster than algorithm-1

In pure Python? Perhaps the messy variant:

def test_func(words):
nox = []
append = nox.append
withx = [x for x in words if x[0] == 'x' or append(x)]
return withx, nox


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


Re: type(None)()

2012-08-16 Thread Laszlo Nagy

On 2012-08-16 14:47, Hans Mulder wrote:

On 8/08/12 04:14:01, Steven D'Aprano wrote:

NoneType raises an error if you try to create a second instance. bool
just returns one of the two singletons (doubletons?) again.

py> type(None)()
Traceback (most recent call last):
   File "", line 1, in 
TypeError: cannot create 'NoneType' instances

Why is that?
Because None is a singleton. It is the only instance of its class. This 
is very useful because it allows you to write conditions like this:


if obj is None:
do_something()


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


Re: type(None)()

2012-08-16 Thread Chris Angelico
On Thu, Aug 16, 2012 at 10:47 PM, Hans Mulder  wrote:
> Why doesn't it just return an existing instance of the type,
> like bool, int, str and other built-in non-mutable types do?
>
>> py> type(False)() is False
>> True

With int and str, it's only an optimization, and not guaranteed to happen.

>>> a=int("1234")
>>> a is int("1234")
False

>>> a=str(1234)
>>> a is str(1234)
False

But with bool, it's required, as a means of "casting to boolean". With
True/False/None, it's normal to compare them with is:

>>> a=bool("1")
>>> a is bool("2")
True

So bool() has to return one of those two actual objects, and not an equivalent.

(Note: All examples done in CPython 3.2's IDLE on Windows. Other
environments, Pythons, versions, etc, may affect exactly what these
show.)

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


Re: A difficulty with lists

2012-08-16 Thread Madison May
On Wednesday, August 15, 2012 8:21:22 PM UTC-4, Terry Reedy wrote:
> On 8/15/2012 5:58 PM, Rob Day wrote:
> 
> Yeah, my apologies for any confusion I created.  Although I suppose my 
> explanation would be somewhat true for immutable objects since they can't be 
> modified in-place (any modification at all would cause the creation of a new 
> immutable object right?), I now understand that it is completely and totally 
> wrong for mutable objects.  

Thanks for the in-depth explanations, Terry and Rob. I feel like I have a much 
more solid grasp of what's going on behind the scenes after your analysis. 
> 
>  > Madison May wrote:
> 
> > The list nlist inside of function xx is not the same as the variable
> 
> > u outside of the function:  nlist and u refer to two separate list
> 
> > objects.  When you modify nlist, you are not modifying u.
> 
> > 
> 
> 
> 
> This is confused and wrong. The parameter *name* 'nlist' of function xx 
> 
> is not the same as the *name* 'u' outside the function. The call xx(u) 
> 
> binds nlist to the same object that u is bound to. At that point, the 
> 
> two name *are* bound to the same list object. The statement 
> 
> "nlist+=[999]" dodifying nlist *does* modify u. The subsequent 
> 
> assignment statement "nlist=nlist[:-1]" rebinds 'nlist' to a *new* list 
> 
> object. That new object gets deleted when the function returns. So the 
> 
> rebinding is completely useless.
> 
> 
> 
> This sequence, modifying the input argument and then rebinding to a new 
> 
> object, is bad code.
> 
> 
> 
> > Well - that's not quite true. Before calling the function, u is [1, 2,
> 
> > 3, 4] - but after calling the function,  u is [1, 2, 3, 4, 999]. This is
> 
> > a result of using 'nlist += [999]' - the same thing doesn't happen if
> 
> > you use 'nlist = nlist+[999]' instead.
> 
> >
> 
> > I'm not completely aware of what's going on behind the scenes here, but
> 
> 
> 
> you got it right.
> 
> 
> 
> > I think the problem is that 'nlist' is actually a reference to a list
> 
> > object - it points to the same place as u.
> 
> 
> 
> Calling a python function binds parameter names to argument objects or 
> 
> (for *args and **kwds parameters) a collection based on argument objects.
> 
> 
> 
> > When you assign to it within
> 
> > the function, then it becomes separate from u - which is why nlist =
> 
> > nlist+[999] and nlist = nlist[:-1] don't modify u - but if you modify
> 
> > nlist in place before doing that, such as by using +=, then it's still
> 
> > pointing to u, and so u gets modified as well.
> 
> 
> 
> 
> 
> -- 
> 
> Terry Jan Reedy

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


Re: type(None)()

2012-08-16 Thread Ian Kelly
On Thu, Aug 16, 2012 at 6:47 AM, Hans Mulder  wrote:
> On 8/08/12 04:14:01, Steven D'Aprano wrote:
>> NoneType raises an error if you try to create a second instance. bool
>> just returns one of the two singletons (doubletons?) again.
>>
>> py> type(None)()
>> Traceback (most recent call last):
>>   File "", line 1, in 
>> TypeError: cannot create 'NoneType' instances
>
> Why is that?
>
> Why doesn't it just return an existing instance of the type,
> like bool, int, str and other built-in non-mutable types do?

Because unlike those other types there is no use case for that.  It's
simpler to raise an error.
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: type(None)()

2012-08-16 Thread Steven D'Aprano
On Thu, 16 Aug 2012 14:47:47 +0200, Hans Mulder wrote:

> On 8/08/12 04:14:01, Steven D'Aprano wrote:
>> NoneType raises an error if you try to create a second instance. bool
>> just returns one of the two singletons (doubletons?) again.
>>
>> py> type(None)()
>> Traceback (most recent call last):
>>   File "", line 1, in 
>> TypeError: cannot create 'NoneType' instances
> 
> Why is that?
> 
> Why doesn't it just return an existing instance of the type, like bool,
> int, str and other built-in non-mutable types do?

bool must return an instance, because it is designed to cast objects to a 
boolean. Since (by design) True and False are singletons (doubletons?), 
bool(x) will always return a pre-existing instance.

Other built-in immutable types do not promise to do that. For example:

py> a = float(42)
py> b = float(42)
py> a is b
False

Sometimes int and str will cache their instances, but this is an 
implementation detail subject to change without notice from version to 
version.

None, NotImplemented and Ellipsis are singletons, but unlikely bool, 
there is no common use-case for having their types return the singleton 
instance. The standard design pattern for singletons is to raise an 
exception if you try to create an instance, so they do. However, this 
behaviour really only makes sense for singletons that hold state. (If 
they hold state, you might be tempted to change that state, not realising 
that you are changing a singleton and not a second instance.)

In my opinion, this is a PITA for None and better behaviour would be to 
return the pre-existing NoneType instance, but I didn't design the 
language.


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


Re: Running Python web apps on shared ASO servers?

2012-08-16 Thread Gilles
On Sun, 12 Aug 2012 02:03:33 +0200, Gilles  wrote:
>Does it mean that ASO only supports writing Python web apps as
>long-running processes (CGI, FCGI, WSGI, SCGI) instead of embedded
>Python à la PHP?

I need to get the big picture about the different solutions to run a
Python web application.

>From what I read, it seems like this is the way things involved over
the years:

CGI : original method. Slow because the server has to spawn a new
process to run the interpreter + script every time a script is run.

mod_python : Apache module alternative to CGI. The interpreter is
loaded once, and running a script means just handling the script

mod_wsgi : mod_python is no longer developped, and mod_wsgi is its new
reincarnation

FastCGI and SCGI: Faster alternativees to CGI; Run as independent
programs, and communicate with the web server through either a Unix
socket (located on the same host) or a TCP socket (remote  host)

Is this correct?

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


Re: [ANNC] pybotwar-0.8

2012-08-16 Thread Ramchandra Apte
nah.. "war" sounds better than "contest"

On 16 August 2012 14:59, Jean-Michel Pichavant wrote:

> Lee Harr wrote:
>
>> pybotwar is a fun and educational game where players
>> write computer programs to control simulated robots.
>>
>> http://pybotwar.googlecode.**com/ 
>>
>>
>> The focus of this release is making all functionality
>> available from the PyQt interface and making PyQt
>> the default interface.
>>
>> pybotwar uses pybox2d for the physical simulation.
>> It can be run in text-only mode -- useful for longer
>> tournaments -- or use pyqt or pygame for a graphical
>> interface and visualization of the action.
>>
>> pybotwar is released under GPLv3.
>>
>>
>> Changes in pybotwar-0.8:
>> - pybox2d-2.0.2b2 is now the required version
>>
>> API
>> - Robot.turret() now takes turret speed instead of angle
>> - PING sensor now differentiates own bullets from enemy bullets
>> - PING differentiates dead robots when remove_dead_robots=False
>> - made internal Robot state variables less likely to conflict with
>> user code
>>
>> Settings
>> - added "robots dir" setting to make it easier to run pybotwar
>> from an installed copy, rather than from the unpacked folder
>> - logs, lineups, and db all go in robots dir
>> - robot modules are now loaded by full path
>> - If using Qt settings, remembers most recently used set of robots
>>
>> PyQt4 Interface
>> - PyQt4 view mode is now the default
>> - (text mode and pygame/pygsear still available)
>> - all settings are available from PyQt interface
>> - remembers most recently used set of robots
>> - added debug window showing sensor values, commands, and logs
>> - can now start tournaments (and multiple battles) from pyqt
>> - can also start tournaments to run in background
>> - editor automatically adds .py to robot filename if needed
>>
>> Other changes
>> - made template.py a valid robot program
>> - single battles are now run as 1-battle tournaments
>> - renamed kill stat to outlasted
>> - started tracking kills as dealing final damage to other robot
>> - commandline can now take a list of robots to load
>> - added "Super Tournaments"
>> - runs tournaments with all possible combinations of robots
>>
>> Fixes:
>> - made POSition sensor scale same as PING sensor
>> - fixed editor backspace between left edge and start of text
>> - fixed inability to re-open file once its window was closed
>> - fixed crash when cancelling file-open dialog
>> - limited motor speed on turret
>> - log messages in testmode when using qt4 view
>> - fall back to :memory: database if unable to open db file
>>
>>
>>
> I have some suggestions:
>
> - rename pybotwar into pybotcontest
> - replace "bullet" by "missile of passion"
> - replace "dead" by "overflood with love"
> - "PING sensor" into "pheromone sensor"
> ... and so on :p
>
> JM
>
> --
> http://mail.python.org/**mailman/listinfo/python-list
>
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Strange behavior

2012-08-16 Thread Virgil Stokes

On 16-Aug-2012 15:02, Peter Otten wrote:

Virgil Stokes wrote:


def testFunc(startingList):
xOnlyList = []; j = -1
for xl in startingList:
if (xl[0] == 'x'):

That's going to fail in the starting list contains an empty string. Use
xl.startswith('x') instead.

Yes, but this was by design (tacitly assumed that startingList was both a
list and non-empty).

You missunderstood it will fail if the list contains an empty string, not if
the list itself is empty:


words = ["alpha", "", "xgamma"]
[word for word in words if word[0] == "x"]

Traceback (most recent call last):
   File "", line 1, in 
IndexError: string index out of range

The startswith() version:


[word for word in words if word.startswith("x")]

['xgamma']

Also possible:


[word for word in words if word[:1] == "x"]

['xgamma']


def testFunc1(startingList):
  '''
Algorithm-1
Note:
  One should check for an empty startingList before
  calling testFunc1 -- If this possibility exists!
  '''
  return([x for x in startingList if x[0] == 'x'],
 [x for x in startingList if x[0] != 'x'])
  


I would be interested in seeing code that is faster than algorithm-1

In pure Python? Perhaps the messy variant:

def test_func(words):
 nox = []
 append = nox.append
 withx = [x for x in words if x[0] == 'x' or append(x)]
 return withx, nox



Very nice Peter,

Here are the new results for timing with your method added (algorithm-3).

Method
average (sd) time in seconds
algorithm-1 (list comprehension)
0.11774 (0.002968)
algorithm-2 (S. D'Aprano)
0.17573 (0.003385)
algorithm-2A (modified S. D'Aprano)
0.18116 (0.003081)
algorithm-3 (improved list comprehension)
0.06639 (0.001728)


Algorithm-3 is 43% faster than algorithm-1.  Again, the code used to obtain 
these results is attached.


Thanks Peter for your contribution
'''
  Purpose: Time four different algorithms for the same task
  
  Author: V. Stokes (v...@it.uu.se, 2012-08-16 (15:46), 2012-08-16)
  Refs:
   python-list@python.org list
* Strange behavior, 14-Aug-2012 17:38, light1qu...@gmail.com
* Re: Strange behavior, 14-Aug-2012 21:40, Stokes, Virgil
* Re: Strange behavior, 15-Aug-2012 02:19, Steven D'Aprano
* Re: Strange behavior, 16-Aug-2012 15:02, Peter Otten
  
  Notes:
   1. The mean and standard deviation over the runs (MC simulations)
  are estimated using recursive equations.
   2. A seed (syd) is used with the RNG for repeatability. Each run is 
  started with a new seed to force the generation of independent 
  random sequences.
   3. Warning! No checks are made on the parameters passed to
  the functions (this was by design).
   4. No effort has been made to make this code elegant. My focus was
  to make the code clear and easy to understand.
   5. This was executed on a Windows Vista 32-bit platform with Python 2.6.6
   Processor: Intel(R) core(TM)2 Duo CPU E8500@3.16GHz 3.17GHz
   6. The estimated time to completion is displayed after each run.
  
'''
import random as random
import math as math
from time import clock # clock gives good resolution on MS Windows

def testFunc1(startingList): 
'''
  Algorithm-1
  Note: 
One should check for an empty startingList before 
calling testFunc1 -- If this possibility exists!
'''
return([x for x in startingList if x[0] == 'x'], 
   [x for x in startingList if x[0] != 'x'])

def testFunc2(words):
'''
  Algorithm-2
'''
words_with_x = []
words_without_x = []
for word in words:
if word.startswith('x'):
words_with_x.append(word)
else:
words_without_x.append(word)
words[:] = words_without_x  # slice assignment
return words_with_x

def testFunc2A(words):
'''
  Algorithm-2A
'''
words_with_x = []
words_without_x = []
for word in words:
if word.startswith('x'):
words_with_x.append(word)
else:
words_without_x.append(word)
#words[:] = words_without_x  # slice assignment
return words_with_x, words_without_x

def testFunc3(words):
'''
  Algorithm-3 (from: Peter Otten) 
'''
nox = []
append = nox.append
withx = [x for x in words if x[0] == 'x' or append(x)]
return withx, nox


def genStrList(NChar,NStrng,Alph,leadChr):
'''
 Purpose: Generate a list of NStrng elements with each element a string
  of length NChar and constrained such that approx. 50% of the 
  strings will begin with the character (symbol) leadChr
   Inputs: 
   NChar -- number of characters in each element
  NStrng -- number of elements in list (strgList)
Alph -- list of characters to be used (must contain the leadChr)
 leadChr -- leading character for strings to be generated from Alph
   Otputs:
strgList -- list with NString strings of NChar characters 

Re: Verify the integrity of the tar file with tarfile module?

2012-08-16 Thread Ramchandra Apte
Just opening the file may not create an error.
You have to read some files to check for errors.

On 15 August 2012 20:29, rudson alves  wrote:

> Hello,
>
> I took a Slackware package file .tar (uncompressed) and edited with random
> characters to generate any error in its structure, as shown in the test
> bash below:
>
> $ tar -tf zoo.tar
> ./
> install/
> install/slack-desc
> tar: Pulando para o próximo cabeçalho
> tar: Exiting with failure status due to previous errors
> $ echo $?
> 2
>
> How can I verify the integrity of the tar file with tarfile module without
> having to expand it?
>
> To be more precise, even expanding this file it not generated any error
> message.
> --
> http://mail.python.org/mailman/listinfo/python-list
>
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: type(None)()

2012-08-16 Thread Robert Kern

On 8/16/12 2:56 PM, Ian Kelly wrote:

On Thu, Aug 16, 2012 at 6:47 AM, Hans Mulder  wrote:

On 8/08/12 04:14:01, Steven D'Aprano wrote:

NoneType raises an error if you try to create a second instance. bool
just returns one of the two singletons (doubletons?) again.

py> type(None)()
Traceback (most recent call last):
   File "", line 1, in 
TypeError: cannot create 'NoneType' instances


Why is that?

Why doesn't it just return an existing instance of the type,
like bool, int, str and other built-in non-mutable types do?


Because unlike those other types there is no use case for that.  It's
simpler to raise an error.


What are the use cases for the empty-argument versions of bool(), int(), 
float(), and str()?


--
Robert Kern

"I have come to believe that the whole world is an enigma, a harmless enigma
 that is made terrible by our own mad attempt to interpret it as though it had
 an underlying truth."
  -- Umberto Eco

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


Re: type(None)()

2012-08-16 Thread MRAB

On 16/08/2012 15:56, Robert Kern wrote:

On 8/16/12 2:56 PM, Ian Kelly wrote:

On Thu, Aug 16, 2012 at 6:47 AM, Hans Mulder  wrote:

On 8/08/12 04:14:01, Steven D'Aprano wrote:

NoneType raises an error if you try to create a second instance. bool
just returns one of the two singletons (doubletons?) again.

py> type(None)()
Traceback (most recent call last):
   File "", line 1, in 
TypeError: cannot create 'NoneType' instances


Why is that?

Why doesn't it just return an existing instance of the type,
like bool, int, str and other built-in non-mutable types do?


Because unlike those other types there is no use case for that.  It's
simpler to raise an error.


What are the use cases for the empty-argument versions of bool(), int(),
float(), and str()?


They can be used with defaultdict. For example:

counts = defaultdict(int)
for i in items:
counts[i] += 1

Of course, an alternative would be:

counts = defaultdict(lambda: 0)

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


Re: Dynamically determine base classes on instantiation

2012-08-16 Thread Hans Mulder
On 16/08/12 14:52:30, Thomas Bach wrote:
> On Thu, Aug 16, 2012 at 12:16:03AM +, Steven D'Aprano wrote:
>> > Some comments:
>> > 
>> > 1) What you show are not "use cases", but "examples". A use-case is a 
>> > description of an actual real-world problem that needs to be solved. A 
>> > couple of asserts is not a use-case.
> Thanks for the clarification on that one. So, here's the use-case: I'm
> querying the crunchbase API which returns JSON data and is rather
> poorly documented. I want to create a data model for the companies
> listed on Crunchbase in order to be able to put the queried data in a
> data-base. As I am too lazy to examine all the data by hand I thought
> I automatize this. I thought that it would be nice to be able to pass
> a function a parsed JSON object (AFAIK these are lists, dicts,
> strings, ints, floats, strs in Python) and it returns me the type of
> these objects. For the simple classes (str, int, float) this is quite
> trivial: F('foo') should return `str' and F(8) should return `int'.
> 
> For a compound object like dict I would like it to return the data
> fields with their type. Hence, F({'foo': 8}) should return 
> {'foo': int}, and given that f = F({'foo': {'bar': 80}}) I would like
> f to equal to {'foo': dict}, with the option to query the type of
> 'foo' via f.foo, where the latter should equal to {'bar': int}. So
> far, this is not a complicated case. But, sometimes a data field on
> returned data set is simply None. Thus, I want to extract the types from
> another data set and merge the two.
> 
> So, my question (as far as I can see it, please correct me if I am
> wrong) is less of the "How do I achieve this?"-kind, but more of the
> "What is a clean design for this?"-kind. My intuitive thought was that
> the `merge' function should be a part of the object returned from `F'.

The misunderstanding is that you feel F should return an object with
a 'merge' method and a varying abse type, while Steven and others
think that F should be a function.

Maybe something like:

def F(obj):
if obj is None:
return None
tp = type(obj)
if tp in (bool, int, float, str):
return tp
elif tp is list:
return merge([F(elem) for elem in obj])
elif tp is dict:
return dict((k, F(v)) for k,v in obj.iteritems())
else:
raise ValueError("Unexpected type %s for value %s" %(tp, obj))

def merge(lst):
if None in lst:
not_nones = [elem for elem in lst if elem is not None]
if not_nones:
not_none = not_nones[0]
lst = [not_none if elem is None else elem for elem in lst]
else:
return lst  # all elements are None; nothing can be done
types = {}
for elem in lst:
if type(elem) is dict:
for k,v in elem.iteritems():
if v is None:
if k in types:
elem[k] = types[k]
else:
for other in lst:
if (other is not elem
and type(other) is dict
and k in other
and other[k] is not None
):
elem[k] = types[k] = other[k]
break
return lst


The merge logic you have in mind may be different from what I just
made up, but the idea remains: F and merge can be functions.


Hope this helps,

-- HansM



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


Re: [ANNC] pybotwar-0.8

2012-08-16 Thread Mark Lawrence

and "bottom" reads better than "top"

On 16/08/2012 15:37, Ramchandra Apte wrote:

nah.. "war" sounds better than "contest"

On 16 August 2012 14:59, Jean-Michel Pichavant wrote:


Lee Harr wrote:


pybotwar is a fun and educational game where players
write computer programs to control simulated robots.

http://pybotwar.googlecode.**com/ 


The focus of this release is making all functionality
available from the PyQt interface and making PyQt
the default interface.

pybotwar uses pybox2d for the physical simulation.
It can be run in text-only mode -- useful for longer
tournaments -- or use pyqt or pygame for a graphical
interface and visualization of the action.

pybotwar is released under GPLv3.


Changes in pybotwar-0.8:
 - pybox2d-2.0.2b2 is now the required version

 API
 - Robot.turret() now takes turret speed instead of angle
 - PING sensor now differentiates own bullets from enemy bullets
 - PING differentiates dead robots when remove_dead_robots=False
 - made internal Robot state variables less likely to conflict with
user code

 Settings
 - added "robots dir" setting to make it easier to run pybotwar
 from an installed copy, rather than from the unpacked folder
 - logs, lineups, and db all go in robots dir
 - robot modules are now loaded by full path
 - If using Qt settings, remembers most recently used set of robots

 PyQt4 Interface
 - PyQt4 view mode is now the default
 - (text mode and pygame/pygsear still available)
 - all settings are available from PyQt interface
 - remembers most recently used set of robots
 - added debug window showing sensor values, commands, and logs
 - can now start tournaments (and multiple battles) from pyqt
 - can also start tournaments to run in background
 - editor automatically adds .py to robot filename if needed

 Other changes
 - made template.py a valid robot program
 - single battles are now run as 1-battle tournaments
 - renamed kill stat to outlasted
 - started tracking kills as dealing final damage to other robot
 - commandline can now take a list of robots to load
 - added "Super Tournaments"
 - runs tournaments with all possible combinations of robots

 Fixes:
 - made POSition sensor scale same as PING sensor
 - fixed editor backspace between left edge and start of text
 - fixed inability to re-open file once its window was closed
 - fixed crash when cancelling file-open dialog
 - limited motor speed on turret
 - log messages in testmode when using qt4 view
 - fall back to :memory: database if unable to open db file




I have some suggestions:

- rename pybotwar into pybotcontest
- replace "bullet" by "missile of passion"
- replace "dead" by "overflood with love"
- "PING sensor" into "pheromone sensor"
... and so on :p

JM

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








--
Cheers.

Mark Lawrence.

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


Re: type(None)()

2012-08-16 Thread Stefan Behnel
Steven D'Aprano, 16.08.2012 15:58:
>>> NoneType raises an error if you try to create a second instance.
> In my opinion, this is a PITA for None and better behaviour would be to 
> return the pre-existing NoneType instance, but I didn't design the 
> language.

The time machine strikes again.

Python 3.3.0b1 (default:f7b59e890e30, Aug 11 2012, 05:30:10)
[GCC 4.6.3] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> type(None)()
>>> print(type(None)())
None

Stefan


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


Re: type(None)()

2012-08-16 Thread Ramchandra Apte
Are they the same object

On 16 August 2012 21:01, Stefan Behnel  wrote:

> Steven D'Aprano, 16.08.2012 15:58:
> >>> NoneType raises an error if you try to create a second instance.
> > In my opinion, this is a PITA for None and better behaviour would be to
> > return the pre-existing NoneType instance, but I didn't design the
> > language.
>
> The time machine strikes again.
>
> Python 3.3.0b1 (default:f7b59e890e30, Aug 11 2012, 05:30:10)
> [GCC 4.6.3] on linux
> Type "help", "copyright", "credits" or "license" for more information.
> >>> type(None)()
> >>> print(type(None)())
> None
>
> Stefan
>
>
> --
> http://mail.python.org/mailman/listinfo/python-list
>
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: [ANNC] pybotwar-0.8

2012-08-16 Thread Ramchandra Apte
On 16 August 2012 21:00, Mark Lawrence  wrote:

> and "bottom" reads better than "top"
>
>
> On 16/08/2012 15:37, Ramchandra Apte wrote:
>
>> nah.. "war" sounds better than "contest"
>>
>> On 16 August 2012 14:59, Jean-Michel Pichavant > >wrote:
>>
>>  Lee Harr wrote:
>>>
>>>  pybotwar is a fun and educational game where players
 write computer programs to control simulated robots.

 http://pybotwar.googlecode.com/ 
 
 >



 The focus of this release is making all functionality
 available from the PyQt interface and making PyQt
 the default interface.

 pybotwar uses pybox2d for the physical simulation.
 It can be run in text-only mode -- useful for longer
 tournaments -- or use pyqt or pygame for a graphical
 interface and visualization of the action.

 pybotwar is released under GPLv3.


 Changes in pybotwar-0.8:
  - pybox2d-2.0.2b2 is now the required version

  API
  - Robot.turret() now takes turret speed instead of angle
  - PING sensor now differentiates own bullets from enemy bullets
  - PING differentiates dead robots when remove_dead_robots=False
  - made internal Robot state variables less likely to conflict with
 user code

  Settings
  - added "robots dir" setting to make it easier to run pybotwar
  from an installed copy, rather than from the unpacked folder
  - logs, lineups, and db all go in robots dir
  - robot modules are now loaded by full path
  - If using Qt settings, remembers most recently used set of robots

  PyQt4 Interface
  - PyQt4 view mode is now the default
  - (text mode and pygame/pygsear still available)
  - all settings are available from PyQt interface
  - remembers most recently used set of robots
  - added debug window showing sensor values, commands, and logs
  - can now start tournaments (and multiple battles) from pyqt
  - can also start tournaments to run in background
  - editor automatically adds .py to robot filename if needed

  Other changes
  - made template.py a valid robot program
  - single battles are now run as 1-battle tournaments
  - renamed kill stat to outlasted
  - started tracking kills as dealing final damage to other robot
  - commandline can now take a list of robots to load
  - added "Super Tournaments"
  - runs tournaments with all possible combinations of robots

  Fixes:
  - made POSition sensor scale same as PING sensor
  - fixed editor backspace between left edge and start of text
  - fixed inability to re-open file once its window was closed
  - fixed crash when cancelling file-open dialog
  - limited motor speed on turret
  - log messages in testmode when using qt4 view
  - fall back to :memory: database if unable to open db file



  I have some suggestions:
>>>
>>> - rename pybotwar into pybotcontest
>>> - replace "bullet" by "missile of passion"
>>> - replace "dead" by "overflood with love"
>>> - "PING sensor" into "pheromone sensor"
>>> ... and so on :p
>>>
>>> JM
>>>
>>> --
>>> http://mail.python.org/mailman/listinfo/python-list
>>> http://mail.python.org/mailman/listinfo/python-list>
>>> >
>>>
>>>
>>
>>
>> Look you are the only person complaining about top-posting.
GMail uses top-posting by default.
I can't help it if you feel irritated by it.

>
> --
> Cheers.
>
> Mark Lawrence.
>
> --
> http://mail.python.org/**mailman/listinfo/python-list
>
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: [ANNC] pybotwar-0.8

2012-08-16 Thread Ramchandra Apte
I think I'll use Google Groups now.

On 16 August 2012 21:10, Ramchandra Apte  wrote:

>
>
> On 16 August 2012 21:00, Mark Lawrence  wrote:
>
>> and "bottom" reads better than "top"
>>
>>
>> On 16/08/2012 15:37, Ramchandra Apte wrote:
>>
>>> nah.. "war" sounds better than "contest"
>>>
>>> On 16 August 2012 14:59, Jean-Michel Pichavant >> >wrote:
>>>
>>>  Lee Harr wrote:

  pybotwar is a fun and educational game where players
> write computer programs to control simulated robots.
>
> http://pybotwar.googlecode.com/ 
> 
> >
>
>
>
> The focus of this release is making all functionality
> available from the PyQt interface and making PyQt
> the default interface.
>
> pybotwar uses pybox2d for the physical simulation.
> It can be run in text-only mode -- useful for longer
> tournaments -- or use pyqt or pygame for a graphical
> interface and visualization of the action.
>
> pybotwar is released under GPLv3.
>
>
> Changes in pybotwar-0.8:
>  - pybox2d-2.0.2b2 is now the required version
>
>  API
>  - Robot.turret() now takes turret speed instead of angle
>  - PING sensor now differentiates own bullets from enemy bullets
>  - PING differentiates dead robots when remove_dead_robots=False
>  - made internal Robot state variables less likely to conflict with
> user code
>
>  Settings
>  - added "robots dir" setting to make it easier to run pybotwar
>  from an installed copy, rather than from the unpacked folder
>  - logs, lineups, and db all go in robots dir
>  - robot modules are now loaded by full path
>  - If using Qt settings, remembers most recently used set of robots
>
>  PyQt4 Interface
>  - PyQt4 view mode is now the default
>  - (text mode and pygame/pygsear still available)
>  - all settings are available from PyQt interface
>  - remembers most recently used set of robots
>  - added debug window showing sensor values, commands, and logs
>  - can now start tournaments (and multiple battles) from pyqt
>  - can also start tournaments to run in background
>  - editor automatically adds .py to robot filename if needed
>
>  Other changes
>  - made template.py a valid robot program
>  - single battles are now run as 1-battle tournaments
>  - renamed kill stat to outlasted
>  - started tracking kills as dealing final damage to other robot
>  - commandline can now take a list of robots to load
>  - added "Super Tournaments"
>  - runs tournaments with all possible combinations of robots
>
>  Fixes:
>  - made POSition sensor scale same as PING sensor
>  - fixed editor backspace between left edge and start of text
>  - fixed inability to re-open file once its window was closed
>  - fixed crash when cancelling file-open dialog
>  - limited motor speed on turret
>  - log messages in testmode when using qt4 view
>  - fall back to :memory: database if unable to open db file
>
>
>
>  I have some suggestions:

 - rename pybotwar into pybotcontest
 - replace "bullet" by "missile of passion"
 - replace "dead" by "overflood with love"
 - "PING sensor" into "pheromone sensor"
 ... and so on :p

 JM

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


>>>
>>>
>>> Look you are the only person complaining about top-posting.
> GMail uses top-posting by default.
> I can't help it if you feel irritated by it.
>
>>
>> --
>> Cheers.
>>
>> Mark Lawrence.
>>
>> --
>> http://mail.python.org/**mailman/listinfo/python-list
>>
>
>
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: type(None)()

2012-08-16 Thread Stefan Behnel
Ramchandra Apte, 16.08.2012 17:39:
> On 16 August 2012 21:01, Stefan Behnel wrote:
>> Steven D'Aprano, 16.08.2012 15:58:
> NoneType raises an error if you try to create a second instance.
>>> In my opinion, this is a PITA for None and better behaviour would be to
>>> return the pre-existing NoneType instance, but I didn't design the
>>> language.
>>
>> The time machine strikes again.
>>
>> Python 3.3.0b1 (default:f7b59e890e30, Aug 11 2012, 05:30:10)
>> [GCC 4.6.3] on linux
>> Type "help", "copyright", "credits" or "license" for more information.
> type(None)()
> print(type(None)())
>> None
>
> Are they the same object

Obviously. None is a singleton (as was already mentioned a couple of times
in this thread).

Stefan


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


Re: Sharing code between different projects?

2012-08-16 Thread andrea crotti
2012/8/16 andrea crotti :
>
>
> Unfortunately I think you guess wrong
> http://forums.perforce.com/index.php?/topic/553-perforce-svnexternals-equivalent/
> Anyway with views and similar things is not that hard to implement the
> same thing..


I'm very happy to say that I finally made it!

It took 3 hours to move / merge a few thousand lines around but
everything seems to work perfectly now..

At the moment I'm just using symlinks, I'll see later if something
smarter is necessary, thanks to everyone for the ideas.
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: type(None)()

2012-08-16 Thread Ethan Furman

Ramchandra Apte wrote:

Are they the same object


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


Re: type(None)()

2012-08-16 Thread Ethan Furman

Hans Mulder wrote:

On 8/08/12 04:14:01, Steven D'Aprano wrote:

NoneType raises an error if you try to create a second instance. bool
just returns one of the two singletons (doubletons?) again.

py> type(None)()
Traceback (most recent call last):
  File "", line 1, in 
TypeError: cannot create 'NoneType' instances


Why is that?


An oversight, and until a few months ago nobody had complained loud 
enough.  ;)




Why doesn't it just return an existing instance of the type,
like bool, int, str and other built-in non-mutable types do?


In 3.3 it now does.

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


Re: dbf.py API question concerning Index.index_search()

2012-08-16 Thread Ethan Furman

MRAB wrote:

On 16/08/2012 02:22, Ethan Furman wrote:

Steven D'Aprano wrote:

On Wed, 15 Aug 2012 16:26:09 -0700, Ethan Furman wrote:


Indexes have a new method (rebirth of an old one, really):

   .index_search(
  match,
  start=None,
  stop=None,
  nearest=False,
  partial=False )

[...]

Why "index_search" rather than just "search"?


Because "search" already exists and returns a dbf.List of all matching
records.


Perhaps that should've been called "find_all"!


In interesting thought.

Currently there are:

  .index(data)   --> returns index of data in Index, or raises error
  .query(string) --> brute force search, returns all matching records
  .search(match) --> binary search through table, returns all matching
 records

'index' and 'query' are supported by Tables, Lists, and Indexes; search 
(and now index_search) are only supported on Indexes.


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


Re: OT: Monty Python in Syria

2012-08-16 Thread Dotan Cohen
On Thu, Aug 16, 2012 at 3:01 AM, Terry Reedy  wrote:
> But it has nothing to do with Monty Python either, that I can see.
> Nor is there a video to see the context of (OP said "For context, start the
> video at 1:00.") Perhaps link is erroneous.
>

At 1:00 the captor asks the pilot to state his name, then asks him to
state his quest. I was expecting the third question to be about
favourite colours or swallows.


> Marking something OT does not excuse it. It should still be related. We
> cannot allow 1000s of OT posts a day, marked OT or not.
>

Agreed. An incidence is not a phenomenon.

-- 
Dotan Cohen

http://gibberish.co.il
http://what-is-what.com
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: dbf.py API question concerning Index.index_search()

2012-08-16 Thread MRAB

On 16/08/2012 17:13, Ethan Furman wrote:

MRAB wrote:

On 16/08/2012 02:22, Ethan Furman wrote:

Steven D'Aprano wrote:

On Wed, 15 Aug 2012 16:26:09 -0700, Ethan Furman wrote:


Indexes have a new method (rebirth of an old one, really):

   .index_search(
  match,
  start=None,
  stop=None,
  nearest=False,
  partial=False )

[...]

Why "index_search" rather than just "search"?


Because "search" already exists and returns a dbf.List of all matching
records.


Perhaps that should've been called "find_all"!


In interesting thought.

Currently there are:

.index(data)   --> returns index of data in Index, or raises error
.query(string) --> brute force search, returns all matching records
.search(match) --> binary search through table, returns all matching
   records

'index' and 'query' are supported by Tables, Lists, and Indexes; search
(and now index_search) are only supported on Indexes.


What exactly is the difference between .index and .index_search with
the default arguments?
--
http://mail.python.org/mailman/listinfo/python-list


Re: [ANNC] pybotwar-0.8

2012-08-16 Thread MRAB

On 16/08/2012 16:40, Ramchandra Apte wrote:


On 16 August 2012 21:00, Mark Lawrence mailto:breamore...@yahoo.co.uk>> wrote:

and "bottom" reads better than "top"


[snip]


Look you are the only person complaining about top-posting.
GMail uses top-posting by default.
I can't help it if you feel irritated by it.


I would've complained too if I was going to post a reply to this thread.
--
http://mail.python.org/mailman/listinfo/python-list


Re: Dynamically determine base classes on instantiation

2012-08-16 Thread Thomas Bach
On Thu, Aug 16, 2012 at 05:10:43PM +0200, Hans Mulder wrote:
> On 16/08/12 14:52:30, Thomas Bach wrote:
> > 
> > So, my question (as far as I can see it, please correct me if I am
> > wrong) is less of the "How do I achieve this?"-kind, but more of the
> > "What is a clean design for this?"-kind. My intuitive thought was that
> > the `merge' function should be a part of the object returned from `F'.
> 
> The misunderstanding is that you feel F should return an object with
> a 'merge' method and a varying abse type, while Steven and others
> think that F should be a function.

OK, then my design wasn't so bad in the first place. :)

I made a class `Model' which wraps the actual type and realized
`merge' and `F' (with a better name, though) as classmethods of
`Model' in order to tie together the stuff that belongs together. By
the way, another need I saw for this design was that

setattr(Model(), 'foo', {'bar': int})

works, whereas 

setattr(dict(), 'foo', {'bar': int}) 

raises an AttributeError (on Python 3.2). Could someone give me the
buzz word (or even an explanation) on why that is so?

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


Re: Dynamically determine base classes on instantiation

2012-08-16 Thread Richard Thomas
class Foo(object):
def __new__(cls, arg):
if isinstance(arg, list):
cls = FooList
elif isinstance(arg, dict):
cls = FooDict
return object.__new__(cls, arg)

class FooList(Foo, list):
pass

class FooDict(Foo, dict):
pass

You could even have __new__ make these Foo* classes dynamically when it 
encounters a new type of argument.

Chard.

On Thursday, 16 August 2012 18:54:12 UTC+2, Thomas Bach  wrote:
> On Thu, Aug 16, 2012 at 05:10:43PM +0200, Hans Mulder wrote:
> 
> > On 16/08/12 14:52:30, Thomas Bach wrote:
> 
> > > 
> 
> > > So, my question (as far as I can see it, please correct me if I am
> 
> > > wrong) is less of the "How do I achieve this?"-kind, but more of the
> 
> > > "What is a clean design for this?"-kind. My intuitive thought was that
> 
> > > the `merge' function should be a part of the object returned from `F'.
> 
> > 
> 
> > The misunderstanding is that you feel F should return an object with
> 
> > a 'merge' method and a varying abse type, while Steven and others
> 
> > think that F should be a function.
> 
> 
> 
> OK, then my design wasn't so bad in the first place. :)
> 
> 
> 
> I made a class `Model' which wraps the actual type and realized
> 
> `merge' and `F' (with a better name, though) as classmethods of
> 
> `Model' in order to tie together the stuff that belongs together. By
> 
> the way, another need I saw for this design was that
> 
> 
> 
> setattr(Model(), 'foo', {'bar': int})
> 
> 
> 
> works, whereas 
> 
> 
> 
> setattr(dict(), 'foo', {'bar': int}) 
> 
> 
> 
> raises an AttributeError (on Python 3.2). Could someone give me the
> 
> buzz word (or even an explanation) on why that is so?
> 
> 
> 
>  Thomas Bach

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


Re: Dynamically determine base classes on instantiation

2012-08-16 Thread Richard Thomas
> a is a Foo
> b is a Foo
> therefore a and b are the same type

What you mean here is "a and b share a common base class".
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Dynamically determine base classes on instantiation

2012-08-16 Thread Thomas Bach
On Thu, Aug 16, 2012 at 12:29:21PM -0400, Dennis Lee Bieber wrote:
> On Thu, 16 Aug 2012 14:52:30 +0200, Thomas Bach
>  declaimed the following in
> gmane.comp.python.general:
> 
>   Of course, since the parse result (at least from my recent
> experiment) is a Python structure, it isn't difficult to walk that
> structure...

I prefer that one, as I have the parsed data already lying around in
memory. But, as I think about it, I could also pass it to json.dumps
and parse it again. But, that wouldn't make much sense, right?

> 
>   "But, sometimes a data field on returned data set is simply None.
> Thus, I want to extract the types from another data set and merge the
> two." ??? A "data field" /value/ of None has the /type/ " 'NoneType'>", so I don't quite understand what you intend to merge? You
> can't arbitrarily change the "type" without changing the "value".

OK, I am probably using the wrong vocabulary here again. :(

Imagine you have two data sets:

d1 = {'foo': None}
d2 = {'foo': 8}

Where I would assume that d1 has "foo" not set. That's why I want this
whole "merge"-thing in the first place: to be able to extract the type
{'foo': None} from d1 and {'foo': int} from d2 and merge the two
together which should result in {'foo': int}.

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


Re: Dynamically determine base classes on instantiation

2012-08-16 Thread Thomas Bach
On Thu, Aug 16, 2012 at 10:03:51AM -0700, Richard Thomas wrote:
> class Foo(object):
> def __new__(cls, arg):
> if isinstance(arg, list):
> cls = FooList
> elif isinstance(arg, dict):
> cls = FooDict
> return object.__new__(cls, arg)
> 
> class FooList(Foo, list):
> pass
> 
> class FooDict(Foo, dict):
> pass
> 
> You could even have __new__ make these Foo* classes dynamically when
> it encounters a new type of argument.
> 
> Chard.

Thanks for that one. Your solution just hit me like a punch in the
face. I had something similar in my mind. But I could not work out how
the mechanics behind it are working.

Regards,

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


Re: dbf.py API question concerning Index.index_search()

2012-08-16 Thread Ethan Furman

MRAB wrote:

On 16/08/2012 17:13, Ethan Furman wrote:

Currently there are:

.index(data)   --> returns index of data in Index, or raises error
.query(string) --> brute force search, returns all matching records
.search(match) --> binary search through table, returns all matching
   records

'index' and 'query' are supported by Tables, Lists, and Indexes; search
(and now index_search) are only supported on Indexes.


What exactly is the difference between .index and .index_search with
the default arguments?


.index requires a data structure that can be compared to a record 
(another record, a dictionary with the same field/key names, or a 
list/tuple with values in the same order as the fields).  It returns the 
index or raises NotFoundError.  It is brute force.


.index_search requires match criteria (a tuple with the desired values 
in the same order as the key).  It returns the index or raises 
NotFoundError (unless nearest is True -- then the value returned is 
where the match should be).  It is binary search.


So the only similarity is that they both return a number or raise 
NotFoundError.  What they use for the search and how they perform the 
search are both completely different.


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


Re: Strange behavior

2012-08-16 Thread Steven D'Aprano
On Thu, 16 Aug 2012 13:18:59 +0200, Virgil Stokes wrote:

> On 15-Aug-2012 02:19, Steven D'Aprano wrote:
>> On Tue, 14 Aug 2012 21:40:10 +0200, Virgil Stokes wrote:
>>
>>> You might find the following useful:
>>>
>>> def testFunc(startingList):
>>>   xOnlyList = []; j = -1
>>>   for xl in startingList:
>>>   if (xl[0] == 'x'):
>> That's going to fail in the starting list contains an empty string. Use
>> xl.startswith('x') instead.
>
> Yes, but this was by design (tacitly assumed that startingList was both
> a list and non-empty).

As Peter already pointed out, I said it would fail if the list contains 
an empty string, not if the list was empty.


>>>   xOnlyList.append(xl)
>>>   else:
>>>   j += 1
>>>   startingList[j] = xl
>>
>> Very cunning, but I have to say that your algorithm fails the "is this
>> obviously correct without needing to study it?" test. Sometimes that is
>> unavoidable, but for something like this, there are simpler ways to
>> solve the same problem.
>
> Sorry, but I do not sure what you mean here.

In a perfect world, you should be able to look at a piece of code, read 
it once, and see whether or not it is correct. That is what I mean by 
"obviously correct". For example, if I have a function that takes an 
argument, doubles it, and prints the result:

def f1(x):
print(2*x)


that is obviously correct. Whereas this is not:

def f2(x):
y = (x + 5)**2 - (x + 4)**2
sys.stdout.write(str(y - 9) + '\n')


because you have to study it to see whether or not it works correctly.

Not all programs are simple enough to be obviously correct. Sometimes you 
have no choice but to write something which requires cleverness to get 
the right result. But this is not one of those cases. You should almost 
always prefer simple code over clever code, because the greatest expense 
in programming (time, effort and money) is to make code correct.

Most code does not need to be fast. But all code needs to be correct.


[...]
> This can meet the requirement that startingList is modified in place via
> the call to this function (see the attached code).

Good grief! See, that's exactly the sort of thing I'm talking about. 
Without *detailed* study of your attached code, how can I possibly know 
what it does or whether it does it correctly?

Your timing code calculates the mean using a recursive algorithm. Why 
don't you calculate the mean the standard way: add the numbers and divide 
by the total? What benefit do you gain from a more complicated algorithm 
when a simple one will do the job just as well?

You have spent a lot of effort creating a complicated, non-obvious piece 
of timing code, with different random seeds for each run, and complicated 
ways of calculating timing statistics... but unfortunately the most 
important part of any timing test, the actually *timing*, is not done 
correctly. Consequently, your code is not correct.

With an average time of a fraction of a second, none of those timing 
results are trustworthy, because they are vulnerable to interference from 
other processes, the operating system, and other random noise. You spend 
a lot of time processing the timing results, but it is Garbage In, 
Garbage Out -- the results are not trustworthy, and if they are correct, 
it is only by accident.

Later in your post, you run some tests, and are surprised by the result:

> Why is algorithm-2A slower than algorithm-2?

It isn't slower. It is physically impossible, since 2A does *less* work 
than 2. This demonstrates that you are actually taking a noisy 
measurement: the values you get have random noise, and you don't make any 
effort to minimise that noise. Hence GIGO.

The right way to test small code snippets is with the timeit module. It 
is carefully written to overcome as much random noise as possible. But 
even there, the authors of the timeit module are very clear that you 
should not try to calculate means, let alone higher order statistics like 
standard deviation. The only statistic which is trustworthy is to run as 
many trials as you can afford, and select the minimum value.

So here is my timing code, which is much shorter and simpler and doesn't 
try to do too much. You do need to understand the timeit.Timer class:

timeit.Timer creates a timer object; timer.repeat does the actual timing. 
The specific arguments to them are not vital to understand, but you can 
read the documentation if you wish to find out what they mean.

First, I define the two functions. I compare similar functions that have 
the same effect. Neither modifies the input argument in place. Copy and 
paste the following block into an interactive interpreter:

# Start block

def f1(startingList):
return ([x for x in startingList if x[0] == 'x'],
[x for x in startingList if x[0] != 'x'])

# Note that the above function is INCORRECT, it will fail if a string is
# empty; nevertheless I will use it for timing purposes anyway.


def 

Re: Dynamically determine base classes on instantiation

2012-08-16 Thread Steven D'Aprano
On Thu, 16 Aug 2012 10:09:08 -0700, Richard Thomas wrote:

>> a is a Foo
>> b is a Foo
>> therefore a and b are the same type
> 
> What you mean here is "a and b share a common base class".

No. I mean what I said: since a and b are both direct instances of Foo, 
not subclasses, they are both the same type, namely Foo.

"Share a common base class" is a much weaker statement:

class Foo: pass
class Bar(Foo): pass

a = Foo()
b = Bar()

Now we can see that a and b are NOT the same type, but they share a 
common base class, Foo.


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


Re: [ANNC] pybotwar-0.8

2012-08-16 Thread Ethan Furman

MRAB wrote:

On 16/08/2012 16:40, Ramchandra Apte wrote:

On 16 August 2012 21:00, Mark Lawrence wrote:


and "bottom" reads better than "top"



[snip]


Look you are the only person complaining about top-posting.
GMail uses top-posting by default.
I can't help it if you feel irritated by it.


I would've complained too if I was going to post a reply to this thread.


Rest assured that in this (bottom-posting vs top-posting) Mark Lawrence 
speaks for many of us.


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


Re: Dynamically determine base classes on instantiation

2012-08-16 Thread Steven D'Aprano
On Thu, 16 Aug 2012 10:03:51 -0700, Richard Thomas wrote:

> class Foo(object):
> def __new__(cls, arg):
> if isinstance(arg, list):
> cls = FooList
> elif isinstance(arg, dict):
> cls = FooDict
> return object.__new__(cls, arg)
> 
> class FooList(Foo, list):
> pass
> 
> class FooDict(Foo, dict):
> pass


Did you actually try your code?


py> x = Foo([])
Traceback (most recent call last):
  File "", line 1, in 
  File "", line 7, in __new__
TypeError: object.__new__(FooList) is not safe, use list.__new__()




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


Re: Dynamically determine base classes on instantiation

2012-08-16 Thread Steven D'Aprano
On Thu, 16 Aug 2012 14:52:30 +0200, Thomas Bach wrote:

> I'm
> querying the crunchbase API which returns JSON data and is rather poorly
> documented. I want to create a data model for the companies listed on
> Crunchbase in order to be able to put the queried data in a data-base.
> As I am too lazy to examine all the data by hand I thought I automatize
> this. I thought that it would be nice to be able to pass a function a
> parsed JSON object (AFAIK these are lists, dicts, strings, ints, floats,
> strs in Python) and it returns me the type of these objects. For the
> simple classes (str, int, float) this is quite trivial: F('foo') should
> return `str' and F(8) should return `int'.

Um, this is utterly trivial for *any* object. Just call type(object), and 
it will return the type of the object.


> For a compound object like dict I would like it to return the data
> fields with their type. Hence, F({'foo': 8}) should return {'foo': int},

Your first problem is defining what you consider a compound object. Once 
you've done that, it just becomes a matter of recursion:

def recursive_type(obj):
if isinstance(obj, dict):
return dict((k, recursive_type(v)) for (k, v) in obj.items())
elif isinstance(obj, list):
pass  # whatever...
else:
return type(obj)



> and given that f = F({'foo': {'bar': 80}}) I would like f to equal to
> {'foo': dict}, with the option to query the type of 'foo' via f.foo,
> where the latter should equal to {'bar': int}. So far, this is not a
> complicated case. But, sometimes a data field on returned data set is
> simply None. Thus, I want to extract the types from another data set and
> merge the two.
> 
> So, my question (as far as I can see it, please correct me if I am
> wrong) is less of the "How do I achieve this?"-kind, but more of the
> "What is a clean design for this?"-kind. My intuitive thought was that
> the `merge' function should be a part of the object returned from `F'.

This isn't Java you know. Just write a function to merge the two data 
sets and be done with it.


>> Consider your two examples:
>> 
>> a = Foo(['a', 'list'])
>> b = Foo({'blah': 8})
>> 
>> According to your design:
>> 
>> a is a Foo
>> b is a Foo
> 
> I actually never said that. 

You might not have said that, but that's what instantiation implies. If 
you instantiate Foo, you get a Foo instance.


> I simply wanted `a' and `b' to share the
> same function (the `merge' function), I thought that the easiest way to
> achieve this is by letting them share the same name-space.

Or you could you use composition, or a mixin, or straits, or prototypes. 
Well, prototypes are hard in Python -- I'm not sure how you would go 
about doing that.



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


Re: Dynamically determine base classes on instantiation

2012-08-16 Thread Steven D'Aprano
On Thu, 16 Aug 2012 19:18:18 +0200, Thomas Bach wrote:

> Imagine you have two data sets:
> 
> d1 = {'foo': None}
> d2 = {'foo': 8}
> 
> Where I would assume that d1 has "foo" not set. That's why I want this
> whole "merge"-thing in the first place: to be able to extract the type
> {'foo': None} from d1 and {'foo': int} from d2 and merge the two
> together which should result in {'foo': int}.

That becomes trivial if you do the merge before converting to types:

d3 = d1.copy()  # the merged dict
for key, value in d2.items():
if key in d1 and d1[key] is None:
d3[key] = value  # merge

Now pass d3 to your recursive_type function.


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


set and dict iteration

2012-08-16 Thread Aaron Brady
Hello,

I observed an inconsistency in the behavior of 'set' and 'dict' iterators.  It 
is "by design" according to the docs.

'''
http://docs.python.org/dev/library/stdtypes.html#dict-views

iter(dictview).  Iterating views while adding or deleting entries in the 
dictionary may raise a RuntimeError or fail to iterate over all entries.
'''

The 'set' has the same behavior.  Iteration might also complete successfully.

The inconsistency is, if we remove an element from a set and add another during 
iteration, the new element might appear later in the iteration, and might not, 
depending on the hash code; therefore comparing the size of the set between 
iterations isn't adequate.  Example:

http://home.comcast.net/~castironpi-misc/clpy-0062%20set%20iterators.py  

'''
# py: { 'ver': '3' }

set0= set( ( 1, 2 ) )

iter0= iter( set0 )
print( next( iter0 ) )

set0.add( 3 )
set0.remove( 2 )
print( next( iter0 ) )


print( )

set0= set( ( 6, 7 ) )

iter0= iter( set0 )
print( next( iter0 ) )

set0.add( 8 )
set0.remove( 7 )
print( next( iter0 ) )
'''

Output:

'''
1
3

6
Traceback (most recent call last):
  File [...] line 22, in 
print( next( iter0 ) )
StopIteration
'''

Iteration should behave the same regardless of the contents of the set.  
Continuing iteration over sets and dicts after a modification isn't defined; it 
should unconditionally raise an error.

What's going on, is '8' is added before the position of the iterator due to 
hashing in the second part, but the size doesn't change, so the iterator 
reaches the end of the set after '7' is removed.

The inconsistency isn't easily solved.  One possibility is to use a timestamp 
or other serial index in the object and iterators, and compare them on every 
iteration to determine if a modification has occurred.

Another possibility which the author prefers, is to maintain a secondary 
collection of the iterators of an object, and invalidate them upon 
modification.  The applicable collection structure is a doubly-linked linked 
list, informally depicted:

http://home.comcast.net/~castironpi-misc/clpy-0062%20set%20iterators.png

Upon modification, the set traverses its iterators, setting an 'invalid' flag 
on each; and subsequent calls to any of them raise an 'IterationError'.  Adding 
and removing iterators to and from the secondary list is performed in O( 1 ) 
time with no penalty.

The above example depicted a 'Set'.  'Dicts' have the same anomaly, but the 
solution is ambiguous, since dict values can be changed meaningfully without 
altering the structure of the object.  In the author's opinion, the dict should 
not raise an 'IterationError' on value changes, only key changes like the set, 
but the argument isn't conclusive.
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: [ANNC] pybotwar-0.8

2012-08-16 Thread William R. Wing (Bill Wing)
On Aug 16, 2012, at 11:40 AM, Ramchandra Apte  wrote:

> Look you are the only person complaining about top-posting.
> GMail uses top-posting by default.
> 

MANY of us find it irritating...

and it only takes a second to move your cursor down and play nice.

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


Re: set and dict iteration

2012-08-16 Thread Dave Angel
On 08/16/2012 02:00 PM, Aaron Brady wrote:
> Hello,
>
> I observed an inconsistency in the behavior of 'set' and 'dict' iterators.  
> It is "by design" according to the docs.
>
> '''
> http://docs.python.org/dev/library/stdtypes.html#dict-views
>
> iter(dictview).  Iterating views while adding or deleting entries in the 
> dictionary may raise a RuntimeError or fail to iterate over all entries.
> '''
>
> The 'set' has the same behavior.  Iteration might also complete successfully.
>
> The inconsistency is, if we remove an element from a set and add another 
> during iteration, the new element might appear later in the iteration, and 
> might not, depending on the hash code; therefore comparing the size of the 
> set between iterations isn't adequate.  Example:
> 
>
>
> Iteration should behave the same regardless of the contents of the set.  
> Continuing iteration over sets and dicts after a modification isn't defined; 
> it should unconditionally raise an error.

Why is it the iterator's job to protect against the user's  bug?  The
doc is clear enough.  If you don't change the collection, you won't have
a problem.

> .

Everything else is implementation defined.  Why should an implementation
be forced to have ANY extra data structure to detect a static bug in the
caller's code?



-- 

DaveA

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


Re: Verify the integrity of the tar file with tarfile module?

2012-08-16 Thread Dave Angel
On 08/16/2012 10:51 AM, Ramchandra Apte wrote:
> Just opening the file may not create an error.
> You have to read some files to check for errors.

You have no context in front of your message, and it's not a reply to
anything existing.  So it must be top-posted.  Try again.



-- 

DaveA

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


Re: [ANNC] pybotwar-0.8

2012-08-16 Thread Dave Angel
On 08/16/2012 10:37 AM, Ramchandra Apte wrote:
> nah.. "war" sounds better than "contest"
Top-posting yet again?



-- 

DaveA

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


Re: Running Python web apps on shared ASO servers?

2012-08-16 Thread Emile van Sebille

On 8/16/2012 7:01 AM Gilles said...

On Sun, 12 Aug 2012 02:03:33 +0200, Gilles  wrote:

Does it mean that ASO only supports writing Python web apps as
long-running processes (CGI, FCGI, WSGI, SCGI) instead of embedded
Python à la PHP?


I need to get the big picture about the different solutions to run a
Python web application.


From what I read, it seems like this is the way things involved over

the years:

CGI : original method. Slow because the server has to spawn a new
process to run the interpreter + script every time a script is run.

mod_python : Apache module alternative to CGI. The interpreter is
loaded once, and running a script means just handling the script

mod_wsgi : mod_python is no longer developped, and mod_wsgi is its new
reincarnation

FastCGI and SCGI: Faster alternativees to CGI; Run as independent
programs, and communicate with the web server through either a Unix
socket (located on the same host) or a TCP socket (remote  host)

Is this correct?

Thank you.




I'm sure there's no single correct answer to this.

Consider (python 2.6]:

emile@paj39:~$ mkdir web
emile@paj39:~$ cd web
emile@paj39:~/web$ cat > test.html
hello from test.html
emile@paj39:~/web$ python -m SimpleHTTPServer

Then browse to localhost:8000/test.html

Emile



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


Re: Strange behavior

2012-08-16 Thread Virgil Stokes

On 16-Aug-2012 19:40, Steven D'Aprano wrote:

On Thu, 16 Aug 2012 13:18:59 +0200, Virgil Stokes wrote:


On 15-Aug-2012 02:19, Steven D'Aprano wrote:

On Tue, 14 Aug 2012 21:40:10 +0200, Virgil Stokes wrote:


You might find the following useful:

def testFunc(startingList):
   xOnlyList = []; j = -1
   for xl in startingList:
   if (xl[0] == 'x'):

That's going to fail in the starting list contains an empty string. Use
xl.startswith('x') instead.

Yes, but this was by design (tacitly assumed that startingList was both
a list and non-empty).

As Peter already pointed out, I said it would fail if the list contains
an empty string, not if the list was empty.



   xOnlyList.append(xl)
   else:
   j += 1
   startingList[j] = xl

Very cunning, but I have to say that your algorithm fails the "is this
obviously correct without needing to study it?" test. Sometimes that is
unavoidable, but for something like this, there are simpler ways to
solve the same problem.

Sorry, but I do not sure what you mean here.

In a perfect world, you should be able to look at a piece of code, read
it once, and see whether or not it is correct. That is what I mean by
"obviously correct". For example, if I have a function that takes an
argument, doubles it, and prints the result:

def f1(x):
 print(2*x)


that is obviously correct. Whereas this is not:

def f2(x):
 y = (x + 5)**2 - (x + 4)**2
 sys.stdout.write(str(y - 9) + '\n')


because you have to study it to see whether or not it works correctly.

Not all programs are simple enough to be obviously correct. Sometimes you
have no choice but to write something which requires cleverness to get
the right result. But this is not one of those cases. You should almost
always prefer simple code over clever code, because the greatest expense
in programming (time, effort and money) is to make code correct.

Most code does not need to be fast. But all code needs to be correct.


[...]

This can meet the requirement that startingList is modified in place via
the call to this function (see the attached code).

Good grief! See, that's exactly the sort of thing I'm talking about.
Without *detailed* study of your attached code, how can I possibly know
what it does or whether it does it correctly?
Very strange question? Perhaps, you should work on understanding code that you 
have not written, or maybe you should learn more about Python, or   I really 
don't know how to help you with this question.


Your timing code calculates the mean using a recursive algorithm. Why
don't you calculate the mean the standard way: add the numbers and divide
by the total? What benefit do you gain from a more complicated algorithm
when a simple one will do the job just as well?
A lot of questions that suggest you have not made much of an effort to answer 
them yourself. Try a little numerical analysis/research before asking such 
questions (This is how you often respond to others on this list who would like 
help --- try apply your advice to other to yourself. I will give you a start:


* Knuth, D. E. (1998) /The Art of Computer Programming vol. 2: Seminumerical 
Algorithms/ /(3rd edition)/. Addison-Wesley, Boston.

[hint: study p. 232]

* Welford, B. P. (1962) Note on a method for calculating sums of squares and 
products. T/echnometrics/ *4*(3).

[hint: pp. 419-420]


You have spent a lot of effort creating a complicated, non-obvious piece
of timing code, with different random seeds for each run, and complicated
ways of calculating timing statistics... but unfortunately the most
important part of any timing test, the actually *timing*, is not done
correctly. Consequently, your code is not correct.
How do you know how much effort I used? Code "non-obvious" and "complicated" for 
you does not mean that this is also true for others. Could you please be more 
specific --- saying code is not correct without providing details is not very 
useful. I did say in an earlier email in reference to my code "if there are any 
errors in my attached code please inform me of them and I will try to correct 
them as soon as possible".


With an average time of a fraction of a second, none of those timing
results are trustworthy, because they are vulnerable to interference from
other processes, the operating system, and other random noise.
Please explain what you mean by the timing results not being trustworthy and how 
this vulnerability works --- in detail please.

  You spend
a lot of time processing the timing results, but it is Garbage In,
Garbage Out -- the results are not trustworthy, and if they are correct,
it is only by accident.
Fantastic --- a lot of criticism but little that can be helpful. What 
specifically is the "Garbage In"?


Later in your post, you run some tests, and are surprised by the result:


Why is algorithm-2A slower than algorithm-2?

It isn't slower. It is physically impossible, since 2A does *less* work
than 2.
Please provide

Re: [OT] Posting under ones full name

2012-08-16 Thread Grant Edwards
On 2012-08-16, Dan Sommers  wrote:
> On 2012-08-15 at 13:59:53 +,
> Grant Edwards  wrote:
>
>> On 2012-08-15, Chris Angelico  wrote:
>
>> > Perhaps you said "Please use `m4-style quotes rather than matching
>> > ASCII quotes' or `something else' to indicate omission instead".
>
> When I've got these antlers on I am dictating and when I take them off I
> am not dictating.

Well done!

-- 
Grant Edwards   grant.b.edwardsYow! Do you like "TENDER
  at   VITTLES"?
  gmail.com
-- 
http://mail.python.org/mailman/listinfo/python-list


remote read eval print loop

2012-08-16 Thread Eric Frederich
Hello,

I have a bunch of Python bindings for a 3rd party software running on the
server side.
I can add client side extensions that communicate over some http / xml type
requests.
So I can define functions that take a string and return a string.
I would like to get a simple read eval print loop working.

Without adding a bunch of syntax checking on the client side can I get the
behavior of the regular interpreter?
What I mean is things like going from >>> to ... after you start a block
(like if, while, for, etc).

Is this possible or can I not send over one line at a time and I'd have to
send over a complete block?

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


Re: [OT] Posting under ones full name

2012-08-16 Thread Alex Strickland

On 2012/08/16 07:01 AM, Dan Sommers wrote:


When I've got these antlers on I am dictating and when I take them off I
am not dictating.


Very good.

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


Re: [ANNC] pybotwar-0.8

2012-08-16 Thread Terry Reedy

On 8/16/2012 11:40 AM, Ramchandra Apte wrote:


Look you are the only person complaining about top-posting.


No he is not. Recheck all the the responses.


GMail uses top-posting by default.


It only works if everyone does it.


I can't help it if you feel irritated by it.


Your out-of-context comments are harder to understand. I mostly do not 
read them.



--
Terry Jan Reedy

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


Re: set and dict iteration

2012-08-16 Thread Paul Rubin
Dave Angel  writes:
> Everything else is implementation defined.  Why should an implementation
> be forced to have ANY extra data structure to detect a static bug in the
> caller's code?

For the same reason the interpreter checks for type errors at runtime
and raises TypeError, instead of letting the program go into the weeds.
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: [ANNC] pybotwar-0.8

2012-08-16 Thread Walter Hurry
On Thu, 16 Aug 2012 17:20:29 -0400, Terry Reedy wrote:

> On 8/16/2012 11:40 AM, Ramchandra Apte wrote:
> 
>> Look you are the only person complaining about top-posting.
> 
> No he is not. Recheck all the the responses.
> 
>> GMail uses top-posting by default.
> 
> It only works if everyone does it.
> 
>> I can't help it if you feel irritated by it.
> 
> Your out-of-context comments are harder to understand. I mostly do not
> read them.

It's strange, but I don't even *see* his contributions (I am using a 
regular newsreader - on comp.lang.python - and I don't have him in the 
bozo bin). It doesn't sound as though I'm missing much.

But I'm just curious. Any idea why that would be the case?

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


How do I display unicode value stored in a string variable using ord()

2012-08-16 Thread Charles Jensen
Everyone knows that the python command

 ord(u'…')

will output the number 8230 which is the unicode character for the horizontal 
ellipsis.

How would I use ord() to find the unicode value of a string stored in a 
variable?  

So the following 2 lines of code will give me the ascii value of the variable 
a.  How do I specify ord to give me the unicode value of a?

 a = '…'
 ord(a)
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: How do I display unicode value stored in a string variable using ord()

2012-08-16 Thread Chris Angelico
On Fri, Aug 17, 2012 at 8:09 AM, Charles Jensen
 wrote:
> How would I use ord() to find the unicode value of a string stored in a 
> variable?
>
> So the following 2 lines of code will give me the ascii value of the variable 
> a.  How do I specify ord to give me the unicode value of a?
>
>  a = '…'
>  ord(a)

I presume you're talking about Python 2, because in Python 3 your
string variable is a Unicode string and will behave as you describe
above.

You'll need to look into what the encoding is, and figure it out from there.

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


Top-posting &c. (was Re: [ANNC] pybotwar-0.8)

2012-08-16 Thread Chris Angelico
On Fri, Aug 17, 2012 at 1:40 AM, Ramchandra Apte  wrote:
> On 16 August 2012 21:00, Mark Lawrence  wrote:
>> and "bottom" reads better than "top"
>>
> Look you are the only person complaining about top-posting.
> GMail uses top-posting by default.
> I can't help it if you feel irritated by it.

I post using gmail, and I just delete two blank lines at the top and
go down the bottom to type. But on the way down, I also trim quoted
text, so people don't have to download and read the entire thread for
every new post. It's not difficult, you should give it a try some
time!

And FWIW, I add my voice to those who prefer to read replies
underneath the original text. Even if Mark were the only person vocal
enough to complain, you can still rest assured that there are many
more who agree. You've now heard from quite a few regular posters;
there are probably several *hundred* lurkers who feel the same way,
but do not post (possibly because they cannot). Also, these mails get
archived all over the internet, so a generation not yet born can read
and be either enlightened or irritated, as the case may be.

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


Re: Top-posting &c. (was Re: [ANNC] pybotwar-0.8)

2012-08-16 Thread Madison May

> And FWIW, I add my voice to those who prefer to read replies
> 
> underneath the original text. Even if Mark were the only person vocal
> 
> enough to complain, you can still rest assured that there are many
> 
> more who agree. You've now heard from quite a few regular posters;
> 
> there are probably several *hundred* lurkers who feel the same way,
> 
> but do not post (possibly because they cannot). Also, these mails get
> 
> archived all over the internet, so a generation not yet born can read
> 
> and be either enlightened or irritated, as the case may be.
> 
> 
> 
> ChrisA


As a lurker, I agree completely with Chris's sentiments. 
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: remote read eval print loop

2012-08-16 Thread Chris Angelico
On Fri, Aug 17, 2012 at 6:54 AM, Eric Frederich
 wrote:
> Hello,
>
> I have a bunch of Python bindings for a 3rd party software running on the
> server side.
> I can add client side extensions that communicate over some http / xml type
> requests.
> So I can define functions that take a string and return a string.
> I would like to get a simple read eval print loop working.

Let's stop *right there*. You're looking for something that will run
on your server, take strings of text from a remote computer, and eval
them.

Please, please, please, on behalf of every systems administrator in
the world I beg you, please do not do this.

Instead, define your own high-level protocol and have your server
respond to that. One excellent way to keep things tidy is to use a
'command, parameters, newline' model: each line of text is one
instruction, consisting of a command word, then optionally parameters
after a space, then a newline. It's easy to debug, easy to read in
your code, and makes sense to anyone who's used a command-line
interface.

Six months from now, when your server still hasn't been compromised,
you'll appreciate the extra design effort :)

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


Re: How do I display unicode value stored in a string variable using ord()

2012-08-16 Thread Dave Angel
On 08/16/2012 06:09 PM, Charles Jensen wrote:
> Everyone knows that the python command
>
>  ord(u'…')
>
> will output the number 8230 which is the unicode character for the horizontal 
> ellipsis.
>
> How would I use ord() to find the unicode value of a string stored in a 
> variable?  
>
> So the following 2 lines of code will give me the ascii value of the variable 
> a.  How do I specify ord to give me the unicode value of a?
>
>  a = '…'
>  ord(a)

You omitted the print statement.  You also didn't specify what version
of Python you're using;  I'll assume Python 2.x because in Python 3.x,
the u"xx" notation would have been a syntax error.

To get the ord of a unicode variable, you do it the same as a unicode
literal:

   a = u"j" #note: for this to work reliably, you probably
need the correct Unicode declaration in line 2 of the file
   print ord(a)

But if you have a byte string containing some binary bits, and you want
to get a unicode character value out of it, you'll need to explicitly
convert it to unicode.

First, decide what method the byte string was encoded.  If you specify
the wrong encoding, you'll likely to get an exception, or maybe just a
nonsense answer.

   a = "\xc1\xc1"#I just made this value up;  it's not
valid utf8
   b = a.decode("utf-8")
   print ord(b)



-- 

DaveA

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


Re: set and dict iteration

2012-08-16 Thread Ian Kelly
On Thu, Aug 16, 2012 at 12:00 PM, Aaron Brady  wrote:
> The inconsistency is, if we remove an element from a set and add another 
> during iteration, the new element might appear later in the iteration, and 
> might not, depending on the hash code; therefore comparing the size of the 
> set between iterations isn't adequate.  Example:

It can be more than just the new element.  For example, here the
entire set is repeated (Python 3.2):

>>> s = set(range(8, 13))
>>> it = iter(s)
>>> from itertools import islice
>>> list(islice(it, 5))  # avoid exhausting the iterator
[8, 9, 10, 11, 12]
>>> s.add(13)
>>> s.remove(13)
>>> list(it)
[8, 9, 10, 11, 12]

This occurs because the addition of the sixth item triggers a resize
of the underlying hash table, and the existing items, which were
originally in slots 0-4, are now in slots 8-12.
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: set and dict iteration

2012-08-16 Thread Ian Kelly
On Thu, Aug 16, 2012 at 4:55 PM, Ian Kelly  wrote:
> On Thu, Aug 16, 2012 at 12:00 PM, Aaron Brady  wrote:
>> The inconsistency is, if we remove an element from a set and add another 
>> during iteration, the new element might appear later in the iteration, and 
>> might not, depending on the hash code; therefore comparing the size of the 
>> set between iterations isn't adequate.  Example:
>
> It can be more than just the new element.  For example, here the
> entire set is repeated (Python 3.2):
>
 s = set(range(8, 13))
 it = iter(s)
 from itertools import islice
 list(islice(it, 5))  # avoid exhausting the iterator
> [8, 9, 10, 11, 12]
 s.add(13)
 s.remove(13)
 list(it)
> [8, 9, 10, 11, 12]
>
> This occurs because the addition of the sixth item triggers a resize
> of the underlying hash table, and the existing items, which were
> originally in slots 0-4, are now in slots 8-12.

Another curious example:

>>> s = set(range(8, 48, 8))
>>> s
{8, 16, 40, 24, 32}
>>> it = iter(s)
>>> from itertools import islice
>>> list(islice(it, 4))
[8, 16, 40, 24]
>>> s.add(48)
>>> s.remove(48)
>>> list(it)
[8, 16, 40, 24]

Hey, what happened to 32?
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: set and dict iteration

2012-08-16 Thread Dave Angel
On 08/16/2012 05:26 PM, Paul Rubin wrote:
> Dave Angel  writes:
>> Everything else is implementation defined.  Why should an implementation
>> be forced to have ANY extra data structure to detect a static bug in the
>> caller's code?
> For the same reason the interpreter checks for type errors at runtime
> and raises TypeError, instead of letting the program go into the weeds.

There's an enormous difference between type errors, which affect the low
level dispatch, and checking for whether a dict has changed and may have
invalidated the iterator.  If we were really going to keep track of what
iterators are tracking a given dict or set, why stop there?  Why not
check if another process has changed a file we're iterating through?  Or ...


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


Re: set and dict iteration

2012-08-16 Thread Ian Kelly
On Thu, Aug 16, 2012 at 5:11 PM, Dave Angel  wrote:
> There's an enormous difference between type errors, which affect the low
> level dispatch, and checking for whether a dict has changed and may have
> invalidated the iterator.  If we were really going to keep track of what
> iterators are tracking a given dict or set, why stop there?  Why not
> check if another process has changed a file we're iterating through?  Or ...

How does this affect low-level dispatch (Python 2.7)?

>>> class Foo(object):
... def bar(self):
... return self
...
>>> Foo().bar()
<__main__.Foo object at 0x00CBEAB0>
>>> Foo.bar(Foo())
<__main__.Foo object at 0x00CC9390>
>>> Foo.bar(object())
Traceback (most recent call last):
  File "", line 1, in 
TypeError: unbound method bar() must be called with Foo instance as
first argument (got object instance instead)

There is no low-level need for this TypeError -- it's purely a case of
not letting the developer shoot himself in the foot.  Although to be
honest the interpreter doesn't give quite enough rope (to mix
metaphors) in this case, and I'm glad for the sake of duck typing that
they removed this particular error in Python 3.

With regard to key insertion and deletion while iterating over a dict
or set, though, there is just no good reason to be doing that
(especially as the result is very implementation-specific), and I
wouldn't mind a more complete low-level check against it as long as
it's not too expensive (which is not clearly the case with the current
suggestion at all).
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: How do I display unicode value stored in a string variable using ord()

2012-08-16 Thread Terry Reedy

a = '…'
print(ord(a))
>>>
8230
Most things with unicode are easier in 3.x, and some are even better in 
3.3. The current beta is good enough for most informal work. 3.3.0 will 
be out in a month.


--
Terry Jan Reedy


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


Re: set and dict iteration

2012-08-16 Thread Paul Rubin
Ian Kelly  writes:
> With regard to key insertion and deletion while iterating over a dict
> or set, though, there is just no good reason to be doing that
> (especially as the result is very implementation-specific), and I
> wouldn't mind a more complete low-level check against it as long as
> it's not too expensive (which is not clearly the case with the current
> suggestion at all).

One possible approach is to freeze the dictionary against modification
while any iterator is open on it.  You could keep a count of active
iterators in the dict structure, adjusting it whenever an iterator is
created or closed/destroyed.
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: python+libxml2+scrapy AttributeError: 'module' object has no attribute 'HTML_PARSE_RECOVER'

2012-08-16 Thread personificator
I believe ftp://xmlsoft.org/libxml2/libxml2-2.8.0.tar.gz was what your looking 
for. Submit a ticket for the docs to get updated if your feeling generous.

On Wednesday, August 15, 2012 7:49:04 AM UTC-5, Dmitry Arsentiev wrote:
> Hello.
> 
> 
> 
> Has anybody already meet the problem like this? -
> 
> AttributeError: 'module' object has no attribute 'HTML_PARSE_RECOVER'
> 
> 
> 
> When I run scrapy, I get
> 
> 
> 
>   File "/usr/local/lib/python2.7/site-packages/scrapy/selector/factories.py",
> 
> line 14, in 
> 
> libxml2.HTML_PARSE_NOERROR + \
> 
> AttributeError: 'module' object has no attribute 'HTML_PARSE_RECOVER'
> 
> 
> 
> 
> 
> When I run
> 
>  python -c 'import libxml2; libxml2.HTML_PARSE_RECOVER'
> 
> 
> 
> I get
> 
> Traceback (most recent call last):
> 
>   File "", line 1, in 
> 
> AttributeError: 'module' object has no attribute 'HTML_PARSE_RECOVER'
> 
> 
> 
> How can I cure it?
> 
> 
> 
> Python 2.7
> 
> libxml2-python 2.6.9
> 
> 2.6.11-gentoo-r6
> 
> 
> 
> 
> 
> I will be grateful for any help.
> 
> 
> 
> DETAILS:
> 
> 
> 
> scrapy crawl lgz -o items.json -t json
> 
> Traceback (most recent call last):
> 
>   File "/usr/local/bin/scrapy", line 4, in 
> 
> execute()
> 
>   File "/usr/local/lib/python2.7/site-packages/scrapy/cmdline.py", line 112, 
> in execute
> 
> cmds = _get_commands_dict(inproject)
> 
>   File "/usr/local/lib/python2.7/site-packages/scrapy/cmdline.py", line 37, 
> in _get_commands_dict
> 
> cmds = _get_commands_from_module('scrapy.commands', inproject)
> 
>   File "/usr/local/lib/python2.7/site-packages/scrapy/cmdline.py", line 30, 
> in _get_commands_from_module
> 
> for cmd in _iter_command_classes(module):
> 
>   File "/usr/local/lib/python2.7/site-packages/scrapy/cmdline.py", line 21, 
> in _iter_command_classes
> 
> for module in walk_modules(module_name):
> 
>   File "/usr/local/lib/python2.7/site-packages/scrapy/utils/misc.py", line 
> 65, in walk_modules
> 
> submod = __import__(fullpath, {}, {}, [''])
> 
>   File "/usr/local/lib/python2.7/site-packages/scrapy/commands/shell.py", 
> line 8, in 
> 
> from scrapy.shell import Shell
> 
>   File "/usr/local/lib/python2.7/site-packages/scrapy/shell.py", line 14, in 
> 
> 
> from scrapy.selector import XPathSelector, XmlXPathSelector, 
> HtmlXPathSelector
> 
>   File "/usr/local/lib/python2.7/site-packages/scrapy/selector/__init__.py", 
> line 30, in 
> 
> from scrapy.selector.libxml2sel import *
> 
>   File 
> "/usr/local/lib/python2.7/site-packages/scrapy/selector/libxml2sel.py", line 
> 12, in 
> 
> from .factories import xmlDoc_from_html, xmlDoc_from_xml
> 
>   File "/usr/local/lib/python2.7/site-packages/scrapy/selector/factories.py", 
> line 14, in 
> 
> libxml2.HTML_PARSE_NOERROR + \
> 
> AttributeError: 'module' object has no attribute 'HTML_PARSE_RECOVER'

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


Re: set and dict iteration

2012-08-16 Thread Steven D'Aprano
On Thu, 16 Aug 2012 19:11:19 -0400, Dave Angel wrote:

> On 08/16/2012 05:26 PM, Paul Rubin wrote:
>> Dave Angel  writes:
>>> Everything else is implementation defined.  Why should an
>>> implementation be forced to have ANY extra data structure to detect a
>>> static bug in the caller's code?
>> For the same reason the interpreter checks for type errors at runtime
>> and raises TypeError, instead of letting the program go into the weeds.
> 
> There's an enormous difference between type errors, which affect the low
> level dispatch, and checking for whether a dict has changed and may have
> invalidated the iterator.  If we were really going to keep track of what
> iterators are tracking a given dict or set, why stop there?  Why not
> check if another process has changed a file we're iterating through?  Or
> ...

Which is why Python doesn't do it -- because it is (claimed to be) 
excessively expensive for the benefit that you would get.

Not because it is a matter of principle that data integrity is 
unimportant. Data integrity *is* important, but in the opinion of the 
people who wrote these particular data structures, the effort required to 
guarantee correct iteration in the face of mutation is too expensive for 
the benefit.

Are they right? I don't know. I know that the list sort method goes to a 
lot of trouble to prevent code from modifying lists while they are being 
sorted. During the sort, the list temporarily appears to be empty to 
anything which attempts to access it. So at least sometimes, the Python 
developers spend effort to ensure data integrity.

Luckily, Python is open source. If anyone thinks that sets and dicts 
should include more code protecting against mutation-during-iteration, 
they are more than welcome to come up with a patch. Don't forget unit and 
regression tests, and also a set of timing results which show that the 
slow-down isn't excessive.


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


Re: remote read eval print loop

2012-08-16 Thread Steven D'Aprano
On Fri, 17 Aug 2012 08:43:50 +1000, Chris Angelico wrote:

> On Fri, Aug 17, 2012 at 6:54 AM, Eric Frederich
>  wrote:
>> Hello,
>>
>> I have a bunch of Python bindings for a 3rd party software running on
>> the server side.
>> I can add client side extensions that communicate over some http / xml
>> type requests.
>> So I can define functions that take a string and return a string. I
>> would like to get a simple read eval print loop working.
> 
> Let's stop *right there*. You're looking for something that will run on
> your server, take strings of text from a remote computer, and eval them.
> 
> Please, please, please, on behalf of every systems administrator in the
> world I beg you, please do not do this.
> 
> Instead, define your own high-level protocol 

Stop right there!

There is already awesome protocols for running Python code remotely over 
a network. Please do not re-invent the wheel without good reason.

See pyro, twisted, rpyc, rpclib, jpc, and probably many others.




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


Re: set and dict iteration

2012-08-16 Thread Paul Rubin
Steven D'Aprano  writes:
> Luckily, Python is open source. If anyone thinks that sets and dicts 
> should include more code protecting against mutation-during-iteration, 
> they are more than welcome to come up with a patch. Don't forget unit and 
> regression tests, and also a set of timing results which show that the 
> slow-down isn't excessive.

It could be a debugging option, in which case even a fairly significant
slowdown is acceptable.
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: How do I display unicode value stored in a string variable using ord()

2012-08-16 Thread Alister
On Thu, 16 Aug 2012 15:09:47 -0700, Charles Jensen wrote:

> Everyone knows that the python command
> 
>  ord(u'…')
> 
> will output the number 8230 which is the unicode character for the
> horizontal ellipsis.
> 
> How would I use ord() to find the unicode value of a string stored in a
> variable?
> 
> So the following 2 lines of code will give me the ascii value of the
> variable a.  How do I specify ord to give me the unicode value of a?
> 
>  a = '…' ord(a)





the same way you did in your original example by defining the string ass 
unicode
a=u'...' ord(a)
-- 
Keep on keepin' on.
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: remote read eval print loop

2012-08-16 Thread Alister
On Fri, 17 Aug 2012 02:27:42 +, Steven D'Aprano wrote:

> On Fri, 17 Aug 2012 08:43:50 +1000, Chris Angelico wrote:
> 
>> On Fri, Aug 17, 2012 at 6:54 AM, Eric Frederich
>>  wrote:
>>> Hello,
>>>
>>> I have a bunch of Python bindings for a 3rd party software running on
>>> the server side.
>>> I can add client side extensions that communicate over some http / xml
>>> type requests.
>>> So I can define functions that take a string and return a string. I
>>> would like to get a simple read eval print loop working.
>> 
>> Let's stop *right there*. You're looking for something that will run on
>> your server, take strings of text from a remote computer, and eval
>> them.
>> 
>> Please, please, please, on behalf of every systems administrator in the
>> world I beg you, please do not do this.
>> 
>> Instead, define your own high-level protocol
> 
> Stop right there!
> 
> There is already awesome protocols for running Python code remotely over
> a network. Please do not re-invent the wheel without good reason.
> 
> See pyro, twisted, rpyc, rpclib, jpc, and probably many others.

I think you missed the main point of the previous post which was.

Do NOT blindly eval data sent from a remote computer as is cannot be 
trusted. This of course is assuming they are not on a secure connection, 
but even then it is good practice as not all attacks come from outside.

although i have to agree with you about not re-inventing wheels, they 
invariably come out square :-)



-- 
 RMS for President???
 ...or ESR, he wants a new job ;)
-- 
http://mail.python.org/mailman/listinfo/python-list