Re: [Tutor] Basic question on spaces

2011-07-20 Thread Alan Gauld

Alexander Quest wrote:


As you can see, this is quite rudimentary; I have not discovered any special
function that eliminates spaces yet, if such a function exits. 


There was a thread on this a week or so back.
There you will find several suggestions however, in Python 3 the
simplest is probably to use print() itself. print takes a couple of 
optional parameters, one of which defines the separator character, by 
default a space. You can specify an empty string instead and then hard 
code your spaces.


Try
 help(print)

for more details.


The other options include using a format string  or string concatenation 
to create the string you want before you send it to print.


HTH,

Alan G.

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


Re: [Tutor] Installing module and running

2011-07-20 Thread Walter Prins
Hi David,

On 19 July 2011 20:34, David Merrick merrick...@gmail.com wrote:

 Thanks for you help.Its 64 bit windows. What directory would the module go
 in? Scripts or site-packages in Lib?

 I have Python 2.6 in C drive but the files I am using are in a directory on
 d drive.

 Does the the module have to be in in the same directory as the files that
 use it?


When you install additional packages into Python (for example, the nose
package as in your case), they become part of your Python installation.
Because of this, you can import them just like you can import any other
module that is part of Python already and in some cases they add new
scripts/commands that can be run from a command line.

Physically what happens during installation is that the .py files (and
folders/packages) get put into your Python\Lib folder (for example
C:\Python27\Lib on my machine.)  As an aside, the reason Python is able to
find modules in this folder automatically is that the Python\Lib folder is
(by default) one of the folders that Python searches in when it encounters
an import statement.  Consequently, when you put a python module in the
Python\Lib folder, it becomes effectively available to any other Python
script you might want to run regardless of where the script being run is
physically located.

As mentioned previously, Python packages oftentimes also include command
line programs to interface with the Python installation, or provide other
functionality.  Typically such modules/programs are put into the
Python\Scripts folder, the idea being that you as a user can then run
these programs from there.  Unfortunately by default (at least on Windows),
the Python\Scripts folder is not on your command line (system) path and so
won't be found if you type only the script/command name at the command
prompt.  Consequently, if you want to use a Python script installed in the
Python\Scripts folder, you have to make that the current working directory
before running the script or specify the full path to the script, otherwise
the system obviously won't know where to find the script to run and will
respond with a Bad command or filename response.

What you want to do is to add the Python\Scripts folder to your system
path, then you'll be able to run these Python scripts/commands as if they're
inbuilt operating system commands without having to either be in the
Python\Scripts folder or specify the full path to run them.  To do this,
click on the Start button, right click Computer, click Properties,
click Advanced System settings, click Environment variables button
below, find the PATH entry in the System variables list and click it to
select it, then click Edit below that list, press End to move to the end
of the line and append the scripts folder with a semicolon to delimit it
from the previous path.  In my case I therefore would append (excluding the
quotes): ;c:\Python27\Scripts Click OK, OK again, OK again. Now when you
open a command prompt any script inside of the Python\Scrtips folder will be
directly runnable.

Now about Python package installation:  By convention if you download a
Python package manually, you can manually install it by extracting the
package archive, then changing the working directory to the root of
extracted package, and then running the setup.py script that is usually
included with Python packages, with an install option, e.g.:

python setup.py install

This does the donkey work to copy the modules of the package into the
Python\Lib folder, and Python\Scripts folder, if relevant, plus anything
else that might need to be done.  You could install your nose package like
this, e.g. by downloading it, extracting it, then running the above command
in the root of the nose folder.

I'd like to reiterate though, that one of the packages that make the
installation other Python packages even easier, is the setuptools package,
which amongst other things creates the easy_install script under the
Python\Scripts folder.  What it does, is to allow you to install other
Python packages directly from the Python package archive without having to
manually download them.  Eg once you have setuptools installed and thus have
easy_setup available as a command/script (runnable from anywhere if you've
updated your system PATH as above), then you can install nose (or any other
common package available in the Python package repository) by simply opening
a command prompt and issuing:

easy_install packagename

So in your case you'd enter:

easy_install nose

That's it!  This will then automatically download the nose package from the
internet, and install it for you. It really is worth it to install
setuptools (or one of the newer variants like pip or distribute...) as
this makes your life as a Python developer a lot easier when it comes to
getting packages downloaded and installed into your Python environment.

