জাভাস্ক্রিপ্টে, কেউ কোনও ফাংশনের সংজ্ঞা মুদ্রণ করতে পারে। পাইথনে এটি সম্পাদনের কোনও উপায় আছে কি?
(কেবল ইন্টারেক্টিভ মোডে ঘুরে বেড়াচ্ছি, এবং আমি খোলা ছাড়াই একটি মডিউলটি পড়তে চেয়েছিলাম () আমি কৌতূহলী ছিলাম)।
জাভাস্ক্রিপ্টে, কেউ কোনও ফাংশনের সংজ্ঞা মুদ্রণ করতে পারে। পাইথনে এটি সম্পাদনের কোনও উপায় আছে কি?
(কেবল ইন্টারেক্টিভ মোডে ঘুরে বেড়াচ্ছি, এবং আমি খোলা ছাড়াই একটি মডিউলটি পড়তে চেয়েছিলাম () আমি কৌতূহলী ছিলাম)।
উত্তর:
আপনি যদি ফাংশনটি আমদানি করে থাকেন তবে আপনি ব্যবহার করতে পারেন inspect.getsource:
>>> import re
>>> import inspect
>>> print inspect.getsource(re.compile)
def compile(pattern, flags=0):
"Compile a regular expression pattern, returning a pattern object."
return _compile(pattern, flags)
এটি ইন্টারেক্টিভ প্রম্পটে কাজ করবে তবে দৃশ্যত কেবলমাত্র আমদানি করা বস্তুগুলিতে (ইন্টারেক্টিভ প্রম্পটের অভ্যন্তরে সংজ্ঞায়িত বস্তু নয়)। এবং অবশ্যই এটি তখনই কাজ করবে যদি পাইথন উত্স কোডটি খুঁজে পেতে পারে (সুতরাং অন্তর্নির্মিত অবজেক্টস, সি লিবস, .পিসিপি ফাইল ইত্যাদিতে নয়)
যদিও আমি সাধারণত সম্মত হই যে inspectএটি একটি উত্তরের উত্তর, আমি দ্বিমত করব যে আপনি দোভাষীতে সংজ্ঞায়িত অবজেক্টের উত্স কোডটি পেতে পারবেন না। আপনি যদি এটি dill.source.getsourceথেকে ব্যবহার করেন তবে আপনি dillকার্যকারিতা এবং ল্যাম্বডাসের উত্স পেতে পারেন, এমনকি যদি সেগুলি ইন্টারেক্টিভভাবে সংজ্ঞায়িত করা হয়। এটি সীমাবদ্ধ বা আনবাউন্ড শ্রেণি পদ্ধতি এবং কারিগুলিতে সংজ্ঞায়িত ফাংশনগুলির জন্য কোডও পেতে পারে ... তবে, আপনি এনকোলেজিং অবজেক্টের কোড ছাড়াই সেই কোডটি সংকলন করতে সক্ষম নন।
>>> from dill.source import getsource
>>>
>>> def add(x,y):
... return x+y
...
>>> squared = lambda x:x**2
>>>
>>> print getsource(add)
def add(x,y):
return x+y
>>> print getsource(squared)
squared = lambda x:x**2
>>>
>>> class Foo(object):
... def bar(self, x):
... return x*x+x
...
>>> f = Foo()
>>>
>>> print getsource(f.bar)
def bar(self, x):
return x*x+x
>>>
আপনি __ ডক__ কীওয়ার্ডটি ব্যবহার করতে পারেন:
#print the class description
print string.__doc__
#print function description
print open.__doc__
__doc__ আসলে ফেরৎ যাই হোক না কেন ডক স্ট্রিং কোড করা (ট্রিপল উদ্ধৃত পংক্তি) এর লেখক। বেশিও না কমও না.
আপনি ফাংশনটিতে ব্যবহার করতে পারেন __doc__, hog()উদাহরণ হিসাবে ফাংশন নিতে পারেন: আপনি এর ব্যবহার দেখতে পারেন hog():
from skimage.feature import hog
print hog.__doc__
আউটপুটটি হবে:
Extract Histogram of Oriented Gradients (HOG) for a given image.
Compute a Histogram of Oriented Gradients (HOG) by
1. (optional) global image normalisation
2. computing the gradient image in x and y
3. computing gradient histograms
4. normalising across blocks
5. flattening into a feature vector
Parameters
----------
image : (M, N) ndarray
Input image (greyscale).
orientations : int
Number of orientation bins.
pixels_per_cell : 2 tuple (int, int)
Size (in pixels) of a cell.
cells_per_block : 2 tuple (int,int)
Number of cells in each block.
visualise : bool, optional
Also return an image of the HOG.
transform_sqrt : bool, optional
Apply power law compression to normalise the image before
processing. DO NOT use this if the image contains negative
values. Also see `notes` section below.
feature_vector : bool, optional
Return the data as a feature vector by calling .ravel() on the result
just before returning.
normalise : bool, deprecated
The parameter is deprecated. Use `transform_sqrt` for power law
compression. `normalise` has been deprecated.
Returns
-------
newarr : ndarray
HOG for the image as a 1D (flattened) array.
hog_image : ndarray (if visualise=True)
A visualisation of the HOG image.
References
----------
* http://en.wikipedia.org/wiki/Histogram_of_oriented_gradients
* Dalal, N and Triggs, B, Histograms of Oriented Gradients for
Human Detection, IEEE Computer Society Conference on Computer
Vision and Pattern Recognition 2005 San Diego, CA, USA
Notes
-----
Power law compression, also known as Gamma correction, is used to reduce
the effects of shadowing and illumination variations. The compression makes
the dark regions lighter. When the kwarg `transform_sqrt` is set to
``True``, the function computes the square root of each color channel
and then applies the hog algorithm to the image.