Re: [Tutor] Python command calls the wrong version!

2010-09-08 Thread Evert Rol
 Dear Python Tutors,
 
 I am new to Python, having perviously used IDL for all my scripts. I was 
 hoping to use Python and so I have just downloaded and installed  version 2.6 
 using the mac installer. That all went fine.
 
 I then opened up X11, all fine.
 
 Then I typed in python
 
 and got this message
 
 python: execv: 
 /Applications/scisoft/i386/Packages/Python-2.5.4/Python.framework/Versions/2.5/Resources/Python.app/Contents/MacOS/Python:
  No such file or directory
 
 W!!! why is my python command trying to call a version which does not 
 exist? I appear to have broken Python? I am sire this is a really easy thing 
 to fix but I do not know enough about hows these programmes work to figure it 
 out for myself.

Most likely your path has been screwed up by the installation of scisoft. From 
the fact that it now can't find this Python 2.5, it appears you've later 
removed scisoft, but never changed your path back (check your .bashrc (or 
.cshrc, depending on your shell).
Also check your PATH:
$ echo $PATH
and perhaps even
$ which python

to see what you get back. If that points to some awkwardly located python, 
there's your problem.


Btw, if you trying to replace IDL with Python (I assume RSI IDL), you may be 
better off using macports, because you'll want various Python libraries  
modules on your Mac (eg numpy, scipy, matplotlib). And, in fact, you don't 
really need X11, although that may depend on the GUI toolkit you're using 
(TKinter works without, pygtk needs X11  GTK, the latter also available 
through macports); you can use start Python from the Terminal.


  Evert

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


Re: [Tutor] Python command calls the wrong version!

2010-09-08 Thread Alan Gauld



I am new to Python, having perviously used IDL for all my scripts.
I was hoping to use Python and so I have just downloaded and 
installed

version 2.6 using the mac installer. That all went fine.


I then opened up X11, all fine.


Why did you open X11?
If you used the Mac installer(*) it should run as a standard Mac 
program,

no need for X?

What happens if you type Python into the standard MacOS Terminal
application?

Running X adds a whole bunch of extra path and environment issues
to resolve, it might be easier to get it running in the vanilla MacOS 
first.



(*) BTW Which Mac installer? - there are at least 3 that I know of!

Just a thought.

Alan G.


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


[Tutor] pickling codecs

2010-09-08 Thread Dinesh B Vadhia
I use codecs to retain consistent unicode/utf-8 encoding and decoding for 
reading/writing to files.  Should the codecs be applied when using the 
pickle/unpickle function?  For example, the standard syntax is:

# pickle object
f = open(object, 'wb')
pickle.dump(object, f, 2)
 
# unpickle object
f = open(object, 'rb')
object= pickle.load(f)
 
or should it be:

# pickle object
f = codecs.open(object, 'wb', 'utf-8')
pickle.dump(object, f, 2)


# unpickle object
f = codecs.open(object, 'rb', 'utf-8')
object= pickle.load(f)

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


Re: [Tutor] Code review, plase

2010-09-08 Thread Alex
On Wed, Sep 8, 2010 at 12:20 AM, Steven D'Aprano st...@pearwood.info wrote:
 On Wed, 8 Sep 2010 06:39:27 am Alex wrote:
 Hi all.

 Could someone review my code? It's the first time I develop a
 reusable module and I would like to have some feedback.
 If you think it's good enough I will package it for pypi.

 I put the code on pastebin: http://pastebin.com/Tz367gAM

 Let's start with some little things... you talk about the Unix CronTab
 command, but there's no such thing, at least on Linux:

 [st...@sylar ~]$ CronTab
 bash: CronTab: command not found

Absolutely right I'll update the docstring

 In the _remove_crontab method, you say *All* the information contained
 in the crontab file are permanently lost, but that's grammatically
 incorrect -- information is a mass noun, i.e. neither singular nor
 plural. So in standard English, you would say all the information in
 the file is permanently lost, not are lost, in the same way that you
 would say the sugar is on the table or the grass is green.

Argh! I should go back to grammar school :-) English is not my first langage

 ct1 = micron.CronTab()
 ct2 = micron.CronTab()
 ct1.add_job('daily', 'echo BOOM!')
 ct2.add_job('daily', 'echo No BOOM today')

 Do they fight? What happens?

They will not be duplicated because the class will generate a unique
job_id incrementing the last id used. Since the module is just a
wrapper for the crontab command any other clashing will be managed by
the cron daemon. I will add an example to document this behaviour.

Thanks for your helpful comments.

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


Re: [Tutor] Pastebin.com fork based completely on Python (Django)

2010-09-08 Thread Robert
Does not *look* Pythonic - have a look at this : http://paste.pocoo.org/


On Tue, Sep 7, 2010 at 6:00 PM, Carlos Guerrero
guerrerocar...@gmail.com wrote:
 I did a pastebin.com fork in Django
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


[Tutor] sort problem

2010-09-08 Thread Roelof Wobben

Hello, 

 

I have this :

 

def sort_sequence(seq):

   sort_sequence([3, 4, 6, 7, 8, 2])
  [2, 3, 4, 6, 7, 8]
   sort_sequence((3, 4, 6, 7, 8, 2))
  (2, 3, 4, 6, 7, 8)
   sort_sequence(nothappy)
  'ahnoppty'

   if type(seq) == type([]):
seq.sort()
elif type(seq)== type(()):
seq = tuple(sorted(seq))
else:
seq2 = list(seq)
seq2.sort()
print seq2
seq.join(seq2)
return seq

 

The problem is that if I want to sort the characters in a string, the list 
exist of the sorted characters but as soon as I convert them to a string I get 
the old string.

 

What went wrong ?

 

Roelof

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


Re: [Tutor] sort problem

2010-09-08 Thread Evert Rol
 I have this :
  
 def sort_sequence(seq):
 
sort_sequence([3, 4, 6, 7, 8, 2])
   [2, 3, 4, 6, 7, 8]
sort_sequence((3, 4, 6, 7, 8, 2))
   (2, 3, 4, 6, 7, 8)
sort_sequence(nothappy)
   'ahnoppty'
 
if type(seq) == type([]):
 seq.sort()
 elif type(seq)== type(()):
 seq = tuple(sorted(seq))
 else:
 seq2 = list(seq)
 seq2.sort()
 print seq2
 seq.join(seq2)
 return seq
  
 The problem is that if I want to sort the characters in a string, the list 
 exist of the sorted characters but as soon as I convert them to a string I 
 get the old string.

Carefully read the documentation for str.join: 
http://docs.python.org/library/stdtypes.html#str.join

How does it work, what does it return, etc. Then fix the corresponding line in 
your code.
As a hint: str.join does work quite different than list.sort; I assume you're 
confusing their syntaxes.

Good luck,

  Evert


  
 What went wrong ?
  
 Roelof
  
 ___
 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] sort problem

2010-09-08 Thread Roelof Wobben


 

 Subject: Re: [Tutor] sort problem
 From: evert@gmail.com
 Date: Wed, 8 Sep 2010 17:26:58 +0200
 CC: tutor@python.org
 To: rwob...@hotmail.com
 
  I have this :
  
  def sort_sequence(seq):
  
   sort_sequence([3, 4, 6, 7, 8, 2])
  [2, 3, 4, 6, 7, 8]
   sort_sequence((3, 4, 6, 7, 8, 2))
  (2, 3, 4, 6, 7, 8)
   sort_sequence(nothappy)
  'ahnoppty'
  
  if type(seq) == type([]):
  seq.sort()
  elif type(seq)== type(()):
  seq = tuple(sorted(seq))
  else:
  seq2 = list(seq)
  seq2.sort()
  print seq2
  seq.join(seq2)
  return seq
  
  The problem is that if I want to sort the characters in a string, the list 
  exist of the sorted characters but as soon as I convert them to a string I 
  get the old string.
 
 Carefully read the documentation for str.join: 
 http://docs.python.org/library/stdtypes.html#str.join
 
 How does it work, what does it return, etc. Then fix the corresponding line 
 in your code.
 As a hint: str.join does work quite different than list.sort; I assume you're 
 confusing their syntaxes.
 
 Good luck,
 
 Evert
 


str.join(iterable)¶
 

How it works.

It puts all the elements of iterable into one string named str.

 

So it returns a string. 

 

Str is here seq  and the iterable is the list made by list.sort so seq2

 

So I don't see the error in that line.

 

 

Roelof

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


Re: [Tutor] sort problem

2010-09-08 Thread Greg
On Wed, Sep 8, 2010 at 11:50 AM, Roelof Wobben rwob...@hotmail.com wrote:



  Subject: Re: [Tutor] sort problem
  From: evert@gmail.com
  Date: Wed, 8 Sep 2010 17:26:58 +0200
  CC: tutor@python.org
  To: rwob...@hotmail.com

 
   I have this :
  
   def sort_sequence(seq):
   
sort_sequence([3, 4, 6, 7, 8, 2])
   [2, 3, 4, 6, 7, 8]
sort_sequence((3, 4, 6, 7, 8, 2))
   (2, 3, 4, 6, 7, 8)
sort_sequence(nothappy)
   'ahnoppty'
   
   if type(seq) == type([]):
   seq.sort()
   elif type(seq)== type(()):
   seq = tuple(sorted(seq))
   else:
   seq2 = list(seq)
   seq2.sort()
   print seq2
   seq.join(seq2)
   return seq
  
   The problem is that if I want to sort the characters in a string, the
 list exist of the sorted characters but as soon as I convert them to a
 string I get the old string.
 
  Carefully read the documentation for str.join:
 http://docs.python.org/library/stdtypes.html#str.join
 
  How does it work, what does it return, etc. Then fix the corresponding
 line in your code.
  As a hint: str.join does work quite different than list.sort; I assume
 you're confusing their syntaxes.
 
  Good luck,
 
  Evert
 

 str.join(*iterable*)¶ #12af20c2e150d2eb_str.join
 How it works.
 It puts all the elements of iterable into one string named str.

 So it returns a string.

 Str is here seq  and the iterable is the list made by list.sort so seq2

 So I don't see the error in that line.


 Roelof



The error is that you misunderstand the usage of str.join.  It doesn't do it
in place, i.e. it doesn't change the actual string, so you have to have a
variable to capture the response.

The biggest thing, though, is that in str.join, str is not the string to
store the joined iterator in, it's the separator for the string.  so, in
your case, where you have

seq.join(seq2)

You really want

seq = .join(seq2)

where  is the separator to join seq2 on (an empty string in this case)

HTH.

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


Re: [Tutor] sort problem

2010-09-08 Thread Roelof Wobben


 


Date: Wed, 8 Sep 2010 12:38:03 -0400
From: gregb...@gmail.com
To: tutor@python.org
Subject: Re: [Tutor] sort problem




On Wed, Sep 8, 2010 at 11:50 AM, Roelof Wobben rwob...@hotmail.com wrote:



 
 Subject: Re: [Tutor] sort problem
 From: evert@gmail.com
 Date: Wed, 8 Sep 2010 17:26:58 +0200
 CC: tutor@python.org
 To: rwob...@hotmail.com

 
  I have this :
  
  def sort_sequence(seq):
  
   sort_sequence([3, 4, 6, 7, 8, 2])
  [2, 3, 4, 6, 7, 8]
   sort_sequence((3, 4, 6, 7, 8, 2))
  (2, 3, 4, 6, 7, 8)
   sort_sequence(nothappy)
  'ahnoppty'
  
  if type(seq) == type([]):
  seq.sort()
  elif type(seq)== type(()):
  seq = tuple(sorted(seq))
  else:
  seq2 = list(seq)
  seq2.sort()
  print seq2
  seq.join(seq2)
  return seq
  
  The problem is that if I want to sort the characters in a string, the list 
  exist of the sorted characters but as soon as I convert them to a string I 
  get the old string.
 
 Carefully read the documentation for str.join: 
 http://docs.python.org/library/stdtypes.html#str.join
 
 How does it work, what does it return, etc. Then fix the corresponding line 
 in your code.
 As a hint: str.join does work quite different than list.sort; I assume you're 
 confusing their syntaxes.
 
 Good luck,
 
 Evert
 


str.join(iterable)¶ 
How it works.
It puts all the elements of iterable into one string named str.
 
So it returns a string. 
 
Str is here seq  and the iterable is the list made by list.sort so seq2
 
So I don't see the error in that line.
 
 
Roelof
 


The error is that you misunderstand the usage of str.join.  It doesn't do it in 
place, i.e. it doesn't change the actual string, so you have to have a variable 
to capture the response.


The biggest thing, though, is that in str.join, str is not the string to store 
the joined iterator in, it's the separator for the string.  so, in your case, 
where you have


seq.join(seq2)


You really want


seq = .join(seq2)


where  is the separator to join seq2 on (an empty string in this case)

HTH.

-- 
Greg Bair
gregb...@gmail.com
 
Oke, 
 
If I understand it right with join I can put two strings into 1 string.
 
Roelof
 

___ 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] sort problem

2010-09-08 Thread bob gailer

 On 9/8/2010 1:12 PM, Roelof Wobben wrote:


If I understand it right


You don't.

What does put two strings into 1 string mean. Provide an example.

What does the documentation say about join? What part of that do you not 
understand?


--
Bob Gailer
919-636-4239
Chapel Hill NC

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


Re: [Tutor] sort problem

2010-09-08 Thread Alan Gauld


Roelof Wobben rwob...@hotmail.com wrote

 Carefully read the documentation for str.join: 
 http://docs.python.org/library/stdtypes.html#str.join


How does it work, what does it return, etc. Then fix the 
corresponding line in your code.



str.join(iterable)¶

It puts all the elements of iterable into one string named str.


Thats not what the documentation says...


So it returns a string.


Thats true.,

When trying to understand how a function works, or debug these kinds 
of
errors use the  prompt to experiment. It's the definitive way of 
seeing what

Python will do.

For example try:


123.join([5,6,7])


Can you see what Python has done?

Use the  prompt it is one of the most powerful tools you have.

HTH,

--
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] how to create a persistent dictionary w/ cpickle?

2010-09-08 Thread Carter Danforth
Hi, I'm trying to a create a basic addressbook for practice. I'm using a
dictionary with cpickle, though I'm not sure how to persistently store each
instance in the dictionary. Below is the code I have so far.

If I run it once and add a contact and the details, it's fine. p.load(f)
shows the details next time I run it, but if I add another contact, and run
it again, the previous key:value doesn't show. It gets replaced by the new
one.

How can I fix this so that it adds the new key:value to the dictionary
instead of replacing the existing one? Appreciate any help, thanks.

import cPickle as p
addressbook = 'addressbook.data'
f = file(addressbook, 'r+')

class address:
def __init__(self, name, tel, email):
self.name = name
self.tel = tel
self.email = email

ab = {self.name : self.tel}

f = file(addressbook, 'r+')
p.dump(ab, f)

print p.load(f)
x = raw_input()

if x == 'add':
name = raw_input('\nName: ')
tel = raw_input('Tel: ')
email = raw_input('Email: ')

contact = address(name, tel, email)
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] sort problem

2010-09-08 Thread Francesco Loffredo

On 08/09/2010 19.12, Roelof Wobben wrote:

...
Oke,
If I understand it right with join I can put two strings into 1 string.
Roelof
Not quite. With join you can put together in one string all the elements 
of a list of strings. While you do so, you can also put another string 
as a wall between each element of the list. Let's make a little example:


separator = Roelof
list = [Wobben, Python, Learner]
print separator.join(list)

... what you will get? Guess before you try.


Francesco
Nessun virus nel messaggio in uscita.
Controllato da AVG - www.avg.com
Versione: 9.0.851 / Database dei virus: 271.1.1/3119 -  Data di rilascio: 
09/07/10 08:34:00
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] sort problem

2010-09-08 Thread Francesco Loffredo

On 08/09/2010 19.12, Francesco Loffredo wrote:

...
a little example:

separator = Roelof
list = [Wobben, Python, Learner]
print separator.join(list)

... what you will get? Guess before you try.


There's more, I forgot to add:

print separator

This is important, this method *returns* a *NEW* string, it does not 
modify the providing string (here separator)! This means you must save 
the result somewhere, if you want to use it later:


together = separator.join(list)

Francesco
Nessun virus nel messaggio in uscita.
Controllato da AVG - www.avg.com
Versione: 9.0.851 / Database dei virus: 271.1.1/3119 -  Data di rilascio: 
09/07/10 08:34:00
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] sort problem

2010-09-08 Thread Roelof Wobben


 
Date: Wed, 8 Sep 2010 20:10:28 +0200
From: f...@libero.it
To: tutor@python.org
Subject: Re: [Tutor] sort problem

On 08/09/2010 19.12, Francesco Loffredo wrote:
 ...
 a little example:

 separator = Roelof
 list = [Wobben, Python, Learner]
 print separator.join(list)

 ... what you will get? Guess before you try.
 
There's more, I forgot to add:
 
print separator
 
This is important, this method *returns* a *NEW* string, it does not 
modify the providing string (here separator)! This means you must save 
the result somewhere, if you want to use it later:
 
together = separator.join(list)
 
Francesco


___ Tutor maillist - 
Tutor@python.org To unsubscribe or change subscription options: 

http://mail.python.org/mailman/listinfo/tutor

 

Oke, 

 

I now see what everyone try to teach me.

 

Roelof

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


[Tutor] Mutable Properties

2010-09-08 Thread Chris King

 Dear Tutors,
I noticed that when you use a property to represent a mutable 
value, I you try to use its methods, it will directly change the value 
returned. I know this happens because I'm not really assigning it to 
something new, but changing whats already there, which won't fire off 
the set method. I was wondering if there was a way around this.

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


Re: [Tutor] Mutable Properties

2010-09-08 Thread Steven D'Aprano
On Thu, 9 Sep 2010 06:59:57 am Chris King wrote:
   Dear Tutors,
  I noticed that when you use a property to represent a mutable
 value, I you try to use its methods, it will directly change the
 value returned. I know this happens because I'm not really assigning
 it to something new, but changing whats already there, which won't
 fire off the set method. I was wondering if there was a way around
 this.

You're going to need to give an example of:

(1) what you do;
(2) what you want to happen; and
(3) what actually happens.

The simplest example I can think of is:

class K(object):
def __init__(self):
self._private = []
def _getter(self):
return self._private
def _setter(self, value):
self._private = list(value)
seq = property(_getter, _setter)


And in use:

 k = K()
 k.seq
[]
 k.seq.append(1)
 k.seq
[1]

Works fine.


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


Re: [Tutor] how to create a persistent dictionary w/ cpickle?

2010-09-08 Thread Steven D'Aprano
On Thu, 9 Sep 2010 03:43:42 am Carter Danforth wrote:
 Hi, I'm trying to a create a basic addressbook for practice. I'm
 using a dictionary with cpickle, though I'm not sure how to
 persistently store each instance in the dictionary. Below is the code
 I have so far.

 If I run it once and add a contact and the details, it's fine.
 p.load(f) shows the details next time I run it, but if I add another
 contact, and run it again, the previous key:value doesn't show. It
 gets replaced by the new one.

Where do you think you are *adding* a new contact? You don't. You 
*replace* the existing contact with a brand new one, every time.

The problem has nothing to do with pickle, or storing each instance in 
the dictionary. Pickle is already storing each instance in the 
dictionary. The problem is that you never *add* anything to the address 
book, you *replace* it each time, so there is never more than two 
instances in the dictionary (one key, one value).

You don't have an address BOOK, you only have a single address.


 How can I fix this so that it adds the new key:value to the
 dictionary instead of replacing the existing one? Appreciate any
 help, thanks.

I would dump the entire address class for now and just go for something 
nice and minimal. Get that working first, and then, *if necessary*, 
wrap it in a class. This is Python, not Java -- we use whatever works, 
and don't force everything to be a class when it doesn't have to be.

What's the simplest address record you might have? How about a name 
linked to a telephone number and email?

address_book = {name: (tel, email), another_name: (tel, email), ...}

So, here's the basic algorithm:

(1) Look for the address-book. If it doesn't exist, create an empty 
dictionary, and pickle it as the address-book.

(2) Read the address-book from the pickle file. It will be a dictionary, 
possibly empty.

(3) Add an address to the dictionary. Don't create a new dictionary:

 addresses = {}  # creates a new, empty address book
 addresses[Fred] = (1234 5678, f...@example.com)
 addresses[Betty] = (2468 1357, be...@nowhere.com)
 addresses  # not empty any more
{'Betty': ('2468 1357', 'be...@nowhere.com'), 'Fred': ('1234 
5678', 'f...@example.com')}

(3) Save the dictionary to the pickle file.


Once you have those steps happening manually, then wrap it into a class 
so they happen automatically.


Some more comments on your code:


 import cPickle as p

Now that's just lazy. While laziness in a programmer in a good thing, 
this is taking it to extremes!!! You use pickle twice, three times if 
you count the import. Is it really such a burden on you to 
type cPickle (or pickle) two more times, that you need to rename 
it p?

Excessive use of one-character names is one of the worst programming 
habits you can have. Don't do this.


 addressbook = 'addressbook.data'

Misleading variable name. addressbook isn't an addressbook at all, 
it's a filename.

 f = file(addressbook, 'r+')

You shouldn't keep the file open for large periods of time. On Windows, 
it may mean that it will be locked from any other program accessing it 
during that time, and it risks data corruption if your program crashes 
or the power goes out.

Open and close the file just when you need it.


 class address:

A minor point: it is the convention in Python that (most) classes start 
with a capital letter. So Address would be the class, leaving address 
available for an instance of the class:

address = Address()


 def __init__(self, name, tel, email):
 self.name = name
 self.tel = tel
 self.email = email
 ab = {self.name : self.tel}
 f = file(addressbook, 'r+')
 p.dump(ab, f)

 print p.load(f)
 x = raw_input()

 if x == 'add':
 name = raw_input('\nName: ')

To get a blank line before the prompt, it might be nicer to do this:

print
name = raw_input('Name: ')

That's a matter of personal preference though.


 tel = raw_input('Tel: ')
 email = raw_input('Email: ')
 contact = address(name, tel, email)




Hope this helps,



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


Re: [Tutor] how to create a persistent dictionary w/ cpickle?

2010-09-08 Thread Dave Angel

 On 2:59 PM, Carter Danforth wrote:

Hi, I'm trying to a create a basic addressbook for practice. I'm using a
dictionary with cpickle, though I'm not sure how to persistently store each
instance in the dictionary. Below is the code I have so far.

If I run it once and add a contact and the details, it's fine. p.load(f)
shows the details next time I run it, but if I add another contact, and run
it again, the previous key:value doesn't show. It gets replaced by the new
one.

How can I fix this so that it adds the new key:value to the dictionary
instead of replacing the existing one? Appreciate any help, thanks.

import cPickle as p
addressbook = 'addressbook.data'
f = file(addressbook, 'r+')

class address:
 def __init__(self, name, tel, email):
 self.name = name
 self.tel = tel
 self.email = email

 ab = {self.name : self.tel}

 f = file(addressbook, 'r+')
 p.dump(ab, f)

print p.load(f)
x = raw_input()

if x == 'add':
 name = raw_input('\nName: ')
 tel = raw_input('Tel: ')
 email = raw_input('Email: ')

 contact = address(name, tel, email)

I have no clue what you're trying to do with that address object;  it's 
confusing initialization with persistence, and you create such an 
object, but never actually use it.  I would remove any reference to the 
address book from that object.  I'd also rename it to Address, since 
class names are supposed to be uppercase (PEP8)


But anyway, in your flow, you do a p.load(), but never save the result.  
Every time you save to the addressbook file, you start over with just 
the one entry.


One way to fix this is to save the p.load() result as a variable, 
presumably of type dictionary, then add the 'contact' to that 
dictionary, and at the end of the script, save that variable with 
p.dump(addresses, f)


addresses =   p.load(f)

interact with user --
   addresses[name] = Address(name, tel, email)

p.dump(addresses, f)

I also don' t know how you get away with running your script the first 
time, since it blows up if there's no existing pickle file.


DaveA

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


Re: [Tutor] how to create a persistent dictionary w/ cpickle?

2010-09-08 Thread Alan Gauld


Carter Danforth carter.danfo...@gmail.com wrote

Hi, I'm trying to a create a basic addressbook for practice. I'm 
using a
dictionary with cpickle, though I'm not sure how to persistently 
store each

instance in the dictionary. Below is the code I have so far.


If you use a dictionary it makes more sense to use shelve rather than 
pickle for storage.
Shelve uses pickle under the hood but it makes a file look like a 
dictionary so you
don't need to load all the data and store it all again as you would 
with pickle.


Unfioortunately your code appears to be truncated, however...


import cPickle as p
addressbook = 'addressbook.data'
f = file(addressbook, 'r+')


I think you should use a binary file for pickle so the mode should be 
'rb' not 'r+'


HTH,

Alan G 



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


Re: [Tutor] slicing a string

2010-09-08 Thread Sandip Bhattacharya
On Tue, Sep 7, 2010 at 1:49 PM, Roel Schroeven
rschroev_nospam...@fastmail.fm wrote:

 But remember that you can make it simpler if you simply don't specify
 the start and end points:

 'hello'[::-1]
 'olleh'


While I know that idiom works, I haven't really found an explanation
as to *why* it works that way.

For a string S:
* Using  range, you need range(len(S),-1,-1) to give you the indexes
for the string in reverse.
* For slices, if you dont specify the start and end indices, they are
supposed to be filled in by 0 and len(S) respectively.
  - So S[::-1] means, S[0:len(S):-1] , so why dont we start with index
0 here, and then go to -1 (last char) and then into an infinite loop?
  - Especially, when S[0:len(S):1] works that way?

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