Re: communicate with external process via pty

2012-10-10 Thread Peter Otten
Tim Arnold wrote:

> I have an external process, 'tralics' that emits mathml when you feed it
> latex equations. I want to get that mathml into a string.
> 
> The problem for me is that tralics wants to talk to a tty and I've never
> done that before; it basically starts its own subshell.
> 
> I have the following code which works for simple things. I'm not sure
> this is the best way though: basically I got this from google...
> 
> import os,sys
> import subprocess
> import shlex
> import pty
> cmd =  'tralics --interactivemath'
> 
> (master, slave) = pty.openpty()
> p = subprocess.Popen(shlex.split(cmd),close_fds=True,
>   stdin=slave,stdout=slave,stderr=slave)
> 
> os.read(master,1024)# start the process
> os.write(master,'$\sqrt{x}$\n') # feed it an equation
> mathml.append(os.read(master,1024)) # get the mathml in a string
> 
> os.write(master,'$\sqrt{x}$\n') # feed more equations
> mathml.append(os.read(master,1024)) # get the next string
> 
> 
> Any suggestions for improvement?

Do you know about pexpect?

http://pypi.python.org/pypi/pexpect

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


Re: Private methods

2012-10-10 Thread D.M. Procida
Demian Brecht  wrote:

> On 12-10-09 04:51 PM, Steven D'Aprano wrote:
> > Really? I tend to view name mangling as a waste of time, and complex
> > inheritance structures as something to avoid.
> 
> Yep, I've been coming around to this as of late.

I have a lot of inheritance. I don't know whether you'd call it complex,
but I use a lot of mixins to build classes.

It certainly makes it quick to build a class with the attributes I need,
but it does make tracing logic sometimes a pain in the neck.

I don't know what the alternative is though.

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


Re: lxml 3.0 final released - efficient XML and HTML processing with Python

2012-10-10 Thread D.M. Procida
Stefan Behnel  wrote:

> it's been a while since the last stable release series appeared, so I'm
> proud to announce the final release of lxml 3.0.

Great. We use it in
.

Thanks.

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


Delete duplicate rows in textfile - except it contains a "{" or "}"

2012-10-10 Thread Joon Ki Choi

Hello Pythonistas,

i have a very large textfile with contents like:

@INBOOK{Ackermann1999-b,
  author = {Ackermann, K.-F. and Ackermann, K.-F. and Ackermann, K.-F. and 
Ackermann,
K.-F. and Ackermann, K.-F. and Ackermann, K.-F. and Ackermann, K.-F.
and Ackermann, K.-F. and Ackermann, K.-F. and Ackermann, K.-F. and
Ackermann, K.-F. and Ackermann, K.-F. and Ackermann, K.-F. and 
Ackermann,
K.-F. and Ackermann, K.-F. and Ackermann, K.-F. and Ackermann, K.-F.
and Ackermann, K.-F. and Ackermann, K.-F. and Ackermann, K.-F. and
Ackermann, K.-F. and Ackermann, K.-F. and Ackermann, K.-F. and 
Ackermann,
K.-F. and Ackermann, K.-F. and Ackermann, K.-F. and Ackermann, K.-F.
and Ackermann, K.-F. and Ackermann, K.-F. and Ackermann, K.-F. and
Ackermann, K.-F. and Ackermann, K.-F. and Ackermann, K.-F. and 
Ackermann},
  year = {1980},
  timestamp = {1995-12-02}
}   

And i want to delete the duplicate rows except these rows containing the 
brackets { or }. 
The result should look like:

@INBOOK{Ackermann1999-b,
  author = {Ackermann, K.-F. and Ackermann, K.-F. and Ackermann, K.-F. and 
Ackermann,
Ackermann, K.-F. and Ackermann, K.-F. and Ackermann, K.-F. and 
Ackermann},
  year = {1980},
  timestamp = {1995-12-02}
}

I come across with this Python-Skript:

lines_seen = set() # holds lines already seen
outfile = open("literatur_clean.txt", "w")
for line in open("literatur_dupl.txt", "r"):
if line not in lines_seen: # not a duplicate
outfile.write(line)
lines_seen.add(line)
outfile.close()

But it deletes also the lines with a closing bracket } and the lines with the 
same authordata.
Therefor i need the condition of the brackets.

Could someone point me out to adding this condition?

Thanks in advance,
Joon




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


Re: Delete duplicate rows in textfile - except it contains a "{" or "}"

2012-10-10 Thread Mark Lawrence

On 10/10/2012 09:51, Joon Ki Choi wrote:


Hello Pythonistas,

i have a very large textfile with contents like:

@INBOOK{Ackermann1999-b,
   author = {Ackermann, K.-F. and Ackermann, K.-F. and Ackermann, K.-F. and 
Ackermann,
K.-F. and Ackermann, K.-F. and Ackermann, K.-F. and Ackermann, K.-F.
and Ackermann, K.-F. and Ackermann, K.-F. and Ackermann, K.-F. and
Ackermann, K.-F. and Ackermann, K.-F. and Ackermann, K.-F. and 
Ackermann,
K.-F. and Ackermann, K.-F. and Ackermann, K.-F. and Ackermann, K.-F.
and Ackermann, K.-F. and Ackermann, K.-F. and Ackermann, K.-F. and
Ackermann, K.-F. and Ackermann, K.-F. and Ackermann, K.-F. and 
Ackermann,
K.-F. and Ackermann, K.-F. and Ackermann, K.-F. and Ackermann, K.-F.
and Ackermann, K.-F. and Ackermann, K.-F. and Ackermann, K.-F. and
Ackermann, K.-F. and Ackermann, K.-F. and Ackermann, K.-F. and 
Ackermann},
   year = {1980},
   timestamp = {1995-12-02}
}   

And i want to delete the duplicate rows except these rows containing the 
brackets { or }.
The result should look like:

@INBOOK{Ackermann1999-b,
   author = {Ackermann, K.-F. and Ackermann, K.-F. and Ackermann, K.-F. and 
Ackermann,
Ackermann, K.-F. and Ackermann, K.-F. and Ackermann, K.-F. and 
Ackermann},
   year = {1980},
   timestamp = {1995-12-02}
}

I come across with this Python-Skript:

lines_seen = set() # holds lines already seen
outfile = open("literatur_clean.txt", "w")


Slight aside, you could use this so there's no need to explicitly close 
the file.


with open("literatur_dupl.txt", "r") as infile


for line in infile:
 if line not in lines_seen: # not a duplicate
 outfile.write(line)
 lines_seen.add(line)


Something like:-

if "{" in line or "}" in line or line not in lines_seen:


outfile.close()

But it deletes also the lines with a closing bracket } and the lines with the 
same authordata.
Therefor i need the condition of the brackets.

Could someone point me out to adding this condition?

Thanks in advance,
Joon



--
Cheers.

Mark Lawrence.

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


Re: Generating C++ code

2012-10-10 Thread Jean-Michel Pichavant
- Original Message -
> On Tue, 09 Oct 2012 20:55:48 +0100
> Andrea Crotti  wrote:
> 
> > On 10/09/2012 05:00 PM, Jean-Michel Pichavant wrote:
> > > Greetings,
> > >
> > > I'm trying to generate C++ code from an XML file. I'd like to use
> > > a template engine, which imo produce something readable and
> > > maintainable.
> > > My google search about this subject has been quite unsuccessful,
> > > I've been redirected to template engine specific to html mostly.
> > >
> > > Does anybody knows a python template engine for generating C++
> > > code ?
> > >
> > > Here's my flow:
> > >
> > > XML file -> nice python app -> C++ code
> > >
> > >  From what I know I could use Cheetah, a generic template engine.
> > >  I never used it though, I'm not sure this is what I need.
> > > I'm familiar with jinja2 but I'm not sure I could use it to
> > > generate C++ code, did anybody try ? (maybe that's a silly
> > > question)
> > >
> > > Any advice would be appreciated.
> > >
> > > JM
> > 
> > I think you can use anything to generate C++ code, but is it a good
> > idea?
> > Are you going to produce this code only one time and then maintain
> > it
> > manually?
> > 
> > And are you sure that the design that you would get from the XML
> > file
> > actually makes sense when
> > translated in C++?
> > --
> > http://mail.python.org/mailman/listinfo/python-list
> 
> You can build nice python app with Cython/Pyrex too if you got
> sufficient knowledge in C/C++ programming
> to extend the app in C. :-)
> 
> Otherwise that seem like a little counter-productive to produce a
> python template only for generating C stubs when python
> is good for many things without requiring advanced C++ skills like
> memory management, etc.
>  
> HTH,
> E
> --
> Etienne Robillard
> Green Tea Hackers Club
> Fine Software Carpentry For The Rest Of Us!
> http://gthc.org/
> [email protected]

Well, the C++ code will end up running on a MIPS on a SOC, unfortunately, 
python is not an option here. 
The xml to C++ makes a lot of sense, because only a small part of the code is 
generated that way (everything related to log & fatal events). Everything else 
is written directly in C++.

To answer Andrea's question, the files are regenerated for every compilation 
(well, unless the xml didn't change, but the xml is highly subject to changes, 
that's actually its purpose)

Currently we already have a python script that translate this xml file to C++, 
but it's done in a way that is difficult to maintain. Basically, when parsing 
the xml file, it writes the generated C++ code. Something like:
if 'blabla' in xml:
  h_file.write("#define blabla 55", append="top")
  c_file.write("someglobal = blabla", append="bottom")

This is working, but the python code is quite difficult to maintain, there's a 
lot of escaping going on, it's almost impossible to see the structure of the c 
files unless generating one and hopping it's successful. It's also quite 
difficult to insert code exactly where you want, because you do not know the 
order in which the xml trees are defined then parsed.

I was just wondering if a template engine would help. Maybe not.

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


Re: Delete duplicate rows in textfile - except it contains a "{" or "}"

2012-10-10 Thread Peter Otten
Joon Ki Choi wrote:

> 
> Hello Pythonistas,
> 
> i have a very large textfile with contents like:
> 
> @INBOOK{Ackermann1999-b,
>   author = {Ackermann, K.-F. and Ackermann, K.-F. and Ackermann, K.-F. and
>   Ackermann,
> K.-F. and Ackermann, K.-F. and Ackermann, K.-F. and Ackermann, K.-F.
> and Ackermann, K.-F. and Ackermann, K.-F. and Ackermann, K.-F. and
> Ackermann, K.-F. and Ackermann, K.-F. and Ackermann, K.-F. and Ackermann,
> K.-F. and Ackermann, K.-F. and Ackermann, K.-F. and Ackermann, K.-F.
> and Ackermann, K.-F. and Ackermann, K.-F. and Ackermann, K.-F. and
> Ackermann, K.-F. and Ackermann, K.-F. and Ackermann, K.-F. and Ackermann,
> K.-F. and Ackermann, K.-F. and Ackermann, K.-F. and Ackermann, K.-F.
> and Ackermann, K.-F. and Ackermann, K.-F. and Ackermann, K.-F. and
> Ackermann, K.-F. and Ackermann, K.-F. and Ackermann, K.-F. and Ackermann},
>   year = {1980},
>   timestamp = {1995-12-02}
> }
> 
> And i want to delete the duplicate rows except these rows containing the
> brackets { or }. The result should look like:
> 
> @INBOOK{Ackermann1999-b,
>   author = {Ackermann, K.-F. and Ackermann, K.-F. and Ackermann, K.-F. and
>   Ackermann,
> Ackermann, K.-F. and Ackermann, K.-F. and Ackermann, K.-F. and Ackermann},
>   year = {1980},
>   timestamp = {1995-12-02}
> }
> 
> I come across with this Python-Skript:
> 
> lines_seen = set() # holds lines already seen
> outfile = open("literatur_clean.txt", "w")
> for line in open("literatur_dupl.txt", "r"):
> if line not in lines_seen: # not a duplicate
> outfile.write(line)
> lines_seen.add(line)
> outfile.close()
> 
> But it deletes also the lines with a closing bracket } and the lines with
> the same authordata. Therefor i need the condition of the brackets.
> 
> Could someone point me out to adding this condition?
> 
> Thanks in advance,
> Joon

Not what you asked for, but here is something that is quick-and-dirty, too, 
but tries a bit harder:

import re

def unique(match):
names = match.group()[1:-1].split(",")
parts = set(" ".join(author.split()) for author in names)
return "{%s}" % ", ".join(parts)

if __name__ == "__main__":
with open("literatur_dupl.txt") as f:
data = f.read()
data = re.compile("{[^{}]*}", re.DOTALL).sub(unique, data)

with open("literatur_clean.txt", "w") as f:
f.write(data)

I'm assuming that "very large" means that the file contents still 
comfortably fit into your computer's memory...

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


Re: Generating C++ code

2012-10-10 Thread Etienne Robillard
On Wed, 10 Oct 2012 11:59:50 +0200 (CEST)
Jean-Michel Pichavant  wrote:

> Well, the C++ code will end up running on a MIPS on a SOC, unfortunately, 
> python is not an option here. 
> The xml to C++ makes a lot of sense, because only a small part of the code is 
> generated that way (everything related to log & fatal events). Everything 
> else is written directly in C++.

sorry but i don't get what you mean with a "MIPS on a SOC". Is not Python well 
supported on MIPS ?
 
> Currently we already have a python script that translate this xml file to 
> C++, but it's done in a way that is difficult to maintain. Basically, when 
> parsing the xml file, it writes the generated C++ code. Something like:
> if 'blabla' in xml:
>   h_file.write("#define blabla 55", append="top")
>   c_file.write("someglobal = blabla", append="bottom")

Don't do that! This is a good example of ambigous coding (to say the least..) 
and you'll make C++ programmers eyes to bleed
at this. 

> This is working, but the python code is quite difficult to maintain, there's 
> a lot of escaping going on, it's almost impossible to see the structure of 
> the c files unless generating one and hopping it's successful. It's also 
> quite difficult to insert code exactly where you want, because you do not 
> know the order in which the xml trees are defined then parsed.

Its maybe working but why then are you stuck asking for help ? I suggest you 
either write plain C++ code or learn to 
use Python more efficiently...


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

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


Re: Generating C++ code

2012-10-10 Thread Stefan Behnel
Jean-Michel Pichavant, 10.10.2012 11:59:
> Well, the C++ code will end up running on a MIPS on a SOC,
> unfortunately, python is not an option here. The xml to C++ makes a lot
> of sense, because only a small part of the code is generated that way
> (everything related to log & fatal events). Everything else is written
> directly in C++.
> 
> To answer Andrea's question, the files are regenerated for every
> compilation (well, unless the xml didn't change, but the xml is highly
> subject to changes, that's actually its purpose)
> 
> Currently we already have a python script that translate this xml file
> to C++, but it's done in a way that is difficult to maintain. Basically,
> when parsing the xml file, it writes the generated C++ code. Something
> like:
> 
> if 'blabla' in xml:
> h_file.write("#define blabla 55", append="top")
> c_file.write("someglobal = blabla", append="bottom")
> 
> This is working, but the python code is quite difficult to maintain,
> there's a lot of escaping going on, it's almost impossible to see the
> structure of the c files unless generating one and hopping it's
> successful. It's also quite difficult to insert code exactly where you
> want, because you do not know the order in which the xml trees are
> defined then parsed.
> 
> I was just wondering if a template engine would help. Maybe not.

Depends. Template engines are great for injecting small data snippets into
large static code blocks. They are less good for finely structured code
with conditional insertions and varying code order all over the place.

In Cython, we use a combination of both: a template engine for large code
blocks with small adaptations and line-by-line generated C code for highly
varying code. Works nicely.

As for a template engine, we use Tempita for the more complex stuff
(because the implementation is small and can be embedded) and
string.Template for the simple stuff. If you need something more advanced
straight away, I'd say go for Cheetah.

In case you decide not to use a template engine at all, given that your
input format is XML, consider using lxml to build up your code tree for you
by using custom element classes. Then, generate the code recursively top-down.

http://lxml.de/element_classes.html

Might or might not be a suitable approach, depending on the complexity of
your mapping from XML to code.

Stefan

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


Re: Generating C++ code

2012-10-10 Thread andrea crotti
2012/10/10 Jean-Michel Pichavant :
> Well, the C++ code will end up running on a MIPS on a SOC, unfortunately, 
> python is not an option here.
> The xml to C++ makes a lot of sense, because only a small part of the code is 
> generated that way (everything related to log & fatal events). Everything 
> else is written directly in C++.
>
> To answer Andrea's question, the files are regenerated for every compilation 
> (well, unless the xml didn't change, but the xml is highly subject to 
> changes, that's actually its purpose)
>
> Currently we already have a python script that translate this xml file to 
> C++, but it's done in a way that is difficult to maintain. Basically, when 
> parsing the xml file, it writes the generated C++ code. Something like:
> if 'blabla' in xml:
>   h_file.write("#define blabla 55", append="top")
>   c_file.write("someglobal = blabla", append="bottom")
>
> This is working, but the python code is quite difficult to maintain, there's 
> a lot of escaping going on, it's almost impossible to see the structure of 
> the c files unless generating one and hopping it's successful. It's also 
> quite difficult to insert code exactly where you want, because you do not 
> know the order in which the xml trees are defined then parsed.
>
> I was just wondering if a template engine would help. Maybe not.
>
> JM
> --
> http://mail.python.org/mailman/listinfo/python-list


I think it depends on what you're writing from the XML, are you
generating just constants (like the #define) or also new classes for
example?

If it's just constants why don't you do a generation from XML -> ini
or something similar and then parse it in the C++ properly, then it
would be very easy to do?

You could also parse the XML in the first place but probably that's
harder given your requirements, but I don't think that an ini file
would be a problem, or would it?
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Generating C++ code

2012-10-10 Thread Ulrich Eckhardt

Am 09.10.2012 18:00, schrieb Jean-Michel Pichavant:

I'm trying to generate C++ code from an XML file. I'd like to use a
template engine, which imo produce something readable and
maintainable.
[...]
Here's my flow:

XML file -> nice python app -> C++ code


There is one question that you should answer (or maybe decide?) first: 
How close is the XML structure to C++ semantically?


The syntactic level is obviously very different, as one uses XML as 
metaformat while the other is C++. The semantic level is rather about 
the question if there is e.g. a "" that directly 
translates to a "class foo {" in C++. If that is the case, the SAX API 
should help you, as it basically invokes callbacks for every XML element 
encountered while parsing the input stream. In those callbacks, you 
could then generate the according C++ code in a way that should be 
readable and maintainable with plain Python or some template engine.


You you need to skip back-and-forth over the input, reading the whole 
XML as DOM tree would probably be a better approach. Still, the 
processing of input is separate from output generation, so you could at 
least divide your task before conquering it.


Notes:
 - There is also XSLT which can generate pretty much anything from XML, 
but it is can't do much more than text replacements triggered by input 
matching. The more the output differs semantically from the input, the 
more difficult it becomes to use. Also, XSLT tends to become write-only 
code, i.e. unreadable.
 - I think there was a feature in GCC that allows generating XML from 
C++ input, maybe even the reverse. Maybe you could leverage that?



Good luck!

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


Re: an error in python lib?

2012-10-10 Thread Ulrich Eckhardt

Am 10.10.2012 02:32, schrieb Wenhua Zhao:

I just noticed that in /usr/lib/python2.7/threading.py

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

The return values seem to be wrong.  They should be swapped:

 def _is_owned(self):
 if self.__lock.acquire(0):
 self.__lock.release()
 return True
 else:
 return False

Or I understood it wrong here?


I think you are correct, but there is one thing that I would audit 
first: The whole code there seems to use integers in places where a 
boolean would be appropriate, like e.g. the 'blocking' parameter to 
acquire(). I wouldn't be surprised to find the interpretation of "0 
means no error" in some places there, so that a False translates to 0 
and then to "OK, I have the lock".


Also, assuming an underlying implementation where a nonblocking 
acquire() could still newly acquire an uncontended lock, that 
implementation would release the acquired lock and still return "yes I'm 
holding the lock", which would be dead wrong. It must verify if the lock 
count is at least 2 after acquiring the lock.


Uli

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


Re: pydelicious documentation

2012-10-10 Thread Alec Taylor
No worries, happy to help :)

On Tue, Oct 9, 2012 at 11:44 PM, Andres Soto  wrote:
> This one I didn't know
> Thanks you!
> Prof. Dr. Andrés Soto
>
>
> 
> From: Alec Taylor 
> To: Andres Soto 
> Cc: "[email protected]" 
> Sent: Tuesday, October 9, 2012 1:52 PM
> Subject: Re: pydelicious documentation
>
> Actually it seems this project has been official abandoned.
>
> Unofficially use this, was updated only a month ago:
> https://github.com/mgan59/python-pinboard
>
> On Tue, Oct 9, 2012 at 10:47 PM, Alec Taylor  wrote:
>> Hi Professor Soto,
>>
>> Not sure what's going on with their servers', but I was able to find
>> the documentation on their repo:
>>
>> https://pydelicious.googlecode.com/svn/trunk/doc/htmlref/index.html
>> https://pydelicious.googlecode.com/svn/trunk/doc/htmlref/HACKING.html
>> https://pydelicious.googlecode.com/svn/trunk/doc/htmlref/dlcs.html
>> https://pydelicious.googlecode.com/svn/trunk/doc/htmlref/pydelicious.html
>>
>> Just save the files locally then open them in your browser.
>>
>> All the best,
>>
>> Alec Taylor
>>
>> On Tue, Oct 9, 2012 at 10:06 PM, Andres Soto 
>> wrote:
>>> Does somebody know where I can get the documentation for pydelicious?
>>> The documentation links ("For code documentation see doc/pydelicious or
>>> doc/dlcs.py.") in http://packages.python.org/pydelicious/README.html#id3
>>> gave me
>>>
>>> 404 Not Found
>>>
>>> 
>>> nginx/1.1.19
>>>
>>> Prof. Dr. Andrés Soto
>>>
>>>
>>> --
>>> http://mail.python.org/mailman/listinfo/python-list
>>>
>
>
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Generating C++ code

2012-10-10 Thread Jean-Michel Pichavant

> sorry but i don't get what you mean with a "MIPS on a SOC". Is not
> Python well supported on MIPS ?

Sorry, SOC means system on chip. The binary runs on the MIPS, there's no file 
system, no operanding system, except for one very basic task scheduler.

That's why everything is done at compile time.

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


Re: Private methods

2012-10-10 Thread Robert Kern

On 10/10/12 12:51 AM, Steven D'Aprano wrote:

On Tue, 09 Oct 2012 11:08:13 -0600, Ian Kelly wrote:


On Tue, Oct 9, 2012 at 8:08 AM, Demian Brecht 
wrote:

A single underscore semantically means private. A double underscore
will name mangle the function such that it's only accessible strictly
by name through the class that it's define in. Note that you *can*
still access it if you understand how name mangling works. Nothing in
Python is truly private.


I tend to view name mangling as being more for avoiding internal
attribute collisions in complex inheritance structures than for
designating names as private.


Really? I tend to view name mangling as a waste of time, and complex
inheritance structures as something to avoid.


Whatever you may think of the use case, it was the motivating reason why it was 
put into the language:


http://docs.python.org/reference/lexical_analysis.html#reserved-classes-of-identifiers

--
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: Private methods

2012-10-10 Thread Roy Smith
In article 
<1krpdak.u0qy9e1a4knspn%[email protected]>,
 [email protected] (D.M. Procida) wrote:

> Mark Lawrence  wrote:
> 
> > On 09/10/2012 14:24, D.M. Procida wrote:
> > > What exactly is the point of a private method? Why or when would I want
> > > to use one?
> > >
> > > Daniele
> > >
> > 
> > Hardly a Python question but using a search engine could have got you
> > here, and rather faster :) 
> >
> http://stackoverflow.com/questions/2620699/why-private-methods-in-the-ob
> ject-oriented
> 
> Thanks. Sometimes I prefer to talk to real people on Usenet than do web
> searches. Just my preference.
> 
> Anyway, one of the answers on that page explains that public methods are
> interfaces to a class, that other things might rely on, and private ones
> are for its own internal logic, that other things don't need to care
> about.
> 
> In Python, using an underscore is simply a convention to note that a
> method is private - it doesn't actually hide it from other things -
> correct?

Yes (modulo some details of how import works that I've never fully 
figured out and which lack of knowledge hasn't seemed to have hurt me 
any).

I view public and private in Python this way:

Public: I hereby declare that this method or attribute is part of the 
promised never to change interface of this class.  I might possibly 
break that promise at some point in the future, but if I do, you have 
the right to bitch and whine about it, and I'm morally obligated to at 
least pretend I care.

Private: I hereby declare that this method or attribute is something I 
needed to have for my own purposes, and is officially hidden inside my 
kimono.  Like all things inside my kimono, you may speculate about their 
existence all you want, but you touch them at your own peril.  I may 
change them at some point in the future, and while you can bitch and 
whine about it all you want, I'm not listening.
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: an error in python lib?

2012-10-10 Thread Ulrich Eckhardt

Am 10.10.2012 03:16, schrieb MRAB:

On 2012-10-10 01:32, Wenhua Zhao wrote:

Hi list,

I just noticed that in /usr/lib/python2.7/threading.py

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

The return values seem to be wrong.  They should be swapped:

 def _is_owned(self):
 if self.__lock.acquire(0):
 self.__lock.release()
 return True
 else:
 return False

Or I understood it wrong here?


The .acquire method will return True if the attempt to acquire has been
successful. This can occur only if it is not currently owned.


The comment clearly states "owned by current thread", not "owned by any 
thread". The latter would also be useless, as that can change 
concurrently at any time when owned by a different thread, so making 
decisions on this state is futile. Also, acquire() can also return true 
when locking recursively, at least that's how I read the sources.


I think that this is really a bug, but it doesn't surface often because 
the built-in lock has its own _is_owned() function which is used instead 
of this flawed logic.


Uli

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


Re: Delete duplicate rows in textfile - except it contains a "{" or "}"

2012-10-10 Thread Dave Angel
On 10/10/2012 04:51 AM, Joon Ki Choi wrote:
> Hello Pythonistas,
>
> i have a very large textfile with contents like:
>
> @INBOOK{Ackermann1999-b,
>   author = {Ackermann, K.-F. and Ackermann, K.-F. and Ackermann, K.-F. and 
> Ackermann,
>   K.-F. and Ackermann, K.-F. and Ackermann, K.-F. and Ackermann, K.-F.
>   and Ackermann, K.-F. and Ackermann, K.-F. and Ackermann, K.-F. and
>   Ackermann, K.-F. and Ackermann, K.-F. and Ackermann, K.-F. and 
> Ackermann,
>   K.-F. and Ackermann, K.-F. and Ackermann, K.-F. and Ackermann, K.-F.
>   and Ackermann, K.-F. and Ackermann, K.-F. and Ackermann, K.-F. and
>   Ackermann, K.-F. and Ackermann, K.-F. and Ackermann, K.-F. and 
> Ackermann,
>   K.-F. and Ackermann, K.-F. and Ackermann, K.-F. and Ackermann, K.-F.
>   and Ackermann, K.-F. and Ackermann, K.-F. and Ackermann, K.-F. and
>   Ackermann, K.-F. and Ackermann, K.-F. and Ackermann, K.-F. and 
> Ackermann},
>   year = {1980},
>   timestamp = {1995-12-02}
> } 
>
> And i want to delete the duplicate rows except these rows containing the 
> brackets { or }. 
> The result should look like:
>
> @INBOOK{Ackermann1999-b,
>   author = {Ackermann, K.-F. and Ackermann, K.-F. and Ackermann, K.-F. and 
> Ackermann,
>   Ackermann, K.-F. and Ackermann, K.-F. and Ackermann, K.-F. and 
> Ackermann},
>   year = {1980},
>   timestamp = {1995-12-02}
> }

Which is it?  Do you want to match your output, or match your
description?  Your description would result in:

@INBOOK{Ackermann1999-b,
  author = {Ackermann, K.-F. and Ackermann, K.-F. and Ackermann, K.-F. and 
Ackermann,
K.-F. and Ackermann, K.-F. and Ackermann, K.-F. and Ackermann, K.-F.
and Ackermann, K.-F. and Ackermann, K.-F. and Ackermann, K.-F. and
Ackermann, K.-F. and Ackermann, K.-F. and Ackermann, K.-F. and 
Ackermann,
Ackermann, K.-F. and Ackermann, K.-F. and Ackermann, K.-F. and 
Ackermann},
  year = {1980},
  timestamp = {1995-12-02}
}   

(that's doing it by eyeball, so i may have missed some minor differences)



-- 

DaveA

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


Re: Delete duplicate rows in textfile - except it contains a "{" or "}"

2012-10-10 Thread Joon Ki Choi
lines_seen = set() # holds lines already seen
outfile = open("literatur_clean.txt", "w")
for line in open("literatur_dupl.txt", "r"):
if ('{' in line or '}' in line) or line not in lines_seen:
outfile.write(line)
lines_seen.add(line)
outfile.close()
-- 
http://mail.python.org/mailman/listinfo/python-list


Python Web Routing Benchmark

2012-10-10 Thread Andriy Kornatskyy

How fast web frameworks process routing (URL dispatch)?
 
Here is a benchmark for various web frameworks (bottle, django, flask, pyramid, 
tornado and wheezy.web) running the following routing: static, dynamic, SEO and 
missing... with a trivial 'hello world' application (all routes are pointing to 
the same handler).

http://mindref.blogspot.com/2012/10/python-web-routing-benchmark.html
 
Benchmark is executed in isolated environment using CPython 2.7. Source is here:

https://bitbucket.org/akorn/helloworld/src/tip/02-routing
 
Comments or suggestions are welcome.
 
Thanks.
 
Andriy Kornatskyy
  
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Generating C++ code

2012-10-10 Thread Michael Torrie
On 10/09/2012 10:00 AM, Jean-Michel Pichavant wrote:
> Greetings,
> 
> I'm trying to generate C++ code from an XML file. I'd like to use a template 
> engine, which imo produce something readable and maintainable.
> My google search about this subject has been quite unsuccessful, I've been 
> redirected to template engine specific to html mostly.
>
> Any advice would be appreciated.

There are two ways I can see to do this.

One is to use XSLT.  XML is designed to be transformed easily, and XSLT
is the standard vehicle to do this transformation.  You can use XSLT to
translate an XML document to HTML, for example, or to another schema of
XML, or to something non-XML like plain text, Tex, or even C++ code.
XSLT itself is turing complete, so complicated, logical transformations
are possible.  Since I know of no existing transformation templates for
emitting C++, or any other language, you'll have to write it yourself.
Yes it won't be python code per se, though you can drive the XML XSLT
engine from python using a module called lxml.  Anyway, XSLT is
complicated (I have done nothing in it myself), but it is designed for
this kind of thing.

Secondly, you could just use a python XML module of some kind, read the
document in, traverse the nodes and emit code.  This is basic compiler
theory, though your source code is likely already in an abstract syntax
tree (XML).  And I bet you could make it one pass, as you can rely on
the compiler to check symbol names for you.  Compilers are not simple
beasts, but they are fun to write, and very educational.  You get to
learn about grammars and recursive descent parsing (though the XML
library basically does both for you).

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


Re: Generating C++ code

2012-10-10 Thread Grant Edwards
On 2012-10-10, Etienne Robillard  wrote:
> On Wed, 10 Oct 2012 11:59:50 +0200 (CEST)
> Jean-Michel Pichavant  wrote:
>
>> Well, the C++ code will end up running on a MIPS on a SOC,
>> unfortunately, python is not an option here.  The xml to C++ makes a
>> lot of sense, because only a small part of the code is generated that
>> way (everything related to log & fatal events). Everything else is
>> written directly in C++.
>
> sorry but i don't get what you mean with a "MIPS on a SOC". Is not
> Python well supported on MIPS ?

SoC == System On a Chip.

It's a single-chip micro-controller embedded inside something that's
not a general purpose computer (e.g. it's in a router, or piece of
industrial equipment, or whatever). It may only have a couple MB of
memory, it might have only a minimal RTOS (non-Linux/Unix,
non-Windows), or it may actually have no OS at all.  It almost
certainly doesn't have a hard drive.

Many years ago, there was a "deeply embedded Python" project that was
attempting to get Python running on such platforms, but it's been
abandoned for ages.  IIRC, it was using Python 1.50 as a base version.

-- 
Grant Edwards   grant.b.edwardsYow! My vaseline is
  at   RUNNING...
  gmail.com
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Generating C++ code

2012-10-10 Thread Jean-Michel Pichavant
- Original Message -
> On 2012-10-10, Etienne Robillard  wrote:
> > On Wed, 10 Oct 2012 11:59:50 +0200 (CEST)
> > Jean-Michel Pichavant  wrote:
> >
> >> Well, the C++ code will end up running on a MIPS on a SOC,
> >> unfortunately, python is not an option here.  The xml to C++ makes
> >> a
> >> lot of sense, because only a small part of the code is generated
> >> that
> >> way (everything related to log & fatal events). Everything else is
> >> written directly in C++.
> >
> > sorry but i don't get what you mean with a "MIPS on a SOC". Is not
> > Python well supported on MIPS ?
> 
> SoC == System On a Chip.
> 
> It's a single-chip micro-controller embedded inside something that's
> not a general purpose computer (e.g. it's in a router, or piece of
> industrial equipment, or whatever). It may only have a couple MB of
> memory, it might have only a minimal RTOS (non-Linux/Unix,
> non-Windows), or it may actually have no OS at all.  It almost
> certainly doesn't have a hard drive.
> 
> Many years ago, there was a "deeply embedded Python" project that was
> attempting to get Python running on such platforms, but it's been
> abandoned for ages.  IIRC, it was using Python 1.50 as a base
> version.
> 
> --
> Grant Edwards   grant.b.edwardsYow! My vaseline
> is
>   at   RUNNING...
>   gmail.com

Have a look at http://code.google.com/p/python-on-a-chip/
Last news on 2011/09/26, I'm not sure the project is still alive.

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


Re: Generating C++ code

2012-10-10 Thread Stefan Behnel
Jean-Michel Pichavant, 10.10.2012 17:05:
>> SoC == System On a Chip.
>>
>> It's a single-chip micro-controller embedded inside something that's
>> not a general purpose computer (e.g. it's in a router, or piece of
>> industrial equipment, or whatever). It may only have a couple MB of
>> memory, it might have only a minimal RTOS (non-Linux/Unix,
>> non-Windows), or it may actually have no OS at all.  It almost
>> certainly doesn't have a hard drive.
>>
>> Many years ago, there was a "deeply embedded Python" project that was
>> attempting to get Python running on such platforms, but it's been
>> abandoned for ages.  IIRC, it was using Python 1.50 as a base
>> version.
>>
>> Grant Edwards
> 
> Have a look at http://code.google.com/p/python-on-a-chip/
> Last news on 2011/09/26, I'm not sure the project is still alive.

For FLOSS projects, it's usually better to look at the revision history,
which, in this case, names September 3 as last entry, *this* year.

Pretty much alive, I'd say.

Stefan


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


Re: Private methods

2012-10-10 Thread Ramchandra Apte
On Wednesday, 10 October 2012 18:38:04 UTC+5:30, Roy Smith  wrote:
> In article 
> 
> <1krpdak.u0qy9e1a4knspn%[email protected]>,
> 
>  [email protected] (D.M. Procida) wrote:
> 
> 
> 
> > Mark Lawrence  wrote:
> 
> > 
> 
> > > On 09/10/2012 14:24, D.M. Procida wrote:
> 
> > > > What exactly is the point of a private method? Why or when would I want
> 
> > > > to use one?
> 
> > > >
> 
> > > > Daniele
> 
> > > >
> 
> > > 
> 
> > > Hardly a Python question but using a search engine could have got you
> 
> > > here, and rather faster :) 
> 
> > >
> 
> > http://stackoverflow.com/questions/2620699/why-private-methods-in-the-ob
> 
> > ject-oriented
> 
> > 
> 
> > Thanks. Sometimes I prefer to talk to real people on Usenet than do web
> 
> > searches. Just my preference.
> 
> > 
> 
> > Anyway, one of the answers on that page explains that public methods are
> 
> > interfaces to a class, that other things might rely on, and private ones
> 
> > are for its own internal logic, that other things don't need to care
> 
> > about.
> 
> > 
> 
> > In Python, using an underscore is simply a convention to note that a
> 
> > method is private - it doesn't actually hide it from other things -
> 
> > correct?
> 
> 
> 
> Yes (modulo some details of how import works that I've never fully 
> 
> figured out and which lack of knowledge hasn't seemed to have hurt me 
> 
> any).
> 
> 
> 
> I view public and private in Python this way:
> 
> 
> 
> Public: I hereby declare that this method or attribute is part of the 
> 
> promised never to change interface of this class.  I might possibly 
> 
> break that promise at some point in the future, but if I do, you have 
> 
> the right to bitch and whine about it, and I'm morally obligated to at 
> 
> least pretend I care.
> 
> 
> 
> Private: I hereby declare that this method or attribute is something I 
> 
> needed to have for my own purposes, and is officially hidden inside my 
> 
> kimono.  Like all things inside my kimono, you may speculate about their 
> 
> existence all you want, but you touch them at your own peril.  I may 
> 
> change them at some point in the future, and while you can bitch and 
> 
> whine about it all you want, I'm not listening.

Uhum. Language please.
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Private methods

2012-10-10 Thread Mark Lawrence

On 10/10/2012 16:56, Ramchandra Apte wrote:

On Wednesday, 10 October 2012 18:38:04 UTC+5:30, Roy Smith  wrote:

Public: I hereby declare that this method or attribute is part of the
promised never to change interface of this class.  I might possibly
break that promise at some point in the future, but if I do, you have
the right to bitch and whine about it, and I'm morally obligated to at
least pretend I care.

Private: I hereby declare that this method or attribute is something I
needed to have for my own purposes, and is officially hidden inside my
kimono.  Like all things inside my kimono, you may speculate about their
existence all you want, but you touch them at your own peril.  I may
change them at some point in the future, and while you can bitch and
whine about it all you want, I'm not listening.


Uhum. Language please.



What language?  Further the original was readable, your use of CrapMail 
made life difficult until I stripped the superfluous newlines out.  Is 
it really so awkward to equip yourself with a semi-decent mail reader? 
Like Thunderbird, hint, hint :)


--
Cheers.

Mark Lawrence.

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


Re: Private methods

2012-10-10 Thread Grant Edwards
On 2012-10-10, Mark Lawrence  wrote:
> On 10/10/2012 16:56, Ramchandra Apte wrote:
>> On Wednesday, 10 October 2012 18:38:04 UTC+5:30, Roy Smith  wrote:
>>> Public: I hereby declare that this method or attribute is part of the
>>> promised never to change interface of this class.  I might possibly
>>> break that promise at some point in the future, but if I do, you have
>>> the right to bitch and whine about it, and I'm morally obligated to at
>>> least pretend I care.
>>>
>>> Private: I hereby declare that this method or attribute is something I
>>> needed to have for my own purposes, and is officially hidden inside my
>>> kimono.  Like all things inside my kimono, you may speculate about their
>>> existence all you want, but you touch them at your own peril.  I may
>>> change them at some point in the future, and while you can bitch and
>>> whine about it all you want, I'm not listening.
>>
>> Uhum. Language please.
>
> What language?

Perhaps he didn't like the kimono metaphor?

I always though the kimono metaphore as commonly used by MBA/sales
types was a little unseemly (but then most of what those types say is,
regardless of language or metaphor).

> Further the original was readable, your use of CrapMail made life
> difficult until I stripped the superfluous newlines out.  Is it
> really so awkward to equip yourself with a semi-decent mail reader?
> Like Thunderbird, hint, hint :)

You're tilting at windmills.

Just give up and filter out all postings with a messageid ending in
'@googlegroups.com'.  I find that solves all sorts of problems...

-- 
Grant Edwards   grant.b.edwardsYow! !  Now I understand
  at   advanced MICROBIOLOGY and
  gmail.comth' new TAX REFORM laws!!
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: an error in python lib?

2012-10-10 Thread Ian Kelly
On Wed, Oct 10, 2012 at 6:18 AM, Ulrich Eckhardt
 wrote:
>> The .acquire method will return True if the attempt to acquire has been
>> successful. This can occur only if it is not currently owned.
>
>
> The comment clearly states "owned by current thread", not "owned by any
> thread". The latter would also be useless, as that can change concurrently
> at any time when owned by a different thread, so making decisions on this
> state is futile.

If you're correct, then the bug runs deeper than simply swapping the
return values.  If the first case returned True instead of False, then
it would be returning True when the lock is not owned by any thread.

> Also, acquire() can also return true when locking
> recursively, at least that's how I read the sources.

Python 2.7.1 (r271:86832, Nov 27 2010, 18:30:46) [MSC v.1500 32 bit (Intel)] on
win32
Type "help", "copyright", "credits" or "license" for more information.
>>> from threading import Lock
>>> lock = Lock()
>>> lock.acquire(0)
True
>>> lock.acquire(0)
False

> I think that this is really a bug, but it doesn't surface often because the
> built-in lock has its own _is_owned() function which is used instead of this
> flawed logic.

Can you demonstrate an API bug that is caused by this?
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: communicate with external process via pty

2012-10-10 Thread Tim
On Wednesday, October 10, 2012 2:58:55 AM UTC-4, Peter Otten wrote:
> Tim Arnold wrote:
> 

> > import os,sys
> > import subprocess
> > import shlex
> > import pty
> > cmd =  'tralics --interactivemath'
> > (master, slave) = pty.openpty()
> > p = subprocess.Popen(shlex.split(cmd),close_fds=True,
> >   stdin=slave,stdout=slave,stderr=slave)
> > 
> > os.read(master,1024)# start the process
> > os.write(master,'$\sqrt{x}$\n') # feed it an equation
> > mathml.append(os.read(master,1024)) # get the mathml in a string
> > os.write(master,'$\sqrt{x}$\n') # feed more equations
> > mathml.append(os.read(master,1024)) # get the next string
> 
> 
> Do you know about pexpect?
> 
> http://pypi.python.org/pypi/pexpect

Thanks Peter. I knew of it, but hadn't tried it. I've been playing with it 
today and I think it should work fine for my project.  With some help from 
stackoverflow, this seems to do what I need:

import pexpect
c = pexpect.spawn('tralics --interactivemath')
c.expect('>')

c.sendline('$x+y=z$')
c.expect("")
print c.math.group()

c.expect('>')
c.sendline('$a+b=c$')
c.expect("")
print c.math.group()
etc.

thanks, 
--Tim


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


Hello, We are looking for a qualified Sr. Python Developer for a job position in Bethesda, MD.

2012-10-10 Thread John Cook
Please let me know, if you might want to consider this progressive job 
opportunity and discuss the position details.

Thank you for the thoughts.

Regards,

John Cook
Team Recruiting Services

Terra Health, Inc
5710 W. Hausman, Ste 108 
San Antonio, TX 78249
P-210.424.4017
F-210.582.0084
C-210.823.6522
[email protected]
  
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Private methods

2012-10-10 Thread alex23
On 10 Oct, 17:03, [email protected] (D.M.
Procida) wrote:
> It certainly makes it quick to build a class with the attributes I need,
> but it does make tracing logic sometimes a pain in the neck.
>
> I don't know what the alternative is though.

Components.

The examples are in C++ and it's about game development, but I found
this article to be very good at explaining the approach:
http://gameprogrammingpatterns.com/component.html

I've become a big fan of components & adaptation using zope.interface:
http://wiki.zope.org/zope3/ZopeGuideComponents
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Private methods

2012-10-10 Thread alex23
On 11 Oct, 02:14, Mark Lawrence  wrote:
> What language?

I think he's objecting to "bitch".

I had to block him on G+ because he kept asking me to self-censor
posts that he had _chosen to read_.
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Private methods

2012-10-10 Thread Steven D'Aprano
On Wed, 10 Oct 2012 18:34:01 -0700, alex23 wrote:

> On 10 Oct, 17:03, [email protected] (D.M.
> Procida) wrote:
>> It certainly makes it quick to build a class with the attributes I
>> need, but it does make tracing logic sometimes a pain in the neck.
>>
>> I don't know what the alternative is though.
> 
> Components.

Composition. Delegation. Traits. Prototypes.

Inheritance is great, but it is not a solution to everything.


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


Re: Private methods

2012-10-10 Thread 88888 Dihedral
D.M. Procida於 2012年10月9日星期二UTC+8下午9時24分30秒寫道:
> What exactly is the point of a private method? Why or when would I want
> 

Private methods in the object level are searched first 
in the run time of python interpreter. 

I did turn some private methods in to c programs from time to time.

 
 

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


Re: Generating C++ code

2012-10-10 Thread Tim Roberts
Jean-Michel Pichavant  wrote:
>
>I'm trying to generate C++ code from an XML file. I'd like to use a template 
>engine, which imo produce something readable and maintainable.
>My google search about this subject has been quite unsuccessful, I've been 
>redirected to template engine specific to html mostly.
>
>Does anybody knows a python template engine for generating C++ code ?

I'm a big fan of Cheetah.  It's simple but flexible enough to be useful.
Besides the many web projects I've done with it, I also I use it in one
project to generate PHP code (it generates data access objects from a live
database schema).
-- 
Tim Roberts, [email protected]
Providenza & Boekelheide, Inc.
-- 
http://mail.python.org/mailman/listinfo/python-list


Is `wsample` a proper name for a function like this?

2012-10-10 Thread Satoru Logic
I came across a function named `wsample` in a `utils` package of my workplace 
recently.

The "w" in `wsample` stands for `weighted`, and it randomly selects an element 
from a container according to relative weights of all the elements.

In most articles and codes I saw online, a function like this is often named 
`weighted_random_choice`, which sounds *correct* to me.
So when I saw this `wsample` function, I considered it a improper name.
Because `wsample`makes me think of `random.sample`, which returns a list of 
randomly generated elements, not a element.
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Is `wsample` a proper name for a function like this?

2012-10-10 Thread Terry Reedy

On 10/10/2012 11:33 PM, Satoru Logic wrote:

I came across a function named `wsample` in a `utils` package of my
workplace recently.

The "w" in `wsample` stands for `weighted`, and it randomly selects
an element from a container according to relative weights of all the
elements.


I agree that wt_select for weighted select might be better for a sample 
size of 1 ;-).



In most articles and codes I saw online, a function like this is
often named `weighted_random_choice`, which sounds *correct* to me.


Easier to understand, at least the first time, harder to write. Be glad 
the author did not use 'ws' as would have once been common.



So when I saw this `wsample` function, I considered it a improper
name. Because `wsample`makes me think of `random.sample`, which
returns a list of randomly generated elements, not a element.


--
Terry Jan Reedy

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


Re: Is `wsample` a proper name for a function like this?

2012-10-10 Thread alex23
On Oct 11, 1:33 pm, Satoru Logic  wrote:
> I came across a function named `wsample` in a `utils` package
> of my workplace recently.
>
> The "w" in `wsample` stands for `weighted`, and it randomly
> selects an element from a container according to relative
> weights of all the elements.
>
> So when I saw this `wsample` function, I considered it a improper name.

I tend to agree. I like descriptive names for functions that don't
assume familiarity. If you're unhappy with a function's name and
you're unable to modify the module it belongs to, you can always
rebind it yourself:

from utils import wsample as weighted_random_choice

If I'm then going to use that function heavily elsewhere, I _might_
rebind it, but always within the scope that is going to use the
abbreviated binding:

wrc = weighted_random_choice

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


Re: Private methods

2012-10-10 Thread Dieter Maurer
alex23  writes:

> On 10 Oct, 17:03, [email protected] (D.M.
> Procida) wrote:
>> It certainly makes it quick to build a class with the attributes I need,
>> but it does make tracing logic sometimes a pain in the neck.
>>
>> I don't know what the alternative is though.
>
> Components.
>
> The examples are in C++ and it's about game development, but I found
> this article to be very good at explaining the approach:
> http://gameprogrammingpatterns.com/component.html
>
> I've become a big fan of components & adaptation using zope.interface:
> http://wiki.zope.org/zope3/ZopeGuideComponents

If multiple inheritance is deemed complex, adaptation is even more so:

  With multiple inheritance, you can quite easily see from the source
  code how things are put together.
  Adaptation follows the "inversion of control" principle. With this
  principle, how a function is implemented, is decided outside
  and can very easily be changed (e.g. through configuration).
  This gives great flexibility but also nightmares when things do
  not work as expected...

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


Re: Is `wsample` a proper name for a function like this?

2012-10-10 Thread Steven D'Aprano
On Wed, 10 Oct 2012 20:33:16 -0700, Satoru Logic wrote:

> I came across a function named `wsample` in a `utils` package of my
> workplace recently.
> 
> The "w" in `wsample` stands for `weighted`, and it randomly selects an
> element from a container according to relative weights of all the
> elements.
> 
> In most articles and codes I saw online, a function like this is often
> named `weighted_random_choice`, which sounds *correct* to me. So when I
> saw this `wsample` function, I considered it a improper name. Because
> `wsample`makes me think of `random.sample`, which returns a list of
> randomly generated elements, not a element.

You can have a sample size of one.

wsample sounds fine to me. weighted_random_choice is okay too. It depends 
whether you value brevity over explicitness. Explicit is good, but some 
names are just too long.

If this were my code base, I would probably go for weighted_sample 
without mentioning "random" in the name, the reasoning being that samples 
are almost always random so explicitly saying so doesn't help much.


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


Re: Is `wsample` a proper name for a function like this?

2012-10-10 Thread suzaku
On Thursday, October 11, 2012 2:29:37 PM UTC+8, Steven D'Aprano wrote:
> On Wed, 10 Oct 2012 20:33:16 -0700, Satoru Logic wrote:
> 
> 
> 
> > I came across a function named `wsample` in a `utils` package of my
> 
> > workplace recently.
> 
> > 
> 
> > The "w" in `wsample` stands for `weighted`, and it randomly selects an
> 
> > element from a container according to relative weights of all the
> 
> > elements.
> 
> > 
> 
> > In most articles and codes I saw online, a function like this is often
> 
> > named `weighted_random_choice`, which sounds *correct* to me. So when I
> 
> > saw this `wsample` function, I considered it a improper name. Because
> 
> > `wsample`makes me think of `random.sample`, which returns a list of
> 
> > randomly generated elements, not a element.
> 
> 
> 
> You can have a sample size of one.
> 
> 
> 
> wsample sounds fine to me. weighted_random_choice is okay too. It depends 
> 
> whether you value brevity over explicitness. Explicit is good, but some 
> 
> names are just too long.
> 
> 
> 
> If this were my code base, I would probably go for weighted_sample 
> 
> without mentioning "random" in the name, the reasoning being that samples 
> 
> are almost always random so explicitly saying so doesn't help much.

I think if a programmer has used the built-in `random` module before, he would 
expect a function with "sample" in its name to return a population sequence.

If a function is to return scalar value instead of sequence, I would expect it 
to be named "choice".

> 
> 
> 
> 
> 
> -- 
> 
> Steven

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