Re: [Tutor] Importing Excel sheet data

2007-09-06 Thread Steve Willoughby
saradhi dinavahi wrote:
> I am new to the Python Programming. I want to Import Excel sheet data 
> using Python. Can any one please provide me the code and explain the 
> basic steps and method of executing the code.

If you can get your Excel data into CSV format, the csv module others 
have already mentioned is a great way to go.  It makes it very easy to 
read and write CSV-format files, which can be used with other 
spreadsheets and other programs, too.

However, if you need to create or input actual Excel worksheets, which 
contain more than simple data tables like CSV supports, check out a 
third-party module called pyExcelerator.

http://sourceforge.net/projects/pyexcelerator

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


Re: [Tutor] Importing Excel sheet data

2007-09-06 Thread Kent Johnson
Ian Witham wrote:
> HI Saradhi,
> 
> I too am fairly new to Python, but I use the csv module successfully for 
> my work. Could you be a little more specific as to what your 
> requirements are and where you are finding difficulty?
> 
> Ian.
> 
> On 9/6/07, *saradhi dinavahi* <[EMAIL PROTECTED] 
> > wrote:
> 
> hi Ian ,
>  
> I have read the CSV module. But I felt difficult to write a code for
> my requirement.

For reading only there is also xlrd:
http://www.lexicon.net/sjmachin/xlrd.htm

The csv module was recently covered here:
http://blog.doughellmann.com/2007/08/pymotw-csv.html

If you are having trouble with the basics you would do well to read one 
of the tutorials here or an introductory book:
http://wiki.python.org/moin/BeginnersGuide/NonProgrammers

BTW we usually bottom post on this list.
http://catb.org/jargon/html/B/bottom-post.html

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


Re: [Tutor] unicode encoding hell

2007-09-06 Thread Kent Johnson
David Bear wrote:

> feedp.entry.title.decode('utf-8', 'xmlcharrefreplace')
> 
> I assume it would take any unicode character and 'do the right thing',
> including replacing higher ordinal chars with xml entity refs. But I still
> get
> 
> UnicodeEncodeError: 'ascii' codec can't encode character u'\u2019' in
> position 31: ordinal not in range(128)
> 
> Clearly, I completely do not understand how unicode is working here. Can
> anyone enlighten me?

It sounds like you already have Unicode. Notice that you are trying to 
decode but the error is for encoding.

In [17]: u'\u2019'.decode('utf-8')

Traceback (most recent call last):
   File "", line 1, in 
   File 
"/Library/Frameworks/Python.framework/Versions/2.5/lib/python2.5/encodings/utf_8.py",
 
line 16, in decode
 return codecs.utf_8_decode(input, errors, True)
: 'ascii' codec can't encode 
character u'\u2019' in position 0: ordinal not in range(128)

decode() goes towards unicode, encode() goes away from unicode.

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


[Tutor] Socket Timeout Handling

2007-09-06 Thread wormwood_3
I am trying to figure out the optimal way to make socket connections (INET) and 
check for timeouts. The socket module has settimeout(timeout) and 
setdefaulttimeout(timeout). However, so far as I can tell, these apply to 
socket objects. The type of socket connection I want to make is 
getfqdn(address). So I can set the default timeout for socket, but not a socket 
object (makes sense so far). I cannot use the getfqdn(address) method on a 
socket object, I have to use it on socket. This means (as I understand it thus 
far), that while I can set a timeout value for socket objects, this will not 
apply to when I use the getfqdn() method, which is where I need a timeout 
check! Some example code for the steps so far:

>>> import socket
>>> conn = socket.socket()
>>> conn.setdefaulttimeout(2.0)
Traceback (most recent call last):
  File "", line 1, in 
AttributeError: '_socketobject' object has no attribute 'setdefaulttimeout'
>>> socket.setdefaulttimeout(2.0)
>>> conn.getfqdn("64.33.212.2")
Traceback (most recent call last):
  File "", line 1, in 
AttributeError: '_socketobject' object has no attribute 'getfqdn'
>>> socket.getfqdn("64.33.212.2")
'64-33-212-2.customers.pingtone.net'
>>> # Disconnected network connection here
... 
>>> socket.getfqdn("64.33.212.2")
'64.33.212.2'
>>> # Reconnected network connection here
>>> socket.getfqdn("64.33.212.2")
'64-33-212-2.customers.pingtone.net'

After I disconnected my network connection and called getfqdn(), it returned 
the IP address I called it with after about 25 seconds. So the default timeout 
was ignored? Is there some other way to call this function so that I can check 
for timeouts? Should I instead just put my network calls in a thread and see 
how long they take, stopping them after a certain period?

Thanks for any help.

-Sam


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


[Tutor] Python / CGI

2007-09-06 Thread Fiyawerx
Hi guys, quick question, I've been trying to learn python lately, and have
written a few little apps to help with some day to day stuff I do, and
recently my fiance asked me if it was possible to come up with a simple web
based schedule she can use with the other teachers in her school to schedule
library time. (She's the librarian). Basically, it will be a small calendar
like app that will have 'slots' teachers can sign up for. It doesn't sound
like it would be too complicated, and may be a good learning project. I was
wondering if python as cgi would be good for this, and if there are any
pitfalls I need to watch out for before I start delving into it. I'm also
fairly new to writing my own html so will basically be learning it all from
scratch.

TIA,
 Lee McClintock
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Python / CGI

2007-09-06 Thread Michael Connors
Hi,
If you have your own server to run it on, I think it would make sense to use
one of the Python web frameworks that are out there. I used cherrypy for my
first web-based python project and I found it very easy to learn and develop
in quickly.
Regards,
Michael

On 06/09/07, Fiyawerx <[EMAIL PROTECTED]> wrote:
>
> Hi guys, quick question, I've been trying to learn python lately, and have
> written a few little apps to help with some day to day stuff I do, and
> recently my fiance asked me if it was possible to come up with a simple web
> based schedule she can use with the other teachers in her school to schedule
> library time. (She's the librarian). Basically, it will be a small calendar
> like app that will have 'slots' teachers can sign up for. It doesn't sound
> like it would be too complicated, and may be a good learning project. I was
> wondering if python as cgi would be good for this, and if there are any
> pitfalls I need to watch out for before I start delving into it. I'm also
> fairly new to writing my own html so will basically be learning it all from
> scratch.
>
> TIA,
>  Lee McClintock
>
> ___
> Tutor maillist  -  Tutor@python.org
> http://mail.python.org/mailman/listinfo/tutor
>
>


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


Re: [Tutor] Python / CGI

2007-09-06 Thread Fiyawerx
Thanks Michael, I'll be using my dreamhost account, and I'm pretty sure
cherrypy works there, will check into it.

On 9/6/07, Michael Connors <[EMAIL PROTECTED]> wrote:
>
> Hi,
> If you have your own server to run it on, I think it would make sense to
> use one of the Python web frameworks that are out there. I used cherrypy for
> my first web-based python project and I found it very easy to learn and
> develop in quickly.
> Regards,
> Michael
>
> On 06/09/07, Fiyawerx <[EMAIL PROTECTED]> wrote:
>
> > Hi guys, quick question, I've been trying to learn python lately, and
> > have written a few little apps to help with some day to day stuff I do, and
> > recently my fiance asked me if it was possible to come up with a simple web
> > based schedule she can use with the other teachers in her school to schedule
> > library time. (She's the librarian). Basically, it will be a small calendar
> > like app that will have 'slots' teachers can sign up for. It doesn't sound
> > like it would be too complicated, and may be a good learning project. I was
> > wondering if python as cgi would be good for this, and if there are any
> > pitfalls I need to watch out for before I start delving into it. I'm also
> > fairly new to writing my own html so will basically be learning it all from
> > scratch.
> >
> > TIA,
> >  Lee McClintock
> >
> > ___
> > Tutor maillist  -  Tutor@python.org
> > http://mail.python.org/mailman/listinfo/tutor
> >
> >
>
>
> --
> Michael Connors
>
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Python / CGI

2007-09-06 Thread Eric Brunson
Michael Connors wrote:
> Hi,
> If you have your own server to run it on, I think it would make sense 
> to use one of the Python web frameworks that are out there. I used 
> cherrypy for my first web-based python project and I found it very 
> easy to learn and develop in quickly.

That's debatable.  I find the frameworks to be overly complicated and 
obfuscated.  They're great when you're going through the tutorials, but 
try to do something "outside the box" and I find myself digging through 
documentation for longer than it would take me to write it from scratch.

For this you'd need:  some sort of backing store and some display 
routines with a simple interface to click on a slot and enter your name 
in it.  I think I could write it in about 75 lines of python.  Maybe 50.

Just my opinion,
e.

> Regards,
> Michael
>
> On 06/09/07, *Fiyawerx* <[EMAIL PROTECTED] 
> > wrote:
>
> Hi guys, quick question, I've been trying to learn python lately,
> and have written a few little apps to help with some day to day
> stuff I do, and recently my fiance asked me if it was possible to
> come up with a simple web based schedule she can use with the
> other teachers in her school to schedule library time. (She's the
> librarian). Basically, it will be a small calendar like app that
> will have 'slots' teachers can sign up for. It doesn't sound like
> it would be too complicated, and may be a good learning project. I
> was wondering if python as cgi would be good for this, and if
> there are any pitfalls I need to watch out for before I start
> delving into it. I'm also fairly new to writing my own html so
> will basically be learning it all from scratch.
>
> TIA,
>  Lee McClintock
>
> ___
> Tutor maillist  -  Tutor@python.org 
> http://mail.python.org/mailman/listinfo/tutor
>
>
>
>
> -- 
> Michael Connors
> 
>
> ___
> 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] Python / CGI

2007-09-06 Thread Kent Johnson
Fiyawerx wrote:
> Hi guys, quick question, I've been trying to learn python lately, and 
> have written a few little apps to help with some day to day stuff I do, 
> and recently my fiance asked me if it was possible to come up with a 
> simple web based schedule she can use with the other teachers in her 
> school to schedule library time. (She's the librarian). Basically, it 
> will be a small calendar like app that will have 'slots' teachers can 
> sign up for. It doesn't sound like it would be too 
> complicated, and may be a good learning project.

I would look for prior art, myself. This sounds like a project that 
could easily get complicated. Here are some promising links:
http://python.about.com/b/a/88.htm
http://www.schooltool.org/products/schooltool-calendar

For that matter you might find that Google Calendar does everything you 
need:
http://www.google.com/calendar

It has a Python API FWIW:
http://code.google.com/apis/calendar/overview.html

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


Re: [Tutor] Python / CGI

2007-09-06 Thread Kent Johnson
Eric Brunson wrote:
> Michael Connors wrote:
>> Hi,
>> If you have your own server to run it on, I think it would make sense 
>> to use one of the Python web frameworks that are out there. I used 
>> cherrypy for my first web-based python project and I found it very 
>> easy to learn and develop in quickly.
> 
> That's debatable.  I find the frameworks to be overly complicated and 
> obfuscated.  They're great when you're going through the tutorials, but 
> try to do something "outside the box" and I find myself digging through 
> documentation for longer than it would take me to write it from scratch.

Hmm. As my boss likes to say, "Reasonable people may disagree." I have 
been developing with Django for about six months now and I love it. Much 
of what I have wanted to do is directly supported and easy in Django; 
sometimes I have to push at it but only a few times have I really felt 
like I was trying to make it do something it really didn't want to do. I 
find both the documentation and code informative and easy to read.

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


Re: [Tutor] Python / CGI

2007-09-06 Thread Eric Brunson
Kent Johnson wrote:
> Eric Brunson wrote:
>   
>> Michael Connors wrote:
>> 
>>> Hi,
>>> If you have your own server to run it on, I think it would make sense 
>>> to use one of the Python web frameworks that are out there. I used 
>>> cherrypy for my first web-based python project and I found it very 
>>> easy to learn and develop in quickly.
>>>   
>> That's debatable.  I find the frameworks to be overly complicated and 
>> obfuscated.  They're great when you're going through the tutorials, but 
>> try to do something "outside the box" and I find myself digging through 
>> documentation for longer than it would take me to write it from scratch.
>> 
>
> Hmm. As my boss likes to say, "Reasonable people may disagree." I have 
> been developing with Django for about six months now and I love it. Much 
> of what I have wanted to do is directly supported and easy in Django; 
> sometimes I have to push at it but only a few times have I really felt 
> like I was trying to make it do something it really didn't want to do. I 
> find both the documentation and code informative and easy to read.
>   

I'll admit, though I looked at it briefly, Django was not one of the 
frameworks I spent much time on (didn't care for the templating style).  
I do most of my web development to run under mod_python and I've 
developed my own libraries to do what I need to do the way I think it 
should be done.  :-)



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


[Tutor] getting iteration level

2007-09-06 Thread David Bear
Lets say I have a list object that I iterate over like this:

for item in myList:
   process(item)

During execution of the for loop something happens and I want to know how
many items have be iterated over, how do I find out? Without resorting to
some counter inside the loop, is there some python object I can ask?


--
David Bear
College of Public Programs at Arizona State University

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


Re: [Tutor] Dynamically changing a class

2007-09-06 Thread Eike Welk
On Wednesday 05 September 2007 16:59, Jason Doege wrote:
> Thanks for the good and useful information on this. Now for the
> why...
>
> I am building an API and for various reasons I have chosen Python
> to implement it. I'd like to separate the implementation from the
> interface as, for instance, C++ does with separate .hpp and .cpp
> files. Apart from defining a class with a bunch of empty methods
> and then redefining them, I have not seen a good way to do this in
> Python. Can you recommend the Pythonic way to do this?
>

If you want to describe the data and validate it, Enthought traits may 
be interesting for you:
http://code.enthought.com/traits/

They additionally have a library that can automatically generate a 
graphical user interface to change the data of a traits using class.

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


Re: [Tutor] Python / CGI

2007-09-06 Thread Eric Walstad
Michael Connors wrote:
> Hi,
> If you have your own server to run it on, I think it would make sense to
> use one of the Python web frameworks that are out there. I used cherrypy
> for my first web-based python project and I found it very easy to learn
> and develop in quickly.
> Regards,
> Michael

I'd argue that sticking with a Python CGI would be a great way to start
because the OP can focus on the basics rather than on the framework API.
 The Python CGI stuff I've written in the past was helpful in my
understanding of how and why Django works the way it does.

After his Web Calendar is functioning as a CGI he'll have lots of ideas
on how to improve it.  A framework might help at that point.

This article looks like it might have some useful information:
[http://python.about.com/od/advancedpython/ss/howtocal.htm]

Sounds like a fun project.

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


Re: [Tutor] getting iteration level

2007-09-06 Thread Kent Johnson
David Bear wrote:
> Lets say I have a list object that I iterate over like this:
> 
> for item in myList:
>process(item)
> 
> During execution of the for loop something happens and I want to know how
> many items have be iterated over, how do I find out? Without resorting to
> some counter inside the loop, is there some python object I can ask?

Use enumerate(), it generates the list index for you:

for i, item in enumerate(myList):
   process(item)
   print 'processed item', i

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


Re: [Tutor] Socket Timeout Handling

2007-09-06 Thread wormwood_3
Since no one bit on this yet, let me simplify to the core issue I am having:

What is the best practice for checking for network connectivity errors when 
making network calls? Is it better to wrap the functions that make said calls 
in threads and time them? Or to use timeout variables for modules like socket? 
Something else?

I found some good general info here: 
http://www.onlamp.com/pub/a/python/2003/11/06/python_nio.html

But I have had a hard time finding info on network error handling specifically.

Thoughts?

__
- Original Message 
From: wormwood_3 <[EMAIL PROTECTED]>
To: Python Tutorlist 
Sent: Thursday, September 6, 2007 9:40:21 AM
Subject: [Tutor] Socket Timeout Handling

I am trying to figure out the optimal way to make socket connections (INET) and 
check for timeouts. The socket module has settimeout(timeout) and 
setdefaulttimeout(timeout). However, so far as I can tell, these apply to 
socket objects. The type of socket connection I want to make is 
getfqdn(address). So I can set the default timeout for socket, but not a socket 
object (makes sense so far). I cannot use the getfqdn(address) method on a 
socket object, I have to use it on socket. This means (as I understand it thus 
far), that while I can set a timeout value for socket objects, this will not 
apply to when I use the getfqdn() method, which is where I need a timeout 
check! Some example code for the steps so far:

>>> import socket
>>> conn = socket.socket()
>>> conn.setdefaulttimeout(2.0)
Traceback (most recent call last):
  File "", line 1, in 
AttributeError: '_socketobject' object has no attribute 'setdefaulttimeout'
>>> socket.setdefaulttimeout(2.0)
>>> conn.getfqdn("64.33.212.2")
Traceback (most recent call last):
  File "", line 1, in 
AttributeError: '_socketobject' object has no attribute 'getfqdn'
>>> socket.getfqdn("64.33.212.2")
'64-33-212-2.customers.pingtone.net'
>>> # Disconnected network connection here
... 
>>> socket.getfqdn("64.33.212.2")
'64.33.212.2'
>>> # Reconnected network connection here
>>> socket.getfqdn("64.33.212.2")
'64-33-212-2.customers.pingtone.net'

After I disconnected my network connection and called getfqdn(), it returned 
the IP address I called it with after about 25 seconds. So the default timeout 
was ignored? Is there some other way to call this function so that I can check 
for timeouts? Should I instead just put my network calls in a thread and see 
how long they take, stopping them after a certain period?

Thanks for any help.

-Sam


___
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] Python / CGI

2007-09-06 Thread Steve Willoughby
On Thu, Sep 06, 2007 at 11:35:14AM -0400, Fiyawerx wrote:
> recently my fiance asked me if it was possible to come up with a simple web
> based schedule she can use with the other teachers in her school to schedule
> library time. (She's the librarian). Basically, it will be a small calendar
> like app that will have 'slots' teachers can sign up for. It doesn't sound

I wrote something like that in a couple of hours using straight Python, so
I know it's doable.  I haven't checked out any of the frameworks, but it
would be really interesting to see what it would take to build a couple
of apps in Django or whatever.

If you're interested in just banging out a Python app, though, my experience
was writing a calendaring tool for a group of my friends who get together
once a month or so for gaming days.  The app displays a year's calendar
(any year desired) and shows what days we're playing or not.  You click on
a link to get a version of that calendar view with "slots" open for you to
vote on which days you prefer or which you can't play on.  Then I have an
admin view which displays all the votes, and lets me make the "play/not play"
decision for each date, which is what is displayed in the normal calendar 
view.

The whole thing only took 318 lines of straight Python code, including all 
the HTML displayed on all those forms.

The "calendar" module is your friend for apps like this, by the way :)



-- 
Steve Willoughby|  Using billion-dollar satellites
[EMAIL PROTECTED]   |  to hunt for Tupperware.
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Python / CGI

2007-09-06 Thread Alan Gauld
"Fiyawerx" <[EMAIL PROTECTED]> wrote

> Hi guys, quick question, I've been trying to learn python lately, 
> and have
> written a few little apps to help with some day to day stuff I do, 
> and
> recently my fiance asked me if it was possible to come up with a 
> simple web
> based schedule she can use with the other teachers in her school to 
> schedule
> library time.

> wondering if python as cgi would be good for this, and if there are 
> any
> pitfalls I need to watch out for before I start delving into it.

If you want to do it as a learning excercise then I recommend using 
vanilla CGI.
Everyone should write a couple of CGI apps to understand what really 
goes on.
But two is enough and then move onto a framework for the sake of your 
sanity!

Django has gotten a mention, personally I've been using TurboGears a 
bit
and like it. Its an amalgamation of CherryPy (already mentioned) to 
convert
http requests into python method calls with kid for templating
(aka isolating your HTML from your code) and SQL Objects for providing
OO style database access - you may not even need this, flat files may
be adequate.(It can also use SQL Alchemy for this but I've not used 
it)

www.turbogears.org

But frankly all web objects provide the same basic features and for
"conventional" web apps there is little to choose IMHO! ) And not just
in Python, the same applies to Ruby on Rails, Struts(Java),
IBM Websphere etc.

HTH,

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



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


[Tutor] manually sorting variables

2007-09-06 Thread Christopher Spears
I'm working out of Core Python Programming (2nd
Edition) by Wesley Chun.

Here is the problem:

Have the user enter three numeric values and store
them in three different variables.  Without using
lists or sorting algorithms, manually sort these three
numbers from smallest to largest.

Here is what I have so far:
#!/usr/bin/env python

def smallest_var(x,y):
if x < y:
return x
elif y < x:
return y
else:
print "x equals y"

var1 = raw_input("Enter a number for var1: ")
var2 = raw_input("Enter a number for var2: ")
var3 = raw_input("Enter a number for var3: ")

small = smallest_var(var1, var2)
#print small_var_1
smallest = smallest_var(small, var3)
print smallest

I'm not sure what the next step would be.  If I was
using a list, I could try to remove the smallest
variable and then just compare the two remaining
variables.  Any hints?

"I'm the last person to pretend that I'm a radio.  I'd rather go out and be a 
color television set."
-David Bowie

"Who dares wins"
-British military motto

"I generally know what I'm doing."
-Buster Keaton
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] manually sorting variables

2007-09-06 Thread Kent Johnson
Christopher Spears wrote:
> I'm working out of Core Python Programming (2nd
> Edition) by Wesley Chun.
> 
> Here is the problem:
> 
> Have the user enter three numeric values and store
> them in three different variables.  Without using
> lists or sorting algorithms, manually sort these three
> numbers from smallest to largest.
> 
> Here is what I have so far:
> #!/usr/bin/env python
> 
> def smallest_var(x,y):
>   if x < y:
>   return x
>   elif y < x:
>   return y
>   else:
>   print "x equals y"
>   
> var1 = raw_input("Enter a number for var1: ")
> var2 = raw_input("Enter a number for var2: ")
> var3 = raw_input("Enter a number for var3: ")
> 
> small = smallest_var(var1, var2)
> #print small_var_1
> smallest = smallest_var(small, var3)
> print smallest
> 
> I'm not sure what the next step would be.  If I was
> using a list, I could try to remove the smallest
> variable and then just compare the two remaining
> variables.  Any hints?

Do you know how to do a bubble sort? You could use comparison and 
swapping to sort the values so x is the smallest, y is the middle and z 
is the largest. I think it can be done with three comparisons and three 
or fewer swaps. Think of x, y and z as the three positions of a list.

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


[Tutor] dynamic attribute assignment

2007-09-06 Thread John
I've just noticed that you can use the import statement to import variables,
such that a simple file such as vars.py:

# File with predefined variables
var1= 'some text'
var2= 2
var3=['a','b','c']

Would then, upon import, provide:

>>>vars.var1
'some text'
>>>vars.var2
2
>>>vars.var3
['a','b','c']

This is great, I had no idea! However, is there then a way to reassign or
update the values? For instance, can you say:

>>>vars.var1='some new text'

??

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


Re: [Tutor] Socket Timeout Handling

2007-09-06 Thread Alan Gauld
"wormwood_3" <[EMAIL PROTECTED]> wrote

> Since no one bit on this yet, let me simplify to the core issue I am 
> having:

That may be because your question ventures into fairly deep areas of 
networking
that most folk who are just learning Python(ie readers of this list) 
have probably
not encountered. I've used python for network programming but not 
extensively
and certainly not for hard core production use so can't help you.

Similarly I've used sockets extesively from C/C++ but I'm  not sure 
how well
that transfers to Python without doing more digging than I hsave time 
for right
now.

If you do want to get into depth on Python networking you may find the
A-Press book Python Network Programming useful - I do regularly :-).

However the only information I could see about timeouts there was
related to socket.settimeout() which ISTR you didn't want to/couldn't 
use...

> What is the best practice for checking for network connectivity
> errors when making network calls? Is it better to wrap the functions
> that make said calls in threads and time them?
> Or to use timeout variables for modules like socket?

Personally if i was doingt that I'd almost certainy put it in a thread
and apply a timeout within the thread. but not having tried that I 
don't
know how easy it would be!

> But I have had a hard time finding info on network error handling 
> specifically.

The A-Press book does devote several pages in total to socket error
handling including connection errors, timeout errors and transmission 
errors.

>
> I am trying to figure out the optimal way to make socket connections
> (INET) and check for timeouts. The socket module has 
> settimeout(timeout)
> and setdefaulttimeout(timeout). However, so far as I can tell, these 
> apply
> to socket objects. The type of socket connection I want to make is
> getfqdn(address).

I don't understand, getfqdn() returns a domain name not an a socket?

> So I can set the default timeout for socket, but not a socket object
> (makes sense so far). I cannot use the getfqdn(address) method on
> a socket object, I have to use it on socket.

Sorry it's not making sense to me, getfqdn takes a host name not a 
socket.

 import socket
 conn = socket.socket()
 conn.setdefaulttimeout(2.0)

setdefaulttimout is a function in the socket module not a method of 
socket.
Thus you woulfd call that before reating a new socket:

>>> socket.setdefaulttimeout(5)   # set default t/o of 5 secs
>>> conn = socket.socket()# with timeout of 5

 socket.setdefaulttimeout(2.0)
 conn.getfqdn("64.33.212.2")

Again getfqdn is a function in the socket module not a method.
But if you give it an IP saddress it will just return that IP address!

 socket.getfqdn("64.33.212.2")
> '64-33-212-2.customers.pingtone.net'

OK, Apparently it will give you more... :-)

 # Disconnected network connection here
> ...
 socket.getfqdn("64.33.212.2")
> '64.33.212.2'

> After I disconnected my network connection and called getfqdn(),
> it returned the IP address I called it with after about 25 seconds.
> So the default timeout was ignored?

Yes because there was no socket being created it was doing a
name lookup using standard DNS etc, and with the network disconnected
timed out at the OS level I suspect. If you want to control the DNS
lookup you will need to cofde that manually I suspect. (As I say,
this is way deeper than I've needed to peer into these type of
operations, the defaults have worked for me!)

> Is there some other way to call this function so that I can check
> for timeouts? Should I instead just put my network calls in a
> thread and see how long they take, stopping them after a certain 
> period?

I don't think that would necessarily help here.

What are you trying to do? Establish a socket connection to something
or just do a name check that times out more quickly?(or slowly)

> Thanks for any help.

Not sure how much help, not even sure I understand what you are
up to, but it might spark an idea...

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


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