Convert < > from 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 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.

Related

Free function that gives a lexicographically bigger string each time [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 3 years ago.
Improve this question
I am implementing a toy db, and I need a free function that gives me a lexicophgraically bigger string each time it's called. It's for naming segment files.
Let's assume I have a max of 1000 files, and I'd prefer if the string was less than 10 characters long.
Could someone give me the easiest example of such a function in python? I'd really like to be a free function as I don't want to introduce complexity with state.
A function that returns a different value each time you call it will have to keep some sort of state. However, defining a generator makes that relatively simple to manage. Specifically, itertools.count will produce an infinite stream of increasing integers; you just need to produce a suitable string from each integer.
from itertools import count
next_label = map("{:010}".format, count()).__next__
Then
>>> next_label()
'0000000000'
>>> next_label()
'0000000001'
>>> next_label()
'0000000002'
and so on, for as many times as you need to call next_label.

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?

How to get all methods/functions that contain a particular 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 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.

Is it a good naming convention to name elements in lists list? [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 9 years ago.
Improve this question
I've ran into many situations such as
while(len(somelists) > 0):
somelist = somelists.pop() # prob not the best example
...
And this often confuses me while I was reading the code because I missed that s in the end.
Since I have seen this quite often in many languages, I just wonder is this actually a good naming convention or not?
The s is okay, but the list is not. Try naming your variables for what they represent, not what their type is. So:
while(len(cars) > 0):
car = cars.pop()
Of course, some people manage to avoid the question of s altogether. They apply the same advice to the container of cars, so we have:
while(len(dealership) > 0):
car = dealership.pop()

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