Sorted Returns List and Reversed Returns Iterator

2008-08-21 Thread ++imanshu
Hi,

 Is there a reason why two similarly named functions Sorted and
Reversed return different types of data or is it an accident.

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


Re: Sorted Returns List and Reversed Returns Iterator

2008-08-21 Thread ++imanshu
On Aug 22, 8:40 am, John Machin <[EMAIL PROTECTED]> wrote:
> On Aug 22, 1:35 pm, John Machin <[EMAIL PROTECTED]> wrote:
>
>
>
> > On Aug 22, 12:12 pm, "++imanshu" <[EMAIL PROTECTED]> wrote:
>
> > > Hi,
>
> > >      Is there a reason why two similarly named functions Sorted and
> > > Reversed return different types of data or is it an accident.
>
> > You seem to have an interesting notion of "similarly named".
> > name0[-2:] == name1[-2:], perhaps? The two functions (eventually, in
> > the case of "reversed") return data in the order one would expect from
> > their names.
>
> > >>> x = [1, 3, 5, 2, 4, 6]
> > >>> sorted(x)
> > [1, 2, 3, 4, 5, 6]
> > >>> reversed(x)
>
> > 
>
> > >>> list(reversed(x))
> > [6, 4, 2, 5, 3, 1]-
>
> Sorry; having re-read the message subject:
>
> reversed came later; returning an iterator rather than a list provides
> more flexibility.
>
> Cheers,
> John

I agree. Iterator is more flexible. Together and both might have
returned the same types.

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


Re: Sorted Returns List and Reversed Returns Iterator

2008-08-22 Thread ++imanshu
On Aug 22, 12:36 pm, Terry Reedy <[EMAIL PROTECTED]> wrote:
> Peter Otten wrote:
> > ++imanshu wrote:
> >> I agree. Iterator is more flexible.
>
> I disagree.  Neither is more flexible.  You can iter the list returned
> by sorted and list the iter returned by reversed.  Both do the minimum
> work necessary.  See below.
>
>  > Together and both might have returned the same types.
>
> True, but only by doing potentially unnecessary work and requiring the
> caller to do potentially unnecessary work that might even prevent the
> program from working.  This is less flexible.
>
> Suppose sorted now returns alist with 50 million items.  Suppose it
> instead returned iter(alist) but the caller wants to randomly index the
> items.  Since the caller could not access the existing 50 million item
> list, the caller would have to make another 50 million item copy.  This
> is non-trivial and might not even work do to memory limitations.
>
> > It's easy to generate a reversed sequence on the fly but impractical for a
> > sorted one. Python is taking the pragmatic approach here.
>
> To expand on this: sorting and reversing are algorithmically different
> operations.   Sorting requires that one have all items in hand in a
> mutable sequence (list) for arbitrary re-ordering.  Sorted works on any
> iterable and starts by making a new list.  There is no point to not
> returning that list after it is sorted.  It would be more work and less
> useful to do more.
>
> sorted(iterable, key=None, reverse=False):
>    newlist = list(iterable)
>    newlist.sort(key, reverse)
>    return newlist
>
> Iterating over a concrete sequence in reverse order, on the other hand,
> is trivial.  It would be more work and less useful to do more.
>
> def _reversed(seq): # 'hidden' generator function
>    n = len(seq)
>    while n:
>      n -= 1
>      yield seq[n]
>
> def reversed(seq):
>    if hasattr(seq, '__reversed__'):
>      return seq.__reversed__() # I presume this is tried first
>    else:
>      return _reversed(seq) # generic fall-back
>
> Terry Jan Reedy

Thanks for giving the 'behind the scenes' reasons. It looks
reasonable now.

Thank You,
++imanshu
--
http://mail.python.org/mailman/listinfo/python-list


why in returns values for array and keys for dictionary

2008-08-25 Thread ++imanshu
Hi,

Wouldn't it be nicer to have 'in' return values (or keys) for both
arrays and dictionaries. Arrays and Dictionaries looked so similar in
Python until I learned this difference.

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


Re: why in returns values for array and keys for dictionary

2008-08-26 Thread ++imanshu
On Aug 26, 11:52 am, Erik Max Francis <[EMAIL PROTECTED]> wrote:
> ++imanshu wrote:
> >     Wouldn't it be nicer to have 'in' return values (or keys) for both
> > arrays and dictionaries. Arrays and Dictionaries looked so similar in
> > Python until I learned this difference.
>
> It's because dealing with keys makes far more sense, since that's how
> the dictionary data structure works.  If you're doing this an awful lot
> -- whether testing for inclusion or iterating -- then you're probably
> using the wrong data structure.

Thanks for the detailed replies. Python seems to be taking the
pragmatic approach here instead of the pure one. And yes it makes more
sense.

Thanks,
++imanshu

>
> --
> Erik Max Francis && [EMAIL PROTECTED] &&http://www.alcyone.com/max/
>   San Jose, CA, USA && 37 18 N 121 57 W && AIM, Y!M erikmaxfrancis
>    Well I have been puppetized / Oh how I have compromised
>     -- Lamya

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


How to Detect Use of Unassigned(Undefined) Variable(Function)

2009-11-27 Thread ++imanshu
Is there a script/module to detect the use of unassigned
(undefined) variables(functions) in python. e.g. can I detect the
problem on line 3 automatically :-

i = 1
if i == 3:
print o
print i

Thank You,
++imanshu
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: How to Detect Use of Unassigned(Undefined) Variable(Function)

2009-11-27 Thread ++imanshu
On Nov 27, 4:06 pm, Marco Mariani  wrote:
> Jon Clements wrote:
> > pychecker returns "test.py:3: No global (o) found" for the above, and
> > can be found athttp://pychecker.sourceforge.net/
>
> > There's also pylint and another one whose name I can't remember...
>
> pyflakes. I use that one

Thanks for the replies. Was looking for just these.

Thank You,
++imanshu
-- 
http://mail.python.org/mailman/listinfo/python-list


Inline Calculation of Dictionary Values

2010-04-21 Thread ++imanshu
Hi,

 Is it possible to something along these lines in python :-

map = {
'key1': f(),
'key2': modify_state(); val = f(); restore_state(); val,
'key3': f(),
}

  For 'key2' I want to store the value returned by f() but after
modifying the state. Do we have something like a "bare block". I am
trying to avoid this :-

def f2():
 modify_state()
 val = f()
 restore_state()
 return val

map = {
'key1': f(),
'key2': f2()
'key3': f(),
}

Thank You,
Himanshu
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Inline Calculation of Dictionary Values

2010-04-21 Thread ++imanshu
On Apr 21, 7:31 pm, Dave Angel  wrote:
> ++imanshu wrote:
> > Hi,
>
> >      Is it possible to something along these lines in python :-
>
> > map = {
> > 'key1': f(),
> > 'key2': modify_state(); val = f(); restore_state(); val,
> > 'key3': f(),
> > }
>
> >       For 'key2' I want to store the value returned by f() but after
> > modifying the state. Do we have something like a "bare block". I am
> > trying to avoid this :-
>
> > def f2():
> >      modify_state()
> >      val = f()
> >      restore_state()
> >      return val
>
> > map = {
> > 'key1': f(),
> > 'key2': f2()
> > 'key3': f(),
> > }
>
> > Thank You,
> > Himanshu
>
> How about something like:
>
> mymap = {
> 'key1': f(),
> 'key2': [modify_state(), f(), restore_state()][1],
> 'key3': f(),
>
> }
>
> This builds a list of three values, and uses only the [1] item from the
> list.
>
> DaveA

This is cool. I'll use it.

Thank You,
Himanshu
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Inline Calculation of Dictionary Values

2010-04-21 Thread ++imanshu
On Apr 21, 6:10 pm, Chris Rebert  wrote:
> On Wed, Apr 21, 2010 at 5:51 AM, ++imanshu  wrote:
> >     Is it possible to something along these lines in python :-
>
> > map = {
> > 'key1': f(),
> > 'key2': modify_state(); val = f(); restore_state(); val,
> > 'key3': f(),
> > }
>
> >      For 'key2' I want to store the value returned by f() but after
> > modifying the state. Do we have something like a "bare block".
>
> Based on what I can find about "bare blocks", Nope. And we like it that way 
> :-)
>
> > I am
> > trying to avoid this :-
>
> > def f2():
> >     modify_state()
> >     val = f()
> >     restore_state()
> >     return val
>
> > map = {
> > 'key1': f(),
> > 'key2': f2()
> > 'key3': f(),
> > }
>
> FWIW, I don't see what's wrong with this. You could probably refactor
> f2() to use the `with` statement and a context manager, but that's
> getting tangential.
> However, the question arises: Why do you have global state in the first place?
>
> Cheers,
> Chris
> --http://blog.rebertia.com

f() = flavor independent os api for getting path to a folder, uses
effective user id (eg Folder.FSFindFolder(Folders.kUserDomain,
Folders.kInternetPlugInFolderType, Folders.kDontCreateFolder))
modify_state() = change current effective user id
restore_state() = restore to old user id

Thanks for the reply.

Thank You,
Himanshu

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


_msi.Record object has no attribute 'GetString'

2009-07-27 Thread ++imanshu
The documentation (http://docs.python.org/library/msilib.html#record-
objects) for msilib mentions the GetString() method on Record objects.
However, the following snippet :-

db = msilib.OpenDatabase(os.path.join(root, file),
msilib.MSIDBOPEN_READONLY)
view = db.OpenView('SELECT * FROM Property')
view.Execute(None)
r = view.Fetch()
print dir(r)
print r.GetString(0)

gives me this error :-

c:\>python msi.py
['ClearData', 'GetFieldCount', 'SetInteger', 'SetStream', 'SetString',
__class__', '__delattr__', '__doc__', '__getattribute__', '__hash__',
'__init__', '__new__', __reduce__', '__reduce_ex__', '__repr__',
'__setattr__', '__str__']
Traceback (most recent call last):
  File "msi.py", line 18, in 
print r.GetString(0)
AttributeError: '_msi.Record' object has no attribute 'GetString'

Am I missing something?

Thank You,
Himanshu
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: _msi.Record object has no attribute 'GetString'

2009-07-27 Thread ++imanshu
On Jul 27, 4:38 pm, "++imanshu"  wrote:
> The documentation (http://docs.python.org/library/msilib.html#record-
> objects) for msilib mentions the GetString() method on Record objects.
> However, the following snippet :-
>
> db = msilib.OpenDatabase(os.path.join(root, file),
> msilib.MSIDBOPEN_READONLY)
> view = db.OpenView('SELECT * FROM Property')
> view.Execute(None)
> r = view.Fetch()
> print dir(r)
> print r.GetString(0)
>
> gives me this error :-
>
> c:\>python msi.py
> ['ClearData', 'GetFieldCount', 'SetInteger', 'SetStream', 'SetString',
> __class__', '__delattr__', '__doc__', '__getattribute__', '__hash__',
> '__init__', '__new__', __reduce__', '__reduce_ex__', '__repr__',
> '__setattr__', '__str__']
> Traceback (most recent call last):
>   File "msi.py", line 18, in 
>     print r.GetString(0)
> AttributeError: '_msi.Record' object has no attribute 'GetString'
>
> Am I missing something?

Known Issue. Already fixed here 
http://mail.python.org/pipermail/python-bugs-list/2008-February/047136.html

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