Re: [Tutor] logging module, how to print line numbers?

2006-08-31 Thread Hans Fangohr
Hi Kent,

> > I have some trouble with the logging module.
> >
> > When I run this program with python2.3:
> >
> > #
> > import logging,sys
> > formatter = logging.Formatter('%(name)s :%(asctime)s %(filename)s
> > %(lineno)s %(levelname)s  %(message)s')
> > stdout_handler = logging.StreamHandler(sys.stdout)
> > stdout_handler.setFormatter(formatter)
> > logger=logging.getLogger('')
> > logger.addHandler(stdout_handler)
> > logger.setLevel(logging.DEBUG)
> > logging.debug('A debug message')
> > logging.info('Some information')
> > logging.warning('A shot across the bows')
> > #
> >
> > I get the following output:
> >
> > root :2006-08-31 20:20:15,085 __init__.py 988 DEBUG  A debug message
> > root :2006-08-31 20:20:15,085 __init__.py 988 INFO  Some information
> > root :2006-08-31 20:20:15,085 __init__.py 988 WARNING  A shot across thebows
> >
> > Note that the line number always appears as 988. I'd like it to be the
> > line number where the logging command has been executed. The documentation
> > says that %(lineno)d should work 'if available'.
> >
> When I run your program with Python 2.3.4 on WinXP I get the expected
> output:
> root :2006-08-31 18:51:27,046 logging.py 8 DEBUG  A debug message
> root :2006-08-31 18:51:27,046 logging.py 9 INFO  Some information
> root :2006-08-31 18:51:27,046 logging.py 10 WARNING  A shot across the bows
Interesting, thank you.
>
> How are you running the program? What OS? What Python (2.3.??)
This was Debian Etch, python 2.3.4. I got the same problem with python
2.4.4 on the same system.

I have just tried this on Mac OS X (with python from fink), and it works
fine there.

> Looking at the source (Python23\Lib\logging\__init__.py), the line
> number is pulled out of the stack by walking up the stack looking for a
> frame whose filename is different from _srcfile. What do you get if you
> print logging._srcfile and logging.__file__?
This was useful information. I played around with the __init__.py file and
the relevant code in there (around line 970).

It turns out that the problem disappears if I remove the

As a result, python needs to read __init.py which appears to work
correctly.

So in summary, it seems that the precompiled
/usr/lib/python2.3/logging/__init__.pyc file was somehow buggy.

I have tried to reproduce the same problem on another machine with Debian
Etch, and it doesn't exist there.

To confuse matters further, (as stated above), I get the same error on
that 'faulty' machine with python2.4.

In summary, the code I emailed initially works fine but it seems that on
the machine I used for testing something was broken with the precompiled
__init__.py file of the logging module. This seems to be an issue with the
debian package.

Many thanks to all who replied, and in particular to Kent who put me on
the right track of solving the problem.

Cheers,

Hans



>
> Kent
> > Is this generally not available? Can I make it available? If so, how?
> >
> > Any advice welcome.
> >
> > Many thanks in advance,
> >
> > Hans
> >
> >
> >
> >
> >
> > ___
> > Tutor maillist  -  Tutor@python.org
> > http://mail.python.org/mailman/listinfo/tutor
> >
> >
> >
>
>
> ___
> Tutor maillist  -  Tutor@python.org
> http://mail.python.org/mailman/listinfo/tutor
>
>
>

--
Hans Fangohr
School of Engineering Sciences
University of Southampton
Phone: +44 (0) 238059 8345

Email: [EMAIL PROTECTED]
http://www.soton.ac.uk/~fangohr




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


Re: [Tutor] Python decorator

2006-08-31 Thread Sean Perry
János Juhász wrote:
> Hi,
> 
> I have just started to play with TurboGears - it is really nice - and I 
> couldn't understand the decorators used by it.
> I have tried to interpret the http://wiki.python.org/moin/PythonDecorators 
> about decorators, but it is too difficult for me.
> 
> May someone explain decorators in very sortly, what is it for and why ?
> Do I need it anyway ?
> 

A decorator is a function that takes a function and returns a new, 
modified function.

In Django (a similar framework) there are a few places where decorators 
are used.

@login_required
def foo_view(args):
   # a view that must be authenticated
   # more code here

This means that before foo_view() is ran the function login_required is 
run. Which in this case will redirect to a login screen if the user is 
not currently authenticated.

here's the Django code:
def user_passes_test(test_func, login_url=LOGIN_URL):
 """
 Decorator for views that checks that the user passes the given test,
 redirecting to the log-in page if necessary. The test should be a 
callable
 that takes the user object and returns True if the user passes.
 """
 def _dec(view_func):
 def _checklogin(request, *args, **kwargs):
 if test_func(request.user):
 return view_func(request, *args, **kwargs)
 return HttpResponseRedirect('%s?%s=%s' % (login_url, 
REDIRECT_FIELD_
NAME, quote(request.get_full_path(
 _checklogin.__doc__ = view_func.__doc__
 _checklogin.__dict__ = view_func.__dict__

 return _checklogin
 return _dec

login_required = user_passes_test(lambda u: u.is_authenticated())


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


Re: [Tutor] logging module, how to print line numbers?

2006-08-31 Thread ralf . steckel
Dear Hans,

i haven't worked with the logging module yet but as a guess from what you write 
in your message:

according to the documentation the format statement for lineno should be 
%(lineno)d, but you use %(lineno)s.

Best regards,

Ralf

> Hi,
> 
> I have some trouble with the logging module.
> 
> When I run this program with python2.3:
> 
> #
> import logging,sys
> formatter = logging.Formatter('%(name)s :%(asctime)s %(filename)s
> %(lineno)s %(levelname)s  %(message)s')
> stdout_handler = logging.StreamHandler(sys.stdout)
> stdout_handler.setFormatter(formatter)
> logger=logging.getLogger('')
> logger.addHandler(stdout_handler)
> logger.setLevel(logging.DEBUG)
> logging.debug('A debug message')
> logging.info('Some information')
> logging.warning('A shot across the bows')
> #
> 
> I get the following output:
> 
> root :2006-08-31 20:20:15,085 __init__.py 988 DEBUG  A debug message
> root :2006-08-31 20:20:15,085 __init__.py 988 INFO  Some information
> root :2006-08-31 20:20:15,085 __init__.py 988 WARNING  A shot across thebows
> 
> Note that the line number always appears as 988. I'd like it to be the
> line number where the logging command has been executed. The documentation
> says that %(lineno)d should work 'if available'.
> 
> Is this generally not available? Can I make it available? If so, how?
> 
> Any advice welcome.
> 
> Many thanks in advance,
> 
> Hans
> 

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


Re: [Tutor] logging module, how to print line numbers?

2006-08-31 Thread Kent Johnson
Hans Fangohr wrote:
> Hi,
>
> I have some trouble with the logging module.
>
> When I run this program with python2.3:
>
> #
> import logging,sys
> formatter = logging.Formatter('%(name)s :%(asctime)s %(filename)s
> %(lineno)s %(levelname)s  %(message)s')
> stdout_handler = logging.StreamHandler(sys.stdout)
> stdout_handler.setFormatter(formatter)
> logger=logging.getLogger('')
> logger.addHandler(stdout_handler)
> logger.setLevel(logging.DEBUG)
> logging.debug('A debug message')
> logging.info('Some information')
> logging.warning('A shot across the bows')
> #
>
> I get the following output:
>
> root :2006-08-31 20:20:15,085 __init__.py 988 DEBUG  A debug message
> root :2006-08-31 20:20:15,085 __init__.py 988 INFO  Some information
> root :2006-08-31 20:20:15,085 __init__.py 988 WARNING  A shot across thebows
>
> Note that the line number always appears as 988. I'd like it to be the
> line number where the logging command has been executed. The documentation
> says that %(lineno)d should work 'if available'.
>   
When I run your program with Python 2.3.4 on WinXP I get the expected 
output:
root :2006-08-31 18:51:27,046 logging.py 8 DEBUG  A debug message
root :2006-08-31 18:51:27,046 logging.py 9 INFO  Some information
root :2006-08-31 18:51:27,046 logging.py 10 WARNING  A shot across the bows

How are you running the program? What OS? What Python (2.3.??)

Looking at the source (Python23\Lib\logging\__init__.py), the line 
number is pulled out of the stack by walking up the stack looking for a 
frame whose filename is different from _srcfile. What do you get if you 
print logging._srcfile and logging.__file__?

Kent
> Is this generally not available? Can I make it available? If so, how?
>
> Any advice welcome.
>
> Many thanks in advance,
>
> Hans
>
>
>
>
>
> ___
> 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] GUI Programing

2006-08-31 Thread John Fouhy
On 01/09/06, Amadeo Bellotti <[EMAIL PROTECTED]> wrote:
> I'm going to try some GUI programming does anyone know where the start like
> using tk or wx or what ever i want it to it will run on Windows UNIX and Mac
> systems can you tell me whats best to use and give me a link to a good
> tutorial?

Tkinter is (IMO) easier to learn.  In particular, check out "Thinking
in Tkinter" (google for it); it's an excellent way to learn Tkinter.

Tkinter is also very basic.  wx has many more widgets and controls
available.  wx also looks a lot better.  But the documentation for wx
isn't very good.  wxpython has been evolving -- it started out as a
direct python port of some C++ libraries, and
has been becoming more pythonic as time goes by.  So if you go looking
for example code, you could find something written in the modern,
pythonic style, or you could get something written in the traditional,
C++ style.  Until you learn to recognise the latter and transform it
to the former, you may find learning from the web difficult.

Both Tkinter and wxpython should work across all platforms.

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


Re: [Tutor] GUI Programing

2006-08-31 Thread Henry Dominik



This guy has been documenting his progress as he 
learns Python GUI programming. See if you can pick up a few tips http://www.learningpython.com/
 

  - Original Message - 
  From: 
  Amadeo Bellotti 
  To: Tutor 
  Sent: Thursday, August 31, 2006 9:12 
  PM
  Subject: [Tutor] GUI Programing
  I'm going to try some GUI programming does anyone know where 
  the start like using tk or wx or what ever i want it to it will run on Windows 
  UNIX and Mac systems can you tell me whats best to use and give me a link to a 
  good tutorial?Thanks
  
  

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


Re: [Tutor] Securing a Computer...

2006-08-31 Thread Luke Paireepinart
On 8/28/06, [EMAIL PROTECTED] <[EMAIL PROTECTED]> wrote:

I just got into high school, and the network and all the computers aren't secure at all...I'm trying to make a program that password protects the computer after an inactivity time, but there are some specific things I can't find how I'm supposed to do it.
Windoze has this feature built in.  It's called 'locking' the computer.Look into it. 

1. Freeze/unfreeze Windows processes OR start/end Windows processes (Preferrably the first)the user may need oto be on an administrative account for you to be able to end processes.
At a school that's unlikely.
2. Turn off/restart the computerAgain this is implemented by the 'lock' feature in Windows. 

3. I can probably find this somewhere else, but I surprisingly haven't: Make a timer that resets when the mouse     moves or a key is pressed (That still resets when the window is minimized, inactive, etc.)
You could make a Pygame program that does a grab_all, or tkinter for that matter.this probably won't work though.I won't go into detail.-Luke

Check out AOL.com today. Breaking news, video search, pictures, email and IM. All on demand. Always Free.





___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] logging module, how to print line numbers?

2006-08-31 Thread Hans Fangohr
Hi,

I have some trouble with the logging module.

When I run this program with python2.3:

#
import logging,sys
formatter = logging.Formatter('%(name)s :%(asctime)s %(filename)s
%(lineno)s %(levelname)s  %(message)s')
stdout_handler = logging.StreamHandler(sys.stdout)
stdout_handler.setFormatter(formatter)
logger=logging.getLogger('')
logger.addHandler(stdout_handler)
logger.setLevel(logging.DEBUG)
logging.debug('A debug message')
logging.info('Some information')
logging.warning('A shot across the bows')
#

I get the following output:

root :2006-08-31 20:20:15,085 __init__.py 988 DEBUG  A debug message
root :2006-08-31 20:20:15,085 __init__.py 988 INFO  Some information
root :2006-08-31 20:20:15,085 __init__.py 988 WARNING  A shot across thebows

Note that the line number always appears as 988. I'd like it to be the
line number where the logging command has been executed. The documentation
says that %(lineno)d should work 'if available'.

Is this generally not available? Can I make it available? If so, how?

Any advice welcome.

Many thanks in advance,

Hans





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


[Tutor] GUI Programing

2006-08-31 Thread Amadeo Bellotti
I'm going to try some GUI programming does anyone know where the start
like using tk or wx or what ever i want it to it will run on Windows
UNIX and Mac systems can you tell me whats best to use and give me a
link to a good tutorial?

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


Re: [Tutor] Python decorator

2006-08-31 Thread Kent Johnson
János Juhász wrote:
> Hi,
>
> I have just started to play with TurboGears - it is really nice - and I 
> couldn't understand the decorators used by it.
> I have tried to interpret the http://wiki.python.org/moin/PythonDecorators 
> about decorators, but it is too difficult for me.

That's not the best starting point, it is more of historical interest. 
There was a huge debate about the syntax to use for decorators, it is 
summarized on that page.

See if this makes more sense:
http://docs.python.org/whatsnew/node6.html

You might also be interested in the examples in PEP 318 and this wiki page:
http://wiki.python.org/moin/PythonDecoratorLibrary?highlight=%28decorator%29

Kent

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


[Tutor] Python decorator

2006-08-31 Thread János Juhász

Hi,

I have just started to play with
TurboGears - it is really nice - and I couldn't understand the decorators
used by it.
I have tried to interpret the http://wiki.python.org/moin/PythonDecorators
about decorators, but it is too difficult for me.

May someone explain decorators
in very sortly, what is it for and why ?
Do I need it anyway ?

Yours sincerely, 
__
Janos Juhasz 
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Unicode problems

2006-08-31 Thread Kent Johnson
Ed Singleton wrote:
> On 8/29/06, Kent Johnson <[EMAIL PROTECTED]> wrote:
>>> The main problem  I am having is in getting python not to give an
>>> error when it encounters a sterling currency sign (£, pound sign here
>>> in UK), which I suspect might be some wider problem on the mac as when
>>> I type that character in the terminal it shows a # (but in Python it
>>> shows a £).
>>>   
>> Where is the pound sign coming from? What encoding is it in? What do you
>> mean, in Python it shows £? You said Python gives an error...Fixing your
>> first problem may not help this one without a bit more digging... (BTW
>> in the US a # is sometimes called a 'pound sign', maybe the computer is
>> trying to translate for you ;) - though it is for pound weight, not
>> pound sterling.)
>> 
>
> The pound sign is in the source code in a string, or in a text file I
> was reading in.  Both should be in utf-8 as I save all files to that
> by default.  I think it was (hopefully) just that python was choking
> on printing the character (I was printing everything for debugging
> purposes).
>   
You also need to tell Python that the file is in UTF-8 by putting an 
encoding declaration at the top of the file.

# -*- coding: utf-8 -*-

You probably want to make the strings Unicode strings as well, e.g. u'xxx'.
> If I type "£" into a text document and copy and paste it to the python
> console, it comes out as " £" (with a space).  If I copy and paste it
> back, the space is gone.
>   
Sounds like maybe you are pasting Unicode (two bytes) and the console 
interprets it as two characters.
> If I type "test £" (without quotes) into a text document and copy and
> paste it to the console it comes out as "#test" and goes to a new
> line, as if I had pressed enter.
>   
That on is very strange.

By the way you can explicitly control the conversion on output by using e.g.

print someString.encode('utf-8')

Finally, please keep the discussion on list.

Kent
> I'll keep digging and trying things out.
>
> Thanks
>
> Ed
>   


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


Re: [Tutor] IP-range

2006-08-31 Thread Kent Johnson
Øyvind wrote:
> Hello.
>
> I have a database where I have some IP-ranges. Then I have some logs over
> IPs from customers and need to connect the two.

Also you might be interested in
http://c0re.23.nu/c0de/IPy/

Kent

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


Re: [Tutor] IP-range

2006-08-31 Thread Kent Johnson
Øyvind wrote:
> Hello.
>
> I have a database where I have some IP-ranges. Then I have some logs over
> IPs from customers and need to connect the two.
>
> So, for example:
>
> Range 1:
> 123.132.122.4-123.132.122.255
>
> How do I check if IP 123.132.122.58 is a part of that? I have thought
> about 4 if statements:
>
> split(.)
> if ip[0] in iprange_from[0] and iprange_to[0]:
>   if ip[1] in iprange_from[1] and iprange_to[1]:
> if ip[2] in iprange_from[2] and iprange_to[2]:
>   if ip[3] in iprange_from[3] and iprange_to[3]:
> then ok
>
> But that seems silly. Is there some better way?

If the IPs are stored as lists or tuples of integers you can compare 
them directly and Python will do the right thing. If they are strings, a 
simple helper function can convert them to lists:
In [1]: def ipStrToList(ip):
   ...: return map(int, ip.split('.'))
   ...:

In [2]: ipStrToList('123.132.122.4')
Out[2]: [123, 132, 122, 4]

In [3]: lower = _

In [4]: upper = ipStrToList('123.132.122.255')

In [5]: lower <= ipStrToList('123.132.122.58') <= upper
Out[5]: True

In [6]: lower <= ipStrToList('123.132.123.58') <= upper
Out[6]: False

Kent

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


Re: [Tutor] IP-range

2006-08-31 Thread Danny Yoo



On Thu, 31 Aug 2006, �yvind wrote:

I have a database where I have some IP-ranges. Then I have some logs 
over IPs from customers and need to connect the two.


So, for example:

Range 1:
123.132.122.4-123.132.122.255


Hello,

You might want to consider using tuple comparison.  For example:

###

(1, 2) <= (1, 3) <= (1, 4)

True
###

Hope this helps!___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


[Tutor] IP-range

2006-08-31 Thread Øyvind
Hello.

I have a database where I have some IP-ranges. Then I have some logs over
IPs from customers and need to connect the two.

So, for example:

Range 1:
123.132.122.4-123.132.122.255

How do I check if IP 123.132.122.58 is a part of that? I have thought
about 4 if statements:

split(.)
if ip[0] in iprange_from[0] and iprange_to[0]:
  if ip[1] in iprange_from[1] and iprange_to[1]:
if ip[2] in iprange_from[2] and iprange_to[2]:
  if ip[3] in iprange_from[3] and iprange_to[3]:
then ok

But that seems silly. Is there some better way?

Thanks in advance


-- 
This email has been scanned for viruses & spam by Domenebutikken - 
www.domenebutikken.no
Denne e-posten er sjekket for virus & spam av Domenebutikken - 
www.domenebutikken.no

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