[Tutor] Unicode Encode Error

2014-06-17 Thread Aaron Misquith
I'm trying to obtain the questions present in StackOverflow for a
particular tag.

Whenever I try to run the program i get this *error:*

Message File Name Line Position
Traceback
 C:\Users\Aaron\Desktop\question.py 20
UnicodeEncodeError: 'ascii' codec can't encode character u'\u201c' in
position 34: ordinal not in range(128)


*This is the code:*
import stackexchange
import sys
sys.path.append('.')
so = stackexchange.Site(stackexchange.StackOverflow)
term= raw_input("Enter the keyword")
print 'Searching for %s...' % term,
sys.stdout.flush()
qs = so.search(intitle=term)

for q in qs:
   print '%8d %s' % (q.id, q.title)
   with open('D:\ques.txt', 'a+') as question:
   question.write(q.title)

Can anyone explain me what is going wrong here? This program used to run
perfectly fine before. Only since yesterday I have started getting this
error.
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Unicode Encode Error

2014-06-17 Thread Peter Otten
Aaron Misquith wrote:

> I'm trying to obtain the questions present in StackOverflow for a
> particular tag.
> 
> Whenever I try to run the program i get this *error:*
> 
> Message File Name Line Position
> Traceback
>  C:\Users\Aaron\Desktop\question.py 20
> UnicodeEncodeError: 'ascii' codec can't encode character u'\u201c' in
> position 34: ordinal not in range(128)
> 
> 
> *This is the code:*
> import stackexchange
> import sys
> sys.path.append('.')
> so = stackexchange.Site(stackexchange.StackOverflow)
> term= raw_input("Enter the keyword")
> print 'Searching for %s...' % term,
> sys.stdout.flush()
> qs = so.search(intitle=term)
> 
> for q in qs:
>print '%8d %s' % (q.id, q.title)
>with open('D:\ques.txt', 'a+') as question:
>question.write(q.title)
> 
> Can anyone explain me what is going wrong here? This program used to run
> perfectly fine before. Only since yesterday I have started getting this
> error.

The traceback and the code you post don't fit together as the latter has 
less than 20 lines. Therefore I have to guess:

q.title is probably unicode

When you are writing unicode to a file it is automatically converted to 
bytes assuming the ascii encoding

>>> f = open("tmp.txt", "w")
>>> f.write(u"abc")

However, this fails when the unicode string contains non-ascii characters:

>>> f.write(u"äöü")
Traceback (most recent call last):
  File "", line 1, in 
UnicodeEncodeError: 'ascii' codec can't encode characters in position 0-2: 
ordinal not in range(128)

So your code "worked" yesterday because the title you retrieved yesterday
did not contain any non-ascii chars.

The fix is to open the file with an encoding that can cope with the extra 
characters. UTF-8 is a good choice here. To use that modify your code as 
follows:

import codecs
...
with codecs.open(filename, "a", encoding="utf-8") as question:
question.write(q.title)

PS: personally I'd open the file once outside the loop:

with codecs.open(...) as questions_file:
for q in qs:
print ...
questions_file.write(q.title)


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


[Tutor] Tips

2014-06-17 Thread keith papa


Hi, I want to no what some tips or information you can give me to remember some 
of the rules of python, when you first start learning programming?
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Tips

2014-06-17 Thread Alan Gauld

On 17/06/14 19:52, keith papa wrote:



Hi, I want to no what some tips or information you can give me to

> remember some of the rules of python,
> when you first start learning programming?


Most of my tips/rules are about programming rather than python...
-
Use for loops when you know (or can predict) the number of iterations.
Use while loops when you don't know in advance

Put repeating code in a function

Use meaningful variable names.

Use dictionaries more often.

Let the data define the structure.
And structure your data so that it can.

And more Python specific:
---
Use modules instead of singleton classes

Use "if/elif" instead of "if/else if" trees

Variables are just names bound to objects

Don't test types, use the interface


Is that the kind of thing?

--
Alan G
Author of the Learn to Program web site
http://www.alan-g.me.uk/
http://www.flickr.com/photos/alangauldphotos

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


Re: [Tutor] Tips

2014-06-17 Thread Alex Kleider

On 2014-06-17 13:35, Alan Gauld wrote:


Don't test types, use the interface


Can you please explain what you mean by this?
alex
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Tips

2014-06-17 Thread Danny Yoo
On Tue, Jun 17, 2014 at 5:02 PM, Alex Kleider  wrote:
> On 2014-06-17 13:35, Alan Gauld wrote:
>
>> Don't test types, use the interface
>
>
> Can you please explain what you mean by this?


If you are writing type tests on a function's inputs, you might better
off by having the inputs implement an interface: the design of the
program will often be more maintainable.


As a toy example, consider the following:

###
class Football(object):
pass

class Baseball(object):
pass

def printBallSize(game):
if isinstance(game, Football):
print "The ball is 68-70cm"
if isinstance(game, Baseball):
print "The ball is 229-235mm"
raise ValueError
###

The problem with getBallSize() is that it hardcoded a decision based
on what the type of the input is.  It's a bit fragile.


A more flexible design allows the game itself to provide that
information for itself:

###
class Football(object):
def size(self):
return "68-70cm"

class Baseball(object):
def size(self):
return "229-235mm"

def printBallSize(game):
return "The ball is", game.size()
###


The reason the latter is usually preferable is because additional
games can be supported without having to revise printBallSize().  For
example, we can write:

###
class AmericanFootball(object):
def size(self):
return "68-70cm"
###

and printBallSize() will work on American footballs as well.
printBallSize() works on anything that implements a size() method.


The example above is very toy.  A more realistic example might be
writing a function whose inputs might be assumed to be a sequence.
Rather than hardcode a test that explicitly checks whether the input
is a list, just use it.  Then anything that satisfies the
"interface"---the way you're using the input---will often Just Work.
Tuples, for example, will do many of the things that lists will do.
And in some cases, even a file might look like a list, for all
practical purposes, if all we care about is iterating though it once.


Hope that makes some sense!
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor