[Tutor] wikibooks

2006-09-18 Thread Hugh Stewart



Hi All,
 
The following site maybe of interest:
 
http://en.wikibooks.org/wiki/Wikibooks:Computing_department
 
 
Hugh
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] How to convert a decimal integer into binary

2006-09-18 Thread Luke Paireepinart
Asrarahmed Kadri wrote:
> Can anyone help me with teh problem of "converting a decimal number 
> into its binary equivalent"??
>  
Ah, sorry, I didn't read the subject line, just the e-mail.
You said a decimal integer into binary.  I thought you meant any random 
decimal value.
My bad :)
You should be able to find a tutorial on this on-line somewhere.

you know that
decimal: 1  = binary: 0001
decimal: 2 = binary: 0010
decimal: 4 = binary: 0100
decimal: 8 = binary: 1000

Do you see the pattern here?
It's powers of 2.

I don't have time to write an example program, but see what you can do.
Basically, if I remember correctly, you have to find the smallest power 
of 2 that is greater than your number,
then repeatedly divide then mod the integer for each binary digit.
> Thanks in anticipation.
sure.
>  
> Regards,
> Asrar Kadri
-Luke
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] How to convert a decimal integer into binary

2006-09-18 Thread Luke Paireepinart
Asrarahmed Kadri wrote:
> Can anyone help me with teh problem of "converting a decimal number 
> into its binary equivalent"??
>  
How is your decimal number stored?
> Thanks in anticipation.
>  
> Regards,
> Asrar Kadri
>  
> --
>  
> Winners are willing to do things that losers wont do.
> 
>
> ___
> 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] How to convert a decimal integer into binary

2006-09-18 Thread Asrarahmed Kadri
Can anyone help me with teh problem of "converting a decimal number into its binary equivalent"??
 
Thanks in anticipation.
 
Regards,
Asrar Kadri
 
--
 
Winners are willing to do things that losers wont do.
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Help with parsing

2006-09-18 Thread Andrew Robert
Bryan Leber wrote:
>
> Hello, I am trying to create a script that reads from the command line
> and puts the sysargv into a list. I have this part done. I have added
> code for testing and one of those testing procedures is to print the
> list out in a text file.  Not all field are required and may not have
> information in them. A sample text file looks like this:
>
>  
>
> PATCH_NUMBER: 
>
> BUG_NUMBER: 4534
>
> FEATURE_AFFECTED: Admin login
>
> OVERVEIW: The icon of the submit has changed
>
>  
>
> Now what I need to accomplish is to search through this list and if
> FEATURE_AFFECTED or OVERVIEW do not have values(i.e. Admin login or
> The icon of the submit changed) then I need to print a message and
> then exit.  Right now I have something like this
>
>  
>
> Size = len(argsList)
>
> If size = 4
>
>For i in argsList
>
>If i[2] == None:
>
>   Print ‘please enter criteria’
>
>   Sys.exit()
>
>Elif i[3] == None:
>
>   Print ‘please enter criteria’
>
>   Sys.exit()
>
>Else:
>
> Sys.exit()
>
>  
>
> Any help would be appreciated. Thanks
>
>  
>
>  
>
> **/Bryan Leber/**
>
> Developer
>
> Fischer International Corporation
>
> www.fischerinternational.com 
>
> [EMAIL PROTECTED]
> 
>
> Cell:(239)963-5267
>
>  
>
> Secure Your Risk. Increase Your Bottom Line. ™
>
>  
>
> 
>
> This mail message may contain confidential and privileged information
> from Fischer International which is protected.  Any unauthorized
> review, use, disclosure or distribution by any means is prohibited. 
> If you are not the intended recipient, please contact the sender by
> reply email and destroy all copies of the original message
>
> 

I recommend that you check out the optparse.module from OptionParser

It does what you are looking for in a concise method.

See http://docs.python.org/lib/module-optparse.html

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


Re: [Tutor] Some questions about my yen-USD.py

2006-09-18 Thread Amadeo Bellotti
thats fine its just my dad would need that and it would be easier for him if he had the exchange rate bulit inOn 9/17/06, Dick Moores <
[EMAIL PROTECTED]> wrote:At 07:10 PM 9/17/2006, Amadeo Bellotti wrote:>ok i jsut wanted to say great program but i would like to see an
>acutal exchange rate like maybe get it from a website it would be so>much nicer and easier to use also it would help linux users who run>from console so they dont have to look up the current rate online
Thanks for the great suggestion. Right now I don't have the faintestidea how to implement it, but I'm sure going to try to learn how.Give me till Tuesday? ;)Dick
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Some questions about my yen-USD.py

2006-09-18 Thread Alan Gauld
>>> And what's the downside of the way I've done it?
>>
>> Its slow...
>
> Actually it is not particularly slow. The actual function code is 
> created once, when the module is compiled; creating a function 
> object and binding it to a name is pretty fast. There is a good 
> discussion here:
> http://tinyurl.com/gzfyl
>

Absolutely, I should have said its "slower", all things are relative.
For this application it probably makes no difference in real terms.

Alan G. 

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


Re: [Tutor] Some questions about my yen-USD.py

2006-09-18 Thread Dick Moores
At 06:56 AM 9/18/2006, Luke Paireepinart wrote:
>Dick Moores wrote:
>>At 05:20 AM 9/18/2006, Kent Johnson wrote:
>>
>>
>>>You have greatly underused Decimal - it is capable of multiplication and
>>>division of fractional quantities directly:
>>>
>>>In [1]: from decimal import Decimal as D
>>>
>>>In [2]: x=D('1.23')
>>>
>>>In [3]: y=D('4.5')
>>>
>>>In [4]: x*y
>>>Out[4]: Decimal("5.535")
>>>
>>>In [5]: x/y
>>>Out[5]: Decimal("0.2733")
>>>
>>
>>And sqrt() as well, which I definitely thought was not possible. 
>>
>>
>>Well, you settled that. I don't know why I didn't see it.
>>
>Perhaps you had some preconceptions about the limits of the Decimal 
>module, and upon
>preliminary investigations something confirmed this for you, so you 
>didn't actually look in-depth
>for a way to do what you were trying to do because it seemed at 
>first glance like Decimal wasn't the right tool
>(maybe you saw an example that didn't fully utilize Decimal or something.)
>Or, alternatively, you cheated and skimmed over the docs, and didn't 
>see something important the first time around :)

A little of both, I think, but more of the former.

>>
of course couldn't rely on the use of the built-in round() or the
formatting of strings (see the line "format = "%." + str(precision) +
'f'" in setPrecision() in v3).  Lack of experience with the slicing
of lists caused many headaches. I didn't succeed in debugging until I
put in print statements wherever a value changes, and trying many
different integer strings and places (the arguments of
roundNumber()). A good lesson, I think.

>>>The recipes page in the docs for Decimal include a moneyfmt() function
>>>that rounds to a specified number of places and inserts a separator char.
>>>
>>
>>I'd seen the recipes in the docs, but couldn't make much sense out 
>>of them. I think I can now. Or at least more sense than before.
>>
>>
>Yes, even though a portion of your program has functionality in the 
>Decimal module already,
>coding anything is good practice.  For example, my dream is to one 
>day write a NES emulator in Python,
>and this has already been done dozens of times in C, C++,  Java, 
>even Visual Basic.  I don't care, my goal is the same
>whether or not I'm reinventing the wheel. :)  I know of no NES 
>emulator in Python, though, so I guess i'm not really
>reinventing the wheel after all (if anyone asks, tell them I'm 
>porting that Java emulator :)

I learned a lot in spending the time I did on roundNumber().

Dick


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


Re: [Tutor] Some questions about my yen-USD.py

2006-09-18 Thread Luke Paireepinart
Dick Moores wrote:
> At 05:20 AM 9/18/2006, Kent Johnson wrote:
>
>   
>> You have greatly underused Decimal - it is capable of multiplication and
>> division of fractional quantities directly:
>>
>> In [1]: from decimal import Decimal as D
>>
>> In [2]: x=D('1.23')
>>
>> In [3]: y=D('4.5')
>>
>> In [4]: x*y
>> Out[4]: Decimal("5.535")
>>
>> In [5]: x/y
>> Out[5]: Decimal("0.2733")
>> 
>
> And sqrt() as well, which I definitely thought was not possible. 
> 
>
> Well, you settled that. I don't know why I didn't see it.
>   
Perhaps you had some preconceptions about the limits of the Decimal 
module, and upon
preliminary investigations something confirmed this for you, so you 
didn't actually look in-depth
for a way to do what you were trying to do because it seemed at first 
glance like Decimal wasn't the right tool
(maybe you saw an example that didn't fully utilize Decimal or something.)
Or, alternatively, you cheated and skimmed over the docs, and didn't see 
something important the first time around :)
>   
>>> of course couldn't rely on the use of the built-in round() or the
>>> formatting of strings (see the line "format = "%." + str(precision) +
>>> 'f'" in setPrecision() in v3).  Lack of experience with the slicing
>>> of lists caused many headaches. I didn't succeed in debugging until I
>>> put in print statements wherever a value changes, and trying many
>>> different integer strings and places (the arguments of
>>> roundNumber()). A good lesson, I think.
>>>   
>> The recipes page in the docs for Decimal include a moneyfmt() function
>> that rounds to a specified number of places and inserts a separator char.
>> 
>
> I'd seen the recipes in the docs, but couldn't make much sense out of 
> them. I think I can now. Or at least more sense than before.
> 
>   
Yes, even though a portion of your program has functionality in the 
Decimal module already,
coding anything is good practice.  For example, my dream is to one day 
write a NES emulator in Python,
and this has already been done dozens of times in C, C++,  Java, even 
Visual Basic.  I don't care, my goal is the same
whether or not I'm reinventing the wheel. :)  I know of no NES emulator 
in Python, though, so I guess i'm not really
reinventing the wheel after all (if anyone asks, tell them I'm porting 
that Java emulator :)
> Thanks, Kent.
>
> Dick
>
>   
Have a good day!
I have to get my butt to class now.  4 minutes! eek.
-Luke


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


Re: [Tutor] Some questions about my yen-USD.py

2006-09-18 Thread Luke Paireepinart
Dick Moores wrote:
> At 01:00 AM 9/18/2006, Alan Gauld wrote:
>   
  1. in your roundNumber function, you define a function
 incrementDigit.
  I'm pretty sure that this function is destroyed and recreated
 every time
  you call the function roundNumber.
 
>>> I don't understand. What's another way?
>>>   
>> def f():
>> def g(): return 42
>> return g()
>>
>> def g(): return 42
>> def f() return g()
>> 
Alan meant 'def f(): return g()' on that last line there I think :)
>
> Doesn't seem slow to me. But I take your point.
>   
Well, there's the whole Python idiom of 'readability > speed'.  It 
really doesn't matter how
fast something is if it increases readability (unless the slower speed 
really makes a difference,
like that guy's program that used range() instead of xrange() and 
crashed the computer!)
However, nested functions are not very common, so for me, they are just 
distracting.
As Kent said, it may not be that slow to do this, either, and in your 
particular program,
if you choose to use nested functions it shouldn't make any noticeable 
speed difference.
If you were going to parse 100,000 text files and change any occurrences 
of $xx.xx into
Yen from a given conversion rate, then you'd probably want it to be as 
efficient as possible,
but if someone's just converting one value, they're not really going to 
notice if it's .001 seconds slower, right?


>> 
>>> No, I'll take your advice. But I hate to type underscores,
>>> so is there another style I could use for functions that
>>> would be different from the aRandomVariable style I
>>> like for variables?
>>>   
Yeah, underscores are kind of bothersome.
That's what I usually end up using, though.
>> 
>>> Function Names
>>>
>>> Function names should be lowercase, with words separated by
>>> underscores  as necessary to improve readability.
>>>   
>> It may be the official style but in practice its not that widely
>> followed.
>> 
Yeah, I haven't seen too much of that going on.  Most pieces of Python 
code I read have some crazy
syntax that I've never seen before.  I learn new things every day :D
-Luke
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Some questions about my yen-USD.py

2006-09-18 Thread Dick Moores
At 05:20 AM 9/18/2006, Kent Johnson wrote:

>You have greatly underused Decimal - it is capable of multiplication and
>division of fractional quantities directly:
>
>In [1]: from decimal import Decimal as D
>
>In [2]: x=D('1.23')
>
>In [3]: y=D('4.5')
>
>In [4]: x*y
>Out[4]: Decimal("5.535")
>
>In [5]: x/y
>Out[5]: Decimal("0.2733")

And sqrt() as well, which I definitely thought was not possible. 


Well, you settled that. I don't know why I didn't see it.

> > of course couldn't rely on the use of the built-in round() or the
> > formatting of strings (see the line "format = "%." + str(precision) +
> > 'f'" in setPrecision() in v3).  Lack of experience with the slicing
> > of lists caused many headaches. I didn't succeed in debugging until I
> > put in print statements wherever a value changes, and trying many
> > different integer strings and places (the arguments of
> > roundNumber()). A good lesson, I think.
>
>The recipes page in the docs for Decimal include a moneyfmt() function
>that rounds to a specified number of places and inserts a separator char.

I'd seen the recipes in the docs, but couldn't make much sense out of 
them. I think I can now. Or at least more sense than before.


Thanks, Kent.

Dick



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


Re: [Tutor] Some questions about my yen-USD.py

2006-09-18 Thread Dick Moores
At 01:00 AM 9/18/2006, Alan Gauld wrote:
> >>  1. in your roundNumber function, you define a function
> >> incrementDigit.
> >>  I'm pretty sure that this function is destroyed and recreated
> >> every time
> >>  you call the function roundNumber.
> > I don't understand. What's another way?
>
>def f():
> def g(): return 42
> return g()
>
>def g(): return 42
>def f() return g()
>
>The two bits of code do the same thing but the first
>constructs/deletes g() each time.

Hm. That's what I get for knowing little computer science.


> > And what's the downside of the way I've done it?
>
>Its slow...

Doesn't seem slow to me. But I take your point.

> > How do you keep from causing all that destruction and recreation?
>
>See above
>
> > And what's bad about it?
>
>Its slow
>
>But sometimes defining a function inside another function is
>what you want, because you need to limit visibility, or it needs
>to access variables that are local to the outer function.
>But unless you have a very clear idea of why you want to
>define a nested function its better to make them global.
>
> > No, I'll take your advice. But I hate to type underscores,
> > so is there another style I could use for functions that
> > would be different from the aRandomVariable style I
> > like for variables?
>
>Personally I don't differentiate variables and functions
>in Python (partly because Python doesn't - they are
>all just names) mainly because functions are usually
>obvious by dint of the parentheses used to call them.
>
> > Function Names
> >
> > Function names should be lowercase, with words separated by
> > underscores  as necessary to improve readability.
>
>It may be the official style but in practice its not that widely
>followed.
>
> > So I guess I should start learning to type underscores accurately.
>
>Me too, I guess :-)

Thanks, Alan.

Dick



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


Re: [Tutor] Some questions about my yen-USD.py

2006-09-18 Thread Kent Johnson
Alan Gauld wrote:
>>>  1. in your roundNumber function, you define a function 
>>> incrementDigit.
>>>  I'm pretty sure that this function is destroyed and recreated 
>>> every time
>>>  you call the function roundNumber.
>> I don't understand. What's another way?
> 
> def f():
> def g(): return 42
> return g()
> 
> def g(): return 42
> def f() return g()
> 
> The two bits of code do the same thing but the first
> constructs/deletes g() each time.
> 
>> And what's the downside of the way I've done it?
> 
> Its slow...

Actually it is not particularly slow. The actual function code is 
created once, when the module is compiled; creating a function object 
and binding it to a name is pretty fast. There is a good discussion here:
http://tinyurl.com/gzfyl

Kent

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


Re: [Tutor] Some questions about my yen-USD.py

2006-09-18 Thread Kent Johnson
Dick Moores wrote:
> I'm bck!
> 
> I kept getting ideas for what I (and some of you) thought was a 
> finished yen-USD.py. And some of the good advice I got was to move on 
> to other things. I did for a while, but I kept thinking up new 
> revisions. The script has more than doubled in length. I'd previously 
> posted v4 at .
> 
> Here's v10: 
> 
> New functions:
> again()
> divide2StringDecimals()
> multiply2StringDecimals()
> roundNumber() -- replaced setPrecision()
> printVariablesNotChanging()
> formatNumber()
> removeCommasFromNumbers()
> 
> rather radically revised function:
> again()  -- offers several more choices
> 
> The most important change is that because I realized I wanted the 
> program to be a general solution and give accurate answers for even 
> very large amounts of Yen or USD, I decided to operate with (number) 
> strings only, except when necessary in getRate() and getAmount() to 
> error check user inputs. Those floats are not used in the 
> calculations of Yen or USD.

You have greatly underused Decimal - it is capable of multiplication and 
division of fractional quantities directly:

In [1]: from decimal import Decimal as D

In [2]: x=D('1.23')

In [3]: y=D('4.5')

In [4]: x*y
Out[4]: Decimal("5.535")

In [5]: x/y
Out[5]: Decimal("0.2733")
> 
> The most difficult function for me to write was roundNumber(), which 
> of course couldn't rely on the use of the built-in round() or the 
> formatting of strings (see the line "format = "%." + str(precision) + 
> 'f'" in setPrecision() in v3).  Lack of experience with the slicing 
> of lists caused many headaches. I didn't succeed in debugging until I 
> put in print statements wherever a value changes, and trying many 
> different integer strings and places (the arguments of 
> roundNumber()). A good lesson, I think.

The recipes page in the docs for Decimal include a moneyfmt() function 
that rounds to a specified number of places and inserts a separator char.

Kent

> 
> I hope some of the Tutors will take a look at the new functions, 
> especially roundNumber().
> Did I just reinvent the wheel?

Yes :-)

> Should it be broken up into more sub-functions (there's only one now)?
> It works, but is it Pythonic? Etc.
> 
> I'm also curious about multiply2StringDecimals() and divide2StringDecimals().
> Again, am I reinventing the wheel with these?
> Is there a simpler way to multiply and divide big decimals with precision?
> 
> Thanks in advance,
> 
> Dick Moores
> 
> 
> ___
> 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] From byte[] to Image

2006-09-18 Thread Ziad Rahhal
I do the following:



file = StringIO.StringIO(buffer)

img = Image.open(file)

img.save(file, 'JPEG')



I get this error: 

img = Image.open(file)

  File "/home/rahhal/python/lib/python2.4/site-packages/PIL/Image.py", line 1745, in open

    raise IOError("cannot identify image file")

IOError: cannot identify image file


Yes I printed the contents of Buffer but how can I make sure if it is all there since the type I am receiving is 

different (temporarly, until I form the Image) from the original content of the Image itself. I tried to attach the original
image and the data (in the buffer) but the email bounced as it was too large. So I am sending the email again without
attachments

Regards,
Ziad
On 9/17/06, Alan Gauld <[EMAIL PROTECTED]> wrote:
> (Note that this function decodes pixel data only, not entire images.> If you> have an entire image file in a string, wrap it in a *StringIO*> object, and> use> *open*<
file:///home/rahhal/Imaging-1.1.5/Docs/pythondoc-PIL.Image.html#PIL.Image.open-function>to> load it.)>> So I guess "frombuffer" must not be used in my case.Looks like it, but did you try what it suggested, namely using
a StringIO object and the open method?What happened?> The java method is working correctly but I am not able to form the> picture> in the right way at the client side using the PIL library.
> I receive it at the client side in python:> buffer = service.getFile(fileName)I assume you tried printing buffer (or at least its len) to check thatit was all there?Alan G.

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


Re: [Tutor] From byte[] to Image

2006-09-18 Thread Ziad Rahhal
I do the following:

file = StringIO.StringIO(buffer)
img = Image.open(file)
img.save(file, 'JPEG')

I get this error: 
img = Image.open(file)
  File "/home/rahhal/python/lib/python2.4/site-packages/PIL/Image.py", line 1745, in open
    raise IOError("cannot identify image file")
IOError: cannot identify image file

Yes I printed the contents of Buffer but how can I make sure if it is all there since the type I am receiving is 
different (temporarly, until I form the Image) from the original content of the Image itself. I will attach the buffer
I am receiving and the original Image I am sending.
On 9/17/06, Alan Gauld <[EMAIL PROTECTED]
> wrote:
> (Note that this function decodes pixel data only, not entire images.> If you> have an entire image file in a string, wrap it in a *StringIO*> object, and> use> *open*<
file:///home/rahhal/Imaging-1.1.5/Docs/pythondoc-PIL.Image.html#PIL.Image.open-function>to> load it.)>> So I guess "frombuffer" must not be used in my case.Looks like it, but did you try what it suggested, namely using
a StringIO object and the open method?What happened?> The java method is working correctly but I am not able to form the> picture> in the right way at the client side using the PIL library.
> I receive it at the client side in python:> buffer = service.getFile(fileName)I assume you tried printing buffer (or at least its len) to check thatit was all there?Alan G.




buffer
Description: Binary data
<>
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] folder and module

2006-09-18 Thread linda.s
> Hi Linda,
>
> what do you get when you try:
>
>  >>> import os
>  >>> os.getcwd()
>
> The current working directory is represented in sys.path as the first
> element:
>
>  >>> import sys
>  >>> sys.path[0]
> ''
>
>
> Not the most intuitive, perhaps.
>
> I suspect you are launching your Python environment from a desktop
> icon. That would explain why import can `see' your Desktop, even
> though it doesn't at first glance seem to be in sys.path.
You are right!!! I launched Python environment from a desktop icon.
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Some questions about my yen-USD.py

2006-09-18 Thread Alan Gauld
>>  1. in your roundNumber function, you define a function 
>> incrementDigit.
>>  I'm pretty sure that this function is destroyed and recreated 
>> every time
>>  you call the function roundNumber.
> I don't understand. What's another way?

def f():
def g(): return 42
return g()

def g(): return 42
def f() return g()

The two bits of code do the same thing but the first
constructs/deletes g() each time.

> And what's the downside of the way I've done it?

Its slow...

> How do you keep from causing all that destruction and recreation?

See above

> And what's bad about it?

Its slow

But sometimes defining a function inside another function is
what you want, because you need to limit visibility, or it needs
to access variables that are local to the outer function.
But unless you have a very clear idea of why you want to
define a nested function its better to make them global.

> No, I'll take your advice. But I hate to type underscores,
> so is there another style I could use for functions that
> would be different from the aRandomVariable style I
> like for variables?

Personally I don't differentiate variables and functions
in Python (partly because Python doesn't - they are
all just names) mainly because functions are usually
obvious by dint of the parentheses used to call them.

> Function Names
>
> Function names should be lowercase, with words separated by
> underscores  as necessary to improve readability.

It may be the official style but in practice its not that widely 
followed.

> So I guess I should start learning to type underscores accurately.

Me too, I guess :-)

Alan G. 

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


Re: [Tutor] Python Course at Foothill College

2006-09-18 Thread Alan Gauld
> *sigh* Perhaps when I start grad school I'll be in a position to use 
> my
> favored language...until then I guess .NET wins.

Have you tried IronPython?
Python for .NET...

Alan G.

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


[Tutor] Fw: i just cant do it

2006-09-18 Thread Alan Gauld


- Original Message - 
From: "federico ramirez" <[EMAIL PROTECTED]>
To: "Alan Gauld" <[EMAIL PROTECTED]>
Sent: Monday, September 18, 2006 12:43 AM
Subject: Re: [Tutor] i just cant do it


> Nope...sorry i tried what they said in that article but it didnt 
> work :(
>>_<
>
> 2006/9/17, Alan Gauld <[EMAIL PROTECTED]>:
>>
>>
>> > Sorry, in the first code i set the cookie and then display it but 
>> > in
>> > the
>> > second i assume that the cookie is created and just want to read 
>> > it,
>> > but
>> > nothing happens
>>
>> Correct.
>> I notice Luke also sent a reply.
>>
>> Between the two replies do you understand what you now need to do
>> differently in the second case to recreate the cookie from its 
>> file?
>>
>> Alan G.
>>
>> >> > #!/usr/bin/python
>> >> >
>> >> > import Cookie
>> >> > C = Cookie.SimpleCookie()
>> >> > C['adminuser'] = 'fedekiller'
>> >> > C['adminuser']['max-age'] = 60*60*24*7
>> >> > print C
>> >> > print "Content-Type: text/html\n\n"
>> >> > print "Bienvenido",C['adminuser'].value,'!'
>> >> >
>> >> >
>> >> > but when i try this it doesnt
>> >>
>> >> Can you explain why you think it should work?
>> >> What do yopu think it is doing?
>> >> The code below is very obviously completely different to
>> >> the code above, so what makes you think it should work?
>> >>
>> >> > #!/usr/bin/python
>> >> >
>> >> > import Cookie
>> >> > print "Content-Type: text/html\n\n"
>> >> > print "Bienvenido",C['adminuser'].value,'!'
>> >>
>> >> What is C? Where is it defined?
>> >>
>> >> >  i know the cookie exists because firefox display it where
>> >> > all the cookies of that domains are.
>> >>
>> >> Cookies exist as small files on your PC. A Python program
>> >> needs to be given a clue as to which cookie you are interested
>> >> in so that it can access that file. That's what the Cookie 
>> >> module
>> >> helps you to do, but it needs to be more than simply imported.
>> >> That just makes the tools available, it doesn't actually turn
>> >> them on.
>> >>
>> >> HTH,
>> >>
>> >> Alan G.
>> >>
>> >
>> >
>> >
>> > --
>> > Best Regards.
>> > fedekiller
>> >
>>
>>
>
>
> -- 
> Best Regards.
> fedekiller
> 

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


Re: [Tutor] Some questions about my yen-USD.py

2006-09-18 Thread Dick Moores


At 08:13 PM 9/17/2006, Luke Paireepinart wrote:
Dick Moores wrote:
> I'm bck!
>
> I kept getting ideas for what I (and some of you) thought was a

> finished yen-USD.py. And some of the good advice I got was to move
on 
> to other things. I did for a while, but I kept thinking up new 
> revisions. The script has more than doubled in length. I'd
previously 
> posted v4 at
<
http://www.rcblue.com/Python/yen-USD-v3.txt>.
>
> Here's v10:
<
http://www.rcblue.com/Python/yen-USD-v10.txt>
>
> New functions:
> again()
> divide2StringDecimals()
> multiply2StringDecimals()
> roundNumber() -- replaced setPrecision()
> printVariablesNotChanging()
> formatNumber()
> removeCommasFromNumbers()
>
> rather radically revised function:
> again()  -- offers several more choices
>
> The most important change is that because I realized I wanted the

> program to be a general solution and give accurate answers for even

> very large amounts of Yen or USD, I decided to operate with (number)

> strings only, except when necessary in getRate() and getAmount() to

> error check user inputs. Those floats are not used in the 
> calculations of Yen or USD.
>
> The most difficult function for me to write was roundNumber(), which

> of course couldn't rely on the use of the built-in round() or the

> formatting of strings (see the line "format = "%." +
str(precision) + 
> 'f'" in setPrecision() in v3).  Lack of experience with
the slicing 
> of lists caused many headaches. I didn't succeed in debugging until
I 
> put in print statements wherever a value changes, and trying many

> different integer strings and places (the arguments of 
> roundNumber()). A good lesson, I think.
>
>   
A few notes:
1. in your roundNumber function, you define a function
incrementDigit.
I'm pretty sure that this function is destroyed and recreated every time

you call the function roundNumber.
This applies to divide2stringdecimals and other functions as well.
Is this what you want?
I don't understand. What's another way? And what's the downside of the
way I've done it? How do you keep from causing all that destruction and
recreation? And what's bad about it?
2.  in your remove commas
from numbers function, you could simplify it:
#from
    a = n.split(',')
    n = ''.join(a)
    return n
#to
    return ''.join(a.split(','))
personally I like this more.
Yes, I do too. 
> I hope some of the Tutors
will take a look at the new functions, 
> especially roundNumber().
> Did I just reinvent the wheel?
>   
I'm pretty sure you could set up a Context in the decimal module
that
will round and keep the precision where you specify.
So if this is true, I guess you did reinvent the wheel.
Yes, I jut got the knew Python for Dummies, and see that I could have
greatly shortened roundNumber() if I'd used the decimal module more. I
had thought I KNEW that you couldn't multiply or divide decimal numbers
directly!   I also didn't realize that I could use it for
rounding. I'll rewrite roundNumber(), divide2StringDecimals, and
multiply2StringDecimals. But I'm still shaky with the decimal module.
Don't really understand what you wrote below about a custom context. But
that's OK. I'll get it, I think, by just experimenting.
> Should it be broken up into
more sub-functions (there's only one now)?
> It works, but is it Pythonic? Etc.
>   
I think you need to settle on a naming convention so your code's more

readable.
Your variables are sometimes named aRandomVariable, with the start of

each new word capitalized,
then in other places (like the variable 'slen' in numberCommas ->

intCommas) they're not.
Also, it's easier to differentiate between functions and variables if

they're named a different way.
Like a_variable_name = someFunction()
That's just my opinion, of course, and you can do whatever you want
:)
No, I'll take your advice. But I hate to type underscores, so is there
another style I could use for functions that would be different from the
aRandomVariable style I like for variables? And intCommas() with its
"slen"--I've had that around a long time. Should be more
critical of my old stuff. I suppose I'll rename slen to lenS. 
But since writing the above I remembered that Python For Dummies
references PEP 8, "Style Guide for Python Code"
(<
http://www.python.org/dev/peps/pep-0008/>), in which I
find

Function Names

   Function names should be lowercase, with words separated by
underscores
   as necessary to improve readability.

   mixedCase is allowed only in contexts where that's already
the
   prevailing style (e.g. threading.py), to retain backwards
compatibility.


So I guess I should start
learning to type underscores accurately.
Also, I've seen that you
call the variable you store the split value in 
'splt'
Ugly, isn't it. Thanks.
Now while this approach may have
its uses, it makes it more difficult to 
understand
what's going on in your code (for me).
If I were writing this software I might misread (or mistype!) split 
instead of splt, and not be able to figure