Re: [Tutor] Accessing methods in same class

2011-11-06 Thread Peter Lavelle

Hi,

Could you post a copy of the code you are working on, so we can help you 
better with this?


Usually, when calling a method in the same class you use the syntax: 
self.method_name()


'self' refers to an attribute or method within the same class.

Sorry, if this does not help you.

Regards

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


Re: [Tutor] Good Book

2011-07-18 Thread Peter Lavelle

There's a free ebook aimed at beginners here:http://inventwithpython.com/

Regards

Peter Lavelle


On 18/07/11 09:26, Ryan wrote:

Dear All Pythonist,

I'm strarting learn python programming and I have been found many 
resources on it but I have a problem. I don't know, what is the best 
complete book for new learner like me.


I need your recommendation, thanks before . . .

--
It is no more than the conspiracy of 0 and 1
[decrypthor]


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



--
LinkedIn Profile: http://linkedin.com/in/pmjlavelle
Twitter: http://twitter.com/pmjlavelle

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


Re: [Tutor] sftp get single file

2011-06-22 Thread Peter Lavelle
You could use the subprocess module to run the relevant system commands. 
More info on running sftp non-interactively (i.e from a script) can be 
found here: http://fixunix.com/ssh/238284-non-interactive-sftp-put.html


Regards

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


Re: [Tutor] regex woes in finding an ip and GET string

2011-06-19 Thread Peter Lavelle
Looking at the regex you have to match an IP address, I think you would 
need to put a range limit on each of the four octets you are searching 
for (as each one would be between 1 and 3 digits long.)


For example: r = 
re.match(r\b\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}\b,line) has worked for me.


I am no expert on regex (it scares me!) I got the example above from:
http://www.regular-expressions.info/examples.html


Hope my semi-coherent ramblings have been of some help

Regards

Peter

On 19/06/11 12:25, Gerhardus Geldenhuis wrote:

Hi
I am trying to write a small program that will scan my access.conf 
file and update iptables to block anyone looking for stuff that they 
are not supposed to.


The code:
#!/usr/bin/python
import sys
import re

def extractoffendingip(filename):
  f = open(filename,'r')
  filecontents = f.read()
#193.6.135.21 - - [11/Jun/2011:13:58:01 +] GET 
/admin/pma/scripts/setup.php HTTP/1.1 404 304 - Mozilla/4.0 
(compatible; MSIE 6.0; Windows 98)
  tuples = re.findall(r'^(\d+\.\d+\.\d+\.\d+).*\GET(.*)HTTP', 
filecontents)

  iplist = []
  for items in tuples:
(ip, getstring) = items
print ip,getstring
#print item
if ip not in iplist:
  iplist.append(ip)
  for item in iplist:
print item
  #ipmatch = re.search(r'', filecontents)

def main():
  extractoffendingip('access_log.1')

if __name__ == '__main__':
  main()

logfile=http://pastebin.com/F3RXDYBW


I could probably have used ranges to be more correct about finding 
ip's but I thought that apache should take care of that. I am assuming 
a level or integrity in the log file with regards to data...


The first problem I ran into was that I added a ^ to my search string:
re.findall(r'^(\d+\.\d+\.\d+\.\d+).*\GET(.*)HTTP', filecontents)

but that finds only two results a lot less than I am expecting. I am a 
little bit confused, first I thought that it might be because the 
string I am searching is now only one line because of the method of 
loading and the ^ should only find one instance but instead it finds two?


So removing the ^ works much better but now I get mostly correct 
results but I also get a number of ip's with an empty get string, only 
thought there should be only one in the log file. I would really 
appreciate any pointers as to what is going on here.


Regards

--
Gerhardus Geldenhuis


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



--
LinkedIn Profile: http://linkedin.com/in/pmjlavelle
Twitter: http://twitter.com/pmjlavelle

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


Re: [Tutor] Beginner puzzle with unpacking argv

2011-06-16 Thread Peter Lavelle
If you need to process command line arguments then the argparse module 
may also be useful to you. More info can be found here: 
http://docs.python.org/library/argparse.html


Regards

Peter Lavelle

On 16/06/11 19:03, Steve Willoughby wrote:

On 16-Jun-11 10:10, Lisi wrote:


1 from sys import argv
2
3 script, user_name = argv

I have tried every permutation of white space I could think of that 
might have

looked like the original, but I always get the same error:


That will work ONLY if argv has at least 2 values in it.  Your source 
code is ok as far as it goes.  Try running your script with two 
command line arguments and see what you get.  (The first argument 
should be the name of your script, incidentally).


If your script were named foo.py, then running the command:
 foo.py

would give you the error you see because argv only has 1 thing in it 
and you're trying to retrieve two.  If you ran it as:

 foo.py bar

that should work, and script would be foo.py and user_name would be 
bar.


You could check len(argv) first to see how many items you have before 
you try to get two values from it.


For more sophisticated argument handling, you could look at the 
optparse or argparse modules (but that's beyond the beginner 
level--something to keep in mind for later).





--
LinkedIn Profile: http://linkedin.com/in/pmjlavelle
Twitter: http://twitter.com/pmjlavelle

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


Re: [Tutor] checking if a variable is an integer?

2011-05-31 Thread Peter Lavelle

I think you could also use the type() function. See example below:

if type(yourvar) == int:
#Do stuff here if it is an integer

else:
 #Do something here if it is not an integer


Regards

Peter

On 31/05/11 22:23, Hans Barkei wrote:
I want to make  a program that finds all the prime numbers up to a 
number inputed by the user.
I want to know if it is an integer because that will tell me if it is 
divisible by that number or not.

***-**/Hans/-*


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


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