Re: [Tutor] permutations?

2010-12-14 Thread Dave Angel

On 01/-10/-28163 02:59 PM, Francesco Loffredo wrote:

On 03/12/2010 1.32, Steven D'Aprano wrote:


mylist = [x for x in mylist if x != "something"]

Up to this point, I share experiences and solution. But the next point
did thrill me:


If you really need to modify the list in place, and not just re-bind
the name "mylist" to the new list, then one tiny change will do
it:

mylist[:] = [x for x in mylist if x != "something"]


I thought mylist[:] was a copy of mylist, and the two lines above would
generate exactly the same code!

Is this a special optimization by the Python interpreter, or is it just
a mistake in my understanding of the slice operator?


Not an optimization, but a fundamental part of the language.

On the left side of an assignment, a slice operator specifies which part 
of an existing object gets replaced by the right side.  So whenever you 
have a list that's referred to by more than one symbol, you may want to 
use a syntax like this.


The slice operator does a very different thing inside an expression (eg. 
on the right side of an assignment).


DaveA
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] permutations?

2010-12-14 Thread Francesco Loffredo

On 03/12/2010 1.32, Steven D'Aprano wrote:

Back in Ancient Days when dinosaurs walked the earth, and I programmed in 
Pascal, computers didn't have much memory, and were slow.
Consequently it wasn't practical to make a copy of a list if you wanted to 
delete a few items. The only practical way to modify
lists was to modify them in place, and if you needed to delete items, you had 
to work backwards. It took me a while to break myself
of the habit of doing this:

for i in range(len(mylist)-1, -1, -1):
 if mylist[i] == "something":
  del mylist[i]

(Note that you only need to work backwards if you're *deleting* entries, not if 
you replace them with something else.)

This is still a useful technique to have in your toolbox, but generally 
speaking the above is better written as:

mylist = [x for x in mylist if x != "something"]

Up to this point, I share experiences and solution. But the next point did 
thrill me:


If you really need to modify the list in place, and not just re-bind the name 
"mylist" to the new list, then one tiny change will do
it:

mylist[:] = [x for x in mylist if x != "something"]


I thought mylist[:] was a copy of mylist, and the two lines above would 
generate exactly the same code!

Is this a special optimization by the Python interpreter, or is it just a 
mistake in my understanding of the slice operator?


-
Nessun virus nel messaggio.
Controllato da AVG - www.avg.com
Versione: 10.0.1170 / Database dei virus: 426/3313 -  Data di rilascio: 
13/12/2010

___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Generating a python file

2010-12-14 Thread Peter Otten
Evans Anyokwu wrote:

> The page you linked to above was not found.
> Could you check the link again -

Sorry, I accidentally stripped off the trailing 'l' during cut-and-paste. 
The correct link is

http://docs.python.org/library/json.html

Peter

___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Writing to the terminal?

2010-12-14 Thread Bill Allen
Still looking on this one, but I will be sure to post back to the list if I
find anything.

Thanks,
Bill Allen


On Tue, Dec 14, 2010 at 5:23 AM, Steven D'Aprano wrote:

> Bill Allen wrote:
>
>> Anyone know how to get WConio.putch() to properly put out a box drawing
>> character to the screen in the while at a cmd prompt?   The code page is
>> 437, but it when I tell it to put out 188, for example, it get a 1/4
>> character instead of the box drawing character.
>>
>
> My guess is that there is a mis-match between the code page you think you
> have, the code page you actually have, and the encoding that your terminal
> is set to.
>
> I suspect though that you might need to take this to a specialist WConio
> mailing list, if there is one, or at least the main Python list:
> pyt...@python.org or comp.lang.python.
>
>
> Good luck, and if you do find the solution, don't forget to post it here
> for future reference.
>
>
> --
> Steven
>
> ___
> Tutor maillist  -  Tutor@python.org
> To unsubscribe or change subscription options:
> http://mail.python.org/mailman/listinfo/tutor
>
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Generating a python file

2010-12-14 Thread Evans Anyokwu
On Tue, Dec 14, 2010 at 12:06 PM, Peter Otten <__pete...@web.de> wrote:

> C.T. Matsumoto wrote:
>
> > Is it possible to create files containing python code in the same sort
> > of way that you can generate text files.
> >
> > A simple example perhaps could be a persistent dictionary. Keys and
> > values are written to the dictionary in a file, that can be imported
> > later.
>
> For simple human-readable dictionaries the established format is json, see
>
> http://docs.python.org/library/json.htm
>
> json files look similar to python dictionaries, but there are libraries to
> read or write them for other programming languages, too.
>
> Peter
>
> ___
> Tutor maillist  -  Tutor@python.org
> To unsubscribe or change subscription options:
> http://mail.python.org/mailman/listinfo/tutor
>

Peter,

The page you linked to above was not found.
Could you check the link again -

--
Evans
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Generating a python file

2010-12-14 Thread Peter Otten
C.T. Matsumoto wrote:

> Is it possible to create files containing python code in the same sort
> of way that you can generate text files.
> 
> A simple example perhaps could be a persistent dictionary. Keys and
> values are written to the dictionary in a file, that can be imported
> later.

For simple human-readable dictionaries the established format is json, see

http://docs.python.org/library/json.htm

json files look similar to python dictionaries, but there are libraries to 
read or write them for other programming languages, too.

Peter

___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Writing to the terminal?

2010-12-14 Thread Steven D'Aprano

Bill Allen wrote:

Anyone know how to get WConio.putch() to properly put out a box drawing
character to the screen in the while at a cmd prompt?   The code page is
437, but it when I tell it to put out 188, for example, it get a 1/4
character instead of the box drawing character.


My guess is that there is a mis-match between the code page you think 
you have, the code page you actually have, and the encoding that your 
terminal is set to.


I suspect though that you might need to take this to a specialist WConio 
mailing list, if there is one, or at least the main Python list: 
pyt...@python.org or comp.lang.python.



Good luck, and if you do find the solution, don't forget to post it here 
for future reference.



--
Steven
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Generating a python file

2010-12-14 Thread Alan Gauld


"C.T. Matsumoto"  wrote

Is it possible to create files containing python code in the same 
sort of way that you can generate text files.


Yes, a python file is just a text file. There is nothing special about
it other than the fact that the contents happen to be Python code.

A simple example perhaps could be a persistent dictionary. Keys and 
values are written to the dictionary in a file, that can be imported 
later.


You can do that and in dfact a well known email application does
something very similar. However for most situations a proper data
storage solution is better (certainly more secure).
And for simple cases pickle and shelve(for dictionaries) should
suffice. For more complex cases a config file, XML or a relational
database would be better.

Alan G.


___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Generating a python file

2010-12-14 Thread Albert-Jan Roskam
Hello,

If it's specifically about a dictionary, try also the following:
import shelve
help(shelve)

A shelve is a persistent dictionary.
 Cheers!!
Albert-Jan 


~~
All right, but apart from the sanitation, the medicine, education, wine, public 
order, irrigation, roads, a fresh water system, and public health, what have 
the 
Romans ever done for us?
~~ 





From: शंतनू 
To: C.T. Matsumoto 
Cc: python-tutor 
Sent: Tue, December 14, 2010 9:54:00 AM
Subject: Re: [Tutor] Generating a python file





On Tue, Dec 14, 2010 at 14:18, C.T. Matsumoto  wrote:

Hello,
>
>Is it possible to create files containing python code in the same sort of way 
>that you can generate text files.
>
>A simple example perhaps could be a persistent dictionary. Keys and values are 
>written to the dictionary in a file, that can be imported later.
>

http://docs.python.org/library/pickle.html is your friend.

HTH.



  ___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Generating a python file

2010-12-14 Thread शंतनू
On Tue, Dec 14, 2010 at 14:18, C.T. Matsumoto wrote:

> Hello,
>
> Is it possible to create files containing python code in the same sort of
> way that you can generate text files.
>
> A simple example perhaps could be a persistent dictionary. Keys and values
> are written to the dictionary in a file, that can be imported later.
>

http://docs.python.org/library/pickle.html is your friend.

HTH.
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


[Tutor] Generating a python file

2010-12-14 Thread C.T. Matsumoto

Hello,

Is it possible to create files containing python code in the same sort 
of way that you can generate text files.


A simple example perhaps could be a persistent dictionary. Keys and 
values are written to the dictionary in a file, that can be imported later.


Thanks,

T
--
C.T. Matsumoto
Claes de Vrieselaan 60a III
3021 JR Rotterdam
The Netherlands

tel.: +31 (0)6 41 45 08 54
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] making onthefly attributes persistent

2010-12-14 Thread Knacktus

Am 13.12.2010 23:50, schrieb Jojo Mwebaze:



On Mon, Dec 13, 2010 at 8:44 PM, Alan Gauld mailto:alan.ga...@btinternet.com>> wrote:


"Jojo Mwebaze" mailto:jojo.mweb...@gmail.com>> wrote

Assuming i have a class bank as below .

class bank(object):
  def __init__(self, bal=0):
  self.bal = bal
  def deposit(self, amount):
  self.bal+=amount
  print self.bal

I define a method debit - which i add to the class onthefly

bank.debit = debit


#I can also add an attribute owner

myaccount.owner = 'jojo'


My problem is how to make the added attributes, 'owner' and 'debit'
persistent automatically


If that's your only problem with this approach congratulations!
How does your orther code know when/if these dynamic
operations/data exist so as to use them? If they just assume
they exist then why not just add them in the definition. Even as nulls?

While Python allows you to dynamically add features to classes/objects
its not something I would recommend unless you have a really good
reason - not least because you bring upon yourself all sorts of
problems!

If you are determined to do so you can make the objects persistent
using the approach I outline on my tutorial but adding a loop to cycle
over the contents of dir(). But you may find that recovering the
objects - especially if they have a mixed set of attribnutes - presents
even more problems...

IMHO This is a feature of python that should be considered unorthodox
and only to be used when no other approach will work!

HTH,




Thanks Allan for the feedback, the idea is to write a method like
store() on the object, that probably looks up all these changes and
commits them into the database.


Assuming you're planning to use a database with a scheme (e.g. a 
relational DB) you need to define your tables in advance anyway. Even if 
you're using a schemeless DB, where do you track which data is in it? 
How do you build your queries dynamically? This can become very messy.
Overall, I think Alan's recommendation to avoid dynamic attribute 
creation is the one to follow.




Please let me know where to find approach you propose in your tutorial.
I read your tutorial when i was just initiated to python, a reference
would be helpful to help me find the soln without hustle.

Cheers



--
Alan Gauld
Author of the Learn to Program web site
http://www.alan-g.me.uk/


___
Tutor maillist  - Tutor@python.org 
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor




___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor