Re: [Tutor] dict['_find']

2011-02-20 Thread Alan Gauld

"Max Niederhofer"  wrote


first post, please be gentle. I'm having serious trouble finding an
alternative for the deprecated find module for dictionaries.


I think you are misunderstanding some of the terminology.
There is no deprecated find module. You are not using
any modules in your code. And you are not doing anything
special with "_fnd", it is just a string which you use as a
key in your dictionary.

cities = {'CA': 'San Francisco', 'MI': 'Detroit', 'FL': 
'Jacksonville'}


def find_city(themap, state):
   if state in themap:
   return themap[state]
   else:
   return "Not found."

cities['_find'] = find_city


What this does is assign the function find_city as the value
corresponding to the key "_find" in your dictionary.
You could use any name you like:

'check' would be one meaningful example...

However, there is no obvious reason to put the
function in the dictionary at all...?


while True:
   print "State? (ENTER to quit)",
   state = raw_input("> ")

   if not state: break


This loops round collecting state names from the user
and throwing them away until the user eventually gets
fed up and hits enter. At that point state holds an
empty string. I don't think you want that.


city_found = cities['_find'](cities, state)
print city_found


I think you want this inside the loop above...

My question is - how do I rewrite this using an alternate module 
given

find is deprecated?


'_find' is just a key, as such it is not deprecated.
What made you think it was? Did you get an error message?
If so always post the entire error text, it helps us enormously.


reference, I'm using 2.6.1 on darwin.


With the slight change to the indentyation of the last two lines
your code should work.

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


Re: [Tutor] dict['_find']

2011-02-20 Thread Knacktus

Am 20.02.2011 05:14, schrieb Max Niederhofer:


Hello all,

Hello Max,


first post, please be gentle. I'm having serious trouble finding an
alternative for the deprecated find module for dictionaries.

The code (from Zed Shaw's Hard Way, exercise 40) goes something like
this. Hope indentation survives.

cities = {'CA': 'San Francisco', 'MI': 'Detroit', 'FL': 'Jacksonville'}
I use a naming convention for dicts that has made me very happy on 
several occasion ;-):

key_to_value, in your case
state_to_city = {...}


def find_city(themap, state):
 if state in themap:
 return themap[state]
 else:
 return "Not found."

cities['_find'] = find_city

Did you put this entry into the same dictionary as the data on purpose?
Or is the purpose a kind of dispatch? Something that could be a dict on 
its own, like

private_function_name_to_function = {'_find': find_city}
You should try to keep things seperate and explicit.


while True:
 print "State? (ENTER to quit)",
 state = raw_input(">  ")

 if not state: break

city_found = cities['_find'](cities, state)
print city_found

My question is - how do I rewrite this using an alternate module given
find is deprecated? Grateful for all suggestions or pointers. For
reference, I'm using 2.6.1 on darwin.

Thanks so much for your help.

Best,
Max

--
Dr. Maximilian Niederhofer
Founder, Qwerly
http://qwerly.com/ | http://qwerly.com/max
+44 78 3783 8227
___
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] dict['_find']

2011-02-20 Thread Max Niederhofer
Steven, Alan, Knacktus,

thanks for your help. I was indeed very confused because I thought
'_find' was calling something special instead of just being added to
the dictionary (the confusion stemming from
http://www.python.org/dev/peps/pep-0004/ where find module is
obsolete).

When I ran the code, the error I got was:

max:python max$ python ex40.py
State? (ENTER to quit) > CA
Traceback (most recent call last):
  File "ex40.py", line 22, in 
city_found = cities['_find'](cities, state)
  File "ex40.py", line 8, in find_city
return themap(state)
TypeError: 'dict' object is not callable

Two main mistakes: (1) not including the actual search in the while
loop and (2) wrong parentheses in return themap(state) instead of
return themap[state].

Fixed version which runs:

cities = {'CA': 'San Francisco', 'MI': 'Detroit', 'FL': 'Jacksonville'}

def find_city(themap, state):
if state in themap:
return themap[state]
else:
return "Not found."

cities['_find'] = find_city

while True:
print "State? (ENTER to quit)",
state = raw_input ("> ")
if not state: break
city_found = cities['_find'](cities, state)
print city_found

Thanks for your help, especially the comments about keeping things
separate and explicit. Now to find out why Zed Shaw thinks this is a
good way of doing it...

Best,
Max

On Sun, Feb 20, 2011 at 9:40 AM, Knacktus  wrote:
> Am 20.02.2011 05:14, schrieb Max Niederhofer:
>
>> Hello all,
>
> Hello Max,
>>
>> first post, please be gentle. I'm having serious trouble finding an
>> alternative for the deprecated find module for dictionaries.
>>
>> The code (from Zed Shaw's Hard Way, exercise 40) goes something like
>> this. Hope indentation survives.
>>
>> cities = {'CA': 'San Francisco', 'MI': 'Detroit', 'FL': 'Jacksonville'}
>
> I use a naming convention for dicts that has made me very happy on several
> occasion ;-):
> key_to_value, in your case
> state_to_city = {...}
>>
>> def find_city(themap, state):
>>     if state in themap:
>>         return themap[state]
>>     else:
>>         return "Not found."
>>
>> cities['_find'] = find_city
>
> Did you put this entry into the same dictionary as the data on purpose?
> Or is the purpose a kind of dispatch? Something that could be a dict on its
> own, like
> private_function_name_to_function = {'_find': find_city}
> You should try to keep things seperate and explicit.
>>
>> while True:
>>     print "State? (ENTER to quit)",
>>     state = raw_input(">  ")
>>
>>     if not state: break
>>
>> city_found = cities['_find'](cities, state)
>> print city_found
>>
>> My question is - how do I rewrite this using an alternate module given
>> find is deprecated? Grateful for all suggestions or pointers. For
>> reference, I'm using 2.6.1 on darwin.
>>
>> Thanks so much for your help.
>>
>> Best,
>> Max
>>
>> --
>> Dr. Maximilian Niederhofer
>> Founder, Qwerly
>> http://qwerly.com/ | http://qwerly.com/max
>> +44 78 3783 8227
>> ___
>> 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
>



-- 
Dr. Maximilian Niederhofer
Founder, Qwerly
http://qwerly.com/ | http://qwerly.com/max
+44 78 3783 8227
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


[Tutor] how to connect to Django's irc

2011-02-20 Thread shaheryar ali
hi guys,

does anyone know how to connect to the Django's irc,
As django is based on python there asking from you guys,


thanks





From: Alan Gauld 
To: tutor@python.org
Sent: Wed, January 19, 2011 8:04:23 PM
Subject: Re: [Tutor] Decoding from strange symbols


"Oleg Oltar"  wrote 
> I am trying to decode a string I took from file:
> 
> file = open ("./Downloads/lamp-post.csv", 'r')

I assume you know what its supposed to represent? What the columns etc are 
intended to be? Otherwise it will be a challenge!

There is a section here that looks like the months of the year (English) in 
reverse:


\x00S\x00e\x00a\x00r\x00c\x00h\x00e\x00s\x00\t\x00D\x00e\x00c\x00
\x002\x000\x001\x000\x00\t\x00N\x00o\x00v\x00
\x002\x000\x001\x000\x00\t\x00O\x00c\x00t\x00
\x002\x000\x001\x000\x00\t\x00S\x00e\x00p\x00
\x002\x000\x001\x000\x00\t\x00A\x00u\x00g\x00
\x002\x000\x001\x000\x00\t\x00J\x00u\x00l\x00
\x002\x000\x001\x000\x00\t\x00J\x00u\x00n\x00
\x002\x000\x001\x000\x00\t\x00M\x00a\x00y\x00
\x002\x000\x001\x000\x00\t\x00A\x00p\x00r\x00
\x002\x000\x001\x000\x00\t\x00M\x00a\x00r\x00
\x002\x000\x001\x000\x00\t\x00F\x00e\x00b\x00
\x002\x000\x001\x000\x00\t\x00J\x00a\x00n\x00

Otherwise its all a bit arbitrary...

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 maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] how to connect to Django's irc

2011-02-20 Thread Corey Richardson
On 02/20/2011 01:03 PM, shaheryar ali wrote:
> hi guys,
> 
> does anyone know how to connect to the Django's irc,
> As django is based on python there asking from you guys,
> 
> 
> thanks
> 

Point your favorite client at irc.freenode.net or use
http://webchat.freenode.net/. #django

You'll have better luck with django-specific questions on the django
mailing list.

-- 
Corey Richardson

I've never known any trouble which an hour's
reading didn't assuage.
-Charles De Secondat
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] how to connect to Django's irc

2011-02-20 Thread bob gailer

On 2/20/2011 1:03 PM, shaheryar ali wrote:

hi guys,

does anyone know how to connect to the Django's irc,
As django is based on python there asking from you guys,


thanks


I don't know BUT what is the relevance of the following to your question?

If it is not relevant why did you include it?

Please in the future start a new email - do NOT spring off an existing one.



*From:* Alan Gauld 
*To:* tutor@python.org
*Sent:* Wed, January 19, 2011 8:04:23 PM
*Subject:* Re: [Tutor] Decoding from strange symbols


"Oleg Oltar" mailto:oltarase...@gmail.com>> wrote
> I am trying to decode a string I took from file:
>
> file = open ("./Downloads/lamp-post.csv", 'r')

I assume you know what its supposed to represent? What the columns etc 
are intended to be? Otherwise it will be a challenge!


There is a section here that looks like the months of the year 
(English) in reverse:



\x00S\x00e\x00a\x00r\x00c\x00h\x00e\x00s\x00\t\x00D\x00e\x00c\x00
\x002\x000\x001\x000\x00\t\x00N\x00o\x00v\x00
\x002\x000\x001\x000\x00\t\x00O\x00c\x00t\x00
\x002\x000\x001\x000\x00\t\x00S\x00e\x00p\x00
\x002\x000\x001\x000\x00\t\x00A\x00u\x00g\x00
\x002\x000\x001\x000\x00\t\x00J\x00u\x00l\x00
\x002\x000\x001\x000\x00\t\x00J\x00u\x00n\x00
\x002\x000\x001\x000\x00\t\x00M\x00a\x00y\x00
\x002\x000\x001\x000\x00\t\x00A\x00p\x00r\x00
\x002\x000\x001\x000\x00\t\x00M\x00a\x00r\x00
\x002\x000\x001\x000\x00\t\x00F\x00e\x00b\x00
\x002\x000\x001\x000\x00\t\x00J\x00a\x00n\x00

Otherwise its all a bit arbitrary...

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 maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor



--
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] tic tac toe

2011-02-20 Thread bob gailer

On 2/20/2011 9:49 AM, Ben Ganzfried wrote:

Thanks, Bob.


I'd love some specific feedback. Exactly what did I offer that you found 
useful?


Also please always reply-all so a copy goes to the list.

--
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


[Tutor] creating classes while coding

2011-02-20 Thread Bill Allen
I know that Python not only supports OOP, but is fundamentally OOP in its
design.   Just in using the language and standard library, that much becomes
obvious.   However, I do wonder a bit about the practice I have seen of some
Python programmers to implement relatively short bits of code, that would be
quite simple using functions and procedural code, with classes.   Some such
code appears far more complicated than the job at hand really demands.   I
know there is not always a single right or wrong way of accomplishing
programming task, but why implement using classes unnecessarily?  When is it
necessary?  When is it best practice?  I'll freely admit that I do not come
from an OOP programming background, so designing classes is not my first
impulse when writing code.  Am I missing something?

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


Re: [Tutor] creating classes while coding

2011-02-20 Thread R. Alan Monroe
> I'll freely admit that I do not come from an OOP programming
> background, so designing classes is not my first impulse when
> writing code. Am I missing something?

In my book, no, you're not missing something. I refer to it as
"drinking the OO kool-aid". It's not compulsory. I just use it when it
makes sense.

Alan

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


Re: [Tutor] creating classes while coding

2011-02-20 Thread Bill Allen
That raises my next question.   Under what sort of programming circumstances
does it make sense?


--Bill








On Sun, Feb 20, 2011 at 19:01, R. Alan Monroe wrote:

> > I'll freely admit that I do not come from an OOP programming
> > background, so designing classes is not my first impulse when
> > writing code. Am I missing something?
>
> In my book, no, you're not missing something. I refer to it as
> "drinking the OO kool-aid". It's not compulsory. I just use it when it
> makes sense.
>
> Alan
>
> ___
> 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] creating classes while coding

2011-02-20 Thread Alan Gauld


"Bill Allen"  wrote


However, I do wonder a bit about the practice I have seen of some
Python programmers to implement relatively short bits of code, that 
would be

quite simple using functions and procedural code, with classes.


OOP is often overkill, but if its the normal way of programming
- and for many recent students its all they get taught - then using
classes is the natural approach. "When all you have is a hammer
everything looks like a nail..." However, there may be good reasons.
Classes make reuse easier, in general, than functions. So if the
task may become a building block for a future project, or even
an extended version of the current one then building a class
makes sense.

code appears far more complicated than the job at hand really 
demands.   I
know there is not always a single right or wrong way of 
accomplishing

programming task, but why implement using classes unnecessarily?


Why do anything unnecessarily?
1) You may not know another way.
2) You may have an extended use case in mind that is
   not directly related to the task at hand
3) Somebody said it was "the right way to do it"
4) A learning exercise (do we really need so many text editors?!)
5) Obstinacy - I've started so I'll finish!

and probably more...


When is it necessary?  When is it best practice?


When reuse is an issue, When multiple instances of the problem
need to be created (think parallel processing), When it has to fit
with a bigger project context (which is using OOP). When objects
naturally fit a complex problem domain - think GUIs.


I'll freely admit that I do not come from an OOP programming
background, so designing classes is not my first
impulse when writing code.  Am I missing something?


Yes, but not as much as you might think!
But when you do start to think OOP before functions you
will probably find it less surprising! :-)

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


Re: [Tutor] creating classes while coding

2011-02-20 Thread Bill Allen
On Sun, Feb 20, 2011 at 19:59, Alan Gauld  wrote:

>
> "Bill Allen"  wrote
>
>
>  However, I do wonder a bit about the practice I have seen of some
>> Python programmers to implement relatively short bits of code, that would
>> be
>> quite simple using functions and procedural code, with classes.
>>
>
> OOP is often overkill, but if its the normal way of programming
> - and for many recent students its all they get taught - then using
> classes is the natural approach. "When all you have is a hammer
> everything looks like a nail..." However, there may be good reasons.
> Classes make reuse easier, in general, than functions. So if the
> task may become a building block for a future project, or even
> an extended version of the current one then building a class
> makes sense.
>

Thanks for the feedback.   Particularly when it comes to reuse of code, I
can see it could be worth my time to look into designing classes a bit more.
  I am starting to write enough code now that I am beginning to reuse some
bits of it regularly.  I have not yet taken the reuse of code further than
packaging up some functions into modules.  I'll look into this further.

You mentioned the programming methods being taught programming students
nowadays.   That OOP is a basic programming method being taught sure leaves
me in the dust.   The last formal programming class I took in college was
way back in the early 90s and it was to learn IBM 370 Assembly Language.
OOP was not only not on the radar, it was not even in the same solar
system!:-D

feeling kinda old, and I am not all that old...

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