How to get all methods/functions that contain a particular string? [closed] - python

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
How to get all methods/functions available that contain a particular string? For ex., use *csv* to get all the method/function details which have the csv text in them.
The above is for the sake of learning, each class supports a lot of methods. I would like to filter and narrow down the list of methods, so that I can explore them. Same is the case with functions also, for the sake of learning.

You can examine the globals() dictionary:
[x for x,y in globals().items() if 'csv' in x and callable(y)]

If you only wanna functions, you can do something like this:
[x for x,y in globals().iteritems() if 'csv' in x and y.__class__.__name__ == 'function']
but if you want also classes I recommend you to use Rob's way.

Related

Functions depending on other functions in Python [closed]

Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 14 days ago.
Improve this question
def mean(x):
return(sum(x)/len(x))
def variance(x):
x_mean = mean(x)
return sum((x-x_mean)**2)/(len(x)-1)
def standard_deviation(x):
return math.sqrt(variance(x))
The functions above build on each other. They depend on the previous function. What is a good way to implement this in Python? Should I use a class which has these functions? Are there other options?
Because they are widely applicable, keep them as they are
Many parts of a program may need to calculate these statistics, and it will save wordiness to not have to get them out of a class. Moreover, the functions actually don't need any class-stored data: they would simply be static methods of a class. (Which in the old days, we would have simply called "functions"!)
If they needed to store internal information to work correctly, that is a good reason to put them into a class
The advantage in that case is that it is more obvious to the programmer what information is being shared. Moreover, you might want to create two or more instances that had different sets of shared data. That is not the case here.

About web scraping in python [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 4 years ago.
Improve this question
There are two major variables (calls and puts), and several sub-variables (e.g. bid, change, time etc.) For example, if there are total 5 data points. I know how to do separately:
data[u'options'][0]["calls"][0]["change"]['fmt'], data[u'options'][0]["calls"][1]["change"]['fmt'], data[u'options'][0]["calls"][2]["change"]['fmt'], data[u'options'][0]["calls"][3]["change"]['fmt'],data[u'options'][0]["calls"][4]["change"]['fmt']
but that spend too much time. I wonder how to choose multiple items in one code.
You can do this with a little bit of list comprehension if I understand your question properly.
For each value in data["options"][0]["calls"], it adds that value's ["change"]["fmt"] value to the list.
d = [call["change"]["fmt"] for call in data["options"][0]["calls"]]
If you want a list of EACH value from every set of options, you could do it like so:
d = [[call["change"]["fmt"] for call in option["calls"]] for option in data["options"]]
and now you can say
for option in d:
for call in option:
print(call)
[data[u'options'][0]["calls"][i]["change"]['fmt'] for i in range(5)]
I don't quite understand your problem, is this what you're after?

Convert < > from string [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 4 years ago.
Improve this question
I want to get < from a string that contains the character.
So what I want to do is:
magic_function('<') = <
in order to make. For example:
1 magic_function('<') 3
I expect, of course, that the program returns True
This isn’t possible with specifically what you want. A function returns an object, not an operator. I’m not sure what your main intention is here but you might want to rethink your architecture.
What you can do (but what I don’t recommend) is using eval() like so:
eval(“1 < 3”)
But there are many resources online about why eval is “evil”: to sum them up, if you don’t know what your data source is, then you could be performing unexpected operations which you might not want.

Why does the filter function exist? [closed]

Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 5 years ago.
Improve this question
If list comprehension is better than filter, as it performs slighly better and is considered more readable (arguably, in my opinion), why does filter even exist?
I use it all the time, but if the consensus is that list comprehensions are better, what are the reasons why we have the filter function?
Way, way back in the day, way before we had list comprehensions, some guy who liked functional programming wrote up map and filter and submitted the change, and it got put in. That's about it.

How to make a this specific Pattern [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 9 years ago.
Improve this question
I am having a problem with patterns.
I have string like this:
string1 = "27.86.80.76.83.45.66.71.80.45.76.68.80.45.67.97.108.108.45.84.105.116.45.77.97.114.105.111"
The strings appear in the middle of one file, with different lengths.
For instance I am reading a file line by line and I need to know if the line has this pattern.
Can you guys point me in the right direction?
There's two different ways to go about this:
Build a parser - much work, but very flexible and possibly best performance (depending on implementation)
Use a regular expression. In your case this could be something like (\d{2,3}\.)+\d{2,3} (shortest string matched should be "111.11")

Categories