Python Forum

Full Version: looking for a sprcil iterator
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
i am looking for some kind of object type that can be an iterator, such as in a for loop, and can also have items added to it either on the front (do it would be the next item) or on the back (so it would be the last item) or both. that and i want to be able to do this from within the body of the for loop that is currently iterating it. adding an item would make it so that look cycles longer. anyone know?
A class can be made to be iterable by implementing the __iter__() and __next__() functions.
The iter function creates the placeholder within the object and returns itself.
The next function gets the current data, advances the placeholder to the next data, and returns the current data.
With these two functions implemented in a class then you can use a for loop to iterate over all the items in the class.
What are you trying to store in your class?
Do you have any code so far?
It's often the case that using the existing list or dictionaries is faster than making your own class.
the code i have so far uses a while loop on a list made from a string, that a uses .pop() on it to get items out and [:0] to push an item back in. it works this way but i'd rather have a for loop on a genuine iterator of some kind. making one means a lot of code to add to this project.

    cntl = [x for x in cstr]
    while cntl:
        ch = cntl.pop(0)
        if ch == endch:
           cntl[:0] = [ch]
           break
       ...
    ...
    while cntl:
        ch = cntl.pop(0)
        ...
in the above example, the 2nd loop sees the character that ended the first loop as its first character. the real code is much larger than this but this is basically what it does
(Jun-13-2019, 12:16 AM)Skaperen Wrote: [ -> ]the code i have so far uses a while loop on a list made from a string, that a uses .pop() on it to get items out and [:0] to push an item back in. it works this way but i'd rather have a for loop on a genuine iterator of some kind. making one means a lot of code to add to this project.

    cntl = [x for x in cstr]
    while cntl:
        ch = cntl.pop(0)
        if ch == endch:
           cntl[:0] = [ch]
           break
       ...
    ...
    while cntl:
        ch = cntl.pop(0)
        ...
in the above example, the 2nd loop sees the character that ended the first loop as its first character. the real code is much larger than this but this is basically what it does

What are you trying to do?
Are you trying to replace characters in a string?
Are you trying to remove characters in a string?
it is interpreting the string as a (compact) specialized language
(Jun-13-2019, 12:47 AM)Skaperen Wrote: [ -> ]it is interpreting the string as a (compact) specialized language

Ok, but what actions are you trying to take to do so?
Your code sample appears to examine every character in a string and only leave specific values in the string.
If you can provide more details you will probably get a better answer to your question.
i am not going to post the whole code. i get people responding about all kinds of other things when i do that.

but i can tell you a little more about the program. it is sorta between the Unix cut command and the (g)awk command. it is a tool to specify how to reformat columns of an input file to produce an output file with more sophistication than the cut command but not as much as the (g)awk command. it uses a mini scripting language that is given to it on the command line. i actually have 2 different ideas how to do this language and i plan to try both to compare ease of use and performance.
(Jun-13-2019, 01:36 AM)Skaperen Wrote: [ -> ]i am not going to post the whole code. i get people responding about all kinds of other things when i do that.
I get that, but you should consider posting an algorithm at least if you want specific help.
For a general answer, my first reply tells how to make an iterable class, but I do not believe that you need one and that your problem is much simpler that you've made it so far.