Re: multiple versions of python

2013-05-08 Thread Fábio Santos
On 8 May 2013 01:03, "Roy Smith"  wrote:
>
> In article <72f93710-9812-441e-8d3d-f221d5698...@googlegroups.com>,
>  sokovic.anamar...@gmail.com wrote:
>
> > Hi,
> >
> > what is the generally recommended structure when we have into play this
type
> > of problem:
> > multiple versions of python (both in the sense of main versions and sub
> > versions, e.g.,
> > 2.7 :
> >2.7.1
> >2.7.3
> > 3:
> >  3.3
> >3.3.1
> > Different versions of gcc
> > different compilation strategies (-vanilla and non-vanilla)
> > different modules (numpy,scipy) together with the different versions of
all
> > the rest.
> >
> > any help is appreciated
> >
> > Ana
>
> Virtualenv is your friend.
> --
> http://mail.python.org/mailman/listinfo/python-list

Have you looked at tox? It manages a folder full of virtualenvs for you,
including your dependencies, and allows you to run your tests against every
version.
-- 
http://mail.python.org/mailman/listinfo/python-list


object.enable() anti-pattern

2013-05-08 Thread Steven D'Aprano
I'm looking for some help in finding a term, it's not Python-specific but 
does apply to some Python code.

This is an anti-pattern to avoid. The idea is that creating a resource 
ought to be the same as "turning it on", or enabling it, or similar. For 
example, we don't do this in Python:


f = file("some_file.txt")
f.open()
data = f.read()


because reading the file can fail if you forget to call open first. 
Instead, Python uses a factory function that creates the file object and 
opens it:

f = open("some_file.txt")  # if this succeeds, f is ready to use
data = f.read()


Basically, any time you have two steps required for using an object, e.g. 
like this:

obj = someobject(arg1, arg2)
obj.enable()

you should move the make-it-work functionality out of the enable method 
and into __init__ so that creating the object creates it in a state ready 
to work.

I read a blog some time ago (a year ago?) that discusses this anti-
pattern, and I'm pretty sure gave it a specific name, but I cannot find 
it again. Can anyone find any references to this anti-pattern? My google-
fu is letting me down.



(For what it's worth, I'm refactoring some of my own code that falls into 
this anti-pattern. Die, enable method, die die die!)


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


cello library

2013-05-08 Thread Mark Lawrence

Hi folks,

I thought some of you might find this interesting http://libcello.org/

--
If you're using GoogleCrap™ please read this 
http://wiki.python.org/moin/GoogleGroupsPython.


Mark Lawrence

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


Re: object.enable() anti-pattern

2013-05-08 Thread Christian Heimes
Am 08.05.2013 10:52, schrieb Steven D'Aprano:
> Basically, any time you have two steps required for using an object, e.g. 
> like this:
> 
> obj = someobject(arg1, arg2)
> obj.enable()
> 
> you should move the make-it-work functionality out of the enable method 
> and into __init__ so that creating the object creates it in a state ready 
> to work.

In general I agree that an object.enable() function is ugly. ;)

But it's not necessarily the best solution for all problems. If the
resource needs some kind of cleanup, the context api (__enter__() /
__exit__()) is perfectly fine way to enable and disable the object.

For example:

class MyFile:
def __init__(self, filename):
self.filename = filename
def __enter__(self):
self.open()
return self
def __exit__(self, exc_type, exc_val, exc_tb):
self.close()

I suggest that you mention the context API in your blog post, too.

Christian

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


Re: cello library

2013-05-08 Thread Chris Angelico
On Wed, May 8, 2013 at 7:39 PM, Mark Lawrence  wrote:
> Hi folks,
>
> I thought some of you might find this interesting [link redacted]

If this is a legit post, can you please elaborate on just _why_ we
would find it interesting? I'm leery of clicking random links like
that without at least some indication. It could be some cool Python
library, or it could be something relating to music, or it could be a
malicious site ready to compromise my browser and your Yahoo account
was compromised to send spam.

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


Re: object.enable() anti-pattern

2013-05-08 Thread Robert Kern

On 2013-05-08 09:52, Steven D'Aprano wrote:

I'm looking for some help in finding a term, it's not Python-specific but
does apply to some Python code.

This is an anti-pattern to avoid. The idea is that creating a resource
ought to be the same as "turning it on", or enabling it, or similar.


I don't think the anti-pattern has a name, but it's opposite pattern is named:

http://en.wikipedia.org/wiki/Resource_Acquisition_Is_Initialization

--
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: Why do Perl programmers make more money than Python programmers

2013-05-08 Thread rusi
On May 8, 6:11 am, Benjamin Kaplan  wrote:
> On May 7, 2013 5:42 PM, "Neil Hodgson"  wrote:

> > jmfauth:
>
> >> 2) More critical, Py 3.3, just becomes non unicode compliant,
> >> (eg European languages or "ascii" typographers !)
> >> ...
>
> >    This is not demonstrating non-compliance. It is comparing performance,
> not compliance.
>
> >    Please show an example where Python 3.3 is not compliant with Unicode.
>
> >    Neil
>
> It's violating page 1+1j of the Unicode spec, where it says precisely how
> long each operation is allowed to take. Only wise people can see that page.

Ha Ha! -- I infer some Harry Potter here!
[Boarding train at platform 7.3 ]
-- 
http://mail.python.org/mailman/listinfo/python-list


Help with implementing callback functions using ctypes

2013-05-08 Thread jamadagni
Hello. I am running Kubuntu Raring with Python 3.3.1. (I mostly don't myself 
use Py2.) 

I have the below C program spiro.c (obviously a simplified testcase) which I 
compile to a sharedlib using clang -fPIC -shared -o libspiro.so spiro.c, sudo 
cp to /usr/lib and am trying to call from a Python script spiro.py using 
ctypes. However it would seem that the first call to the callback results in a 
segfault. (The line before the callback is executed.) I tried debugging using 
gdb but perhaps because of my insufficient knowledge I couldn't find out what 
is causing the problem. 

I hope some of the kind knowledgeable souls here can help me out:

-- spiro.c

# include 
# include 

typedef struct _bezctx bezctx ;
struct _bezctx {
void ( * moveto) ( bezctx * bc, double x, double y, bool 
starts_open_path ) ;
void ( * lineto) ( bezctx * bc, double x, double y ) ;
void ( * curveto   ) ( bezctx * bc, double c1x, double c1y, double c2x, 
double c2y, double x, double y ) ;
void ( * track_seg ) ( bezctx * bc, unsigned int start_node_idx ) ;
} ;

typedef struct {
double x, y ;
char ty ;
} spiro_cp ;
 
bool spiro_to_bezier_strict ( const spiro_cp src [], int src_len, bezctx * bc ) 
{
for ( int i = 0 ; i < src_len ; ++i ) {
const spiro_cp * p = &(src[i]) ;
printf ( "%g %g %c\n", p->x, p->y, p->ty ) ;
if ( i % 2 == 0 )
bc -> moveto ( bc, p->x, p->y, i % 3 ) ;
else
bc -> lineto ( bc, p->x, p->y ) ;
bc -> curveto ( bc, 233, 150, 267, 150, 300, 100 ) ;
bc -> track_seg ( bc, i ) ;
}
return src_len % 2 == 0 ;
}

-- spiro.py

from ctypes import *

# incomplete struct definition needed for pointer below
class bezctx ( Structure ) : pass

# pointer to function types for callback
MoveToFnType   = CFUNCTYPE ( None, POINTER ( bezctx ), c_double, c_double, 
c_bool )
LineToFnType   = CFUNCTYPE ( None, POINTER ( bezctx ), c_double, c_double )
CurveToFnType  = CFUNCTYPE ( None, POINTER ( bezctx ), c_double, c_double, 
c_double, c_double, c_double, c_double )
TrackSegmentFnType = CFUNCTYPE ( None, POINTER ( bezctx ), c_uint )

# completing the struct definition
bezctx._fields_ = [ ( "moveto"   , MoveToFnType   ), 
( "lineto"   , LineToFnType   ), 
( "curveto"  , CurveToFnType  ), 
( "track_seg", TrackSegmentFnType ) ]

# the python callbacks
def moveTo ( bc, x, y, startsOpenPath ) :
print ( "moveTo", bc, x, y, startsOpenPath )
def lineTo ( bc, x, y ) :
print ( "lineTo", bc, x, y )
def curveTo ( bc, c1x, c1y, c2x, c2y, x, y ) :
print ( "curveTo", bc, c1x, c1y, c2x, c2y, x, y )
def trackSegment ( bc, start_node_idx ) :
print ( "trackSegment", bc, start_node_idx )

# instance of struct
bc = bezctx ()
bc . moveto= MoveToFnType   ( moveTo   )
bc . lineto= LineToFnType   ( lineTo   )
bc . curveto   = CurveToFnType  ( curveTo  )
bc . track_seg = TrackSegmentFnType ( trackSegment )

# another struct
class spiro_cp ( Structure ) :
_fields_ = [ ( "x", c_double ), ( "y", c_double ), ( "ty", c_char ) ]

# array of struct instances
srclist = [ spiro_cp ( 147, 215, b'o' ), 
spiro_cp ( 168, 136, b'o' ), 
spiro_cp ( 294, 184, b'[' ), 
spiro_cp ( 381, 136, b']' ), 
spiro_cp ( 445, 204, b'c' ), 
spiro_cp ( 364, 235, b'c' ), 
spiro_cp ( 266, 285, b'v' ) ]
src = ( spiro_cp * len ( srclist ) ) ( *srclist )

# open the sharedlib
libspiro = CDLL ( "libspiro.so" )
spiro_to_bezier_strict = libspiro.spiro_to_bezier_strict

# call the C function
spiro_to_bezier_strict ( src, len ( src ), bc )

-- bash session

$ python3 spiro-ctypes.py 
147 215 o
Segmentation fault (core dumped)
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: object.enable() anti-pattern

2013-05-08 Thread Steven D'Aprano
On Wed, 08 May 2013 11:13:33 +0100, Robert Kern wrote:

> On 2013-05-08 09:52, Steven D'Aprano wrote:
>> I'm looking for some help in finding a term, it's not Python-specific
>> but does apply to some Python code.
>>
>> This is an anti-pattern to avoid. The idea is that creating a resource
>> ought to be the same as "turning it on", or enabling it, or similar.
> 
> I don't think the anti-pattern has a name, but it's opposite pattern is
> named:
> 
> http://en.wikipedia.org/wiki/Resource_Acquisition_Is_Initialization

That sounds like it should be related, but it actually isn't since RAII 
actually has little to do with *acquiring* the resource and everything to 
do with *releasing* it. See, for example:

http://stackoverflow.com/questions/2321511/what-is-meant-by-resource-
acquisition-is-initialization-raii

where it is pointed out that the resource may be acquired outside of the 
object constructor and passed in as an argument. RAII is actually about 
deterministic destruction of objects and the release of their resources.

But thanks for the attempt :-)


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


Re: object.enable() anti-pattern

2013-05-08 Thread Roy Smith
In article <518a123c$0$11094$c3e8...@news.astraweb.com>,
 Steven D'Aprano  wrote:

> I'm looking for some help in finding a term, it's not Python-specific but 
> does apply to some Python code.
> 
> This is an anti-pattern to avoid. The idea is that creating a resource 
> ought to be the same as "turning it on", or enabling it, or similar. For 
> example, we don't do this in Python:
> 
> 
> f = file("some_file.txt")
> f.open()
> data = f.read()

I've worked with C++ code that did this.  At one point in the evolution 
of OOP group consciousness, there was a feeling that constructors must 
never fail.  I don't remember if it was a general language-agnostic 
pattern, or a specific C++ reaction to poor exception handling code in 
early compilers.  What came out of that was the pattern you describe.  
All the code that could fail was factored out of the constructor into an 
"enable" method.

That being said, sometimes there are good reasons for doing this.  One 
example might be something like:

frobnicator = Frobnicator()
for file in my_file_list:
   frobnicator.munch(file)
   for line in frobnicator:
  process(line)

If creating a Frobnicator instance is very expensive, it might pay to 
create an instance once and keep reusing it on multiple files.  Here, 
munch() is your enable() method.  But, that's not quite what you were 
talking about.
-- 
http://mail.python.org/mailman/listinfo/python-list


Unicode humor

2013-05-08 Thread Roy Smith
Apropos to any of the myriad unicode threads that have been going on 
recently:

http://xkcd.com/1209/
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: object.enable() anti-pattern

2013-05-08 Thread Duncan Booth
Steven D'Aprano  wrote:

> I'm looking for some help in finding a term, it's not Python-specific
> but does apply to some Python code.
> 
> This is an anti-pattern to avoid. The idea is that creating a resource
> ought to be the same as "turning it on", or enabling it, or similar

I've come across this under the name 'two-phase construction', but as a 
desirable(!?) pattern rathern than as an anti-pattern.

In particular Symbian used it throughout as originally their C++ 
implementation didn't support exceptions. Instead they had a separate 
cleanup stack and objects that require cleanup were pushed onto that stack 
after being fully constructed but before calling the initialisation that 
required cleanup. See 
http://www.developer.nokia.com/Community/Wiki/Two-phase_construction


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


Re: Unicode humor

2013-05-08 Thread Sven
I saw this this morning and the first thing I thought of was this list...

unlike you I was too lazy to post it :)


On 8 May 2013 14:19, Roy Smith  wrote:

> Apropos to any of the myriad unicode threads that have been going on
> recently:
>
> http://xkcd.com/1209/
> --
> http://mail.python.org/mailman/listinfo/python-list
>



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


Re: Unicode humor

2013-05-08 Thread Christian Gollwitzer

Am 08.05.13 15:19, schrieb Roy Smith:

Apropos to any of the myriad unicode threads that have been going on
recently:

http://xkcd.com/1209/


http://xkcd.com/1137/


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


MySQL Database

2013-05-08 Thread Kevin Holleran
Hello,

I want to connect to a MySQL database, query for some records, manipulate
some data, and then update the database.

When I do something like this:

db_c.execute("SELECT a, b FROM Users")

for row in db_c.fetchall():

(r,d) = row[0].split('|')

(g,e) = domain.split('.')

db_c.execute("UPDATE Users SET g = '" + g + "' WHERE a ='" + row[0])

Will using db_c to update the database mess up the loop that is cycling
through db_c.fetchall()?

Thanks for your help.

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


Re: MySQL Database

2013-05-08 Thread Chris Angelico
On Thu, May 9, 2013 at 4:52 AM, Kevin Holleran  wrote:
> Will using db_c to update the database mess up the loop that is cycling
> through db_c.fetchall()?

Nope; fetchall() returns a list, which you're then iterating over.
Nothing the database does can disrupt that.

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


Re: MySQL Database

2013-05-08 Thread Kevin Holleran
On Wed, May 8, 2013 at 2:56 PM, Chris Angelico  wrote:

> On Thu, May 9, 2013 at 4:52 AM, Kevin Holleran  wrote:
> > Will using db_c to update the database mess up the loop that is cycling
> > through db_c.fetchall()?
>
> Nope; fetchall() returns a list, which you're then iterating over.
> Nothing the database does can disrupt that.
>
> ChrisA
> --
> http://mail.python.org/mailman/listinfo/python-list
>

Awesome.  That was my hope...

Thanks for your help & quick response.

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


Re: MySQL Database

2013-05-08 Thread MRAB

On 08/05/2013 19:52, Kevin Holleran wrote:

Hello,

I want to connect to a MySQL database, query for some records,
manipulate some data, and then update the database.

When I do something like this:

 db_c.execute("SELECT a, b FROM Users")

for row in db_c.fetchall():

 (r,d) = row[0].split('|')

 (g,e) = domain.split('.')

 db_c.execute("UPDATE Users SET g = '"+ g + "' WHERE a ='"+ row[0])


Will using db_c to update the database mess up the loop that is cycling
through db_c.fetchall()?


You shouldn't be building an SQL string like that because it's
susceptible to SQL injection. You should be doing it more like this:

db_c.execute("UPDATE Users SET g = %s WHERE a = %s", (g, row[0]))

The values will then be handled safely for you.
--
http://mail.python.org/mailman/listinfo/python-list


Re: MySQL Database

2013-05-08 Thread Kevin Holleran
Thanks,  I actually intend to, was just whipping something up to be an
example for my question.



--
Kevin Holleran
Master of Science, Computer Information Systems
Grand Valley State University
Master of Business Administration
Western Michigan University
GCFA, GCFE, CCNA, ISA, MCSA, MCDST, MCP

"Do today what others won't, do tomorrow what others can't" - SEALFit

"We are what we repeatedly do. Excellence, then, is not an act, but a
habit." - Aristotle


On Wed, May 8, 2013 at 3:07 PM, MRAB  wrote:

> On 08/05/2013 19:52, Kevin Holleran wrote:
>
>> Hello,
>>
>> I want to connect to a MySQL database, query for some records,
>> manipulate some data, and then update the database.
>>
>> When I do something like this:
>>
>>  db_c.execute("SELECT a, b FROM Users")
>>
>> for row in db_c.fetchall():
>>
>>  (r,d) = row[0].split('|')
>>
>>  (g,e) = domain.split('.')
>>
>>  db_c.execute("UPDATE Users SET g = '"+ g + "' WHERE a ='"+
>> row[0])
>>
>>
>> Will using db_c to update the database mess up the loop that is cycling
>> through db_c.fetchall()?
>>
>>  You shouldn't be building an SQL string like that because it's
> susceptible to SQL injection. You should be doing it more like this:
>
> db_c.execute("UPDATE Users SET g = %s WHERE a = %s", (g, row[0]))
>
> The values will then be handled safely for you.
> --
> http://mail.python.org/**mailman/listinfo/python-list
>
-- 
http://mail.python.org/mailman/listinfo/python-list


ANN: MacroPy: quasiquotes, case classes, LINQ and more! Now available on PyPI

2013-05-08 Thread Haoyi Li
Hey All,

MacroPy is an implementation of Macros in Python which lets you very easily 
modify the semantics of a python program. Apart from the implementation of 
macros themselves, we also have a pretty impressive list of feature demos that 
were implemented on top of macros:

- Quasiquotes, a quick way to manipulate fragments of a program
- String Interpolation, a common feature in many languages
- Pyxl, integrating XML markup into a Python program
- Tracing and Smart Asserts
- Case Classes, easy Algebraic Data Types from Scala
- Pattern Matching from the Functional Programming world
- LINQ to SQL from C#
- Quick Lambdas from Scala and Groovy,
- Parser Combinators, inspired by Scala's.

The full documentation is over on github (https://github.com/lihaoyi/macropy) 
if anyone wants to check it out. It runs fine on both CPython 2.7 and PyPy 1.9, 
and I've just pushed the last up-to-date version of MacroPy to PyPI: 

https://pypi.python.org/pypi/MacroPy

Hope someone finds this useful!

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


Re: Get filename using filefialog.askfilename

2013-05-08 Thread cheirasacan
El martes, 7 de mayo de 2013 23:53:32 UTC+2, Terry Jan Reedy  escribió:
> On 5/7/2013 4:27 PM, cheirasa...@gmail.com wrote:
> 
> 
> 
> > file = filedialog.askopenfile ( mode... )
> 
> 
> 
> askopenfile is a convenience function that creates an Open dialog 
> 
> object, shows it, gets the name returned by the dialog, opens the file 
> 
> with that name, and returns an appropriate normal file object
> 
> 
> 
> > to open a file with an open dialog box, OK. Made it.
> 
> >
> 
> > How i get the name of the opened file?
> 
> 
> 
> file.name, (at least in 3.3), which in your example below is "file.doc"
> 
> 
> 
> > print(file)
> 
> >
> 
> > the output is: <..name="file.doc"...mode=..encoding..  >
> 
> 
> 
> This is the standard string representation of a file object. It is 
> 
> created from the various attributes of the file instance, including 
> 
> file.name.
> 
> 
> 
> > How can i get the second member of 'file'?
> 
> 
> 
> Strings do not have fields. The second 'member', would be the second 
> 
> character, file[1], which is not what you want.
> 
> 
> 
> > And i am unable to find a detailed reference to this object in the i.net
> 
> 
> 
> Use the Fine Manual. The entry for builtin open() function, which you 
> 
> should read to understand the 'open' part of askopenfile, directs you to 
> 
> the Glossary entry 'file object' which says "There are actually three 
> 
> categories of file objects: raw binary files, buffered binary files and 
> 
> text files. Their interfaces are defined in the io module. The canonical 
> 
> way to create a file object is by using the open() function." The kind 
> 
> of file object you get is determined by the mode ('b' present or not), 
> 
> buffer arg, and maybe something else. You can look in the io chapter or 
> 
> use dir() and help() as John G. suggested.
> 
> 
> 
> Python programmers should really learn to use dir(), help(), and the 
> 
> manuls, including the index and module index.
> 
> 
> 
> --
> 
> Terry Jan Reedy

Yeah. This is an answer. A lot of thanks.
-- 
http://mail.python.org/mailman/listinfo/python-list


Style question -- plural of class name?

2013-05-08 Thread Roy Smith
FooEntry is a class.  How would you describe a list of these in a
docstring?

"A list of FooEntries"

"A list of FooEntrys"

"A list of FooEntry's"

"A list of FooEntry instances"

The first one certainly sounds the best, but it seems wierd to change
the spelling of the class name to make it plural.
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Style question -- plural of class name?

2013-05-08 Thread Skip Montanaro
This one:

> "A list of FooEntry instances"

Besides the obvious spelling issues in the others, it's not
immediately clear if the list contains just FooEntry instances,
FooEntry classes (perhaps subclasses) or a mix of the two.  #4 makes
it clear.

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


Re: Style question -- plural of class name?

2013-05-08 Thread John Downs
On Wed, May 8, 2013 at 4:20 PM, Roy Smith  wrote:

> FooEntry is a class.  How would you describe a list of these in a
> docstring?
>
> "A list of FooEntries"
>
> "A list of FooEntrys"
>
> "A list of FooEntry's"
>
> "A list of FooEntry instances"
>
> The first one certainly sounds the best, but it seems wierd to change
> the spelling of the class name to make it plural.
> --
> http://mail.python.org/mailman/listinfo/python-list
>

How about: "A list with elements of type FooEntry"?  I also like the last
one: "A list of FooEntry instances".
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Style question -- plural of class name?

2013-05-08 Thread Ian Kelly
On Wed, May 8, 2013 at 2:37 PM, John Downs  wrote:
> On Wed, May 8, 2013 at 4:20 PM, Roy Smith  wrote:
>>
>> FooEntry is a class.  How would you describe a list of these in a
>> docstring?
>>
>> "A list of FooEntries"
>>
>> "A list of FooEntrys"
>>
>> "A list of FooEntry's"
>>
>> "A list of FooEntry instances"
>>
>> The first one certainly sounds the best, but it seems wierd to change
>> the spelling of the class name to make it plural.
>> --
>> http://mail.python.org/mailman/listinfo/python-list
>
>
> How about: "A list with elements of type FooEntry"?  I also like the last
> one: "A list of FooEntry instances".

list

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


Globally available I/O connection (K8055 I/O board)

2013-05-08 Thread flexage
I'm having a bit of an issue trying to make a globally available connection to 
my Velleman K8055 I/O board...

I've documented my issue as best I can here: 
http://stackoverflow.com/questions/16449706/python-access-global-instance-of-connection

Could anybody shed some light on a way to combat my problem?
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: cello library

2013-05-08 Thread Terry Jan Reedy

On 5/8/2013 6:01 AM, Chris Angelico wrote:

On Wed, May 8, 2013 at 7:39 PM, Mark Lawrence  wrote:

Hi folks,

I thought some of you might find this interesting [link redacted]


Unredacted: http://libcello.org/


If this is a legit post, can you please elaborate on just _why_ we
would find it interesting? I'm leery of clicking random links like
that without at least some indication. It could be some cool Python
library, or it could be something relating to music, or it could be a
malicious site ready to compromise my browser and your Yahoo account
was compromised to send spam.


Legitimate request, like some I have made of others. Since I trust Mark:

"Cello is a GNU99 C library which brings higher level programming to C.

Interfaces allow for structured design
Duck Typing allows for generic functions
Exceptions control error handling
Constructors/Destructors aid memory management
Syntactic Sugar increases readability
C Library means excellent performance and integration
...Cello is licensed under BSD3."

Partly inspired by Python, including this:

 /* "with" automatically closes file at end of scope. */
  with (file in open($(File, NULL), "prices.bin", "wb"))) {
...
  }

