using pdb and catching exception

2007-12-01 Thread Amit Gupta
Py'ites

I am using pdb to check my code, and I would like to put a statement
like equivalent of "C++gdb>catch throw".

Basically, I would like debugger to start as soon as an exception is
thrown. How may I do it?


Thanks
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: using pdb and catching exception

2007-12-03 Thread Amit Gupta
On Dec 1, 11:14 pm, Frank Millman <[EMAIL PROTECTED]> wrote:
> See this post from less than a week ago.
>
> http://tinyurl.com/2zyr7u
>
> I think that the message from Diez B. Roggisch has what you are
> looking for.
>
> Frank Millman

Thanks Frank. But again, this results into stack-track when the
exception is caught. On the other hand, I would like the debug-trace
just before throwing the exception. As a case, I might be debugging
code, where the programmar forgot to handle an exception and it is
being caught way up in the flow (with generic catch block)
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: using pdb and catching exception

2007-12-03 Thread Amit Gupta
On Dec 3, 11:10 am, Amit Gupta <[EMAIL PROTECTED]> wrote:
>
>
> Thanks Frank. But again, this results into stack-track when the
> exception is caught. On the other hand, I would like the debug-trace
> just before throwing the exception. As a case, I might be debugging
> code, where the programmar forgot to handle an exception and it is
> being caught way up in the flow (with generic catch block)

One thing that I can guess is to put a breakpoint on function "raise".
I assume all of the exceptions are thrown by "raise" and raise is not
a keyword, instead a function.

Will it work?
-- 
http://mail.python.org/mailman/listinfo/python-list


getting all user defined attributes of a class

2008-02-06 Thread Amit Gupta
Hi

How do I get user defined attributes of a class? e.g

Class A(object) :
  self.x = 1
--

I want something like:
  for userattrib in A.getAllUserAttribute() :
print userattrib

My question is, is there a builtin function, called
getAllUserAttributes?

Thanks
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: getting all user defined attributes of a class

2008-02-06 Thread Amit Gupta
On Feb 6, 2:15 pm, Marc 'BlackJack' Rintsch <[EMAIL PROTECTED]> wrote:
> On Wed, 06 Feb 2008 14:07:23 -0800, Amit Gupta wrote:
> > Class A(object) :
> >   self.x = 1
>
> This is not valid Python code.
>
> > I want something like:
> >   for userattrib in A.getAllUserAttribute() :
> > print userattrib
>
> > My question is, is there a builtin function, called
> > getAllUserAttributes?
>
> No and there can't be since the attributes you seem to be interested in
> don't exist until an instance is created.
>
> Ciao,
> Marc 'BlackJack' Rintsch

My mistake:

I should make class A as:
class A (object) :
  x = 1

Now, x is class attribute and I am looking for some-way to filter non-
user-defined attributes.

e.g.g if I do
for attr in a.__dict__ :
  print attr


I will also get

__module__, __weakref__ and others including "x"

Thanks
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: getting all user defined attributes of a class

2008-02-06 Thread Amit Gupta
On Feb 6, 2:55 pm, "Diez B. Roggisch" <[EMAIL PROTECTED]> wrote:
> Amit Gupta schrieb:
>
>
>
> > On Feb 6, 2:15 pm, Marc 'BlackJack' Rintsch <[EMAIL PROTECTED]> wrote:
> >> On Wed, 06 Feb 2008 14:07:23 -0800, Amit Gupta wrote:
> >>> Class A(object) :
> >>>   self.x = 1
> >> This is not valid Python code.
>
> >>> I want something like:
> >>>   for userattrib in A.getAllUserAttribute() :
> >>> print userattrib
> >>> My question is, is there a builtin function, called
> >>> getAllUserAttributes?
> >> No and there can't be since the attributes you seem to be interested in
> >> don't exist until an instance is created.
>
> >> Ciao,
> >> Marc 'BlackJack' Rintsch
>
> > My mistake:
>
> > I should make class A as:
> > class A (object) :
> >   x = 1
>
> > Now, x is class attribute and I am looking for some-way to filter non-
> > user-defined attributes.
>
> > e.g.g if I do
> > for attr in a.__dict__ :
> >   print attr
>
> > I will also get
>
> > __module__, __weakref__ and others including "x"
>
> Just create an empty class, gather all attribute names from that and
> then subtract that set of names from the names you get from a "real" class.
>
> Dize

Fine. This is a hack. I am looking if python language itself provides
any built-in function for this. E.g.:

When I do help on some built-in function, it displays that function is
built_in. Can that information get accessed using a function? (now,
don't ask me to store help-output in buffer and grep for built-in).


A
-- 
http://mail.python.org/mailman/listinfo/python-list


loading dictionary from a file

2008-02-06 Thread Amit Gupta
Need a python trick, if it exists:

I have a file that stores key, value in following format
--
"v1" : "k1",
"v2" : "k2"
--

Is there a way to directly load this file as dictionary in python. I
could do (foreach line in file, split by ":" and then do dictionary
insert). Wondering, if some python built-in function can just read a
valid dictionary-file and load it?

Thanks
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: loading dictionary from a file

2008-02-06 Thread Amit Gupta
On Feb 6, 5:33 pm, Ben Finney <[EMAIL PROTECTED]>
wrote:
> Amit Gupta <[EMAIL PROTECTED]> writes:
> > Need a python trick, if it exists:
>
> > I have a file that stores key, value in following format
> > --
> > "v1" : "k1",
> > "v2" : "k2"
> > --
>
> > Is there a way to directly load this file as dictionary in python.
>
> That input looks almost like valid JSON http://json.org/>.
>
> If you can easily massage it into JSON format, you can use the Python
> JSON library http://cheeseshop.python.org/pypi/python-json>:
>
> import json
>
> input_text = open('foo.txt').read()
> input_json = "{%(input_text)s}" % vars()
>
> reader = json.JsonReader()
> data = reader.read(input_json)
>
> If the 'input_json' above actually is valid JSON, that will give the
> corresponding Python data object.
>
> This avoids the massive security hole of performing 'eval' on
> arbitrary user input; the input isn't executed, merely parsed (as
> JSON) to create a data object.
>
> --
>  \  "I busted a mirror and got seven years bad luck, but my lawyer |
>   `\thinks he can get me five." --Steven Wright |
> _o__)  |
> Ben Finney

Great Inputs from both the posters.

Thanks
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: re question

2008-02-07 Thread Amit Gupta
On Feb 7, 10:38 am, Amit Gupta <[EMAIL PROTECTED]> wrote:
> Python'ites
>
> I searched around "google" to find the answer to this question, but I
> can't:
>
> I have a named regexp : x = re.compile("(?P[a-z]+)")
>
> What I want is an iterator, that can return me both the "groupname"
> and the matched string,  e.g:
>
> m = x.search("aa")
>
> Somehow, I want to get
> {"me" : "aa"}, either as dictionary or some iterable form.
>
> All I found is, I need to know the "groupname" to get the
> corresponding match. Any help is appreciated.
>
> A

Got It. re.search() has a function groupdict(), doing precisely that.
-- 
http://mail.python.org/mailman/listinfo/python-list


re question

2008-02-07 Thread Amit Gupta
Python'ites

I searched around "google" to find the answer to this question, but I
can't:

I have a named regexp : x = re.compile("(?P[a-z]+)")

What I want is an iterator, that can return me both the "groupname"
and the matched string,  e.g:

m = x.search("aa")

Somehow, I want to get
{"me" : "aa"}, either as dictionary or some iterable form.


All I found is, I need to know the "groupname" to get the
corresponding match. Any help is appreciated.

A
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: getting all user defined attributes of a class

2008-02-07 Thread Amit Gupta
On Feb 7, 12:28 am, grflanagan <[EMAIL PROTECTED]> wrote:
> On Feb 6, 11:07 pm, Amit Gupta <[EMAIL PROTECTED]> wrote:
>
> > Hi
>
> > How do I get user defined attributes of a class? e.g
>
> > Class A(object) :
> >   self.x = 1
> > --
>
> > I want something like:
> >   for userattrib in A.getAllUserAttribute() :
> > print userattrib
[..]
>
> HTH
>
> Gerard

Thanks. What I found is: If I call iterate over the __dict__ of the
instance of the class, I only get user-atttributes and not built-in
attributes. I have an instance of that class, anyway, so this will do.
However, I wonder if I am getting just lucky and this might change in
future. In that regard the solution provides by all posters above
might well be more robust.

A
-- 
http://mail.python.org/mailman/listinfo/python-list


seperate directory for .pyc files

2008-02-15 Thread Amit Gupta
Python'ites

Is there an environment variable or some settings, that python can use
to know the directory name for dumping .pyc files.

Not a hard-requirement, I just don't like pyc files alongwith py files
in my work area.

Thanks
A
-- 
http://mail.python.org/mailman/listinfo/python-list


packing things back to regular expression

2008-02-20 Thread Amit Gupta
Hi

I wonder if python has a function to pack things back into regexp,
that has group names.

e.g:
exp = ([a-z]+)
compiledexp = re.compile(exp)

Now, I have a dictionary "mytable = {"a" : "myname"}

Is there a way in re module, or elsewhere, where I can have it match
the contents from dictionary to the re-expression (and check that it
matches the rules) and than return the substituted string?

e.g
>> re.SomeNewFunc(compilexp, mytable)
"myname"
>> mytable = {"a" : "1"}
>> re.SomeNewFunc(compileexp, mytable)
ERROR



Thanks
A
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: packing things back to regular expression

2008-02-20 Thread Amit Gupta
Before I read the message: I screwed up.

Let me write again

>> x = re.compile("CL(?P[a-z]+)")
# group name "name1" is attached to the match of lowercase string of
alphabet
# Now I have a dictionary saying {"name1", "iamgood"}
# I would like a function, that takes x and my dictionary and return
"CLiamgood"
# If my dictionary instead have {"name1", "123"}, it gives error on
processingit
#
# In general, I have reg-expression where every non-trivial match has
a group-name. I want to do the reverse of reg-exp match. The function
can take reg-exp and replace the group-matches from dictionary
# I hope, this make it clear.

-- 
http://mail.python.org/mailman/listinfo/python-list


unitests don't run under pdb

2008-02-20 Thread Amit Gupta
Hi

I have a unitest file: If I do

python testname.py : the unitests runs as usual and I get the
following results:
--
Ran 2 tests in 0.024s

OK


However, if I do "python -m pdb testnames.py": I get
ython -m pdb testnames.py
> /s/nd6/amit/pyiglu/testnames.py(1)()
-> import unittest
(Pdb) c

--
Ran 0 tests in 0.000s

OK
---



Anything else, I should be doing (python version 2.5.1)


Thanks!!
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: packing things back to regular expression

2008-02-24 Thread Amit Gupta

> "CL(?P[a-z]+)XY(?:AB)[aeiou]+(?PCD(?P..)\?EF)"
>
> Good luck.
>
> --
> Steven

This is what I did in the end (in principle). Thanks.

A
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: unitests don't run under pdb

2008-03-14 Thread Amit Gupta
On Feb 20, 8:51 pm, Miki <[EMAIL PROTECTED]> wrote:
> Hello Amit,
>
>
>
> > python testname.py : the unitests runs as usual and I get the
> > following results:
> > --
> > Ran 2 tests in 0.024s
>
> > OK
> > 
>
> > However, if I do "python -mpdbtestnames.py": I get
> > ython -mpdbtestnames.py> /s/nd6/amit/pyiglu/testnames.py(1)()
>
> > -> importunittest
> > (Pdb) c
>
> > --
> > Ran 0 tests in 0.000s
>
> > OK
> > ---
>
> IIRCunittestchecks the __main__ module for tests to run. Once you
> run python with "-mpdb" the __main__ module ispdband not your
> script.
>
> HTH,
> --
> Miki <[EMAIL PROTECTED]>http://pythonwise.blogspot.com

Ok, Sorry for late reply on this.

So What do I do, if my testcase if failing because of an uncaught
exception and I want to run it in pdb.

Thanks
-- 
http://mail.python.org/mailman/listinfo/python-list


ctype question

2009-06-04 Thread Amit Gupta
Hi,

I have been using ctype.cdll to load a library, but I am unable to
figure out how to load multiple libraries that depends on each other.
E.g. I have two libraries A.so and B.so. A.so has some undefined
references, and those symbols are defined in B.so.

When I try to load ctypes.cdll.LoadLibrary("A.so"), it gives errors
about the undefined Symbols. Even if I load B.so before loading A.so,
the error remains the same (which is expected). Can someone help me to
find out, how to load A.so by telling it to look for undefined symbols
in B.so as well?

Thanks, Amit
-- 
http://mail.python.org/mailman/listinfo/python-list


would this be called bug?

2008-05-23 Thread Amit Gupta
The code is attached below. Basically I am taking a substring of
variable m, and using "buffer" instead of slicing.

However, the application of int on that buffer start correctly from
the offset, but it scans all the way to the end, instead of stopping
at the length of the buffer.

It works however, if I apply str function to output of buffer
function.

-A


(Pdb) m = "AA1234AA"
(Pdb) x = buffer(m, 2, 4)
(Pdb) x

(Pdb) int(x)
*** ValueError: invalid literal for int() with base 10: '1234AA'
(Pdb) m = "AA1234AA"
(Pdb) m = "AA1234"
(Pdb) x = buffer(m, 2, 4)
(Pdb) int(x)
1234
(Pdb)
--
http://mail.python.org/mailman/listinfo/python-list


python scripts to standalone executable

2008-03-31 Thread Amit Gupta
Hi

I am looking for a some tool that can convert python scripts to
executable on Linux.

I found freeeze.py as the only option so far. Couple of queries on
freeze:

1. Have anyone used the freeze utility and any experiences to share
from that?
2. Is there any enterprise-level exe-builder for python on linux
(ActiveState has nothing)?

Any other related commets are also welcome.

Thanks
Amit
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Newbie Question - Overloading ==

2008-03-31 Thread Amit Gupta
On Mar 31, 10:23 am, xkenneth <[EMAIL PROTECTED]> wrote:
>
> class A:
> def __eq__(self,other):
>  return self.a == other.a and self.b == other.b
>
> class B:
> def __eq__(self,other):
> return self.a == other.a and self.c == other.c

>
> Thanks!
>
> Regards,
> Kenneth Miller

Can't say aboyt unpythonic: I am no expert at that: but to avoid
catching Attribute-Error everywhere, you can redefine __eq__ as
def __eq__(self, other) :
  try :
 return <>
  except AttributeError:
 return False
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: python scripts to standalone executable

2008-03-31 Thread Amit Gupta
On Mar 31, 10:37 am, John Henry <[EMAIL PROTECTED]> wrote:
> On Mar 31, 10:24 am, Amit Gupta <[EMAIL PROTECTED]> wrote:
>
>
>
> > Hi
>
> > I am looking for a some tool that can convert python scripts to
> > executable on Linux.
>
> > I found freeeze.py as the only option so far. Couple of queries on
> > freeze:
>
> > 1. Have anyone used the freeze utility and any experiences to share
> > from that?
> > 2. Is there any enterprise-level exe-builder for python on linux
> > (ActiveState has nothing)?
>
> > Any other related commets are also welcome.
>
> > Thanks
> > Amit
>
> I don't know about freeeze.py but for me, I've been using py2exe, and
> also pyinstall quite often and they both work for me.

Isnt py2exe for windows only?
I haven't looked at pyinstall.. Is it for linux?
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: python scripts to standalone executable

2008-03-31 Thread Amit Gupta
On Mar 31, 11:45 am, John Henry <[EMAIL PROTECTED]> wrote:

> Not sure.  I use it on windows.
>
> > I haven't looked at pyinstall.. Is it for linux?
>
> It appears so - according tohttp://www.pyinstaller.org/

Thanks! It does show support for Linux. The documentation says it
works for python until version 2.4. I am using 2.5.1. Not sure if it
will work seamlessly, but I will try.

If anyone has experience to share on using pyinstaller on 2.5.1 or
higher, please share.

Amit
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Newbie Question - Overloading ==

2008-03-31 Thread Amit Gupta
On Mar 31, 11:00 am, xkenneth <[EMAIL PROTECTED]> wrote:
> Yeah, this is what I'm talking about:
>
> > def __eq__(self, other) :
> >   try :
> >  return <>
> >   except AttributeError:
> >  return False
>
> That seems a bit nasty to me.

One thing about python (IMO); you can't just say this doesn't look
good. You need to say: why do you think this is not good.
To me, it appears a concise and local solution to your problem.
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: python scripts to standalone executable

2008-03-31 Thread Amit Gupta
On Mar 31, 1:52 pm, Mike Driscoll <[EMAIL PROTECTED]> wrote:
>
> What about creating a setup.py and using the distutils command to
> build rpms or tarballs?
>
> http://docs.python.org/dist/built-dist.html
>
> Mike

My quick look: The link you sent is under the header "Distributing
Python Modules". In my case, I have set of python-files that
altogether is part of one product-functionality. I would like to
package it and have it run standalone, even if the user does not have
python installed.

Ok, I guess build-dist can possibly achieve the same purpose (without
reading through the link you sent).  So my question would be: why is
there pyinstaller, if this does the job. Is build-dist more low-level
and thus is over-kill for the kind of application I am looking for?:

Thanks
-- 
http://mail.python.org/mailman/listinfo/python-list