Does that give you enough understanding to get you going?

Cheers

Walter
___

[Tutor] Is the Python 3.2.1 documentation available as a .chm file?

2011-07-20 Thread Richard D. Moores
Is the Python 3.2.1 documentation available as a .chm file from Python.org?

Thanks,

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


Re: [Tutor] Is the Python 3.2.1 documentation available as a .chm file?

2011-07-20 Thread Alan Gauld

Richard D. Moores wrote:

Is the Python 3.2.1 documentation available as a .chm file from Python.org?


I think you'll need to go to Activestate for that.

Alan g

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


Re: [Tutor] Is the Python 3.2.1 documentation available as a .chm file?

2011-07-20 Thread Richard D. Moores
On Wed, Jul 20, 2011 at 06:58, Flynn, Stephen (L  P - IT)
steve.fl...@capita.co.uk wrote:
 Saying that, there's nothing stopping you from taking the html version
 and compiling your own chm from it. Pretty easy to do.

Great! But how?

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


Re: [Tutor] Is the Python 3.2.1 documentation available as a .chm file?

2011-07-20 Thread Walter Prins
On 20 July 2011 15:06, Richard D. Moores rdmoo...@gmail.com wrote:

 On Wed, Jul 20, 2011 at 06:58, Flynn, Stephen (L  P - IT)
 steve.fl...@capita.co.uk wrote:
  Saying that, there's nothing stopping you from taking the html version
  and compiling your own chm from it. Pretty easy to do.

 Great! But how?


Google throws up this as most relevant IMHO:
http://msdn.microsoft.com/en-us/library/ms670169%28v=vs.85%29.aspx

Google also throws up this (http://chmprocessor.sourceforge.net/) which may
be interesting/relevant.

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


[Tutor] Unstable IDLE

2011-07-20 Thread Timo Smieszek

Hi

I use Python 2.7.2 (v2.7.2:8527427914a2, Jun 11 2011, 15:22:34) on a 
Macbook Pro (Mac OS X 10.6.7).
Whenever I want to open a new window in the IDLE or whenever I work with 
.py file within the IDLE, it crashes either immediately (in case of 
opening a new window) or after a short time (in case of opening an 
existing file).


I already installed Python a second time, but this also does not help.

Has anybody an idea how to fix this problem?

Thanks and all best

Timo


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


Re: [Tutor] Is the Python 3.2.1 documentation available as a .chm file?

2011-07-20 Thread Richard D. Moores
On Wed, Jul 20, 2011 at 07:28, Walter Prins wpr...@gmail.com wrote:


 On 20 July 2011 15:06, Richard D. Moores rdmoo...@gmail.com wrote:

 On Wed, Jul 20, 2011 at 06:58, Flynn, Stephen (L  P - IT)
 steve.fl...@capita.co.uk wrote:
  Saying that, there's nothing stopping you from taking the html version
  and compiling your own chm from it. Pretty easy to do.

 Great! But how?


 Google throws up this as most relevant IMHO:
 http://msdn.microsoft.com/en-us/library/ms670169%28v=vs.85%29.aspx

Sorry, I just can't make sense of that page.

 Google also throws up this (http://chmprocessor.sourceforge.net/) which may
 be interesting/relevant.

So I downloaded and installed chmProcessor only to find that Only one
HTML file can be used as source, and there are many in the Python
docs.

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


[Tutor] Web Programming

2011-07-20 Thread Dharmit Shah
Hi all,

I have been reading Head First
Pythonhttp://www.headfirstlabs.com/books/hfpython/ since
some time now. I am stuck in a chapter on Web Development. Web Development
has never been an area of my interest and hence I feel stuck in there. If
anyone here has read the book, I wish to know if it's okay to skip that
chapter and read further?

-- 
Regards

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


Re: [Tutor] Web Programming

2011-07-20 Thread James Reynolds
If it doesn't interest you, skip it and move on. You can always come back
and learn it later, which will be easier anyway, with a more robust
background.

On Wed, Jul 20, 2011 at 11:22 AM, Dharmit Shah shahdhar...@gmail.comwrote:

 Hi all,

 I have been reading Head First 
 Pythonhttp://www.headfirstlabs.com/books/hfpython/ since
 some time now. I am stuck in a chapter on Web Development. Web Development
 has never been an area of my interest and hence I feel stuck in there. If
 anyone here has read the book, I wish to know if it's okay to skip that
 chapter and read further?

 --
 Regards

 Dharmit Shah http://about.me/dharmit



 ___
 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] Pyglet for 2.7

2011-07-20 Thread Shwinn Ricci
Is this out yet?
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


[Tutor] Pyglet for 2.7

2011-07-20 Thread Shwinn Ricci
(on a mac. 2.7 python just in case)
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Is the Python 3.2.1 documentation available as a .chm file?

2011-07-20 Thread xDog Walker
On Wednesday 2011 July 20 06:41, Richard D. Moores wrote:
 Is the Python 3.2.1 documentation available as a .chm file from Python.org?


http://www.python.org/ftp/python/3.2.1/python321.chm

HTH

-- 
I have seen the future and I am not in it.
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Pyglet for 2.7

2011-07-20 Thread Wayne Werner
What does their website say?

On Wed, Jul 20, 2011 at 10:57 AM, Shwinn Ricci arm...@gmail.com wrote:

 (on a mac. 2.7 python just in case)

 ___
 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] Is the Python 3.2.1 documentation available as a .chm file?

