Re: [Tutor] reclassify values in an array

2011-09-01 Thread Sander Sweers
On Thu,   1 Sep 2011, 01:17:45 CEST, questions anon  
wrote:

> Firstly just make them zeros and ones, for example if the values in the
> array are less than 100 make them 0 and if greater than 100 make them 1.
> And then finally sum them together.
> I have attempted a few methods, see code below. Any feedback will be
> greatly appreciated.

*If* you do not need the array after you summed you can do something like below 
(untested!):

big_array = N.ma.concatenate(all_FFDI)
rusult = 0

for i in big_array:
if i >= 100:
result += 1

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


Re: [Tutor] reclassify values in an array

2011-09-01 Thread Peter Otten
questions anon wrote:

> I have been going round in circles trying to solve something that sounds
> simple. I have a huge array and I would like to reclassify the values.
> Firstly just make them zeros and ones, for example if the values in the
> array are less than 100 make them 0 and if greater than 100 make them 1.
> And then finally sum them together.
> I have attempted a few methods, see code below. Any feedback will be
> greatly appreciated.

>>> import numpy
>>> all_FFDI = numpy.arange(60).reshape((3,4,5))
>>> all_FFDI
array([[[ 0,  1,  2,  3,  4],
[ 5,  6,  7,  8,  9],
[10, 11, 12, 13, 14],
[15, 16, 17, 18, 19]],

   [[20, 21, 22, 23, 24],
[25, 26, 27, 28, 29],
[30, 31, 32, 33, 34],
[35, 36, 37, 38, 39]],

   [[40, 41, 42, 43, 44],
[45, 46, 47, 48, 49],
[50, 51, 52, 53, 54],
[55, 56, 57, 58, 59]]])
>>> all_FFDI >= 25
array([[[False, False, False, False, False],
[False, False, False, False, False],
[False, False, False, False, False],
[False, False, False, False, False]],

   [[False, False, False, False, False],
[ True,  True,  True,  True,  True],
[ True,  True,  True,  True,  True],
[ True,  True,  True,  True,  True]],

   [[ True,  True,  True,  True,  True],
[ True,  True,  True,  True,  True],
[ True,  True,  True,  True,  True],
[ True,  True,  True,  True,  True]]], dtype=bool)
>>> (all_FFDI >= 25).sum()
35


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


[Tutor] SOLVED and thank you was: Re: meaning of % in: if n % x == 0:

2011-09-01 Thread Lisi
This was meant to go to the list.  I did notrealise that it had not until I 
looked at the list just now and couln't see my reply.  Sorry, "delegbede", 
and sorry list.

On Wednesday 31 August 2011 Lisi wrote:
> ??  If either n or x or both were 0, and % were the same thing as *, the
> statement would be true, but from the context I don't think that % does
> mean the same as *, because * appears very soon after in the same fragment
> of code.

On Wednesday 31 August 2011 20:59:05 delegb...@dudupay.com wrote:
> % is a remainder division. I think its called modulo.

Yes!  Thank you. :-) 

I ought to have been able to guess that it was modulo - shows that I am indeed 
becoming slow-witted. :-(

Thank you, all three of you, for such fast and helpful responses.

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


[Tutor] Is there a test for hashability?

2011-09-01 Thread Richard D. Moores
The glossary defines "hashable" as:

hashable
An object is hashable if it has a hash value which never changes
during its lifetime (it needs a __hash__() method), and can be
compared to other objects (it needs an __eq__() method). Hashable
objects which compare equal must have the same hash value.

Hashability makes an object usable as a dictionary key and a set
member, because these data structures use the hash value internally.


All of Python’s immutable built-in objects are hashable, while no
mutable containers (such as lists or dictionaries) are. Objects which
are instances of user-defined classes are hashable by default; they
all compare unequal, and their hash value is their id().

I'm trying to write a general test for hashability. How can I test if
an object has both a  __hash__() method and an __eq__() method?

Thanks,

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


Re: [Tutor] Is there a test for hashability?

2011-09-01 Thread James Reynolds
On Thu, Sep 1, 2011 at 10:32 AM, Richard D. Moores wrote:

> The glossary defines "hashable" as:
>
> hashable
> An object is hashable if it has a hash value which never changes
> during its lifetime (it needs a __hash__() method), and can be
> compared to other objects (it needs an __eq__() method). Hashable
> objects which compare equal must have the same hash value.
>
> Hashability makes an object usable as a dictionary key and a set
> member, because these data structures use the hash value internally.
>
>
> All of Python’s immutable built-in objects are hashable, while no
> mutable containers (such as lists or dictionaries) are. Objects which
> are instances of user-defined classes are hashable by default; they
> all compare unequal, and their hash value is their id().
>
> I'm trying to write a general test for hashability. How can I test if
> an object has both a  __hash__() method and an __eq__() method?
>
> Thanks,
>
> Dick Moores
> ___
> Tutor maillist  -  Tutor@python.org
> To unsubscribe or change subscription options:
> http://mail.python.org/mailman/listinfo/tutor
>



To test for a method within any object you can just go like this:

>>> a = ()
>>> type(a)

>>> if '__hash__' in dir(a): print True
True
>>> if '__eq__' in dir(a): print True
True
>>>

But, I think the method you are approaching it from will only test for
hashability. For example, you could do this:

>>> a = []
>>> type(a)

>>> if '__hash__' in dir(a): print True
True
>>> if '__eq__' in dir(a): print True
True
>>>

and then do this:

>>> b = []
>>> c = {b:1}

Traceback (most recent call last):
  File "", line 1, in 
c = {b:1}
TypeError: unhashable type: 'list'

here is the dir for a list (not hashable):

>>> dir(b)
['__add__', '__class__', '__contains__', '__delattr__', '__delitem__',
'__delslice__', '__doc__', '__eq__', '__format__', '__ge__',
'__getattribute__', '__getitem__', '__getslice__', '__gt__', '__hash__',
'__iadd__', '__imul__', '__init__', '__iter__', '__le__', '__len__',
'__lt__', '__mul__', '__ne__', '__new__', '__reduce__', '__reduce_ex__',
'__repr__', '__reversed__', '__rmul__', '__setattr__', '__setitem__',
'__setslice__', '__sizeof__', '__str__', '__subclasshook__', 'append',
'count', 'extend', 'index', 'insert', 'pop', 'remove', 'reverse', 'sort']

here is the dir for a tuple (hashable):

>>> dir(())
['__add__', '__class__', '__contains__', '__delattr__', '__doc__', '__eq__',
'__format__', '__ge__', '__getattribute__', '__getitem__', '__getnewargs__',
'__getslice__', '__gt__', '__hash__', '__init__', '__iter__', '__le__',
'__len__', '__lt__', '__mul__', '__ne__', '__new__', '__reduce__',
'__reduce_ex__', '__repr__', '__rmul__', '__setattr__', '__sizeof__',
'__str__', '__subclasshook__', 'count', 'index']

What I would probably do though is use the built in method hash

so you could do something like this:

>>> a = 'a'
>>> b = ()
>>> c = []
>>> print type(a), type(b), type(c)
  
>>> print hash(a)
-468864544
>>> print hash(b)
3527539
>>> print hash(c)

Traceback (most recent call last):
  File "", line 1, in 
print hash(c)
TypeError: unhashable type: 'list'
>>>


You can then use try, except to catch it on an as needed basis.

Not sure if this answers the question you are asking though.
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Is there a test for hashability?

2011-09-01 Thread Richard D. Moores
Thanks, James, from your ideas I've come up with this function as a
general test for hashibility of any object:

def is_hashable(object):
try:
if hash(object):
return True
except TypeError:
return False

But is it?  It returns True for ints, floats, sets, tuples, strings,
functions; and False for lists

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


Re: [Tutor] Is there a test for hashability?

2011-09-01 Thread Hugo Arts
On Thu, Sep 1, 2011 at 5:28 PM, Richard D. Moores  wrote:
> Thanks, James, from your ideas I've come up with this function as a
> general test for hashibility of any object:
>
> def is_hashable(object):
>    try:
>        if hash(object):
>            return True
>    except TypeError:
>        return False
>
> But is it?  It returns True for ints, floats, sets, tuples, strings,
> functions; and False for lists
>
> Dick

Are you sure? In my testing it returns False for sets, but True for
frozensets as it should.
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Is there a test for hashability?

2011-09-01 Thread James Reynolds
On Thu, Sep 1, 2011 at 11:37 AM, Hugo Arts  wrote:

> On Thu, Sep 1, 2011 at 5:28 PM, Richard D. Moores 
> wrote:
> > Thanks, James, from your ideas I've come up with this function as a
> > general test for hashibility of any object:
> >
> > def is_hashable(object):
> >try:
> >if hash(object):
> >return True
> >except TypeError:
> >return False
> >
> > But is it?  It returns True for ints, floats, sets, tuples, strings,
> > functions; and False for lists
> >
> > Dick
>
> Are you sure? In my testing it returns False for sets, but True for
> frozensets as it should.
>

I agree with hugo, I just tested with all of these:

a = 'a'
b = []
c = 1
d = ()
e = set()
f = frozenset()

it gave the correct response for each

a = 'a' - True
b = [] - False
c = 1 - True
d = () - True
e = set() - False
f = frozenset() - True
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Is there a test for hashability?

2011-09-01 Thread Chris Fuller
On Thursday 01 September 2011, Richard D. Moores wrote:
> Thanks, James, from your ideas I've come up with this function as a
> general test for hashibility of any object:
> 
> def is_hashable(object):
> try:
> if hash(object):
> return True
> except TypeError:
> return False
> 
> But is it?  It returns True for ints, floats, sets, tuples, strings,
> functions; and False for lists
> 
> Dick
> ___
> Tutor maillist  -  Tutor@python.org
> To unsubscribe or change subscription options:
> http://mail.python.org/mailman/listinfo/tutor

You shouldn't be checking the truth value of the hash.  If it's zero, this 
function will fall through and return None!

def is_hashable(object):
   try:
hash(object):
return True
except TypeError:
return False

Is what you want.

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


Re: [Tutor] Is there a test for hashability?

2011-09-01 Thread Emile van Sebille

On 9/1/2011 11:30 AM Chris Fuller said...

On Thursday 01 September 2011, Richard D. Moores wrote:

Thanks, James, from your ideas I've come up with this function as a
general test for hashibility of any object:

def is_hashable(object):
 try:
 if hash(object):
 return True
 except TypeError:
 return False

But is it?  It returns True for ints, floats, sets, tuples, strings,
functions; and False for lists

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


You shouldn't be checking the truth value of the hash.  If it's zero, this
function will fall through and return None!

def is_hashable(object):
try:
 hash(object):
 return True
 except TypeError:
 return False

Is what you want.


You should, of course, express it as valid python code though. :)

def is_hashable(object):
try:
 hash(object)
 return True
 except TypeError:
 return False


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


Re: [Tutor] Quote of the Day version 1.0

2011-09-01 Thread Christopher King
I would use a tuple of dictionaries.
import random

quotes = (
   {'author':"Kahlil Gibran", 'quote':"A candle loses nothing of its light
when
lighting another."), #My favorite
   {'author':"Henrik Ibsen", 'quote':"The strongest man in the world is he
who stands
most alone."})

quote = random.choice(quotes)
print "{quote}\n\tBy {author}".format(**quote)

I use the dictionaries, because your not just storing a list of strings,
your storing two strings of different purposes. I store the dictionaries in
a tuple, because they are all quotes, but they currently never change during
the course of the program. Some quotes have been omitted due to my laziness.
I have not tested or debugged this code(I had to leave you something to do.)
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Is there a test for hashability?

2011-09-01 Thread Richard D. Moores
def is_hashable(object):
try:
hash(object)
return True
except TypeError:
return False

it is then. Thanks to all!

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


Re: [Tutor] [Python-ideas] aliasing

2011-09-01 Thread Christopher King
>
> 
> >>> list = [3,]
> >>> a = list
> >>> list[0] = 6
> >>> a[0]
> 3
> -
>
Slight error in my code. It should be.

>>> list = [3,]
>>> a = list
>>> list[0] = 6
>>> a[0]
6
-
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Is there a test for hashability?

2011-09-01 Thread Chris Fuller
On Thursday 01 September 2011, Chris Fuller wrote:
> On Thursday 01 September 2011, Richard D. Moores wrote:
> > Thanks, James, from your ideas I've come up with this function as a
> > general test for hashibility of any object:
> > 
> > def is_hashable(object):
> > try:
> > if hash(object):
> > return True
> > 
> > except TypeError:
> > return False
> > 
> > But is it?  It returns True for ints, floats, sets, tuples, strings,
> > functions; and False for lists
> > 
> > Dick
> > ___
> > Tutor maillist  -  Tutor@python.org
> > To unsubscribe or change subscription options:
> > http://mail.python.org/mailman/listinfo/tutor
> 
> You shouldn't be checking the truth value of the hash.  If it's zero, this
> function will fall through and return None!
> 
> def is_hashable(object):
>try:
> hash(object):
> return True
> except TypeError:
> return False
> 
> Is what you want.
> 
> Cheers
> ___
> Tutor maillist  -  Tutor@python.org
> To unsubscribe or change subscription options:
> http://mail.python.org/mailman/listinfo/tutor

*Ahem*

def is_hashable(object):
   try:
hash(object)
except TypeError:
return False

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


Re: [Tutor] Is there a test for hashability?

2011-09-01 Thread Richard D. Moores
On Thu, Sep 1, 2011 at 12:29, Chris Fuller
 wrote:


> *Ahem*
>
> def is_hashable(object):
>   try:
>        hash(object)
>    except TypeError:
>        return False
>
>    return True

Why is that preferred to

def is_hashable(object):
try:
hash(object)
return True
except TypeError:
return False

??

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


Re: [Tutor] Is there a test for hashability?

2011-09-01 Thread Chris Fuller
On Thursday 01 September 2011, Richard D. Moores wrote:
> On Thu, Sep 1, 2011 at 12:29, Chris Fuller
> 
>  wrote:
> > *Ahem*
> > 
> > def is_hashable(object):
> >   try:
> >hash(object)
> >except TypeError:
> >return False
> > 
> >return True
> 
> Why is that preferred to
> 
> def is_hashable(object):
> try:
> hash(object)
> return True
> except TypeError:
> return False
> 
> ??
> 
> Dick

It's a style issue, really.  Either would be fine, but I don't like mixing 
code-flow disrupting actions when it's easily avoided.

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


[Tutor] openpyxl

2011-09-01 Thread Helen Brown
Will someone share with me  a link where I can download subject in order for my 
script to run? Any assistance will help!
Thanks,
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


[Tutor] openpyxl

2011-09-01 Thread Helen Brown
Will someone share if there is a link where I can download to read a script 
with subject file?
Thanks,
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Is there a test for hashability?

2011-09-01 Thread Richard D. Moores
Ah. I'll follow you with that.

Thanks,

Dick

On Thu, Sep 1, 2011 at 15:42, Chris Fuller
 wrote:
> On Thursday 01 September 2011, Richard D. Moores wrote:
>> On Thu, Sep 1, 2011 at 12:29, Chris Fuller
>>
>>  wrote:
>> > *Ahem*
>> >
>> > def is_hashable(object):
>> >   try:
>> >        hash(object)
>> >    except TypeError:
>> >        return False
>> >
>> >    return True
>>
>> Why is that preferred to
>>
>> def is_hashable(object):
>>     try:
>>         hash(object)
>>         return True
>>     except TypeError:
>>         return False
>>
>> ??
>>
>> Dick
>
> It's a style issue, really.  Either would be fine, but I don't like mixing
> code-flow disrupting actions when it's easily avoided.
>
> Cheers
> ___
> 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


Re: [Tutor] Is there a test for hashability?

2011-09-01 Thread Steven D'Aprano

Richard D. Moores wrote:

I'm trying to write a general test for hashability. How can I test if
an object has both a  __hash__() method and an __eq__() method?



Just because an object has a __hash__ method doesn't mean it is 
guaranteed to be hashable. The method might (deliberately, or 
accidentally) fail and raise an exception.


>>> t = (1, 2, 3)
>>> t.__hash__

>>> hash(t)
-378539185

But:

>>> t = (1, 2, [3])
>>> t.__hash__

>>> hash(t)
Traceback (most recent call last):
  File "", line 1, in 
TypeError: list objects are unhashable


The only effective way to find out if an object is hashable is to try it 
and see.


Even more effective is to avoid trying to find out whether it is 
hashable, and just use it, as required, and deal with the error if it 
isn't. This is the "Easy to get forgiveness than to ask permission" 
model of error handling, as opposed to "Look before you leap".


For various reasons, LBYL can be risky... like in Quantum Mechanics, the 
very act of *looking* at an object might change its behaviour. (Methods 
can have side-effects.) So, even if is_hashable(obj) returns True, you 
can't *quite* be 100% certain that mydict[obj] will succeed until you 
try it.



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


Re: [Tutor] Is there a test for hashability?

2011-09-01 Thread Steven D'Aprano

Richard D. Moores wrote:

Thanks, James, from your ideas I've come up with this function as a
general test for hashibility of any object:

def is_hashable(object):
try:
if hash(object):
return True
except TypeError:
return False


No need for the "if hash" test, just try hash:

def is_hashable(obj):
try:
hash(object)
return True
except TypeError:
return False


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


Re: [Tutor] Is there a test for hashability?

2011-09-01 Thread Steven D'Aprano
On Fri, Sep 02, 2011 at 12:17:48PM +1000, Steven D'Aprano wrote:
> Richard D. Moores wrote:
> >Thanks, James, from your ideas I've come up with this function as a
> >general test for hashibility of any object:
> >
> >def is_hashable(object):
> >try:
> >if hash(object):
> >return True
> >except TypeError:
> >return False
> 
> No need for the "if hash" test, just try hash:

Er, except this was already discussed :) 

Sorry for the noise.


-- 
Steven

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


Re: [Tutor] openpyxl

2011-09-01 Thread Steven D'Aprano
On Thu, Sep 01, 2011 at 05:55:04PM -0700, Helen Brown wrote:
> Will someone share with me  a link where I can download subject in order for 
> my script to run? Any assistance will help!

Did you try googling for it?

http://duckduckgo.com/?q=openpyxl
http://www.bing.com/search?q=openpyxl
http://au.search.yahoo.com/search?p=openpyxl
http://www.dogpile.com/info.dogpl/search/web?q=openpyxl

Even Google can find it :)

http://www.google.com/search?q=openpyxl

These days, I recommend using Duck Duck Go because, unlike Google, it 
doesn't track or bubble your searches:

http://donttrack.us/
http://dontbubble.us/



-- 
Steven

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


[Tutor] use of logging module is shared by all?

2011-09-01 Thread James Hartley
I'm just needing to verify some behavior.

Functionality within the logging module is exercised by calling functions
defined within the module itself.  I am using SQLAlchemy for database
access, but it can be configured to dump out intermediate access information
& queries to the logging module -- which is great.  It really helps in
debugging.

However, if I want to write to a log which is separate from what SQLAlchemy
is doing, am I correct stating that I will not be able to do so through the
logging module?

Thanks for any insight which can be shared.

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


Re: [Tutor] Is there a test for hashability?

2011-09-01 Thread Richard D. Moores
On Thu, Sep 1, 2011 at 18:08, Steven D'Aprano  wrote:
> Richard D. Moores wrote:
>>
>> I'm trying to write a general test for hashability. How can I test if
>> an object has both a  __hash__() method and an __eq__() method?
>
>
> Just because an object has a __hash__ method doesn't mean it is guaranteed
> to be hashable. The method might (deliberately, or accidentally) fail and
> raise an exception.
>
 t = (1, 2, 3)
 t.__hash__
> 
 hash(t)
> -378539185
>
> But:
>
 t = (1, 2, [3])
 t.__hash__
> 
 hash(t)
> Traceback (most recent call last):
>  File "", line 1, in 
> TypeError: list objects are unhashable
>
>
> The only effective way to find out if an object is hashable is to try it and
> see.
>
> Even more effective is to avoid trying to find out whether it is hashable,
> and just use it, as required, and deal with the error if it isn't. This is
> the "Easy to get forgiveness than to ask permission" model of error
> handling, as opposed to "Look before you leap".
>
> For various reasons, LBYL can be risky... like in Quantum Mechanics, the
> very act of *looking* at an object might change its behaviour. (Methods can
> have side-effects.) So, even if is_hashable(obj) returns True, you can't
> *quite* be 100% certain that mydict[obj] will succeed until you try it.

Steven,

Do you have an actual example of an actual object for which "my"
is_hashable(object) function would return the wrong answer?

>>> def is_hashable(object):
... try:
... hash(object)
... except TypeError:
... return False
... return True
...
>>> is_hashable((1, 2, [3]))
False
>>>

Is that one?

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


Re: [Tutor] use of logging module is shared by all?

2011-09-01 Thread Peter Otten
James Hartley wrote:

> I'm just needing to verify some behavior.
> 
> Functionality within the logging module is exercised by calling functions
> defined within the module itself.  I am using SQLAlchemy for database
> access, but it can be configured to dump out intermediate access
> information
> & queries to the logging module -- which is great.  It really helps in
> debugging.
> 
> However, if I want to write to a log which is separate from what
> SQLAlchemy is doing, am I correct stating that I will not be able to do so
> through the logging module?
> 
> Thanks for any insight which can be shared.

Loggers are typically organized in a tree; sqlalchemy will probably log to

logging.getLogger("sqlalchemy").warn("whatever")

or something like

logging.getLogger("sqlalchemy.some.sublogger").critical("test")

You can prevent the rootlogger from seeing these messages with

sq = logging.getLogger("sqlalchemy")
sq.propagate = False

If for example you want to see messages in stdout by default, but write 
SQLAlchemy's messages to a file you'd do (untested)

import logging
import sys
logging.basicConfig(stream=sys.stdout)

sq = logging.getLogger("sqalchemy")
sqhandler = logging.FileHandler("alchemy.log")
sqformatter = logging.Formatter(logging.BASIC_FORMAT)
sqhandler.setFormatter(sqformatter)
sq.addHandler(sqhandler)
sq.propagate = False

sq.critical("alchemy") # goes to file alchemy.log
logging.getLogger("mine").critical("mine") # goes to stdout


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


Re: [Tutor] use of logging module is shared by all?

2011-09-01 Thread James Hartley
Thanks, Peter for taking the time to respond. I need to study the reference
further, & your comments pointed out some of my misconceptions.  Thank you
for clearing up some of my half-researched understanding.

Jim

On Thu, Sep 1, 2011 at 10:53 PM, Peter Otten <__pete...@web.de> wrote:

> James Hartley wrote:
>
> > I'm just needing to verify some behavior.
> >
> > Functionality within the logging module is exercised by calling functions
> > defined within the module itself.  I am using SQLAlchemy for database
> > access, but it can be configured to dump out intermediate access
> > information
> > & queries to the logging module -- which is great.  It really helps in
> > debugging.
> >
> > However, if I want to write to a log which is separate from what
> > SQLAlchemy is doing, am I correct stating that I will not be able to do
> so
> > through the logging module?
> >
> > Thanks for any insight which can be shared.
>
> Loggers are typically organized in a tree; sqlalchemy will probably log to
>
> logging.getLogger("sqlalchemy").warn("whatever")
>
> or something like
>
> logging.getLogger("sqlalchemy.some.sublogger").critical("test")
>
> You can prevent the rootlogger from seeing these messages with
>
> sq = logging.getLogger("sqlalchemy")
> sq.propagate = False
>
> If for example you want to see messages in stdout by default, but write
> SQLAlchemy's messages to a file you'd do (untested)
>
> import logging
> import sys
> logging.basicConfig(stream=sys.stdout)
>
> sq = logging.getLogger("sqalchemy")
> sqhandler = logging.FileHandler("alchemy.log")
> sqformatter = logging.Formatter(logging.BASIC_FORMAT)
> sqhandler.setFormatter(sqformatter)
> sq.addHandler(sqhandler)
> sq.propagate = False
>
> sq.critical("alchemy") # goes to file alchemy.log
> logging.getLogger("mine").critical("mine") # goes to stdout
>
>
> ___
> 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