[Tutor] how do we represent left arrow key in python programming.

2009-08-21 Thread Ajith Gopinath
Hi Folks,
how do we represent left arrow key in a python program?
Can anybody help?
|| a j i t ||
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] how do we represent left arrow key in python programming.

2009-08-21 Thread Luke Paireepinart
you're gonna have to be a lot more specific than that if you want a decent
answer.

On Fri, Aug 21, 2009 at 4:45 AM, Ajith Gopinath  wrote:

> Hi Folks,
> how do we represent left arrow key in a python program?
> Can anybody help?
> || a j i t ||
>
> ___
> 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] how do we represent left arrow key in python programming.

2009-08-21 Thread Ajith Gopinath
Hi,
I just want to travese a text based menu.My prog.  will login to a text
based app. and traverse its menu  and clicks  where i want to be. rit now i
find it sifficult to traverse the menu.I am but able to click on the very
first item.Will this info help?
|| a j i t ||


On Fri, Aug 21, 2009 at 3:28 PM, Luke Paireepinart
wrote:

> you're gonna have to be a lot more specific than that if you want a decent
> answer.
>
> On Fri, Aug 21, 2009 at 4:45 AM, Ajith Gopinath wrote:
>
>> Hi Folks,
>> how do we represent left arrow key in a python program?
>> Can anybody help?
>> || a j i t ||
>>
>> ___
>> 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] how do we represent left arrow key in python programming.

2009-08-21 Thread Dave Angel

Ajith Gopinath wrote:

Hi Folks,
how do we represent left arrow key in a python program?
Can anybody help?
|| a j i t ||

  
Be more specific.  Are you writing your gui in wxpython, in gtk, in 
tkinter?  Or are you writing a console program?  In what Python version?


Are you looking to see how an arrow key shows up in a getch() call?   In 
raw_input()?  Or in some event in a gui program?


Are you running on a single platform (eg. Windows)?  Or must the 
approach work several places?


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


Re: [Tutor] Help on input error

2009-08-21 Thread dshunick
Changing the folder to site-packages did the trick. 

Thanks 
David Shunick 

- Original Message - 
From: "Kent Johnson"  
To: "David Shunick"  
Cc: tutor@python.org 
Sent: Wednesday, August 19, 2009 6:26:19 PM GMT -08:00 US/Canada Pacific 
Subject: Re: [Tutor] Help on input error 

On Wed, Aug 19, 2009 at 7:14 PM, David Shunick wrote: 
> I'm trying to learn Python, but keep running into the erroor message 
> ImportError: no module named area. 
> 
> I created a file area.py in IDLE. I'm using Python 3.1 and Window XP. After 
> saving the file in c:/Python31/Lib/Python Modules/area.py 
> 
> I can run the program using F5. But when I start a new shell, and enter 
> import area, that's when I get the error message. And it's not just this 
> program.I have the same problem with all that I try to import. 


"Python Modules" is not a folder recognized by Python. Try putting the 
file in Python31/Lib/site-packages/area.py 

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


Re: [Tutor] help cvs module writerow issue (Now Fixed)

2009-08-21 Thread David Jamieson
> Hi All,
>
> I am looking to make an append to csv file function more dynamic by
> allowing it to receive a variable number of arguments to write out to
> a single a csv file row. The funtion is an append to file function
> that writes out test date to a csv file. Reason for trying to make it
> dynamic is to allow for future expansion of data capture during a test
> run.
> I have an issue when trying to format the output to the
> csv.write(file).writerow() call to accept the variable number of
> arguments as individual cell values when written to the csv file. I
> have tried a few ways to escape the variable values but to no avail.
> When I check the csv file it has the timestamp in one cell on the row
> and the data in the second cell '1','2','3','4','5' Has this issue to
> do with the output_str being a str type and the writerow not
> interpreting it correctly. I added a number 2 to the writerow line to
> prove that I can write to an individual cell

>You seem to misunderstand writerow(). You don't have to escape the
>values yourself, the csv module will do that.

> function call
> =
> append_to_file(output_file_name,'1','2','3','4','5')
>
> file
> ==
> output_file_name = 'C:\\rubbish.csv'
>
> function def
> 
> def append_to_file(append_file_name,*values):
> #Open the test results file and append test data.
> output_str =' '
> file_out = open(append_file_name,'ab')
> #Creating a timestamp for the test results
> time = datetime.datetime.now()
> time_stamp = time.ctime()
> #Building a output str to follow the syntax required for writerow
> 'x','x','x'
> for x in values:
> output_str = output_str+"'"+x+"'"+","
> str_length = len(output_str)
> #removing the last , from the output_str variable
> output_str = output_str[0:(str_length-1)]

>You could write this more simply as
>output_str = ','.join("'"+x+"'" for x in values)
>but see below for what you really want.

> print "The final output string is ",output_str
> print "The output string is of type ",type(output_str)
> #list(output_str)
> csv.writer(file_out).writerow([time_stamp, output_str]+['2'])

>Here you are saying that you want to write a row with three elements -
>the time stamp, the string you constructed in output_str, and the
>string '2'. That's not really what you want - you want each value in a
>separate cell. Skip all the output_str stuff and just
>csv.writer(file_out).wrinerow([time_stamp] + list(values) + ['2'])

>Kent


Hi Kent,
thank you for your help in sorting out my writerow issue and for the
better way to join strings. I have the code applied to my project.

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


Re: [Tutor] Help on input error

2009-08-21 Thread Alan Gauld


 wrote

"Python Modules" is not a folder recognized by Python. Try putting the 
file in Python31/Lib/site-packages/area.py 


Although that solved your problem I tend to use sire-packages for third 
party modules I've downloaded - and can  download again.


For my own code I keep a separate Projects area outside my python 
installation and reference it via PYTHONPATH., That means if I delete 
a Python installation, including site-packages-oops! - or install a 
second one I can still access my modules or share thjem across both 
without copying.


Just a thought,


--
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] how do we represent left arrow key in python programming.

2009-08-21 Thread Alan Gauld


"Ajith Gopinath"  wrote 


how do we represent left arrow key in a python program?
Can anybody help?


That depends entirely on your terminal/environment.
If you are using a Unix terminal it will be diffeent to using 
a Mac or a PC.


You can write a program to display the key code of any 
key using curses on *nix or msvcrt on Windows. Here
is an example from my web tutor under the Event Driven 
Programming topic...


import msvcrt

def doKeyEvent(key):
   if key == '\x00' or key == '\xe0': # non ASCII
  key = msvcrt.getch() # fetch second character
   print ord(key),

def doQuitEvent(key):
   raise SystemExit


# First, clear the screen of clutter then warn the user 
# of what to do to quit

lines = 25 # set to number of lines in console
for line in range(lines): print

print "Hit space to end..."
print

# Now mainloop runs "forever"
while True:
  ky = msvcrt.getch()
  length = len(ky)
  if length != 0:
 # send events to event handling functions
 if ky == " ": # check for quit event
doQuitEvent(ky)
 else: 
doKeyEvent(ky)

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


[Tutor] get name of calling class at runtime

2009-08-21 Thread Serdar Tumgoren
Hi everyone,
I'm trying to create a data-retriever class that executes certain SQL
statements based on the name of a calling class (I'm doing this so I
can group a bunch of SQL statements in one place; they're currently
scattered all over the program and it's getting unwieldy).

Currently, I'm forced to pass in the name of the "caller" from the
calling class:

class DataSources(object):
def getdata(self, caller):
if caller == 'CallerA':
  # execute sql for callerA
elif caller == 'CallerB':
  #execute sql for callerB

class CallerA(object):
def getdata(self):
caller = self.__class__.__name__
dao = DataSources()
dao.getdata(caller)

class CallerB(object):
def getdata(self):
caller = self.__class__.__name__
dao = DataSources()
dao.getdata(caller)

So I'm wondering, is there any way to have the DataSources class
access the name of the calling class at runtime, and avoid having to
pass in the "caller" variable? Perhaps there's some other standard
approach to this kind of problem?
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] get name of calling class at runtime

2009-08-21 Thread Kent Johnson
On Fri, Aug 21, 2009 at 1:53 PM, Serdar Tumgoren wrote:
> Hi everyone,
> I'm trying to create a data-retriever class that executes certain SQL
> statements based on the name of a calling class (I'm doing this so I
> can group a bunch of SQL statements in one place; they're currently
> scattered all over the program and it's getting unwieldy).
>
> Currently, I'm forced to pass in the name of the "caller" from the
> calling class:
>
> class DataSources(object):
>    def getdata(self, caller):
>        if caller == 'CallerA':
>          # execute sql for callerA
>        elif caller == 'CallerB':
>          #execute sql for callerB
>
> class CallerA(object):
>    def getdata(self):
>        caller = self.__class__.__name__
>        dao = DataSources()
>        dao.getdata(caller)
>
> class CallerB(object):
>    def getdata(self):
>        caller = self.__class__.__name__
>        dao = DataSources()
>        dao.getdata(caller)
>
> So I'm wondering, is there any way to have the DataSources class
> access the name of the calling class at runtime, and avoid having to
> pass in the "caller" variable? Perhaps there's some other standard
> approach to this kind of problem?

You could simplify the code above by passing self to DataSources(). Or
make CallerX inherit from DataSources and use the class attribute
trick we talked about before. Or perhaps you should look into
SQLObject or SQLAlchemy?

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


Re: [Tutor] how do we represent left arrow key in python programming.

2009-08-21 Thread Eike Welk
On Friday 21 August 2009, Ajith Gopinath wrote:
> Hi,
> I just want to travese a text based menu.My prog.  will login to a
> text based app. and traverse its menu  and clicks  where i want to
> be. rit now i find it sifficult to traverse the menu.I am but able
> to click on the very first item.Will this info help?

You should look at the curses library for complex, but text based 
interfaces. I don't know if it exists on Windows though:
http://docs.python.org/library/curses.html

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


Re: [Tutor] get name of calling class at runtime

2009-08-21 Thread Serdar Tumgoren
> You could simplify the code above by passing self to DataSources(). Or
> make CallerX inherit from DataSources and use the class attribute
> trick we talked about before. Or perhaps you should look into
> SQLObject or SQLAlchemy?
>
The class attribute trick worked nicely. I'll think about how to apply
that here.

Meantime, how do I pass "self" to DataSources?

Do I literally just pass in the word "self" from inside the calling
object? Apologies for the confusion -- I've never used that technique
before and it's bending my brain a little:)
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] get name of calling class at runtime

2009-08-21 Thread Alan Gauld

"Serdar Tumgoren"  wrote


I'm trying to create a data-retriever class that executes certain SQL
statements based on the name of a calling class 


This is a really bad "smell" from an OO design point of view.


can group a bunch of SQL statements in one place; they're currently
scattered all over the program and it's getting unwieldy).


Normally in an OO program the SQL for each class is in the methods 
for that class. That way any changes to the class canbe easily reflected 
in the related SQL.



class DataSources(object):
   def getdata(self, caller):
   if caller == 'CallerA':
 # execute sql for callerA
   elif caller == 'CallerB':
 #execute sql for callerB


This is exactly the kind of code that OO and polymorphism tries to avoid.
It is one of the most error prone and non performant code patterns you 
can write.


Why not put the SQL for classA in classA?


class CallerA(object):
   def getdata(self):
   caller = self.__class__.__name__
   dao = DataSources()
   dao.getdata(caller)


Just put the SQL for A here.
Then if you add new classes you don't need to go and change 
your DataSources class as well. Thats what polymorphism is for.



So I'm wondering, is there any way to have the DataSources class
access the name of the calling class at runtime, and avoid having to
pass in the "caller" variable? Perhaps there's some other standard
approach to this kind of problem?


There are ways of doing what you want, but the "standard" way is 
to keep the code for class A in class A. Thats why its called 
Object Oriented programming.


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] get name of calling class at runtime

2009-08-21 Thread Alan Gauld


"Serdar Tumgoren"  wrote 


Meantime, how do I pass "self" to DataSources?

Do I literally just pass in the word "self" from inside the calling
object? Apologies for the confusion -- I've never used that technique
before and it's bending my brain a little:)


self is just a reference to the current object.
Thus when you write:

class C:
  def f(self): pass

and call it with

c = C()
c.f()

you are effectively calling 


C.f(c)

ie self takers on the value of the current instance.

Thus inside f() if you hasd a line

DataSources.getData(self)

you would pass the instance of C to DataSources.
(and you can use isInstance() to check its type).

But unless you have a really good reason its still better 
to get the class to do its own SQL. IMHO.


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


[Tutor] simple plots

2009-08-21 Thread questions anon
I would like to make some simple plots using matplotlib (or any python
plotting modules) and I can find lots of examples that generate random data
and then plot those, but I cannot find any that read in data from excel or a
text file, manipulate the data and then plot the data. Does anyone have any
examples for this or webpages I could go to?
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] get name of calling class at runtime

2009-08-21 Thread Kent Johnson
On Fri, Aug 21, 2009 at 6:58 PM, Alan Gauld wrote:
> "Serdar Tumgoren"  wrote
>
>> I'm trying to create a data-retriever class that executes certain SQL
>> statements based on the name of a calling class
>
> This is a really bad "smell" from an OO design point of view.
>
>> can group a bunch of SQL statements in one place; they're currently
>> scattered all over the program and it's getting unwieldy).
>
> Normally in an OO program the SQL for each class is in the methods for that
> class. That way any changes to the class canbe easily reflected in the
> related SQL.

But if all the classes need nearly the same SQL, it may make sense to
abstract that out to a common location.That is what Serdar is trying
to do. Duplication is also a bad smell.

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


Re: [Tutor] simple plots

2009-08-21 Thread Kent Johnson
On Fri, Aug 21, 2009 at 8:34 PM, questions anon wrote:
> I would like to make some simple plots using matplotlib (or any python
> plotting modules) and I can find lots of examples that generate random data
> and then plot those, but I cannot find any that read in data from excel or a
> text file, manipulate the data and then plot the data. Does anyone have any
> examples for this or webpages I could go to?

What form is the data? You can use the csv module to read comma- or
tab-separated values. You can use the third-party xlrd module or COM
to read Excel files. What kind of data manipulation do you need?
Python has many built-in facilities for that.

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


Re: [Tutor] simple plots

2009-08-21 Thread Che M


> I would like to make some simple plots using matplotlib (or any python 
> plotting 
> modules) and I can find lots of examples that generate random data and then 
> plot those, but I cannot find any that read in data from excel or a text 
> file, 
> manipulate the data and then plot the data. Does anyone have any examples 
> for this or webpages I could go to?

Keep in mind, too, that in terms of matplotlib, for simple plots all you need to
feed the plot() function is a list of x points and a list of y points.  
Sometimes
the demos on their web page might seem to obscure this for beginners because
of the fancy math functions that make pretty plots, but that's all you need:
two lists.

Getting those two lists of points is what you are really concerned about, as 
Kent indicated, and so this is not really a matplotlib concern.  And as he said,
Python has lots of good support for that. 

Che

_
Hotmail® is up to 70% faster. Now good news travels really fast. 
http://windowslive.com/online/hotmail?ocid=PID23391::T:WLMTAGL:ON:WL:en-US:WM_HYGN_faster:082009___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor