top button
Flag Notify
    Connect to us
      Facebook Login
      Site Registration Why to Join

Facebook Login
Site Registration
Print Preview

Documenting builtin methods using python

0 votes

I have this innocent and simple code:

from collections import deque
exhaust_iter = deque(maxlen=0).extend
exhaust_iter.__doc__ = "Exhaust an iterator efficiently without caching any of its yielded values."

Obviously it does not work. Is there a way to get it to work simply and without creating a new scope (which would be a rather inefficient a way to set documentation, and would hamper introspection)?

How about dropping the "simply" requirement?

posted Jul 11, 2013 by anonymous

Share this question
Facebook Share Button Twitter Share Button Google+ Share Button LinkedIn Share Button Multiple Social Share Button

2 Answers

+1 vote

I think the canonical way to specialize a class (even if it's only docstrings or method re-names) is to extend it with a new class.

answer Jul 11, 2013 by anonymous
+1 vote

I would just go with the most obvious approach:

 def exhaust_iter(iter):
 Exhaust an iterator efficiently without caching
 any of its yielded values

It's not going to be that inefficient unless you're calling it in a long inner loop.

answer Jul 11, 2013 by anonymous
Similar Questions
0 votes

I've been using the settrace function to write a tracer for my program, which is working great except that it doesn't seem to work for built-in functions, like open('filename.txt'). This doesn't seem to be documented, so I'm not sure if I'm doing something wrong or that's the expected behavior.

If settrace's behavior in this regard is fixed, is there any way to trace calls to open()? I don't want to use Linux's strace, as it'll run for whole program (not just the part I want) and won't show my python line numbers/file names, etc. The other option I considered was monkey-patching the open function through a wrapper, like:

def wrapped_open(*arg,**kw):
 print 'open called'
 f =*arg,**kw)
 return f
open = wrapped_open

but that seemed very brittle to me. Could someone suggest a better way of doing this?

+2 votes

How can I flatten just a specific sublist of each list in a list of lists?

So if I had this data

[ ['46295', 'Montauk', '3', '60', '85', ['19', '5', '1', '0 $277790.00']],
 ['46295', 'Dark Eyes', '5', '59', '83', ['6', '4', '1', '0 $105625.00']],
 ['46295', 'Machinegun Jubs', '6', '53', '77', ['6', '2', '1', '1 $71685.00']],
 ['46295', 'Zara Bay', '1', '53', '77', ['12', '2', '3', '3 $112645.00']]]

How can I make it be

[ ['46295', 'Montauk', '3', '60', '85', '19', '5', '1', '0 $277790.00'],
 ['46295', 'Dark Eyes', '5', '59', '83', '6', '4', '1', '0 $105625.00'],
 ['46295', 'Machinegun Jubs', '6', '53', '77', '6', '2', '1', '1 $71685.00'],
 ['46295', 'Zara Bay', '1', '53', '77', '12', '2', '3', '3 $112645.00']]

Been looking around but most solutions just entirely flatten everything. This was popular on SO but yeah it flattens everything I want to be more selective

def flatten(lst):
 for elem in lst:
 if type(elem) in (tuple, list):
 for i in flatten(elem):
 yield i
 yield elem

What I am thinking is that if for each list the sublist should be at index 1, so


for item in list:
 item[1] - somehow flatten.

Any Idea or pointer?

0 votes

I want to install sip on windows bit using python 32 bit.
I download sip from this link and I try to install from cmd line :

C:WINDOWSsystem32>cd C:UsersusernameDesktopsip-4.17

C:UsersusernameDesktopsip-4.17>python install
and take that error any idea ?

This is SIP 4.17 for Python 2.7.11 on win32.
Error: Unsupported macro name specified. Use the --show-build-macros flag to see a list of supported macros.

+1 vote

I'm trying to multiply two matrices that has different size.

import numpy as np

a = np.random.randn(4, 3)
b = np.random.randn(4, 1)

print a
print b

How should I multiply a and b so that the multipled matrix's size is 4*3?

I want to multiply matrix 'b' to each row of matrix 'a'.
So that if matrix a is
[[1, 2, 3],
[2, 3, 4]]
and b is
a*b is
[[2, 4, 6],
[4, 6 ,8]]

Plz help me, if possible, plz use numpy

Useful Links with Similar Problem
Contact Us
+91 9880187415
#470/147, 3rd Floor, 5th Main,
HSR Layout Sector 7,
Bangalore - 560102,
Karnataka INDIA.