[Tutor] Pygame

2009-07-20 Thread Mazhar Hussain
 Hello All! My name is Mazhar Hussain, and I am new to python, in fact, I am
new to programming as a whole. I dont know if its the right list to talk
about this.
 I am really interested in 2d games, especially 2d games. I am also
interested in making games. I searched for a lot of programming languages
but I didnt like anyone of them, either they were too hard to learn or not
suitable to make games. But then I found Python. I had heard that it was
very easy to learn and great for making games, it also had a binding for SDL
called pygame. But the main games I want to create are: a pokemon clone, a
megaman battle network clone and a world of goo like game. I just want to
know if I can make these type of games with pygame(before learning python).
If it can then I'll start learning python right away but if it cant then I
think I may better find another language.
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Using insert method on a list matrix

2009-07-20 Thread Alan Gauld


Raj Medhekar cosmicsan...@yahoo.com wrote


I would like to know how I could use the insert method in a
List matrix


OK, A list matrix is just a list containing other lists.
TThere is nothing special about it, it is just an ordinary list.
You could do it in two steps:

firstList = M[0]
firstList.insert(0,'pod')

But it is just as easy to do it directly...


eg. for the matrix below

M=[[1,2,3], [3,2,1], [4,3,2]]

if wanted to insert the string 'pod' in list [0] before [1] in list [0] 
in M


You said it right the first time. You want to insert 'pod' into list[0], ie 
M[0]

So you must call the insert method of that object and specify the index
of the insertion there: 0 in your case.

M[0].insert(...)


M=[[1,2,3], [3,2,1], [4,3,2]]
M.insert([1][1], 'pod')


This makes no sense since insert() takes a single index
because lists (always!) have a single dimension. In M's case
it is a list of 3 elements, which elements just happen to be lists too.
Also insert does not expect the index to be in brackets.


[row[0] for row in M.insert(1, 'pod')]


insert() returns None so the loop in this comprehension breaks,
But you do get the insert syntax right this time. M should now
look like

M=[[1,2,3], 'pod', [3,2,1], [4,3,2]]



M.insert(1,'pod')
M

[[1, 2, 3], 'pod', 'pod', [3, 2, 1], [4, 3, 2]]


You got it right here too but you are inserting it into M not
into the first element of M.


M.insert[0](1,'pod')


So, one more time, it should be

M[0].insert(0,'pod')

HTH,

--
Alan Gauld
Author of the Learn to Program web site
http://www.alan-g.me.uk/ 



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


Re: [Tutor] Pygame

2009-07-20 Thread Alan Gauld


Mazhar Hussain yam.m...@gmail.com wrote


Hello All! My name is Mazhar Hussain, and I am new to python,


Welcome to the tutor list


new to programming as a whole. I dont know if its the right list to talk
about this.


Yes, we are here to help people learning Python, many of whom
are also learning programming. There are a set of tutorials on the
Python web site specifically designed for new programmers, here:

http://wiki.python.org/moin/BeginnersGuide/NonProgrammers

You will notice one of these is specifically targetted at games writers.


I am really interested in 2d games, especially 2d games. I am also
interested in making games.


I guess you might like games?

called pygame. But the main games I want to create are: a pokemon clone, 
a

megaman battle network clone and a world of goo like game.


I can't really help there, I've only heard of pokemon and never even seen 
it!

Buty Pygame can produce a lot of games and even standard python can
deliver simple interactive games. Unless you want the very fastest shoot
'em up type games Pygame should suffice. And if you do want the fastest
shoot 'em up you have an awful lot of learning to do first!! :-)

If it can then I'll start learning python right away but if it cant then 
I

think I may better find another language.


If you start learning python you will probably learn the fundamentals of
programming much faster than if you use abnother language. Even if
you then move to something else more hard core - like C++ -  you will
find the lessons you learned in Python still apply. Once you know one
programming language learning a new one is very much easier.

Finally, stick to Python v2 just now, Python v3 is not best suited to
beginners yet.

--
Alan Gauld
Author of the Learn to Program web site
http://www.alan-g.me.uk/ 



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


Re: [Tutor] python interpreter vs bat file

2009-07-20 Thread Dinesh B Vadhia
Running time in CMD and IDLE
- Program running time is about the same in both CMD and IDLE
- The programs take a long time to run NOT because of runaway processes that 
are using up memory
- The programs are being optimized with each successive generation to reduce 
resources and time but the limitations boil down to Python for-loops (within 
functions) and sorts (probably the subject of another note to Tutor).

IDLE masking program errors
- Could be but ...
- The programs work under IDLE and return the correct results
- At this point I decided to run the programs from a batch file

Batch file method
- Except for one program, all other programs work using the batch file method.  
- The program with the error is run under IDLE and combined at the end with the 
output of the batch file programs and correct results are returned.

Program memory use
- The program with the memory error uses a lot of memory but the data 
structures should fit into available memory as it does when run with IDLE

Use of DOS Start command
- I'll try out the /I, /B and /WAIT commands in the next run and will let you 
know what happens.  Thanks.

Dinesh




Message: 1
Date: Sun, 19 Jul 2009 23:22:47 +0100
From: Alan Gauld alan.ga...@btinternet.com
To: tutor@python.org
Subject: Re: [Tutor] python interpreter vs bat file
Message-ID: h406bp$jd...@ger.gmane.org
Content-Type: text/plain; format=flowed; charset=iso-8859-1;
reply-type=original


Dinesh B Vadhia dineshbvad...@hotmail.com wrote 

 Bob Gailer suggested running the Python programs individually 
 in CMD one after the other.  This is sensible but my test programs 
 run for days and the full suite of programs take longer.  

OK, But it can't take longer than in IDLE? Or even in the bat file.
So you can start the program running and then iconify it.

The reason this is important is that IDLE catches some errors 
that the normal python interpreter does not So IDLE may be 
masking a real problem in your code. However...

 The programs are memory intensive (the 64-bit machine 
 has 8gb ram).  Hence, it is not easy to test this scenario 
 right now.

Have you chedked in Task Manager how much RAM the python 
programs use up - they should be visible in the process tab.

If it is a lot then maybe we can rewrite the code to use less 
memory (Or maybe leak less memory).

 It seems to me as if Windows is not freeing up memory 
 between Python invocations in the batch file but can't be 
 sure.  

Windows should free up the memory, but it might depend on 
how you run the programs. In your earlier post you said the 
bat file contained lines like

python foo.py
python bar.py

You could try usng the start command instead, as in:

start foo.py

You might want to explore the /I, /B and /WAIT options

start gives you a lot more control over the execution environment.

Notice you don;t need the 'python' because start uses the file 
association.

HTH,


-- 
Alan Gauld
Author of the Learn to Program web site
http://www.alan-g.me.uk/



--

Message: 2
Date: Sun, 19 Jul 2009 23:36:03 +0100
From: Alan Gauld alan.ga...@btinternet.com
To: tutor@python.org
Subject: Re: [Tutor] hitting a wall (not a collision detection
question :P)
Message-ID: h4074l$l0...@ger.gmane.org
Content-Type: text/plain; format=flowed; charset=iso-8859-1;
reply-type=original

Michael punkbohem...@yahoo.com wrote
 ...everything up to functions vs. methods and the basics of classes
 and OOP. This is where I'm hitting a wall. It's at this point the all the
 books go off in different directions

OK, First thing is don;t worry about it, you are far from alone.
Many, Many programmers (even long term pros) find the transition
from functions to objects really hard to adjust to. Not surprising,
since it doers require a new way of thinking about program
structure. Eventually the OOP way will become second nature,
in fact you might even find it hard to think about ordinary functions
after a while! But it can take a while.

 and I'm not sure a) what I'm learning, b) why I'm learning it,
 and c) how this is going to help me get to my goals.

It might be good to throw us some specific questions and we can
try to answer them. General questions tend to produce vague
answers!

You can try my tutorial on OOP to see if that helps. Follow it
up with the case study to see OOP in action.

 I'm not really even understanding much of what these books
 are talking about at this point anyway.

Again, anything you are unsure about tell us and we can try to
explain. That isd what this klist is really good at because there
are many different perspectives who have all gone through
the same learning curve. Someone likely has the same way if
thinking about it as you do!

 It's like a few chapters after Classes and OOP were torn out of all of 
 them.

:-)

 So, I'm just wondering what I should be doing at this point.

Start writing code. Don't worry about OOP 

Re: [Tutor] Pygame

2009-07-20 Thread bhaaluu
Python Programming for the Absolute Beginner Second Edition.
Michael Dawson.
Boston, MA: Thomson Course Technology, 2006.
ISBN-13: 978-1-59863-112-8
ISBN-10: 1-59863-112-8

No experience required to learn Python programming.
This book will teach you the basics of Python programming
through simple game creation. You'll learn to:
Build, slice, and index strings.
Work with functions,
Read from and write to text files,
Create and manipulate sprites,
Tackle object-oriented programming,
Create a GUI,
and work sound, music, and create animation.

The book comes with a CD that has
Python 2.3.5
PyGame 1.6
LiveWires 2.0 --customized version for this book
Source code and projects from the book.

If you can make it through Dawson's book, you'll be prepared
to tackle PyGame tutorials such as:

Game Programming by Andy Harris (ISBN-13: 978-0-470-06822-9)
http://www.cs.iupui.edu/~aharris/pygame/

or

Beginning Game Development with Python and Pygame:
From Novice to Professional by Will McGugan (ISBN-13: 978-1590598726)

The PyGame site also has beginnner tutorials.
http://www.pygame.org/docs/

Look for Michael Dawson's book, and start programming text-based
games in Python from the very first chapter! By the end of the book,
you'll be programming 2D arcade-style games. (447 pages)

Hopefully helpful.
-- 
b h a a l u u at g m a i l dot c o m
Gnu/Linux IS user-friendly.
It's NOT ignorant-friendly or idiot-friendly.

On Mon, Jul 20, 2009 at 3:04 AM, Mazhar Hussainyam.m...@gmail.com wrote:
  Hello All! My name is Mazhar Hussain, and I am new to python, in fact, I am
 new to programming as a whole. I dont know if its the right list to talk
 about this.
  I am really interested in 2d games, especially 2d games. I am also
 interested in making games. I searched for a lot of programming languages
 but I didnt like anyone of them, either they were too hard to learn or not
 suitable to make games. But then I found Python. I had heard that it was
 very easy to learn and great for making games, it also had a binding for SDL
 called pygame. But the main games I want to create are: a pokemon clone, a
 megaman battle network clone and a world of goo like game. I just want to
 know if I can make these type of games with pygame(before learning python).
 If it can then I'll start learning python right away but if it cant then I
 think I may better find another language.

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


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


Re: [Tutor] Pygame

2009-07-20 Thread David
Mazhar,

bhaaluu wrote:
 Python Programming for the Absolute Beginner Second Edition.
 Michael Dawson.
 Boston, MA: Thomson Course Technology, 2006.
 ISBN-13: 978-1-59863-112-8
 ISBN-10: 1-59863-112-8
This is, in my judgment, a good suggestion, provided you are working on
a Windows system. Otherwise some of the tools Dawson introduces will not
work.
So, start here, and move on to McGugan's book thereafter.

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


Re: [Tutor] Pygame

2009-07-20 Thread bhaaluu
On Mon, Jul 20, 2009 at 6:00 AM, Davidld...@gmx.net wrote:
 Mazhar,

 bhaaluu wrote:
 Python Programming for the Absolute Beginner Second Edition.
 Michael Dawson.
 Boston, MA: Thomson Course Technology, 2006.
 ISBN-13: 978-1-59863-112-8
 ISBN-10: 1-59863-112-8
 This is, in my judgment, a good suggestion, provided you are working on
 a Windows system. Otherwise some of the tools Dawson introduces will not
 work.
 So, start here, and move on to McGugan's book thereafter.

 David


I was able to work through Dawson's entire book on a Gnu/Linux system,
using X (Window system), Konsole, and the vim plain text editor.

Dawson uses a customized version of LiveWires towards the end of the book.
You must install the customized version for Dawson's examples to work
with Gnu/Linux.

I've worked through some of the source code for McGugan's book.
It seems to lean more towards 3D programming than 2D programming.
The Andy Harris book is geared to 2D programming in PyGame, and is
a good beginner introduction to PyGame.

You should already know some Python before reading Harris' book
because he doesn't really give a Python programming tutorial.
So, Dawson -- Harris -- McGugan is a good syllabus for Game
Programming in Python/PyGame.
-- 
b h a a l u u at g m a i l dot c o m
Gnu/Linux IS user-friendly.
It's NOT ignorant-friendly or idiot-friendly.
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Pygame

2009-07-20 Thread Wayne
On Mon, Jul 20, 2009 at 2:04 AM, Mazhar Hussain yam.m...@gmail.com wrote:

  Hello All! My name is Mazhar Hussain, and I am new to python, in fact, I
 am new to programming as a whole. I dont know if its the right list to talk
 about this.
  I am really interested in 2d games, especially 2d games. I am also
 interested in making games. I searched for a lot of programming languages
 but I didnt like anyone of them, either they were too hard to learn or not
 suitable to make games. But then I found Python. I had heard that it was
 very easy to learn and great for making games, it also had a binding for SDL
 called pygame. But the main games I want to create are: a pokemon clone, a
 megaman battle network clone and a world of goo like game. I just want to
 know if I can make these type of games with pygame(before learning python).
 If it can then I'll start learning python right away but if it cant then I
 think I may better find another language.


Take a look at these games:
http://www.pyweek.org/all_games/

I believe this one:
http://www.pyweek.org/e/gamebyalex2/

was written with the pyglet library.

I'd say it'd be a good challenge to write the pokemon clone. The battles
part would probably not take you too long. If you devote around an hour a
day to learning and an hour working on your project, I'd guess you could be
done with the battle part in around six months, if you're working on the
project solo.

Of course having the tutor list to help you when you run into problems will
help you reach that mark ;)

I'd recommend writing some sort of text-menu based version first, and then
extending that into a graphical version. If you get your code properly
broken up into functions and classes (which you'll learn more about later)
it should be fairly simple to wrap the graphics around it.

Anyway, others have also offered great advice, and Alan Gauld's tutorial is
one of the many high-quality online tutorials for learning to program,
specifically in python.

Another great thing about the pyweek games is when you download them you can
examine the source code so you can see exactly how many lines of code went
into writing the games.

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


[Tutor] I need help to the Python blackjack code

2009-07-20 Thread GAONEO

Hi

 

This is the first code that I have programed. I think there is something 
wrong with it. It is not a loop and computer seems wont bust. In the game, 

 

ace will be fixed as low (value=1). And can you write documentation comments 
for the first 39lines. 

 

kind regards

 

Neo Gao

_
您可以借助 Windows Live 整理、编辑和共享您的照片。
http://www.microsoft.com/china/windows/windowslive/products/photo-gallery-edit.aspxfrom random import *
from math import *

#GLOBAL VARIABLES

cards = range(0,52)

def randRange(in_lower,in_upper):
 generates a random number between in_lower and in_upper
temp_range = in_upper - in_lower
return int(round((temp_range)*random() + (in_lower)))


def popRandArray(in_list):
return in_list.pop(randRange(0,len(in_list)-1))

def realDealCard():

global cards
if len(cards)==0:
print new deck
cards = range(0,52)
return popRandArray(cards)


def cardAsString(in_card):

value = 
[ace,two,three,four,five,six,seven,eight,nine,ten,jack,queen,king]
suit = [hearts,diamonds,spades,clubs]
return value[in_card%13]+  of  + suit[in_card/13]



def cardScore(in_card):

score = in_card%13+1
if score  10:
score = 10
return score

print $Blackjack$

#~ player is delt with two cards
player_card1 = realDealCard()
player_card2 = realDealCard()

#~ show player the two cards
print your card1 is, cardAsString(player_card1)
print your card2 is, cardAsString(player_card2)

#~ count score of player
player_score = cardScore(player_card1) + cardScore(player_card2)

#~ show playerthe score
print your score is, player_score

#~ computer is delt with two cards
computer_card1 = realDealCard()
computer_card2 = realDealCard()

#~ show player one of the two cards
print The card1 of computer is, cardAsString(computer_card1)

#~ count score of computer
computer_score = computer_card1+computer_card2

#~ ask players action
while True:
player_action = str(raw_input(twist (t) or stick (s)?))

#~ if player chooses twist
if player_action == t : 
#~ player is delt with one more card
player_card3 = realDealCard()
#~ show player the third card
print your card3 is, cardAsString(player_card3)
#~ count current score of player
player_score += cardScore(player_card3)
#~ show player current score
print your score is, player_score
#~ check bust
#~ if current score of player  21
if player_score  21 :
#~ bust
print you bust
#~ player lose
print you lose and computer wins

#~ elif current score of player == 21
elif player_score == 21 :
#~player has a blackjack
print blackjack!
#~win
print you win and computer loses

#~ elif player chooses stick
elif player_action == s :
#~ dealers turn
print you choose stick
print It is computers turn

#~ if first score of computer = 18
if computer_score =18 :
#~ computer chooses twist
print computer twist
#~computer is delt one more card
computer_card3 = realDealCard()
computer_score += cardScore(computer_card3)
#~ check bust
#~ if current score of computer  21
if computer_score  21 :
#~computer bust
print computer score is, 
computer_score
print computer bust and You win
elif computer_score == player_score :
print computer score is, 
computer_score
print draw- No winner

#~ elif first two score  18
elif computer_score  18 :
#~computer choose stick
print computer stick
print computer score is, computer_score
#~compare score
if computer_score  player_score :
print you win and computer loses
elif computer_score  player_score :
print you lose and computer wins


Re: [Tutor] hitting a wall (not a collision detection question :P)

2009-07-20 Thread Kent Johnson
On Sun, Jul 19, 2009 at 4:34 PM, Michaelpunkbohem...@yahoo.com wrote:

 I've been progressing steadily, until now. At this point, I have a pretty
 solid understanding of strings, integers, tuples, lists, dictionaries, etc.
 and everything up to functions vs. methods and the basics of classes and
 OOP. This is where I'm hitting a wall. It's at this point the all the books
 go off in different directions and I'm not sure a) what I'm learning, b) why
 I'm learning it, and c) how this is going to help me get to my goals. I'm
 not really even understanding much of what these books are talking about at
 this point anyway. It's like a few chapters after Classes and OOP were
 torn out of all of them.

 So, I'm just wondering what I should be doing at this point.

Maybe you are ready to try a simple game? You could try the pygame
tutorial, see if you get an idea for a simple game you can work on.

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


Re: [Tutor] I need help to the Python blackjack code

2009-07-20 Thread Wayne
2009/7/20 GAONEO gys1...@hotmail.com

  Hi

 This is the first code that I have programed. I think there is
 something wrong with it. It is not a loop and computer seems wont bust. In
 the game,

 ace will be fixed as low (value=1). And can you write documentation
 comments for the first 39lines.



 your score is 87
you bust
you lose and computer wins
twist (t) or stick (s)?s
you choose stick
It is computers turn
computer stick
computer score is 63
you win and computer loses
twist (t) or stick (s)?

Evidently you don't stop on either your bust or the computer bust. Maybe you
should try writing out in more or less plain language how a game of
blackjack works.

What happens first? What happens next? In the casino if the deal has a hard
OR soft 17, it always stands (or sticks in your program). See if you can
make the computer stop once it reaches 17. Also see if you can make the
program stop once the user makes it to 21.

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


[Tutor] Thanks!

2009-07-20 Thread Mazhar Hussain
Thanks for all your help guys! I will try to buy all those books so that if
I find something in one book not understandable, I will look for it in
another book. Again Thanks. This list really is Helpful!
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Using insert method on a list matrix

2009-07-20 Thread Raj Medhekar
Thanks that works! But what if I wanted to modify 2 of the three lists in 2 
different positions? Could you please let me know how I would go about doing 
that?

Thanks!
-Raj





From: Ken Oliver ksterl...@mindspring.com
To: Raj Medhekar cosmicsan...@yahoo.com; Python Tutor tutor@python.org
Sent: Sunday, July 19, 2009 8:16:11 PM
Subject: Re: [Tutor] Using insert method on a list matrix

try 
 
M[0].insert(0, 'pod')



-Original Message-

From: Raj Medhekar 

Sent: Jul 19, 2009 7:12 PM

To: Python Tutor 

Subject: [Tutor] Using insert method on a list matrix




I would like to know how I could use the insert method in a List matrix eg. 
for the matrix below

M=[[1,2,3], [3,2,1], [4,3,2]]

if wanted to insert the string 'pod' in list [0] before [1] in list [0] in M 
using the built in method insert How would I go about doing this? I've tried 
may List Comprehension possibilities but none of then worked. Below are some 
of the things I tried. 

 M=[[1,2,3], [3,2,1], [4,3,2]]
 M.insert([1][1], 'pod')

Traceback (most recent call last):
  File pyshell#19, line 1, in module
M.insert([1][1], 'pod')
IndexError: list index out of range
 [row[0] for row in M.insert(1, 'pod')]

Traceback (most recent call last):
  File
 pyshell#20, line 1, in module
[row[0] for row in M.insert(1, 'pod')]
TypeError: 'NoneType' object is not iterable
 M.insert(1,'pod')
 M
[[1, 2, 3], 'pod', 'pod', [3, 2, 1], [4, 3, 2]]
 M.insert[0](1,'pod')

Traceback (most recent call last):
  File pyshell#23, line 1, in module
M.insert[0](1,'pod')
TypeError: 'builtin_function_or_method' object is unsubscriptable


Any help is greatly appreciated! Thanks!

-Raj



 .


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


[Tutor] advice on creating and working with a complex data structure

2009-07-20 Thread Serdar Tumgoren
Hi everyone,
I'm working with a database of campaign finance filings, and I'm
trying to create a data structure that captures the fact that:
  - there can be multiple races
  - races can have multiple candidates
  - candidates can have multiple campaign committees
  - campaign committees can file multiple reports

I'm trying to pull these records from a database and output them to an
HTML file in sorted order.

Here's an example of the data I'll be pulling:

records_from_db = (
  # name, party, cmte_id, state, district, risk_rating,
filing_type, filing_num
  ['Mike Brown','Rep','C0035820','FL','15','tossup','F3','8183'],
  ['Mike Brown','Rep','C0035820','FL','15','tossup','F3','8149'],
  ['Susan Miller','Dem','C0013802','FL','15','tossup','F3','2180'],
)

I was leaning toward creating a nested data structure like this:

races={'FL-15':
 {'status':'tossup',
  'candidates':
 {'Susan Miller':
{'party':'Dem',
 'cmtes':
   {'C0013802':
 {'2180':'F3'},
 }, # end cmtes
   }, #end Susan Miller
  'Mike Brown':
{'party':'Rep',
 'cmtes':
   {'C0035820':
 {'8183':'F3',
  '2810':'F3'},
'C0015823':
 {'3900':'F3X'},
   }, #end cmtes
 }, #end Mike Brown
}, #end Candidates
 }, #End FL-15
} #end races dict

Once I've created the data object,  I'd like to group the output by
race, party, candidate, committee and filing, so that when I output in
my template, it appears like this:

FL-15:
  Dem
Susan Miller
  C0035820
2180
  Rep
Mike Brown
  C0035820
  8149
  8183

I've been looking at Cookbook examples that rely on the setdefault
method to add key:value pairs to a dictionary, but I wasn't sure how
to implement that approach for a data structure with so many layers of
nesting.

Based on all of the above, I had a few questions:
* Is the best approach to create some type of a Race class that can
store all these varying data points for each race?
* Should I be using some type of recursive function to handle all the
levels of nesting?
* Given the above data structure, what sorting approach is most appropriate?

And of course, please let me know if there's a simpler approach I'm
overlooking that would meet my requirements.

As always, the advice is appreciated.

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


Re: [Tutor] advice on creating and working with a complex data structure

2009-07-20 Thread Lucas P Melo

Serdar Tumgoren wrote:

And of course, please let me know if there's a simpler approach I'm
overlooking that would meet my requirements.

As always, the advice is appreciated.
You could just create some classes with methods to handle the insertion 
and deletion of those data.

You could also keep a 'dict' to get objects given a string (or int).

I hope it helps.

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


Re: [Tutor] Using insert method on a list matrix

2009-07-20 Thread Lie Ryan
Raj Medhekar wrote:
 Thanks that works! But what if I wanted to modify 2 of the three lists
 in 2 different positions? Could you please let me know how I would go
 about doing that?

M[0].insert(2, 'foo')
M[2].insert(1, 'bar')

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


Re: [Tutor] advice on creating and working with a complex data structure

2009-07-20 Thread Alan Gauld


Serdar Tumgoren zstumgo...@gmail.com wrote 


I'm working with a database of campaign finance filings, and I'm
trying to create a data structure that captures the fact that:
 - there can be multiple races
 - races can have multiple candidates
 - candidates can have multiple campaign committees
 - campaign committees can file multiple reports


Have you considered using classes? This looks like a fairly 
natural fit for race, candidate, committee and maybe report 
casses



I'm trying to pull these records from a database and output them to an
HTML file in sorted order.


Each class could have a render method that retuirns 
an HTML fragment as a  string...



Once I've created the data object,  I'd like to group the output by
race, party, candidate, committee and filing, so that when I output in
my template, it appears like this:


You could provide support for a sort method that would
do the grouping for yopu...


I've been looking at Cookbook examples that rely on the setdefault
method to add key:value pairs to a dictionary, but I wasn't sure how
to implement that approach for a data structure with so many layers of
nesting.


One level at a time!
But with classes there would be less nesting and more relatinships


* Is the best approach to create some type of a Race class that can
store all these varying data points for each race?


I think you want a lot more than a Race class, see above.



* Should I be using some type of recursive function to handle all the
levels of nesting?


Probably not, a simple inter-bject relationship will make traversal 
much easier



And of course, please let me know if there's a simpler approach I'm
overlooking that would meet my requirements.


I think creating more objects and getting each object to handle 
fetching the stuff from its next level down would be simpler


HTH,


--
Alan Gauld
Author of the Learn to Program web site
http://www.alan-g.me.uk/

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


Re: [Tutor] advice on creating and working with a complex data structure

2009-07-20 Thread Serdar Tumgoren
 Have you considered using classes? This looks like a fairly natural fit for
 race, candidate, committee and maybe report casses

Aha, okay, the multiple classes approach makes sense.  But would these
be nested classes, perhaps inheriting attributes from their parent
classes? E.g.:

class Race

class Candidate

class Committee

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


Re: [Tutor] advice on creating and working with a complex data structure

2009-07-20 Thread bob gailer

Serdar Tumgoren wrote:

Hi everyone,
I'm working with a database of campaign finance filings, and I'm
trying to create a data structure that captures the fact that:
  - there can be multiple races
  - races can have multiple candidates
  - candidates can have multiple campaign committees
  - campaign committees can file multiple reports

I'm trying to pull these records from a database and output them to an
HTML file in sorted order.
I suggest using the ORDER BY SQL Clause rather than trying to sort it in 
Python.


[snip]
--
Bob Gailer
Chapel Hill NC
919-636-4239
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] sftp get single file

2009-07-20 Thread Matt Herzog
On Fri, Jul 17, 2009 at 12:12:52PM -0400, Kent Johnson wrote:
 On Fri, Jul 17, 2009 at 11:42 AM, Sander Sweerssander.swe...@gmail.com 
 wrote:
 
  import time
 
  today = time.localtime()
  datestr = time.strftime(%Y%m%d,today)
  ext = .tab
 
  print datestr + ext
 
 You can include literal characters in the format string:
 
 In [4]: time.strftime(%Y%m%d.tab,today)
 Out[4]: '20090717.tab'

That does work and is compact while being intelligible.
I'm still not getting the file. I thought this would work:

if __name__ == __main__:
t = paramiko.Transport((hostname, port))
t.connect(username=username, password=password)
sftp = paramiko.SFTPClient.from_transport(t)
sftp(remotepath,localpath)
t.close()

 . . . but obviously something is missing. 

Traceback (most recent call last):
  File ./scpgetter.py, line 20, in ?
  sftp(remotepath,localpath)
  TypeError: 'SFTPClient' object is not callable

I tried using the paramiko.SFTP.get method too. Failed.

-- Matt H


 
 Kent

-- 
I fear you speak upon the rack,
Where men enforced do speak anything.

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


Re: [Tutor] sftp get single file

2009-07-20 Thread Sander Sweers
I do not know paramiko but looking over the client documentations...

2009/7/20 Matt Herzog m...@blisses.org:
 if __name__ == __main__:
t = paramiko.Transport((hostname, port))
t.connect(username=username, password=password)
sftp = paramiko.SFTPClient.from_transport(t)

Up to here it looks fine.

sftp(remotepath,localpath)

If I understand it correctly you need to use sftp.get(remotepath, localpath).

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


Re: [Tutor] sftp get single file

2009-07-20 Thread Matt Herzog
On Mon, Jul 20, 2009 at 10:22:37PM +0200, Sander Sweers wrote:
 I do not know paramiko but looking over the client documentations...
 
 2009/7/20 Matt Herzog m...@blisses.org:
  if __name__ == __main__:
 t = paramiko.Transport((hostname, port))
 t.connect(username=username, password=password)
 sftp = paramiko.SFTPClient.from_transport(t)
 
 Up to here it looks fine.
 
 sftp(remotepath,localpath)
 
 If I understand it correctly you need to use sftp.get(remotepath, localpath).

Yeah that's exactly what I have tried, and the error is:

Traceback (most recent call last):
  File ./scpgetter.py, line 20, in ?
  sftp.get(remotepath, localpath)
File build/bdist.linux-x86_64/egg/paramiko/sftp_client.py, line 584, 
in get
  File build/bdist.linux-x86_64/egg/paramiko/sftp_client.py, line 
240, in open
File build/bdist.linux-x86_64/egg/paramiko/sftp_client.py, line 
609, in _request
  File build/bdist.linux-x86_64/egg/paramiko/sftp_client.py, line 
656, in _read_response
File build/bdist.linux-x86_64/egg/paramiko/sftp_client.py, 
line 682, in _convert_status
IOError: (2, '')

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

-- 
I fear you speak upon the rack,
Where men enforced do speak anything.

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


Re: [Tutor] sftp get single file

2009-07-20 Thread Sander Sweers
2009/7/20 Matt Herzog m...@blisses.org:
 Traceback (most recent call last):
  File ./scpgetter.py, line 20, in ?
      sftp.get(remotepath, localpath)
        File build/bdist.linux-x86_64/egg/paramiko/sftp_client.py, line 584, 
 in get
          File build/bdist.linux-x86_64/egg/paramiko/sftp_client.py, line 
 240, in open
            File build/bdist.linux-x86_64/egg/paramiko/sftp_client.py, line 
 609, in _request
              File build/bdist.linux-x86_64/egg/paramiko/sftp_client.py, 
 line 656, in _read_response
                File build/bdist.linux-x86_64/egg/paramiko/sftp_client.py, 
 line 682, in _convert_status
                IOError: (2, '')

Ah, now it get interesting. Genrally IOError means the file is not
found. And looking at the source for _convert_status it is axactly
that. The file you are trying to get is not found on the server so
check your remotepath.

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


Re: [Tutor] sftp get single file

2009-07-20 Thread Matt Herzog
On Mon, Jul 20, 2009 at 11:02:52PM +0200, Sander Sweers wrote:
 2009/7/20 Matt Herzog m...@blisses.org:
  Traceback (most recent call last):
  ??File ./scpgetter.py, line 20, in ?
  ?? ?? ??sftp.get(remotepath, localpath)
  ?? ?? ?? ??File build/bdist.linux-x86_64/egg/paramiko/sftp_client.py, 
  line 584, in get
  ?? ?? ?? ?? ??File build/bdist.linux-x86_64/egg/paramiko/sftp_client.py, 
  line 240, in open
  ?? ?? ?? ?? ?? ??File 
  build/bdist.linux-x86_64/egg/paramiko/sftp_client.py, line 609, in 
  _request
  ?? ?? ?? ?? ?? ?? ??File 
  build/bdist.linux-x86_64/egg/paramiko/sftp_client.py, line 656, in 
  _read_response
  ?? ?? ?? ?? ?? ?? ?? ??File 
  build/bdist.linux-x86_64/egg/paramiko/sftp_client.py, line 682, in 
  _convert_status
  ?? ?? ?? ?? ?? ?? ?? ??IOError: (2, '')
 
 Ah, now it get interesting. Genrally IOError means the file is not
 found. And looking at the source for _convert_status it is axactly
 that. The file you are trying to get is not found on the server so
 check your remotepath.

The file is there. I can sftp it using fugu.

The path is /20090720.tab since the file lives in a jail.

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


Re: [Tutor] sftp get single file

2009-07-20 Thread Sander Sweers
2009/7/20 Matt Herzog m...@blisses.org:
 The file is there. I can sftp it using fugu.

I am sure it is but paramiko can't find it.

 The path is /20090720.tab since the file lives in a jail.

Issue a sftp.listdir() and see what paramiko sees on the remote server.

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


Re: [Tutor] sftp get single file

2009-07-20 Thread Matt Herzog
If I change

remotepath = 'datestr'

to 

remotepath = datestr

I get:

sftp.get(remotepath, localpath)
  File build/bdist.linux-x86_64/egg/paramiko/sftp_client.py, line 587, in get
  IOError: [Errno 21] Is a directory: '/tmp/testor/'


So remotepath is really more like a path + filname.

So I need to do something like os.join the two?


-- 
I fear you speak upon the rack,
Where men enforced do speak anything.

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


[Tutor] paramiko list user said,

2009-07-20 Thread Matt Herzog
A user of the paramiko mailing list said,

Paramiko has an SFTPClient class and an SSHClient that can be used to  
  transfer files, why complicate it by using a Transport directly. The  
   easiest thing is to open an SSHClient:   
c=paramiko.SSHClient()  
 c.connect(username=username_, password=password_, 
hostname=hostname_)s=c.open_sftp()  
s.get/put   
 etc..   

He lost me on the s.get/put part. I suppose he means s.get(remotepath, 
localpath)
 . . . or something.



-- 
I fear you speak upon the rack,
Where men enforced do speak anything.

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


Re: [Tutor] sftp get single file

2009-07-20 Thread Sander Sweers
2009/7/20 Matt Herzog m...@blisses.org:
 remotepath = 'datestr'

Ok, you are now making a string.

 remotepath = datestr

Like Kent wrote the datestr can include other characters. So I would
try /%Y%m%d.tab.

 sftp.get(remotepath, localpath)
  File build/bdist.linux-x86_64/egg/paramiko/sftp_client.py, line 587, in get
  IOError: [Errno 21] Is a directory: '/tmp/testor/'

paramiko still can not find the file.

 So remotepath is really more like a path + filname.

Yes.

 So I need to do something like os.join the two?

You probably mean os.path.join(), you can use it but imho it is
overkill in your case. Just make sure you get the full path to the
file in your remotepath.

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


Re: [Tutor] sftp get single file

2009-07-20 Thread Sander Sweers
Please reply to the list.

2009/7/20 Matt Herzog m...@blisses.org:
 Yeah. I have no idea if I am able to do this. The jail makes it ambiguous.

There is no difference, the jail just moves the root directory as seen
by the client.

Did you do as suggested earlier to use listdir()? This will tell you
how paramiko sees the server.

I get the feeling you are in on over your head on this. Maybe you need
to read the paramiko docs and play around in idle or other interactive
python interpreter. The SFTPClient has all the basic commands any ssh
client has (listdir, chdir etc). See [1].

Also have a look at [2] which is a good example of what can be done.

Greets
Sander

[1] http://www.lag.net/paramiko/docs/paramiko.SFTPClient-class.html
[2] http://code.activestate.com/recipes/576810/
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


[Tutor] how to monitor if their data in a variable/object

2009-07-20 Thread Amit Sethi
Hi , I am writing a python gtk application in which I am required to
fetch a large amount of data from web , now i want to create a wait
dialog for the user to know that I am still fetching data .The
question is how can i monitor that the required data has been fetched
. I could have a variable that is given a particular value when data
has been fetched . But how can I   keep checking if the variable has
got a value or not.

-- 
A-M-I-T S|S
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] how to monitor if their data in a variable/object

2009-07-20 Thread Wayne
On Mon, Jul 20, 2009 at 5:01 PM, Amit Sethi amit.pureene...@gmail.comwrote:

 Hi , I am writing a python gtk application in which I am required to
 fetch a large amount of data from web , now i want to create a wait
 dialog for the user to know that I am still fetching data .The
 question is how can i monitor that the required data has been fetched
 . I could have a variable that is given a particular value when data
 has been fetched . But how can I   keep checking if the variable has
 got a value or not.


It really depends on how you're fetching the data. If you write your own
method to read a few kilobytes at a time, you can easily update a status
bar. Otherwise if you know the file it's downloading to (i.e. not a temp
file) you could continually call os.stat to update the status bar
(especially if you know the size it's supposed to be in the end).

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


Re: [Tutor] sftp get single file

2009-07-20 Thread Matt Herzog
On Mon, Jul 20, 2009 at 11:57:57PM +0200, Sander Sweers wrote:
 Please reply to the list.
 
 2009/7/20 Matt Herzog m...@blisses.org:
  Yeah. I have no idea if I am able to do this. The jail makes it ambiguous.
 
 There is no difference, the jail just moves the root directory as seen
 by the client.
 
 Did you do as suggested earlier to use listdir()? This will tell you
 how paramiko sees the server.

Yeah, I managed to get that to work like so:

if __name__ == __main__:
t = paramiko.Transport((hostname, port))
t.connect(username=username, password=password)
sftp = paramiko.SFTPClient.from_transport(t)
print sftp.listdir()
t.close()

It displays the contents of the dir no problem.

 
 I get the feeling you are in on over your head on this. 

Yes, and I have a deadline.

 Maybe you need
 to read the paramiko docs and play around in idle or other interactive
 python interpreter. The SFTPClient has all the basic commands any ssh
 client has (listdir, chdir etc). See [1].

I should not need to chdir, right? In fact, the server's policy forbids it.
Perhaps I need to chdir on the local host?

 Also have a look at [2] which is a good example of what can be done.

Yeah I saw that page. The code seemed bloated to me.

 
 Greets
 Sander
 
 [1] http://www.lag.net/paramiko/docs/paramiko.SFTPClient-class.html
 [2] http://code.activestate.com/recipes/576810/
 ___
 Tutor maillist  -  Tutor@python.org
 http://mail.python.org/mailman/listinfo/tutor

-- 
I fear you speak upon the rack,
Where men enforced do speak anything.

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


Re: [Tutor] sftp get single file

2009-07-20 Thread Wayne
On Mon, Jul 20, 2009 at 5:18 PM, Matt Herzog m...@blisses.org wrote:

 On Mon, Jul 20, 2009 at 11:57:57PM +0200, Sander Sweers wrote:
  Please reply to the list.
 
  2009/7/20 Matt Herzog m...@blisses.org:
   Yeah. I have no idea if I am able to do this. The jail makes it
 ambiguous.
 
  There is no difference, the jail just moves the root directory as seen
  by the client.
 
  Did you do as suggested earlier to use listdir()? This will tell you
  how paramiko sees the server.

 Yeah, I managed to get that to work like so:

 if __name__ == __main__:
t = paramiko.Transport((hostname, port))
t.connect(username=username, password=password)
sftp = paramiko.SFTPClient.from_transport(t)
 print sftp.listdir()
t.close()

 It displays the contents of the dir no problem.


Is the file present in the dir? What is it listed as? Is it just
07232009.tab (or similar) or is it /home/user/jail/07232009.tab? That may
make a difference.




 
  I get the feeling you are in on over your head on this.

 Yes, and I have a deadline.

  Maybe you need
  to read the paramiko docs and play around in idle or other interactive
  python interpreter. The SFTPClient has all the basic commands any ssh
  client has (listdir, chdir etc). See [1].

 I should not need to chdir, right? In fact, the server's policy forbids it.
 Perhaps I need to chdir on the local host?


That shouldn't make a difference as far as getting the file is concerned -
unless you're trying to put it in a file on your computer that doesn't
exist!

I'd really recommend playing around in the interpreter - especially if you
have ipython.

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


Re: [Tutor] advice on creating and working with a complex datastructure

2009-07-20 Thread Alan Gauld
Serdar Tumgoren zstumgo...@gmail.com wrote 


Aha, okay, the multiple classes approach makes sense.  But would these
be nested classes, 


No although they would probably have attributes containing
the related lists. Thus Race might have a list of Candidates.
And Candidate would have a list of Committees etc.


perhaps inheriting attributes from their parent
classes? E.g.:


I do not think any of these meet the criteria for inheritance. 
None of them is a kind of any of the others.



class Race

   class Candidate

   class Committee

   class Filing


I don;t like the use of Filing for a class name, it sounds like 
a collection and in Python we don;t usually need to crteate 
collection classes, we can just use a standard collection type. 
Maybe File would be better although it could be confused 
with a comuter file... Maybe Record would be best?


HTH

Alan G.

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


Re: [Tutor] sftp get single file

2009-07-20 Thread Matt Herzog
On Mon, Jul 20, 2009 at 05:26:09PM -0500, Wayne wrote:
 On Mon, Jul 20, 2009 at 5:18 PM, Matt Herzog m...@blisses.org wrote:
 
  On Mon, Jul 20, 2009 at 11:57:57PM +0200, Sander Sweers wrote:
   Please reply to the list.
  
   2009/7/20 Matt Herzog m...@blisses.org:
Yeah. I have no idea if I am able to do this. The jail makes it
  ambiguous.
  
   There is no difference, the jail just moves the root directory as seen
   by the client.
  
   Did you do as suggested earlier to use listdir()? This will tell you
   how paramiko sees the server.
 
  Yeah, I managed to get that to work like so:
 
  if __name__ == __main__:
 t = paramiko.Transport((hostname, port))
 t.connect(username=username, password=password)
 sftp = paramiko.SFTPClient.from_transport(t)
  print sftp.listdir()
 t.close()
 
  It displays the contents of the dir no problem.
 
 
 Is the file present in the dir? What is it listed as? Is it just
 07232009.tab (or similar) or is it /home/user/jail/07232009.tab? That may
 make a difference.

Yes, the file 20090720.tab (today's date dot tab) is present in the dir 
listing. The dir contents are printed out in a comma delimited list from left 
to right that wraps several times. There are probably 30 subdirs and a dozen 
files in that dir. If you use the sftp cli binary and type, 'pwd' it tells you, 
'/'.

 
 
 
 
  
   I get the feeling you are in on over your head on this.
 
  Yes, and I have a deadline.
 
   Maybe you need
   to read the paramiko docs and play around in idle or other interactive
   python interpreter. The SFTPClient has all the basic commands any ssh
   client has (listdir, chdir etc). See [1].
 
  I should not need to chdir, right? In fact, the server's policy forbids it.
  Perhaps I need to chdir on the local host?
 
 
 That shouldn't make a difference as far as getting the file is concerned -
 unless you're trying to put it in a file on your computer that doesn't
 exist!

I'm trying to put a file in a dir called /tmp/testor. The user the script runs 
as owns the dir.

 
 I'd really recommend playing around in the interpreter - especially if you
 have ipython.

I have ipython. I  like ipython. I just don't get enough opportunities to do 
python scripting.

 
 Good luck, HTH,
 Wayne

-- 
I fear you speak upon the rack,
Where men enforced do speak anything.

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