Re: Multi-dimensional list initialization

2012-11-05 Thread Chris Angelico
On Mon, Nov 5, 2012 at 6:54 PM, Andrew Robinson
 wrote:
> On 11/04/2012 11:27 PM, Chris Angelico wrote:
>>
>> On Mon, Nov 5, 2012 at 6:07 PM, Chris Rebert  wrote:
>>
>> x = None
>> x.a = 42
>>>
>>> Traceback (most recent call last):
>>>File "", line 1, in
>>> AttributeError: 'NoneType' object has no attribute 'a'
>>
>> Python needs a YouGottaBeKiddingMeError for times when you do
>> something utterly insane like this. Attributes of None??!? :)
>>
>> ChrisA
>
> Hmmm? Everything in Python is an object.
> Therefore! SURE. None *does* have attributes! ( even if not useful ones... )
>
> eg: " None.__getattribute__( "__doc__" ) " doesn't produce an error.

Eh, I meant mutating None's attributes, which is just as insane as I said.

> In C, in Linux, at the end of the file "errno.h", where all error codes are
> listed eg:( EIO, EAGAIN, EBUSY, E) They had a final error like the one
> you dreamed up, it was called "EIEIO"; and the comment read something like,
> "All the way around Elmer's barn".

There's been a collection of those around the place. A few memorable ones:

EMILYPOST: Bad fork()
ETOBACCO: Read on empty pipe
EHORSE: Mount failed

I may be misremembering, but I'm sure the originals can be found at
the other end of a web search.

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


Re: python destructor

2012-11-05 Thread Ferencik Ioan
On Monday, November 5, 2012 8:51:00 AM UTC+2, Ferencik Ioan wrote:
> Hello there folks,
> 
> 
> 
> I have a bit of a special issue.
> 
> I'll start by disclosing myself for what i am doing. I am a postgraduate 
> student and I really have good reasons to do what I am doing. At least i 
> think so.
> 
> 
> 
> And not the issue.
> 
> I am building a python web service. This web service has some generic objects 
> and I use a metaclass to customize the classes. 
> 
> Second I use a non-conventional object oriented database, dybase 
> 
> 
> 
> (http://www.garret.ru/dybase/doc/dybase.html#introduction)
> 
> 
> 
> Now these is a OODBMS claiming to support ACID transactions.
> 
> 
> 
> 
> 
> The instances of my objects are recursively organizing themselves into a 
> hierarchical tree-like structure. When I make an instance of this object 
> persistent dybase actually can recursively save all tree structure.
> 
> Everything works well here. 
> 
> 
> 
> I altered the main class situated at the root of my class hierarchy to 
> actually store inside the__dict__ not the instances of its children but their 
> unique ID's. Then when I set a child attribute I create it and instead of 
> being stored in the instance the child goes to a database index object. Thus 
> it becomes Universally addressable. The a parent retrieves the child it 
> actually fetches it from the database. 
> 
> In this way I ended up with very small objects.However these objects can 
> regenerate the treelike structure as if they were storing there children in 
> the __dict__.
> 
> 
> 
> The issue is how to give the instances access to the database and properly 
> handle the opening and closing of the database.
> 
> It seems futile to me to actually open/close the connection through a 
> context. Because the database is a file it will issue an IO operation on 
> every attribute access and we all know __getattribute__ is used extremely 
> often.
> 
> For this reason I thought the best way would be to wrap the dybase Storage 
> (main class) into a local storage version which would have __del__ method.
> 
> The local Storage is a new style class..it opens the DB file but the __del__ 
> is never called.
> 
> This is because the Storage class has at least 2 cyclic references. 
> 
> So my Storage class never closes the database. I would like this class to 
> close the database when it is garbage collected.
> 
> The class is a Singleton FYI as well but this might not be relevant or even 
> necessary.
> 
> So my question is:
> 
> what s the best way to force __del__ on a singleton that has cyclic 
> references. Should i use weakref and alter the original source? Is there a 
> way i can force a singleton to garbage collect itself?.
> 
> 
> 
> I am by no means a software engineer so i would appreciate any advice from 
> some experts on the matter.
> 
> Thank you in advance.

Just in case somebody is interested:
Because my Storage is a singleton I registered the close() method with atexit 
from the Storage open().

This actually closes the connection. Not sure if this is feasible but it WORKS!

I am using mod_wsgi in daemon mode so I have multithreading issues. If I 
configure the mod_wsgi with one process dybase works correctly.
I have to override the Persistent.store() and make it thread safe using 
multiprocessing. This is for ANYONE who uses or plans to use dybase in a web 
environment.
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Multi-dimensional list initialization

2012-11-05 Thread Hans Mulder
On 5/11/12 07:27:52, Demian Brecht wrote:
> So, here I was thinking "oh, this is a nice, easy way to initialize a 4D 
> matrix"
> (running 2.7.3, non-core libs not allowed):
> 
> m = [[None] * 4] * 4
> 
> The way to get what I was after was:
> 
> m = [[None] * 4, [None] * 4, [None] * 4, [None * 4]] 

Or alternateively:

m = [[None] * 4 for _ in range(4)]

> (Obviously, I could have just hardcoded the initialization, but I'm too
> lazy to type all that out ;))
> 
> The behaviour I encountered seems a little contradictory to me.
> [None] * 4 creates four distinct elements in a single array

Actually, it creates a list with four references to the same object.
But then, this object is immutable, so you won't notice that it's the
same object.

> while [[None] * 4] * 4 creates one distinct array of four distinct
> elements, with three references to it:

We usually phrase that as "a list with four references to the
same list".  The first reference is not special in any way.

 a = [None] * 4
 a[0] = 'a'
 a
> ['a', None, None, None]
> 
 m = [[None] * 4] * 4
 m[0][0] = 'm'
 m
> [['m', None, None, None], ['m', None, None, None], ['m', None, None, None], 
> ['m', None, None, None]]
> 
> Is this expected behaviour

Yes.

> and if so, why? In my mind either result makes sense, but the
> inconsistency is what throws me off.

There's no inconsistency: in both cases you get a list with four
references to the same object.  The only difference is that in the
fist case, the references are to an immutable object, so the fact
that it's the same object won't hurt you.


Hope this helps,

-- HansM

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


Re: Multi-dimensional list initialization

2012-11-05 Thread wxjmfauth
Le lundi 5 novembre 2012 07:28:00 UTC+1, Demian Brecht a écrit :
> So, here I was thinking "oh, this is a nice, easy way to initialize a 4D 
> matrix" (running 2.7.3, non-core libs not allowed):
> 
> 
> 
> m = [[None] * 4] * 4
> 
> 
> 
> The way to get what I was after was:
> 
> 
> 
> m = [[None] * 4, [None] * 4, [None] * 4, [None * 4]] 
> 
> 
> 
> (Obviously, I could have just hardcoded the initialization, but I'm too lazy 
> to type all that out ;))
> 
> 
> 
> The behaviour I encountered seems a little contradictory to me. [None] * 4 
> creates four distinct elements in a single array while [[None] * 4] * 4 
> creates one distinct array of four distinct elements, with three references 
> to it:
> 
> 
> 
> >>> a = [None] * 4
> 
> >>> a[0] = 'a'
> 
> >>> a
> 
> ['a', None, None, None]
> 
> 
> 
> >>> m = [[None] * 4] * 4
> 
> >>> m[0][0] = 'm'
> 
> >>> m
> 
> [['m', None, None, None], ['m', None, None, None], ['m', None, None, None], 
> ['m', None, None, None]]
> 
> 
> 
> Is this expected behaviour and if so, why? In my mind either result makes 
> sense, but the inconsistency is what throws me off.
> 
> 
> 
> Demian Brecht
> 
> @demianbrecht
> 
> http://demianbrecht.github.com

--

You probably mean a two-dimensional matrix not a 4D matrix.

>>> def DefMatrix(nrow, ncol, val):
... return [[val] * ncol for i in range(nrow)]
... 
>>> aa = DefMatrix(2, 3, 1.0)
>>> aa
>>> aa = DefMatrix(2, 3, 1.0)
>>> aa
[[1.0, 1.0, 1.0], [1.0, 1.0, 1.0]]
>>> aa[0][0] = 3.14
>>> aa[1][2] = 2.718
>>> aa
[[3.14, 1.0, 1.0], [1.0, 1.0, 2.718]]
>>> 
>>> bb = DefMatrix(2, 3, None)
>>> bb
[[None, None, None], [None, None, None]]
>>> bb[0][0] = 3.14
>>> bb[1][2] = 2.718
>>> bb
[[3.14, None, None], [None, None, 2.718]]


jmf

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


Re: accepting file path or file object?

2012-11-05 Thread Peter Otten
andrea crotti wrote:

> Quite often I find convenient to get a filename or a file object as
> argument of a function, and do something as below:
> 
> def grep_file(regexp, filepath_obj):
> """Check if the given text is found in any of the file lines, take
> a path to a file or an opened file object
> """
> if isinstance(filepath_obj, basestring):
> fobj = open(filepath_obj)
> else:
> fobj = filepath_obj
> 
> for line in fobj:
> if re.search(regexp, line):
> return True
> 
> return False
> 
> 
> This makes it also more convenient to unit-test, since I can just pass
> a StringIO.  But then there are other problems, for example if I pass
> a file object is the caller that has to make sure to close the file
> handle..
> 
> So I'm thinking if it's not just worth to skip the support for file
> objects and only use the filenames, which seems a more robust and
> consistent choice..
> 
> Any comment/suggestions about this?

I sometimes do something like this:

$ cat xopen.py
import re
import sys
from contextlib import contextmanager

@contextmanager
def xopen(file=None, mode="r"):
if hasattr(file, "read"):
yield file
elif file == "-":
if "w" in mode:
yield sys.stdout
else:
yield sys.stdin
else:
with open(file, mode) as f:
yield f

def grep(stream, regex):
search = re.compile(regex).search
return any(search(line) for line in stream)

if len(sys.argv) == 1:
print grep(["alpha", "beta", "gamma"], "gamma")
else:
with xopen(sys.argv[1]) as f:
print grep(f, sys.argv[2])
$ python xopen.py 
True
$ echo 'alpha beta gamma' | python xopen.py - gamma
True
$ echo 'alpha beta gamma' | python xopen.py - delta
False
$ python xopen.py xopen.py context
True
$ python xopen.py xopen.py gamma
True
$ python xopen.py xopen.py delta
False
$ 


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


Re: Obnoxious postings from Google Groups

2012-11-05 Thread Roy Smith
In article ,
 Chris Angelico  wrote:

> It's nothing to do with operating system. File names are names, and
> spaces in them are seldom worth the hassle unless you manipulate those
> files solely using a GUI.

That's a very ascii-esqe attitude.  In a fully unicode world, I could 
easily see using U+00A0 (NO-BREAK SPACE) in file names, and still have 
space-delimited CLI work just fine.

But, yeah, in the world we live in today, I try to avoid spaces in 
filenames.  But, instead of turning "My File Name" into MyFileName, I'll 
usually do it as My-File-Name or My_File_Name.
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: accepting file path or file object?

2012-11-05 Thread andrea crotti
2012/11/5 Peter Otten <[email protected]>:
> I sometimes do something like this:
>
> $ cat xopen.py
> import re
> import sys
> from contextlib import contextmanager
>
> @contextmanager
> def xopen(file=None, mode="r"):
> if hasattr(file, "read"):
> yield file
> elif file == "-":
> if "w" in mode:
> yield sys.stdout
> else:
> yield sys.stdin
> else:
> with open(file, mode) as f:
> yield f
>
> def grep(stream, regex):
> search = re.compile(regex).search
> return any(search(line) for line in stream)
>
> if len(sys.argv) == 1:
> print grep(["alpha", "beta", "gamma"], "gamma")
> else:
> with xopen(sys.argv[1]) as f:
> print grep(f, sys.argv[2])
> $ python xopen.py
> True
> $ echo 'alpha beta gamma' | python xopen.py - gamma
> True
> $ echo 'alpha beta gamma' | python xopen.py - delta
> False
> $ python xopen.py xopen.py context
> True
> $ python xopen.py xopen.py gamma
> True
> $ python xopen.py xopen.py delta
> False
> $
>
>
> --
> http://mail.python.org/mailman/listinfo/python-list

That's nice thanks, there is still the problem of closing the file
handle but that's maybe not so important if it gets closed at
termination anyway..
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: accepting file path or file object?

2012-11-05 Thread Ulrich Eckhardt

Am 05.11.2012 11:54, schrieb andrea crotti:

Quite often I find convenient to get a filename or a file object as
argument of a function, and do something as below:

def grep_file(regexp, filepath_obj):
 """Check if the given text is found in any of the file lines, take
 a path to a file or an opened file object
 """
 if isinstance(filepath_obj, basestring):
 fobj = open(filepath_obj)
 else:
 fobj = filepath_obj

 for line in fobj:
 if re.search(regexp, line):
 return True

 return False

This makes it also more convenient to unit-test, since I can just pass
a StringIO.


I do the same for the same reason, but I either pass in a file object or 
the actual data contained in the file, but not a path.




But then there are other problems, for example if I pass a file

> object is the caller that has to make sure to close the file

handle..


I don't consider that a problem. If you open a file, you should do that 
in a with expression:


  with open(..) as f:
  found = grep_file(regex, f)

That is also the biggest criticism I have with your code, because you 
don't close the file after use. Another things is the readability of 
your code:


  grep_file("foo", "bar")

The biggest problem there is that I don't know which of the two 
arguments is which. I personally would expect the file to come first, 
although the POSIX grep has it opposite on the commandline. Consider as 
alternative:


  grep("foo", path="bar")
  with open(..) as f:
grep("foo", file=f)
  with open(..) as f:
grep("foo", data=f.read())

Using **kwargs, you could switch inside the function depending on the 
mode that was used, extract lines accordingly and match these against 
the regex.



Greetings!

Uli

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


Re: accepting file path or file object?

2012-11-05 Thread Peter Otten
andrea crotti wrote:

> 2012/11/5 Peter Otten <[email protected]>:
>> I sometimes do something like this:

>> @contextmanager
>> def xopen(file=None, mode="r"):
>> if hasattr(file, "read"):
>> yield file
   elif file == "-" or file is None: # add file=None handling
>> if "w" in mode:
>> yield sys.stdout
>> else:
>> yield sys.stdin
>> else:
>> with open(file, mode) as f:
>> yield f

> That's nice thanks, there is still the problem of closing the file
> handle but that's maybe not so important if it gets closed at
> termination anyway..

I think you misunderstood the behaviour of my context manager. The with-
statement (the last two lines) in xopen() implicitly closes files that are 
opened by xopen().

If you pass a file name the file will be closed:

>>> with xopen("xopen.py") as f: pass
... 
>>> f.closed
True

However, if you pass an existing file it will not be closed:

>>> import sys
>>> from xopen import xopen
>>> with xopen(sys.stdout) as f: pass
... 
>>> f.closed
False

That behaviour allows arbitrary nesting of with-statements

fn = ...
with xopen(fn) as f1:
with xopen(f1) as f2:
with xopen(f2) as f3:
pass

and the file may only be closed on the outermost level.


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


Re: Obnoxious postings from Google Groups

2012-11-05 Thread Chris Angelico
On Mon, Nov 5, 2012 at 11:56 PM, Roy Smith  wrote:
> That's a very ascii-esqe attitude.  In a fully unicode world, I could
> easily see using U+00A0 (NO-BREAK SPACE) in file names, and still have
> space-delimited CLI work just fine.
>

Oh, do you have a "U+00A0-bar" on your keyboard?

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


Difference between range and xrange ??

2012-11-05 Thread inshu chauhan
what is the difference between range and xrange.. both seem to work the
same. ? And which should be used where and in what situations.. ??
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Difference between range and xrange ??

2012-11-05 Thread Dave Angel
On 11/05/2012 09:23 AM, inshu chauhan wrote:
> what is the difference between range and xrange.. both seem to work the
> same. ? And which should be used where and in what situations.. ??
>
>

One difference is that from versions of Python 3.0 and later,  xrange
doesn't exist, and range takes over the behavior of what was formerly
xrange.

So presumably you're asking about Python 2.x


In Python 2.x, range() generates a list, possibly a very large one. 
Sometimes that's exactly what you need.  But other times, you're just
using the list as an iterable, perhaps as a counter, or simply as a way
to make a loop go a fixed number of times.

xrange(), usually more efficient for speed, and certainly for space,
generates an iterable.  So it's interchangeable in a for loop, for example.

In general, if you're going to discard the list immediately after using
it, you should be using the iterable form, not the list form.


In Python 3.x, if you really need a list, you can trivially convert an
iterable into a list with the list "function."

mylist = list(range(4))



-- 

DaveA

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


Re: Difference between range and xrange ??

2012-11-05 Thread Joel Goldstick
in python 2.x xrange is a generator and range returns a list.  In python
3.x xrange is renamed to range replacing the list function with the
generator


On Mon, Nov 5, 2012 at 9:23 AM, inshu chauhan  wrote:

> what is the difference between range and xrange.. both seem to work the
> same. ? And which should be used where and in what situations.. ??
>
> --
> http://mail.python.org/mailman/listinfo/python-list
>
>


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


Re: Multi-dimensional list initialization

2012-11-05 Thread Demian Brecht

On 2012-11-04, at 10:44 PM, Andrew Robinson  wrote:
> but I think you meant:
> 
> m = [[None] * 4, [None] * 4, [None] * 4, [None] *4 ]
> rather than:
> m = [[None] * 4, [None] * 4, [None] * 4, [None * 4]]

Yes, I meant the former, thanks for catching the typo.

Demian Brecht
@demianbrecht
http://demianbrecht.github.com




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


Re: Obnoxious postings from Google Groups

2012-11-05 Thread Grant Edwards
On 2012-11-05, Roy Smith  wrote:
> In article ,
>  Chris Angelico  wrote:
>
>> It's nothing to do with operating system. File names are names, and
>> spaces in them are seldom worth the hassle unless you manipulate those
>> files solely using a GUI.
>
> That's a very ascii-esqe attitude.  In a fully unicode world, I could 
> easily see using U+00A0 (NO-BREAK SPACE) in file names, and still have 
> space-delimited CLI work just fine.

No, it wouldn't work just fine.  You'd never know when looking at
names whether it was a "regular" space or a "no-break space", and
names would be visually ambiguous.  Visually ambiguous names are
horrible.

> But, yeah, in the world we live in today, I try to avoid spaces in 
> filenames.  But, instead of turning "My File Name" into MyFileName, I'll 
> usually do it as My-File-Name or My_File_Name.


-- 
Grant Edwards   grant.b.edwardsYow! Boys, you have ALL
  at   been selected to LEAVE th'
  gmail.comPLANET in 15 minutes!!
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Multi-dimensional list initialization

2012-11-05 Thread Demian Brecht

On 2012-11-04, at 11:07 PM, Chris Rebert  wrote:

> However, unlike a list object (as in your latter example), the object
> `None` is completely immutable (and what's more, a singleton value),
> so you just-so-happen *not to be able to* run into the same problem of
> mutating an object (assignment to an index of a list constitutes
> mutation of that list) that is referenced in multiple places, for you
> cannot mutate None in the first place!

Thanks for clearing that up Chris (and the link to the FAQ). I had thought 
about that after going to bed ("D'oh.. None is immutable.. *That's* gotta be 
why").

Demian Brecht
@demianbrecht
http://demianbrecht.github.com




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


Re: accepting file path or file object?

2012-11-05 Thread Grant Edwards
On 2012-11-05, andrea crotti  wrote:

> Quite often I find convenient to get a filename or a file object as
> argument of a function, and do something as below:
>
> def grep_file(regexp, filepath_obj):
[...]
> if isinstance(filepath_obj, basestring):
> fobj = open(filepath_obj)
> else:
> fobj = filepath_obj
[...]
> This makes it also more convenient to unit-test, since I can just pass
> a StringIO.  But then there are other problems, for example if I pass
> a file object is the caller that has to make sure to close the file
> handle..
>
> So I'm thinking if it's not just worth to skip the support for file
> objects and only use the filenames, which seems a more robust and
> consistent choice..
>
> Any comment/suggestions about this?

I have found that accepting either a "file-like-object" or a filename
is sometimes worth the effort for a module that's going to be re-used
in a variety of contexts.  However, when I do it, I don't usually
check the type of the object -- I check for whatever "feature" I want
to use.  If I'm going to want to be able to call a read() method, I
check for presence of a read() method.  If that fails, then I assume
it's a filename and pass it to open().  If that fails, then it fails.

-- 
Grant Edwards   grant.b.edwardsYow! Oh my GOD -- the
  at   SUN just fell into YANKEE
  gmail.comSTADIUM!!
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Obnoxious postings from Google Groups

2012-11-05 Thread Grant Edwards
On 2012-11-05, Dennis Lee Bieber  wrote:
> On Mon, 5 Nov 2012 17:39:35 +1100, Chris Angelico 
> declaimed the following in gmane.comp.python.general:
>
>> 
>> It's nothing to do with operating system. File names are names, and
>> spaces in them are seldom worth the hassle unless you manipulate those
>> files solely using a GUI.
>>
>   At least spaces are visible.
>
>   CP/V would accept non-printable characters in file names... Things
> like BEL were valid -- but figure out where in the name the BEL
> character was when listing the directory contents 

Don't most OSes allow non-printing characters in filenames?  VMS and
Unix always have.  AFAIK, there are only two characters that can't
appear in a Unix filename: '\x00' and '/'.

-- 
Grant Edwards   grant.b.edwardsYow! I feel better about
  at   world problems now!
  gmail.com
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: accepting file path or file object?

2012-11-05 Thread Terry Reedy

On 11/5/2012 5:54 AM, andrea crotti wrote:

Quite often I find convenient to get a filename or a file object as
argument of a function, and do something as below:

def grep_file(regexp, filepath_obj):
 """Check if the given text is found in any of the file lines, take
 a path to a file or an opened file object
 """
 if isinstance(filepath_obj, basestring):
 fobj = open(filepath_obj)
 else:
 fobj = filepath_obj

 for line in fobj:
 if re.search(regexp, line):
 return True

 return False


This makes it also more convenient to unit-test, since I can just pass
a StringIO.  But then there are other problems, for example if I pass
a file object [it] is the caller that has to make sure to close the file
handle..


Which is as it should be. The caller should (normally) call your 
function within a with statement or might want to do other operations on 
the file before or after passing it to your grep_file.


--
Terry Jan Reedy

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


Re: Difference between range and xrange ??

2012-11-05 Thread Terry Reedy

On 11/5/2012 9:23 AM, inshu chauhan wrote:

what is the difference between range and xrange.. both seem to work the
same. ?


>>> range(3)
[0, 1, 2]
>>> xrange(3)
xrange(3)

You should read the appropriate manual entries before asking trivial 
questions. They say pretty clearly that range returns a list and xrange 
an xrange object.

http://docs.python.org/2/library/functions.html#range
http://docs.python.org/2/library/functions.html#xrange

If you do not understand the entries, quote the part that confuses you 
and say what you are unclear about.



And which should be used where and in what situations.. ??


The entry for xrange (added after range) tries to explain this. It is 
for situations in which one does not want a list, but only the sequence 
of numbers, especially in situations where the length of the list would 
be large enough to make creating the unneeded list a nuisance.



--
Terry Jan Reedy

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


py2exe with python 3.x

2012-11-05 Thread Monkey
Hi, 
i wonderd if there is a way to convert a py-file to a exe-file with version 3.x 
of python.

I know that it was possible till version 2.7.x. But is there a way to do it in 
version 3.x?

Yours sincerely,
Monkey
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: py2exe with python 3.x

2012-11-05 Thread Kwpolska
On Mon, Nov 5, 2012 at 8:34 PM, Monkey  wrote:
> Hi,
> i wonderd if there is a way to convert a py-file to a exe-file with version 
> 3.x of python.
>
> I know that it was possible till version 2.7.x. But is there a way to do it 
> in version 3.x?
>
> Yours sincerely,
> Monkey
> --
> http://mail.python.org/mailman/listinfo/python-list

Use cx_Freeze: http://cx-freeze.sourceforge.net/

-- 
Kwpolska 
stop html mail  | always bottom-post
www.asciiribbon.org | www.netmeister.org/news/learn2quote.html
GPG KEY: 5EAAEA16
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Obnoxious postings from Google Groups

2012-11-05 Thread rurpy
On 11/04/2012 04:13 AM, Jamie Paul Griffin wrote:
> / [email protected] wrote on Fri  2.Nov'12 at 11:39:10 -0700 /
> 
>> (I also hope I haven't just been suckered by a troll attempt,
>> windows/unix is better then unix/windows being an age-old means of
>> trolling.)
> 
> No, i'm not a "troll". I was just adding my opinion to the thread, I
> assumed that was allowed. I didn't say UNIX is better than Windows,
> did I; I just feel that Windows is not -- for me anyway -- the most
> suitable plaform for learning about the science of computing and
> coding, etc... being a computer science student that's the view i
> have and share with those I learn with and from. Why must people be
> accused of trolling everytime they make a statement that conveys a
> preference over one platform or language, for example, than the
> other. Provoking someone by labeling them a troll or implying they
> might be is a bit childish really.

I deliberately worded my response to avoid calling you a 
troll but rather just suggested the possibility so a denial 
without all the indignation would be more appropriate.  
(And FWIW I agree that "troll" is often used here synonymously 
with "I disagree with you" but that is not how I used it.)

You should realize that a good way to start an endless argument 
and one that has a long history of being used by trolls is to 
assert that OS/language/whatever is "better" than some other 
one in an environment where there are also lots of users of the 
other one.  Substituting "serious" or any other value-laden, 
unsubstantiatable word for "better" is just a insignificant 
variation.
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Proper place for everything

2012-11-05 Thread rurpy
On 11/02/2012 04:12 PM, Steven D'Aprano wrote:
> On Fri, 02 Nov 2012 11:51:29 -0700, Jason Benjamin wrote:
> 
>> On another note, it appears that Google (the only archive I can find for
>> this group) only has a little under 400 messages archived for this
>> group,
> 
> Google Groups is poison. If you post with it, you will be ignored by a 
> large percentage of the regulars here.
>[...]
> Bad options are:
> 
> - Absolutely anything to do with Google Groups. It's a crap user-
>   interface, posts don't follow established standards for email and news,
>   and Google requires you to sign in just to read the archives, which in
>   my opinion is morally indefensible.

Jason,

You should also be aware that there are a number of 
people here such as myself that use Google Groups for 
various reasons and there is concurrently some discussion 
of this in other threads including the correction of
just plain erroneous info about GG that has been
propagated here.

AFAIK, there is no real definition of "regular" nor 
any count of how many of them (whoever "them" is) that 
ignore GG.  Pretty obviously "crap user interface" is 
in the eye of the beholder and the login requirement 
is a personal issue.  
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Coordination between developers in the Python project

2012-11-05 Thread Chris Angelico
On Tue, Nov 6, 2012 at 6:09 AM, Terry Reedy  wrote:
>> I would like to remind you that the participation is absolutely
>> anonymous and voluntary, and you can quit it at any time. Your answers
>> will be strictly confidential and will be used only for research purpose
>> (no commercial use of any information you provided).
>
>
> Even if you mean that for yourself, you cannot guarantee any of that. For
> all you know, bit.ly records the ip address of each click.

Maybe so, but bit.ly won't get your survey answers, so it's still
anonymous in that sense.

But I agree with your other concerns, which is why I did not click the
link (and didn't even bother to respond till now).

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


Re: Multi-dimensional list initialization

2012-11-05 Thread Joshua Landau
On 5 November 2012 06:27, Demian Brecht  wrote:

> >>> a = [None] * 4
> >>> a[0] = 'a'
> >>> a
> ['a', None, None, None]
>
> >>> m = [[None] * 4] * 4
> >>> m[0][0] = 'm'
> >>> m
> [['m', None, None, None], ['m', None, None, None], ['m', None, None,
> None], ['m', None, None, None]]
>
> Is this expected behaviour and if so, why? In my mind either result makes
> sense, but the inconsistency is what throws me off.


z = [[None] * 4]

goes to

z = [x, x, x, x]
where x = [y]
where y = None

AND THEN

z[0] = 2

means

z = [p, x, x, x]
where p = 2

AND

z[1][0] = 3

means

x = [q]
where q = 3
hence z = [2, [3], [3], [3]]
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: accepting file path or file object?

2012-11-05 Thread Cameron Simpson
On 05Nov2012 10:54, andrea crotti  wrote:
| Quite often I find convenient to get a filename or a file object as
| argument of a function, and do something as below:

I tend to do this:

  def f(fp):
if isinstance(fp, str):
  with open(fp) as subfp:
return f(subfp)
... main code using fp as file object ...

That solves the file close issue neatly and lets you put this stuff up
the top where it is obvious.

| So I'm thinking if it's not just worth to skip the support for file
| objects and only use the filenames, which seems a more robust and
| consistent choice..

You can't always use filenames; plenty of calling code will already have
a file-like object to hand (eg a HTTP response or a StringIO or an
already opened file or any of a million other things). So requiring
filenames all the time is unreasonable.

It is almost always etter to write for file objects, since that is what
you would be converting any passed filename into, and put a self call at
the top to convert a filename into a file object if that is a reasonable
use case in your app.

Cheers,
-- 
Cameron Simpson 

...valve spreeengs?  VALVE _*SPRENGS*_!?!  We don' nd
no stnking valve spreengs!...   - Dr. Desmo
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Coordination between developers in the Python project

2012-11-05 Thread Chris Angelico
On Fri, Nov 2, 2012 at 7:49 AM, Tengy Td  wrote:
> It would be a great help for me if you could answer a short online survey
> (it should take approximately 5 minutes).
>
> This survey is designed to reach a better understanding of the cooperation
> and coordination between developers in Free/libre open source projects.
>
> There is no right or wrong answers, therefore, feel free to answer
> spontaneously and to skip the questions you feel you do not want to answer.

Okay, now that I have a chance to actually look at your survey, I have
some more useful comments to make :)

Most surveys don't care about people's exact ages. Unless yours is an
unusual exception, it'd be more manageable to ask for ranges, eg:

How old are you?
* Under 18
* 18-25
* 26-35
* 36-45
* 46-55
* 56-65
* Over 65

Also, there's a bit of a gap in your Education drop-down. I had to
leave it blank, as I never finished high school. Or does that not
matter to your survey?

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


Re: Multi-dimensional list initialization

2012-11-05 Thread Oscar Benjamin
On 5 November 2012 09:13, Hans Mulder  wrote:
> On 5/11/12 07:27:52, Demian Brecht wrote:
>> So, here I was thinking "oh, this is a nice, easy way to initialize a 4D 
>> matrix"
>> (running 2.7.3, non-core libs not allowed):
>>
>> m = [[None] * 4] * 4
>>
>> The way to get what I was after was:
>>
>> m = [[None] * 4, [None] * 4, [None] * 4, [None * 4]]
>
> Or alternateively:
>
> m = [[None] * 4 for _ in range(4)]

That's the way to do it.

I've seen this question many times between here and the python-tutor
list. It does seem to be a common gotcha.

I was just thinking to myself that it would be a hard thing to change
because the list would need to know how to instantiate copies of all
the different types of the elements in the list. Then I realised it
doesn't. It is simply a case of how the list multiplication operator
is implemented and whether it chooses to use a reference to the same
list or make a copy of that list. Since all of this is implemented
within the same list type it is a relatively easy change to make
(ignoring backward compatibility concerns).

I don't see this non-copying list multiplication behaviour as
contradictory but has anyone ever actually found a use for it?


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


Re: Multi-dimensional list initialization

2012-11-05 Thread Chris Angelico
On Tue, Nov 6, 2012 at 12:32 PM, Oscar Benjamin
 wrote:
> I was just thinking to myself that it would be a hard thing to change
> because the list would need to know how to instantiate copies of all
> the different types of the elements in the list. Then I realised it
> doesn't. It is simply a case of how the list multiplication operator
> is implemented and whether it chooses to use a reference to the same
> list or make a copy of that list. Since all of this is implemented
> within the same list type it is a relatively easy change to make
> (ignoring backward compatibility concerns).
>
> I don't see this non-copying list multiplication behaviour as
> contradictory but has anyone ever actually found a use for it?

Stupid example of why it can't copy:

bad = [open("test_file")] * 4

How do you clone something that isn't Plain Old Data? Ultimately,
that's where the problem comes from. It's easy enough to clone
something that's all scalars (strings, integers, None, etc) and
non-recursive lists/dicts of scalars, but anything more complicated
than that is rather harder.

If you want a deep copy and are prepared to handle any issues that
might result, you can do this:

>>> import copy
>>> a=[[2,3,4]]
>>> a.extend(copy.deepcopy(a))
>>> a[0][1]=10
>>> a
[[2, 10, 4], [2, 3, 4]]

And some things just won't work:
>>> bad.extend(copy.deepcopy(bad))
Traceback (most recent call last):
  File "", line 1, in 
bad.extend(copy.deepcopy(bad))
  File "C:\Python32\lib\copy.py", line 147, in deepcopy
y = copier(x, memo)
  File "C:\Python32\lib\copy.py", line 209, in _deepcopy_list
y.append(deepcopy(a, memo))
  File "C:\Python32\lib\copy.py", line 166, in deepcopy
rv = reductor(2)
TypeError: cannot serialize '_io.TextIOWrapper' object

The default behaviour is safe and reliable. When you want something
other than the default, there are ways of doing it.

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


Re: Multi-dimensional list initialization

2012-11-05 Thread Oscar Benjamin
On 6 November 2012 02:01, Chris Angelico  wrote:
> On Tue, Nov 6, 2012 at 12:32 PM, Oscar Benjamin
>  wrote:
>> I was just thinking to myself that it would be a hard thing to change
>> because the list would need to know how to instantiate copies of all
>> the different types of the elements in the list. Then I realised it
>> doesn't. It is simply a case of how the list multiplication operator
>> is implemented and whether it chooses to use a reference to the same
>> list or make a copy of that list. Since all of this is implemented
>> within the same list type it is a relatively easy change to make
>> (ignoring backward compatibility concerns).
>>
>> I don't see this non-copying list multiplication behaviour as
>> contradictory but has anyone ever actually found a use for it?
>
> Stupid example of why it can't copy:
>
> bad = [open("test_file")] * 4
>
> How do you clone something that isn't Plain Old Data? Ultimately,
> that's where the problem comes from. It's easy enough to clone
> something that's all scalars (strings, integers, None, etc) and
> non-recursive lists/dicts of scalars, but anything more complicated
> than that is rather harder.

That's not what I meant. But now you've made me realise that I was
wrong about what I did mean. In the case of

   stuff = [[obj] * n] * m

I thought that the multiplication of the inner list ([obj] * n) by m
could create a new list of lists using copies. On closer inspection I
see that the list being multiplied is in fact [[obj] * n] and that
this list can only know that it is a list of lists by inspecting its
element(s) which makes things more complicated.

I retract my claim that this change would be easy to implement.


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


problem with eval and time

2012-11-05 Thread Wincent
Dear all, I would like to convert tstr to representation of time, but encounter 
the following error. Is there a simple way to get what I want? Thanks.

>>> import time
>>> tstr = str(time.localtime())
>>> eval(tstr)
Traceback (most recent call last):
  File "", line 1, in 
  File "", line 1, in 
TypeError: structseq() takes at most 2 arguments (9 given)
>>> sys.version
'2.7.2 (default, Jun 12 2011, 15:08:59) [MSC v.1500 32 bit (Intel)]'

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


Re: How to improve the usability of nested packages

2012-11-05 Thread Rouslan Korneychuk

On 11/02/2012 12:11 PM, Michael Schwarz wrote:

… which doesn't work. Some of the modules reference other modules in
the same package. I'm not talking about cyclic references, but, for
example, the "dialog" module uses the "transaction" module. The
problem is that the "dialog" module uses the same mechanism shown
above to import the other modules from it's package. This means that
modules and packages are imported in this order:

- Application code executes "from the_library import sip"
- the_library/__init__.py is executed. No imports here.
- the_library/sip/__init__.py executes "from . import [...], dialog"
- the_library/sip/dialog.py executes "from the_library import sip"



In a way, you do have a cyclic reference. If you think of "import sip" 
as "from sip import __init__ as sip", it should become apparent.



Anyway, I see two ways around this. One is to rename the package and 
create a module under the_library with the old package name that imports 
what you want. To keep using the same names, every module in the package 
can import siblings like so (where sippkg is the new package name):

  import the_library.sippkg.transaction
  sip = the_library.sippkg


The second way is to not use import at all to load sibling modules 
(except in __init__.py), and instead use:

  sip = sys.modules['the_library.sip']

This will work as long as you are careful to load the modules from 
__init__.py in the correct order, where each module's dependencies must 
appear before the module, in the import list.

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


Re: problem with eval and time

2012-11-05 Thread alex23
On Nov 6, 1:32 pm, Wincent  wrote:
> Dear all, I would like to convert tstr to representation
> of time, but encounter the following error. Is there a
> simple way to get what I want? Thanks.
>
> >>> import time
> >>> tstr = str(time.localtime())
> >>> eval(tstr)
>
> Traceback (most recent call last):
>   File "", line 1, in 
>   File "", line 1, in 
> TypeError: structseq() takes at most 2 arguments (9 given)>>> sys.version

The problem is that the repr of `time.struct_time` isn't its
constructor, so you won't be able to do this without parsing the
string, I believe.

What are you trying to achieve here? You already have a
time.struct_time object, why turn it into a string if what you want is
the object?

If you're wanting to pass time values around as strings, maybe
`time.strptime` will be more useful.
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: problem with eval and time

2012-11-05 Thread Wincent
Thanks.

I fetch data from social networking sites and want to mark the time of access. 
I store all the information in a redis database, which converts everything into 
strings and I need to convert those strings back to original python objects 
when analyzing the data.

Best Regards

On Tuesday, November 6, 2012 12:22:44 PM UTC+8, alex23 wrote:
> On Nov 6, 1:32 pm, Wincent  wrote:
> 
> > Dear all, I would like to convert tstr to representation
> 
> > of time, but encounter the following error. Is there a
> 
> > simple way to get what I want? Thanks.
> 
> >
> 
> > >>> import time
> 
> > >>> tstr = str(time.localtime())
> 
> > >>> eval(tstr)
> 
> >
> 
> > Traceback (most recent call last):
> 
> >   File "", line 1, in 
> 
> >   File "", line 1, in 
> 
> > TypeError: structseq() takes at most 2 arguments (9 given)>>> sys.version
> 
> 
> 
> The problem is that the repr of `time.struct_time` isn't its
> 
> constructor, so you won't be able to do this without parsing the
> 
> string, I believe.
> 
> 
> 
> What are you trying to achieve here? You already have a
> 
> time.struct_time object, why turn it into a string if what you want is
> 
> the object?
> 
> 
> 
> If you're wanting to pass time values around as strings, maybe
> 
> `time.strptime` will be more useful.

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


Re: Coordination between developers in the Python project

2012-11-05 Thread Steven D'Aprano
On Mon, 05 Nov 2012 14:09:08 -0500, Terry Reedy wrote:

> On 11/1/2012 4:49 PM, Tengy Td wrote:
>> Hello,
>>
>>
>> I am a French student and I am currently realizing my final thesis in
>> the field of Free/libre open source software.
> 
> If you really are what you claim, you should give more details to make
> that plausible: university, college/dept, course of study, and post from
> a university account, not an address at one of the current favorite
> sites for con artists.
> 
>> It would be a great help for me if you could answer a short online
>> survey (it should take approximately 5 minutes).
> 
> If you want the survey emailed to you, you should include it. If it is
> hosted on a legitimate survey site, you should give a direct link.

I'm pretty sure that "legitimate" survey sites don't vet the moral 
character of their users. Just because a link comes from 
"www.legitimatesurveys.com" doesn't mean it is safe or desirable.


https://docs.google.com/spreadsheet/viewform?
formkey=dDE4YzYxZ1lDN0dpdFFheGJZM3prdVE6MA

Is that legitimate enough? 


>> The link to the survey is:http://bit.ly/SzVrJe
> 
> Careful people do not open anonymous links from stangers. That file
> could literally be anything, including a malware injection page.

Yes dad.

While it is good and proper to exhibit a certain level of caution on the 
internet, it is important to keep the dangers in perspective and not go 
overboard. You just implicitly accusing somebody of likely being a 
criminal who lures others to a malware site based on little more evidence 
than the fact that he has a gmail email address and linked to bit.ly. I 
think that has edging right up to the line between showing proper 
caution, and rudeness and paranoia.

"Legitimate" sites are not safe either. With the proliferation of 
Javascript, Flash, third-party advertisers, etc., just about any http 
page on the Internet could in principle contain malware. And not just by 
accident: Google has been caught illegally installing tracking cookies 
then lying about it, Sony installed a root-kit on users' computers 
(although not over the Internet) and Facebook is engaged in a neverending 
privacy and security war against its users.

Nevertheless most pages are safe. Hundreds of millions of people browse 
the web without being infected, protected by a combination of:

- firewall
- OS security features (such as not running everything as the
  Administrator or root user)
- anti-virus and anti-spyware software

etc.

Let me be frank here: if this link was malware, there would be *far* more 
cost-effective ways to spread it than by appealing to FOSS developers to 
fill in a survey. A fake link promoted with "Check out this blog post by 
Linus Torvalds where he smacks down Richard Stallman" ought to do it.



>> I would like to remind you that the participation is absolutely
>> anonymous and voluntary, and you can quit it at any time. Your answers
>> will be strictly confidential and will be used only for research
>> purpose (no commercial use of any information you provided).
> 
> Even if you mean that for yourself, you cannot guarantee any of that.
> For all you know, bit.ly records the ip address of each click.

You use gmane to post here. How do you know that they aren't recording 
your IP address? If they are, what are they going to do with it? What 
nefarious use do you think people are going to do with your IP address?



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


Re: problem with eval and time

2012-11-05 Thread Chris Angelico
On Tue, Nov 6, 2012 at 3:29 PM, Wincent  wrote:
> Thanks.
>
> I fetch data from social networking sites and want to mark the time of 
> access. I store all the information in a redis database, which converts 
> everything into strings and I need to convert those strings back to original 
> python objects when analyzing the data.

The easiest way, imho, is to store Unix times - simply the number of
seconds since 1970, as an integer or float. That can easily and safely
be turned into a string and back (floats might lose a little accuracy,
depending on how you do it, but the difference will be a small
fraction of a second).

>>> time.time()
1352176547.787
>>> time.gmtime(1352176547.787)
time.struct_time(tm_year=2012, tm_mon=11, tm_mday=6, tm_hour=4,
tm_min=35, tm_sec=47, tm_wday=1, tm_yday=311, tm_isdst=0)

Easy and unambiguous. Also compact, which may or may not be a selling point.

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


Re: problem with eval and time

2012-11-05 Thread Dave Angel
On 11/05/2012 11:29 PM, Wincent wrote:

(Please don't top-post.  it messes everything up.  And your use of
Google-groups is making everything double-spaced)

> Thanks.
>
> I fetch data from social networking sites and want to mark the time of 
> access. I store all the information in a redis database, which converts 
> everything into strings and I need to convert those strings back to original 
> python objects when analyzing the data.
>

Then restate your problem in terms of describing these strings. 
Apparently you don't get them from

str(time.localtime()), but from some redis methodology.

To convert most reasonable strings back to a date/time, you can probably use 
strftime.

But you certainly cannot reasonably use eval.  You're lucky it didn't work, so 
you didn't leave it that way.  Eval on data stored in some database?  You gotta 
be kidding.



-- 

DaveA

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


Re: Obnoxious postings from Google Groups

2012-11-05 Thread Steven D'Aprano
On Mon, 05 Nov 2012 14:47:47 -0500, Dennis Lee Bieber wrote:

>> Don't most OSes allow non-printing characters in filenames?  VMS and
>> Unix always have.  AFAIK, there are only two characters that can't
>> appear in a Unix filename: '\x00' and '/'.
> 
>   But can you /enter/ them with common keystrokes on a plain text
> terminal (it's been 35 years, so I don't recall the exact key used for
> the BEL on CP/V -- my mind thinks  was used)... No cut&paste
> from "character map", no -3digitsequence...


For most people, that's a pointless restriction. You might as well insist 
that the file name can be typed without using the shift key, or using 
only the left hand of the keyboard. Copy-paste, char map, alt-digits are 
as much a part of the input environment on modern systems as the keyboard.

Nevertheless, I do tend to prefer underscores to spaces, simply because I 
often use naive tools that treat spaces as separators. That is, command 
line shells.

For what it's worth, you can enter any control character in Unix/Linux 
systems with readline in bash using the C-q key combination. Newline 
needs a bit of special treatment: you need to wrap the name in single 
quotes to stop the newline from being interpreted as the end of the 
command.

[steve@ando temp]$ touch 'foo
bar'

To enter the newline, I typed Ctrl-Q to tell bash to treat the next 
character as a literal, and then typed Ctrl-J to get a newline.

[steve@ando temp]$ ls
foo?bar
[steve@ando temp]$ python -c "import os
> for nm in os.listdir('.'): print repr(nm)"
'foo\nbar'



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


Re: Obnoxious postings from Google Groups

2012-11-05 Thread Steven D'Aprano
On Mon, 05 Nov 2012 17:39:35 +1100, Chris Angelico wrote:

> On Mon, Nov 5, 2012 at 5:10 PM, rusi  wrote:
>> Among people who know me, I am a linux nerd: My sister scolded me
>> yesterday because I put files on her computer without spaces:
>> DoesAnyoneWriteLikeThis?!?!
> 
> My filenames seldom have spaces in them, but that has nothing to do with
> how I write English. Names are names. They're not essays, they are not
> written as full sentences. 

But names are often multiple words and sentence fragments, and for those 
you *need* spaces. Or at least an ersatz space, like underscore, which is 
ugly and harder to use, but at least makes using command shells easier to 
use. But don't be fooled: the fault belongs to the shell, not the space.

The problem is that shells are untyped and treat *everything* as a stream 
of text, and therefore cannot distinguish between arguments, variables, 
numbers, commands, etc. except by a few simplistic conventions such as:

"The first word on the line is the command."
"If it starts with a dash, it must be a command option."
"Arguments are separated by spaces."
etc.

When your data (e.g. filenames) violate those naive assumptions, as they 
frequently do, the shell cannot cope. One solution would be to fix the 
tools. Another would be to mangle the data.

"Real" programming languages don't have this problem. You can trivially 
refer to any file name, regardless of the characters in its name, in 
Python because you have a much more powerful set of tools: commands take 
real arguments, and the presence of a space in one argument cannot cause 
it to "bleed over" into the next argument.

The downside is that if spaces are not argument separators, then you need 
something else to be an argument separator. Or you need argument 
delimiters. Or strings need to be quoted. Programming languages do these 
things because they are designed to be correct. Shell do not because they 
are designed for lazy users and merely aim to be "good enough".


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


How to only get a list of the names of the non-directory files in current directory ('.')?

2012-11-05 Thread iMath
How to only get a list of the names of the non-directory files in current 
directory ('.')?
(Note excluding its subdirectories ).

I need the code : )
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: How to only get a list of the names of the non-directory files in current directory ('.')?

2012-11-05 Thread Chris Angelico
On Tue, Nov 6, 2012 at 4:19 PM, iMath  wrote:
> How to only get a list of the names of the non-directory files in current 
> directory ('.')?
> (Note excluding its subdirectories ).
>
> I need the code : )

Start by getting a list of names of everything in the current directory.

Then filter that list by testing each one to see if it's a directory.

Tip: The second step can be done with os.path.isdir

Put some code together and try it. If you have trouble, post your
code, any exception traceback you get, and what you're having
difficulty with, and we'll go on from there.

Have fun!

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


Re: Multi-dimensional list initialization

2012-11-05 Thread Andrew Robinson

On 11/05/2012 06:30 PM, Oscar Benjamin wrote:

On 6 November 2012 02:01, Chris Angelico  wrote:

On Tue, Nov 6, 2012 at 12:32 PM, Oscar Benjamin
  wrote:

I was just thinking to myself that it would be a hard thing to change
because the list would need to know how to instantiate copies of all
the different types of the elements in the list. Then I realised it
doesn't. It is simply a case of how the list multiplication operator
is implemented and whether it chooses to use a reference to the same
list or make a copy of that list. Since all of this is implemented
within the same list type it is a relatively easy change to make
(ignoring backward compatibility concerns).

I don't see this non-copying list multiplication behaviour as
contradictory but has anyone ever actually found a use for it?

Stupid example of why it can't copy:

bad = [open("test_file")] * 4

How do you clone something that isn't Plain Old Data? Ultimately,
that's where the problem comes from. It's easy enough to clone
something that's all scalars (strings, integers, None, etc) and
non-recursive lists/dicts of scalars, but anything more complicated
than that is rather harder.

That's not what I meant. But now you've made me realise that I was
wrong about what I did mean. In the case of

stuff = [[obj] * n] * m

I thought that the multiplication of the inner list ([obj] * n) by m
could create a new list of lists using copies. On closer inspection I
see that the list being multiplied is in fact [[obj] * n] and that
this list can only know that it is a list of lists by inspecting its
element(s) which makes things more complicated.

I retract my claim that this change would be easy to implement.


Oscar

Hi Oscar,

In general, people don't use element multiplication (that I have *ever* 
seen) to make lists where all elements of the outer most list point to 
the same sub-*list* by reference.  The most common use of the 
multiplication is to fill an array with a constant, or short list of 
constants;  Hence, almost everyone has  to work around the issue as the 
initial poster did by using a much longer construction.


The most compact notation in programming really ought to reflect the 
most *commonly* desired operation.  Otherwise, we're really just making 
people do extra typing for no reason.


Further, list comprehensions take quite a bit longer to run than low 
level copies; by a factor of roughly 10. SO, it really would be worth 
implementing the underlying logic -- even if it wasn't super easy.


I really don't think doing a shallow copy of lists would break anyone's 
program.
The non-list elements, whatever they are, can be left as reference 
copies -- but any element which is a list ought to be shallow copied.  
The behavior observed in the opening post where modifying one element of 
a sub-list, modifies all elements of all sub-lists is never desired as 
far as I have ever witnessed.


The underlying implementation of Python can check an object type 
trivially, and the only routine needed is a shallow list copy.  So, no 
it really isn't a complicated operation to do shallow copies of lists.


:)

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


Re: Multi-dimensional list initialization

2012-11-05 Thread Chris Angelico
On Tue, Nov 6, 2012 at 4:51 PM, Andrew Robinson
 wrote:
> I really don't think doing a shallow copy of lists would break anyone's
> program.

Well, it's a change, a semantic change. It's almost certainly going to
break _something_. But for the sake of argument, we can suppose that
the change could be made. Would it be the right thing to do?

Shallow copying by default would result in extremely weird behaviour.
All the same confusion would result, only instead of comparing
[None]*4 with [[None]]*4, there'd be confusion over the difference
between [[None]]*4 and [[[None]]]*4.

I don't think it would help anything, and it'd result in a lot more
work for no benefit.

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