Free function that gives a lexicographically bigger string each time [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 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.

Related

Creating a List of a Range is slow, are there alternatives? [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 1 year ago.
Improve this question
so I have to create a List of a big range and am trying to accomplish this with list(range(number1, number2))and the numbers number1 and number2 can have a big difference, for example 235 and 4323246376. Now, this takes a long time to process. Can this process be sped up?
If you're only looking to use the list as a filter to count items in another list, you should not be using a range at all.
For example:
count = numpy.sum(theList >= 235) - numpy.sum(theList >= 4323246376)
What use case are you going to achieve with this?
In case you are going to use this list once, don't typecast the range object into a list.
When you typecast the range object into a list, It stores all the numbers in a list which costs you memory.
You can just iterate over the range object.

Can dividing code too much make it inefficient? [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 2 years ago.
Improve this question
If code is divided into too many segments, can this make the program slow?
For example - Creating a separate file for just a single function.
In my case, I'm using Python, and suppose there are two functions that I need in the main.py file. If I placed them in different files (just containing the function).
(Suppose) Also, If I'm using the same library for the two functions and I've divided the functions into separate files.
How can this affect efficiency? (Machine performance-wise and Team-wise).
It depends on the language, the framework you use etc. However, dividing the code too much can make it unreadable, which is (most of the time) the bigger problem. Since most of the time you will (or should) be working in a team, you should consider how readable your code would be for them.
However, answering this in a definite way is difficult. You should ask a Senior developer on your team for guidelines.

Array vs object - what's faster in Python [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 4 years ago.
Improve this question
I am wondering what I should do for the purpose of my project.
I am gonna operate on about 100 000 rows, every time.
what I wanted to do is to create an object "{}" and then, if I need to search for a value, just call it , for example
data['2018']['09']['Marketing']['AccountName']
the second option is to pull everyting into an array "[]" and in case I need to pull value, I will create a function to go through the array and sum numbers for specific parameters.
But don't know which method is faster.
Will be thankful if you can shed some light on this
Thanks in advance,
If performance (speed) is an issue, Python might not be the ideal choice...
Otherwise:
Might I suggest the use of a proper database, such as SQLLite (which comes shipped with Python).
And maybe SQLAlchemy as an abstraction layer. (https://docs.sqlalchemy.org/en/latest/orm/tutorial.html)
After all, they were made exactly for this kind of tasks.
If that seems overkill: Have a look at Pandas.

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?

realization of a syntactic parser under python [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 4 years ago.
Improve this question
I am working on a python project, I have to realize a syntaxic parser complex numbers, I need to make a calculator for arithmetic operations. I want to know how to get started, can you help me please ?
You might get away with very little if you can accept individual operands one at a time. As you may well know, Python accepts 1+2j as a complex number, 5.36j as an imaginary number. Thus, most parsers are overkill for parsing single complex numbers, if you ask the user to input numbers in the conventional way (ie, 10.2+i6.57). Just do a little string processing to extract the real and imaginary parts and use the complex function.
I suggest:
Tk for the GUI
pyparsing, if you really want a parser but it's probably unnecessary
Python itself for doing the arithmetic
Possible exchange with user:
>>> x = 2+8j
>>> y = 3-7j
>>> x/y
(-0.8620689655172415+0.6551724137931035j)
>>> x*y
(62+10j)

Categories