Re: [Tutor] Tutor Digest, Vol 67, Issue 22

2009-09-06 Thread Alan Gauld


"Corey Richardson"  wrote

then you can program anything since sequences, loops and 
branches are all that are necessary to program anything!



Ummm, whats a branch? Haha, I know loops, but not branches.


Branches are if/else statements and case statements and jump 
or goto statements. In Python they are restricted to if/elif/else


Sometimes known as conditionals.

See the branching topic in my tutor for more detail.


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


Re: [Tutor] Help deciding between python and ruby

2009-09-06 Thread Alan Gauld


"Luke Paireepinart"  wrote

 I agree, but I can see how some people don't like it.  It's similar to 
the

Java vs. C++ debate I guess, Java is forced OO and C++ is optional.


While this is a genuine complaint for Java I have to be fair and say
that Ruby does a good job of hiding its OOP purity when you don't
need it.

Any function defined outside of a class automatically gets created
as a static method of a "secret" hidden toplevel class that is never
explicitly instantiated. So for the programmer you can 99% of the
time forget that Ruby always requires OOP and use it just as you
would non OOP Python.

And of course so many of Python's types are now objects that it's
virtually impossible to write a truly non OOP program in Python! So
that point is rather moot for the Python/Ruby debate. - IMHO of course :-)

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


Re: [Tutor] Tutor Digest, Vol 67, Issue 22

2009-09-06 Thread Luke Paireepinart
>
>
>>  Ummm, whats a branch? Haha, I know loops, but not branches.
>
A branch changes the program flow based on a condition.
Example:
x = int(raw_input("Please enter a number!"))
if x > 10:
print "Your number was greater than 10!"
else:
print "Your number was less than or equal to 10!"
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Tutor Digest, Vol 67, Issue 22

2009-09-06 Thread Corey Richardson





"Corey Richardson"  wrote 

  
So far, I can use tuples/lists/dictionary's, and define some functions, 
and a bit of other things.
Would it be hard for me to make a simple text rpg game? Or is there 
something else I should know before I try that.



No idea about rpg because I've never played one far less written one.

But you certainly have enough to write a simple game like mastermind, 
oxo or hangman...


Assuming you also mean that you know about loops and branches 
then you can program anything since sequences, loops and 
branches are all that are necessary to program anything!


So go for it, and if you get stuick as here.



  

Ummm, whats a branch? Haha, I know loops, but not branches.


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


Re: [Tutor] how to read and transmit/send only section of the image (sub-image)

2009-09-06 Thread Jojo Mwebaze
thanks a lot .
cheers

Johnson



On Sun, Sep 6, 2009 at 4:23 PM, Kent Johnson  wrote:

> On Sat, Sep 5, 2009 at 10:52 AM, Jojo Mwebaze 
> wrote:
> > oooh that will be helpful,  kindly point me to how i can begin with
> getting
> > a sub-image that has the same width as the
> > original. That will be my starting point.
>
> I don't know the details of FITS format, but I imagine it would be
> something like this:
> - read enough of the FITS header to figure out where the image data is
> in the file
> - seek to the start of the sub-image and read the image data
> - construct a new header for the sub-image
> - write the new header and data to a new file
>
> This will require some understanding of FITS format, or a library that
> supports the operations you need. You might find code in PyFITS that
> will help.
>
> Kent
>
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Help deciding between python and ruby

2009-09-06 Thread Luke Paireepinart
On Sun, Sep 6, 2009 at 10:21 AM, kevin parks  wrote:

> Well, the only thing more annoying than off topic discussion is "meta"
> discussion on lists and folks passing judgement on other people's posts (ie.
> you should have x, your should not have Y... I shouldn't have this second
> doughnut, but you know what, i am gonna eat that *and* have a second cup of
> coffee). So i'll resist the temptation to reply to Luke in detail, who i did
> not realize was a moderator here.
>
For the record, I'm not a moderator.  I'm sorry if it seemed like I was
flaming you, I just don't think this discussion is a bad thing so far.

Most of this has already been touched on in this thread ... But I would add
> that that very past point is a HUGE reason why i use python. For little
> pea-shooter scripts I don't want to have to know and apply advanced OOP
> concepts. Yet if you do know them and your project requires it... it is
> there. [snip] I like to dig right in and Python is good for that for sure.
>
>  I agree, but I can see how some people don't like it.  It's similar to the
 Java vs. C++ debate I guess, Java is forced OO and C++ is optional.
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] A basic question

2009-09-06 Thread Alan Gauld

"sharon"  wrote

I have been trying how to get a any python program to run by clicking on 
the icon.  I keep reading on the internet  make it executable:


chmod a+x try_python.py


You do this in a console window, at a bash prompt.

You also need to add the "shebang" line at the top of the file.
That's a standard format comment line that Unix knows to look 
at to determine which interpreter to use to execute the script.
It's a Unix thing, not Python, which is why the tutorials don't say 
much about it, they assume you know Unix if you are using it.


everytime.  If I then sent this try_python.py  to someone else with 
Ubuntu would it run on their system?  


Yes provided they use chmod on it too.

You can find more about the chmod and shebang stuff 
in the Style topic off my tutorial inside the Note for Unix Users 
near the bottom.


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


Re: [Tutor] Simple Game

2009-09-06 Thread Alan Gauld


"Corey Richardson"  wrote 

So far, I can use tuples/lists/dictionary's, and define some functions, 
and a bit of other things.
Would it be hard for me to make a simple text rpg game? Or is there 
something else I should know before I try that.


No idea about rpg because I've never played one far less written one.

But you certainly have enough to write a simple game like mastermind, 
oxo or hangman...


Assuming you also mean that you know about loops and branches 
then you can program anything since sequences, loops and 
branches are all that are necessary to program anything!


So go for it, and if you get stuick as here.

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


Re: [Tutor] A basic question

2009-09-06 Thread Lucas Prado Melo
On Thu, Aug 6, 2009 at 4:20 PM, sharon  wrote:

> I have been trying how to get a any python program to run by clicking on
> the icon.  I keep reading on the internet  make it executable:
>
> chmod a+x try_python.py
>
Did you write:
#!/usr/bin/env python

at the first line of the file too?


> Where do I actually put the above expression so that it will work
> everytime.  If I then sent this try_python.py  to someone else with Ubuntu
> would it run on their system?  Sorry if this is too basic but it is an
> assumption that seems to be made and I really am a complete beginner.
>
Don't worry about it. :-)
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Pack/Unpack hacking

2009-09-06 Thread Kent Johnson
On Sun, Sep 6, 2009 at 2:20 PM, Tino Dai  wrote:
> Hi All,
>
>     Hope the people in the US are having a nice Labor Day! I am looking for
> the source code
> for the pack/unpack functions found in the struct package. As of this email,
> I have tried a
> strings on the struct.pyc file. The inspection of the pyc file was hoping
> that I could find a
> stub to the source. I also looked directly at struct.py, with no success.
> Finally, I also tried
> downloading the source and grepping through the files, which didn't prove
> all that useful.
> Does anybody have any ideas on how I can find the source without having to
> go through
> the entire source tree file by file?

In general, xxx.pyc is the compiled Python bytecode for xxx.py, so
struct.py is the source for struct.pyc.

Looking at struct.py, it's entire contents is
from _struct import *
from _struct import _clearcache

This is a pretty common idiom in the std lib for modules that are
implemented partially or completely as C extensions - there is a
Python wrapper, called xxx.py, which imports functions from a C
extension called _xxx. Often there are some functions in the Python
module; in this case, the implementation is entirely in _struct and
struct.py is just a shell.

The source for C extension modules in the std lib is in the Modules
folder. Look for _struct.c.

BTW another common convention is for modules that are implemented
entirely in C; they will have source in Modules/xxxmodule.c, for
example datetimemodule.c.

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


Re: [Tutor] Pack/Unpack hacking

2009-09-06 Thread Tim Golden

Hope the people in the US are having a nice Labor Day! I am looking for
the source code
for the pack/unpack functions found in the struct package. As of this
email, I have tried a
strings on the struct.pyc file. The inspection of the pyc file was hoping
that I could find a
stub to the source. I also looked directly at struct.py, with no success.
Finally, I also tried
downloading the source and grepping through the files, which didn't prove
all that useful.
Does anybody have any ideas on how I can find the source without having to
go through
the entire source tree file by file?



http://svn.python.org/view/python/trunk/Modules/_struct.c?view=markup

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


[Tutor] A basic question

2009-09-06 Thread sharon
I have been trying how to get a any python program to run by clicking on 
the icon.  I keep reading on the internet  make it executable:


chmod a+x try_python.py

Where do I actually put the above expression so that it will work 
everytime.  If I then sent this try_python.py  to someone else with 
Ubuntu would it run on their system?  Sorry if this is too basic but it 
is an assumption that seems to be made and I really am a complete beginner.


Thanks for any help

Sharon

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


Re: [Tutor] Pack/Unpack hacking

2009-09-06 Thread Lucas Prado Melo
On Sun, Sep 6, 2009 at 3:20 PM, Tino Dai  wrote:

> Hi All,
>
> Hope the people in the US are having a nice Labor Day! I am looking for
> the source code
> for the pack/unpack functions found in the struct package. As of this
> email, I have tried a
> strings on the struct.pyc file. The inspection of the pyc file was hoping
> that I could find a
> stub to the source. I also looked directly at struct.py, with no success.
> Finally, I also tried
> downloading the source and grepping through the files, which didn't prove
> all that useful.
> Does anybody have any ideas on how I can find the source without having to
> go through
> the entire source tree file by file?
>
IIUC, there's a program called Cscope that might help. :-)
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


[Tutor] Pack/Unpack hacking

2009-09-06 Thread Tino Dai
Hi All,

Hope the people in the US are having a nice Labor Day! I am looking for
the source code
for the pack/unpack functions found in the struct package. As of this email,
I have tried a
strings on the struct.pyc file. The inspection of the pyc file was hoping
that I could find a
stub to the source. I also looked directly at struct.py, with no success.
Finally, I also tried
downloading the source and grepping through the files, which didn't prove
all that useful.
Does anybody have any ideas on how I can find the source without having to
go through
the entire source tree file by file?

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


Re: [Tutor] Simple Game

2009-09-06 Thread Eric Dorsey
Hi Corey,

If this is going to be a command line program, two things that immediately
come to mind for me are: validating user input and persistence. If you put
something like "(1) to attack, (2) to run" what if the user types "yes", or
"Y", or "9"? You'll want to make sure your program doesn't crash on wrong
user input. And as far as "saving" the game, will the user be able to pick
up where they left off(tracking user stats, progress in the game,
posessions, etc)? If so, you'll need some kind of persistence, Pickle-ing or
a SQLite database, etc.


On Sun, Sep 6, 2009 at 10:09 AM, Corey Richardson  wrote:

> So far, I can use tuples/lists/dictionary's, and define some functions, and
> a bit of other things.
> Would it be hard for me to make a simple text rpg game? Or is there
> something else I should know before I try that.
> ___
> 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] Simple Game

2009-09-06 Thread Corey Richardson
So far, I can use tuples/lists/dictionary's, and define some functions, 
and a bit of other things.
Would it be hard for me to make a simple text rpg game? Or is there 
something else I should know before I try that.

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


Re: [Tutor] Problem with complex strings as key for a shelve-Dictionary

2009-09-06 Thread Kent Johnson
On Sun, Sep 6, 2009 at 6:20 AM, markus kossner  wrote:
> Dear all,
> I have a Problem with complex strings as keys in a shelve Object:
>
> #!/usr/bin/python
> import shelve
> test=shelve.open('testshelve')
> test={
> 'CCN1c2c2/C(=C2/S/C(=N\c3ccc(Cl)cc3)N(C)C2=O)C1=O':int(0),
> 'CN1/C(=N\c2ccc(Cl)cc2)S/C(=C2\C(=O)Nc3c32)C1=O':int(20),
> 'c1cc2c(cc1)N(CCCl)C(=O)/C2=C1\S/C(=N/c2ccc(F)cc2)NC1=O':float(1),
> 'Cc1(/N=C2/NC(=O)/C(=C3\c4c(4)N(C)C3=O)S2)c1C':0.5,
> }

You just re-assigned test to be a plain dict, the result of
shelve.open() is lost. You want
test=shelve.open('testshelve')
test['CCN1c2c2/C(=C2/S/C(=N\c3ccc(Cl)cc3)N(C)C2=O)C1=O'] = int(0)
etc.


> This works fine as long as I keep the shelve opened.
> However, when I then want to open the shelve with another python script
> I get key errors, as the different keys are not found:

Because you are not actually using the shelve, you are throwing away
immediately.

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


Re: [Tutor] how to read and transmit/send only section of the image (sub-image)

2009-09-06 Thread Kent Johnson
On Sat, Sep 5, 2009 at 10:52 AM, Jojo Mwebaze  wrote:
> oooh that will be helpful,  kindly point me to how i can begin with getting
> a sub-image that has the same width as the
> original. That will be my starting point.

I don't know the details of FITS format, but I imagine it would be
something like this:
- read enough of the FITS header to figure out where the image data is
in the file
- seek to the start of the sub-image and read the image data
- construct a new header for the sub-image
- write the new header and data to a new file

This will require some understanding of FITS format, or a library that
supports the operations you need. You might find code in PyFITS that
will help.

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


Re: [Tutor] mapping/filtering a sequence

2009-09-06 Thread kevin parks
I actually find the map biz easier to get my head around than the list  
comp. I guess this makes it another good reason for me to be happy  
that map is apparently staying in after nearly being depreciated. I  
generally prefer list comp in every instance, but the idea of an if  
else construct within the conditional of a list comp really hurts my  
brain, but it is nice to see that, had map(), filter() and reduce()  
all gotten the boot, there is a way to do it with a list comp, opaque  
as it is.


Thanks to all who responded this.

-kp--





On Sep 6, 2009, at 5:29 AM, Mark Tolonen wrote:



"Douglas Philips"  wrote in message news:9ee00578-6af7-4c6c-9968-af5f25a00...@mac.com 
...

On 2009 Sep 5, at 12:22 PM, Mark Tolonen wrote:

As a list comp:


L=range(30,41)
[{38:34,40:39}.get(n,n) for n in L]

[30, 31, 32, 33, 34, 35, 36, 37, 34, 39, 39]



True, that is terse, but IMHO has maintainability issues. The  
mapping data structure and the method of transformation (.get())  
are tangled  in with the transformation itself.
Again, IMHO, unless this is a very short script, the maintenance   
should outweigh raw terseness.


This *was* a very short script.  My point was using a list comp.  If  
you want to nitpick, don't use lower-case L for variables either :^)


my_map = { 38: 34, 40: 39 }

def filter_item(item):
   return my_map.get(item, item)

L = [filteritem(n) for n in L]

Happy?

-Mark


___
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 deciding between python and ruby

2009-09-06 Thread kevin parks
Well, the only thing more annoying than off topic discussion is "meta"  
discussion on lists and folks passing judgement on other people's  
posts (ie. you should have x, your should not have Y... I shouldn't  
have this second doughnut, but you know what, i am gonna eat that  
*and* have a second cup of coffee). So i'll resist the temptation to  
reply to Luke in detail, who i did not realize was a moderator here.


In any case, I happen to have right in front of me, a brandy new  
spendy (in Korea anyway) copy of the Lutz book Learning Python 3rd  
Edition O'Reilly and if you look at "Part I Getting Started," the book  
begins with a very compelling section called "Why People Use Python"  
and it outlines:


Software quality
Development productivity
Program Portability
Support libraries
Component integration
Enjoyment

and the end of this chapter, pp. 18-19 has a section called: "How does  
python stack up to language x" which gives a bunch of comparisons to  
Ruby and others. Here he says:


[Python] is more mature and has a more readable syntax than Ruby.  
Unlike Ruby and Java, OOP is an option in Python — Python does not  
impose OOP on users or projects which might not apply.


Most of this has already been touched on in this thread ... But I  
would add that that very past point is a HUGE reason why i use python.  
For little pea-shooter scripts I don't want to have to know and apply  
advanced OOP concepts. Yet if you do know them and your project  
requires it... it is there. One of the criticism of Python by Ruby  
users is that it isn't as "purely OO" as Ruby is. It is up to you  
whether that is a bug or a feature. But you can go quite far in Python  
without being an OOP guru but i am not sure that is entirely true of  
Ruby. Check with Ruby users. Personally i don't want to have to know  
how to do Schenkerian analysis and set theory just to have my first  
piano lesson and play Mary had a little Lamb, and i don't want to  
learn Norwegian just to hear a Norwegian folk tale. I like to dig  
right in and Python is good for that for sure.


-k




On Sep 6, 2009, at 5:45 AM, Luke Paireepinart wrote:



ruby on the other is more 'implicit,' used more in web-apps (via  
ROR), emphasizes code-readability/beauty, and is more flexible (i.e.  
has more than one way of doing something).
You mean people actually like ruby's syntax?  I think Python's the  
prettiest language I've worked with syntactically.
I wouldn't call having multiple ways to do something "flexible".  I  
agree more with Python's philosophy, there should be one (and  
preferably only one) obvious way to do something.


@kevin parks:
> I am (and i am sure i will be over-ruled and shouted down for  
this), not entirely sure the tutor list
> is the appropriate place for this query. Places like  
comp.lang.python better serve this purpose.

This is a good point, you probably should have stopped there.

Yes, this is probably not the best place for this discussion.  I  
would like to see how it pans out, though, now that we're already 15  
posts in.  It's not very difficult to ignore a thread in your  
favorite e-mail client, perhaps you could just do that?  If these  
debates become commonplace I will probably help you yell at everyone  
to stop flooding the list, but the amount of such questions has  
always been quite low and never bothersome, and I've been on Tutor  
since 2005.

___
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] Problem with complex strings as key for a shelve-Dictionary

2009-09-06 Thread markus kossner
Dear all,
I have a Problem with complex strings as keys in a shelve Object:

#!/usr/bin/python
import shelve
test=shelve.open('testshelve')
test={
'CCN1c2c2/C(=C2/S/C(=N\c3ccc(Cl)cc3)N(C)C2=O)C1=O':int(0),
'CN1/C(=N\c2ccc(Cl)cc2)S/C(=C2\C(=O)Nc3c32)C1=O':int(20),
'c1cc2c(cc1)N(CCCl)C(=O)/C2=C1\S/C(=N/c2ccc(F)cc2)NC1=O':float(1),
'Cc1(/N=C2/NC(=O)/C(=C3\c4c(4)N(C)C3=O)S2)c1C':0.5,
}
print test['CCN1c2c2/C(=C2/S/C(=N\c3ccc(Cl)cc3)N(C)C2=O)C1=O']
==> script works without problems

This works fine as long as I keep the shelve opened.
However, when I then want to open the shelve with another python script
I get key errors, as the different keys are not found:

#!/usr/bin/python
import shelve
test=shelve.open('testshelve')
print test['CCN1c2c2/C(=C2/S/C(=N\c3ccc(Cl)cc3)N(C)C2=O)C1=O']
==> leads to key error

I tried raw strings and so on, but did not manage.
Any Ideas?
Cheers Markus

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