Re: [Tutor] Key Error

2007-07-08 Thread Sara Johnson
 
  Probably best if I skip the example and show what code I do have:
  ~~~
  for key in h.keys():
wssd=h[key]['WSSD']
wspd=h[key]['WSPD']
wmax=h[key]['WMAX']
newi=h[key]['NEWI']
if wssd<-989. or wspd<-989. or wmax<-989.: break
if wspd==0.: break
~
  Where the "newi" = "abcd" that I showed before.  I have no where else in my 
code anything pertaining to these 4 keys.  The first 3 were there, and produce 
no errors.  I am making adjustments to an existing script.  I only have C 
programming knowledge so my thought was that it "newi" was just a variable that 
needed to be assigned.  You'll notice the parameters below (i.e., if wssd < 
-989 ) but there is obviously nothing for "newi" at the moment.  The program's 
only error at the moment seems to be this line:
   
   newi=h[key]['NEWI']

  But as you can see, the other items are set up the same way.
   
  Thanks bundles!!!  
  Sara

  Message: 7
Date: Sun, 08 Jul 2007 02:21:25 -0400
From: Brian van den Broek 

Subject: Re: [Tutor] Key Error
To: [EMAIL PROTECTED]
Cc: tutor@python.org
Message-ID: <[EMAIL PROTECTED]>
Content-Type: text/plain; charset=ISO-8859-1; format=flowed

Sara Johnson said unto the world upon 07/08/2007 01:34 AM:
> Sorry, this is probably too general a question, but I can't find
> any specific information on it. What exactly is a "key error" and
> how do I clear it?
> 
> I entered something like this:
> 
> abcd=h[key]['ABCD']
> 
> and when I run it I'm getting
> 
> KeyError: 'ABCD'
> 
> What does this mean?
> 
> Thanks!
> 


Hi Sara,

It means you've tried to access a data structure (most likely a
dictionary) with a key that does not exist in that structure. Witness

>>> my_dict={42:"Six times seven", 1: "The loneliest number"} 
>>> my_dict[42]
'Six times seven'
>>> my_dict['42']
Traceback (most recent call last):
File "", line 1, in 
KeyError: '42'
>>> my_dict[17]
Traceback (most recent call last):
File "", line 1, in 
KeyError: 17
>>> 

It isn't a question of `clearing' it, but of tracking down the wrong
assumption behind your code. It may be that you thought you were using
a key you'd added before and were wrong (my_dict['42'] as opposed to
my_dict[42] shows a common source of that).

But, from your

> abcd=h[key]['ABCD']

I'm guessing that you've got the key-access syntax a bit wrong. Did 
you mean

abcd = h['ABCD']

instead?

HTH,

Brian vdB

 
-
Don't get soaked.  Take a quick peak at the forecast 
 with theYahoo! Search weather shortcut.___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


[Tutor] object names

2007-07-08 Thread elis aeris

I need to create object with numbers,

for instance, i need to create 5 object of names like these

object_1
object_2
and so on,

how do I write a script that would do it when I specify that I need # number
of it?
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] object names

2007-07-08 Thread elis aeris

or it's not an object but common variables.


var_1
var_2
and so on
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Key Error

2007-07-08 Thread Bob Gailer
Sara Johnson wrote:
>  
> Probably best if I skip the example and show what code I do have:
> ~~~
> for key in h.keys():
> wssd=h[key]['WSSD']
> wspd=h[key]['WSPD']
> wmax=h[key]['WMAX']
> newi=h[key]['NEWI']
> if wssd<-989. or wspd<-989. or wmax<-989.: break
> if wspd==0.: break
> ~
> Where the "newi" = "abcd" that I showed before.  I have no where else 
> in my code anything pertaining to these 4 keys.  The first 3 were 
> there, and produce no errors.  I am making adjustments to an existing 
> script.  I only have C programming knowledge so my thought was that it 
> "newi" was just a variable that needed to be assigned.  You'll notice 
> the parameters below (i.e., if wssd < -989 ) but there is obviously 
> nothing for "newi" at the moment.  The program's only error at the 
> moment seems to be this line:
>  
>  newi=h[key]['NEWI']
> But as you can see, the other items are set up the same way.
Most likely h is a dictionary, and the values are also dictionaries. At 
least one of the value dictionaries has no key "NEWI".
To see what h is, put:
print h
before the for statement. You should see something like:
{'somekey': {'WSSD': 3, 'WSPD': 4, 'WMAX': 5, 'NEWI': 6}, 'anotherkey': ...}
Python uses braces with key : value pairs to represent a dictionary.
If that does not help you, post the result (or attach if it is really long).

