[Tutor] Using Easygui in Python v3

2009-04-09 Thread Alan Gauld

I'm currently updating my tutorial to cover Python V3.
I thought it miight be good to include some stuff on EasyGUI for the 
user input topic and on checking the web site it said Easygui was 
compatible with Python v3. However after downloading  and installing 
I get a "No module named StringIO" error under Python v3.


Before I start digging deeper I thought I'd ask if anyone else had 
the same problem and knows of a fix?



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

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


Re: [Tutor] Using Easygui in Python v3

2009-04-09 Thread Alan Gauld


"Alan Gauld"  wrote 

user input topic and on checking the web site it said Easygui was 
compatible with Python v3. However after downloading  and installing 
I get a "No module named StringIO" error under Python v3.


OK, Not much digging required.
StringIO has moved to io.StringIO in v3
So changing the version check code to:

if runningPython3:
  from tkinter import *
  import tkinter.filedialog as tk_FileDialog
  from io import StringIO
else:
  from Tkinter import *
  import tkFileDialog as tk_FileDialog
  import StringIO

and deleting the original import StringIO line seems to work...

If someone else could carry out a more exhaustive test than my 
basic EnterBox then  I'll notify the easygui team.


Alan G.

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


[Tutor] Accessing a list inside a class...

2009-04-09 Thread Alexander Daychilde (Gmail)
I feel like I'm missing something simple, but I have now spent hours
googling for an answer. I think I must not be searching for the right terms,
or else this is something I'm not supposed to be doing - but it seems
straightforward to me.

 

Here's my test code (condensed from the actual much longer code I'm adapting
from functions to objects - I'm new to OOP, but I'm trying J ):

 

___

 

class TestObject:



def __init__(self):



# options and arguments from commandline-->OptionParser

self.opt = ""

self.args = []

 

def load_cfg(self):



# read+parse commandlnie

self._parse_commandline()



def load_ini(self):



# I'm trying to get at 'inifile' from the commandline... #

print self.opt['inifile']

 

def _parse_commandline(self):



parser = OptionParser()



parser.add_option("-i", dest = "inifile", help = "specify an ini
file to load")



(self.opt, self.args) = parser.parse_args()

 

if __name__ == '__main__':



# parses command-line argumenmts

from optparse import OptionParser



test = TestObject()

test.load_cfg()

test.load_ini()

 

 

___

 

 

In the middle is a comment with five hashes to show you the crux of my
problem.

 

If I eliminate "['inifile']", I can print the list.

 

How in the world can I get just the 'inifile' element?

 

Ideally, of course, I'm not printing this - I'm going to access it from
outside the class, or even inside the class.

 

The error I get when running the above code:

 

Traceback (most recent call last):

  File "listinclass.py", line 34, in 

test.load_ini()

  File "listinclass.py", line 17, in load_ini

print self.opt['inifile']

AttributeError: Values instance has no attribute '__getitem__'

 

I've googled for "list in a class" and pretty much every variant I can think
of, and I just can't seem to find a single example of someone trying to get
to an element of a list stored in a class. so I apologize for asking a
basic/simple question, but I just can't find it.

 

Many thanks for your consideration.

 

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


Re: [Tutor] Accessing a list inside a class...

2009-04-09 Thread bob gailer

With the small font size and lots of blank lines I find this hard to read!

When I look at the optparse documentation I guess you want
print self.opt.inifile

Alexander Daychilde (Gmail) wrote:


I feel like I’m missing something simple, but I have now spent hours 
googling for an answer. I think I must not be searching for the right 
terms, or else this is something I’m not supposed to be doing – but it 
seems straightforward to me…


Here’s my test code (condensed from the actual much longer code I’m 
adapting from functions to objects – I’m new to OOP, but I’m trying J ):


___

class TestObject:

def __init__(self):

# options and arguments from commandline-->OptionParser

self.opt = ""

self.args = []

def load_cfg(self):

# read+parse commandlnie

self._parse_commandline()

def load_ini(self):

# I'm trying to get at 'inifile' from the commandline... #

print self.opt['inifile']

def _parse_commandline(self):

parser = OptionParser()

parser.add_option("-i", dest = "inifile", help = "specify an ini file 
to load")


(self.opt, self.args) = parser.parse_args()

if __name__ == '__main__':

# parses command-line argumenmts

from optparse import OptionParser

test = TestObject()

test.load_cfg()

test.load_ini()

___

In the middle is a comment with five hashes to show you the crux of my 
problem…


If I eliminate “[‘inifile’]”, I can print the list.

How in the world can I get just the ‘inifile’ element?

Ideally, of course, I’m not printing this – I’m going to access it 
from outside the class, or even inside the class…


The error I get when running the above code:

Traceback (most recent call last):

File "listinclass.py", line 34, in 

test.load_ini()

File "listinclass.py", line 17, in load_ini

print self.opt['inifile']

AttributeError: Values instance has no attribute '__getitem__'

I’ve googled for “list in a class” and pretty much every variant I can 
think of, and I just can’t seem to find a single example of someone 
trying to get to an element of a list stored in a class… so I 
apologize for asking a basic/simple question, but I just can’t find it.


Many thanks for your consideration…



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



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


Re: [Tutor] Accessing a list inside a class...

2009-04-09 Thread spir
Le Thu, 9 Apr 2009 08:47:59 -0700,
"Alexander Daychilde (Gmail)"  s'exprima ainsi:

> The error I get when running the above code:
> 
> Traceback (most recent call last):
> 
>   File "listinclass.py", line 34, in 
> 
> test.load_ini()
> 
>   File "listinclass.py", line 17, in load_ini
> 
> print self.opt['inifile']
> 
> AttributeError: Values instance has no attribute '__getitem__'

This means the following:
* You're trying to access an item inside something called self.opt by using a 
key: 'inifile'.
* self.opt should then be able to return you an item from a key, in other words 
it should be an object that behaves like a dictionary, right?
* The "magic" method the allows this behaviour is called __getitem__ (the same 
as for list indexing, actually). So that python looks for this method in the 
class of self.opt, that happens to be Values, and tells you...

Sure, not very clear!

> I've googled for "list in a class" and pretty much every variant I can think
> of, and I just can't seem to find a single example of someone trying to get
> to an element of a list stored in a class. so I apologize for asking a
> basic/simple question, but I just can't find it.

The issue has nothing to do with the fact the self.opt happens to be an 
attribute.

Denis
--
la vita e estrany
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Accessing a list inside a class...

2009-04-09 Thread Emile van Sebille

Alexander Daychilde (Gmail) wrote:


class TestObject:
def __init__(self):
# options and arguments from commandline-->OptionParser
self.opt = ""

here, self.opt is initialized as a string...

self.args = []
def load_cfg(self):
# read+parse commandlnie
self._parse_commandline()
def load_ini(self):
# I'm trying to get at 'inifile' from the commandline... #
print self.opt['inifile']

... and here you're accessing it as a dict

def _parse_commandline(self):
parser = OptionParser()
parser.add_option("-i", dest = "inifile", help = "specify an ini 
file to load")

(self.opt, self.args) = parser.parse_args()

... and here it's changed to whatever is returned by parser.parse_args


if __name__ == '__main__':
# parses command-line argumenmts
from optparse import OptionParser
test = TestObject()

... so when __init__ has complete, test.opt will be a string

test.load_cfg()

... and after this it's whatever is returned by parser.parse_args

try print test.opt to see what was returned -- that'll probably help 
clear things up.  If it's really a list type, you don't access those 
with keys, so self.opt['inifile'] would fail.



test.load_ini()



HTH,

Emile

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


[Tutor] Finding all IP addresses associated with a host

2009-04-09 Thread Scott Newton
I'm very new to python, and my google-fu has failed to locate an answer to
the following:

How can I find all of the (ipv4) addresses that are currently up on a host?

I realize I can call ifconfig and then munge the output with awk, but that
seems messy, and before I tried that I though I would ask around.

To give a bit more information, I'm writing a script that will use the ip
address assigned to the wifi interface to determine location (this is on a
Mac) and then mount network drives appropriate to the location. If anyone
has a better idea how to do this, I'd be happy to hear them.

Thanks in advance!

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


Re: [Tutor] Finding all IP addresses associated with a host

2009-04-09 Thread Emile van Sebille

Scott Newton wrote:
I'm very new to python, and my google-fu has failed to locate an answer 
to the following:


How can I find all of the (ipv4) addresses that are currently up on a host?



In google I searched for "python what's my ip" and about the fourth 
entry down found a link that led me to 
http://pypi.python.org/pypi/netifaces/0.3


How far does that get you?

HTH,

Emile





I realize I can call ifconfig and then munge the output with awk, but 
that seems messy, and before I tried that I though I would ask around.


To give a bit more information, I'm writing a script that will use the 
ip address assigned to the wifi interface to determine location (this is 
on a Mac) and then mount network drives appropriate to the location. If 
anyone has a better idea how to do this, I'd be happy to hear them.


Thanks in advance!

Scott Newton




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


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


[Tutor] BeautifulSoup confusion

2009-04-09 Thread Steve Lyskawa
I am not a programmer by trade but I've been using Python for 10+ years,
usually for text file conversion and protocol analysis.  I'm having a
problem with Beautiful Soup.  I can get it to scrape off all the href links
on a web page but I am having problems selecting specific URI's from the
output supplied by Beautiful Soup.
What exactly is it returning to me and what command would I use to find that
out?  Do I have to take each line it give me and put it into a list before I
can, for example, get only certain URI's containing a certain string or use
the results to get the web page that the URI is referring to?

The pseudo code for what I am trying to do:

Get all URI's from web page that contain string "env.html"
Open the web page it is referring to.
Scrape selected information off of that page.

I'm have problem with step #1.  I can get all URI's but I can't see to get
re.compile to work right.  If I could get it to give me the URI only without
tags or link description, that would be ideal.

Thanks for your help.

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


Re: [Tutor] BeautifulSoup confusion

2009-04-09 Thread Kent Johnson
On Thu, Apr 9, 2009 at 7:27 PM, Steve Lyskawa  wrote:
> I'm having a
> problem with Beautiful Soup.  I can get it to scrape off all the href links
> on a web page but I am having problems selecting specific URI's from the
> output supplied by Beautiful Soup.
> What exactly is it returning to me and what command would I use to find that
> out?

Generally it gives you Tag and NavigableString objects, or lists of
same. To find out what something is, print its type:
print type(x)

> Do I have to take each line it give me and put it into a list before I
> can, for example, get only certain URI's containing a certain string or use
> the results to get the web page that the URI is referring to?
> The pseudo code for what I am trying to do:
> Get all URI's from web page that contain string "env.html"
> Open the web page it is referring to.
> Scrape selected information off of that page.

If you want to get all URI's at once, then that would imply creating a
list. You could also process URI's one-at-a-time.

> I'm have problem with step #1.  I can get all URI's but I can't see to get
> re.compile to work right.  If I could get it to give me the URI only without
> tags or link description, that would be ideal.

Something like this should get you started:

soup = BeautifulSoup()
for anchor in soup.findAll('a', href=re.compile(r'env\.html')):
  print anchor['href']

That says, find all  tags whose 'href' attribute matches the regex
'env\.html'. The Tag object will be assigned to the anchor variable.
Then the value of the 'href' attribute is printed.

I find it very helpful with BS to experiment at the command line. It
often takes a few tries to understand what it is giving you and how to
get exactly what you want.

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


[Tutor] What is Dani Web?

2009-04-09 Thread Wayne Watson
Title: Signature.html




See . It seems to a set of forum
for a lot of software, including Python. I don't see any explanation of
Dani and how it formed. 
-- 


   Wayne Watson (Watson Adventures, Prop., Nevada City, CA)

 (121.01 Deg. W, 39.26 Deg. N) GMT-8 hr std. time)

  "In arithemtic as in politics, the importance of one is
   determined by the number of zeros behind it." -- Anon 
  




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


Re: [Tutor] Finding all IP addresses associated with a host

2009-04-09 Thread Scott Newton
Thanks, not sure how I missed that one.

-Scott

On Thu, Apr 9, 2009 at 7:25 PM, Emile van Sebille  wrote:

> Scott Newton wrote:
>
>> I'm very new to python, and my google-fu has failed to locate an answer to
>> the following:
>>
>> How can I find all of the (ipv4) addresses that are currently up on a
>> host?
>>
>
>
> In google I searched for "python what's my ip" and about the fourth entry
> down found a link that led me to http://pypi.python.org/pypi/netifaces/0.3
>
> How far does that get you?
>
> HTH,
>
> Emile
>
>
>
>
>> I realize I can call ifconfig and then munge the output with awk, but that
>> seems messy, and before I tried that I though I would ask around.
>>
>> To give a bit more information, I'm writing a script that will use the ip
>> address assigned to the wifi interface to determine location (this is on a
>> Mac) and then mount network drives appropriate to the location. If anyone
>> has a better idea how to do this, I'd be happy to hear them.
>>
>> Thanks in advance!
>>
>> Scott Newton
>>
>>
>> 
>>
>> ___
>> Tutor maillist  -  Tutor@python.org
>> http://mail.python.org/mailman/listinfo/tutor
>>
>
> ___
> Tutor maillist  -  Tutor@python.org
> http://mail.python.org/mailman/listinfo/tutor
>
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


[Tutor] How to write server which listens to specific port

2009-04-09 Thread ShivKumar Anand

Dear All,

Greetings

 

I have developed an application in Python 2.4.2, TurboGears 1.0, cherryPy 2.3.1.

 

Now, I have to develop a server, which listens to a specific port (104) and 
save the file to a specific location (/tmp/myDir) on RHEL 4/5.

 

Guide me how to do that. what all packages I have to use.

 

 

Thanks in anticipation.

 

Shiv

_
The new Windows Live Messenger. You don’t want to miss this.
http://www.microsoft.com/india/windows/windowslive/messenger.aspx___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] BeautifulSoup confusion

2009-04-09 Thread Stefan Behnel
Steve Lyskawa wrote:
> I am not a programmer by trade but I've been using Python for 10+ years,
> usually for text file conversion and protocol analysis.  I'm having a
> problem with Beautiful Soup.  I can get it to scrape off all the href links
> on a web page but I am having problems selecting specific URI's from the
> output supplied by Beautiful Soup.
> What exactly is it returning to me and what command would I use to find that
> out?  Do I have to take each line it give me and put it into a list before I
> can, for example, get only certain URI's containing a certain string or use
> the results to get the web page that the URI is referring to?
> 
> The pseudo code for what I am trying to do:
> 
> Get all URI's from web page that contain string "env.html"
> Open the web page it is referring to.
> Scrape selected information off of that page.

That's very easy to do with lxml.html, which offers an iterlinks() method
on elements to iterate over all links in a document (not only a-href, but
also in stylesheets, for example). It can parse directly from a URL, so you
don't need to go through urllib and friends, and it can make links in a
document absolute before iterating over them, so that relative links will
work for you are doing.

http://codespeak.net/lxml/lxmlhtml.html#working-with-links

Also, you should use the urlparse module to split the URL (in case it
contains parameters etc.) and check only the path section.

Stefan

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


Re: [Tutor] problem in replacing regex

2009-04-09 Thread Kumar
Hey Moos/Denis/Kent,

Thanks a lot for your replies and suggestions.

Actually the value which I had given was separated by \n\r.
I now realized that this cannot be done with one regular expression as there
is one limit in negative look behind that it requires fixed width. I guess
This limit is there up to python 2.7 version.
So actually though I don't want, but I had to do it using splitting :(

But thanks a lot for your help, this has given me better understanding the
regex.
Thanks you all thank you so much.

Regards,
Kumar

On Thu, Apr 9, 2009 at 3:35 AM, Moos Heintzen  wrote:

> Hi,
>
> You can do the substitution in many ways.
>
> You can first search for bare account numbers and substitute them with
> urls. Then substitute urls into  tags.
>
> To substitute account numbers that aren't in urls, you simply
> substitutes account numbers if they don't start with a "/", as you
> have been trying to do.
>
> re.sub() can accept a function instead of a string. The function
> receives the match object and returns a replacement. This way you can
> do extra processing to matches.
>
> import re
>
> text = """https://hello.com/accid/12345-12
>
> 12345-12
>
> http://sadfsdf.com/asdf/asdf/asdf/12345-12
>
> start12345-12end
>
> this won't be replaced
> start/123-45end
> """
>
> def sub_num(m):
>if m.group(1) == '/':
>return m.group(0)
>else:
># put url here
>return m.group(1) + 'http://example.com/' + m.group(2)
>
> >>> print re.sub(r'(\D)(\d+-\d+)', sub_num , text)
> https://hello.com/accid/12345-12
>
> http://example.com/12345-12
>
> http://sadfsdf.com/asdf/asdf/asdf/12345-12
>
> starthttp://example.com/12345-12end
>
> this won't be replaced
> start/123-45end
>
> >>> _
>
> This is assuming there isn't any  tags in the input, so you should
> do this before substituting urls into  tags.
>
>
> I have super cow powers!
>
> Moos
>
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor