Broken generators

Need help, or want to share a macro? Post here!
Forum rules
Be nice to others! Respect the FreeCAD code of conduct!
Post Reply
llll
Posts: 173
Joined: Fri Nov 12, 2021 1:56 am

Broken generators

Post 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
Attachments
nested generators testk.py
(872 Bytes) Downloaded 5 times
Last edited by llll on Wed Jun 29, 2022 1:19 am, edited 2 times in total.
User avatar
Kunda1
Veteran
Posts: 13434
Joined: Thu Jan 05, 2017 9:03 pm

Re: Broken generators

Post by Kunda1 »

Please always add your full About info you are testing with
Alone you go faster. Together we go farther
Please mark thread [Solved]
Want to contribute back to FC? Checkout:
'good first issues' | Open TODOs and FIXMEs | How to Help FreeCAD | How to report Bugs
User avatar
onekk
Veteran
Posts: 6146
Joined: Sat Jan 17, 2015 7:48 am
Contact:

Re: Broken generators

Post 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.
GitHub page: https://github.com/onekk/freecad-doc.
- In deep articles on FreeCAD.
- Learning how to model with scripting.
- Various other stuffs.

Blog: https://okkmkblog.wordpress.com/
llll
Posts: 173
Joined: Fri Nov 12, 2021 1:56 am

Re: Broken generators

Post 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
Post Reply