Then we need to find how h itself was created. Any clues about that?

-- 
Bob Gailer
510-978-4454 Oakland, CA
919-636-4239 Chapel Hill, NC


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


Re: [Tutor] Key Error

2007-07-08 Thread Alan Gauld
"Sara Johnson" <[EMAIL PROTECTED]> wrote
>  Probably best if I skip the example and show what code I do have:
>  ~~~
>  for key in h.keys():
>wssd=h[key]['WSSD']
>wspd=h[key]['WSPD']
>wmax=h[key]['WMAX']
>newi=h[key]['NEWI']
>if wssd<-989. or wspd<-989. or wmax<-989.: break
>if wspd==0.: break
> ~
>  Where the "newi" = "abcd" that I showed before.

OK, Then that is telling us that h is a dictionary containing
further dictionaries inside. The error tells us that at least one
of the dictionaries does not have a key 'NEWI'

If this code is supposed to be production strength it is not of good
quality. It should either be defending these dictionary accesses
with a try/except block or it should be using the get() method
of the dictionary to force a default value. (The tests at the end
are poorly written too. If one of my team produced code like
this I'd be having strong words with them!)

Which is the best solution will depend on the situation...

> I have no where else in my code anything pertaining to
> these 4 keys.

Might I suggest grep? :-)

> The first 3 were there, and produce no errors.  I am making
> adjustments to an existing script.  I only have C programming
> knowledge

I hope you have at least basic Python too? Otherwise even
reading the code will be difficult. While Python is easy to read
thats a strictly relative measure!

> so my thought was that it "newi" was just a variable that
> needed to be assigned.

newi is indeed a "just a variable" that is being assigned a value,
but the value does not exist. This is a data error, your problems
lie in the initialisation of the dictionaries coupled to the 
vulnerability
of the code to this kind of error.

> You'll notice the parameters below (i.e., if wssd < -989 ) but
> there is obviously nothing for "newi" at the moment.

Indeed, but there may be further on. Its certainly not involved
in any of these tests.

> The program's only error at the moment seems to be this line:
>
>   newi=h[key]['NEWI']

The program has several errors IMHO but the one that is causing
the interpreter to complain is due to non existent data.
You need to find why that data is missing.
Or, you could find a valid default value and assign that, either
through a try/except block or by uysing a get() instead of
key access.

-- 
Alan Gauld
Author of the Learn to Program web site
http://www.freenetpages.co.uk/hp/alan.gauld 


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


Re: [Tutor] object names

2007-07-08 Thread Alan Gauld
"elis aeris" <[EMAIL PROTECTED]> wrote 

> for instance, i need to create 5 object of names like these
> 
> object_1
> object_2
> and so on,

It's very unlikely that you need to do this.
The usual solution in cases like this is to store the objects 
in a collection object, either a list or a dictionary. You can 
then access them by index or name.

For example:

# create some objects with a list
objects = []
for n in range(5):
objects.append(n)

# now fetch number 3
print objects[3]

# fetch them all in turn
for object in objects:
print object

# repeat above with a dictionary
d = {}
for n in range(5):
name = 'object_' + str(n)
d[name] = n

# fetch object_3
print d['object_3']

# fetch all
for object in d.keys()
print d[object]

If that solution won't work for you for some reason tell us 
why and we can provide ways to do what you need. But 
using a collection will work for the vast majority of cases.

HTH,

-- 
Alan Gauld
Author of the Learn to Program web site
http://www.freenetpages.co.uk/hp/alan.gauld

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


[Tutor] backslashes

2007-07-08 Thread max .

hello i am writing a simple password tester that uses a word list and am
running into some problems when i read the words from a text file they are
written to the screen with a backslash at the end and i cant seem to find a
way to get rid of them

here is the script:

__name__="dictionary password tester"
__author__="max baseman ([EMAIL PROTECTED])"
__version__="0.1"
__discription__="tests a password against a dictonary"
#

print
print
password=raw_input("password >")
passlist=open("/Users/max/passlist.rtf","r") # passlist is just a list of
50,000 words
guesses=0
for line in passlist:
   line=line.replace('\ ','') #heres what i was trying now
   guesses=guesses+1
   line=line.lower()
   print line
   if line==password:
   print
   print
   print"you password was",line,"and it took",guesses,"guesses"
   break





p.s i am about to look up how to type to python and not show the text for
the password bit but if anyone knows please explain
any help would be great thanks
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Key Error

2007-07-08 Thread Bob Gailer
Sara Johnson wrote:
> Okay, it is VERY long!  So long in fact that I can't get to the top of 
> it to copy from where it begins.  Basically it's a series of codes 
> like 'WSSD' followed by values like 0.2 (more than just 
> the ones I listed, perhaps a few hundred.  The end gives the same error.
>  
> Traceback (most recent call last):
>   File "./mymods.py", line 118, in ?
> newi=h[key]['NEWI']
> KeyError: 'NEWI'
>
> I can still attach what the screen has, if that'll help.
Let's take a different approach:

print len(h)
for key in h.keys():
try:
newi=h[key]['NEWI']
except KeyError:
   print key

That will tell us how many items are in h and whether we have few or 
many of them that have the KeyError.
-- 
Bob Gailer
510-978-4454 Oakland, CA
919-636-4239 Chapel Hill, NC


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


Re: [Tutor] object names

2007-07-08 Thread elis aeris

ugh,  i guess what I need is just to name variables, not objects.


var_1
var_2

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


Re: [Tutor] Key Error

2007-07-08 Thread Sara Johnson
  I appologize...but what is, 'grep'?  I'm at the end of my rope, which right 
now looks about as sturdy as string (and I don't mean a string buffer either)!!!
   
  Okay, where to go from here...  Again, as I mentioned there may be holes in 
what I'm giving but that's because 1). segments of this program do work, and I 
don't want to overload this list with my Python problems (and believe me, I 
have enough to probably keep you kind, knowledgable folks busy for weeks!) and 
2). I don't always know if I'm giving you all enough information to decipher my 
mess, or too much information.  
   
  I believe 'NEWI' is supposed to be a new subkey that will hold a value called 
'newind.'  But if I can't get the program to initialize this 'NEWI' then I 
don't know how any values can come from it. 
   
  Thanks anyways...  I'll keep digging,
   
  Sara

"Sara Johnson" wrote
> Probably best if I skip the example and show what code I do have:
> ~~~
> for key in h.keys():
> wssd=h[key]['WSSD']
> wspd=h[key]['WSPD']
> wmax=h[key]['WMAX']
> newi=h[key]['NEWI']
> if wssd<-989. or wspd<-989. or wmax<-989.: break
> if wspd==0.: break
> ~
> Where the "newi" = "abcd" that I showed before.

OK, Then that is telling us that h is a dictionary containing
further dictionaries inside. The error tells us that at least one
of the dictionaries does not have a key 'NEWI'

If this code is supposed to be production strength it is not of good
quality. It should either be defending these dictionary accesses
with a try/except block or it should be using the get() method
of the dictionary to force a default value. (The tests at the end
are poorly written too. If one of my team produced code like
this I'd be having strong words with them!)
   
  
Which is the best solution will depend on the situation...

> I have no where else in my code anything pertaining to
> these 4 keys.

Might I suggest grep? :-)

> The first 3 were there, and produce no errors. I am making
> adjustments to an existing script. I only have C programming
> knowledge

I hope you have at least basic Python too? Otherwise even
reading the code will be difficult. While Python is easy to read
thats a strictly relative measure!

> so my thought was that it "newi" was just a variable that
> needed to be assigned.

newi is indeed a "just a variable" that is being assigned a value,
but the value does not exist. This is a data error, your problems
lie in the initialisation of the dictionaries coupled to the 
vulnerability
of the code to this kind of error.

> You'll notice the parameters below (i.e., if wssd < -989 ) but
> there is obviously nothing for "newi" at the moment.

Indeed, but there may be further on. Its certainly not involved
in any of these tests.

> The program's only error at the moment seems to be this line:
>
> newi=h[key]['NEWI']

The program has several errors IMHO but the one that is causing
the interpreter to complain is due to non existent data.
You need to find why that data is missing.
Or, you could find a valid default value and assign that, either
through a try/except block or by uysing a get() instead of
key access.

-- 
Alan Gauld
Author of the Learn to Program web site
http://www.freenetpages.co.uk/hp/alan.gauld 


   
-
Get the Yahoo! toolbar and be alerted to new email wherever you're surfing. ___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] backslashes

2007-07-08 Thread Luke Paireepinart
max . wrote:
> hello i am writing a simple password tester that uses a word list and 
> am running into some problems when i read the words from a text file 
> they are written to the screen with a backslash at the end and i cant 
> seem to find a way to get rid of them
>
> here is the script:
>
> __name__="dictionary password tester"
> __author__="max baseman ([EMAIL PROTECTED] )"
> __version__="0.1"
> __discription__="tests a password against a dictonary"
> #
>
> print
> print
> password=raw_input("password >")
> passlist=open("/Users/max/passlist.rtf","r") # passlist is just a list 
> of 50,000 words
This is almost definitely the problem.
.rtf is 'rich text formatting' which, depending on which program wrote 
it (word pad / Microsoft Word / etc)
contains various degrees of markup / formatting mixed in with the text.
If you want straight text, don't try to process the rtf and remove the 
formatting.
Just save your file as a .txt (or anything else - just so long as the 
program that's actually saving it knows
to save it as straight ascii without any markup.)
> guesses=0
> for line in passlist:
> line=line.replace('\ ','') #heres what i was trying now
> guesses=guesses+1
> line=line.lower()
> print line
> if line==password:
> print
> print
> print"you password was",line,"and it took",guesses,"guesses"
> break
>
>
>  
>
>
> p.s i am about to look up how to type to python and not show the text 
> for the password bit but if anyone knows please explain
> any help would be great thanks
try this link: http://docs.python.org/lib/module-getpass.html

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


Re: [Tutor] backslashes

2007-07-08 Thread Dave Kuhlman
On Sun, Jul 08, 2007 at 10:54:51AM -0600, max . wrote:
> hello i am writing a simple password tester that uses a word list and am
> running into some problems when i read the words from a text file they are
> written to the screen with a backslash at the end and i cant seem to find a
> way to get rid of them
> 

In Python, the backslash is a character escape character.  In order
to include a backslash in a string, use a double backslash.  Here
is some example code::

In [1]: s1 = 'abcd\\efg\\'
In [2]: s1
Out[2]: 'abcd\\efg\\'
In [3]: len(s1)
Out[3]: 9
In [4]: s1.replace('\\', '')
Out[4]: 'abcdefg'

Notice the length of the string (before removing the backslashes). 
Each apparently double backslash actually has a length of 1.

Dave


-- 
Dave Kuhlman
http://www.rexx.com/~dkuhlman
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] object names

2007-07-08 Thread Luke Paireepinart
elis aeris wrote:
> ugh,  i guess what I need is just to name variables, not objects.
>
>
> var_1
> var_2
>
> and so on.
Did you read what Alan said?
He gave you a way to do this without using separate variables.
There are many problems associated with creating variable names dynamically,
and you haven't given us a reason why you need to do this.
We're not withholding information from you to be egregious,
we're doing it for your own good.
You most likely don't need to do this, and we gave you a perfectly 
viable alternative
in 99% of cases.
Hope you read this,
-Luke
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Key Error

2007-07-08 Thread Bob Gailer
Sara Johnson wrote:
> Should I temporarily comment out the other values (i.e., WSSD, 
> WSPD...)?  I'm getting an error now that says:
>  
>   File "./mymods.py", line 122
> if wssd<-989. or wspd<-989. or wmax<-989.: break
> ^
> SyntaxError: invalid syntax
Please post (always) the surrounding code. The error is likely due to 
whatever is on the preceding line.

Here's your original code:

for key in h.keys():
wssd=h[key]['WSSD']
wspd=h[key]['WSPD']
wmax=h[key]['WMAX']
newi=h[key]['NEWI']
if wssd<-989. or wspd<-989. or wmax<-989.: break
if wspd==0.: break

I was suggesting you replace all of that with:

print len(h)
for key in h.keys():
try:
newi=h[key]['NEWI']
except KeyError:
print key

Keep in mind that Python uses indentation to convey structure instead of braces 
(like C).


-- 
Bob Gailer
510-978-4454 Oakland, CA
919-636-4239 Chapel Hill, NC


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


Re: [Tutor] Key Error

2007-07-08 Thread Bob Gailer
Sara Johnson wrote:
> On how the 'h' was created
>  
> Well, it appears 'h' is referenced in a few spots in this code.  It 
> looks like initially, at least, a dictionary 'h' is created and some h 
> keys and sub keys follow, obviously from the purpose of this project 
> (having to do with pickling), h is assigned:
>  
> h=cPickle.load(inf)
>  
> Is this because of the sub keys that follow? 
This means that some (same or other) program created h, "pickled" it and 
most likely wrote it to a file. So that program is failing in some way 
to create the correct keys for the dictionaries that comprise the values 
of h.
> Could this be why I'm not able to create 'NEWI'?
Again note you are not trying to create anything.  newi=h[key]['NEWI'] 
is attempting to reference an existing key, and the key does not exist.
>  
> Sorry if I'm missing something or not answering your question, I'm 
> highly confused!
Of course you are. Going into a totally new realm with just C under your 
belt. If you'd like, give me a call. We might be able to cover more 
ground faster on the phone.

-- 
Bob Gailer
510-978-4454 Oakland, CA
919-636-4239 Chapel Hill, NC


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


[Tutor] launching mayavi2 via SPE?

2007-07-08 Thread pierre cutellic

hi, i just started to work with scipy and i would like to know the basic
example code to launch or script mayavi2

because i can't get anything with the documentation given on the
scipy.orgsite :(


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


Re: [Tutor] how long?

2007-07-08 Thread Tiger12506
> Thorsten Kampe wrote:
>> * Ben Waldin (Tue, 3 Jul 2007 19:46:42 +1200)
>>> How long will it take until I successfully create my own working program 
>>> that is useful? I have crated the address book ones in the tutors and 
>>> just want to know how long it takes before I start to create my own 
>>> thought up programs that will be useful. Thanks Ben
>>
>> Approximately ten days, four hours and six minutes
>>
>> ___
>> Tutor maillist  -  Tutor@python.org
>> http://mail.python.org/mailman/listinfo/tutor

Hmmm... I'd say it depends on whether or not you can... 'create my own 
thought up programs', much more than how long it takes.

Hey! I have an idea! Why don't you write a python program that can calculate 
just how long it takes for you to do that? I'd guess that's what Thorsten 
Kampe has done. ;-)

Factors involved in the algorithm:
1) How quickly you develop creative ideas (women usually do this better - My 
mother walks into a craft store, looks at an ugly little doll and exclaims, 
"Oooh! I know just what to do with that!")

2) How "useful" those ideas are - i.e. How many drawings you have compared 
to started projects

3) How motivated you are to finish them - ex. how crowded your workbench is 
compared to how many projects you have finished.

4) How effectively you translate thoughts into words, that is, how well you 
can articulate the language you are working in. (Be it English, Spanish, 
French, German, Python, C/C++, etc.)

5) Hmmm... I can't think of a "Five". That means I must be lacking a little 
in #1. ;-)

Let's see... Once you've accumulated a bunch of data about yourself, your 
habits, your hobbies, the 4 factors i listed, etc, etc, etc... Then all you 
have to do is do a weighted calculation averaging these together... Hmmm... 
you'll need a test case too, so you will have to accumulate data on other 
people to see how long it takes them (based on their abilities because 
different people have different capacities for learning). Oh. And you will 
have to determine just what useful means to you. Collect a lot of data on 
that, being sure that you compare what you think is useful against what 
other people think is useful. Average it all up and come up with some sort 
of result. Just always keep in mind what you're aiming for, your goal (which 
in this case is to find how long it takes to write useful programs).

LOL. Do you really think that anyone on this list can tell you how long it 
will take? Only *you* know how long it will take *you* to write what *you* 
think is a useful program.

Jacob S. 

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


Re: [Tutor] How can I escape a pound symbol in my script?

2007-07-08 Thread Tiger12506
> ron wrote:
>> in the US, # is a symbol for weight, not currency.
>
> I didn't know that; I assumed it was only
> used for ordinal numbering (as in Item #3).
>
> # How do you write out, with a quick symbol, "I'm going to
>> buy 3# of potatoes?

This - #3 - means Number 3
This - 3# - means 3 pounds in weight.

This is the true reason why (most) Americans call it a pound sign. Back in 
the days of typewriters when the key change may have been a reason, most 
Americans did not type. (That was for secretary women ;-)

Sometimes you will see the "pound sign" on old, old recipes, but it is not 
used anymore. Only a novelty now.

In fact, most English teachers in America are young enough now that using 
the # sign for pound will generate a syntax error. (that usage has been 
deprecated) They've all upgraded to the new version of English.

English v1900.1001.2.2 or something like that. ;-)

> Assuming that "you" is us Brits, then:
>
>   3lb

That is the official way in America also. But technically (as I learned in 
some Physics courses) it's supposed to be succeeded by '.' whereas metric 
units are NOT. Picky, picky people, aren't they? sigh.

Jacob S. 

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


Re: [Tutor] Key Error

2007-07-08 Thread Alan Gauld
"Sara Johnson" <[EMAIL PROTECTED]> wrote 

>  I apologize...but what is, 'grep'?  

The General Regular Expression Parser - at least that's one 
of the explanations. (Another is that it is the ed search 
command, and there are at least two more, it's part of the 
Unix mythology, debated for at least 30 years! :-)

More pragmatically it is the standard Unix tool for searching 
text files for strings. Its available on other OS too thanks to GNU.

Thus grep "NEWI" *.py

will find all the occurences of NEWI in your python files.
(grep -f will list only the files containing it.) If you use emacs 
or vi as your editor you can get the editor to step through the 
results in the same way as you step through the compilation 
errors after a make...

You can search for sophisticated regex patterns and 
include or exclude patterns etc. grep should be a basic 
tool of any programmer regardless of language used or OS.

> But if I can't get the program to initialize this 'NEWI' then 
> I don't know how any values can come from it. 

Thats the critical factor here. You need to initialise the 
dictionary in the first place. I notice from another post tat its 
being loaded from a pickle file. Try looking to see where it 
gets "dumped" to the file, that might help (grep again!)

If the dump only happens when the program closes down 
then maybe just writing some default values on the first 
load of the program will do - thats where the get() method 
comes in again...

It looks suspiciuously to me as if this NEWI key is some 
extra feature thats been added to the code and the initialisation 
code has not been updated to cope. Thats a common error 
especially in programs where the initialisation is in another 
program that only got run once a long time ago!

HTH,

-- 
Alan Gauld
Author of the Learn to Program web site
http://www.freenetpages.co.uk/hp/alan.gauld

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


Re: [Tutor] help with translating a c function to a python function

2007-07-08 Thread Tiger12506

>> i have a c function from some modbus documentation that i need to
>> translate into python.

>> unsigned short CRC16(puchMsg, usDataLen)
>> unsigned char *puchMsg ;
>> unsigned short usDataLen ;
>> {
>> unsigned char uchCRCHi = 0xFF ;
>> unsigned char uchCRCLo = 0xFF ;
>> unsigned uIndex ;
>> while (usDataLen––)
>> {
>> uIndex = uchCRCHi ^ *puchMsgg++ ;
>> uchCRCHi = uchCRCLo ^ auchCRCHi[uIndex} ;
>> uchCRCLo = auchCRCLo[uIndex] ;
>> }
>> return (uchCRCHi << 8 | uchCRCLo) ;
>> }

I found this link which may provide some insight into what's going on here.
(google "modbus CRC16")

http://www.modbustools.com/modbus_crc16.htm

This proves to me that auchCRCHi is a lookup table that you do not have 
access to. Happily :-) that link provides the table.

Hmmm... let's see, the difficult C stuff... *puchMsgg++ means to return the 
current character in a string, and then increment the pointer, so that when 
the C code encounters *puchMsgg++ again it reads the next character, 
increments, etc. You can emulate this with an index and array notation in 
python.

 ^ ,  <<   , and | are all bitwise operators, and python uses all of these 
in the same way as C

'^' means XOR exclusive OR.
0101 ^ 0011 = 0110i.e.  5 ^ 3 = 6

'<< ' means left - shift
0010 << 2 = 1000  i.e. a << b = a * (2**b)

'|' means OR.
0101 ^ 0011 = 0111   i.e. 5 ^ 3 = 7

puchMsgg   is basically a string
and all the unsigned stuff are (very roughly) integers.

HTH,
Jacob S. 

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


Re: [Tutor] Key Error

2007-07-08 Thread jim stockford

On Jul 8, 2007, at 9:45 AM, Alan Gauld wrote:

> (The tests at the end
> are poorly written too. If one of my team produced code like
> this I'd be having strong words with them!)

If you'd be willing to share your strong words, I'd
be grateful to learn better alternatives.

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


Re: [Tutor] Key Error

2007-07-08 Thread Sara Johnson
If this is in reference to the code that I listed.  
   
  I have no experience with Python so I may have left something off unknowingly 
which resulted in him questioning whatever was there.  The strong words 
probably should be directed at me for not knowing what I'm doing.

jim stockford <[EMAIL PROTECTED]> wrote:
  
On Jul 8, 2007, at 9:45 AM, Alan Gauld wrote:

> (The tests at the end
> are poorly written too. If one of my team produced code like
> this I'd be having strong words with them!)

If you'd be willing to share your strong words, I'd
be grateful to learn better alternatives.

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


 
-
We won't tell. Get more on shows you hate to love
(and love to hate): Yahoo! TV's Guilty Pleasures list.___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] help with translating a c function to a python function

2007-07-08 Thread shawn bright

Hey thanks,
i finally did get a function working.
i posted it on www.bitsbam.com

i did guess that the puchMsg++ ment that it was iterating through the bytes
of an array.
And Kent and Alan helped me get through the other parts.

I am glad for all this help, because this is an issue that comes up
increasingly often.
so i am also glad for the email archiving by gmail. he he

thanks all,
shawn


On 7/8/07, Tiger12506 <[EMAIL PROTECTED]> wrote:



>> i have a c function from some modbus documentation that i need to
>> translate into python.

>> unsigned short CRC16(puchMsg, usDataLen)
>> unsigned char *puchMsg ;
>> unsigned short usDataLen ;
>> {
>> unsigned char uchCRCHi = 0xFF ;
>> unsigned char uchCRCLo = 0xFF ;
>> unsigned uIndex ;
>> while (usDataLen––)
>> {
>> uIndex = uchCRCHi ^ *puchMsgg++ ;
>> uchCRCHi = uchCRCLo ^ auchCRCHi[uIndex} ;
>> uchCRCLo = auchCRCLo[uIndex] ;
>> }
>> return (uchCRCHi << 8 | uchCRCLo) ;
>> }

I found this link which may provide some insight into what's going on
here.
(google "modbus CRC16")

http://www.modbustools.com/modbus_crc16.htm

This proves to me that auchCRCHi is a lookup table that you do not have
access to. Happily :-) that link provides the table.

Hmmm... let's see, the difficult C stuff... *puchMsgg++ means to return
the
current character in a string, and then increment the pointer, so that
when
the C code encounters *puchMsgg++ again it reads the next character,
increments, etc. You can emulate this with an index and array notation in
python.

^ ,  <<   , and | are all bitwise operators, and python uses all of these
in the same way as C

'^' means XOR exclusive OR.
0101 ^ 0011 = 0110i.e.  5 ^ 3 = 6

'<< ' means left - shift
0010 << 2 = 1000  i.e. a << b = a * (2**b)

'|' means OR.
0101 ^ 0011 = 0111   i.e. 5 ^ 3 = 7

puchMsgg   is basically a string
and all the unsigned stuff are (very roughly) integers.

HTH,
Jacob S.

___
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] Bundle help!

2007-07-08 Thread Sara Johnson
I brought this up with Kent a little while ago...
   
  >>>If you have a list of pairs of (name, percentage) then you should be 
>>>able to sort it directly with the sort() method of the list. For
 >>>example:

>>>In [3]: data = [ ('Kent', 50), ('Sara', 80), ('Fred', 20) ]
>>>In [4]: data.sort()
>>>In [5]: data
>>>Out[5]: [('Fred', 20), ('Kent', 50), ('Sara', 80)]
>Use append() to add more data, then sort again to get it in order:
>>>In [6]: data.append(('Joe', 90))
>>>In [7]: data.sort()
>>>In [8]: data
>>>Out[8]: [('Fred', 20), ('Joe', 90), ('Kent', 50), ('Sara', 80)]

  What happens if I need to sort alphabetical and numerically?
  I guess in this case it would be
   
  ('Fred', 20), ('Joe', 50), ('Kent', 80), ('Sara', 90)
   
  I'm taking the original list and the original values
  i.e., ('Fred', 20), ('Joe', 90), ('Kent', 80)...  and switching it so that it 
reads in both ways...
   
  List 1, [('Fred', 20), ('Joe', 90), ('Kent', 50), ('Sara', 80)]

  List 2, ('Fred', 20), ('Joe', 50), ('Kent', 80), ('Sara', 90)]
   
  How would I do that so I wind up with both lists?  I have two lists now, but 
if I try and reorder them they just reverse in alphabetical order without 
affecting the second value (the number).
   
  Thanks!
   

   
-
Boardwalk for $500? In 2007? Ha! 
Play Monopoly Here and Now (it's updated for today's economy) at Yahoo! Games.___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor