Broken generators

Need help, or want to share a macro? Post here!
Post Reply
llll
Posts: 168
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 1 time
Last edited by llll on Wed Jun 29, 2022 1:19 am, edited 2 times in total.
User avatar
Kunda1
Veteran
Posts: 12743
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:
#lowhangingfruit | Use the Source, Luke. | How to Help FreeCAD | How to report FC bugs and features
User avatar
onekk
Veteran
Posts: 2820
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.
My English and Italian Scripting Guide https://github.com/onekk/freecad-doc.
My Blog: https://onekk-maker.blogspot.com/

To help development see on GitHub Page

For enquiry about customized FC Scripting job PM me.
llll
Posts: 168
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