Page 1 of 1

Broken generators

Posted: Tue Jun 28, 2022 3:28 pm
by llll
Trying to make code more readable by managing variables better. Can't get a generator to work nested in a string.


Normally I would do this but it's hard to read in some tools

Code: Select all

def ll():
    print(f"DOC.getObject('Assembly{next(TT)}','Parts{next(TT)}.Common{next(TT)}.')")
print('\n#Normally & functional ')
for i in range(6):
    ll()
Call in string, print string - broken

Code: Select all

triplet2 = f"DOC.getObject('Assembly{next(TT)}','Parts{next(TT)}.Common{next(TT)}.')"
def jj():
    print(f"Something.{triplet2}")
print('\n#3 Call in string, print string')
for i in range(6):
    jj()
Call outside string - broken

Code: Select all

k = next(TT)
 triplet = f"DOC.getObject('Assembly{k}','Parts{k}.Common{k}.')"
def kk():
    print(f"Something.{triplet}")
print('\n#Separate gen call and string')
for i in range(6):
    kk()

#The generator

Code: Select all

def tripleCUT():
    gen = 0
    end = 200
    for i in range(end):
        for i in range(3):
            yield gen
        gen += 1
_tripleCUT  = tripleCUT()
TT  = (f"{x:0=3}" for x in _tripleCUT)

Can someone show me the way?
ty ty

Py3.10 -- executing in IDLE

Re: Broken generators

Posted: Tue Jun 28, 2022 3:30 pm
by Kunda1
Please always add your full About info you are testing with

Re: Broken generators

Posted: Tue Jun 28, 2022 4:58 pm
by onekk
Kunda1 wrote: Tue Jun 28, 2022 3:30 pm Please always add your full About info you are testing with
Version in important, as there are differences between python versions, so if you use a 3.6 version it could be different from a 3.10 version, and FC could have different python versions, depending also on the Os you use or other things, maybe if manual compiled.

Plus generators are not a FC peculiarities probably you will find more info on:

https://docs.python.org/3.9/reference/e ... =generator

simply change version number in the link and you will generally obtain infos about a specific Python version.

As usually the answer depend on the question, I think that the question is:

What do you want to achieve?

Regards

Carlo D.

Re: Broken generators

Posted: Wed Jun 29, 2022 1:23 am
by llll
Kunda1 wrote: Tue Jun 28, 2022 3:30 pm Please always add your full About info you are testing with
onekk wrote: Tue Jun 28, 2022 4:58 pm a specific Python version.
Executed in IDLE. v3.10