An interesting question is whether it could be used to convert or 
rewrite Python to C.


__
Terry Jan Reedy


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


Re: Globally available I/O connection (K8055 I/O board)

2013-05-08 Thread Dave Angel

On 05/08/2013 04:50 PM, flex...@gmail.com wrote:

I'm having a bit of an issue trying to make a globally available connection to 
my Velleman K8055 I/O board...

I've documented my issue as best I can here: 
http://stackoverflow.com/questions/16449706/python-access-global-instance-of-connection



I don't think that's a Python question at all.  You have some constraint 
your hardware gives you that requires you to run function k8055(0) once, 
and to use that result object to access the board subsequently.  Looks 
to me like you're doing exactly that.  Are there any other calls to that 
function in your code?


If you were messing up with your access to the global object 
globalK8055, you'd get an exception.


The only Python mistake I can think of that you might be doing is if 
you're using your script as a module, or otherwise doing circular 
imports, or if you're accessing some module under more than one name.


In particular, if you run SmartyPi/appglobals.py as a script, it'd be 
calling the k8055() function once and saving the value.  And then when 
somebody imports it as

from smartypi.appglobals import globalK8055 as k

they'd get a NEW instance of the module and a new value for globalK8055.

If you have some form of logging mechanism (including print), you could 
record each time the k8055() is called.



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


Re: Style question -- plural of class name?

2013-05-08 Thread Denis McMahon
On Wed, 08 May 2013 16:20:48 -0400, Roy Smith wrote:

> FooEntry is a class.  How would you describe a list of these in a
> docstring?
> 
> "A list of FooEntries"
> 
> "A list of FooEntrys"
> 
> "A list of FooEntry's"
> 
> "A list of FooEntry instances"
> 
> The first one certainly sounds the best, but it seems wierd to change
> the spelling of the class name to make it plural.

I wouldn't use an apostrophe for pluralisation.

The Normal pluralisation of FooEntry would be FooEntries. Who are you 
expecting to read the docstring? English majors, grammar nazis, wikipedia 
editors, programmers, or all 4?

-- 
Denis McMahon, denismfmcma...@gmail.com
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Get filename using filefialog.askfilename

2013-05-08 Thread Dave Angel

On 05/08/2013 04:14 PM, cheirasa...@gmail.com wrote:

El martes, 7 de mayo de 2013 23:53:32 UTC+2, Terry Jan Reedy  escribió:

On 5/7/2013 4:27 PM, cheirasa...@gmail.com wrote:







Yeah. This is an answer. A lot of thanks.



For a moment there, I thought you were being sarcastic, and ungrateful 
at that.  But I figured that it must have been my imagination.


Usually, it's better to teach a man to fish, rather than just handing 
him one.  But some would rather just have an answer, but not the 
understanding of how to acquire it.


So, some unsolicited advice.  And mixed in, maybe the actual answer to 
your original question.


1) Lose googlegroups, or figure a way to avoid its bad habits.  Your 
responses here are doublespaced, which makes it hard to read, especially 
when you include so much that has nothing to do with your response. 
Also, triple posting leads to a number of problems, not the least of 
which is the number of other responders who killfile anything from 
googlegroups.  See http://wiki.python.org/moin/GoogleGroupsPython.


2) Include enough of your code that people can actually figure out what 
you're up to.  You're asking a question about a method of an object 
filedialog, but you never show how it's created.


3) Include enough of your error messages or diagnostic prints that we 
can see what's going on.


> output is: <..name="file.doc"...mode=..encoding..  >

You elided the only important part of that line, the type of the object 
you're asking about.  Most of us would know that a file object has an 
attribute of name.  But instead I spent quite a while trying to find 
what GUI library you were using that might be handing back something 
representing a file.


4) include the environment you're running in.  In your first thread, you 
included a line:

   Using Windows 7, Python 3.3, sfml 1.3.0 library, ...

Very helpful.  But no such hints here.

5) try not to override builtin names with your own local variables.  In 
this case, you defined a local variable called file, which overwrites, 
presumably by coincidence, its own type name.



Thanks for pointing out the "annotated source code" on fossies.org.  I 
wasn't aware of that location.






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


Re: Red Black Tree implementation?

2013-05-08 Thread duncan smith

On 07/05/13 02:20, Dan Stromberg wrote:


On Mon, May 6, 2013 at 5:55 PM, duncan smith mailto:buzzard@invalid.invalid>> wrote:




[snip]



I'd prefer Apache or MIT or BSD 3-clause, but I could probably work with
this.
http://joinup.ec.europa.eu/community/eupl/news/licence-proliferation-way-out

I'm eager to see the code, and would love it if you sorted out the
deletion rebalance issue.

I just plunked some time into
https://github.com/headius/redblack/blob/master/red_black_tree.py , only
to find that it didn't appear to be doing deletions correctly - the tree
would become unprintable after deleting one element.  It's possible I
introduced the bug, but right now I don't particularly suspect so,
having not changed the __del__ method.

I'm starting to think Red Black Trees are pretty complex.




Mine is fixed now (sent to your gmail address). Restoring the tree 
properties after deletion is awkward to get right, and doesn't affect 
the performance noticeably for smallish trees if you get it wrong.


I realised my code was buggy when I tried adding, then removing a 
million items and ran into the recursion limit. It now passes a test 
where I check the tree properties after each addition / deletion.


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


help on Implementing a list of dicts with no data pattern

2013-05-08 Thread rlelis
Hi guys,

I'm working on this long file, where i have to keep reading and
storing different excerpts of text (data) in different variables (list).

Once done that i want to store in dicts the data i got from the lists mentioned 
before. I want them on a list of dicts for later RDBMs purpose's.

The data i'm working with, don't have fixed pattern (see example bellow), so 
what i'm doing is for each row, i want to store combinations of  word/value 
(Key-value) to keep track of all the data.

My problem is that once i'm iterating over the list (original one a.k.a 
file_content in the link), then i'm nesting several if clause to match
the keys i want. Done that i select the keys i want to give them values and 
lastly i append that dict into a new list. The problem here is that i end up 
always with the last line repeated several times for each row it found's.

Please take a look on what i have now:
http://pastebin.com/A9eka7p9
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Style question -- plural of class name?

2013-05-08 Thread Steven D'Aprano
On Wed, 08 May 2013 16:20:48 -0400, Roy Smith wrote:

> FooEntry is a class.  How would you describe a list of these in a
> docstring?


Which language are you writing your docstrings in? Obey the normal rules 
of spelling, grammar and punctuation for your language, which I assume is 
English.


> "A list of FooEntries"

Perfectly acceptable.


> "A list of FooEntrys"

There is no standard variant or dialect of English (British English, 
American English, etc.) that pluralises Entry as Entrys, so that would be 
"absolutely not".


> "A list of FooEntry's"

"Here come's an S! Quick, jam on an apostrophe!"

This is called the grocer's apostrophe, and is universally held in 
contempt no matter what variant of English you write in. Don't do this.

The only acceptable use of an apostrophe to make a plural is if the thing 
being pluralised is a single letter. E.g. one a, two a's.

 
> "A list of FooEntry instances"

This is also acceptable, although a little wordy. Do you write "a list of 
strings" or "a list of str instances"?


> The first one certainly sounds the best, but it seems wierd to change
> the spelling of the class name to make it plural.

No weirder (note spelling) than changing any other noun. Whether you 
change "int" to "ints" or FooEntry" to "FooEntries", you're still 
changing it. That's how you make it plural.


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


Re: cello library

2013-05-08 Thread Chris Angelico
On Thu, May 9, 2013 at 7:06 AM, Terry Jan Reedy  wrote:
> Legitimate request, like some I have made of others. Since I trust Mark:

It's actually nothing to do with trusting Mark. I've often seen posts
from addresses of people I trust, but with links that I definitely
would not want to click... and Mark posts from Yahoo, which is one of
the common targets of that sort of attack.

Anyway, a little context would have solved the whole thing.

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


Re: help on Implementing a list of dicts with no data pattern

2013-05-08 Thread Dave Angel

On 05/08/2013 07:47 PM, rlelis wrote:

Hi guys,



Please read this http://wiki.python.org/moin/GoogleGroupsPython.



I'm working on this long file, where i have to keep reading and
storing different excerpts of text (data) in different variables (list).

Once done that i want to store in dicts the data i got from the lists mentioned 
before. I want them on a list of dicts for later RDBMs purpose's.

The data i'm working with, don't have fixed pattern (see example bellow), so 
what i'm doing is for each row, i want to store combinations of  word/value 
(Key-value) to keep track of all the data.

My problem is that once i'm iterating over the list (original one a.k.a 
file_content in the link), then i'm nesting several if clause to match
the keys i want. Done that i select the keys i want to give them values and 
lastly i append that dict into a new list. The problem here is that i end up 
always with the last line repeated several times for each row it found's.

Please take a look on what i have now:
http://pastebin.com/A9eka7p9



If you want a response here, please post the code here.  If it's over 50 
lines, simplify it first.  And be sure and tell us what version of 
Python and what other dependencies you might have.  And what OS you're 
targeting.



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


Re: Style question -- plural of class name?

2013-05-08 Thread Chris Angelico
On Thu, May 9, 2013 at 6:20 AM, Roy Smith  wrote:
> "A list of FooEntry's"

Only if you put another apostrophe in:

"A list of 'FooEntry's"

But the delimited style is almost never of use. I'd go for this only
if there were some sort of automated markup being applied - if the
word FooEntry were turned into a hyperlink or something.

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


Re: help on Implementing a list of dicts with no data pattern

2013-05-08 Thread MRAB

On 09/05/2013 00:47, rlelis wrote:

Hi guys,

I'm working on this long file, where i have to keep reading and
storing different excerpts of text (data) in different variables (list).

Once done that i want to store in dicts the data i got from the lists mentioned 
before. I want them on a list of dicts for later RDBMs purpose's.

The data i'm working with, don't have fixed pattern (see example bellow), so 
what i'm doing is for each row, i want to store combinations of  word/value 
(Key-value) to keep track of all the data.

My problem is that once i'm iterating over the list (original one a.k.a 
file_content in the link), then i'm nesting several if clause to match
the keys i want. Done that i select the keys i want to give them values and 
lastly i append that dict into a new list. The problem here is that i end up 
always with the last line repeated several times for each row it found's.

Please take a look on what i have now:
http://pastebin.com/A9eka7p9


You're creating a dict for highway_dict and a dict for aging_dict, and
then using those dicts for every iteration of the 'for' loop.

You're also appending both of the dicts onto the 'queue_row' list for
every iteration of the 'for' loop.

I think that what you meant to do was, for each match, to create a
dict, populate it, and then append it to the list.
--
http://mail.python.org/mailman/listinfo/python-list


Re: Style question -- plural of class name?

2013-05-08 Thread Steven D'Aprano
On Wed, 08 May 2013 15:33:07 -0500, Skip Montanaro wrote:

> This one:
> 
>> "A list of FooEntry instances"
> 
> Besides the obvious spelling issues in the others, it's not immediately
> clear if the list contains just FooEntry instances, FooEntry classes
> (perhaps subclasses) or a mix of the two.  #4 makes it clear.


I don't think this is a real issue. There isn't normally any ambiguity 
between instances and subclasses. When you read "a list of ints", do you 
assume that the list looks like [int, MyInt, AnotherInt, FooInt] or do 
you expect it to look like [2, 7, 6, 1]?

The normal interpretation of "one or more Foo" is that we're talking 
about Foo *instances*, not subclasses of Foo. If that is not that case, 
then the onus is on the author of the documentation to make it clear that 
they are talking about subclasses.


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


Re: Making safe file names

2013-05-08 Thread Roy Smith
In article ,
 Dennis Lee Bieber  wrote:

> On Tue, 07 May 2013 18:10:25 -0500, Andrew Berg
>  declaimed the following in
> gmane.comp.python.general:
> 
> > None of these would work because I would have no idea which file stores 
> > data for which artist without writing code to figure it out. If I
> > were to end up writing a bug that messed up a few of my cache files and 
> > noticed it with a specific artist (e.g., doing a "now playing" and
> > seeing the wrong tags), I would either have to manually match up the hash 
> > or base64 encoding in order to delete just that file so that it
> > gets regenerated or nuke and regenerate my entire cache.
> >
>   And now you've seen why music players don't show the user the
> physical file name, but maintain a database mapping the internal data
> (name, artist, track#, album, etc.) to whatever mangled name was needed
> to satisfy the file system.

Yup.  At Songza, we deal with this crap every day.  It usually bites us 
the worst when trying to do keyword searches.  When somebody types in 
"Blue Oyster Cult", they really mean "Blue Oyster Cult", and our search 
results need to reflect that.  Likewise for Ke$ha, Beyonce, and I don't 
even want to think about the artist formerly known as an unpronounceable 
glyph.

Pro-tip, guys.  If you want to form a band, and expect people to be able 
to find your stuff in a search engine some day, don't play cute with 
your name.
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Making safe file names

2013-05-08 Thread Chris Angelico
On Thu, May 9, 2013 at 10:16 AM, Roy Smith  wrote:
> Pro-tip, guys.  If you want to form a band, and expect people to be able
> to find your stuff in a search engine some day, don't play cute with
> your name.

It's the modern equivalent of names like Catherine Withekay.

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


Re: Style question -- plural of class name?

2013-05-08 Thread Colin J. Williams

On 08/05/2013 4:20 PM, Roy Smith wrote:

FooEntry is a class.  How would you describe a list of these in a
docstring?

"A list of FooEntries"  0

"A list of FooEntrys"   -1

"A list of FooEntry's"  +1

"A list of FooEntry instances"  No FooEntry is specified as a class.

The first one certainly sounds the best, but it seems wierd to change
the spelling of the class name to make it plural.



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


Re: Style question -- plural of class name?

2013-05-08 Thread Cameron Simpson
On 09May2013 00:02, Steven D'Aprano  
wrote:
| On Wed, 08 May 2013 16:20:48 -0400, Roy Smith wrote:
| > "A list of FooEntry's"
| 
| "Here come's an S! Quick, jam on an apostrophe!"
| 
| This is called the grocer's apostrophe, and is universally held in 
| contempt no matter what variant of English you write in. Don't do this.
| 
| The only acceptable use of an apostrophe to make a plural is if the thing 
| being pluralised is a single letter. E.g. one a, two a's.

Frankly, not even then for me. I spell that "one A, two As".

| > "A list of FooEntry instances"
| 
| This is also acceptable, although a little wordy. Do you write "a list of 
| strings" or "a list of str instances"?

How about "a FooEntry list"?
-- 
Cameron Simpson 

Yes Officer, yes Officer, I will Officer. Thank you.
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Red Black Tree implementation?

2013-05-08 Thread Dan Stromberg
OK, I've got one copy of trees.py with md5
211f80c0fe7fb9cb42feb9645b4b3ffe.  You seem to be saying I should have
two though, but I don't know that I do...


On 5/8/13, duncan smith  wrote:
> On 07/05/13 02:20, Dan Stromberg wrote:
>>
>> On Mon, May 6, 2013 at 5:55 PM, duncan smith > > wrote:
>>
>>
>
> [snip]
>
>>
>> I'd prefer Apache or MIT or BSD 3-clause, but I could probably work with
>> this.
>> http://joinup.ec.europa.eu/community/eupl/news/licence-proliferation-way-out
>>
>> I'm eager to see the code, and would love it if you sorted out the
>> deletion rebalance issue.
>>
>> I just plunked some time into
>> https://github.com/headius/redblack/blob/master/red_black_tree.py , only
>> to find that it didn't appear to be doing deletions correctly - the tree
>> would become unprintable after deleting one element.  It's possible I
>> introduced the bug, but right now I don't particularly suspect so,
>> having not changed the __del__ method.
>>
>> I'm starting to think Red Black Trees are pretty complex.
>>
>>
>
> Mine is fixed now (sent to your gmail address). Restoring the tree
> properties after deletion is awkward to get right, and doesn't affect
> the performance noticeably for smallish trees if you get it wrong.
>
> I realised my code was buggy when I tried adding, then removing a
> million items and ran into the recursion limit. It now passes a test
> where I check the tree properties after each addition / deletion.
>
> Duncan
> --
> http://mail.python.org/mailman/listinfo/python-list
>
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Making safe file names

2013-05-08 Thread Steven D'Aprano
On Wed, 08 May 2013 20:16:25 -0400, Roy Smith wrote:

> Yup.  At Songza, we deal with this crap every day.  It usually bites us
> the worst when trying to do keyword searches.  When somebody types in
> "Blue Oyster Cult", they really mean "Blue Oyster Cult", 

Surely they really mean Blue Öyster Cult.


> and our search
> results need to reflect that.  Likewise for Ke$ha, Beyonce, and I don't
> even want to think about the artist formerly known as an unpronounceable
> glyph.

Dropped or incorrect accents are no different from any other misspelling, 
and good search engines (whether online or in a desktop application) 
should be able to deal with a tolerable number of misspellings.

Googling for "Blue Oyster Cult" brings up four of the top ten hits 
spelled correctly with the accent, "Blue Öyster Cult". Even misspelled as 
"blew oytser cult", Google does the right thing.

Even Bing manages to find Ke$ha's wikipedia page, her official website, 
youtube channel, facebook and myspace pages from the misspelling "kehsha".



> Pro-tip, guys.  If you want to form a band, and expect people to be able
> to find your stuff in a search engine some day, don't play cute with
> your name.

Googling for "the the" (including quotes) brings up 145 million hits, 
nine of the first ten hits being relevant to the band. 

On the other hand, I wouldn't want to be in a band called "The Beetles".


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


Re: Making safe file names

2013-05-08 Thread Roy Smith
In article <518b00a2$0$29997$c3e8da3$54964...@news.astraweb.com>,
 Steven D'Aprano  wrote:

> > When somebody types in
> > "Blue Oyster Cult", they really mean "Blue Oyster Cult", 
> 
> Surely they really mean Blue Öyster Cult.

Yes.  The oomlaut was there when I typed it.  Who knows what happened to 
it by the time it hit the wire.
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Making safe file names

2013-05-08 Thread Andrew Berg
On 2013.05.08 19:16, Roy Smith wrote:
> Yup.  At Songza, we deal with this crap every day.  It usually bites us 
> the worst when trying to do keyword searches.  When somebody types in 
> "Blue Oyster Cult", they really mean "Blue Oyster Cult", and our search 
> results need to reflect that.  Likewise for Ke$ha, Beyonce, and I don't 
> even want to think about the artist formerly known as an unpronounceable 
> glyph.
>
> Pro-tip, guys.  If you want to form a band, and expect people to be able 
> to find your stuff in a search engine some day, don't play cute with 
> your name.
It's a thing (especially in witch house) to make names with odd glyphs in order 
to be harder to find and be more "underground". Very silly.
Try doing searches for these artists with names like these:
http://www.last.fm/music/%E2%96%BC%E2%96%A1%E2%96%A0%E2%96%A1%E2%96%A0%E2%96%A1%E2%96%A0
http://www.last.fm/music/ki%E2%80%A0%E2%80%A0y+c%E2%96%B2t
-- 
CPython 3.3.1 | Windows NT 6.2.9200 / FreeBSD 9.1
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Message passing syntax for objects | OOPv2

2013-05-08 Thread Mark Janssen
On Fri, Apr 12, 2013 at 11:28 PM, Mark Janssen
 wrote:
>> Mark, this proposal is out of place on a Python list, because it proposes an
>> object methodology radically different from any that is implemented in
>> Python now, or is even remotely likely to be implemented in Python in the
>> future.
>
> Wow, you guys are a bunch of ninnies.  I'm going to find some
> theoretical folks

Okay, to anyone who might be listening, I found the core of the problem.

This issue is/was much deeper than OOP (which would be roughly a 20
year refactoring) -- that was my mistake.  The issue goes right to the
core to models of computation and the historical factions within
theoretical CS itself (a 50+ year refactoring).

The field needs re-invented and re-centered.  Mark my words.  There
has been a half-century of confusion between two entirely separate
domains and they've been using the same lexicon.  Long story short:
the lambda calculus folks have to split from the Turing machine folks.
 These models of computation should not use the same language.  Their
computation models are too radically different.  Lisp will remain a
pinnacle of the lambda calculus, but should be remanded to philosophy.
 The logic of the binary/boolean arithmetic is simply not compatible,
but forms the basis of any sensible computer science here in the West.

Here pronouncith the whatever

-- 
MarkJ
Tacoma, Washington
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: object.enable() anti-pattern

2013-05-08 Thread Dan Sommers
On Wed, 08 May 2013 14:27:53 +, Duncan Booth wrote:

> Steven D'Aprano  wrote:

>> I'm looking for some help in finding a term, it's not Python-specific
>> but does apply to some Python code.
>> 
>> This is an anti-pattern to avoid. The idea is that creating a
>> resource ought to be the same as "turning it on", or enabling it, or
>> similar
> 
> I've come across this under the name 'two-phase construction', but as
> a desirable(!?) pattern rathern than as an anti-pattern.

Think of spinning off a thread:  initialize it synchronously, and then
let it execute asynchronously.  We tend towards that pattern even when
we know that execution will be synchronous, because we also that it
could be asynchronous one day.  Yes, we could wrap that up in a neat
bundle, but explicit is better than implicit.

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


Re: object.enable() anti-pattern

2013-05-08 Thread Dan Sommers
On Wed, 08 May 2013 08:52:12 +, Steven D'Aprano wrote:

> This is an anti-pattern to avoid. The idea is that creating a resource 
> ought to be the same as "turning it on", or enabling it, or similar. For 
> example, we don't do this in Python:
> 
> f = file("some_file.txt")
> f.open()
> data = f.read()

So why don't we do this?

data = read("some_file.txt")

> because reading the file can fail if you forget to call open first. 
> Instead, Python uses a factory function that creates the file object and 
> opens it:
> 
> f = open("some_file.txt")  # if this succeeds, f is ready to use
> data = f.read()

That's just the "enable" paradigm in a socially acceptable and
traditional wrapper.

Opening and closing the file is an implementation detail, often defeated
by caching (application level or OS level) anyway.

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


Re: object.enable() anti-pattern

2013-05-08 Thread Mark Janssen
> I'm looking for some help in finding a term, it's not Python-specific but
> does apply to some Python code.
>
> This is an anti-pattern to avoid. The idea is that creating a resource
> ought to be the same as "turning it on", or enabling it, or similar. For
> example, we don't do this in Python:

I would call it "user-mediated resource allocation" as distinct from
"object-mediated" resource allocation.
-- 
MarkJ
Tacoma, Washington
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: object.enable() anti-pattern

2013-05-08 Thread Mark Janssen
>> This is an anti-pattern to avoid. The idea is that creating a resource
>> ought to be the same as "turning it on", or enabling it, or similar. For
>> example, we don't do this in Python:
>
> I would call it "user-mediated resource allocation" as distinct from
> "object-mediated" resource allocation.

I should point out, though, it's not really an anti-pattern (coming
form the C community).

-- 
MarkJ
Tacoma, Washington
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Making safe file names

2013-05-08 Thread Steven D'Aprano
On Wed, 08 May 2013 21:11:28 -0500, Andrew Berg wrote:

> It's a thing (especially in witch house) to make names with odd glyphs
> in order to be harder to find and be more "underground". Very silly. Try
> doing searches for these artists with names like these:

Challenge accepted.

> http://www.last.fm/music/%E2%96%BC%E2%96%A1%E2%96%A0%E2%96%A1%E2%96%A0%
E2%96%A1%E2%96%A0
> http://www.last.fm/music/ki%E2%80%A0%E2%80%A0y+c%E2%96%B2t


The second one is trivial. Googling for "kitty cat" "witch 
house" (including quotes) gives at least 3 relevant links out of the top 
4 hits are relevant. (I'm not sure about the Youtube page.) That gets you 
the correct spelling, "ki††y c△t", and googling for that brings up many 
more hits.

The first one is a tad trickier, since googling for "▼□■□■□■" brings up 
nothing at all, and "mourning star" doesn't give any relevant hits on the 
first page. But "mourning star" "witch house" (inc. quotes) is successful.

I suspect that the only way to be completely ungoogleable would be to 
name yourself something common, not something obscure. Say, if you called 
yourself "Hard Rock Band", and did hard rock. But then, googling for 
"Heavy Metal" alone brings up the magazine as the fourth hit, so if you 
get famous enough, even that won't work.



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


Re: object.enable() anti-pattern

2013-05-08 Thread Steven D'Aprano
On Wed, 08 May 2013 14:27:53 +, Duncan Booth wrote:

> Steven D'Aprano  wrote:
> 
>> I'm looking for some help in finding a term, it's not Python-specific
>> but does apply to some Python code.
>> 
>> This is an anti-pattern to avoid. The idea is that creating a resource
>> ought to be the same as "turning it on", or enabling it, or similar
> 
> I've come across this under the name 'two-phase construction', but as a
> desirable(!?) pattern rathern than as an anti-pattern.
> 
> In particular Symbian used it throughout as originally their C++
> implementation didn't support exceptions. Instead they had a separate
> cleanup stack and objects that require cleanup were pushed onto that
> stack after being fully constructed but before calling the
> initialisation that required cleanup. See
> http://www.developer.nokia.com/Community/Wiki/Two-phase_construction



Thanks for the link. It's obviously not the blog post I was looking for, 
but it is interesting.



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


Re: Message passing syntax for objects | OOPv2

2013-05-08 Thread rusi
On May 9, 7:35 am, Mark Janssen  wrote:
> On Fri, Apr 12, 2013 at 11:28 PM, Mark Janssen
>
>  wrote:
> >> Mark, this proposal is out of place on a Python list, because it proposes 
> >> an
> >> object methodology radically different from any that is implemented in
> >> Python now, or is even remotely likely to be implemented in Python in the
> >> future.
>
> > Wow, you guys are a bunch of ninnies.  I'm going to find some
> > theoretical folks
>
> Okay, to anyone who might be listening, I found the core of the problem.
>
> This issue is/was much deeper than OOP (which would be roughly a 20
> year refactoring) -- that was my mistake.  The issue goes right to the
> core to models of computation and the historical factions within
> theoretical CS itself (a 50+ year refactoring).
>
> The field needs re-invented and re-centered.  Mark my words.  There
> has been a half-century of confusion between two entirely separate
> domains and they've been using the same lexicon.  Long story short:
> the lambda calculus folks have to split from the Turing machine folks.
>  These models of computation should not use the same language.  Their
> computation models are too radically different.  Lisp will remain a
> pinnacle of the lambda calculus, but should be remanded to philosophy.
>  The logic of the binary/boolean arithmetic is simply not compatible,
> but forms the basis of any sensible computer science here in the West.
>
> Here pronouncith the whatever
>
> --
> MarkJ
> Tacoma, Washington

"Lisp will remain the pinnacle of lambda calculus" ???  : Surreal
feeling of falling into a 25-year time-warp

Read this http://www.cs.kent.ac.uk/people/staff/dat/miranda/wadler87.pdf

Just for historical context:
When this was written in the 80s:
- The FP languages of the time -- KRC, SASL, Miranda, Orwell -- were
elegant and academic
- Lisp was quasi-industrial-strength but as Wadler argues above, was
not doing good service to functional programming
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Message passing syntax for objects | OOPv2

2013-05-08 Thread Mark Janssen
> "Lisp will remain the pinnacle of lambda calculus" ???  : Surreal
> feeling of falling into a 25-year time-warp
>
> Read this http://www.cs.kent.ac.uk/people/staff/dat/miranda/wadler87.pdf
>
> Just for historical context:
> When this was written in the 80s:
> - The FP languages of the time -- KRC, SASL, Miranda, Orwell -- were
> elegant and academic
> - Lisp was quasi-industrial-strength but as Wadler argues above, was
> not doing good service to functional programming

Fascinating.
-- 
MarkJ
Tacoma, Washington
-- 
http://mail.python.org/mailman/listinfo/python-list


Anybody familiar with pygments ?

2013-05-08 Thread dabaichi

Hello guys:
pygments version: 1.6
python version :3.3.1(64bits, use msi installer install)
os:windows 7 (64bits)
Hereis my python source code:

#!/usr/bin/env python
#-*- coding=utf-8 -*-

import os
import sys
from pygments import highlight as html_highlight
from pygments.lexers import get_lexer_by_name
from pygments.formatters import HtmlFormatter
from pygments.styles import get_style_by_name

def highlight(filename):
   formatter = HtmlFormatter(linenos=True, style="vim")
   filename_split = filename.split('.')

   if len(filename_split) == 1:
   return None

   lexer = get_lexer_by_name(filename_split[len(filename_split) - 1])

   try:
   with open(filename, 'r') as fp:
   content = fp.read()
   except Exception:
   return None

   html_content = html_highlight(content, lexer, formatter)
   return html_content

def main():
   filename = r'test.c'
   print(highlight(filename))

if __name__ == "__main__":
   main()


Here is test.c source code:
#include 

int main(int argc, char * argv[])
{
   printf("This is a test!\n");
   return 0;
}

And hereis the output file:
class="linenodiv">1

2
3
4
5
6
7class="cp">#include 


int mainclass="p">(int class="n">argc, char 
* argvclass="p">[])

{
   printf(class="s">"This is a test!\nclass="s">");
   return 0class="p">;

}



It looks like:
1 #include 
2
3 int main(int argc, char * argv[])
4 {
5 printf("This is a test!\n");
6 return 0;
7 }
8
with no color in firefox (20.0.1)

I want to know why output html file with no color ?
Is my code error ?

Best regard!
--peipei 



--- news://freenews.netfront.net/ - complaints: n...@netfront.net ---
--
http://mail.python.org/mailman/listinfo/python-list


Re: Red Black Tree implementation?

2013-05-08 Thread duncan smith

On 09/05/13 02:40, Dan Stromberg wrote:

OK, I've got one copy of trees.py with md5
211f80c0fe7fb9cb42feb9645b4b3ffe.  You seem to be saying I should have
two though, but I don't know that I do...




I've just re-sent it.

Duncan

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


Re: object.enable() anti-pattern

2013-05-08 Thread Steven D'Aprano
On Thu, 09 May 2013 02:42:01 +, Dan Sommers wrote:

> On Wed, 08 May 2013 08:52:12 +, Steven D'Aprano wrote:
> 
>> This is an anti-pattern to avoid. The idea is that creating a resource
>> ought to be the same as "turning it on", or enabling it, or similar.
>> For example, we don't do this in Python:
>> 
>> f = file("some_file.txt")
>> f.open()
>> data = f.read()
> 
> So why don't we do this?
> 
> data = read("some_file.txt")

Because there is a lot more that you might want to do to a file than just 
read from it.

py> f = open('/tmp/x')
py> dir(f)
['_CHUNK_SIZE', '__class__', '__delattr__', '__dict__', '__dir__', 
'__doc__', '__enter__', '__eq__', '__exit__', '__format__', '__ge__', 
'__getattribute__', '__getstate__', '__gt__', '__hash__', '__init__', 
'__iter__', '__le__', '__lt__', '__ne__', '__new__', '__next__', 
'__reduce__', '__reduce_ex__', '__repr__', '__setattr__', '__sizeof__', 
'__str__', '__subclasshook__', '_checkClosed', '_checkReadable', 
'_checkSeekable', '_checkWritable', 'buffer', 'close', 'closed', 
'detach', 'encoding', 'errors', 'fileno', 'flush', 'isatty', 
'line_buffering', 'mode', 'name', 'newlines', 'read', 'readable', 
'readline', 'readlines', 'seek', 'seekable', 'tell', 'truncate', 
'writable', 'write', 'writelines']


That's 24 public methods and attributes, excluding private and dunder 
attributes.

There is no sensible use-case for creating a file without opening it. 
What would be the point? Any subsequent calls to just about any method 
will fail. Since you have to open the file after creating the file object 
anyway, why make them two different calls?

Besides, this is not to denigrate the idea of a read() function that 
takes a filename and returns its contents. But that is not an object 
constructor. It may construct a file object internally, but it doesn't 
return the file object, so it is completely unrelated to the scenario I 
described.



>> because reading the file can fail if you forget to call open first.
>> Instead, Python uses a factory function that creates the file object
>> and opens it:
>> 
>> f = open("some_file.txt")  # if this succeeds, f is ready to use 
>> data = f.read()
> 
> That's just the "enable" paradigm in a socially acceptable and
> traditional wrapper.

Well duh. That's like saying that for loops, while loops and co-routines 
are just GOTO in a socially acceptable and traditional wrapper. Of course 
they are. That's the whole point.



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


Re: object.enable() anti-pattern

2013-05-08 Thread Steven D'Aprano
On Thu, 09 May 2013 02:38:36 +, Dan Sommers wrote:

> Think of spinning off a thread:  initialize it synchronously, and then
> let it execute asynchronously.  We tend towards that pattern even when
> we know that execution will be synchronous, because we also that it
> could be asynchronous one day.

Whether it is synchronous or asynchronous is irrelevant.

I can see use-cases for separating "make it go" from initialisation. It 
all depends on what you might want to do to the object before making it 
go. If the answer is "Nothing", then there is no reason not to have the 
constructor make it go. If the answer is, "Well, sometimes we need to do 
things to the object before making it go", then it makes sense to 
separate the two:

blob = MyBlob(arg1, arg2, agr3)
blob.poke("with a stick")
blob.append(data)
blob.foo = "spam"
blob.make_it_go()


I'm not talking about this case. I'm talking about the case where there's 
nothing you can do with the blob before making it go.


> Yes, we could wrap that up in a neat
> bundle, but explicit is better than implicit.

"And that is why we always write code like this:

n = int(int(len(something)) + int(1))

just to be sure that the result is explicitly an int and not just 
implicitly an int. Suppose some Javascript programmer was reading the 
code, and they thought that 1 was a floating point value. That would be 
bad!"


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


Re: Message passing syntax for objects | OOPv2

2013-05-08 Thread Steven D'Aprano
On Wed, 08 May 2013 19:35:58 -0700, Mark Janssen wrote:

> Long story short: the lambda
> calculus folks have to split from the Turing machine folks.
>  These models of computation should not use the same language.  Their
> computation models are too radically different.  

Their computation models are exactly equivalent.

This is like saying that Cartesian coordinates and polar coordinates are 
so radically different that they cannot possibly both describe the same 
space.


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


Re: object.enable() anti-pattern

2013-05-08 Thread Chris Angelico
On Thu, May 9, 2013 at 3:37 PM, Steven D'Aprano
 wrote:
> I can see use-cases for separating "make it go" from initialisation. It
> all depends on what you might want to do to the object before making it
> go. If the answer is "Nothing", then there is no reason not to have the
> constructor make it go. If the answer is, "Well, sometimes we need to do
> things to the object before making it go", then it makes sense to
> separate the two:
>
> blob = MyBlob(arg1, arg2, agr3)
> blob.poke("with a stick")
> blob.append(data)
> blob.foo = "spam"
> blob.make_it_go()

Example use-case: Most GUI frameworks. You create a window, then
populate it, then show it. When you create the window object in
Python, you expect an actual window to exist, it should have its
handle etc. So it's still the same thing; the object is fully created
in its constructor.

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


Re: Help with implementing callback functions using ctypes

2013-05-08 Thread dieter
jamadagni  writes:
> ...
I cannot help you with "ctypes". But, if you might be able to use
"cython", then calling callbacks is not too difficult
(you can find an example in e.g. my "dm.xmlsec.binding").

Note, however, that properly handling the GIL ("Global Interpreter Lock")
may be of great importance when the Python/C boundary is crossed --
at least when you intend to use your code in a multi thread environment.

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


Append to python List

2013-05-08 Thread RAHUL RAJ
Checkout the following code:

sample2 = [x+y for x in range(1,10) for y in range(1,10) if x!=y]   
output=[]
output=[x for x in sample2 if x not in output]

the output I get is
3 4 5 6 7 8 9 10 3 5 6 7 8 9 10 11 4 5 7 8 9 10 11 12 5 6 7 9 10 11 12 13 6 7 8 
9 11 12 13 14 7 8 9 10 11 13 14 15 8 9 10 11 12 13 15 16 9 10 11 12 13 14 15 17 
10 11 12 13 14 15 16 17

which contains duplicate values.




But if I do like this:

sample2 = [x+y for x in range(1,10) for y in range(1,10) if x!=y]   
output=[]
for x in sample2:
  if x not in output:
 output.append(x)


the value of 'output' I get like this:
3 4 5 6 7 8 9 10 11 12 13 14 15 16 17

I know that both the programs have the same functionality, but why do I have 
different outputs?

Please help!
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: object.enable() anti-pattern

2013-05-08 Thread Terry Jan Reedy

On 5/9/2013 1:23 AM, Steven D'Aprano wrote:


Besides, this is not to denigrate the idea of a read() function that
takes a filename and returns its contents. But that is not an object
constructor. It may construct a file object internally, but it doesn't
return the file object, so it is completely unrelated to the scenario I
described.


At least a few stdlib modules that define classes *also* have such 
functions. They create an instance of the class, call one or more 
methods, and return the result of the method, discarding the instance. 
For instance, see the subprocess module, its POpen class, and module 
functions; or the timeit module, its Timer class, and functions.



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