An expression that rebinds a variable?

2007-05-17 Thread GreenH
Can I know what kind of expressions rebind variables, of course unlike
in C, assignments are not expressions (for a good reason)
So, eval(expr) should bring about a change in either my global or
local namespace, where 'expr' is the expression

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


Re: An expression that rebinds a variable?

2007-05-17 Thread GreenH
On May 17, 9:15 am, Maric Michaud <[EMAIL PROTECTED]> wrote:
> GreenH a écrit :
>
> > Can I know what kind of expressions rebind variables, of course unlike
> > in C, assignments are not expressions (for a good reason)
> > So, eval(expr) should bring about a change in either my global or
> > local namespace, where 'expr' is the expression
>
> For global scope you could use globals().__setitem__('x', 5) but it's
> not possible in local scope because the dict returned by locals() in
> function is not where the local variables are really stored.
>
> So the preferred way is to use :
>
> In [39]: exec "x=5"
>
> which the same as :
>
> In [40]: eval(compile('x=5', '', 'exec'))
>
> --
> _
>
> Maric Michaud
> _
>
> Aristote -www.aristote.info
> 3 place des tapis
> 69004 Lyon
> Tel: +33 4 26 88 00 97
> Mobile: +33 6 32 77 00 21

Thanks, But, my interest is actually in finding the cases in which
eval(expr) would throw surprises at me by bringing changes in
namespace(s), just because I haven't given a namespace for that eval()
i.e., where would we see the perils of not passing namespace to the
'eval'.

-Green

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


pythonic parsing of URL

2007-07-27 Thread GreenH
I get some string as below from a library method (qt3
QDropEvent.data()) I use.
file:///C:/Documents%20and%20Settings/Username/My%20Documents/45-61-Abc%20fold-%20den.vru

I need file path on my system, for the above example:
C:/Documents and Settings/Username/My Documents/45-61-Abc fold-
den.vru

I am doing the below, it doesn't look pythonic,
can someone suggest any elegant solution, which is not too cryptic to
understand? yep, I do care about readability of the code to average
python user :)


 tmpTuple = urlparse.urlparse(fileURLname)

tmpString = tmpTuple[2].strip('/\\')

fileName = urllib.unquote(tmpString)

#For some reason the string contained in 'fileName' has some
unprintable trailing characters, Any ideas on that?
#thus I do below

fileName = fileName.split('.vru')[0] + '.vru'


Thanks,
Greene.

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


Re: pythonic parsing of URL

2007-07-30 Thread GreenH
On Jul 27, 10:04 pm, Steven D'Aprano
<[EMAIL PROTECTED]> wrote:
> On Sat, 28 Jul 2007 03:10:32 +, GreenH wrote:
> > I get some string as below from a library method (qt3
> > QDropEvent.data()) I use.
> > file:///C:/Documents%20and%20Settings/Username/My%20Documents/45-61-Abc%20fold-%20den.vru
>
> > I need file path on my system, for the above example:
> > C:/Documents and Settings/Username/My Documents/45-61-Abc fold-
> > den.vru
>
> > I am doing the below, it doesn't look pythonic,
> > can someone suggest any elegant solution, which is not too cryptic to
> > understand? yep, I do care about readability of the code to average
> > python user :)
>
> Try this:
>
> import urlparse, sys
> fileURLname = ("file:///C:/Documents%20and%20Settings/Username/"
> "My%20Documents/45-61-Abc%20fold-%20den.vru")
> pathname = urlparse.urlparse(fileURLname)[2]
> if sys.platform == "win32":
> import nturl2path
> clean = nturl2path.url2pathname(pathname)
> else:
> import urllib
> clean = urllib.unquote(pathname)
> print clean
>
> > 
> >  tmpTuple = urlparse.urlparse(fileURLname)
>
> > tmpString = tmpTuple[2].strip('/\\')
>
> > fileName = urllib.unquote(tmpString)
>
> > #For some reason the string contained in 'fileName' has some
> > unprintable trailing characters, Any ideas on that?
>
> It works for me: No trailing characters at all, printable or otherwise.
>
> >>> len(fileName.split('.vru', 1)[1])
>
> 0
>
> --
> Steven.

Hi!
Thanks for the reply. The unprintable character was null byte.

-Greene.

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