2011-07-20 Thread Richard D. Moores
On Wed, Jul 20, 2011 at 09:04, xDog Walker thud...@gmail.com wrote:
 On Wednesday 2011 July 20 06:41, Richard D. Moores wrote:
 Is the Python 3.2.1 documentation available as a .chm file from Python.org?


 http://www.python.org/ftp/python/3.2.1/python321.chm

Did that work for you? It got me a chm file that says everywhere,
Navigation to the web
page was cancelled.

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


Re: [Tutor] Unstable IDLE

2011-07-20 Thread Wayne Werner
On Wed, Jul 20, 2011 at 9:26 AM, Timo Smieszek timo.smies...@daad-alumni.de
 wrote:

 Hi

 I use Python 2.7.2 (v2.7.2:8527427914a2, Jun 11 2011, 15:22:34) on a
 Macbook Pro (Mac OS X 10.6.7).
 Whenever I want to open a new window in the IDLE or whenever I work with
 .py file within the IDLE, it crashes either immediately (in case of opening
 a new window) or after a short time (in case of opening an existing file).

 I already installed Python a second time, but this also does not help.

 Has anybody an idea how to fix this problem?


Try launching from the command line - you should be able to open the
terminal and type idle at the prompt. You'll probably get some more useful
information from the traceback when it crashes. If you copy the last line
and search Google for mac python idle crash  + line, then it might get you
some good information. If not, you could copy/paste the entire traceback
here and maybe someone can point you in the right direction.

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


[Tutor] questions on encoding

2011-07-20 Thread Albert-Jan Roskam
Hi,

I am looking for test data with accented and multibyte characters. I have found 
a good resource that I could use to cobble something together 
(http://www.inter-locale.com/whitepaper/learn/learn-to-test.html) but I was 
hoping somebody knows some ready resource.

I also have some questions about encoding. In the code below, is there a 
difference between unicode() and .decode?
s = §ÇǼÍÍ
x = unicode(s, utf-8)
y = s.decode(utf-8)
x == y # returns True

Also, is it, at least theoretically, possible to mix different encodings in 
byte strings? I'd say no, unless there are multiple BOMs or so. Not that I'd 
like to try this, but it'd improve my understanding of this sort of obscure 
topic.


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?

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


Re: [Tutor] what is 'doubleword alignment'?

2011-07-20 Thread Albert-Jan Roskam
--- On Mon, 7/18/11, Kushal Kumaran kushal.kumaran+pyt...@gmail.com wrote:

From: Kushal Kumaran kushal.kumaran+pyt...@gmail.com
Subject: Re: [Tutor] what is 'doubleword alignment'?
To: Walter Prins wpr...@gmail.com
Cc: tutor@python.org
Date: Monday, July 18, 2011, 8:39 AM

On Sun, Jul 17, 2011 at 9:15 PM, Walter Prins wpr...@gmail.com wrote:


 On 17 July 2011 15:26, Lisi lisi.re...@gmail.com wrote:

 Sorry to be slow.  Blame virtually no sleep last night ;-(  But even were
 the
 power of two bit correct (and I see subsequently that it is not), how is
 18 a
 power of two?


 The 18 bytes is a bit of an irrelevance.  The point is that if the start of
 the buffer falls on a dword (double word) alligned memory location then in
 theory the access should be faster.  The term is a little bit ambiguous
 because strictly speaking different processors have different word sizes.
 Even so, usually when people speak of double-word alignment, it's often the
 case that the term word in such a context has its original meaning, e.g. 16
 bits.  A dword is then 32bits or 4 bytes.   A doubleword aligned memory
 address is, using these assumptions, therefore an address that is divisible
 by 4.  Obviously if the word size is 32bits, then a double word would be
 64bits and a doubleword aligned address would need to be divisible by 8.  As
 an aside, this type of optimization is often taken care of by compilers
 under the hood, and in any case it's generally not something that you'll
 really be considering as a Python programmer.  (If however you were working
 on one of the Python runtimes or implementations, then you might well be
 sometimes considering this type of thing, depending on exactly how
 performance critical what you are working might be and what the runtime was
 being implemented in.)


It's not just about performance.  Some hardware simply cannot access
data that is not correctly aligned.  C programs that indiscriminately
cast among pointers to types of different sizes are a pain to port off
lenient architectures like x86.  If you're writing C code that deals
with pointers, you *always* need to keep alignment in mind.

-- 
regards,
kushal

=== Hello, 

Sorry for the late reply. Thank you all for your replies. I said '18' but I 
meant '16' (maybe my fingers are too big ;-). 

The text I put in my original post was from the documentation of a .dll/.so 
file (programmed in C). It was part of a procedure that reads out data. Each 
variable is 8 bytes or (for string variables) mutliples of 8 bytes (and each 
byte is 8 bits). I am using struct.unpack and ctypes to process the data in 
python. It works now, although I still want to read more about this. Where does 
the distinction little/big endian enter this story?

Thanks again, I appreciate it!

Albert-Jan
___
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] little/big endian was Re: what is 'doubleword alignment'?

2011-07-20 Thread Alan Gauld

Albert-Jan Roskam wrote:
 and ctypes to process the data in python. It works now, although I
 still want to read more about this. Where does the distinction
 little/big endian enter this story?

That's to do with which bit in a byte/word is most significant.

e.g. is the decimal value 1 stored as

0001   # the one on the right hand nibble
or as
0001   # the one on the left hand nibble

Now scale that up to word sized numbers...
Different CPUs do it differently.

I can't recall which is which - I'm sure wikipedia will
reveal all! :-)

HTH,

Alan G.
Growing to hate my Netbook keyboard more by the day!
And hating my PC repairer even more!
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


[Tutor] Homework problem

2011-07-20 Thread Ken Baclig
Hi,

I'm trying to make a function that receives text (a string) as an argument and 
returns the same text (as string), but with 1 added to each word that is a 
number.

I need help getting started.

So far, I have:

def FindNumbers(a_string):

    for index, char in enumerate(a_string):
        if char.isdigit():
a_string[index] = 


def Test():

    sometext = I got 432 when I counted, but Jim got 433 which is a lot 
foronly 6 cats, or were there 12 cats?
    
    FindNumbers(sometext)

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


Re: [Tutor] Homework problem

2011-07-20 Thread Marc Tompkins
On Wed, Jul 20, 2011 at 2:54 PM, Ken Baclig kbac...@yahoo.com wrote:

 Hi,

 I'm trying to make a function that receives text (a string) as an argument
 and returns the same text (as string), but with 1 added to each word that is
 a number.

 I need help getting started.

 So far, I have:

 def FindNumbers(a_string):

 for index, char in enumerate(a_string):
 if char.isdigit():
 a_string[index] =


 def Test():

 sometext = I got 432 when I counted, but Jim got 433 which is a lot
 foronly 6 cats, or were there 12 cats?

 FindNumbers(sometext)

 Test()


First of all, don't enumerate() the string; split() it instead - this will
give you a list of words instead of characters.
Then, look at each item in that list; check to see whether it's numeric -
isdigit() works for this.
If it _is_ numeric, convert it to an int, add one to it, and turn it back
into a string.
Join the list back into a string, and you're done.

Note: you can step through the items in a list by saying (for example) for
word in words: - but if you do it that way you can't modify any of the
items.  If you need to modify them - by adding 1, for example - you need to
refer to them by index instead, and the quickest way to do that is for x in
range(len(words)):  print words[x].

That was a bunch of broad hints - if you need help putting them together,
feel free to ask.
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Homework problem

2011-07-20 Thread Ken Baclig
Does this look right?  Still a little confused

        if char.isdigit():
           num = int(char) + 1
           a_string[index] = str(num)
print a_string





From: Marc Tompkins marc.tompk...@gmail.com
To: Ken Baclig kbac...@yahoo.com
Cc: tutor@python.org tutor@python.org
Sent: Wednesday, July 20, 2011 3:45 PM
Subject: Re: [Tutor] Homework problem


On Wed, Jul 20, 2011 at 2:54 PM, Ken Baclig kbac...@yahoo.com wrote:

Hi,


I'm trying to make a function that receives text (a string) as an argument and 
returns the same text (as string), but with 1 added to each word that is a 
number.


I need help getting started.


So far, I have:


def FindNumbers(a_string):


    for index, char in enumerate(a_string):
        if char.isdigit():
a_string[index] = 




def Test():


    sometext = I got 432 when I counted, but Jim got 433 which is a lot 
foronly 6 cats, or were there 12 cats?
    
    FindNumbers(sometext)


Test()
First of all, don't enumerate() the string; split() it instead - this will give 
you a list of words instead of characters.
Then, look at each item in that list; check to see whether it's numeric - 
isdigit() works for this.
If it _is_ numeric, convert it to an int, add one to it, and turn it back into 
a string.
Join the list back into a string, and you're done.

Note: you can step through the items in a list by saying (for example) for 
word in words: - but if you do it that way you can't modify any of the items.  
If you need to modify them - by adding 1, for example - you need to refer to 
them by index instead, and the quickest way to do that is for x in 
range(len(words)):  print words[x].

That was a bunch of broad hints - if you need help putting them together, feel 
free to ask. ___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Homework problem

2011-07-20 Thread Alan Gauld

Ken Baclig wrote:

Does this look right?  Still a little confused



Nope.
Notice that Marc said NOT to operate on characters but to split() the 
string into a wordlist. Then test for each word in the wordlist to see 
if it isdigit(). Igf so then convert the word to an int() and add one.
Then convert the new int back to a str()and insert back into your 
wordlist. Finally join() your wordlist with spaces to get your

original sentence back.



if char.isdigit():
   num = int(char) + 1
   a_string[index] = str(num)
print a_string


If you need to modify them - by adding 1, for example - you need to 

 refer to them by index instead, and the quickest way to do that is
 for x in range(len(words)):  print words[x].

Or use the enumerate() function you started with

HTH,

Alan G.

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


Re: [Tutor] Homework problem

2011-07-20 Thread Steven D'Aprano

Ken Baclig wrote:

Hi,

I'm trying to make a function that receives text (a string) as an argument and 
returns the same text (as string), but with 1 added to each word that is a 
number.



What counts as a number? In the string:

Hello world 1234 ham spam

which of these do you expect to get back?

Hello world 2345 ham spam  # Add one to each digit.
Hello world 1235 ham spam  # Add one to each number.

What about strings without spaces like foo123bar?

I'm going to assume that you mean to add 1 to any digit, not just to 
complete numbers.





I need help getting started.

So far, I have:

def FindNumbers(a_string):

for index, char in enumerate(a_string):
if char.isdigit():
a_string[index] = 




This can't work, because strings are immutable. They cannot be changed 
in place. You have to form a new string.


The approach I would take is something like this. Here's a version which 
doesn't actually add one to each digit, but merely turns any digit into 
an asterisk. Your job is to make it do what it is supposed to do.



def add_one_to_digits(a_string):
# Give the function a name that says what it does. It doesn't
# just FindNumbers, that name is completely inappropriate.
result = []  # Holder to build up a new string.
for char in a_string:
if char.isdigit():
char = *
# Anything else gets passed through unchanged.
result.append(char)
# Assemble the characters into a string.
return ''.join(result)





def Test():
sometext = I got 432 when I counted, but Jim got 433 which is a lot foronly 6 
cats, or were there 12 cats?
FindNumbers(sometext)

Test()



Test functions should actually test something. Try this instead:


def test():
source = (I got 432 when I counted, but Jim got 433 which 
  is a lot for only 6 cats, or were there 12 cats?)
expected = (I got 543 when I counted, but Jim got 544 which 
  is a lot for only 7 cats, or were there 23 cats?)
actual = add_one_to_digits(source)
if actual == expected:
print(Test passes!)
else:
print(Test fails!)
print(Expected '%s' % expected)
print(but actually got '%s' % actual)



By the way, the test case is not very good, because there is one digit 
which is special compared to the others, and it doesn't get tested. 
Think about it... out of the 10 possible digits, 9 of them are obvious: 
0 - 1

1 - 2
2 - 3 etc.

but one digit is not obvious. Can you see which one? So you need to 
include that digit in the test case, so you know it gets handled correctly.





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


Re: [Tutor] questions on encoding

2011-07-20 Thread Steven D'Aprano

Albert-Jan Roskam wrote:

Hi,

I am looking for test data with accented and multibyte characters. I have found 
a good resource that I could use to cobble something together 
(http://www.inter-locale.com/whitepaper/learn/learn-to-test.html) but I was 
hoping somebody knows some ready resource.

I also have some questions about encoding. In the code below, is there a 
difference between unicode() and .decode?


Not functionality-wise. unicode may be slightly faster, on account of 
being a function rather than a method, for small strings. But in Python 
3, unicode is gone as no longer needed.




s = §ÇǼÍÍ
x = unicode(s, utf-8)
y = s.decode(utf-8)
x == y # returns True


The fact that this works at all is a fluke, dependent on the settings of 
your terminal.  If I copy and paste the line


s = §ÇǼÍÍ

into my terminal, with an arbitrarily chosen encoding, I get this:

 unicode(s, 'utf-8')
Traceback (most recent call last):
  File stdin, line 1, in module
UnicodeDecodeError: 'utf8' codec can't decode byte 0xa7 in position 0: 
unexpected code byte


(I get a hint that things are not as they should, because the characters 
of s look different too.)


Without knowing what encoding your terminal is set to, it is impossible 
to tell what bytes s *actually* includes. But whatever they are, whether 
they are valid UTF-8 is a matter of chance.





Also, is it, at least theoretically, possible to mix different encodings in 
byte strings? I'd say no, unless there are multiple BOMs or so. Not that I'd 
like to try this, but it'd improve my understanding of this sort of obscure 
topic.


Of course it is! That gives you a broken file, like taking a file 
containing a jpeg and appending it to a file containing an mp3. The 
resultant file is neither a well-formed mp3 nor a well-formed jpeg. 
Unless you have some way of telling where one part ends and the other 
starts, you've just broken your file.


Going back to a terminal with the default encoding (whatever that is!), 
I can do this:


 s = §ÇǼÍÍ  # copy and pasted from your email
 # note the chars look different in my terminal and email client!
...
 a = unicode(s, 'utf-8')  # treat it as UTF-8 bytes
 b = unicode(s, 'utf-16')  # treat it as UTF-16 bytes
 t = a.encode('utf-8') + b.encode('utf-16')  # mix them together
 t
'\xc2\xa7\xc3\x87\xc3\x87\xc2\xbc\xc3\x8d\xc3\x8d\xff\xfe\xc2\xa7\xc3\x87\xc3\x87\xc2\xbc\xc3\x8d\xc3\x8d'
 t.decode('utf-8')
Traceback (most recent call last):
  File stdin, line 1, in module
  File /usr/lib/python2.5/encodings/utf_8.py, line 16, in decode
return codecs.utf_8_decode(input, errors, True)
UnicodeDecodeError: 'utf8' codec can't decode byte 0xff in position 12: 
unexpected code byte

 t.decode('utf-16')
u'\ua7c2\u87c3\u87c3\ubcc2\u8dc3\u8dc3\ufeff\ua7c2\u87c3\u87c3\ubcc2\u8dc3\u8dc3'

So the mixed bytes t does *not* make valid utf-8, but it happens to make 
valid utf-16. That's an accident of the particular bytes that happened 
to be in the string s. There's no guarantee that it will always work, 
but even when it does, you rarely get a sensible string of characters.


In the same way, a random chunk of bytes from an mp3 file might, by 
chance, happen to make up a valid jpeg file -- but it almost certainly 
won't make a nice picture, rather just a blob of random pixels.




--
Steven

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


Re: [Tutor] little/big endian was Re: what is 'doubleword alignment'?

2011-07-20 Thread Dave Angel

On 07/19/2011 05:43 PM, Alan Gauld wrote:

Albert-Jan Roskam wrote:
 and ctypes to process the data in python. It works now, although I
 still want to read more about this. Where does the distinction
 little/big endian enter this story?

That's to do with which bit in a byte/word is most significant.

e.g. is the decimal value 1 stored as

0001   # the one on the right hand nibble
or as
0001   # the one on the left hand nibble

Now scale that up to word sized numbers...
Different CPUs do it differently.

I can't recall which is which - I'm sure wikipedia will
reveal all! :-)
Little-endian is the method used by the Intel processor (such as the 
Pentium).  Big-endian is the system used by most network protocols, as 
well as the 68000 and many other processors.


For our purposes, it's the ordering of the bytes within a 16 or 32 bit 
number.  Little-endian puts the least significant byte first, while 
big-endian puts the most significant byte first.


--

DaveA

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


Re: [Tutor] Homework problem

2011-07-20 Thread Ken Baclig
The instructions are to add one to each number.  So the expected result would 
be:

I got 433 when I counted, but Jim got 434 which is a lot for only 7 cats, or 
were there 13 cats?




From: Steven D'Aprano st...@pearwood.info
To: tutor@python.org tutor@python.org
Sent: Wednesday, July 20, 2011 6:11 PM
Subject: Re: [Tutor] Homework problem

Ken Baclig wrote:
 Hi,
 
 I'm trying to make a function that receives text (a string) as an argument 
 and returns the same text (as string), but with 1 added to each word that is 
 a number.


What counts as a number? In the string:

Hello world 1234 ham spam

which of these do you expect to get back?

Hello world 2345 ham spam  # Add one to each digit.
Hello world 1235 ham spam  # Add one to each number.

What about strings without spaces like foo123bar?

I'm going to assume that you mean to add 1 to any digit, not just to complete 
numbers.



 I need help getting started.
 
 So far, I have:
 
 def FindNumbers(a_string):
 
     for index, char in enumerate(a_string):
         if char.isdigit():
 a_string[index] = 



This can't work, because strings are immutable. They cannot be changed in 
place. You have to form a new string.

The approach I would take is something like this. Here's a version which 
doesn't actually add one to each digit, but merely turns any digit into an 
asterisk. Your job is to make it do what it is supposed to do.


def add_one_to_digits(a_string):
    # Give the function a name that says what it does. It doesn't
    # just FindNumbers, that name is completely inappropriate.
    result = []  # Holder to build up a new string.
    for char in a_string:
        if char.isdigit():
            char = *
        # Anything else gets passed through unchanged.
        result.append(char)
    # Assemble the characters into a string.
    return ''.join(result)




 def Test():
     sometext = I got 432 when I counted, but Jim got 433 which is a lot 
foronly 6 cats, or were there 12 cats?
     FindNumbers(sometext)
 
 Test()


Test functions should actually test something. Try this instead:


def test():
    source = (I got 432 when I counted, but Jim got 433 which 
              is a lot for only 6 cats, or were there 12 cats?)
    expected = (I got 543 when I counted, but Jim got 544 which 
              is a lot for only 7 cats, or were there 23 cats?)
    actual = add_one_to_digits(source)
    if actual == expected:
        print(Test passes!)
    else:
        print(Test fails!)
        print(Expected '%s' % expected)
        print(but actually got '%s' % actual)



By the way, the test case is not very good, because there is one digit which is 
special compared to the others, and it doesn't get tested. Think about it... 
out of the 10 possible digits, 9 of them are obvious: 0 - 1
1 - 2
2 - 3 etc.

but one digit is not obvious. Can you see which one? So you need to include 
that digit in the test case, so you know it gets handled correctly.




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