Re: [Tutor] samples on sort method of sequence object.

2010-01-13 Thread Albert-Jan Roskam
Interesting. Can this also be used to make sorting of alphanumerical list items 
in a 'numerical' way easier? E.g.:
>>> x
['var_0', 'var_13', 'var_11', 'var_9', 'var_4', 'var_1', 'var_5', 'var_6', 
'var_7', 'var_14', 'var_2', 'var_3', 'var_8', 'var_10', 'var_12']
>>> x.sort()
>>> x
['var_0', 'var_1', 'var_10', 'var_11', 'var_12', 'var_13', 'var_14', 'var_2', 
'var_3', 'var_4', 'var_5', 'var_6', 'var_7', 'var_8', 'var_9']
>>> 
 
This clearly does not give the desired result. I once solved this by making a 
dictionary of which the keys are zfilled versions of the list items (e.g. 
var_01, etc), then sort the keys, get the dictionary values in the right order, 
etc. It seemed a cumbersome way. Isn't there an easier way to sort x?

Cheers!!
Albert-Jan

~~
In the face of ambiguity, refuse the temptation to guess.
~~

--- On Tue, 1/12/10, Kent Johnson  wrote:


From: Kent Johnson 
Subject: Re: [Tutor] samples on sort method of sequence object.
To: "Make Twilight" 
Cc: tutor@python.org
Date: Tuesday, January 12, 2010, 7:53 PM


On Tue, Jan 12, 2010 at 9:39 AM, Make Twilight  wrote:

>  I can understand how to use parameters of cmp and reverse,except the
> key parameter...
>  Would anyone give me an example of using sort method with key parameter?

http://personalpages.tds.net/~kent37/kk/7.html
Kent
___
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] samples on sort method of sequence object.

2010-01-13 Thread Albert-Jan Roskam
Very concise == neat solutions. Yummy. ;-) Thanks for your replies!

Cheers!!
Albert-Jan

~~
In the face of ambiguity, refuse the temptation to guess.
~~

--- On Wed, 1/13/10, Sander Sweers  wrote:


From: Sander Sweers 
Subject: Re: [Tutor] samples on sort method of sequence object.
To: "Albert-Jan Roskam" 
Cc: "*tutor python" 
Date: Wednesday, January 13, 2010, 2:14 PM


2010/1/13 Albert-Jan Roskam 
>
> Interesting. Can this also be used to make sorting of alphanumerical list 
> items in a 'numerical' way easier?
> >>> x
> ['var_0', 'var_13', 'var_11', 'var_9', 'var_4', 'var_1', 'var_5', 'var_6', 
> 'var_7', 'var_14', 'var_2', 'var_3', 'var_8', 'var_10', 'var_12']

Yes.
>>> x
['var_0', 'var_13', 'var_11', 'var_9', 'var_4', 'var_1', 'var_5',
'var_6', 'var_7', 'var_14', 'var_2', 'var_3', 'var_8', 'var_10',
'var_12']
>>> sorted(x, key=lamda x: int(x.strip('var_')))
SyntaxError: invalid syntax
>>> sorted(x, key=lambda x: int(x.strip('var_')))
['var_0', 'var_1', 'var_2', 'var_3', 'var_4', 'var_5', 'var_6',
'var_7', 'var_8', 'var_9', 'var_10', 'var_11', 'var_12', 'var_13',
'var_14']

Greets
Sander



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


Re: [Tutor] language aid

2010-02-04 Thread Albert-Jan Roskam
Hi,
 
A dictionary (associative array of keys and values) seems a good datatype to 
use.
vocab = {}
vocab[frenchword] = englishword
 
For instance:
>>> vocab = {"aimer": "love"}
>>> vocab
{'aimer': 'love'}
>>> vocab["parler"] = "speak"
>>> vocab
{'aimer': 'love', 'parler': 'speak'}
>>> for engword, frword in vocab.iteritems():
 print "%s - %s" % (engword, frword)
 aimer - love
parler - speak
 
But if one word has different meanings in the other language, you may need to 
use a list of words as the values.

Cheers!!
Albert-Jan

~~
In the face of ambiguity, refuse the temptation to guess.
~~

--- On Thu, 2/4/10, Owain Clarke  wrote:


From: Owain Clarke 
Subject: [Tutor] language aid
To: tutor@python.org
Date: Thursday, February 4, 2010, 11:43 AM


Hello, all.

I am a newcomer to Python, and I know that I have much learning to do before I 
implement my idea, but I am working on the beginnings of a vocabulary building 
program. This is how I am catching new words at the moment.

def newVocab(x,y):
"""
Add new word pair, English word second.
Words to be separated by ':' """
x.append(y)

I can follow this with

french = []
newVocab(french,"souris:mouse")

The idea is that eventually the user would be prompted to enter a french (or 
whatever) word, and then to enter its English equivalent. The code would then 
be responsible for adding the ":" storing all the words in a list which would 
be written to a file, and recovering them as needed. Although i don't know how 
at this point, I assume I can search the part of the string before the colon or 
after, and thus set up vocabulary tests.

My question is, that if I proceed like this I will end up with a single list of 
potentially several hundred strings of the form "frword:engword". In terms of 
performance, is this a reasonable way to do it, or will the program 
increasingly slow down?

All suggestions appreciated


Owain Clarke

___
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] Coin flip game

2010-02-12 Thread Albert-Jan Roskam
Hi,

random.choice offers an intuitive way to write the code:

import random
for i in range(10):
    print random.choice(["head", "tail"])

Cheers!!

Albert-Jan



~~

In the face of ambiguity, refuse the temptation to guess.

~~

--- On Fri, 2/12/10, David  wrote:

From: David 
Subject: Re: [Tutor] Coin flip game
To: tutor@python.org
Date: Friday, February 12, 2010, 1:49 AM

Hello Lawrence,

let me try to clarify this (warning: am a beginner myself).

On 12/02/10 06:15, Jones, Lawrence D wrote:
> Hi,
>
> I'm new to the list and to Python. I'm reading through Michael Dawson's 
> 'Python programming: for absolute beginners' and at the end of chapter 3 he's 
> set a challenge where the reader has to create a coin flip game. My code now 
> works, but only after I randomly switched pieces of the code around and, 
> basically, pulled my hair out because it wouldn't work.
>
> My code is below. But can someone please explain to me why the following 
> variable has to be placed where it is for the code to work? I thought it 
> would need to go nearer the start of the code i.e. just before heads = 0, 
> tails = 0 etc:
>
>                  coin = random.randrange(2)

Python runs through your code, step by step. I believe it starts at the 
top and goes down, following the logic of your code. When you make 
Python refer to a variable in your while loop that Python has not 
encountered yet, then it will not know what to do -- and complain about 
it. Solution: let Python know of the variable _before_ you then start to 
work with it.

>
> Also, why does the randrange integer have to be '2'? I only discovered this 
> worked by complete accident. I tried '1' and '0,1' as my integers but they 
> just didn't work.

That is because your coin has _two_ sides, and you therefore want a 
random choice out of _two_ possibilities. With the random.randrange(2) 
function the choices will be 0 and 1, satisfying your demands. This 
means that the randrange() function goes up to, but not including, the 
integer you supply. It amounts to two choices in the end all the same 
because the counting starts with 0 instead of 1.
That is, if you chose randrange(1) you will get only one answer, namely 
0. If you type randrange(0) then you will get an error message 
(ValueError: empty range for randrange). Which makes sense. Remember, 
randrange() goes up to, but not including the integer supplied.

HTH,

David










>
> Thanks,
>
> Lawrence
>
>
> import random
> print "The Coin Flip Game\n"
>
> heads = 0
> tails = 0
> count = 0
>
> while count<  100:
>      coin = random.randrange(2)
>      if coin == 0:
>          heads = heads + 1
>      else:
>          tails = tails + 1
>      count += 1
>
> print "Heads: ", heads
> print "Tails: ", tails
>
> raw_input("\nPress enter to exit.")
>
>
>
>
>
>
>
> ___
> 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


Re: [Tutor] Bowing out

2010-03-03 Thread Albert-Jan Roskam
Hi Kent,

Thank you very much for sharing your knowledge. Much appreciated!

Cheers!!

Albert-Jan



~~

In the face of ambiguity, refuse the temptation to guess.

~~

--- On Wed, 3/3/10, Kent Johnson  wrote:

From: Kent Johnson 
Subject: [Tutor] Bowing out
To: Tutor@python.org
Date: Wednesday, March 3, 2010, 2:17 PM

Hi all,

After six years of tutor posts my interest and energy have waned and
I'm ready to move on to something new. I'm planning to stop reading
and contributing to the list. I have handed over list moderation
duties to Alan Gauld and Wesley Chun.

Thanks to everyone who contributes questions and answers. I learned a
lot from my participation here.

So long and keep coding!
Kent
___
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] Encoding

2010-03-04 Thread Albert-Jan Roskam
Hi,
 
For everybody who's having trouble understanding encoding, I found this page 
useful:
http://evanjones.ca/python-utf8.html

Cheers!!
Albert-Jan

~~
In the face of ambiguity, refuse the temptation to guess.
~~

--- On Thu, 3/4/10, spir  wrote:


From: spir 
Subject: Re: [Tutor] Encoding
To: tutor@python.org
Date: Thursday, March 4, 2010, 8:01 AM


On Wed, 3 Mar 2010 20:44:51 +0100
Giorgio  wrote:

> Please let me post the third update O_o. You can forgot other 2, i'll put
> them into this email.
> 
> ---
> >>> s = "ciao è ciao"
> >>> print s
> ciao è ciao
> >>> s.encode('utf-8')
> 
> Traceback (most recent call last):
>   File "", line 1, in 
>     s.encode('utf-8')
> UnicodeDecodeError: 'ascii' codec can't decode byte 0xe8 in position 5:
> ordinal not in range(128)
> ---
> 
> I am getting more and more confused.

What you enter on the terminal prompt is text, encoded in a format (ascii, 
latin*, utf*,...) that probably depends on your system locale. As this format 
is always a sequence of bytes, python stores it as a plain str:
>>> s = "ciao è ciao"
>>> s,type(s)
('ciao \xc3\xa8 ciao', )
My system is parametered in utf8. c3-a8 is the repr of 'é' in utf8. It needs 2 
bytes because of the rules of utf8 itself. Right?

To get a python unicode string, it must be decoded from its format, for me utf8:
>>> u = s.decode("utf8")
>>> u,type(u)
(u'ciao \xe8 ciao', )
e8 is the unicode code for 'è' (decimal 232). You can check that in tables. It 
needs here one byte only because 232<255.

[comparison with php]

> Ok, now, the point is: you (and the manual) said that this line:
> 
> s = u"giorgio è giorgio"
> 
> will convert the string as unicode.

Yes and no: it will convert it *into* a  string, in the sense of a 
python representation for universal text. When seeing u"..." , python will 
automagically *decode* the part in "...", taking as source format the one you 
indicate in a pseudo-comment on top of you code file, eg:
# coding: utf8
Else I guess the default is the system's locale format? Or ascii? Someone knows?
So, in my case u"giorgio è giorgio" is equivalent to "giorgio è 
giorgio".decode("utf8"):
>>> u1 = u"giorgio è giorgio"
>>> u2 = "giorgio è giorgio".decode("utf8")
>>> u1,u2
(u'giorgio \xe8 giorgio', u'giorgio \xe8 giorgio')
>>> u1 == u2
True

> But also said that the part between ""
> will be encoded with my editor BEFORE getting encoded in unicode by python.

will be encoded with my editor BEFORE getting encoded in unicode by python
-->
will be encoded *by* my editor BEFORE getting *decoded* *into* unicode by python

> So please pay attention to this example:
> 
> My editor is working in UTF8. I create this:
> 
> c = "giorgio è giorgio" // This will be an UTF8 string because of the file's
> encoding
Right.
> d = unicode(c) // This will be an unicode string
> e = c.encode() // How will be encoded this string? If PY is working like PHP
> this will be an utf8 string.

Have you tried it?
>>> c = "giorgio è giorgio" 
>>> d = unicode(c)
Traceback (most recent call last):
  File "", line 1, in 
UnicodeDecodeError: 'ascii' codec can't decode byte 0xc3 in position 8: ordinal 
not in range(128)

Now, tell us why! (the answer is below *)

> Can you help me?
> 
> Thankyou VERY much
> 
> Giorgio


Denis

(*)
You don't tell which format the source string is encoded in. By default, python 
uses ascii (I know, it's stupid) which max code is 127. So, 'é' is not 
accepted. Now, if I give a format, all works fine:
>>> d = unicode(c,"utf8")
>>> d
u'giorgio \xe8 giorgio'

Note: unicode(c,format) is an alias for c.decode(format):
>>> c.decode("utf8")
u'giorgio \xe8 giorgio'


la vita e estrany

spir.wikidot.com

___
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] Really learn programming

2010-03-08 Thread Albert-Jan Roskam
This very funny cartoon is about learning C++ in 21 days, but it could be about 
any language: http://high5.net/comic/AG/ars_longa_vita_brevis.PNG


Cheers!!

Albert-Jan



~~

In the face of ambiguity, refuse the temptation to guess.

~~

--- On Mon, 3/8/10, Alan Gauld  wrote:

From: Alan Gauld 
Subject: Re: [Tutor] Really learn programming
To: tutor@python.org
Date: Monday, March 8, 2010, 7:43 PM


"Wayne Werner"  wrote

> a link to this interesting site about programming in 21 days (or not).
> 
> http://norvig.com/21-days.html
> 
> I thought it was rather interesting

Indeed, although I disagree with some of it. (Including some of his "Answers")

And it focusses on programming only, which is a bit like focussing on 
bricklaying
when considering the building of bridges.
You can only build part of a structure if all you can do is lay bricks... To 
engineer
a complete solution takes many other skills too. Software Engineering is more
than programming.

But if you only need to build a garden shed or dog kennel then you don't
need to go on a Civil Engineering course or even a bricklayers apprenticeship.
I don't think the "Learn Z in 21 days" books are aimed at professionals, they 
are
aimed at amateurs who usually only need to build kennels!

So yes professional grade programming takes a lifetime to learn (not least
because it is constantly changing!) but an amateur can meet all their needs
much more quickly and have a lot of fun in doing so.

-- 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 do I find information about a Python object.

2010-03-30 Thread Albert-Jan Roskam
Hi,

Are you looking for the inspect module?
 
import inspect
help(inspect)
cls = str
inspect.classify_class_attrs(cls)

Cheers!!

Albert-Jan



~~

In the face of ambiguity, refuse the temptation to guess.

~~

--- On Tue, 3/30/10, Mike Baker  wrote:

From: Mike Baker 
Subject: [Tutor] How do I find information about a Python object.
To: tutor@python.org
Date: Tuesday, March 30, 2010, 5:23 PM

Hi,
 
I've downloaded IDLE python for windows.  I've also downloaded Eclipse with the 
python addition. I have simple programs that will run on both IDLE and Eclipse. 
How do I get more information about a object/variable, such as proc in the 
example below.

 
For example, if I execute the following:
    >>> proc = subprocess.Popen(['C:\\Progra~1\\putty\\plink','Catt'],
    shell=False)
  I can remote log into our Linux machine named 'Catt'.
 
 
How do I find a list of attributes for the object/variable proc?  I've tried 
subprocess.__doc__ and subprocess.Popen.__doc__.
 
Random Googling shows that there are things like process identification numbers 
available - such as proc.pid.  How do I find the other options?
 
Thanks,
 
Mike

-Inline Attachment Follows-

___
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] ask-why I cannot run it, and I am so confused about the traceback

2010-04-07 Thread Albert-Jan Roskam
What's with Pythonistas and colours? 
http://www.mail-archive.com/python-l...@python.org/msg231447.html ;-)))

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?

~~

--- On Wed, 4/7/10, bob gailer  wrote:

From: bob gailer 
Subject: Re: [Tutor] ask-why I cannot run it, and I am so confused about the 
traceback
To: tutor@python.org
Date: Wednesday, April 7, 2010, 3:15 PM

You have the solution. Good.

I beg you to avoid colored text. I find it hard to read.

Just use good old plain text. No fancy fonts, sizes, colors.

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


[Tutor] xml question

2010-07-26 Thread Albert-Jan Roskam
Hi,

I am making a data processing program that will use a configuration file. The 
file should contain information about: (1) source files used, (2) 
(intermediate) output files, (3) used parameters/estimation methods (4) manual 
data edits + datetime stamp + user name . I'd like to store this config file in 
xml. However, I've never created something like this before. Is this a suitable 
format, and, if so, what would the elementtree look like? Should I just use 
'config'  or something similar as root, and the information elements 1 through 
3 as child elements? And should the manual edits be stored as an element 'edit' 
with various attributes (the edit itself, the time stamp, etc.)?

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] xml question

2010-07-27 Thread Albert-Jan Roskam
Thank you all for your replies; they were most useful! I've never made a 
connection between xml and mass-suicide  ---interesting. ;-) Yaml seems a good 
choice (I didn't llook at config parser yet). In my office, we use Python and R 
(among other tools) and therfore it seems best not to use some pure python 
config file format. I agree that the readability/editability (by humans) of a 
yaml file is a huge advantage. Today I experimented with the yaml library of R 
and it felt very intuitive. I expect this will be the same for Python (not in 
the last place because Python is such a *beautiful*, clear language ;-)

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?

~~

--- On Tue, 7/27/10, Mac Ryan  wrote:

From: Mac Ryan 
Subject: Re: [Tutor] xml question
To: tutor@python.org
Date: Tuesday, July 27, 2010, 11:46 AM

On Mon, 2010-07-26 at 12:09 -0700, Albert-Jan Roskam wrote:
> I am making a data processing program that will use a configuration
> file. The file should contain information about: (1) source files
> used, (2) (intermediate) output files, (3) used parameters/estimation
> methods (4) manual data edits + datetime stamp + user name . I'd like
> to store this config file in xml. However, I've never created
> something like this before. Is this a suitable format, and, if so,
> what would the elementtree look like? Should I just use 'config'  or
> something similar as root, and the information elements 1 through 3 as
> child elements? And should the manual edits be stored as an element
> 'edit' with various attributes (the edit itself, the time stamp,
> etc.)?

I am with Steven on the fact that XML might not necessarily be the best
choice, unless you plan to use the configuration file with other
third-party programs, in which case the fact that XML has built-in
parsing libs for nearly all languages makes life of fellow developer
easier.

For the next project of mines, I am planning to use YAML (YAML Ain't a
Markup Language). I stumbled upon this format while toying around with
the google app engine, that uses it for storing your application
configuration data. IMO, YAML has the following prominent advantages:
   1) It is easy to read and edit by humans [think "markdown"]
   2) It has solid parsing libraries for Python
   3) It is far less verbose than XML
   4) It is consistent with Python "relevant whitespaces" [indentation
is used to define data structure hierarchy]

I have not yet got to the stage of writing code that use YAML (will take
two more weeks at best for me to get to that point), but should you go
down this road, I would love to hear back from you. [As I would love to
hear from anybody else who might have experience with YAML+Python]

Some links:
- http://en.wikipedia.org/wiki/YAML [good overview of the format]
- http://www.yaml.org/ [official site... when consistency with the
format makes a website hard to browse!]
- http://pyyaml.org/wiki/PyYAMLDocumentation [documentation of the
library for Python - pure Python code // can use a C library]

An example of YAML file to give you the taste of it:
> receipt:     Oz-Ware Purchase Invoice
> date:        2007-08-06
> 
> customer:
>     given:   Dorothy
>     family:  Gale
> 
> items:
>     - part_no:   A4786
>       descrip:   Water Bucket (Filled)
>       price:     1.47
>       quantity:  4
>     - part_no:   E1628
>       descrip:   High Heeled "Ruby" Slippers
>       price:     100.27
>       quantity:  1
> 
> bill-to:  &id001
>     street: |
>             123 Tornado Alley
>             Suite 16
>     city:   East Westville
>     state:  KS
> 
> ship-to:  *id001
> 
> specialDelivery:  >
>     Follow the Yellow Brick
>     Road to the Emerald City.
>     Pay no attention to the
>     man behind the curtain.

HTH,
Mac.


___
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] os.access unreliable?

2010-08-25 Thread Albert-Jan Roskam
Hi,

Hi I'm using os.access to do a preliminary check to see if I have RW access, 
but 
it seems to be unreliable. In a dir for which I have only read access, 
os.access 
also says I have write access. This is under Windows 2000. I could of course 
use 
a try-except and catch the IOError, but I'd like to know why the code below 
isn;t working.

    def isAcccessible(self):
    if os.access(self.path, os.R_OK) and os.access(self.path, os.W_OK):
    return True
    else:
    return False
 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] os.access unreliable?

2010-08-25 Thread Albert-Jan Roskam
Hi Tim and Steven,

Thanks a lot for your very useful replies!
 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: Tim Golden 
Cc: tutor@python.org
Sent: Wed, August 25, 2010 12:25:13 PM
Subject: Re: [Tutor] os.access unreliable?

On 25/08/2010 11:15, Steven D'Aprano wrote:
> It also warns that os.access doesn't take into account network file
> sharing permissions.

Heh. On Windows it doesn't take into account *any* file sharing
permissions :)

TJG
___
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] wx to convert Group 4 Tifs to Gifs

2010-08-26 Thread Albert-Jan Roskam
Hi,

I have a small application, written in Tkinter. It is supposed to display a tif 
image, among other things. The problem is, PIL won't decode group 4 Tifs (G4 
TIF).
The tifs that I have are about 125kb and contain two pages per file. They're 
scanned, monochrome files. I need the second page of the tif, more specifically 
the lower half 

of the second page. I will have 5000-6000 tifs eventually, to manual conversion 
is not an option.

While googling around, I found many other people who faced the same problem. 
Since I cannot download executables here, I decided to use wx.
Ideally, I'd like an object that can be handled with Tkinter.PhotoImage, but 
I'm 
also very happy with a simple program that converts from G4 tif to Gif.
I have zero knowledge from wx. Therefroe, I'd really appreciate some pointers 
as 
to how to make the program below work.

import wx, os
def tif2gif(infile):
  """ This does *not* work """
  SECONDPAGE = 1
  outfile = os.path.splitext(infile[0] + ".gif")
  image   = wx.Image(name = infile, type = wx.BITMAP_TYPE_TIF, index = 
SECONDPAGE) 

  bitmap  = wx.BitmapFromImage(self.image)
  bitmap.SaveFile(outfile", wx.BITMAP_TYPE_GIF)
tif2gif(infile  = "d:/temp/somefile.tif")
 
Thank you very much in advance!
 
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


[Tutor] more on wx and tiff

2010-08-27 Thread Albert-Jan Roskam
Hi again,

Some more questions about tiff conversion. 

First, thanks for your previous replies. I cannot use IrfanView any time soon, 
nor will my boss switch to Linux. 


So I'm trying to do the conversion from tiff to png using the wx package (gif 
would also be fine, but that won't work due to copyright issues -- I pulled 
about half of my hairs out before I found that out ;-). I have to access the 
second page of the tiff file, then write it to a png file. The code below (see 
also http://codepad.org/CV9xZXgi) works for one file only. Two questions:

1--- the program gives a 'tiff library warning', which is related to multipage 
tiff files (http://libtiff.maptools.org/man/TIFFReadDirectory.3tiff.html), but 
it generates the same warning with a single page tiff. The warning is given in 
a 
little menu (annoying!). How can I prevent or suppress this warning? This is 
the 
complete warning log:
17:49:54: tiff module: TIFFReadDirectory
17:49:54: TIFF library warning.
17:49:54: tiff module: TIFFReadDirectory
17:49:54: TIFF library warning.
17:49:54: tiff module: TIFFReadDirectory
17:49:54: TIFF library warning.

2--- the program is not closed properly (despite of the destroy() call). What 
should I do to correct this?

As always, thanks in advance for your replies.

Best wishes,
Albert-Jan


import wx, Image
import os, glob

class MyFrame(wx.Frame):
def __init__(self, parent, id, title):
self.tifLoc = tifLoc
self.debug = True
wx.Frame.__init__(self, parent, id, title, size = (600, 800))
SECONDPAGE = 1
self.image = wx.Image(name = self.tifLoc, type = wx.BITMAP_TYPE_TIF, 
index = SECONDPAGE)
if self.debug:
h, w = self.image.GetSize()
print "Reading file '%s' of %s x %s pixels" % (self.tifLoc, h, w )
self.bitmap = wx.BitmapFromImage(self.image)
wx.EVT_PAINT(self, self.OnPaint)
self.Centre()

def OnPaint(self, event):
dc = wx.PaintDC(self)
dc.DrawBitmap(self.bitmap, 0, 0)
self.saveIt()

def saveIt(self):
pngLoc = os.path.splitext(self.tifLoc)[0] + ".png"
self.bitmap.SaveFile(pngLoc, wx.BITMAP_TYPE_PNG)

class MyApp(wx.App):

def OnInit(self):
frame = MyFrame(None, -1, 'My file')
frame.Show(False) # do not show pic
self.SetTopWindow(frame)
if frame.debug:
print "... Conversion done!"
return True

path = "d:/temp"
for f in glob.glob(os.path.join(path, "*.tif")):
tifLoc = f
app = MyApp(0)
app.MainLoop()
app.Destroy()

 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] more on wx and tiff

2010-08-27 Thread Albert-Jan Roskam
Hi Wayne,

Yep, I considered using PIL, but that package won't read so-called Group 4 
Tiffs 
[1]. They're two-page, black-and-white scanned forms. I need part of the second 
form (i.e., the backside of a certificate). The page contains some hand-written 
info which needs to be presented in a simple data entry program, which I made 
using Tkinter.

The forms are confidential so they may not leave the company network. And the 
IT 
droids are somewhat paranoid so it's very time-consuming to get some new 
executable 'inside' the company network. Beeeh, formalities... :-(

 
[1] http://www.digitalpreservation.gov/formats/fdd/fdd24.shtml

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: Wayne Werner 
To: Albert-Jan Roskam 
Cc: Python Mailing List 
Sent: Fri, August 27, 2010 6:44:04 PM
Subject: Re: [Tutor] more on wx and tiff


On Fri, Aug 27, 2010 at 11:25 AM, Albert-Jan Roskam  wrote:

Hi again,
>
>Some more questions about tiff conversion. 
>
>First, thanks for your previous replies. I cannot use IrfanView any time soon, 
>nor will my boss switch to Linux. 
>
>

Have you tried using the PIL?
http://www.pythonware.com/products/pil/

import Image
i = Image.open("file.tiff")
i.save(open('file.png', 'w'), filetype='png')

I don't know if that was previously suggested, but it should work on any 
platform with PIL installed. 

HTH,
Wayne


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


Re: [Tutor] more on wx and tiff

2010-08-29 Thread Albert-Jan Roskam
Hi,

Got it. Thought it would be nice to share it. What set me on the wrong foot 
completely was that writing gif files is not possible due to copyright issues.
Below is a very simple version of a conversion function for Group 4 Tif to png.

import wx, os, time

def g4TifToPng(tif, png = None):
if png is None:
png = os.path.splitext(tif)[0] + ".png"
myImg = wx.Image(tif)
if myImg.GetImageCount(tif) == 2:
SECONDPAGE = 1
myImg = wx.Image(tif, type = wx.BITMAP_TYPE_TIF, index = SECONDPAGE)
# myImg = myImg.GetSubImage((0, 2338, 3304, 2338)) # offset h & w, rect 
h & w
myImg.SaveFile(png, wx.BITMAP_TYPE_PNG)
newW, newH = myImg.GetSize()
print "%s:: writing file %s (%s x %s pixels)" % \
  (time.strftime("%H:%M:%S"), png, newW, newH)

g4TifToPng(tif = "c:/temp/somefile.tif")



 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: Albert-Jan Roskam 
To: Wayne Werner 
Cc: Python Mailing List 
Sent: Fri, August 27, 2010 8:00:16 PM
Subject: Re: [Tutor] more on wx and tiff


Hi Wayne,

Yep, I considered using PIL, but that package won't read so-called Group 4 
Tiffs 
[1]. They're two-page, black-and-white scanned forms. I need part of the second 
form (i.e., the backside of a certificate). The page contains some hand-written 
info which needs to be presented in a simple data entry program, which I made 
using Tkinter.

The forms are confidential so they may not leave the company network. And the 
IT 
droids are somewhat paranoid so it's very time-consuming to get some new 
executable 'inside' the company network. Beeeh, formalities... :-(

 
[1] http://www.digitalpreservation.gov/formats/fdd/fdd24.shtml

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: Wayne Werner 
To: Albert-Jan Roskam 
Cc: Python Mailing List 
Sent: Fri, August 27, 2010 6:44:04 PM
Subject: Re: [Tutor] more on wx and tiff


On Fri, Aug 27, 2010 at 11:25 AM, Albert-Jan Roskam  wrote:

Hi again,
>
>Some more questions about tiff conversion. 
>
>First, thanks for your previous replies. I cannot use IrfanView any time soon, 
>nor will my boss switch to Linux. 
>
>

Have you tried using the PIL?
http://www.pythonware.com/products/pil/

import Image
i = Image.open("file.tiff")
i.save(open('file.png', 'w'), filetype='png')

I don't know if that was previously suggested, but it should work on any 
platform with PIL installed. 

HTH,
Wayne



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


Re: [Tutor] Help with Object Oriented Programming

2010-08-31 Thread Albert-Jan Roskam
I read Head First Design Patterns (http://oreilly.com/catalog/9780596007126). 
It 
focuses on Java and it's not only good because of the nice woman on the cover. 
;-)

 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: Tino Dai 
To: Alan Gauld 
Cc: tutor@python.org
Sent: Tue, August 31, 2010 3:43:57 PM
Subject: Re: [Tutor] Help with Object Oriented Programming

On Mon, Aug 30, 2010 at 3:21 PM, Alan Gauld  wrote:
>
> "Tino Dai"  wrote
>
>>  I'm beefing up my Object-Oriented Analysis and Design - getting
>> the gaps in my
>> knowledge filled in through the Head First OOAD book
>
> I don't know it although I've seen a couple of others in the series.
>
> My recommendations for general OOAD books are:
>
> Timothy Budd - OOP. It's not Python but covers the basic principles well.
>
> Grady Booch - OOAD - The second edition is all in C++, The first edition, if
> you can find one,
> is in 5 different langiages and IMHO much better for it. It stops you
> focusing on the
> language and helps focus on the OO principles.
>
> Bruce Eckel - Thinking in Java - One of the very few books on Java that does
> a good
> job of teaching OO. He was going to do a Thinking in Python but I think it
> died :-(
>
> And finally the original Design Patterns book by the Gang of Four. Its a bit
> heavy
> but the information is excellent.
>

I have Design Patterns on my desk. And I will check out the Timothy Budd and
Grady Booch book. I think we might even have the first edition of that
book - a benefit
of working at a library. Thanks Alan!

-Tino
___
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] Begginer Python Problems - Urgent! Due by tomorrow morning.

2010-09-03 Thread Albert-Jan Roskam
Hi,

Just guessing:

x = 12

print (2*x)

def greetings(no):
for i in range (no+1):
print ('Hello ')
print ('Greetings, earthling')

def regreet(no):
for j in range(no+1):
greetings(no)

prompt = "Enter a number: "
no = raw_input(prompt)
regreet(no)

It's not tested because I run Python 2.7 It's generally recommended that novice 
programmers run Py 2.x

 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: Colleen Glaeser 
To: tutor@python.org
Sent: Fri, September 3, 2010 6:11:08 AM
Subject: [Tutor] Begginer Python Problems - Urgent! Due by tomorrow morning.

Dear Python Tutors,

I'm having trouble while typing a beginner program for python.
Due to troublesome circumstances, I had to take pictures of the assignment 
sheet 
on my phone, and work from that.
Although it is all clearly legible, I still think something with my program is 
not working.

If I run the program in the shell, I should be getting something that asks for 
a 
number, and I need to try positive and negative numbers and see what spits out.

However, I'm getting the number 24, and a repetition of the words "Hello" and 
"Greetings, earthlings."

This below is my program, and I've retyped it too many times to remember, while 
looking at the assignment sheet.
What am I doing wrong?  Can anybody help?  D:  I need to turn my results in to 
class tomorrow morning!

# Lab 1
# Programmed by Colleen G.

x = 12

print (2*x)

def greetings():
for i in range (3):
print ('Hello ')
print ('Greetings, earthling')

def regreet():
for j in range(4):
greetings()

regreet()


End of program.help needed quickly!  Thank you!  I am using Python 3.1.2

-- 
Colleen Glaeser
songbird42...@gmail.com 
636.357.8519



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


[Tutor] design question

2010-09-10 Thread Albert-Jan Roskam
Hi,

I've made a small data entry program where a picture of a hand-written, scanned 
form is shown next to several text entries. The design has been largely 'bottom 
up', although of course I started with a rough sketch. I started with the 
following classes: Menu (responsible for the widget creation), Autocomplete (a 
subclass of the tkinter text entry widget which I've found on the internet, I 
wanted to keep this intact), Convert (responsible for the conversion/cropping 
of 
the images, which is somewhat tedious) and Events (here's the pain spot: this 
has become a mixed bag). Originally, the Events class was supposed to contain 
all the commands behind the widgets, but it has slowly gotten more 
responsibilities. Notably, all methods that deal with reading/writing the data 
also live here. The program is about 500 lines of code now, including blanks.

I'd like to rewrite to entire program so that (1) all classes have just one 
responsibility (2) classes connect to as little other classes (so it becomes 
easy to e.g. change to another widget set). I'd love to hear some thoughts or 
advice on the new design from you (I hope my description wasn't too short). Is 
there a specific design pattern that could be used?

One specific, additional question: the program has a title bar that shows 
something like "processing file X (1 of 3; 33%)". My colleague suggested to use 
the Iterator pattern (gang of four) for this. Is this (a) overkill (b) a simple 
opportunity to apply this pattern? 


Thank you in advance for your advice!

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] design question

2010-09-10 Thread Albert-Jan Roskam
Hi Jan,

Here's a screendump of my program: http://nl.tinypic.com/r/2qtlojc/7 . This 
might make my description a little bit clearer. The beautiful sunset will in 
reality be a dull, handwritten form. ;-)

Regarding the iterator pattern, I was referring to this: 
http://www.suttoncourtenay.org.uk/duncan/accu/pythonpatterns.html#iterators-and-generators

Inside my program I have to keep a list of all the image files that are 
scheduled for data entry. The main purpose is to be able to read in the image 
files one by one. Another one of the purposes of this list is to show some 
information on the title bar. Currently, my program only has a 'next' button 
and 
the fact that implementing a 'previous' button is causing problems suggests to 
me that I have to look for a more fundamentally better solution.

 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: Knacktus 
To: tutor@python.org
Sent: Fri, September 10, 2010 3:38:13 PM
Subject: Re: [Tutor] design question

Am 10.09.2010 11:23, schrieb Albert-Jan Roskam:
> Hi,
> 
> I've made a small data entry program where a picture of a hand-written,
> scanned form is shown next to several text entries. The design has been
> largely 'bottom up', although of course I started with a rough sketch. I
> started with the following classes: Menu (responsible for the widget
> creation), Autocomplete (a subclass of the tkinter text entry widget
> which I've found on the internet, I wanted to keep this intact), Convert
> (responsible for the conversion/cropping of the images, which is
> somewhat tedious) and Events (here's the pain spot: this has become a
> mixed bag). Originally, the Events class was supposed to contain all the
> commands behind the widgets, but it has slowly gotten more
> responsibilities. Notably, all methods that deal with reading/writing
> the data also live here. The program is about 500 lines of code now,
> including blanks.
It's difficult to judge from the outside. It sounds to me like a good idea to 
create separate classes or modules for reading and writing.
> 
> I'd like to rewrite to entire program so that (1) all classes have just
> one responsibility (2) classes connect to as little other classes (so it
> becomes easy to e.g. change to another widget set). I'd love to hear
> some thoughts or advice on the new design from you (I hope my
> description wasn't too short). Is there a specific design pattern that
> could be used?
> 
> One specific, additional question: the program has a title bar that
> shows something like "processing file X (1 of 3; 33%)". My colleague
> suggested to use the Iterator pattern (gang of four) for this. Is this
> (a) overkill (b) a simple opportunity to apply this pattern?
For what exactly do you want to use the pattern?
The iterator pattern is easy to implement in Python. And the usage of iterators 
is even more easy (you can use them in loops without calling the next() method 
explicitly). Lists among others are iterator types. You can create your own 
with 
generator functions or of course implement the iterator-protocol. See here:

http://docs.python.org/library/stdtypes.html#iterator-types

Generally iterator types and generators are great features of Python. For a 
more 
concrete advice you'd need to explain a bit further what you need to do.

HTH

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


Re: [Tutor] design question

2010-09-11 Thread Albert-Jan Roskam
Hi Jan and Steven,

Thanks a lot for your valuable comments. I greatly appreciate you looking into 
it.

Steven, you recommend to <>. On 
one website it was mentioned that "To a young boy with a hammer, everything 
looks like a nail". Which meant to say that just because you CAN use a design 
pattern, it doesn't mean you SHOULD. This corresponds with your advice!

 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: Steven D'Aprano 
To: tutor@python.org
Sent: Sat, September 11, 2010 10:40:17 AM
Subject: Re: [Tutor] design question

On Sat, 11 Sep 2010 12:11:37 am Albert-Jan Roskam wrote:

> Inside my program I have to keep a list of all the image files that
> are scheduled for data entry. 

Sounds like you need to keep a list of all the image files that are 
scheduled for data entry then.


> The main purpose is to be able to read 
> in the image files one by one. Another one of the purposes of this
> list is to show some information on the title bar. 

No it isn't. You don't need a list of image files in order to show "some 
information" in the title bar (unless that information is the list of 
files). From your earlier post, you want to show:

filename 23 of 245 (9.4%)

or similar. For that, all you need is three pieces of information:

* the name of the current file
* the number of the current file
* the total number of files

You don't need all 245 files for that.

I'm being pedantic here. Obviously you need the list of files 
*somewhere*, and you need it for other reasons, but you don't *need* a 
list of 243 files in order to show the name of one of them!

You may decide that it is *convenient* to give the function which 
changes the title bar access to the entire list, but it's not a 
*requirement*. Think about alternatives: 

def change_titlebar(name, count, total):
template = "%s %d of %d (%.1f %%)"
percentage = count*100.0/total
title = template % (name, count, total, percentage)
do_magic_with(title)  # whatever it takes to change the title

It doesn't need the entire list.


> Currently, my 
> program only has a 'next' button and the fact that implementing a
> 'previous' button is causing problems suggests to me that I have to
> look for a more fundamentally better solution.

Sounds to me that you *don't* want the iterator pattern. The iterator 
pattern generally means that you step forward through each item. If you 
want to advance backwards or forwards, something with random access is 
probably better. That would probably mean a list.

Don't be too hung up about "design patterns". Most design patterns just 
exist to work around limitations of the language, lack of power, or 
slavish devotion to object oriented programming when the problem is 
crying out for a functional or procedural solution. Don't be afraid to 
write functions instead of classes -- there's no need for a 
TitlebarManipulator class just to change the titlebar.

My solution would be to keep *two* pieces of information:

* the list of filenames;
* the current position in that list

Set the current position to 0, and have the Next button add one to the 
position and refresh the page, and Prev subtract one and refresh the 
page. Obviously you need code to ensure that the position doesn't go 
out of bounds, code to save the data in the fields, and so forth. The 
page refresh code should do something like:

* given the current position, extract the filename to use;
* change the title bar;
* open and display the appropriate image;
* pre-populate any fields;
etc.

Good luck!


-- 
Steven D'Aprano
___
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] (de)serialization questions

2010-09-30 Thread Albert-Jan Roskam
Hi,

I have data about zip codes, street and city names (and perhaps later also of 
street numbers). I made a dictionary of the form {zipcode: (street, city)}
I dumped the dictionary into a marshal file. I have two questions:

The first question is a very basic one: if I deserialize the dictionary it is 
placed into memory. This is fast, but can cause problems for very large 
objects. 
Are there forms of object permanence that do not read all data into memory? 
Does 
the shelve module work this way?

Second, does anybody know of a speed comparison test (e.g., web source) of 
various de/serialization methods? I don't mind if the dumping is somewhat slow. 
Fast loading speed is more important for my purpose.

Btw, I am aware of the portability problems of marshal files. I found that, 
compared to a shelve object, dumping is *much* faster,  loading is slightly 
faster,  look-ups are scarily fast, and file size is about half. 


 
I am using Python 2.5 and installing non-standard modules is a problem for the 
IT droids in my office.

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] inheritance problem

2010-09-30 Thread Albert-Jan Roskam

>> And so forth. Laborious? Time consuming? Lots of detail? Yes. Most of
>> us have gone thru the same thing in our education.

 
---> You forgot: 'Lots of frowning', 'lots of sixpacks' and 'lots of FUN' ;-)))


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: Bob Gailer 
To: Roelof Wobben ; tutor@python.org
Sent: Thu, September 30, 2010 9:12:48 PM
Subject: Re: [Tutor] inheritance problem

Sorry I hit the send button instead of the save button

On Thu, Sep 30, 2010 at 2:58 PM, Bob Gailer  wrote:
> On Thu, Sep 30, 2010 at 2:38 PM, Roelof Wobben  wrote:
>>
>> hello,
>>
>> Im following this page : 
>>http://openbookproject.net/thinkcs/python/english2e/ch17.html
>>
>> [snip]
>>
>> What went wrong here.
>
IMHO you are tackling a very large program while still not getting
Python basics and still not walking through the program

I once again am weary of holding your hand.

I suggest (again) that you walk through the program BY HAND step by
step, analyzing and understanding what happens at each step.

There are so many problems in your code even if I told you exactly
what's wrong you will just get the next error-or-unexpected result
then the next, and so on.

I will start you:

game = CardGame()
# leads to
class CardGame:
   def __init__(self):
   self.deck = Deck()
#leads to
class Deck:
   def __init__(self):
   self.cards = [] # an empty list is bound to the instance
attribute "cards"
   for suit in range(4):
   for rank in range(1, 14):
   self.cards.append(Card(suit, rank)) # one-by-one Card
instances are appended to self.cards
#leads to
class Card:
   suits = ["Clubs", "Diamonds", "Hearts", "Spades"] # a list of suit
names is bound to class attribute suits
   ranks = ["narf", "Ace", "2", "3", "4", "5", "6", "7",
"8", "9", "10", "Jack", "Queen", "King"] # a list of rank
names is bound to class attribute ranks
   def __init__(self, suit=0, rank=0):
   self.suit = suit # the suit number passed as an argument is
bound to the Card instance suit
   self.rank = rank # the rank number passed as an argument is
bound to the Card instance rank
   # the result is a Card instance with two attributes, suit and rank.

And so forth. Laborious? Time consuming? Lots of detail? Yes. Most of
us have gone thru the same thing in our education.

Regarding the error:

AttributeError: Deck instance has no attribute 'pop'

You should be able by now to know what that means and why it is happening.

A lot of us on this list are spending a lot of time "helping" you. I
for one again am imploring you to spend more time gaining
understanding and asking us for less help of this nature.

Others may disagree - that is my opinion.

-- 
Bob Gailer
919-636-4239
___
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] (de)serialization questions

2010-10-01 Thread Albert-Jan Roskam
Hi Lee,

Thanks for your response.

Maybe my main question is as follows: what permanent object is most suitable to 
store a large amount of entries (maybe too many to fit into the computer's 
memory), which can be looked up very fast.

Eventually, I want to create two objects:
1-one to look up street name and city using zip code
2-one to look up zip code using street name, apartment number and city

I stored object1 in a marshalled dictionary. Its length is about 450.000 (I 
live 
in Holland, not THAT many streets). Look-ups are incredibly fast (it has to, 
because it's part of an autocompletion feature of a data entry program). I 
haven't got the street number data needed for object2 yet, but it's going to be 
much larger. Many streets have different zip codes for odd or even numbers, or 
the zip codes are divided into street number ranges (for long streets).

You suggest to simply use a file. I like simple solutions, but doesn't that, by 
definition, require a slow, linear search? I chose a dictionary for object1 
because hash tables are very fast in terms of lookup times. Another option 
might 
be to use the bisect module. Not sure if using a binary search algoritm is much 
faster with these still relatively small numbers. It's interesting for sure, 
but 
I don't know whether using bisect would justify the added amount of complexity 
and bug risks.

Funny you should mention sqlite: I was just considering it yesterday. Gosh, 
Python has so much interesting stuff to offer! 


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: Lee Harr 
To: tutor@python.org
Sent: Fri, October 1, 2010 12:37:58 AM
Subject: Re: [Tutor] (de)serialization questions


> I have data about zip codes, street and city names (and perhaps later also of
> street numbers). I made a dictionary of the form {zipcode: (street, city)}

One dictionary with all of the data?

That does not seem like it will work. What happens when
2 addresses have the same zip code?


> Are there forms of object permanence that do not read all data into memory?

How about a file?

You could write your data one record per line, and then
read it in one line at a time also.



You could also use a csv file, or maybe use a database
like sqlite.

  
___
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] (de)serialization questions

2010-10-03 Thread Albert-Jan Roskam
Hi Lee, Alan and Steven,

Thank you very much for your replies!

First, Lee:
>> That does not seem like it will work. What happens when
>> 2 addresses have the same zip code?

--> Sorry I didn't answer that before. When the zipcode is known, that's not a 
problem. The data typist simply has to enter the zip code and the street number 
and voilà, the street name and city name appear. A big time saver. When the 
zipcode is the UNknown, indeed I need street name, apt number, and city to get 
the right zip code. Without the street number, I might end up with a list of 
zip 
codes. But having no street number would automatically invalidate the given 
address. We couldn't possibly mail a letter without having the apt. number!

I just ordered a book on sqlite this morning 
(http://www.amazon.com/SQLite-Chris-Newman/dp/067232685X/ref=sr_1_2?ie=UTF8&s=books&qid=1256736664&sr=1-2)
 
 It indeed seems like the way to go, also in the wider context of the program. 
It makes much more sense to maintain one database table instead of 3 csv files 
for the three data typists' output.

Alan: I forwarded your book to my office address. I'll print and read it!
Btw, your private website is nice too. Nice pictures! Do you recognize where 
this was taken:http://yfrog.com/n0scotland046j .You're lucky to live in a 
beautiful place like Scotland 


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: Lee Harr 
To: tutor@python.org
Sent: Sat, October 2, 2010 12:56:21 AM
Subject: Re: [Tutor] (de)serialization questions


>>> I have data about zip codes, street and city names (and perhaps later also 
of
>>> street numbers). I made a dictionary of the form {zipcode: (street, city)}
>>
>> One dictionary with all of the data?
>>
>> That does not seem like it will work. What happens when
>> 2 addresses have the same zip code?

You did not answer this question.

Did you think about it?


> Maybe my main question is as follows: what permanent object is most suitable 
to
> store a large amount of entries (maybe too many to fit into the computer's
> memory), which can be looked up very fast.

One thing about Python is that you don't normally need to
think about how your objects are stored (memory management).

It's an advantage in the normal case -- you just use the most
convenient object, and if it's fast enough and small enough
you're good to  go.

Of course, that means that if it is not fast enough, or not
small enough, then you've got to do a bit more work to do.


> Eventually, I want to create two objects:
> 1-one to look up street name and city using zip code

So... you want to have a function like:

def addresses_by_zip(zipcode):
'''returns list of all addresses in the given zipcode'''



> 2-one to look up zip code using street name, apartment number and city

and another one like:

def zip_by_address(street_name, apt, city):
'''returns the zipcode for the given street name, apartment, and city'''



To me, it sounds like a job for a database (at least behind the scenes),
but you could try just creating a custom Python object that holds
these things:

class Address(object):
street_number  = '345'
street_name = 'Main St'
apt = 'B'
city = 'Springfield'
zipcode = '9'

Then create another object that holds a collection of these addresses
and has methods addresses_by_zip(self, zipcode) and
zip_by_address(self, street_number, street_name, apt, city)


> I stored object1 in a marshalled dictionary. Its length is about 450.000 (I 
>live
> in Holland, not THAT many streets). Look-ups are incredibly fast (it has to,
> because it's part of an autocompletion feature of a data entry program). I
> haven't got the street number data needed for object2 yet, but it's going to 
be
> much larger. Many streets have different zip codes for odd or even numbers, or
> the zip codes are divided into street number ranges (for long streets).

Remember that you don't want to try to optimize too soon.

Build a  simple working system and see what happens. If it
is too slow or takes up too much memory, fix it.


> You suggest to simply use a file. I like simple solutions, but doesn't that, 
by
> definition, require a slow, linear search?

You could create an index, but then any database will already have
an indexing function built in.

I'm not saying that rolling your own custom database is a bad idea,
but if you are trying to get some work done (and not just playing around
and learning Python) then it's probably better to use something that is
already proven to work.


If you have some code you are trying out, but are not sure you
are going the right way, post it and let people ta

Re: [Tutor] (de)serialization questions

2010-10-04 Thread Albert-Jan Roskam
Hi Alan,

Wow, I'm impressed! You're right, it's near Glencoe! Coming from Edingburgh 
it's a couple of miles just before Glencoe. Marvellous place!

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?

~~

--- On Mon, 10/4/10, Alan Gauld  wrote:

From: Alan Gauld 
Subject: Re: [Tutor] (de)serialization questions
To: tutor@python.org
Date: Monday, October 4, 2010, 1:46 AM


"Albert-Jan Roskam"  wrote

> It makes much more sense to maintain one database table instead of 3 csv files
> for the three data typists' output.

To be pedantic you will probably want several tables but they will all
be in one file... :-)

> Btw, your private website is nice too. Nice pictures!

Well done, not many people notice that section :-)
I keep meaning to add another dozen or so pages,
but finding time

> Do you recognize where this was taken:
> http://yfrog.com/n0scotland046j .


Could be any of a dozen places but if pushed I'll guess the Rannoch Moor.
Maybe the top end of Glencoe? But the layer of low cloud wipes out too much
to be sure.

> You're lucky to live in a beautiful place like Scotland

I think so :-)

-- 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] updating a Csv file

2010-10-20 Thread Albert-Jan Roskam
Hi all,



How can I update a csv file? I've written the code below, but it does 
not work ("Error: line contains NULL byte"). I've never tried opening a 
file in read and write mode (r+) at the same time before, so I suspect 
that this is the culprit. Should I instead just open one file, write to 
another, throw away the original and rename the new file? That seems 
inefficient.



import csv, string



def updateLine(idVar, idValue, myCsv, newLine):

    f = open(myCsv, "r+")

    r = csv.reader(f)

    w = csv.writer(f)

    header = r.next()

    idPos = header.index(idVar)

    for row  in r:

    if row[idPos] == idValue:

    row = newLine

    w.writerow(row)

    f.close()



updateLine(idVar = "Id",

   idValue = "hawxgXvbfu",

   myCsv = "c:/temp/someCsv.csv",

   newLine = [ch for ch in string.letters[0:9]])

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] updating a Csv file

2010-10-20 Thread Albert-Jan Roskam
Hi Steven and Dave,

Thanks for replying to me. Steven, I pasted the traceback below, along with 
some version info. I'm using Windows XP. I used the very same code as the one I 
posted before, including the string.letters bit. Btw, thanks for the little 
'list' tip.

Dave, what you say makes sense. I tried updating the old values with new values 
of exactly the same length (10 characters), but that didn't work either (I 
simply used [ch * 10 for ch in string.letters[0:9]] --list trick not possible 
here ;-)). I pasted the contents of my test csv file below the traceback.

Anyway, thanks again for your time!

Python 2.6 (r26:66721, Oct  2 2008, 11:35:03) [MSC v.1500 32 bit (Intel)] on 
win32
Type "copyright", "credits" or "license()" for more information.

  
IDLE 2.6  
>>> 

Traceback (most recent call last):
  File "C:\Documents and Settings\UserXP\Bureaublad\test.py", line 18, in 

    newLine = [ch for ch in string.letters[0:9]])
  File "C:\Documents and Settings\UserXP\Bureaublad\test.py", line 10, in 
updateLine
    for row  in r:
Error: line contains NULL byte
>>> 

Id,NaamInstelling,Naam3,Straat,HuisNr,Postcode,Plaats,dummy
MQrrSzDboW,XWvxiqlrEp,ERQewcVYva,ppBXnpeCOs,HTmVvHRVhH,KHvjNHIYeM,bcEMrYPmuB,w
hawxgXvbfu,VqiCmTSwdD,GUcoNnXUyL,LJexEROxrN,aPIuRapjDS,YUNJHBmCsQ,mQWbajBxKm,ww
JSIXUYMxMt,CNebFXwmtZ,GTHQMyYUwT,XgRdYuFtfY,WyIeoiqqnC,SpbJWgDsHo,ZEuIXNujUd,www
hawxgXvbfu,VqiCmTSwdD,GUcoNnXUyL,LJexEROxrN,aPIuRapjDS,YUNJHBmCsQ,mQWbajBxKm,ww

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?

~~

--- On Wed, 10/20/10, Dave Angel  wrote:

From: Dave Angel 
Subject: Re: [Tutor] updating a Csv file
To: "Albert-Jan Roskam" 
Cc: "Python Mailing List" 
Date: Wednesday, October 20, 2010, 12:23 PM

  On 2:59 PM, Albert-Jan Roskam wrote:
> Hi all,
>
>
>
> How can I update a csv file? I've written the code below, but it does
> not work ("Error: line contains NULL byte"). I've never tried opening a
> file in read and write mode (r+) at the same time before, so I suspect
> that this is the culprit. Should I instead just open one file, write to
> another, throw away the original and rename the new file? That seems
> inefficient.
>
>
>
> import csv, string
>
> def updateLine(idVar, idValue, myCsv, newLine):
>      f = open(myCsv, "r+")
>      r = csv.reader(f)
>      w = csv.writer(f)
>      header = r.next()
>      idPos = header.index(idVar)
>      for row  in r:
>          if row[idPos] == idValue:
>              row = newLine
>              w.writerow(row)
>      f.close()
>
> updateLine(idVar = "Id",
>             idValue = "hawxgXvbfu",
>             myCsv = "c:/temp/someCsv.csv",
>             newLine = [ch for ch in string.letters[0:9]])
>
> Cheers!!
>
> Albert-Jan
>
In general, you don't want to update anything in place, unless the new 
items are guaranteed to be the same size as the old.  So when you're 
looping through a list, if you replace one item with another, no 
problem, but if you insert or delete, then you should be doing it on a copy.

In the file, the byte is your unit of measure.  So if what you're 
writing might be a different size (smaller or larger), then don't do it 
in place.  You may not be able to anyway, if the csv module can't handle 
it.  For example, the file object 'f' has a position, which is set by 
either read or write.  csv may assume that they can count on that 
position not changing from outside influences, in which case even 
same-size updates could fail.

DaveA






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


Re: [Tutor] List comprehension question

2010-11-09 Thread Albert-Jan Roskam
For larger blocks of code, cProfile may also be useful for speed tests.

 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: Steven D'Aprano 
To: tutor@python.org
Sent: Tue, November 9, 2010 9:54:24 PM
Subject: Re: [Tutor] List comprehension question

Richard D. Moores wrote:

> See  for a speed test with n =
> 100,000 and 100,000 loops

As a general rule, you shouldn't try to roll your own speed tests. There are 
various subtleties that can throw your results right out. Timing small code 
snippets on modern multi-tasking computers is fraught with difficulties.

Fortunately Python comes with that battery already included: the timeit module. 
You can use it from the shell like this:

python -m timeit -s "setup code goes here" "test code goes here"

At the Python interactive interpreter, the most useful pattern I've found is:

from timeit import Timer
t = Timer("proper_divisors(n)",
"from __main__ import proper_divisors; n = 10")
min(t.repeat(repeat=5, number=10))


Or if you're happy with Python's defaults, that last line can be just:

min(t.repeat())



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


[Tutor] smptlib question

2009-06-08 Thread Albert-jan Roskam

(sorry for posting this again, but something might have gone wrong)

Hi,

I made a very simple program to send everybody I know a change of address I am 
parsing the .msg files with another function, which returns a set called 
'recipients'.

The code below works, but it displays all recipients in the 'to' field. That 
is, in the Nth iteration, N recipients are shown. So the first mail has one 
recipient in the 'to' field, the second mail has the first and the second 
recipient, and so forth. I only want one recipient to be shown in the 'to' 
field. It's ugly and since I have a lot of email addresses to parse (4 years 
worth of emails!), it would become a very long list.

Pointers, anyone?

Thanks!
Albert-Jan
Python 2.5, Windows XP

import smtplib, email.utils
from email.mime.text import MIMEText

msg = MIMEText("""Dear reader:\n\nblah blaah blaah.\nSincerely,\nThe 
dude""")

sender = 'r...@stimpy.com'
recipients = ['happyha...@joy.com', 'y...@eediot.com]
for no, recipient in enumerate(recipients):
print "--> Sending message to: %s (%s of %s)" % (recipient, no+1, 
len(recipients))
msg['To'] = email.utils.formataddr((recipient, recipient))
print msg['To']
msg['From'] = email.utils.formataddr(('Cool dude', sender))
msg['Subject'] = 'Change of email address'

server = smtplib.SMTP("mysmtpserver.isp.com")
server.set_debuglevel(True)
try:
server.sendmail(sender, [recipient], msg.as_string())
finally:
server.quit()


  
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


[Tutor] smtplib question

2009-06-08 Thread Albert-jan Roskam

Hi,

I made a very simple program to send everybody I know a change of address I am 
parsing the .msg files with another function, which returns a set called 
'recipients'.

The code below works, but it displays all recipients in the 'to' field. That 
is, in the Nth iteration, N recipients are shown. So the first mail has one 
recipient in the 'to' field, the second mail has the first and the second 
recipient, and so forth. I only want one recipient to be shown in the 'to' 
field.

Pointers, anyone?

Thanks!
Albert-Jan

import smtplib, email.utils
from email.mime.text import MIMEText

msg = MIMEText("""Dear reader:\n\nblah blaah blaah.\nSincerely,\nThe 
dude""")

sender = 'r...@stimpy.com'
recipients = ['happyha...@joy.com', 'y...@eediot.com]
for no, recipient in enumerate(recipients):
print "--> Sending message to: %s (%s of %s)" % (recipient, no+1, 
len(recipients))
msg['To'] = email.utils.formataddr((recipient, recipient))
print msg['To']
msg['From'] = email.utils.formataddr(('Cool dude', sender))
msg['Subject'] = 'Change of email address'

server = smtplib.SMTP("mysmtpserver.isp.com")
server.set_debuglevel(True) 
try:
server.sendmail(sender, [recipient], msg.as_string())
finally:
server.quit()



  
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] smptlib question

2009-06-09 Thread Albert-jan Roskam

Hi David and Tino,

Thanks! I will try this tonight when I'm back home from the office. It's 
interesting to see how you split up the code into several little functions. 
Maybe it's typical for a newbie (like myself) to have the tendency to put 
everything into one function, instead of letting each function have its own 
purpose.

Cheers!!
Albert-Jan

--- On Tue, 6/9/09, David  wrote:

> From: David 
> Subject: Re: [Tutor] smptlib question
> To: "Albert-jan Roskam" 
> Cc: "*tutor python" 
> Date: Tuesday, June 9, 2009, 2:13 AM
> Albert-jan Roskam wrote:
> > (sorry for posting this again, but something might
> have gone wrong)
> > 
> > Hi,
> > 
> > I made a very simple program to send everybody I know
> a change of address I am parsing the .msg files with another
> function, which returns a set called 'recipients'.
> > 
> > The code below works, but it displays all recipients
> in the 'to' field. That is, in the Nth iteration, N
> recipients are shown. So the first mail has one recipient in
> the 'to' field, the second mail has the first and the second
> recipient, and so forth. I only want one recipient to be
> shown in the 'to' field. It's ugly and since I have a lot of
> email addresses to parse (4 years worth of emails!), it
> would become a very long list.
> > 
> > Pointers, anyone?
> > 
> > Thanks!
> > Albert-Jan
> > Python 2.5, Windows XP
> > 
> > import smtplib, email.utils
> > from email.mime.text import MIMEText
> > 
> > msg = MIMEText("""Dear reader:\n\nblah blaah
> blaah.\nSincerely,\nThe dude""")
> > 
> > sender = 'r...@stimpy.com'
> > recipients = ['happyha...@joy.com',
> 'y...@eediot.com]
> > for no, recipient in enumerate(recipients):
> >     print "--> Sending message
> to: %s (%s of %s)" % (recipient, no+1, len(recipients))
> >     msg['To'] =
> email.utils.formataddr((recipient, recipient))
> >     print msg['To']
> >     msg['From'] =
> email.utils.formataddr(('Cool dude', sender))
> >     msg['Subject'] = 'Change of
> email address'
> > 
> >     server =
> smtplib.SMTP("mysmtpserver.isp.com")
> >     server.set_debuglevel(True)
> >     try:
> >     
>    server.sendmail(sender, [recipient],
> msg.as_string())
> >     finally:
> >         server.quit()
> > 
> > 
> >   
>    ___
> > Tutor maillist  -  Tutor@python.org
> > http://mail.python.org/mailman/listinfo/tutor
> > 
> > 
> 
> Hi Albert,
> I got this to work;
> 
> #!/usr/bin/python
> 
> import smtplib, email.utils
> from email.mime.text import MIMEText
> from email.MIMEMultipart import MIMEMultipart
> from email.Utils import COMMASPACE, formatdate
> 
> def recipients():
>     recipients = ['happyha...@joy.com',
> 'y...@eediot.com]
>     for no, recipient in enumerate(recipients):
>         print "--> Sending message
> to: %s (%s of %s)" % (recipient, no+1, len(recipients))
>         message(recipient)
> 
> def message(recipient):
>     body = "Dear reader:\n\nblah blaah
> blaah.\nSincerely,\nThe dude"
>     send_mail([recipient], 'Change of email
> address', body)
> 
> 
> def send_mail(send_to, subject, text,
> server="mail.stimpy.com"):
>     send_from = "r...@stimpy.com"
>     msg = MIMEMultipart()
>     msg['To'] = COMMASPACE.join(send_to)
>     msg['Date'] = formatdate(localtime=True)
>     msg['From'] = send_from
>     msg['Subject'] = subject
>     msg.attach( MIMEText(text) )
>     smtp = smtplib.SMTP(server)
>     smtp.sendmail(send_from, send_to,
> msg.as_string())
>     smtp.close()
> 
> recipients()
> 
> -- Powered by Gentoo GNU/Linux
> http://linuxcrazy.com
> 


  
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Need help solving this problem

2009-06-10 Thread Albert-jan Roskam

hi!

This is how I would do it, but I'm still learning this too, so I'm very much 
open for suggestions.

Cheers!!
Albert-Jan

import random
def draw ():
return random.sample(["head", "tail"], 1)
def toss ():
heads, tails = 0, 0
for flip in range(100):
if draw() == ["head"]: heads += 1
else: tails += 1
return heads, tails
for attempt in range(20):
print "attempt:", attempt+1, "heads:", toss()[0], "tails:", toss()[1]


--- On Wed, 6/10/09, Raj Medhekar  wrote:

> From: Raj Medhekar 
> Subject: [Tutor] Need help solving this problem
> To: "Python Tutor" 
> Date: Wednesday, June 10, 2009, 10:08 PM
> I
> have been teaching myself Python using a book. The chapter I
> am on currently, covers branching, while loops and program
> planning. I am stuck on on of the challenges at the end of
> this chapter, and I was hoping to get some help with this.
> Here it is:
> 
> Write a program that flips a coin 100 times and the tells
> you the number of heads and tails.
> 
> I have tried to think about several ways to go about doing
> this but I have hit a wall. Even though I understand the
> general concept of branching and looping, I have been having
> trouble writing a program with these.  I look forward
> to your reply that will help me understand these structures
> better.
> 
> Sincerely,
> Raj
> 
> 
> 
>   
> -Inline Attachment Follows-
> 
> ___
> Tutor maillist  -  Tutor@python.org
> http://mail.python.org/mailman/listinfo/tutor
> 


  
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Help Needed

2009-06-16 Thread Albert-jan Roskam

Hi, 

This is how I would do it, although there might be more readable solutions:
s = raw_input("Enter a message: ")
print "".join([s[-letter] for letter in range(len(s)+1)])

Cheers!!
Albert-Jan

--- On Tue, 6/16/09, Alan Gauld  wrote:

> From: Alan Gauld 
> Subject: Re: [Tutor] Help Needed
> To: tutor@python.org
> Date: Tuesday, June 16, 2009, 1:57 AM
> 
> "Raj Medhekar" 
> wrote
> 
> > I am looking to build a program that gets a message
> from the user and then prints it backward. I 've been at it
> for hours now but I can't seem to figure it out. 
> 
> So tell us what you re thinking? How would you solve it
> manually?
> 
> > I've been having trouble trying to index the message
> so I can print it out backwards. 
> 
> You can index the characters of a string in Python exactly
> as you do a list.
> 
> > I would've posted my code but I really haven't gotten
> past the  'raw_input("Enter a message: ")' command
> line.  
> 
> Its always better to try something even if its wrong, it
> lets us see where your thinking is going adrift. Or at least
> describe what you'd like to do conceptually.
> 
> Alan G.
> 
> ___
> Tutor maillist  -  Tutor@python.org
> http://mail.python.org/mailman/listinfo/tutor
> 


  
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


[Tutor] xlwt & xlrd: can I refactor this code better?

2009-07-22 Thread Albert-Jan Roskam

Hi,

A while ago I wrote a program to merge xls files. Now I refactored it because 
before, it was one big chunk of spaghetti code and I wanted to add some 
functionality. The code below works, but I have the feeling that it could still 
be simplified. Most functions have many arguments - isn't that called 'tight 
coupling'? Could somebody give me a few general pointers as to how to improve 
this program without loosing functionality? The first function is the main() 
function. Shouldn't a programmer strive for information hiding in such a 
function? Ideally, it should almost read like regular english, right? Or is 
that too textbook-ish? ;-)

Thanks in advance!
Albert-Jan

"""
Merge all xls files in a given directory into one multisheet xls file.
The sheets get the orginal file name, without the extension.
File names should not exceed 29 characters
"""

import glob, os.path, time
import xlrd, xlwt

def merge_xls(in_dir="d:/temp/", out_file="d:/temp/merged_output.xls"):
""" Main function: merge xls sheets """
xls_files = glob.glob(in_dir + "*.xls")
xls_files.sort()
merged_book = xlwt.Workbook()
osheet_names = [os.path.basename(xls_file)[:-4] for xls_file in xls_files]
for xls_no, xls_file in enumerate(xls_files):
print "---> Processing file %s" % (xls_file)
book = xlrd.open_workbook(xls_file)
isheet_names = xlrd.Book.sheet_names(book)
check_xls(book, merged_book, isheet_names, osheet_names, xls_no, 
xls_files)
stamped_outfile = out_file[:-4] + "_" + time.strftime("%Y-%m-%d") + ".xls"
merged_book.save(stamped_outfile)
print_msg(xls_files, osheet_names, stamped_outfile)

def check_xls(book, merged_book, isheet_names, osheet_names, xls_no, xls_files):
""" Check existence and file names of input xls files """
if xls_files and len(osheet_names[xls_no]) <= 29:
write_sheets(book, merged_book, isheet_names, osheet_names, xls_no, 
xls_files)
elif xls_files:
print "WARNING *** File name too long: <%s.xls> (maximum is 31 chars) " 
% (osheet_names[xls_no])
print "WARNING *** File <%s.xls> was skipped." % (osheet_names[xls_no])
else:
print "NOTE *** No xls files in %s. Nothing to do" % (in_dir)

def write_sheets(book, merged_book, isheet_names, osheet_names, xls_no, 
xls_files):
""" Write sheets, and add sheet numbering in case of multisheet xls input 
"""
osheet_name = osheet_names[xls_no]
xls_file = xls_files[xls_no]
if book.nsheets == 1:
ws = merged_book.add_sheet(osheet_name)
isheet_name = isheet_names[0]
sheet = book.sheet_by_index(0)
write_cells(sheet, book, ws, isheet_name, osheet_name, xls_file)
elif book.nsheets in range(1, 100):
for sheetx in range(book.nsheets):
isheet_name = isheet_names[sheetx]
ws = merged_book.add_sheet(osheet_name+str(sheetx+1).zfill(2))
sheet = book.sheet_by_index(sheetx)
write_cells(sheet, book, ws, isheet_name, osheet_name, xls_file)

else:
raise Exception ("ERROR *** File %s has %s sheets (maximum is 99)" % 
(xls_file, book.nsheets))

def write_cells(sheet, book, ws, isheet_name, osheet_name, xls_file, 
format_cell=True):
""" Write cells, and apply formatting if needed """
MISSINGVALUES = ("#LEEG!", "#NULL!")
rx = 0 # initialize to zero in case of empty input xls file.
style = format_cells(ws)
for rx in range(sheet.nrows):
for cx in range(sheet.ncols):
cell_value = sheet.cell_value(rx, cx)
if format_cell and rx == 0:
format_cells(ws)
ws.write(rx, cx, cell_value, style)
elif cell_value in MISSINGVALUES or xlrd.XL_CELL_EMPTY: 
ws.write(rx, cx, "   ")
else:
ws.write(rx, cx, cell_value)
footer = "source tab: " + isheet_name + " || source file: " + 
os.path.basename(xls_file)
if format_cell:
ws.write(rx+2, 0, footer.upper(), style)  # print bold source tab & 
file name below the table
else:
ws.write(rx+2, 0, footer.upper())

def format_cells(ws, font='Arial', boldrow=True, panes_frozen=True):
""" Add horizontal split pane and bold font at first row """
ws.panes_frozen = panes_frozen
ws.horz_split_pos = 1
font0 = xlwt.Font()
font0.name = font
font0.struck_out = False
font0.bold = boldrow
style0 = xlwt.XFStyle()
style0.font = font0
return style0

def print_msg(xls_files, osheet_names, stamped_outfile):
""" Print status messages """
print "\n---> Merged xls file written to %s using the following source 
files: " % (stamped_outfile)
MAXSHEETNAMELEN = 29
for n_sheet, osheet_name in enumerate(osheet_names):
if len(osheet_name) <= MAXSHEETNAMELEN:
print "\t", str(n_sheet+1).zfill(3), "%s.xls" % (osheet_name)
excl_sheets = [os.path.basename(xls_file)[:-4] for xls_file in xls_files if 
\
   len

Re: [Tutor] how to get blank value

2009-07-24 Thread Albert-Jan Roskam

Mark Summerfield recently wrote a book called Programming in Python 3 
(http://www.qtrac.eu/py3book.html) The chapter on regexes happens to be freely 
downloadable as a sample chapter:
http://ptgmedia.pearsoncmg.com/images/9780137129294/samplepages/0137129297_Sample.pdf

I found that chapter (in fact, the entire book!) very helpful. It's on Python 
3, but that doesn't matter too much, esp. not in the regex chapter.

Cheers!!
Albert-Jan


  
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] simple text replace

2009-07-27 Thread Albert-Jan Roskam

Hi!

Did you consider using a regex?

import re
re.sub("python\s", "snake ", "python is cool, pythonprogramming...")

Cheers!!
Albert-Jan


--- On Mon, 7/27/09, Dave Angel  wrote:

> From: Dave Angel 
> Subject: Re: [Tutor] simple text replace
> To: "j booth" 
> Cc: tutor@python.org
> Date: Monday, July 27, 2009, 12:41 AM
> j booth wrote:
> > Hello,
> > 
> > I am scanning a text file and replacing words with
> alternatives. My
> > difficulty is that all occurrences are replaced (even
> if they are part of
> > another word!)..
> > 
> > This is an example of what I have been using:
> > 
> >     for line in
> fileinput.FileInput("test_file.txt",inplace=1):
> >   
> >>         line =
> line.replace(original, new)
> >>         print line,
> >>     
>    fileinput.close()
> >>     
> > 
> > 
> > original and new are variables that have string values
> from functions..
> > original finds each word in a text file and old is a
> manipulated
> > replacement. Essentially, I would like to replace only
> the occurrence that
> > is currently selected-- not the rest. for example:
> > 
> > python is great, but my python knowledge is limited!
> regardless, I enjoy
> >   
> >> pythonprogramming
> >>     
> > 
> > 
> > returns something like:
> > 
> > snake is great, but my snake knowledge is limited!
> regardless, I enjoy
> >   
> >> snakeprogramming
> >>     
> > 
> > 
> > thanks so much!
> > 
> >   
> Not sure what you mean by "currently selected," you're
> processing a line at a time, and there are multiple
> legitimate occurrences of the word in the line.
> 
> The trick is to define what you mean by "word." 
> replace() has no such notion.  So we want to write a
> function such as:
> 
> given three strings, line, inword, and outword.  Find
> all occurrences of inword in the line, and replace all of
> them with outword.  The definition of word is a group
> of alphabetic characters (a-z perhaps) that is surrounded by
> non-alphabetic characters.
> 
> The approach that I'd use is to prepare a translated copy
> of the line as follows:   Replace each
> non-alphabetic character with a space.  Also insert a
> space at the beginning and one at the end.  Now, take
> the inword, and similarly add spaces at begin and end. 
> Now search this modified line for all occurrences of this
> modified inword, and make a list of the indices where it is
> found.  In your example line, there would be 2 items in
> the list.
> 
> Now, using the original line, use that list of indices to
> substitute the outword in the appropriate places.  Use
> slices to do it, preferably from right to left, so the
> indices will work even though the string is changing. 
> (The easiest way to do right to left is to reverse() the
> list.
> 
> DaveA
> 
> ___
> Tutor maillist  -  Tutor@python.org
> http://mail.python.org/mailman/listinfo/tutor
> 


  
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


[Tutor] easy way to pupulate a dict with functions

2009-08-06 Thread Albert-Jan Roskam
Hi,

I was playing with the code below, and I was wondering if there was a way to 
populate the dictionary called 'commands' (in the function 'check_command()').

Suppose I would add another function, which I would also like to store as a 
value in 'commands', could it simply be programmed, or would every 
update/addition require the definition of the dictionary to be extended?
It would be most desirable if one could simply add another function without 
there being the need to touch any of the other code. 

Cheers!!
Albert-Jan()

def foo (a):
return "foo" * a

def bletch (q):
for i in range(20):
print i * q

def stilton (n):
print "yes sir, " * n

def romans (z):
print "what have the romans really done for us?\n" * z

def check_command():
commands = {'1': foo, '2': bletch, '3': stilton, '4': romans}
while True:
select = raw_input("choose an option [%s]: " % 
"|".join(sorted(commands.keys(
if select in commands.keys():
check_parameter(commands, select)
break
else:
print "illegal option (%s)" % select

def check_parameter(commands, select):
while True:
parameter = raw_input("choose a parameter [integer]: ")
if parameter.isdigit():
commands[select](int(parameter))
break
else:
print "illegal parameter (%s)" % parameter

check_command()



  
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] easy way to populate a dict with functions

2009-08-06 Thread Albert-Jan Roskam
Hi Bob,

Very neat solution, thanks a lot! I didn't know the inspect module, but it's 
just what's needed here. Cool! And sorry about not starting a new mail. I'll 
keep it in mind for next time.

Best wishes,
Albert-Jan 

--- On Thu, 8/6/09, bob gailer  wrote:

> From: bob gailer 
> Subject: RE:  [Tutor] easy way to populate a dict with functions
> To: fo...@yahoo.com
> Cc: "tutorpythonmailinglist Python" 
> Date: Thursday, August 6, 2009, 7:28 PM
> Please start a new email when
> starting a new topic. Otherwise it gets linked to the
> previous one in email clients that follow threads!
> 
> To avoid that here I am responding in a new email. Also
> fixed spelling in subject.
> 
> Albert-Jan Roskam wrote:
> > Hi,
> > 
> > I was playing with the code below, and I was wondering
> if there was a way to populate the dictionary called
> 'commands' (in the function 'check_command()').
> > 
> > Suppose I would add another function, which I would
> also like to store as a value in 'commands', could it simply
> be programmed, or would every update/addition require the
> definition of the dictionary to be extended?
> > It would be most desirable if one could simply add
> another function without there being the need to touch any
> of the other code. 
> > 
> >   
> Here's how I'd do it. Others may have other solutions.
> 
> Put the command functions in a separate module, e.g.,
> commands.py:
> #  code ---
> def foo (a):
>  return "foo" * a
> 
> def bletch (q):
>  for i in range(20):
>    print i * q
> 
> def stilton (n):
>  print "yes sir, " * n
> 
> def romans (z):
>  print "what have the romans really done for us?\n" * z
> #  end code ---
> 
> Put the "main" program (in another module) e.g., main.py:
> 
> #  code ---
> import commands
> import inspect
> cmds = inspect.getmembers(commands, inspect.isfunction)
> num_cmds = len(cmds)
> option_list = "|".join(str(c) for c in range(1,
> num_cmds+1))
> prompt = "choose an option [%s]: " % option_list
> 
> def check_command():
>  while True:
>    select = raw_input(prompt)
>    try:
>      command = cmds[int(select)][1]
>    except ValueError:
>      print "non-numeric option 
>    (%s)" % select
>    except IndexError:
>      print "option out of range (%s)" %
> select
>    else:
>      check_parameter(command)
>      break
> 
> def check_parameter(command):
>  while True:
>    parameter = raw_input("choose a parameter
> [integer]: ")
>    if parameter.isdigit():
>      command(int(parameter))
>      break
>    else:
>      print "illegal parameter (%s)" %
> parameter
> 
> check_command()
> #  end code ---
> 
> cmds is a list similar to:
>  [('bletch', ),
>  ('foo', ),
>  ('romans', ),
>  ('stilton', )]
> 
> -- Bob Gailer
> Chapel Hill NC
> 919-636-4239
> 
> 


  
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] easy way to populate a dict with functions

2009-08-06 Thread Albert-Jan Roskam
...Still one more question:
cmds = inspect.getmembers(commands, inspect.isfunction)
print cmds
#will generate
[('bletch', ), ('foo', ), ('romans', ), ('stilton', 
)]

This looks okay, but the order in which the functions appear in the module 
(commands.py) is different:
foo, bletch, stilton, romans

Do you know why?

Cheers again,
Albert-Jan

--- On Thu, 8/6/09, bob gailer  wrote:

> From: bob gailer 
> Subject: RE:  [Tutor] easy way to populate a dict with functions
> To: fo...@yahoo.com
> Cc: "tutorpythonmailinglist Python" 
> Date: Thursday, August 6, 2009, 7:28 PM
> Please start a new email when
> starting a new topic. Otherwise it gets linked to the
> previous one in email clients that follow threads!
> 
> To avoid that here I am responding in a new email. Also
> fixed spelling in subject.
> 
> Albert-Jan Roskam wrote:
> > Hi,
> > 
> > I was playing with the code below, and I was wondering
> if there was a way to populate the dictionary called
> 'commands' (in the function 'check_command()').
> > 
> > Suppose I would add another function, which I would
> also like to store as a value in 'commands', could it simply
> be programmed, or would every update/addition require the
> definition of the dictionary to be extended?
> > It would be most desirable if one could simply add
> another function without there being the need to touch any
> of the other code. 
> > 
> >   
> Here's how I'd do it. Others may have other solutions.
> 
> Put the command functions in a separate module, e.g.,
> commands.py:
> #  code ---
> def foo (a):
>  return "foo" * a
> 
> def bletch (q):
>  for i in range(20):
>    print i * q
> 
> def stilton (n):
>  print "yes sir, " * n
> 
> def romans (z):
>  print "what have the romans really done for us?\n" * z
> #  end code ---
> 
> Put the "main" program (in another module) e.g., main.py:
> 
> #  code ---
> import commands
> import inspect
> cmds = inspect.getmembers(commands, inspect.isfunction)
> num_cmds = len(cmds)
> option_list = "|".join(str(c) for c in range(1,
> num_cmds+1))
> prompt = "choose an option [%s]: " % option_list
> 
> def check_command():
>  while True:
>    select = raw_input(prompt)
>    try:
>      command = cmds[int(select)][1]
>    except ValueError:
>      print "non-numeric option 
>    (%s)" % select
>    except IndexError:
>      print "option out of range (%s)" %
> select
>    else:
>      check_parameter(command)
>      break
> 
> def check_parameter(command):
>  while True:
>    parameter = raw_input("choose a parameter
> [integer]: ")
>    if parameter.isdigit():
>      command(int(parameter))
>      break
>    else:
>      print "illegal parameter (%s)" %
> parameter
> 
> check_command()
> #  end code ---
> 
> cmds is a list similar to:
>  [('bletch', ),
>  ('foo', ),
>  ('romans', ),
>  ('stilton', )]
> 
> -- Bob Gailer
> Chapel Hill NC
> 919-636-4239
> 
> 


  
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] easy way to populate a dict with functions

2009-08-11 Thread Albert-Jan Roskam
Hi Bob,

Sorry for the late reply, but thank you for your code. I still haven't 
completely wrappped my brain around decorator functions, but thanks to your 
reply at least now my frontal lobe is touching it. ;-)

Re: the ordering of functions: I thought it mattered because the inspect module 
returns a list of two-tuples (in this case, a list of four two-tuples). 
Therefore, e.g. option #1 should always cause the same function to be run. But 
apparently I'm wrong here somewhere.

Best wishes,
Albert-Jan
Netherlands 

--- On Thu, 8/6/09, bob gailer  wrote:

> From: bob gailer 
> Subject: Re: [Tutor] easy way to populate a dict with functions
> To: "Albert-Jan Roskam" 
> Cc: "tutorpythonmailinglist Python" 
> Date: Thursday, August 6, 2009, 10:20 PM
> Albert-Jan Roskam wrote:
> > Hi Bob,
> > 
> > Very neat solution, thanks a lot! I didn't know the
> inspect module, but it's just what's needed here. Cool! 
> 
> Great.
> 
> Regarding the ordering of functions in the list - variable
> names in namespaces (such as modules) are stored in
> dictionaries. Any ordering is lost in this process. Why is
> order important to you here?
> 
> There are other things that could be said about your
> program and my revision.
> 
> 1 - it is better to have functions return values and have
> the main program decide whether to print them or take some
> other action. (Also note you were not consistent - foo
> returns the others print!)
> 
> 2 - since the list contains the function names as well as
> references to the functions you could present the names to
> the user. You could even ask the user to enter the first
> letter(s) of the names instead of a number.
> 
> 3 - with a slight enhancement to things you can preserve
> the order:
> 
> #  commands.py ---
> cmds = [] 
> def collect(func):
>  'a "decorator" that adds each function to the cmds list'
>  cmds.append((func.__name__, func))
> 
> @collect
> def foo (a):
> return "foo" * a
> 
> @collect
> def bletch (q):
> for i in range(20):
>   return i * q
> 
> @collect
> def stilton (n):
> return "yes sir, " * n
> 
> @collect
> def romans (z):
> return "what have the romans really done for us?\n" * z
> 
> #  end code ---
> 
> #  main.py ---
> import commands
> cmds = commands.cmds
> 
> # etc.
> 
> -- Bob Gailer
> Chapel Hill NC
> 919-636-4239
> 


  
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


[Tutor] Input validation

2009-09-03 Thread Albert-Jan Roskam
Hi,

I'm wondering what is the most common method of input validation. See the 
example below. 
-Is the code below the most common/recognizable way something like this is done?
-Which of the options #1 and #2 is the preferred method? Option #2 looks less 
esoteric, but #1 seems better when you have to check for several data types 
simultaneously.

Thanks!
Albert-Jan

import os.path, textwrap

def main(param):
if validate_input(param) == "LT":
return param[-1]
elif validate_input(param) == "FILE":
f = open(param, "rb").readlines()
return " ".join(f)
elif validate_input(param) == "STR":
return textwrap.wrap(param)
else:
print "Invalid input!"
return None

def validate_input (param):
if param.__class__ in (list, tuple): #option 1
return "LT"
elif isinstance(param, str):# option 2
if os.path.isfile(param):
return "FILE"
else:
return "STR"
else:
return None

print main(param=[1,2,3])
print main(param="d:/temp/txt.txt")
print main(param="yeah but yeah, but no")
print main(param=1.0)




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


[Tutor] (no subject)

2009-10-13 Thread Albert-Jan Roskam
Hi,

I'm using Tkinter to program my very frist GUI. Each button grays out after it 
has been used so the user knows what next steps to take.
Now I want to define a Reset button to 'ungray' all buttons (state='normal'). 
How can I programmatically create a list of all available buttons?

Cheers!!
Albert-Jan

~~
Before you criticize someone, walk a mile in their shoes, that way 
when you do criticize them, you're a mile away and you have their shoes! 
~~


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


Re: [Tutor] GUI Buttons

2009-10-14 Thread Albert-Jan Roskam
Hi Alan and Kent,

Thank you for helping me. This is a lot simpler than the solution I had in 
mind. The button names in my program always start with "button_", so I wanted 
to use locals() in a list comprehension to compile a list of the buttons. But 
then I'd still have the names and not the instances. 

But when is the configure() method more appropriate to change e.g. the button 
status?

Cheers!!
Albert-Jan

~~
Before you criticize someone, walk a mile in their shoes, that way 
when you do criticize them, you're a mile away and you have their shoes! 
~~


--- On Tue, 10/13/09, Alan Gauld  wrote:

> From: Alan Gauld 
> Subject: [Tutor] GUI Buttons
> To: tutor@python.org
> Date: Tuesday, October 13, 2009, 10:06 AM
> Please provide a subject when sending
> mail to the list.
> And please create a new message so it doesn't get lost in
> an old thread...
> 
> "Albert-Jan Roskam" 
> wrote in message
> 
> > I'm using Tkinter to program my very frist GUI.
> > Each button grays out after it has been used so the
> user knows what next steps to take.
> > Now I want to define a Reset button to 'ungray' all
> buttons (state='normal').
> > How can I programmatically create a list of all
> available buttons?
> 
> Just add your buttons to a list when you create them
> Then in your reset method do
> 
> for button in ButtonList:
>    button['state'] = NORMAL
> 
> 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] "if clause" in list comprehensions.

2009-10-21 Thread Albert-Jan Roskam
Is the if-else (esp. 'else') in a list comprehension specific for Python 3.x? 
Or did I miss something?

Cheers!!
Albert-Jan

~~
Before you criticize someone, walk a mile in their shoes, that way 
when you do criticize them, you're a mile away and you have their shoes! 
~~


--- On Tue, 10/20/09, Alan Gauld  wrote:

> From: Alan Gauld 
> Subject: Re: [Tutor] "if clause" in list comprehensions.
> To: tutor@python.org
> Date: Tuesday, October 20, 2009, 12:29 AM
> Ooops, hit send by mistake...
> 
> "vince spicer" 
> wrote
> 
> >> Lambda can save the day to keep everything on one
> line, and leave variable
> >> type the same:
> >> 
> >> mylist = ['John', 'Canada', 25, 32, 'right']
> >> new_list = [(lambda y: y.upper() if hasattr(y,
> 'upper') else y)(a) for a in
> >> mylist ]
> >> 
> >> >>  ['JACK', 'CANADA', 25, 32,
> 'RIGHT']
> 
> In what way does lambda help?
> 
> new_list = [y.upper() if hasattr(y, 'upper') else y for y
> in mylist]
> 
> does exactly the same and is shorter.
> 
> lambda helps if you want to pass a function object around
> but
> defining it and immediately calling it means it can be
> replaced
> directly by the expression within the lambda.
> 
> But using hasattr() still has the problem of failing if
> upper is not a
> callable attribute (or does something radical like
> exiting)
> 
> 
> -- 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] ifdef in python

2009-10-24 Thread Albert-Jan Roskam
Isn't it better to use
if __debug__:

I thought such an if statement always evaluated to False when the python 
program was run in OPTIMIZED (-o) mode?

Cheers!!
Albert-Jan

~~
Before you criticize someone, walk a mile in their shoes, that way 
when you do criticize them, you're a mile away and you have their shoes! 
~~


--- On Wed, 10/21/09, Wayne  wrote:

> From: Wayne 
> Subject: Re: [Tutor] ifdef in python
> To: "Lie Ryan" 
> Cc: tutor@python.org
> Date: Wednesday, October 21, 2009, 11:37 PM
> 
> 
> On Wed, Oct 21, 2009 at 3:29 PM,
> Lie Ryan 
> wrote:
> 
> 
> Lizhi Yang wrote:
> 
> 
> Is there anything similar to ifdef statement in C or C++ in
> python?
> 
> 
> 
> 
> Not really, but python's regular if can be used for a
> similar purpose:
> 
> 
> 
> DEBUG = True
> 
> 
> 
> if DEBUG:
> 
>     import logging
> 
>     def foo():
> 
>         # debug version
> 
> else:
> 
>     def foo():
> 
>         # normal version
> 
> 
> 
> def main():
> 
>     foo()
> 
> 
> 
> if __name__ == '__main__':
> 
>     main()
> 
> 
> 
> or something like that.
> Really a try/except block would actually
> work
> def spam1():    pass
> try:   spam2except
> NameError:
> 
>    def spam2():      
> pass
> Because if DEBUG isn't declared you'll
> throw a NameError
> 
> -Inline Attachment Follows-
> 
> ___
> 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 load a dict into a dict subclass?

2009-10-27 Thread Albert-Jan Roskam
I thought reimplementing dict was a matter of defining a __dict__ attribute in 
the Bar class? And isn't the normal dict inherited anyway? (through __super__)

Or, if one would want to disable inheritance of the normal dict, one could use 
__slots__. Right?

Or did I misinterpret the New Testament of my Python Bible?

Cheers!!
Albert-Jan

~~
Before you criticize someone, walk a mile in their shoes, that way 
when you do criticize them, you're a mile away and you have their shoes! 
~~


--- On Tue, 10/27/09, Alan Gauld  wrote:

> From: Alan Gauld 
> Subject: Re: [Tutor] How to load a dict into a dict subclass?
> To: tutor@python.org
> Date: Tuesday, October 27, 2009, 6:45 PM
> 
> "John" 
> wrote
> 
> >> > >>> class Bar(dict):
> >> > >>>     pass
> >> > >>>
> >> > >>> foo = {'a':100, 'b':200, 'c':
> 300}
> >> > >>> myvar = Bar()
> >> > >>> myvar.update(foo)
> >> > >>> myvar
> >> >
> >> > {'a': 100, 'c': 300, 'b': 200}
> > 
> > Hey guru's could one of you explain why such a
> subclass is needed.  How would
> > it be used.  I'm not sure but does not the
> deepcopy() work as above?
> 
> This particular subclass does nothing different to the
> normal dict so its
> never needed. But it's only a demo of how to use the
> standard dict
> methods in a subclass.
> 
> In general you might subclass a dict if you wanted to
> change the
> behaviour of some specific method, or maybe add some new
> methods.
> Or maybe you wanted a dictionary that could take a list as
> a key...
> or even take a list of keys and return a list of values.
> 
> Lots of reasons for wanting to modify the standard dict
> behaviour.
> 
> -- 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] Apologies for sent spam

2009-11-16 Thread Albert-Jan Roskam
Hi,
 
I just noticed that a spam message was sent with my email address. Apologies.
I have no idea what caused that. I'll check my windows machine for viruses. Or 
could it have some other cause? Perhaps I should also change my password.

Cheers!!
Albert-Jan

~~
Before you criticize someone, walk a mile in their shoes, that way 
when you do criticize them, you're a mile away and you have their shoes! 
~~


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


Re: [Tutor] Writing code while tired, counterproductive?

2009-11-16 Thread Albert-Jan Roskam
I find that switching to a completely different approach or strategy becomes 
more difficult when tired. Depending on the context, that could be called 
persistence of perseverence (begin good or bad, respectively). However, in my 
opinion, not being able to view things from a different angle is usually 
counterproductive.

Cheers!!
Albert-Jan

~~
In the face of ambiguity, refuse the temptation to guess.
~~

--- On Mon, 11/16/09, Luke Paireepinart  wrote:


From: Luke Paireepinart 
Subject: Re: [Tutor] Writing code while tired, counterproductive?
To: "OkaMthembo" 
Cc: tutor@python.org
Date: Monday, November 16, 2009, 9:56 AM


I hate to be the odd one out here, but I actually find that I am extremely 
productive when I'm tired.  It's easier for me to commit completely to the 
code, when I'm well-rested I dream about running through fields of sunflowers 
and such, get distracted more easily.  The code I write when I'm tired is 
usually of marginally worse quality, but it's usually easy to audit it when I'm 
rested and fix any problems.  Although if I'm trying to solve something 
particularly difficult, especially something I've never done before, sometimes 
I won't be able to do it until I wake up a bit.  Note that this is for being 
tired, not exhausted.  If I'm exhausted I write a whole lot of awful code that 
I have to completely rewrite when I wake up.


On Mon, Nov 16, 2009 at 12:57 AM, OkaMthembo  wrote:

>From first-hand experience, i would concur :)

A refreshed mind will perform much better than an over-exerted one.





On Sat, Nov 14, 2009 at 8:57 PM, Alan Gauld  wrote:


"Modulok"  wrote


Does anyone else find, writing code while tired to be counterproductive?

Yes. Doing anything that is mentally taxing is usually a bad idea when tired!

Alan G

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



-- 
Regards,
Lloyd

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



-Inline Attachment Follows-


___
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] elementtree question

2009-11-19 Thread Albert-Jan Roskam
Hi,
 
I have an elementtree question that probably reflects my inexperience with xml 
processing (and/or with Python). The xml file is a stream of the Spss 
Clementine program. Each stream consists of, among others, nodes. Each nodes 
has properties, among which "tooltiptext" and "label". I want to replace the 
contents of "label" to "tooltiptext".
 
Below is what I've cooked up till now. Could anyone give me some pointers? 
Thanks a lot in advance!
 
from elementtree import ElementTree as ET
"""
Replace the empty text of the tooltipText tag with the text of the label tag
Relevant part of the tree: stream/diagram/nodes/node/properties
Within properties, the tooltiptext tag is listed before the label tag.
"""
in_tree = ET.ElementTree(file="d:/jib/test.xml")
parent_map = dict((c, p) for p in in_tree.getiterator() for c in p)
def xml_read(parent_map):
    for c, p in parent_map.iteritems():
    if p.tag == "properties" and c.tag == "label":
    yield c.text
##newnames = xml_read(parent_map)
##for newname in newnames:
##    print newname
 
def xml_write(parent_map, in_tree):
    newnames = xml_read(parent_map)
    for c, p in parent_map.iteritems():
    if p.tag == "properties" and c.tag == "toolTipText":
    for newname in newnames:
    print newname
    c.text = newname
    in_tree.write("d:/jib/out_xml.xml")
xml_write(parent_map, in_tree)


Cheers!!
Albert-Jan

~~
In the face of ambiguity, refuse the temptation to guess.
~~


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


Re: [Tutor] Breaking out of loop...

2009-11-20 Thread Albert-Jan Roskam
Hi!

Slightly different (extented) than your original question, but here's how I'd 
do this (although the program doesn't really do very much): 

import time, random

def is_valid_date():
    while True:
    prompt = 'Enter the date in ISO format (-MM-DD), or 0 to exit: '
    date = raw_input(prompt)
    try:
    time.strptime(date, "%Y-%m-%d")
    print "You've entered: %s" % date
    return True
    except ValueError:
    if date == str(0):
    print "Finished!"
    break
    else:
    print "Invalid date or invalid date format (%s)" % date
    return False

def do_something():
    for i in range(10):
    print "%05d" % random.randint(0, 10**4)

if is_valid_date():
    do_something()

Cheers!!

Albert-Jan



~~

In the face of ambiguity, refuse the temptation to guess.

~~

--- On Fri, 11/20/09, Ken G.  wrote:

From: Ken G. 
Subject: [Tutor] Breaking out of loop...
To: tutor@python.org
Date: Friday, November 20, 2009, 6:02 PM

I am trying to break out of a loop posted below.  When asked for monthdate, it 
should break out if I entered the number zero and it does not.  GRRR.  Been 
working on this for almost an hour.

   monthdate = 999

   while monthdate <> 0:
       monthdate = raw_input('Enter the month and date in two digit format 
each: ')                  month = monthdate[0:2]
       date = monthdate[2:4]
       year = ('2009')
       print month, date, year

   print "Finished!"

Thanking you all in advance for your help.

Ken

  ___
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] UnicodeEncodeError

2009-11-25 Thread Albert-Jan Roskam
Hi,
 
I'm parsing an xml file using elementtree, but it seems to get stuck on certain 
non-ascii characters (for example: "ê"). I'm using Python 2.4. Here's the 
relevant code fragment:
 
# CODE:
for element in doc.getiterator():
  try:
m = re.match(search_text, str(element.text))
  except UnicodeEncodeError:
    raise # I want to get rid of this exception.

# PRINTBACK:
m = re.match(search_text, str(element.text))
UnicodeEncodeError: 'ascii' codec can't encode character u'\xea' in position 4: 
ordinal not in range(128)
 
How can I get rid of this unicode encode error. I tried:
s = str(element.text)
s.encode("utf-8")
(and then feeding it into the regex)
 
The xml file is in UTF-8. Somehow I need to tell the program not to use ascii 
but utf-8, right?
 
Thanks in advance!

Cheers!!
Albert-Jan

~~
In the face of ambiguity, refuse the temptation to guess.
~~


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


Re: [Tutor] UnicodeEncodeError

2009-11-26 Thread Albert-Jan Roskam
OK, thanks a lot Spir and Kent for your replies. I converted element.text to 
str because some of the element.text were integers and these caused TypeErrors 
later on in the program. I don't have the program here (it's in the office) so 
I can't tell you the exact details. It's a search-and-replace program where 
users can enter a search text (or regex pattern) and a replace text. The source 
file is an xml file. Currently, strings with non-ascii letters still need to be 
inputted in unicode format, eg. u'enqu\xeate' instead of "enquête". Kinda ugly. 
I'll try to fix that later. Thanks again!

Cheers!!

Albert-Jan



~~

In the face of ambiguity, refuse the temptation to guess.

~~

--- On Wed, 11/25/09, Kent Johnson  wrote:

From: Kent Johnson 
Subject: Re: [Tutor] UnicodeEncodeError
To: "Albert-Jan Roskam" 
Cc: "tutor@python.org tutor@python.org tutor@python.org" 
Date: Wednesday, November 25, 2009, 5:55 PM

On Wed, Nov 25, 2009 at 8:44 AM, Albert-Jan Roskam  wrote:


Hi,
 
I'm parsing an xml file using elementtree, but it seems to get stuck on certain 
non-ascii characters (for example: "ê"). I'm using Python 2.4. Here's the 
relevant code fragment:
 
# CODE:
for element in doc.getiterator():
  try:
m = re.match(search_text, str(element.text))
  except UnicodeEncodeError:
    raise # I want to get rid of this exception.

# PRINTBACK:
m = re.match(search_text, str(element.text))
UnicodeEncodeError: 'ascii' codec can't encode character u'\xea' in position 4: 
ordinal not in range(128)

You can't convert element.text to a str because it contains non-ascii 
characters. Why are you converting it? re.match() will accept a unicode string 
as its argument.



 
How can I get rid of this unicode encode error. I tried:
s = str(element.text)
s.encode("utf-8")
(and then feeding it into the regex)
This fails because it is the str() that won't work. To get UTF-8 use
  s = element.text.encode('utf-8')
 but I don't think this is the correct solution.

 

The xml file is in UTF-8. Somehow I need to tell the program not to use ascii 
but utf-8, right?

No, just pass Unicode to re.match().

Kent 






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


[Tutor] unicode mapping doesn't work

2009-11-26 Thread Albert-Jan Roskam
Hi,

I want to substitute some letters with accents with theire non-accented 
equivalents. It should be easy, but it doesn't work. What am I doing wrong?

trans = {}
funnychars = u"éèêëóòôöáàâäÉÈÊËÓÒÔÖÁÀÂÄ"
asciichars = ""
for f, a in zip(funnychars, asciichars):
    trans.update({f: a})
print funnychars.translate(trans) # why doesn't this return the letters without 
accents?

Cheers!!

Albert-Jan



~~

In the face of ambiguity, refuse the temptation to guess.

~~


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


Re: [Tutor] unicode mapping doesn't work

2009-11-26 Thread Albert-Jan Roskam
Thank you! Quite frankly, I didn't know about ordinals *blushes*.

Also, I thought that d.update({k: v}) was equivalent to d[k] = v, but 
apparently it's not. So thank you twice!

Cheers!!

Albert-Jan



~~

In the face of ambiguity, refuse the temptation to guess.

~~

--- On Thu, 11/26/09, Lie Ryan  wrote:

From: Lie Ryan 
Subject: Re: [Tutor] unicode mapping doesn't work
To: tutor@python.org
Date: Thursday, November 26, 2009, 5:33 PM

Albert-Jan Roskam wrote:
> Hi,
> 
> I want to substitute some letters with accents with theire non-accented 
> equivalents. It should be easy, but it doesn't work. What am I doing wrong?
> 
> trans = {}
> funnychars = u"éèêëóòôöáàâäÉÈÊËÓÒÔÖÁÀÂÄ"
> asciichars = ""
> for f, a in zip(funnychars, asciichars):
>     trans.update({f: a})
> print funnychars.translate(trans) # why doesn't this return the letters 
> without accents?
> 

Perhaps the doc should be more obvious and perhaps unicode.translate is badly 
designed, but:

translate(...)
    S.translate(table) -> unicode

    Return a copy of the string S, where all characters have been mapped
    through the given translation table, which must be a mapping of
    Unicode ordinals to Unicode ordinals, Unicode strings or None.
    Unmapped characters are left untouched. Characters mapped to None
    are deleted.


it says "mapping of Unicode ordinals to Unicode ordinals, Unicode strings or 
None"

which reads as a mapping of (int) to (int, unicode, or None).

your translation table is a mapping of (unicode) to (str).

this works:
for f, a in zip(funnychars, asciichars):
    trans.update({ord(f): unicode(a)})

this as well:
for f, a in zip(funnychars, asciichars):
    trans.update({ord(f): ord(a)})


btw, you're updating the dict and creates a new dict for each character then 
immediately disposing it. Why not just:
for f, a in zip(funnychars, asciichars):
    trans[ord(f)] = ord(a)


___
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] Python best practices

2009-11-30 Thread Albert-Jan Roskam
I'm currently reading the book "Code Complete" (I don't know the author name), 
which gives a lot of useful best practices. It's not specifically about one 
programming language. The author stresses that the majority of those practices 
are a matter of consensus/consistency and not a matter of religion. There is no 
one best way to do it.
 
Re: functions, the author recommends that they have one purpose and one purpose 
only, and that a function returns only one result.

Cheers!!
Albert-Jan

~~
In the face of ambiguity, refuse the temptation to guess.
~~

--- On Mon, 11/30/09, Alan Gauld  wrote:


From: Alan Gauld 
Subject: Re: [Tutor] Python best practices
To: tutor@python.org
Date: Monday, November 30, 2009, 1:57 AM


"spir"  wrote

>> > - functions should return one value (im not 100% of this one)
>> 
>> I 100% disagree or with this one.
> 
> Could you explain this bit, Lie? I'm very interested.
> I use multiple-value result myself, for it's so practicle in given cases.

My take on this is that in Python its OK to return multiple values if it
is as a tuple - because a tuple is really a single value. Its like returning
a record in Pascal or a struct in C or a List in Lisp...

> But it makes me uneasy; also have the impression (why?) it
> reveals wrong design.

Its better than

> a function both to have an effect (eg assignment outside the func scope) and
> return a value.

Side effects in functions are nearly always bad news and are always
avoidable if you can return multiple values (or pass arguments by reference).

> "Command-Query Separation Principle" (Eiffel) & Pascal "procedure vs 
> function".

You can have Command/Query separation without having side-effects.
A Query is a procedure or function that doesn't change anything but just
returns a result. A Command changes something, but it can be the thing
passed to it - or if a method of a class the internal data of the class.
Again a command can be a function or a procedure. Those are separate
concepts. (I very rarely write procedures in real programs - there is nearly
always something useful you can return - and in an object that's usually
a minimum of self! (Returning self is the default idiom in Smalltalk - it
allows chaining of methods)

HTH,

Alan G. 

___
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] python and kiviat diagrams

2009-12-23 Thread Albert-Jan Roskam
I was studying the code on 
http://matplotlib.sourceforge.net/examples/api/radar_chart.html.
Isn't it very unusual that a class is defined within a function? Why not use a 
class instance inside the function instead? No methods of the class can 
currently be inherited outside the scope of the function, right?

Just curious..

Cheers!!

Albert-Jan



~~

In the face of ambiguity, refuse the temptation to guess.

~~

--- On Tue, 12/22/09, Kent Johnson  wrote:

From: Kent Johnson 
Subject: Re: [Tutor] python and kiviat diagrams
To: dwba...@earthlink.net
Cc: "python tutor" 
Date: Tuesday, December 22, 2009, 10:45 PM

On Tue, Dec 22, 2009 at 1:18 PM,   wrote:
> One of the drawbacks of Matplotlib is that it does not have the capability of 
> drawing Kiviat diagrams.
>
> Does anyone know of a software package for drawing Kiviat diagrams written in 
> Python?

I don't know what a Kiviat diagram is but the images google shows me
look a lot like this:
http://matplotlib.sourceforge.net/examples/api/radar_chart.html

Kent
___
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] python and kiviat diagrams

2009-12-25 Thread Albert-Jan Roskam
Hi all,

Thanks for your insightful answers, and a merry Christmas + a happy new year!

Btw, somebody on the list mentioned rpy as an alternative way to create spider 
charts. I also think that's a better route to take. R is thoroughly tested and 
built for the job, and if you ever need more statistical/plotting things, you 
could easily extend your program.

Cheers!!

Albert-Jan



~~

In the face of ambiguity, refuse the temptation to guess.

~~

--- On Thu, 12/24/09, Kent Johnson  wrote:

From: Kent Johnson 
Subject: Re: [Tutor] python and kiviat diagrams
To: "Albert-Jan Roskam" 
Cc: "dwbarne" , "python tutor" 
Date: Thursday, December 24, 2009, 2:19 PM

On Wed, Dec 23, 2009 at 5:27 PM, Albert-Jan Roskam  wrote:
>
> I was studying the code on 
> http://matplotlib.sourceforge.net/examples/api/radar_chart.html.
> Isn't it very unusual that a class is defined within a function?

Unusual, but not un-heard of. Usually the function is a factory
function that returns the class to the caller. In this case the class
is registered as the handler for projections of type 'radar'.
Presumably RadarAxes is implementing a protocol (interface) that is
required by register_projection().

> Why not use a class instance inside the function instead?

Presumably register_projection() requires a class (strictly speaking,
it probably requires a callable that returns an instance of a class
that implements the required protocol). There don't seem to be any
docs for register_projection() so it's hard to know for sure what it
wants.

> No methods of the class can currently be inherited outside the scope of the 
> function, right?

Do you mean, the class cannot be subclassed? Not easily, only because
the function doesn't return the class. There is no inherent reason
this class could not be subclassed if a reference to the class were
available.

Kent



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


Re: [Tutor] Listing available variables

2009-12-25 Thread Albert-Jan Roskam
This may also be useful:

>>> a = 1
>>> b = 2
>>> c = 3
>>> locals()
{'a': 1, 'c': 3, 'b': 2, '__builtins__': , 
'__name__': '__main__', '__doc__': None}
>>> locals().keys()
['a', 'c', 'b', '__builtins__', '__name__', '__doc__']
>>> 

Cheers!!

Albert-Jan



~~

In the face of ambiguity, refuse the temptation to guess.

~~

--- On Fri, 12/25/09, Lie Ryan  wrote:

From: Lie Ryan 
Subject: Re: [Tutor] Listing available variables
To: tutor@python.org
Date: Friday, December 25, 2009, 9:16 AM

On 12/25/2009 6:50 PM, Mkhanyisi Madlavana wrote:
> How do I list all the available variables in python. for example if I say:
 a = range(10)
 b = 16
 c = ""
>  (some variables)
 z = ["this","that","none"]
> I then need a command that will list the variables I assigned like:
 some_command
> a, b, c, ... (some variables), z

dir()

>>> a = range(10)
>>> b = 16
>>> c = ""
>>> z = ["this","that","none"]
>>> dir()
['__builtins__', '__doc__', '__name__', '__package__', 'a', 'b', 'c', 'z']

___
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] Is there any way I can use named tuples in Python 2.5?

2010-12-02 Thread Albert-Jan Roskam
Hi,

Is there any way I can use named tuples in Python 2.5? It's available since 2.6 
and I'm not sure if from "__future__" works. I can't try it here.
I cannot simply upgrade Python (alas!)

http://www.python.org/doc//current/library/collections.html#collections.namedtuple


 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] Is there any way I can use named tuples in Python 2.5?

2010-12-02 Thread Albert-Jan Roskam
Hi,

Thank you! Woaah, very cool code btw. There's much to learn from it, for me at 
least.

 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: Emile van Sebille 
To: tutor@python.org
Sent: Thu, December 2, 2010 8:16:19 PM
Subject: Re: [Tutor] Is there any way I can use named tuples in Python 2.5?

On 12/2/2010 9:52 AM Albert-Jan Roskam said...
> Hi,
>
> Is there any way I can use named tuples in Python 2.5? It's available since 
2.6
> and I'm not sure if from "__future__" works. I can't try it here.
> I cannot simply upgrade Python (alas!)

Maybe this'll help...

http://code.activestate.com/recipes/500261/

Emile

___
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] Question on tkinter event binding

2010-12-03 Thread Albert-Jan Roskam
Hi,

I'm trying to make a small improvement on a data entry program and it is 
literally giving me a headache. I would appreciate your help or suggestions. 


The actual program uses Autocomplete entry widgets [1], which is a subclass of 
the Tkinter Entry widget. The sample code below uses a simple Entry widget, for 
the sake of simplicity. The autocompletions are recorded in a dictionary of the 
form {entry name: set()}. The problem is that entries with typos 
cannot 
be deleted, so wrong autocomplete suggestions ("Bbbilly Gates") are given until 
the end of time. My solution: I want to bind each entry widget to the Delete 
key, which makes it possible to remove the typo-entry from the set of entries. 
I 
am using an ' expanded event handler' [2] to do the event binding.

The sample code below creates two entry widgets. The problem is that the entry 
contents is not retrieved correctly. If I fill the 2nd entry with some text, 
then hit 'Del' , it shows the content of the *first* entry. Also, I would like 
to isolate the event handler into its own function, not as a function within a 
function, but I'm not quite sure how.

[1] http://tkinter.unpythonic.net/wiki/AutocompleteEntry
[2] http://www.daniweb.com/code/snippet306072.html

from Tkinter import *

def createWidgets(veldnamen):
root=Tk()
termenlijst = {"Naam": set(["Bill Gates", "Elvis Presley"]), "*Postcode": 
set(["2600AA", "8000NN"])}
handleDels = {}
for veldnaam in veldnamen:
# tcl names must start with lowercase letter
entryWidget=Entry(root, name=veldnaam[0].lower() + veldnaam[1:])
entryWidget.grid()
def handleDel(event, widget=entryWidget, root=root, 
termenlijst=termenlijst):
vensternaam = str(root.focus_get())[1:].capitalize() # ... and back 
to uppercase
if vensternaam.startswith("*"):# mandatory fields start with 
'*' 
in my program.
vensternaam = "*" + vensternaam[1:].capitalize()
vensterinhoud = entryWidget.get()
print "Naam", vensternaam   # entry name
print "Inhoud", vensterinhoud   # entry contents
try:
termenlijst[vensternaam].remove(vensterinhoud)  # here's where 
the typo is removed
except KeyError:
pass # user tries to delete a term that doesn't exist in the 
termenlijst.
handleDels[entryWidget] = handleDel
# do all the bindings (is this where it goes wrong??)
for entryWidget, handleDel in handleDels.iteritems():
entryWidget.bind("", handleDel)
print handleDels
print termenlijst

createWidgets(["Naam", "*Postcode"])

 
Thanks again for having a look at this.

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] Question on tkinter event binding

2010-12-03 Thread Albert-Jan Roskam
Aaahhh, got it! Peace! I did two things wrong: (1) I didn't use a tcl 
StringVar() to get the entry widget contents (2) I didn't consistently close 
the 
menus generated by previous attempts to run the program, which led to 
inconsistent results.

I'll paste the working code below. It's partially in Dutch, but hey, so is 
Guido 
van Rossem. ;-)

Even so, I'd be happy to hear suggestions for improvement or simplification. 
I'd 
love to chop the code up into smaller, more comprehensible bits.

from Tkinter import *

def createWidgets(veldnamen):
root=Tk()
termenlijst = {"Naam": set(["Bill Gates", "Elvis Presley"]),
   "*Postcode": set(["2600AA", "8000BB"]),
   "Adres": set(["Street", "Avenue"])}
handleDeletions = {}
for veldnaam in veldnamen:
labelWidget=Label(root, text=veldnaam, takefocus=False)
labelWidget.grid()
# tcl names must start with a lowercase letter
tclName = veldnaam[0].lower() + veldnaam[1:]
content = StringVar()
entryWidget=Entry(root, name=tclName, textvariable=content)
entryWidget.grid()

def handleDeletion(event, widget=entryWidget, root=root, 
termenlijst=termenlijst,content=content):
actieveVenster = root.focus_get()
actieveVensternaam = str(actieveVenster)[1:].capitalize()
if actieveVensternaam.startswith("*"):
actieveVensternaam = "*" + actieveVensternaam[1:].capitalize()
vensterinhoud = content.get().strip()
print "Name: %s -- Contents: %s" % (actieveVensternaam, 
vensterinhoud)
try:
termenlijst[actieveVensternaam].remove(vensterinhoud)
actieveVenster.delete(0, END)
print "Deleted term '%s'" % vensterinhoud
except KeyError:
print "No such term '%s'" % vensterinhoud
pass
handleDeletions[entryWidget] = handleDeletion

for entryWidget, handleDeletion in handleDeletions.iteritems():
entryWidget.bind("", handleDeletion)

createWidgets(["Naam", "*Postcode", "Adres"])


 

 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: Albert-Jan Roskam 
To: Python Mailing List 
Sent: Fri, December 3, 2010 11:19:02 AM
Subject: [Tutor] Question on tkinter event binding


Hi,

I'm trying to make a small improvement on a data entry program and it is 
literally giving me a headache. I would appreciate your help or suggestions. 


The actual program uses Autocomplete entry widgets [1], which is a subclass of 
the Tkinter Entry widget. The sample code below uses a simple Entry widget, for 
the sake of simplicity. The autocompletions are recorded in a dictionary of the 
form {entry name: set()}. The problem is that entries with typos 
cannot 
be deleted, so wrong autocomplete suggestions ("Bbbilly Gates") are given until 
the end of time. My solution: I want to bind each entry widget to the Delete 
key, which makes it possible to remove the typo-entry from the set of entries. 
I 
am using an ' expanded event handler' [2] to do the event  binding.

The sample code below creates two entry widgets. The problem is that the entry 
contents is not retrieved correctly. If I fill the 2nd entry with some text, 
then hit 'Del' , it shows the content of the *first* entry. Also, I would like 
to isolate the event handler into its own function, not as a function within a 
function, but I'm not quite sure how.

[1] http://tkinter.unpythonic.net/wiki/AutocompleteEntry
[2] http://www.daniweb.com/code/snippet306072.html

from Tkinter import *

def createWidgets(veldnamen):
root=Tk()
termenlijst = {"Naam": set(["Bill Gates", "Elvis Presley"]), "*Postcode": 
set(["2600AA", "8000NN"])}
handleDels = {}
for veldnaam  in veldnamen:
# tcl names must start with lowercase letter
entryWidget=Entry(root, name=veldnaam[0].lower() + veldnaam[1:])
entryWidget.grid()
def handleDel(event, widget=entryWidget, root=root, 
termenlijst=termenlijst):
vensternaam = str(root.focus_get())[1:].capitalize() # ... and back 
to uppercase
if vensternaam.startswith("*"):# mandatory fields start with 
'*' 
in my program.
vensternaam = "*" + vensternaam[1:].capitaliz

Re: [Tutor] Question on tkinter event binding

2010-12-04 Thread Albert-Jan Roskam
Hi Patty,
 
As far as books are concerned, I actually prefer (programming) books in the 
English language. Although the Dutch don't do it as much as e.g. the French or 
the Germans, I hate it when technical terms are translated into Dutch in a 
somewhat artificial way ("Computer" is "Ordinateur" in French and "Rechner" in 
German [although "Computer" is also OK]; in Dutch it's simply "Computer") . It 
also makes it harder to find additional info on the internet. In addition, 
books 
in the English language are usually far cheaper than those in Dutch.

As far as programming itself is concerned, I find it slightly more readable to 
use Dutch variable and function names. The risk of name clashes is also 
virtually absent! In my office we have a coding convention which states that 
Dutch names should be used. I must confess, however, I don't always 
consistently 
follow the convention. One example are getter and setter methods. It's just 
clearer to use 'get'  and 'set'  in the method name. When I download software, 
I 
use then 'as-is'. I might translate code snippets so it blends better with the 
rest of the code. If I send snippets to e.g. this mailing list, I usually 
translate the variable names + comments --only this time I was a bit lazy. ;-)

 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: Patty 
To: Albert-Jan Roskam ; Python Mailing List 
Sent: Fri, December 3, 2010 11:39:28 PM
Subject: Re: [Tutor] Question on tkinter event binding

 
Hello Albert-Jan:
I am glad you made the comment below.  I was fascinated with the fact that your 
code was  partly in English/Python and also in Dutch.  I am a linguist so have 
great  interest in bilingualism.  How does this work in practice?  I mean as  a 
programmer, with native language other than English, do you download or buy  
English language software programs and work with them as-is?  Do you have  
translated tutorials to help you learn?  If you had a Dutch language  software 
program and created your own program so that everything is totally in  Dutch, 
and you wanted to communicate with English language email group :)   How would 
you do that?  Or would you try and find a Dutch language  resource?
 
Besides that, I am definitely saving your code  segments for the future.  
Thanks 
for sharing.
 
Patty
- Original Message - 
>From: Albert-Jan Roskam 
>To: Albert-Jan Roskam ; Python Mailing List 
>Sent: Friday, December 03, 2010 12:18PM
>Subject: Re: [Tutor] Question on tkinterevent binding
>
>
>
> 
>I'llpaste the working code below. It's partially in Dutch, but hey, so is 
>Guidovan Rossem. ;-)
>
>
>


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


Re: [Tutor] Question on tkinter event binding

2010-12-04 Thread Albert-Jan Roskam
Hi Evert,

I actually wanted the var names to be in English, but apparently the people who 
came up with the coding convention did not agree with me on this. Then again, 
the Zen of Python states that (1) readability counts and that (2) practicality 
beats purity. Most of the time, I'm 120% sure the code doesn't leave Holland. 
If 
it inadvertently does, Ctrl-H is my friend.  It's impractical to overgeneralize 
one's applications. Let's say I have a list of all the medical specialisms we 
have here in N. I really wouldn't know all the exact English equivalents. My 
manager wouldn't like it if I spent my time doing all the translations. 
Besides, 
doesn't it with the current political climate to have dutch-only code? ;-) 
[Note: We had a landslide win of a xenophobic, right-wing party during the last 
elections]

Meanwhile, I tinkered a bit more with the code. I used exec() to isolate the 
event handler function. It works and it's better, but I think it could be still 
better. I'm not so fond of eval() and exec().

from Tkinter import *

def createWidgets(veldnamen):
root=Tk()
termenlijst = {"Naam": set(["Bill Gates", "Elvis Presley"]),
   "*Postcode": set(["2600AA", "8000BB"]),
   "Adres": set(["Street", "Avenue"])}
handleDeletions = {}
for veldnaam in veldnamen:
labelWidget=Label(root, text=veldnaam, takefocus=False)
labelWidget.grid()
# tcl names must start with a lowercase letter
tclName = veldnaam[0].lower() + veldnaam[1:]
content = StringVar()
entryWidget=Entry(root, name=tclName, textvariable=content)
entryWidget.grid()

exec(doHandleDeletion())
handleDeletions[entryWidget] = handleDeletion

for entryWidget, handleDeletion in handleDeletions.iteritems():
entryWidget.bind("", handleDeletion)

def doHandleDeletion():
func = \
"""def handleDeletion(event, widget=entryWidget, root=root, 
termenlijst=termenlijst,content=content):
actieveVenster = root.focus_get()
actieveVensternaam = str(actieveVenster)[1:].capitalize()
if actieveVensternaam.startswith("*"):
actieveVensternaam = "*" + actieveVensternaam[1:].capitalize()
vensterinhoud = content.get().strip()
print "Name: %s -- Contents: %s" % (actieveVensternaam, 
vensterinhoud)
try:
termenlijst[actieveVensternaam].remove(vensterinhoud)
actieveVenster.delete(0, END)
print "Deleted term '%s'" % vensterinhoud
except KeyError:
print "No such term '%s'" % vensterinhoud
pass"""
return func

createWidgets(["Naam", "*Postcode", "Adres"])


 



 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: Evert Rol 
To: Albert-Jan Roskam 
Cc: Patty ; Python Mailing List 
Sent: Sat, December 4, 2010 2:10:04 PM
Subject: Re: [Tutor] Question on tkinter event binding

> Hi Patty,
>  
> As far as books are concerned, I actually prefer (programming) books in the 
>English language. Although the Dutch don't do it as much as e.g. the French or 
>the Germans, I hate it when technical terms are translated into Dutch in a 
>somewhat artificial way ("Computer" is "Ordinateur" in French and "Rechner" in 
>German [although "Computer" is also OK]; in Dutch it's simply "Computer") . It 
>also makes it harder to find additional info on the internet. In addition, 
>books 
>in the English language are usually far cheaper than those in Dutch.
> 
> As far as programming itself is concerned, I find it slightly more readable 
> to 
>use Dutch variable and function names. The risk of name clashes is also 
>virtually absent! In my office we have a coding convention which states that 
>Dutch names should be used. I must confess, however, I don't always 
>consistently 
>follow the convention. One example are getter and setter methods. It's just 
>clearer to use 'get'  and 'set'  in the method name. When I download software, 
>I 
>use then 'as-is'. I might translate code snippets so it blends better with the 
>rest of the code. If I send snippets to e.g. this mailing list, I usually 
>translate the v

Re: [Tutor] Question on tkinter event binding

2010-12-04 Thread Albert-Jan Roskam
Hi Steven,

Awesome, I've got it working now!  Here's the code:  
http://pastebin.com/BQhW8piD (I also pasted it below this message).
I tried your approach before but I abandoned it because I made a mistake in 
lines 16 (pass None for event) and 22 (I didn't use the parameters of the outer 
function as the arguments of the inner function. Thanks a lot for putting me 
back on the right track!! It's really working cool now and I was able to remove 
some unnecessary code (and I added some bells and whistles ;-))

Cheers!!
Albert-Jan

import Tkinter, time, sys

# To be used in conjunction with the AutocompleteEntry class:
# http://tkinter.unpythonic.net/wiki/AutocompleteEntry

def createWidgets(veldnamen, termenlijst):
root=Tk()
for veldnaam in veldnamen:
labelWidget=Tkinter.Label(root, text=veldnaam, takefocus=False)
labelWidget.grid()
# tcl names must start with a lowercase letter
tclName = veldnaam[0].lower() + veldnaam[1:]
entryWidget=Tkinter.Entry(root, name=tclName, highlightcolor="yellow")
entryWidget.grid()
makeDeletionHandler(event=None,
widget=entryWidget,
root=root,
termenlijst=termenlijst)

def makeDeletionHandler(event, widget, root, termenlijst):
def handleDeletion(event, widget=widget, root=root, 
termenlijst=termenlijst):
vensternaam = str(widget)[1:].capitalize()
if vensternaam.startswith("*"):
vensternaam = "*" + vensternaam[1:].capitalize()
vensterinhoud = widget.get().strip()
print "Name: %s -- Contents: %s" % (vensternaam, vensterinhoud)
try:
termenlijst[vensternaam].remove(vensterinhoud)
widget.delete(0, END)
widget.configure(bg = "green")
#print termenlijst
print "Deleted term '%s'" % vensterinhoud
except KeyError:
print "No such term '%s'" % vensterinhoud
pass
finally:
delay = 0.5
if sys.platform.lower().startswith("win"):
delay = delay * 1000
time.sleep(delay) # Windows: specify in ms!)
widget.configure(bg = "white")
widget.bind("", handleDeletion)
return handleDeletion

createWidgets(veldnamen = ["Naam", "*Postcode", "Adres", "*Organization name"],
  termenlijst = {"Naam": set(["Bill Gates", "Elvis Presley"]),
 "*Postcode": set(["2600AA", "8000BB"]),
 "Adres": set(["Street", "Avenue"]),
 "*Organization name": set(["CWI", "MIT"])})




~~
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: Steven D'Aprano 
To: Python Mailing List 
Sent: Sat, December 4, 2010 3:49:26 PM
Subject: Re: [Tutor] Question on tkinter event binding

Albert-Jan Roskam wrote:

> Meanwhile, I tinkered a bit more with the code. I used exec() to isolate the 
>event handler function. It works and it's better, but I think it could be 
>still 
>better. I'm not so fond of eval() and exec().

They have their uses, but yes, it's best to avoid them unless you need them. 
Let's see if we can avoid them :)


> from Tkinter import *
> 
> def createWidgets(veldnamen):
> root=Tk()
> termenlijst = {"Naam": set(["Bill Gates", "Elvis Presley"]),
>"*Postcode": set(["2600AA", "8000BB"]),
>"Adres": set(["Street", "Avenue"])}
> handleDeletions = {}
> for veldnaam in veldnamen:
> labelWidget=Label(root, text=veldnaam,  takefocus=False)
> labelWidget.grid()
> # tcl names must start with a lowercase letter
> tclName = veldnaam[0].lower() + veldnaam[1:]
> content = StringVar()
> entryWidget=Entry(root, name=tclName, textvariable=content)
> entryWidget.grid()
> 
> exec(doHandleDeletion())
> handleDeletions[entryWidget] = handleDeletion

The classic solution for callbacks is to use lambda, but of course lambda is 
limited to a single expression and won't do the job here. So what you need is a 
factory function that returns a new function:


Re: [Tutor] Feedback on coding style

2010-12-09 Thread Albert-Jan Roskam
Hi,

Re: coding style, I can *really* recommend the book 'Code Complete' 
(http://cc2e.com/). It doesn't focus on Python specifically, but it's a 
wonderful book. You can find a pdf checklist of the book if you Google a bit.
 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: Dave Angel 
To: howit...@archlinux.us
Cc: tutor@python.org
Sent: Wed, December 8, 2010 11:01:05 PM
Subject: Re: [Tutor] Feedback on coding style

On 01/-10/-28163 02:59 PM, howit...@archlinux.us wrote:
> Hi.
> 
> For the past week, I've been following an online Python guide named:
> 'Learn Python the Hard Way'. I'm very happy with it as it gives a lot of
> freedom to explore.
> 
> However, due to this I have no idea if I'm thinking the right way. That's
> why I've attached a script of mine I've been working on all day.
> 
> It works a 100%, but I'm afraid I've made very bad choices concerning
> design and coding style. (If it could've been much simpler, if there are
> glaring mistakes, poor methods, ..)
> 
> Could anyone be so friendly as to offer a bit of feedback, to a newbie?
> 
> PS: The script very simple accepts 2 arguments from the commandline.
>     First arg being the number to which should be counted,
>     second arg being the interval.
> 
> Thank you,
> Adrian

I agree with Hugo, probably on all his points.  But to help you fix it, I think 
I can help you a bit as well.

First is I think you're misunderstanding the purpose of a function.  A function 
should be a self-contained piece of code that gets called, and that returns 
when 
done.  You're using them mostly as a way to implement what BASIC used to have 
as 
a GOTO.  For example, when a loop is needed, you do it by calling another 
function which calls the first one.  That is called recursion if it's done on 
purpose, and a bug if it happens by accident.

Second is that you're misusing global variables.  A function needs to be called 
with those values it needs to do its work, and it needs to return the results 
of 
that work.  Very seldom should those things be globals.

Start with the first function.  You declare it with maxn and incr as 
parameters, 
and you call it correctly.  But it also uses the globals, rather than using the 
ones passed in.

Then the function ask_change().  You should be passing it the two arguments, 
and 
getting back modified arguments as return values.  And the function shouldn't 
call the_loop() itself, it should just get the new values.

jibjab_result() is trying to be a loop, by effectively calling itself, through 
choice_result().  If you need a loop, just write one.  And that loop will 
probably be in ask_change(), without needing any other functions inside.

It's good to decompose a problem into functions, but you're not using functions 
in the way they're designed.  For small problems, this can work, but as the 
problems get more complex, you'll be forced to change your habits.

DaveA



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


[Tutor] pyodbc/date values in MS Access

2010-12-15 Thread Albert-Jan Roskam
Hi,

I'm using pyodbc (Python 2.5) to insert records in an MS Access database. For 
security reasons, question marks should be used for string replacement [*]. The 
standard %s would make the code vulnerable to sql code injection. Problem is, 
string replacement in the Good Way somehow doesn't work when the values are 
dates. Below, snippet #1 does not work (Access says the inserted value is not 
consistent with the defined datatype), but #2 does. I tried various other ways 
(ie. DateValue, CDate, etc.) but none of them works. Is there a solution for 
this?

[*] see http://code.google.com/p/pyodbc/wiki/GettingStarted --> under 
'Parameters'

### 1
sql = "INSERT INTO tblSomeTable (myDate) VALUES (?);"
cursor.execute(sql, "#01/01/2010#")

### 2
sql = "INSERT INTO tblSomeTable (myDate) VALUES (%s);"
cursor.execute(sql % "#01/01/2010#")

 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] pyodbc/date values in MS Access

2010-12-22 Thread Albert-Jan Roskam
Hi,

Sorry for the late reply, but thanks a lot for helping me. It's solved now. 
Peter, the link you posted in another thread (or should I say 'query') was also 
relevant AND funny (http://xkcd.com/327/)

Merry Christmas and Happy Coding! *)

 Cheers!!
Albert-Jan

*) Including those who have to parse a huge xml file *winks* ;-)


~~
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: Peter Otten <__pete...@web.de>
To: tutor@python.org
Sent: Wed, December 15, 2010 3:06:19 PM
Subject: Re: [Tutor] pyodbc/date values in MS Access

Albert-Jan Roskam wrote:

> Hi,
> 
> I'm using pyodbc (Python 2.5) to insert records in an MS Access database.
> For security reasons, question marks should be used for string replacement
> [*]. The standard %s would make the code vulnerable to sql code injection.
> Problem is, string replacement in the Good Way somehow doesn't work when
> the values are dates. Below, snippet #1 does not work (Access says the
> inserted value is not consistent with the defined datatype), but #2 does.
> I tried various other ways (ie. DateValue, CDate, etc.) but none of them
> works. Is there a solution for this?
> 
> [*] see http://code.google.com/p/pyodbc/wiki/GettingStarted --> under
> 'Parameters'
> 
> ### 1
> sql = "INSERT INTO tblSomeTable (myDate) VALUES (?);"
> cursor.execute(sql, "#01/01/2010#")


(1) Try providing the date in ISO format "-mm-dd"

"2010-01-01"

or (even better if supported) as a date value

from datetime import date
date(2010, 1, 1)

(2) Wrap the value into a tuple which I think is required by the Python 
DBAPI.

cursor.execute(sql, ("2010-01-01",))
cursor.execute(sql, (date(2010, 1, 1),))

Peter

___
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] Weighted Random Choice - Anyone have an efficient algorithm?

2010-12-24 Thread Albert-Jan Roskam
Hi Steven,

Doesn't this qualify as 'monkeying with the loop index'? [*]

>>> import random
>>> weights = [5, 20, 75]
>>> counts = {0:0, 1:0, 2:0}
>>> for i in xrange(100):
... i = weighted_choice(weights) # <--- monkeying right here (?)
... counts[i] += 1

[*] http://stackoverflow.com/questions/457036/dont-monkey-with-the-loop-index


 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: Steven D'Aprano 
To: tutor@python.org
Sent: Thu, December 23, 2010 1:30:50 PM
Subject: Re: [Tutor] Weighted Random Choice - Anyone have an efficient 
algorithm?

Modulok wrote:
> Does anyone know of an efficient way of doing a weighted random
> choice? (I don't even know what algorithms like this would be called.)

If you google for "python weighted random choice" you will find a number of 
hits.


> Preferably, something that doesn't grow exponentially with the number
> of elements in the list, or the size of their respective values.


Here's one method that is linear on the number of elements:

def weighted_choice(weights):
total = sum(weights)
p = random.uniform(0, total)  # random float between 0 and total.
assert 0.0 <= p <= total
# Do a linear search for the right index value. If you have a
# huge number of weights, a binary search may be faster.
running_total = 0
for i, weight in enumerate(weights):
running_total += weight
if p <= running_total:
return i

And tested:

>>> import random
>>> weights = [5, 20, 75]
>>> counts = {0:0, 1:0, 2:0}
>>> for i in xrange(100):
... i = weighted_choice(weights)
... counts[i] += 1
...
>>> counts
{0: 50252, 1: 17, 2: 749751}
>>> [n*1e6/100 for n in weights]  # expected values
[5.0, 20.0, 75.0]

As you can see, the results are very close to what should be expected, and of 
course the difference can be chalked up to random chance.



-- 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] Help on RE

2011-01-23 Thread Albert-Jan Roskam


http://imgs.xkcd.com/comics/regular_expressions.png

;-)
 
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: Steven D'Aprano 
To: tutor@python.org
Sent: Sun, January 23, 2011 4:10:35 AM
Subject: Re: [Tutor] Help on RE

tee chwee liong wrote:
> thanks for making me understand more on re. re is a confusing topic as i'm 
>starting on python. 
>

I quote the great Jamie Zawinski, a world-class programmer and hacker:

Some people, when confronted with a problem, think 'I know, I'll
use regular expressions." Now they have two problems.


Zawinski doesn't mean that you should never use regexes. But they should be 
used 
only when necessary, for problems that are difficult enough to require a 
dedicated domain-specific language for solving search problems.

Because that's what regexes are: they're a programming language for text 
searching. They're not a full-featured programming language like Python 
(technically, they are not Turing Complete) but nevertheless they are a 
programming language. A programming language with a complicated, obscure, 
hideously ugly syntax (and people complain about Forth!). Even the creator of 
Perl, Larry Wall, has complained about regex syntax and gives 19 serious faults 
with regular expressions:

http://dev.perl.org/perl6/doc/design/apo/A05.html

Most people turn to regexes much too quickly, using them to solve problems that 
are either too small to need regexes, or too large. Using regexes for solving 
your problem is like using a chainsaw for peeling an orange.

Your data is very simple, and doesn't need regexes. It looks like this:


Platform: PC
Tempt : 25
TAP0 :0
TAP1 :1
+
Port Chnl Lane EyVt EyHt
+
0  1  1  75  55
0  1  2  10 35
0  1  3  25 35
0  1  4  35 25
0  1  5  10 -1
+
Time: 20s


The part you care about is the table of numbers, each line looks like this:

0  1  5  10 -1

The easiest way to parse this line is this:

numbers = [int(word) for word in line.split()]

All you need then is a way of telling whether you have a line in the table, or 
a 
header. That's easy -- just catch the exception and ignore it.

template = "Port=%d, Channel=%d, Lane=%d, EyVT=%d, EyHT=%d"
for line in lines:
try:
numbers = [int(word) for word in line.split()]
except ValueError:
continue
print(template % tuple(numbers))


Too easy. Adding regexes just makes it slow, fragile, and difficult.


My advice is, any time you think you might need regexes, you probably don't.


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


[Tutor] regex questions

2011-02-17 Thread Albert-Jan Roskam
Hello,

I have a couple of regex questions:

1 -- In the code below, how can I match the connecting words 'van de' , 'van 
der', etc. (all quite common in Dutch family names)?
2 -- It is quite hard to make a regex for all surnames, but easier to make 
regexes for the initials and the connecting words. How could I ' subtract'  
those two regexes to end up with something that matches the surnames (I used 
two 
.replaces() in my code, which roughly work, but I'm thinking there's a re way 
to 
do it, perhaps with carets (^).
3 -- Suppose I want to yank up my nerd rating by adding a re.NONDIACRITIC flag 
to the re module (matches letters independent of their accents), how would I go 
about? Should I subclass from re and implement the method, using the other 
existing methods as an example? I would find this a very useful addition.

Thanks in advance for your thoughts!

Python 2.7.0+ (r27:82500, Sep 15 2010, 18:04:55) 
[GCC 4.4.5] on linux2

>>> import re
>>> names = ["J. van der Meer", "J. van den Meer", "J. van Meer", "Meer, J. van 
>>>der", "Meer, J. van den", "Meer, J. van de", "Meer, J. van"]
>>> for name in names:
print re.search("(van? de[nr]?)\b? ?", name, re.IGNORECASE).group(1)

van der
van den
Traceback (most recent call last):
  File "", line 2, in 
print re.search("(van? de[nr]?)\b? ?", name, re.IGNORECASE).group(1)
AttributeError: 'NoneType' object has no attribute 'group'

 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] regex questions

2011-02-18 Thread Albert-Jan Roskam
Hi Steven,

Thanks a BUNCH for helping me! Yes, you were correct in assuming that my input 
data are already names. They're names in a column in a csv file.  They're the 
names of GPs, in various formats. Besides the forms I've mentioned already 
there 
are examples such as 'Doctor's office Duh, J. & Dah, J.', with or without 
initials and/or connecting words. There are also offices with names as Doctor's 
Office 'Under the Oaks'. I want to normalise those cases too till  'Doctor's 
office J. Duh & J. Dah', etc. Currently I use " & ".split() and apply my 
regexes 
(I use three, so I will certainly study your very fancy function!).


So the raw string \b means means "ASCII backspace". Is that another way of 
saying that it means 'Word boundary'?

You're right: debugging regexes is a PIA. One teeny weeny mistake makes all the 
difference. Could one say that, in general, it's better to use a Divide and 
Conquer strategy and use a series of regexes and other string operations to 
reach one's goal?

http://code.activestate.com/recipes/251871-latin1-to-ascii-the-unicode-hammer/ 
is interesting. I did something similar with  unicode.translate(). Many people 
here have their keyboard settings as US, so accented letters are not typed very 
easily, and are therefore likely to be omitted (e.g. enquête vs enquete). 


Thanks again!

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: Steven D'Aprano 
To: Python Mailing List 
Sent: Fri, February 18, 2011 4:45:42 AM
Subject: Re: [Tutor] regex questions

Albert-Jan Roskam wrote:
> Hello,
> 
> I have a couple of regex questions:
> 
> 1 -- In the code below, how can I match the connecting words 'van de' , 'van 
>der', etc. (all quite common in Dutch family names)?

You need to step back a little bit and ask, what is this regex supposed to 
accomplish? What is your input data? Do you expect this to tell the difference 
between van used as a connecting word in a name, and van used otherwise?

In other words, do you want:

re.search(???, "J. van Meer")  # matches
re.search(???, "The van stopped")  # doesn't match

You might say, "Don't be silly, of course not!" *but* if you expect this regex 
to detect names in arbitrary pieces of text, that is exactly what you are 
hoping 
for. It is beyond the powers of a regex alone to distinguish between arbitrary 
text containing a name:

"... and to my nephew Johann van Meer I leave my collection of books..."

and arbitrary text without a name:

"... and the white van merely touched the side of the building..."

You need a proper parser for that.

I will assume that your input data will already be names, and you just want to 
determine the connecting words:

van der
van den
van de
van

wherever they appear. That's easy: the only compulsory part is "van":

pattern = r"\bvan\b( de[rn]?)?"

Note the use of a raw string. Otherwise, \b doesn't mean "backslash b", but 
instead means "ASCII backspace".

Here's a helper function for testing:

def search(pattern, string):
mo = re.search(pattern, string, re.IGNORECASE)
if mo:
return mo.group(0)
return "--no match--"


And the result is:

>>> names = ["J. van der Meer", "J. van den Meer", "J. van Meer",
... "Meer, J. van der", "Meer, J. van den", "Meer, J. van de",
... "Meer, J. van"]
>>>
>>> for name in names:
... print search(pattern, name)
...
van der
van den
van
van der
van den
van de
van

Don't forget to test things which should fail:

>>> search(pattern, "Fred Smith")
'--no match--'
>>> search(pattern, "Fred Vanderbilt")
'--no match--'



> 2 -- It is quite hard to make a regex for all surnames, but easier to make 

"\b[a-z]+[-']?[a-z]*\b" should pretty much match all surnames using only 
English 
letters, apostrophes and hyphens. You can add in accented letters as need.

(I'm lazy, so I haven't tested that.)


> regexes for the initials and the connecting words. How could I ' subtract'  
>those two regexes to end up with something that matches the surnames (I used 
>two 
>.replaces() in my code, which roughly work, but I'm thinking there's a re way 
>to 
>do it, perhaps with carets (^).

Don't try to use regexes to do too

[Tutor] ctypes question

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

I want to use a dll to read Spss data files. But when I use 
lib = ctypes.cdll.LoadLibary("d:/temp/spssio32.dll")
I get a WindowsError (cannot find module), even though the path exists. Why is 
that? Do I need to extend some environment variable (add another dir)? I am 
using Python 2.5 on Windows 2000, but I also unsuccessfully tried it using 
Python 2.7 on Ubuntu 10 (however, I don't want to write the program on a linux 
system because I'll be using it on windows only). I also got the same error 
when 
I tried some other dll's.

Thanks in advance!
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] ctypes question

2011-03-08 Thread Albert-Jan Roskam
Hi all,

Thanks for your replies. It wasn't a permissions issue. Apparently, not the 
full 
path name should be used. When I use os.chdir (by the way: why on earth isn't 
this called os.setcwd()?? That's consistent with os.getcwd()) and then use the 
file name only, it works. See the Idle session below. Thought it'd be nice to 
share this with you.

Best wishes,
Albert-Jan

Python 2.5.4 (r254:67916, Dec 23 2008, 15:10:54) [MSC v.1310 32 bit (Intel)] on 
win32

IDLE 1.2.4  
>>> import ctypes
>>> lib = ctypes.cdll.LoadLibrary("d:/temp/spss_io/win32/spssio32.dll")

Traceback (most recent call last):
  File "", line 1, in 
lib = ctypes.cdll.LoadLibrary("d:/temp/spss_io/win32/spssio32.dll")
  File "C:\Python25\lib\ctypes\__init__.py", line 431, in LoadLibrary
return self._dlltype(name)
  File "C:\Python25\lib\ctypes\__init__.py", line 348, in __init__
self._handle = _dlopen(self._name, mode)
WindowsError: [Error 126] Kan opgegeven module niet vinden
>>> import os
>>> os.path.exists("d:/temp/spss_io/win32/spssio32.dll")
True
>>> os.chdir("d:/temp/spss_io/win32")
>>> lib = ctypes.cdll.LoadLibrary("spssio32.dll")
>>> dir(lib)
['_FuncPtr', '__class__', '__delattr__', '__dict__', '__doc__', '__getattr__', 
'__getattribute__', '__getitem__', '__hash__', '__init__', '__module__', 
'__new__', '__reduce__', '__reduce_ex__', '__repr__', '__setattr__', '__str__', 
'__weakref__', '_handle', '_name']
>>> 

 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: Alan Gauld 
To: tutor@python.org
Sent: Mon, March 7, 2011 7:26:15 PM
Subject: Re: [Tutor] ctypes question


"Pacific Morrowind"  wrote

> but that path doesn't actually exist... replace that path with either
> r"d:/temp/spssio32.dll" or with "d://temp//spssio32.dll"; otherwise the
> /t will be transformed into a tab.

You've got your / and \ mixed up.

Forward slashes (/) are fine in Windows paths. It's back
slashes(\) that need escaping or raw stringing. \t is tab...

On the original ask - could it be a permissions issue?
Does your app have acess to the file?

Alan G. 

___
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] ctypes question

2011-03-08 Thread Albert-Jan Roskam
Hi Joel,

I found this on stackoverflow*)
os.environ['PATH'] = os.path.dirname(__file__) + ';' + os.environ['PATH']
windll.LoadLibrary('mydll.dll')

It extends the directory list of the environment variable 'path'.

Now at least I've loaded the dll, but I still need to read up on ctypes an file 
handles. 

Any good pointers (no pun intended ;-) are welcome!

 *) 
http://stackoverflow.com/questions/2980479/python-ctypes-loading-dll-from-from-a-relative-path


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: Joel Goldstick 
To: tutor@python.org
Sent: Tue, March 8, 2011 12:14:45 PM
Subject: Re: [Tutor] ctypes question




On Tue, Mar 8, 2011 at 4:09 AM, ALAN GAULD  wrote:

> When I use os.chdir (by the way: why on earth isn't this called os.setcwd()?? 
>> That's consistent with os.getcwd()) 
>
>History.
>They are Unix commands (and possibly Multics/PDP before that!).
>cd has been the command in almost every CLI OS I've ever used from 
>CP/M thru' OS/9, Unix, DOS,  etc...
>
>The only exceptions being VAX/VMS(uses 'set def') and OS/390 on 
>a mainframe which doesn't use a directory based file system.
>
>That doesn't mean Python shouldn't adopt a more consistent naming 
>scheme it's just that the folks building it simply transferred the names 
>of the commands that they were familiar with. Its a self perpetuating
>habit... :-)
>
>Alan G.
>
>___
>Tutor maillist  -  Tutor@python.org
>To unsubscribe or change subscription options:
>http://mail.python.org/mailman/listinfo/tutor
>
>
I'm glad to see you got the result you wanted.  But, by moving your current 
working directory to the library's directory seems like it could cause other 
problems with the code.  I don't do python on windows, and have unremembered a 
lot I used to know about windows.  So, my question is, isn't there another way 
to do this?

-- 
Joel Goldstick


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


[Tutor] ctypes and spssio.dll

2011-03-16 Thread Albert-Jan Roskam
Hi,

I'm still working on a program that uses a .dll to read SPSS system files.
It's getting somewhere already, but I'm still struggling with one thing. 
When using the C function spssGetVarNames I'm having trouble 
translating C arrays to Python lists. I want to translate an 
array that holds the variable names into a Python list. Same thing 
for variable types.

The documentation of the SPSSIO dll says the following:
spssGetVarNames
int spssGetVarNames (int handle, int *numVars, char ***varNames, int **varTypes)
Parameter - Description
handle - Handle to the data file
numVars - Pointer to number of variables
varNames - Pointer to array of pointers to variable
varTypes - Pointer to array of variable types

In the code below, which can also be found on http://pastebin.com/d7d0hpyV, 
the equivalent Python function is called getVarNamesAndTypes(). This is 
the output I typically get:
retcode: 0
Varnames: ['\x80\xde\x10\x01P\xd8\x10\x01\xf0\xd0\x10\x01', None, None, None, 
None, None, None, None, None, None]
varTypes: [17885264, 0, 0, 0, 0, 0, 0, 0, 0, 0]

The first item of each list is clearly wrong, but what does it mean? If varType 
> 0 it is
supposed to be a string var of that length. And of course the varNames are 
mostly 'None'.

Probably, something is wrong with the way the C arrays are initialized, 
or with the two list comprehensions, but I can't find the solution.
varNames = [varNamesPtr[0][i] for i in range(numVars)] does not seem to work.

For those who are interested, the complete program can be found on 
http://pastebin.com/ff1b1Y9a (note that it's still work in progress)

Any hints? Thanks very much in advance!

import os, ctypes, datetime
 
# dll and py file should be in the same dir!
def loadSavFile(fn):
os.environ["PATH"] += ";" + os.path.abspath(os.curdir)
ctypes.cdll.LoadLibrary("spssio32.dll")
spssio = ctypes.windll.spssio32
libc = ctypes.cdll.msvcrt
 
if os.path.exists(fn):
fh = libc._fdopen(fn, "rb")
fhPtr = ctypes.pointer(ctypes.c_int(fh))
retcode = spssio.spssOpenRead(ctypes.c_char_p(fn), fhPtr)
return retcode, spssio, fh
else:
raise Exception, "File '%s' does not exist!" % fn
 
def getVarNamesAndTypes(fh, spssio):
numVarsPtr = ctypes.pointer(ctypes.c_int())
spssio.spssGetNumberofVariables(fh, numVarsPtr)
numVars = numVarsPtr[0]

varNamesArray = (ctypes.c_char_p * numVars)() 
varNamesPtr = ctypes.pointer(varNamesArray)
 
varTypesArray = (ctypes.c_int * numVars)()
varTypesPtr = ctypes.pointer(varTypesArray)

retcode = spssio.spssGetVarNames(fh, numVarsPtr, varNamesPtr, varTypesPtr)
 
varNames = [varNamesPtr[0][i] for i in range(numVars)] # -- WRONG!!
varTypes = [varTypesPtr[0][i] for i in range(numVars)]
 
return retcode, varNames, varTypes
 
savFileName = r"C:\Program Files\SPSS\Employee data.sav"
retcode, spssio, fh = loadSavFile(savFileName)
retcode, varNames, varTypes = getVarNamesAndTypes(fh, spssio)
print "retcode:", retcode
print "Varnames:", varNames

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] how to join two text files ?

2011-03-27 Thread Albert-Jan Roskam
Hello,

If the files are not too big, you could do something like:
with open("/home/me/Desktop/test.csv", "wb") as w:
writer = csv.writer(w)
for f in glob.glob("/home/me/Desktop/files/*.txt"):
rows = open(f, "rb").readlines()
writer.writerows(rows)

 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: Mateusz K 
To: tutor@python.org
Sent: Sat, March 26, 2011 10:08:43 PM
Subject: [Tutor] how to join two text files ?

Hello,

I have many text files, where data is delimited by space.
Each file contain three colums: coordinate x, coordinate y and value for this 
location.

I would like to join this data to get one big file which will contain 
columns:
coordinate x, coordinate y, value1,value2,..., value_n and save it to another 
text file.

I wrote script, but there's some stupid error (like "\n" character although
I have added .rstrip() command).

Coul You tell me what is most convenient way to join this data
(maybe by using csv module?)?

=
Best regards,
Mateusz

___
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] how to optimize this code?

2011-03-27 Thread Albert-Jan Roskam
Hello,

I made a program that reads spss data files. I ran cProfile to see if I can 
optimize things (see #1 below).
It seems that the function getValueNumeric is a pain spot (see #2 below). This 
function calls a C function in a dll for each numerical cell value. On the 
basis 
of this limited amount of info, what could I do to further optimize the code? I 
heard about psyco, but I didn't think such tricks would be necessary as the 
function spssGetValueNumeric is is implemented in C already (which should be 
fast).

1 ## cProfile output
 50018706 function calls in 521.261 CPU seconds

   Ordered by: standard name

   ncalls  tottime  percall  cumtime  percall filename:lineno(function)
10.0000.000  521.261  521.261 :1()
  2470.0010.0000.0010.000 :1(fileno)
10.2750.2750.4360.436 SPSSio12.py:102(loadSavFile)
10.0000.0000.0000.000 
SPSSio12.py:125(getNumberofVariables)
  2000.0020.0000.0040.000 SPSSio12.py:132(getVarNameAndType)
10.0010.0010.0050.005 SPSSio12.py:140(getVarInfo)
10.0000.0000.0000.000 SPSSio12.py:150(getNumberofCases)
  1000.0020.0000.0020.000 SPSSio12.py:157(getVarHandle)
 1000   96.0900.000  103.0740.000 SPSSio12.py:177(getValueNumeric)
  1000.0020.0000.0020.000 SPSSio12.py:206(getVarPrintFormat)
 1000   57.9370.000   57.9370.000 SPSSio12.py:224(formatValue)
10.0070.0071.4031.403 SPSSio12.py:260(getFileReport)
 1002  313.0490.000  486.6740.000 SPSSio12.py:270(savReader)
1   34.181   34.181  521.261  521.261 SPSSio12.py:349(main)

## most time consuming function

 def getValueNumeric(fh, spssio, varHandle):
numValue = ctypes.c_double()
numValuePtr = ctypes.byref(numValue)
retcode = spssio.spssGetValueNumeric(fh,
   ctypes.c_double(varHandle),
   numValuePtr)

return retcode, numValue.value

Thanks in advance for your thoughts!

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] how to optimize this code?

2011-03-28 Thread Albert-Jan Roskam
Hi Stefan,

Thanks for your advice. I seriously thought ctypes was the module to use. That 
was before I found out the evaluating all 10**9 values of my test data set is 
glacially slow (several hours). You're right, the dll implies the program is 
running on windows. I've also been trying to make it work under Linux but I 
wanted to get the basic algorithm right first. Also, it was quite a PIA to get 
all the dependencies of the (old) .so files.

Your speed tip reminded me of: 
http://wiki.python.org/moin/PythonSpeed/PerformanceTips#Avoiding_dots...
Does this mean that "from ctypes import *" gives slightly faster code than 
"import ctypes"? If so: wow! I've always avoided the first notation like the 
plague.

What do you mean with '... using a constant pointer for numValue' ? Is this the 
byref/pointer object distinction? I replaced a the pointer object with a byref 
object, which reduced processing time by about 10 %.

Cython might be interesting as a hobby project, but I'm affraid I'll never get 
the ICT droids in my office to install that.
 

 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: Stefan Behnel 
To: tutor@python.org
Sent: Mon, March 28, 2011 7:43:16 AM
Subject: Re: [Tutor] how to optimize this code?

Albert-Jan Roskam, 27.03.2011 21:57:
> I made a program that reads spss data files. I ran cProfile to see if I can
> optimize things (see #1 below).

First thing to note here: sort the output by "time", which refers to the 
"tottime" column. That will make it more obvious where most time is really 
spent.


> It seems that the function getValueNumeric is a pain spot (see #2
> below). This function calls a C function in a dll for each numerical
> cell value. On the basis of this limited amount of info, what could I do
> to further optimize the code? I heard about psyco, but I didn't think
> such tricks would be necessary as the function spssGetValueNumeric is is
> implemented in C already (which should be fast).

The problem is that you are using ctypes to call it. It's useful for simple 
things, but it's not usable for performance critical things, such as calling a 
C 
function ten million times in your example. Since you're saying "dll", is this 
under Windows? It's a bit more tricky to set up Cython on that platform than on 
pretty much all others, since you additionally need to install a C compiler, 
but 
if you want to go that route, it will reward you with a much faster way to call 
your C code, and will allow you to also speed up the code that does the calls.

That being said, see below.


> ## most time consuming function
> 
>   def getValueNumeric(fh, spssio, varHandle):
>  numValue = ctypes.c_double()
>  numValuePtr = ctypes.byref(numValue)
>  retcode = spssio.spssGetValueNumeric(fh,
> ctypes.c_double(varHandle),
> numValuePtr)

You may still be able to make this code a tad faster, by avoiding the function 
name lookups on both the ctypes module and "spssio", and by using a constant 
pointer for numValue (is you're not using threads). That may not make enough of 
a difference, but it should at least be a little faster.

Stefan

___
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] can I walk or glob a website?

2011-05-18 Thread Albert-Jan Roskam
Hello,

How can I walk (as in os.walk) or glob a website? I want to download all the 
pdfs from a website (using urllib.urlretrieve), extract certain figures (using 
pypdf- is this flexible enough?) and make some statistics/graphs from those 
figures (using rpy and R). I forgot what the process of 'automatically 
downloading' is called again, something that sounds like 'whacking' (??)

 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] can I walk or glob a website?

2011-05-18 Thread Albert-Jan Roskam




From: Dave Angel 
To: Alan Gauld 
Cc: tutor@python.org
Sent: Wed, May 18, 2011 11:51:35 AM
Subject: Re: [Tutor] can I walk or glob a website?

On 01/-10/-28163 02:59 PM, Alan Gauld wrote:
> 
> "Albert-Jan Roskam"  wrote
>> How can I walk (as in os.walk) or glob a website?
> 
> I don't think there is a way to do that via the web.
> Of course if you have access to the web servers filesystem you can use
> os.walk to do it as for any other filesystem, but I don't think its
> generally possible over http. (And indeed it shouldn''t be for very good
> security reasons!)
> 
> OTOH I've been wrong before! :-)
> 

It has to be (more or less) possible.  That's what google does for their search 
engine.

Three broad issues.

1) Are you violating the terms of service of such a web site?  Are you going to 
be doing this seldom enough that the bandwidth used won't be a DOS attack?  Are 
there copyrights to the material you plan to download?  Is the website 
protected 
by a login, by cookies, or a VPN?  Does the website present a different view to 
different browsers, different OS's, or different target domains?

===> This crossed my mind too. The advantage of using Python is that it's fun 
and that it saves me from gettng a mouse arm. It is a Dutch government site 
with 
pdf quality control reports (from the municipal health service) of 
kindergartens. I thought it would be fun and useful to make graphic 
representations of (spider charts) of each kindergarten, so they can be easily 
compared. This is just a hobby project. It just bothers me that they're not 
very 
easy to compare.

2) Websites vary enormously in their adherence to standards.  There are many 
such standards, and browsers tend to be very tolerant of bugs in the site which 
will be painful for you to accomodate.  And some of the extensions/features are 
very hard to parse, such as flash.  Others, such as javascript, can make it 
hard 
to do it statically.

===> I checked some of the deep links already. They are of the form  
[/\\\.a-z]+docid[0-9]+resultid[0-9]+ (roughly speaking), e.g.
http://www.landelijkregisterkinderopvang.nl/pp/inzien/Oko/InspectieRapport.jsf?documentId=5547&selectedResultId=5548

I could use a brute force approach and try all the doc/result id combinations. 
But wouldn't that result in a high server load? If so, I could put the program 
to sleep for n seconds.

3) How important is it to do it reliably?  Your code may work perfectly with a 
particular website, and next week they'll make a change which breaks your code 
entirely.  Are you willing to rework the code each time that happens?

===> It should be reliable. Portability to other sites is, of course, cool, but 
not strictly necessary.

Many sites have API's that you can use to access them.  Sometimes this is a 
better answer.

With all of that said, I'll point you to Beautiful Soup, as a library that'll 
parse a page of moderately correct html and give you the elements of it.  If 
it's a static page, you can then walk the elements of the tree that Beautiful 
Soup gives you, and find all the content that interests you.  You can also find 
all the web pages that the first one refers to, and recurse on that.

Notice that you need to limit your scope, since many websites have direct and 
indirect links to most of the web. For example, you might only recurse into 
links that refer to the same domain.  For many websites, that means you won't 
get it all.  So you may want to supply a list of domains and/or subdomains that 
you're willing to recurse into.

See  http://pypi.python.org/pypi/BeautifulSoup/3.2.0

===> Thanks, I'll check BS.

DaveA

Best wishes, 
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


Re: [Tutor] can I walk or glob a website?

2011-05-18 Thread Albert-Jan Roskam
Hi Steven,


From: Steven D'Aprano 

To: tutor@python.org
Sent: Wed, May 18, 2011 1:13:17 PM
Subject: Re: [Tutor] can I walk or glob a website?

On Wed, 18 May 2011 07:06:07 pm Albert-Jan Roskam wrote:
> Hello,
>
> How can I walk (as in os.walk) or glob a website? 

If you're on Linux, use wget or curl.

===> Thanks for your reply. I tried wget, which seems to be a very handy tool. 
However, it doesn't work on this particular site. I tried wget -e robots=off -r 
-nc --no-parent -l6 -A.pdf 'http://www.landelijkregisterkinderopvang.nl/' (the 
quotes are there because I originally used a deeper link that contains 
ampersands). I also tested it on python.org, where it does work. Adding -e 
robots=off didn't work either. Do you think this could be a protection from the 
administrator?

If you're on Mac, you can probably install them using MacPorts.

If you're on Windows, you have my sympathies.

*wink*


> I want to download 
> all the pdfs from a website (using urllib.urlretrieve), 

This first part is essentially duplicating wget or curl. The basic 
algorithm is:

- download a web page
- analyze that page for links 
  (such  but possibly also others)
- decide whether you should follow each link and download that page
- repeat until there's nothing left to download, the website blocks 
  your IP address, or you've got everything you want

except wget and curl already do 90% of the work.

If the webpage requires Javascript to make things work, wget or curl 
can't help. I believe there is a Python library called Mechanize to 
help with that. For dealing with real-world HTML (also known 
as "broken" or "completely f***ed" HTML, please excuse the 
self-censorship), the library BeautifulSoup may be useful.

Before doing any mass downloading, please read this:

http://lethain.com/an-introduction-to-compassionate-screenscraping/



> extract 
> certain figures (using pypdf- is this flexible enough?) and make some
> statistics/graphs from those figures (using rpy and R). I forgot what
> the process of 'automatically downloading' is called again, something
> that sounds like 'whacking' (??)

Sometimes called screen or web scraping, recursive downloading, or 
copyright-infringement *wink*

http://en.wikipedia.org/wiki/Web_scraping



-- 
Steven D'Aprano
___
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] can I walk or glob a website?

2011-05-18 Thread Albert-Jan Roskam






From: Alan Gauld 
To: tutor@python.org
Sent: Wed, May 18, 2011 4:40:19 PM
Subject: Re: [Tutor] can I walk or glob a website?


"Dave Angel"  wrote

>> "Albert-Jan Roskam"  wrote
>>> How can I walk (as in os.walk) or glob a website?
>> 
>> I don't think there is a way to do that via the web.

> It has to be (more or less) possible.  That's what google does for their 
> search 
>engine.

Google trawls the site following links. If thats all he wants then its fairly 
easy.
I took it he wanted to actually trawl the server getting *all* the pdf files not
just the published pdfs...

Depends what the real requirement is.

===> No, I meant only the published ones. I would consider it somewhat 
dodgy/unethical/whatever-you-wanna-call-it to download unpublished stuff. 
Indeed 
I only need published data.


Alan G. 

___
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] can I walk or glob a website?

2011-05-19 Thread Albert-Jan Roskam
Thank you, always useful to study other people's code. I wasn't planning to 
create a Gui for my app. It struck me that the Gui class also contains all the 
methods that deal with the html parsing. But maybe that's what your warnings 
were about. ;-)
 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: Marc Tompkins 
To: tutor@python.org
Sent: Wed, May 18, 2011 9:10:06 PM
Subject: Re: [Tutor] can I walk or glob a website?


On Wed, May 18, 2011 at 11:21 AM, Marc Tompkins  wrote:

On Wed, May 18, 2011 at 11:04 AM, Prasad, Ramit  
wrote:
>
>> It's horribly crude, in retrospect, and I'm embarrassed re-reading my code - 
>>but if you're interested I can forward it (if only as an example of what 
>>_not_to 
>>do.)
>>I would be interested even if the OP is not ;)
>>
>OK then, but bear in mind that I was young and foolish then.  Of course, I'm 
>old 
>and foolish now...
>I just DID re-read it, and I'd just like to point out:
-  I wrote my script to work with the structure of one particular website, 
"audiobooks.ulitka.com".  As written, it probably wouldn't work with a generic 
site.
-  "audiobooks.ulitka.com" no longer exists, so even if you installed all the 
dependencies and got my code working, it wouldn't work.   The "ulitka.com" 
domain appears to have been sold to a completely different 
business/organization...
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Excited about python

2011-06-12 Thread Albert-Jan Roskam
My all-time favourite is Programming in Python 3 (Mark Summerfield) 
http://www.qtrac.eu/py3book.html

Most of it is not for absolute beginners. Some of the chapters contain stuff I 
still cannot wrap my brain around. I believe the chapter about regexes (which 
is 
VERY good) is freely downloadable.

 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: Chris Fuller 
To: tutor@python.org
Sent: Fri, June 10, 2011 7:12:11 PM
Subject: Re: [Tutor] Excited about python


For a handy reference, you can't beat "Python Essential  Reference" by David 
Beazley (along with the online documentation, of course!).  I think this book 
is obligatory if you are going to be working with Python a lot.  I own all 
four editions :)

But you wanted something more in depth with algorithms, etc.  The O'Reilly 
book "Programming Python" by Mark Lutz is a classic and is probably a good bet 
for you.  Core Python by Wesley Chun is also good, and I've seen him on this 
list from time to time.

Also, check out the Python wiki:
http://wiki.python.org/moin/PythonBooks

Cheers
___
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] Cython question

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

Some time ago I finished a sav reader for Spss .sav data files (also with the 
help of some of you!):
http://code.activestate.com/recipes/577650-python-reader-for-spss-sav-files/

It works fine, but it is not fast with big files. I am thinking of implementing 
two of the functions in cython (getValueChar and getValueNum).
As far as I understood it requires the functions to be re-written in a 
Python-like langauge, 'minus the memory manager'. That little piece of code is 
converted to C and subsequently compiled to a .dll or .so file. The original 
program listens and talks to that .dll file. A couple of questions:
-is this a correct representation of things?
-will the speed improvement be worthwhile? (pros)
-are there reasons not to try this? (cons)
-is it 'sane' to mix ctypes and cython for nonintensive and intensive 
operations, respectively?

Thanks in advance!

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] Cython question

2011-07-02 Thread Albert-Jan Roskam
Hi Stefan, Alan, Matt,

Thanks for your replies. 

I used cProfile to find the bottlenecks, the two Python functions getValueChar 
and getValueNum. These two Python functions simply call two equivalent C 
functions in a .dll (using ctypes). The problem is that these functions are 
called as many times as there are VALUES in a file (e.g. 1000 records * 100 
columns = 10 function calls). So if I understand you correctly, this is not 
Cpu bound and, therefore, alas, Cython won't improve the excution time. Correct?

That .dll contains many more functions, for example to extract certain header 
information (a list of all the spss variables, etc.). Getting this kind of 
information is only done once per spss file. So, to answer your question, 
Stefan, I'd like this part of the code to remain the same, ie. with ctypes. 
Nothing much to win anyway, with just one function call per data file.

Cython might be useful when the program is converting spss date/times (seconds 
since gregorian epoch) to iso-date/times. If I understand it correctly, this is 
certainly cpu bound.

Btw, Matt, I indeed used psyco already, although I never precisely quantified 
the improvement in speed.
 
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?

~~

--- On Sat, 7/2/11, Stefan Behnel  wrote:

From: Stefan Behnel 
Subject: Re: [Tutor] Cython question
To: tutor@python.org
Date: Saturday, July 2, 2011, 1:29 PM

Albert-Jan Roskam, 02.07.2011 11:49:
> Some time ago I finished a sav reader for Spss .sav data files (also with the
> help of some of you!):
> http://code.activestate.com/recipes/577650-python-reader-for-spss-sav-files/
> 
> It works fine, but it is not fast with big files. I am thinking of 
> implementing
> two of the functions in cython (getValueChar and getValueNum).
> As far as I understood it requires the functions to be re-written in a
> Python-like langauge

"rewritten" only in the sense that you may want to apply optimisations or 
provide type hints. Cython is Python, but with language extensions that allow 
the compiler to apply static optimisations to your code.


> , 'minus the memory manager'.

Erm, not sure what you mean here. Cython uses the same memory management as 
CPython.


> That little piece of code is
> converted to C and subsequently compiled to a .dll or .so file. The original
> program listens and talks to that .dll file. A couple of questions:
> -is this a correct representation of things?

More or less. Instead of "listens and talks", I'd rather say "uses". What you 
get is just another Python extension module which you can use like any other 
Python module.


> -will the speed improvement be worthwhile? (pros)

Depends. If your code is I/O bound, then likely not. If the above two functions 
are true CPU bottlenecks that do some kind of calculation or data 
transformation, it's likely going to be faster in Cython.


> -are there reasons not to try this? (cons)

If your performance problem is not CPU related, it may not be worth it.


> -is it 'sane' to mix ctypes and cython for nonintensive and intensive
> operations, respectively?

Why would you want to use ctypes if you can use Cython?

Stefan

___
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] Cython question

2011-07-03 Thread Albert-Jan Roskam
Hi Stefan, Alan,

Thanks for your useful advice. The first thing I will try is take the call to 
the spssio dll out of the Python method (ie, 'unwrap' it) and put it inside the 
loop. I didn't think wrapping the code inside a method/function would create so 
much overhead.

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?

~~

--- On Sat, 7/2/11, Stefan Behnel  wrote:

From: Stefan Behnel 
Subject: Re: [Tutor] Cython question
To: tutor@python.org
Date: Saturday, July 2, 2011, 4:52 PM

Alan Gauld, 02.07.2011 15:28:
> "Albert-Jan Roskam" wrote
>> I used cProfile to find the bottlenecks, the two Python functions
>> getValueChar and getValueNum. These two Python functions simply call two
>> equivalent C functions in a .dll (using ctypes).

The code is currently declared as Windows-only and I don't know any good 
C-level profiling code for that platform. Under Linux, once I'm sure I have a 
CPU bound problem below the Python level, I'd use valgrind and KCacheGrind to 
analyse the performance. That will include all C function calls (and even CPU 
instructions, if you want) in the call trace. Makes it a bit less obvious to 
see what Python is doing, but leads to much more detailed results at the C 
level.

It's also worth keeping in mind that all profiling attempts *always* interfere 
with the normal program execution. The results you get during a profiling run 
may not be what you'd get with profiling disabled. So, profiling is nice, but 
it doesn't replace proper benchmarking.


> In that case cythin will speed up the calling loops but it can't do
> anything to speed up the DLL calls, you have effectively already optimised
> those functions by calling the DLL.
> 
>> The problem is that these functions are called as many times as there are
>> VALUES in a file
> 
> It might be worth a try if you have very big data sets
> because a C loop is faster than a Python loop. But don't expect order of
> magnitude improvements.

Looking at the code now, it's actually worse than that. The C function call 
does not only go through ctypes, but is additionally wrapped in a method call. 
So the OP is paying the call overhead twice for each field, plus the method 
lookup and some other operations. These things can add up quite easily.

So, iff the conversion code is really a CPU bottleneck, and depending on how 
much work the C functions actually do, the current call overhead, 100 times per 
record, may be a substantial part of the game. It's worth seeing if it can be 
dropped at the Python level by removing method lookup and call levels (i.e. by 
inlining the method), but if that's not enough, Cython may still be worth it. 
For one, Cython's call overhead is lower than that of ctypes, and if the call 
is only done once, and the loop is moved into Cython (i.e. C) entirely, the 
overhead will also drop substantially.

It might also be worth running the code in PyPy instead of CPython. PyPy will 
optimise a lot of the overhead away that this code contains.


>> So if I understand you correctly, this is not Cpu bound

I don't have enough information to comment on that.


> It may still be CPU bound in that the CPU is doing all the work, but if
> the CPU time is in the DLL functions rather than in the loop cython
> won't help much.
> 
> CPU bound refers to the type of processing - is it lots of logic, math,
> control flows etc? Or is it I/O bound - reading network, disk, or user
> input? Or it might be memory bound - creating lots of in memory objects
> (especially if that results in paging to disk, when it becomes I/O
> bound  too!)
> 
> Knowing what is causing the bottleneck will determine how to improve
> things. Use tools like TaskManager in Windows or top in *nix to see
> where the time is going and what resources are being consumed. Fast code
> is not always the answer.

That is very good advice. As a rule of thumb, a process monitor like top will 
tell you how much time is spent in I/O and CPU. If, during a test run (with 
profiling disabled, as that eats time, too!), your CPU usage stays close to 
100%, your program is CPU bound. If, however, it stays lower, and the monitor 
reports a high I/O waiting time, it's I/O bound. In this case, I/O bound is 
what you want to achieve, because it means that your code is running faster 
than your hard drive can deliver the data.

Stefan

___
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] Algorithm for sequence matching

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

Are you looking for a Longest Common Subsequence (LCS) algorithm?
http://code.activestate.com/recipes/576869-longest-common-subsequence-problem-solver/

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?

~~

--- On Sun, 7/3/11, Christopher King  wrote:

From: Christopher King 
Subject: Re: [Tutor] Algorithm for sequence matching
To: "Walter Prins" 
Cc: tutor@python.org
Date: Sunday, July 3, 2011, 5:48 PM

I know a way to do thatset1 = set(list1)set2 = set(list2)combined = set1&set2

On Sat, Jul 2, 2011 at 5:16 PM, Walter Prins  wrote:

Hi Ankur,

On 2 July 2011 21:30, ANKUR AGGARWAL  wrote:


HeyI am looking for an algo for the largest sequence search in the two list.
Example : list a accepts some say 'm' numbers. list b accept says 'n' numbers. 
I want to look for the largest same sequence between the two list and then 
display it. I tried out but failed to do so. 



Say A=[11,23,45,21,63,56,78,32]B=[56,78,11,23,45,21,111,234,56543]
There are two  similar sequence matching over here [11,23] and [23,45,21] i 
want to display second sequence because its larger in number. Plz help



Thanks in Advance :)
Umm, what about [11,23,45,21]?  That seems to be longer still so should be the 
best one to display, or?

Walter 



___

Tutor maillist  -  Tutor@python.org

To unsubscribe or change subscription options:

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





-Inline Attachment Follows-

___
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] what is 'doubleword alignment'?

2011-07-16 Thread Albert-Jan Roskam
Hello,

What is 'doubleword alignment'? It is used in the following sentence: "Fill up 
the buffer with the correctly encoded numeric and string values, taking care of 
blank padding and doubleword alignment." 

I know that the buffer is comprised of variables of 8-bytes, or multiples 
thereof, each. Numeric variables are 8 bytes, char vars are at least 8 bytes. 
For example, a 10-byte value is 'ceiled' to 18 bytes. This is done with padding 
(spaces, I think). But the aligment part...?

TIA

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-16 Thread Albert-Jan Roskam
Got it already, I think. The word boundary of one chunk of information (in my 
case 8 bytes) is aligned in the computer's memory such that the boundary's 
address is a power of two.

But correct me if I'm wrong ;-)

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?

~~

--- On Sat, 7/16/11, Albert-Jan Roskam  wrote:

From: Albert-Jan Roskam 
Subject: [Tutor] what is 'doubleword alignment'?
To: "Python Mailing List" 
Date: Saturday, July 16, 2011, 8:23 PM

Hello,

What is 'doubleword alignment'? It is used in the following sentence: "Fill up 
the buffer with the correctly encoded numeric and string values, taking care of 
blank padding and doubleword alignment." 

I know that the buffer is comprised of variables of 8-bytes, or multiples 
thereof, each. Numeric variables are 8 bytes, char vars are at least 8 bytes. 
For example, a 10-byte value is 'ceiled' to 18 bytes. This is done with padding 
(spaces, I think). But the aligment part...?

TIA

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?

~~
-Inline Attachment Follows-

___
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] 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  wrote:

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

On Sun, Jul 17, 2011 at 9:15 PM, Walter Prins  wrote:
>
>
> On 17 July 2011 15:26, Lisi  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


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

2011-07-21 Thread Albert-Jan Roskam
Thanks a lot for your explanations, that was most helpful! I never realized my 
mother tongue (Dutch) is Little Endian, whereas English is Big Endian, e.g.:
dutch: negen-en-twintig (nine-and-twenty)
english: twenty-nine

I will improve my program based on what you all have said. I will let the 
program:
-find out the encoding of the input file
-find out the encoding of the terminal
-if necessary: find out if there are any other, compatible encodings on the 
terminal

Thanks again!

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?

~~

--- On Thu, 7/21/11, Steven D'Aprano  wrote:

From: Steven D'Aprano 
Subject: Re: [Tutor] little/big endian was Re: what is 'doubleword alignment'?
To: tutor@python.org
Date: Thursday, July 21, 2011, 2:56 PM

Dave Angel wrote:

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

There used to be mainframes with various forms of middle-endian layouts. 
Fortunately they are no longer around.

http://www.retrologic.com/jargon/M/middle-endian.html


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

In this context, least significant and most significant may need explaining.

In decimal numbers, we write one hundred and twenty-three as 123. The 1 is most 
significant, because it represents 1 HUNDRED rather than 1 UNIT. And similarly 
the 3 is least significant. So numbers using Arabic numerals are big-endian.

The same applies for computer integers. 123 written in hexadecimal is 7B, which 
of course is big-endian just like decimal. But when storing this number in 
memory, we have a choice: we can store it in big-endian format, just like we 
write it: 7B, where the 7 is the "left-most" (lowest address) number. Or we can 
store it in little-endian format, B7, where the 7 has the higher address, and 
read from right-to-left.

And of course, bytes themselves can be either little-endian or big-endian, 
*independently* of byte ordering within larger units.

http://en.wikipedia.org/wiki/Endianness
http://en.wikipedia.org/wiki/Bit_numbering



-- 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] Strange zip syntax

2011-09-16 Thread Albert-Jan Roskam
Hi,
 
I would write the slightly longer, but more readable (for me at least) code:
>>> a = ['a','1','b','2','c','3']
>>> [(a[i], a[i+1]) for i in range(0, len(a), 2)]
[('a', '1'), ('b', '2'), ('c', '3')]
>>> zip(*[iter(a)]*2)
[('a', '1'), ('b', '2'), ('c', '3')]
 
And it's also faster:
 
>>> import timeit
>>> t = timeit.Timer("zip(*[iter(['a', '1', 'b', '2', 'c', '3'])]*2)")
>>> t.timeit()
5.1758860413823555
>>> t = timeit.Timer("a=['a', '1', 'b', '2', 'c', '3']; [(a[i], a[i+1]) for i 
>>> in range(0, len(a), 2)]")
>>> t.timeit()
2.5330216549868823
>>> 

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: Brett Ritter 
>To: *tutor python 
>Sent: Friday, September 16, 2011 4:35 AM
>Subject: [Tutor] Strange zip syntax
>
>I ran into this article (
>http://blog.adku.com/2011/09/hodgepodge-of-python.html ) and found
>myself temporarily stymied by one line it in:
>
>zip(*[iter(a)]*2)
>
>Used like this:
>
 a = ['a','1','b','2','c','3']
 zip(*[iter(a)]*2)
>[('a', '1'), ('b', '2'), ('c', '3')]
>
>While I'm unlikely to use such a construct (if I can't easily follow
>it now, I or my successor likely won't follow it should it need to be
>debugged sometime in the future), I found the education I got in
>deciphering it was worth the effort.  I'm sharing it here so others
>can benefit from my puzzlement.
>
>iter(a) returns a list iterator for a.  See help(iter) for more.
>[iter(a)] is a list containing one element, an iterator.  This is
>created only so we can do the below:
>[iter(a)]*2 is a list containing two elements, each the SAME list iterator.
>For simplicity, let's break this out for further analysis:
>
 b = iter(a)
 c = [b,b]
>
>*[iter(a)]*2 flattens the list when passed into a function call.
>Using our more verbose but simple syntax: *c.  This only works when
>passed to a function.
>zip() creates tuples each holding the Nth elements from a number of
>sequences.  See help(zip) for more.
>Thus, zip(a) or zip(a,a) would return:
 zip(a)
>[('a',), ('1',), ('b',), ('2',), ('c',), ('3',)]
 zip(a,a)
>[('a', 'a'), ('1', '1'), ('b', 'b'), ('2', '2'), ('c', 'c'), ('3', '3')]
>
>What happens when we pass an iterator to zip?  That's not mentioned in
>the docstring blurb.
 zip(iter(a))
>[('a',), ('1',), ('b',), ('2',), ('c',), ('3',)]
>Answer: It works as intended.
>
>Now we come to the magic of this little snippet.
>zip(iter(a),iter(a)) wouldn't work, because each call to iter(a)
>returns a DIFFERENT iterator.
 zip(iter(a), iter(a))
>[('a', 'a'), ('1', '1'), ('b', 'b'), ('2', '2'), ('c', 'c'), ('3', '3')]
>
>But by creating the list of two elements each of which is the SAME
>iterator, as each is asked to iterate it advances the common element
>indicator:
 zip(*c)
>[('a', '1'), ('b', '2'), ('c', '3')]
>Notice that the flattening is required, because zip needs to get
>multiple arguments:
 b = iter(a)  #our original iterator is spent, so we're assigning a new one
 c = [b,b]
 zip(c)  #Not flattened, is just a single list, like a.
>[(,), (0x024E32D0>,)]
 zip(b,b)  # here it is two iterators sent to zip() (though they happen to 
 be the SAME iterator)
>[('a', '1'), ('b', '2'), ('c', '3')]
>
>I hope some of you enjoy playing with this, and hopefully someone
>learned something useful!  While I'm not likely to use the listed
>form, I can very well see myself saying:
>
 a = ['a','1','b','2','c','3']  #well, I can see myself using this with 
 meaningful variable names
 b = iter(a)
 zip(b,b)  # Group in sets of 2 elements
>
>-- 
>Brett Ritter / SwiftOne
>swift...@swiftone.org
>___
>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] ctypes and arrays of pointers

2011-10-13 Thread Albert-Jan Roskam
Hi,

I have a question about ctypes. I am trying to call a C function but I am not 
able to construct the arguments in ctypes. I need to construct two pointers. 
Each is a pointer to an array of pointers to character values. I did it like 
this (I tried numerous ways; this seemed the cleanest way):
>>> MAXSIZE = 10
>>> valueArrayPtr = ctypes.POINTER(ctypes.c_char_p) * MAXSIZE
>>> valueArrayPtr

>>> valueArrayPtrPtr = ctypes.POINTER(valueArrayPtr)
>>> valueArrayPtrPtr

>>> 


But this yields an error: : Don't know how to 
convert parameter ..

Any idea what I am doing wrong? Below is more complete code.
And once it works, can I simply use the ctypes as an iterable to retrieve the 
values?


Thanks in advance!
Albert-Jan


import ctypes

def getValueLabels(self, varName):

    MAXSIZE = 10

    # Pointer to array of pointers to values
    valueArrayPtr = ctypes.POINTER(ctypes.c_char_p) * MAXSIZE
    valueArrayPtrPtr = ctypes.POINTER(valueArrayPtr)
    
    # Pointer to array of pointers to labels
    labelArrayPtr = ctypes.POINTER(ctypes.c_char_p) * MAXSIZE
    labelArrayPtrPtr = ctypes.POINTER(labelArrayPtr)

    # Pointer to number of values or labels
    numLabels = ctypes.c_int()
    numLabelsPtr = ctypes.byref(numLabels)
    
    # call C function with the following prototype:
    # int GetValueLabels(int handle, const char *varName, char ***values, char 
***labels, int *numLabels)
    retcode = self.theLib.GetValueLabels(self.fh, varName, valueArrayPtrPtr, 
labelArrayPtrPtr, numLabelsPtr)
    # yields ArgumentError: argument 3: : Don't 
know how to convert parameter 3


Description

This function gets the set of labeled values and associated labels for a short 
string
variable. The number of values is returned as *numLabels. Values are stored 
into an
array of *numLabels pointers, each pointing to a char string containing a 
nullterminated
value, and *values is set to point to the first element of the array. Each value
string is as long as the variable. The corresponding labels are structured as 
an array of
*numLabels pointers, each pointing to a char string containing a 
null-terminated label,
and *labels is set to point to the first element of the array. 
Parameter Description
handle Handle to the data file
varName Variable name
values Pointer to array of pointers to values
labels Pointer to array of pointers to labels
numLabels Pointer to number of values or labels


 
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] ctypes and arrays of pointers

2011-10-15 Thread Albert-Jan Roskam


 Hi,

Got it already. Here's some incomplete code that shows how it could be done:
# Pointer to array of pointers to labels 
labelsPtr = ctypes.pointer((ctypes.POINTER(ctypes.c_char_p) * 
MAX_ARRAY_SIZE)()) 
retcode = self.theLib.GetValueLabels(self.fh, varName, valuesPtr, labelsPtr, 
numLabelsPtr)
labels = [unicode(labelsPtr.contents[0][i], "utf-8") for i in 
range(numLabels.value)] 

Thought I might share this with you ;-)


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: Albert-Jan Roskam 
>To: Python Mailing List 
>Sent: Thursday, October 13, 2011 9:25 PM
>Subject: [Tutor] ctypes and arrays of pointers
>
>
>Hi,
>
>
>I have a question about ctypes. I am trying to call a C function but I am not 
>able to construct the arguments in ctypes. I need to construct two pointers. 
>Each is a pointer to an array of pointers to character values. I did it like 
>this (I tried numerous ways; this seemed the cleanest way):
>>>> MAXSIZE = 10
>>>> valueArrayPtr = ctypes.POINTER(ctypes.c_char_p) * MAXSIZE
>>>> valueArrayPtr
>
>>>> valueArrayPtrPtr = ctypes.POINTER(valueArrayPtr)
>>>> valueArrayPtrPtr
>
>>>> 
>
>
>
>But this yields an error: : Don't know how to 
>convert parameter ..
>
>
>Any idea what I am doing wrong? Below is more complete code.
>And once it works, can I simply use the ctypes as an iterable to retrieve the 
>values?
>
>
>
>Thanks in advance!
>Albert-Jan
>
>
>
>import ctypes
>
>def getValueLabels(self, varName):
>
>    MAXSIZE = 10
>
>    # Pointer to array of pointers to values
>    valueArrayPtr = ctypes.POINTER(ctypes.c_char_p) * MAXSIZE
>    valueArrayPtrPtr = ctypes.POINTER(valueArrayPtr)
>    
>    # Pointer to array of pointers to labels
>    labelArrayPtr = ctypes.POINTER(ctypes.c_char_p) * MAXSIZE
>    labelArrayPtrPtr =
 ctypes.POINTER(labelArrayPtr)
>
>    # Pointer to number of values or labels
>    numLabels = ctypes.c_int()
>    numLabelsPtr = ctypes.byref(numLabels)
>    
>    # call C function with the following prototype:
>    # int GetValueLabels(int handle, const char *varName, char ***values, char 
>***labels, int *numLabels)
>    retcode = self.theLib.GetValueLabels(self.fh, varName, valueArrayPtrPtr, 
>labelArrayPtrPtr, numLabelsPtr)
>    # yields ArgumentError: argument 3: : Don't 
>know how to convert parameter 3
>
>
>Description
>
>This function gets the set of labeled values and associated labels for a short 
>string
>variable. The number of values is returned as *numLabels. Values are stored 
>into an
>array of *numLabels pointers, each pointing to a char string containing a
 nullterminated
>value, and *values is set to point to the first element of the array. Each 
>value
>string is as long as the variable. The corresponding labels are structured as 
>an array of
>*numLabels pointers, each pointing to a char string containing a 
>null-terminated label,
>and *labels is set to point to the first element of the array. 
>Parameter Description
>handle Handle to the data file
>varName Variable name
>values Pointer to array of pointers to values
>labels Pointer to array of pointers to labels
>numLabels Pointer to number of values or labels
>
> 
>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
>
>
>___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


  1   2   3   4   5   6   >