Instruction:
Implement a Python 3 function for
handling a key combination and dispatches associated events.

Response:
def handle_combo(self,combo,symbol,modifiers,release=False,mod=True):
        """
        Handles a key combination and dispatches associated events.

        First, all keybind handlers registered via :py:meth:`add` will be handled,
        then the pyglet event :peng3d:pgevent:`on_key_combo` with params ``(combo,symbol,modifiers,release,mod)`` is sent to the :py:class:`Peng()` instance.

        Also sends the events :peng3d:event:`peng3d:keybind.combo`\, :peng3d:event:`peng3d:keybind.combo.press` and :peng3d:event`peng3d:keybind.combo.release`\ .

        :params str combo: Key combination pressed
        :params int symbol: Key pressed, passed from the same argument within pyglet
        :params int modifiers: Modifiers held while the key was pressed
        :params bool release: If the combo was released
        :params bool mod: If the combo was sent without mods
        """
        if self.peng.cfg["controls.keybinds.debug"]:
            print("combo: nm=%s %s"%(mod,combo))
        if mod:
            for kbname in self.keybinds.get(combo,[]):
                self.kbname[kbname](symbol,modifiers,release)
        else:
            for kbname in self.keybinds_nm.get(combo,[]):
                self.kbname[kbname](symbol,modifiers,release)
        self.peng.sendPygletEvent("on_key_combo",(combo,symbol,modifiers,release,mod))
        self.peng.sendEvent("peng3d:keybind.combo",{"peng":self.peng,"combo":combo,"symbol":symbol,"modifiers":modifiers,"release":release,"mod":mod})
        if release:
            self.peng.sendEvent("peng3d:keybind.combo.release",{"peng":self.peng,"combo":combo,"symbol":symbol,"modifiers":modifiers,"release":release,"mod":mod})
        else:
            self.peng.sendEvent("peng3d:keybind.combo.press",{"peng":self.peng,"combo":combo,"symbol":symbol,"modifiers":modifiers,"release":release,"mod":mod})


Instruction:
Can you generate a brief explanation for the following Python 3 code
def registerEventHandlers(self):
        # Forward
        self.peng.keybinds.add(self.peng.cfg["controls.controls.forward"],"peng3d:actor.%s.player.controls.forward"%self.actor.uuid,self.on_fwd_down,False)
        # Backward
        self.peng.keybinds.add(self.peng.cfg["controls.controls.backward"],"peng3d:actor.%s.player.controls.backward"%self.actor.uuid,self.on_bwd_down,False)
        # Strafe Left
        self.peng.keybinds.add(self.peng.cfg["controls.controls.strafeleft"],"peng3d:actor.%s.player.controls.strafeleft"%self.actor.uuid,self.on_left_down,False)
        # Strafe Right
        self.peng.keybinds.add(self.peng.cfg["controls.controls.straferight"],"peng3d:actor.%s.player.controls.straferight"%self.actor.uuid,self.on_right_down,False)
        pyglet.clock.schedule_interval(self.update,1.0/60)

Response: Registers needed keybinds and schedules the update method.


Instruction:
Write a Python 3 script for
returning the movement vector according to held buttons and the rotation.

Response:
def get_motion_vector(self):
        """
        Returns the movement vector according to held buttons and the rotation.

        :return: 3-Tuple of ``(dx,dy,dz)``
        :rtype: tuple
        """
        if any(self.move):
            x, y = self.actor._rot
            strafe = math.degrees(math.atan2(*self.move))
            y_angle = math.radians(y)
            x_angle = math.radians(x + strafe)
            dy = 0.0
            dx = math.cos(x_angle)
            dz = math.sin(x_angle)
        else:
            dy = 0.0
            dx = 0.0
            dz = 0.0
        return (dx, dy, dz)


Instruction:
Here you have a function in Python 3, explain what it does
def registerEventHandlers(self):
        self.world.registerEventHandler("on_mouse_motion",self.on_mouse_motion)
        self.world.registerEventHandler("on_mouse_drag",self.on_mouse_drag)

Response: Registers the motion and drag handlers.


Instruction:
Can you generate a brief explanation for the following Python 3 code
def registerEventHandlers(self):
        # Crouch/fly down
        self.peng.keybinds.add(self.peng.cfg["controls.controls.crouch"],"peng3d:actor.%s.player.controls.crouch"%self.actor.uuid,self.on_crouch_down,False)
        # Jump/fly up
        self.peng.keybinds.add(self.peng.cfg["controls.controls.jump"],"peng3d:actor.%s.player.controls.jump"%self.actor.uuid,self.on_jump_down,False)
        pyglet.clock.schedule_interval(self.update,1.0/60)

Response: Registers the up and down handlers.


Instruction:
How would you code a function in Python 3 to
add all widgets to the internal cache.

Response:
def add_widgets(self,**kwargs):
        """
        Called by the initializer to add all widgets.

        Widgets are discovered by searching through the :py:attr:`WIDGETS` class attribute.
        If a key in :py:attr:`WIDGETS` is also found in the keyword arguments and
        not none, the function with the name given in the value of the key will
        be called with its only argument being the value of the keyword argument.

        For more complex usage scenarios, it is also possible to override this method
        in a subclass, but the original method should always be called to ensure
        compatibility with classes relying on this feature.
        """
        for name,fname in self.WIDGETS.items():
            if name in kwargs and kwargs[name] is not None:
                assert hasattr(self,fname)
                assert callable(getattr(self,fname))
                getattr(self,fname)(kwargs[name])


Instruction:
Create a Python 3 function for
adding the main label of the dialog.

Response:
def add_label_main(self,label_main):
        """
        Adds the main label of the dialog.

        This widget can be triggered by setting the label ``label_main`` to a string.

        This widget will be centered on the screen.
        """
        # Main Label
        self.wlabel_main = text.Label("label_main",self,self.window,self.peng,
                        pos=lambda sw,sh, bw,bh: (sw/2-bw/2,sh/2-bh/2),
                        size=[0,0],
                        label=label_main,
                        #multiline=True, # TODO: implement multine dialog
                        )
        self.wlabel_main.size = lambda sw,sh: (sw,self.wlabel_main._label.font_size)
        self.addWidget(self.wlabel_main)


Instruction:
Here you have a function in Python 3, explain what it does
def add_btn_ok(self,label_ok):
        # OK Button
        self.wbtn_ok = button.Button("btn_ok",self,self.window,self.peng,
                        pos=lambda sw,sh, bw,bh: (sw/2-bw/2,sh/2-bh/2-bh*2),
                        size=[0,0],
                        label=label_ok,
                        borderstyle=self.borderstyle
                        )
        self.wbtn_ok.size = lambda sw,sh: (self.wbtn_ok._label.font_size*8,self.wbtn_ok._label.font_size*2)
        self.addWidget(self.wbtn_ok)

        def f():
            self.doAction("click_ok")
            self.exitDialog()
        self.wbtn_ok.addAction("click",f)

Response: Adds an ok button to allow the user to exit the dialog.


Instruction:
Make a summary of the following Python 3 code
def exitDialog(self):
        if self.prev_submenu is not None:
            # change back to the previous submenu
            # could in theory form a stack if one dialog opens another
            self.menu.changeSubMenu(self.prev_submenu)
        self.prev_submenu = None

Response: Helper method that exits the dialog.


Instruction:
Can you tell what is the following Python 3 function doing
def add_btn_confirm(self,label_confirm):
        # Confirm Button
        self.wbtn_confirm = button.Button("btn_confirm",self,self.window,self.peng,
                        pos=lambda sw,sh, bw,bh: (sw/2-bw-4,sh/2-bh/2-bh*2),
                        size=[0,0],
                        label=label_confirm,
                        borderstyle=self.borderstyle
                        )
        self.wbtn_confirm.size = lambda sw,sh: (self.wbtn_confirm._label.font_size*8,self.wbtn_confirm._label.font_size*2)
        self.addWidget(self.wbtn_confirm)

        def f():
            self.doAction("confirm")
            self.exitDialog()
        self.wbtn_confirm.addAction("click",f)

Response: Adds a confirm button to let the user confirm whatever action they were presented with.


Instruction:
Explain what the following Python 3 code does
def add_btn_cancel(self,label_cancel):
        # Cancel Button
        self.wbtn_cancel = button.Button("btn_cancel",self,self.window,self.peng,
                        pos=lambda sw,sh, bw,bh: (sw/2+4,sh/2-bh/2-bh*2),
                        size=[0,0],
                        label=label_cancel,
                        borderstyle=self.borderstyle
                        )
        self.wbtn_cancel.size = lambda sw,sh: (self.wbtn_cancel._label.font_size*8,self.wbtn_cancel._label.font_size*2)
        self.addWidget(self.wbtn_cancel)

        def f():
            self.doAction("cancel")
            self.exitDialog()
        self.wbtn_cancel.addAction("click",f)

Response: Adds a cancel button to the dialog.


Instruction:
Can you create a Python 3 function that
updates the progressbar by re - calculating the label.

Response:
def update_progressbar(self):
        """
        Updates the progressbar by re-calculating the label.

        It is not required to manually call this method since setting any of the
        properties of this class will automatically trigger a re-calculation.
        """
        n,nmin,nmax = self.wprogressbar.n,self.wprogressbar.nmin,self.wprogressbar.nmax
        if (nmax-nmin)==0:
            percent = 0 # prevents ZeroDivisionError
        else:
            percent = max(min((n-nmin)/(nmax-nmin),1.),0.)*100
        dat = {"value":round(n,4),"n":round(n,4),"nmin":round(nmin,4),"nmax":round(nmax,4),"percent":round(percent,4),"p":round(percent,4)}
        txt = self._label_progressbar.format(**dat)
        self.wprogresslabel.label = txt


Instruction:
Here you have a function in Python 3, explain what it does
def add_progressbar(self,label_progressbar):
        # Progressbar
        self.wprogressbar = slider.AdvancedProgressbar("progressbar",self,self.window,self.peng,
                        pos=lambda sw,sh, bw,bh: (sw/2-bw/2,self.wlabel_main.pos[1]-bh*1.5),
                        size=[0,0],
                        #label=label_progressbar # TODO: add label
                        borderstyle=self.borderstyle
                        )
        self.addWidget(self.wprogressbar)

        # Progress Label
        self.wprogresslabel = text.Label("progresslabel",self,self.window,self.peng,
                        pos=lambda sw,sh, bw,bh: (sw/2-bw/2,self.wprogressbar.pos[1]+8),
                        size=[0,0],
                        label="", # set by update_progressbar()
                        )
        self.wprogresslabel.size = lambda sw,sh: (sw,self.wprogresslabel._label.font_size)
        self.addWidget(self.wprogresslabel)

        self.wprogressbar.size = lambda sw,sh: (sw*0.8,self.wprogresslabel._label.font_size+10)

        self._label_progressbar = label_progressbar

        if getattr(label_progressbar,"_dynamic",False):
            def f():
                self.label_progressbar = str(label_progressbar)
            self.peng.i18n.addAction("setlang",f)

        self.wprogressbar.addAction("progresschange",self.update_progressbar)

        self.update_progressbar()

Response: Adds a progressbar and label displaying the progress within a certain task.


Instruction:
Can you generate the documentation for the following Python 3 function
def createWindow(self,cls=None,caption_t=None,*args,**kwargs):
        if cls is None:
            from . import window
            cls = window.PengWindow
        if self.window is not None:
            raise RuntimeError("Window already created!")
        self.sendEvent("peng3d:window.create.pre",{"peng":self,"cls":cls})
        if caption_t is not None:
            kwargs["caption"] = "Peng3d Application"
        self.window = cls(self,*args,**kwargs)
        self.sendEvent("peng3d:window.create.post",{"peng":self,"window":self.window})
        if self.cfg["rsrc.enable"] and self.resourceMgr is None:
            self.sendEvent("peng3d:rsrc.init.pre",{"peng":self,"basepath":self.cfg["rsrc.basepath"]})
            self.resourceMgr = resource.ResourceManager(self,self.cfg["rsrc.basepath"])
            self.rsrcMgr = self.resourceMgr
            self.sendEvent("peng3d:rsrc.init.post",{"peng":self,"rsrcMgr":self.resourceMgr})
            if self.cfg["i18n.enable"] and self.i18n is None:
                self.sendEvent("peng3d:i18n.init.pre",{"peng":self})
                self.i18n = i18n.TranslationManager(self)
                self._t = self.i18n.t
                self._tl = self.i18n.tl
                self.sendEvent("peng3d:i18n.init.post",{"peng":self,"i18n":self.i18n})
        if caption_t is not None:
            self.window.set_caption(self.t(caption_t))
            def f():
                self.window.set_caption(self.t(caption_t))
            self.i18n.addAction("setlang",f)
        return self.window

Response: Creates a new window using the supplied class.


Instruction:
Make a summary of the following Python 3 code
def run(self,evloop=None):
        self.sendEvent("peng3d:peng.run",{"peng":self,"window":self.window,"evloop":evloop})
        self.window.run(evloop)
        self.sendEvent("peng3d:peng.exit",{"peng":self})

Response: Runs the application main loop.


Instruction:
Explain what the following Python 3 code does
def sendPygletEvent(self,event_type,args,window=None):
        args = list(args)

        self.sendEvent("pyglet:%s"%event_type,{"peng":self,"args":args,"window":window,"src":self,"event_type":event_type})
        self.sendEvent("peng3d:pyglet",{"peng":self,"args":args,"window":window,"src":self,"event_type":event_type})

        if event_type not in ["on_draw","on_mouse_motion"] and self.cfg["debug.events.dump"]:
            print("Event %s with args %s"%(event_type,args))
        if event_type in self.pygletEventHandlers:
            for whandler in self.pygletEventHandlers[event_type]:
                # This allows for proper collection of deleted handler methods by using weak references
                handler = whandler()
                if handler is None:
                    del self.pygletEventHandlers[event_type][self.pygletEventHandlers[event_type].index(whandler)]
                handler(*args)

Response: Sends a pyglet event to the current window.


Instruction:
Can you write a function in Python 3 where it
registers an event handler for the specified event type.

Response:
def addPygletListener(self,event_type,handler):
        """
        Registers an event handler.

        The specified callable handler will be called every time an event with the same ``event_type`` is encountered.

        All event arguments are passed as positional arguments.

        This method should be used to listen for pyglet events.
        For new code, it is recommended to use :py:meth:`addEventListener()` instead.

        See :py:meth:`handleEvent()` for information about tunneled pyglet events.

        For custom events, use :py:meth:`addEventListener()` instead.
        """
        if self.cfg["debug.events.register"]:
            print("Registered Event: %s Handler: %s"%(event_type,handler))
        if event_type not in self.pygletEventHandlers:
            self.pygletEventHandlers[event_type]=[]
        # Only a weak reference is kept
        if inspect.ismethod(handler):
            handler = weakref.WeakMethod(handler)
        else:
            handler = weakref.ref(handler)
        self.pygletEventHandlers[event_type].append(handler)


Instruction:
Make a summary of the following Python 3 code
def sendEvent(self,event,data=None):
        if self.cfg["debug.events.dumpfile"]!="" and event not in self.event_list:
            self.event_list.add(event)
        if event not in self.eventHandlers:
            if event not in self.events_ignored or self.events_ignored[event]<=self.cfg["events.maxignore"]: # Prevents spamming logfile with ignored event messages
                # TODO: write to logfile
                # Needs a logging module first...
                self.events_ignored[event] = self.events_ignored.get(event,0)+1
            return

        for handler in self.eventHandlers[event]:
            f = handler[0]
            try:
                f(event,data)
            except Exception:
                if not handler[1]:
                    raise
                else:
                    # TODO: write to logfile
                    if self.cfg["events.removeonerror"]:
                        self.delEventListener(event,f)

Response: Sends an event to the log file with optional data.


Instruction:
Write a Python 3 script for
adding a handler to the given event.

Response:
def addEventListener(self,event,func,raiseErrors=False):
        """
        Adds a handler to the given event.

        A event may have an arbitrary amount of handlers, though assigning too
        many handlers may slow down event processing.

        For the format of ``event``\ , see :py:meth:`sendEvent()`\ .

        ``func`` is the handler which will be executed with two arguments, ``event_type`` and ``data``\ , as supplied to :py:meth:`sendEvent()`\ .

        If ``raiseErrors`` is True, exceptions caused by the handler will be re-raised.
        Defaults to ``False``\ .
        """
        if not isinstance(event,str):
            raise TypeError("Event types must always be strings")

        if event not in self.eventHandlers:
            self.eventHandlers[event]=[]
        self.eventHandlers[event].append([func,raiseErrors])


Instruction:
Given the following Python 3 function, write the documentation
def delEventListener(self,event,func):
        if event not in self.eventHandlers:
            raise NameError("No handlers exist for event %s"%event)
        if [func,True] in self.eventHandlers[event]:
            del self.eventHandlers[event][self.eventHandlers[event].index(func)]
        elif [func,False] in self.eventHandler[event]:
            del self.eventHandlers[event][self.eventHandlers[event].index(func)]
        else:
            raise NameError("This handler is not registered for event %s"%event)
        if self.eventHandlers[event] == []:
            del self.eventHandlers[event]

Response: Removes the given handler from the given event.


Instruction:
Write a Python 3 script to
set the default language for all domains.

Response:
def setLang(self,lang):
        """
        Sets the default language for all domains.

        For recommendations regarding the format of the language code, see
        :py:class:`TranslationManager`\ .

        Note that the ``lang`` parameter of both :py:meth:`translate()` and
        :py:meth:`translate_lazy()` will override this setting.

        Also note that the code won't be checked for existence or plausibility.
        This may cause the fallback strings to be displayed instead if the language
        does not exist.

        Calling this method will cause the ``setlang`` action and the
        :peng3d:event`peng3d:i18n.set_lang` event to be triggered. Note that both
        action and event will be triggered even if the language did not actually change.

        This method also automatically updates the :confval:`i18n.lang` config value.
        """
        self.lang = lang
        self.peng.cfg["i18n.lang"] = lang

        if lang not in self.cache:
            self.cache[lang]={}

        self.doAction("setlang")
        self.peng.sendEvent("peng3d:i18n.set_lang",{"lang":self.lang,"i18n":self})


Instruction:
How would you code a function in Python 3 to
generate a list of languages based on files found on disk.

Response:
def discoverLangs(self,domain="*"):
        """
        Generates a list of languages based on files found on disk.

        The optional ``domain`` argument may specify a domain to use when checking
        for files. By default, all domains are checked.

        This internally uses the :py:mod:`glob` built-in module and the
        :confval:`i18n.lang.format` config option to find suitable filenames.
        It then applies the regex in :confval:`i18n.discover_regex` to extract the
        language code.
        """
        rsrc = self.peng.cfg["i18n.lang.format"].format(domain=domain,lang="*")
        pattern = self.peng.rsrcMgr.resourceNameToPath(rsrc,self.peng.cfg["i18n.lang.ext"])
        files = glob.iglob(pattern)

        langs = set()
        r = re.compile(self.peng.cfg["i18n.discover_regex"])

        for f in files:
            m = r.fullmatch(f)
            if m is not None:
                langs.add(m.group("lang"))

        return list(langs)


Instruction:
Write a Python 3 function for
adding a camera to the internal registry.

Response:
def addCamera(self,camera):
        """
        Add the camera to the internal registry.

        Each camera name must be unique, or else only the most recent version will be used. This behavior should not be relied on because some objects may cache objects.

        Additionally, only instances of :py:class:`Camera() <peng3d.camera.Camera>` may be used, everything else raises a :py:exc:`TypeError`\ .
        """
        if not isinstance(camera,Camera):
            raise TypeError("camera is not of type Camera!")
        self.cameras[camera.name]=camera


Instruction:
Given the following Python 3 function, write the documentation
def addView(self,view):
        if not isinstance(view,WorldView):
            raise TypeError("view is not of type WorldView!")
        self.views[view.name]=view

Response: Adds the supplied WorldView object to the internal registry.


Instruction:
Explain what the following Python 3 code does
def getView(self,name):
        if name not in self.views:
            raise ValueError("Unknown world view")
        return self.views[name]

Response: Returns the view with name Raises a : py : exc : ValueError if the view does not exist.


Instruction:
Can you implement a function in Python 3 that
renders the world in 3d - mode.

Response:
def render3d(self,view=None):
        """
        Renders the world in 3d-mode.

        If you want to render custom terrain, you may override this method. Be careful that you still call the original method or else actors may not be rendered.
        """
        for actor in self.actors.values():
            actor.render(view)


Instruction:
How would you implement a function in Python 3 that
sets the active camera.

Response:
def setActiveCamera(self,name):
        """
        Sets the active camera.

        This method also calls the :py:meth:`Camera.on_activate() <peng3d.camera.Camera.on_activate>` event handler if the camera is not already active.
        """
        if name == self.activeCamera:
            return # Cam is already active
        if name not in self.world.cameras:
            raise ValueError("Unknown camera name")
        old = self.activeCamera
        self.activeCamera = name
        self.cam.on_activate(old)


Instruction:
Explain what the following Python 3 code does
def on_menu_enter(self,old):
        super(WorldViewMouseRotatable,self).on_menu_enter(old)
        self.world.peng.window.toggle_exclusivity(True)

Response: Override this method to force mouse exclusivity.


Instruction:
How would you implement a function in Python 3 that
overrides to force - disables mouse exclusivity.

Response:
def on_menu_exit(self,new):
        """
        Fake event handler, same as :py:meth:`WorldView.on_menu_exit()` but force-disables mouse exclusivity.
        """
        super(WorldViewMouseRotatable,self).on_menu_exit(new)
        self.world.peng.window.toggle_exclusivity(False)


Instruction:
Can you tell what is the following Python 3 function doing
def on_key_press(self,symbol,modifiers):
        if symbol == key.ESCAPE:
            self.world.peng.window.toggle_exclusivity()
            return pyglet.event.EVENT_HANDLED

Response: This method is called when a key is pressed.


Instruction:
How would you explain what the following Python 3 function does
def on_mouse_motion(self, x, y, dx, dy):
        if not self.world.peng.window.exclusive:
            return
        m = self.world.peng.cfg["controls.mouse.sensitivity"]
        x, y = self.rot
        x, y = x + dx * m, y + dy * m
        y = max(-90, min(90, y))
        x %= 360
        newrot = (x,y)
        self.rot= newrot

Response: Handles mouse motion and rotates the attached camera accordingly.


Instruction:
Write a Python 3 script to
start a new step. returns a context manager which allows you to to report an error

Response:
def step(self, step_name):
        """Start a new step. returns a context manager which allows you to
        report an error"""

        @contextmanager
        def step_context(step_name):
            if self.event_receiver.current_case is not None:
                raise Exception('cannot open a step within a step')

            self.event_receiver.begin_case(step_name, self.now_seconds(), self.name)
            try:
                yield self.event_receiver
            except:
                etype, evalue, tb = sys.exc_info()
                self.event_receiver.error('%r' % [etype, evalue, tb])
                raise
            finally:
                self.event_receiver.end_case(step_name, self.now_seconds())

        return step_context(step_name)


Instruction:
How would you code a function in Python 3 to
convert the given resource name to a file path.

Response:
def resourceNameToPath(self,name,ext=""):
        """
        Converts the given resource name to a file path.

        A resource path is of the format ``<app>:<cat1>.<cat2>.<name>`` where cat1 and cat2 can be repeated as often as desired.

        ``ext`` is the file extension to use, e.g. ``.png`` or similar.

        As an example, the resource name ``peng3d:some.category.foo`` with the extension ``.png`` results in the path ``<basepath>/assets/peng3d/some/category/foo.png``\ .

        This resource naming scheme is used by most other methods of this class.

        Note that it is currently not possible to define multiple base paths to search through.
        """
        nsplit = name.split(":")[1].split(".")
        return os.path.join(self.basepath,"assets",name.split(":")[0],*nsplit)+ext


Instruction:
Here you have a function in Python 3, explain what it does
def resourceExists(self,name,ext=""):
        return os.path.exists(self.resourceNameToPath(name,ext))

Response: Returns whether or not the resource with the given name and extension exists.


Instruction:
Implement a Python 3 function for
adding a new texture category with the given name.

Response:
def addCategory(self,name):
        """
        Adds a new texture category with the given name.

        If the category already exists, it will be overridden.
        """
        self.categories[name]={}
        self.categoriesTexCache[name]={}
        self.categoriesTexBin[name]=pyglet.image.atlas.TextureBin(self.texsize,self.texsize)
        self.peng.sendEvent("peng3d:rsrc.category.add",{"peng":self.peng,"category":name})


Instruction:
Can you generate a brief explanation for the following Python 3 code
def getTex(self,name,category):
        if category not in self.categoriesTexCache:
            return self.getMissingTex(category)
        if name not in self.categoriesTexCache[category]:
            self.loadTex(name,category)
        return self.categoriesTexCache[category][name]

Response: Gets the texture associated with the given name and category.


Instruction:
Can you generate a brief explanation for the following Python 3 code
def loadTex(self,name,category):
        try:
            img = pyglet.image.load(self.resourceNameToPath(name,".png"))
        except FileNotFoundError:
            img = self.getMissingTexture()
        texreg = self.categoriesTexBin[category].add(img)
        #texreg = texreg.get_transform(True,True) # Mirrors the image due to how pyglets coordinate system works
        # Strange behavior, sometimes needed and sometimes not
        self.categories[category][name]=texreg
        target = texreg.target
        texid = texreg.id
        texcoords = texreg.tex_coords
        # Prevents texture bleeding with texture sizes that are powers of 2, else weird lines may appear at certain angles.
        glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT)
        glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT)
        glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST)
        glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST_MIPMAP_LINEAR)
        glGenerateMipmap(GL_TEXTURE_2D)

        out = target,texid,texcoords
        self.categoriesTexCache[category][name]=out
        self.peng.sendEvent("peng3d:rsrc.tex.load",{"peng":self.peng,"name":name,"category":category})
        return out

Response: Loads the given texture and returns the texture id and texture coordinates.


Instruction:
Can you tell what is the following Python 3 function doing
def getMissingTexture(self):
        if self.missingTexture is None:
            if self.resourceExists(self.missingtexturename,".png"):
                self.missingTexture = pyglet.image.load(self.resourceNameToPath(self.missingtexturename,".png"))
                return self.missingTexture
            else: # Falls back to create pattern in-memory
                self.missingTexture = pyglet.image.create(1,1,pyglet.image.SolidColorImagePattern([255,0,255,255]))
                return self.missingTexture
        else:
            return self.missingTexture

Response: Returns a missing texture for the given source distribution.


Instruction:
Write a Python 3 function that can
add a new texture from the given image.

Response:
def addFromTex(self,name,img,category):
        """
        Adds a new texture from the given image.

        ``img`` may be any object that supports Pyglet-style copying in form of the ``blit_to_texture()`` method.

        This can be used to add textures that come from non-file sources, e.g. Render-to-texture.
        """
        texreg = self.categoriesTexBin[category].add(img)
        #texreg = texreg.get_transform(True,True) # Mirrors the image due to how pyglets coordinate system works
        # Strange behaviour, sometimes needed and sometimes not

        self.categories[category][name]=texreg
        target = texreg.target
        texid = texreg.id
        texcoords = texreg.tex_coords

        # Prevents texture bleeding with texture sizes that are powers of 2, else weird lines may appear at certain angles.
        glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT)
        glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT)
        glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST)
        glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST_MIPMAP_LINEAR)
        glGenerateMipmap(GL_TEXTURE_2D)

        out = target,texid,texcoords
        self.categoriesTexCache[category][name]=out
        return out


Instruction:
Create a Python 3 function for
getting the model object by the given name.

Response:
def getModel(self,name):
        """
        Gets the model object by the given name.

        If it was loaded previously, a cached version will be returned.
        If it was not loaded, it will be loaded and inserted into the cache.
        """
        if name in self.modelobjcache:
            return self.modelobjcache[name]
        return self.loadModel(name)


Instruction:
Here you have a function in Python 3, explain what it does
def loadModel(self,name):
        m = model.Model(self.peng,self,name)
        self.modelobjcache[name]=m
        self.peng.sendEvent("peng3d:rsrc.model.load",{"peng":self.peng,"name":name})
        return m

Response: Loads the model with the given name.


Instruction:
Can you generate a brief explanation for the following Python 3 code
def getModelData(self,name):
        if name in self.modelcache:
            return self.modelcache[name]
        return self.loadModelData(name)

Response: Gets the model data associated with the given name.


Instruction:
Can you create a Python 3 function that
loads the model data of the given name.

Response:
def loadModelData(self,name):
        """
        Loads the model data of the given name.

        The model file must always be a .json file.
        """
        path = self.resourceNameToPath(name,".json")
        try:
            data = json.load(open(path,"r"))
        except Exception:
            # Temporary
            print("Exception during model load: ")
            import traceback;traceback.print_exc()
            return {}# will probably cause other exceptions later on, TODO

        out = {}

        if data.get("version",1)==1:
            # Currently only one version, basic future-proofing
            # This version should get incremented with breaking changes to the structure

            # Materials
            out["materials"]={}
            for name,matdata in data.get("materials",{}).items():
                m = model.Material(self,name,matdata)
                out["materials"][name]=m
            out["default_material"]=out["materials"][data.get("default_material",list(out["materials"].keys())[0])]

            # Bones
            out["bones"]={"__root__":model.RootBone(self,"__root__",{"start_rot":[0,0],"length":0})}
            for name,bonedata in data.get("bones",{}).items():
                b = model.Bone(self,name,bonedata)
                out["bones"][name]=b
            for name,bone in out["bones"].items():
                if name == "__root__":
                    continue
                bone.setParent(out["bones"][bone.bonedata["parent"]])

            # Regions
            out["regions"]={}
            for name,regdata in data.get("regions",{}).items():
                r = model.Region(self,name,regdata)
                r.material = out["materials"][regdata.get("material",out["default_material"])]
                r.bone = out["bones"][regdata.get("bone","__root__")]
                out["bones"][regdata.get("bone","__root__")].addRegion(r)
                out["regions"][name]=r

            # Animations
            out["animations"]={}
            out["animations"]["static"]=model.Animation(self,"static",{"type":"static","bones":{}})
            for name,anidata in data.get("animations",{}).items():
                a = model.Animation(self,name,anidata)
                a.setBones(out["bones"])
                out["animations"][name]=a
            out["default_animation"]=out["animations"][data.get("default_animation",out["animations"]["static"])]
        else:
            raise ValueError("Unknown version %s of model '%s'"%(data.get("version",1),name))

        self.modelcache[name]=out
        return out


Instruction:
Can you generate a brief explanation for the following Python 3 code
def setBackground(self,bg):
        self.bg = bg
        if isinstance(bg,list) or isinstance(bg,tuple):
            if len(bg)==3 and isinstance(bg,list):
                bg.append(255)
            self.bg_vlist.colors = bg*4
        elif bg in ["flat","gradient","oldshadow","material"]:
            self.bg = ContainerButtonBackground(self,borderstyle=bg,batch=self.batch2d)
            self.redraw()

Response: Sets the background of the Container.


Instruction:
Can you generate the documentation for the following Python 3 function
def addWidget(self,widget):
        if self is widget: # Prevents being able to add the container to itself, causing a recursion loop on redraw
            return
        self.widgets[widget.name]=widget

Response: Adds a widget to this container.


Instruction:
Can you tell what is the following Python 3 function doing
def draw(self):
        if not self.visible:
            # Simple visibility check, has to be tested to see if it works properly
            return

        if not isinstance(self.submenu,Container):
            glEnable(GL_SCISSOR_TEST)
            glScissor(*self.pos+self.size)

        SubMenu.draw(self)

        if not isinstance(self.submenu,Container):
            glDisable(GL_SCISSOR_TEST)

Response: Draw the submenu and its background.


Instruction:
Implement a function in Python 3 to
redraw the background and any child widgets.

Response:
def on_redraw(self):
        """
        Redraws the background and any child widgets.
        """
        x,y = self.pos
        sx,sy = self.size
        self.bg_vlist.vertices = [x,y, x+sx,y, x+sx,y+sy, x,y+sy]
        self.stencil_vlist.vertices = [x,y, x+sx,y, x+sx,y+sy, x,y+sy]
        if isinstance(self.bg,Background):
            if not self.bg.initialized:
                self.bg.init_bg()
                self.bg.initialized=True
            self.bg.redraw_bg()


Instruction:
Make a summary of the following Python 3 code
def on_redraw(self):
        n = self._scrollbar.n
        self.offset_y = -n # Causes the content to move in the opposite direction of the slider

        # Size of scrollbar
        sx=24 # Currently constant, TODO: add dynamic sx of scrollbar
        sy=self.size[1]
        # Pos of scrollbar
        x=self.size[0]-sx
        y=0 # Currently constant, TODO: add dynamic y-pos of scrollbar

        # Dynamic pos/size may be added via align/lambda/etc.

        # Note that the values are written to the _* variant of the attribute to avoid 3 uneccessary redraws
        self._scrollbar._size = sx,sy
        self._scrollbar._pos = x,y
        self._scrollbar._nmax = self.content_height

        super(ScrollableContainer,self).on_redraw()

Response: Redraws the background and contents including scrollbar.


Instruction:
Make a summary of the following Python 3 code
def mouse_aabb(mpos,size,pos):
    return pos[0]<=mpos[0]<=pos[0]+size[0] and pos[1]<=mpos[1]<=pos[1]+size[1]

Response: Check if the mouse is within the bounds of the AABB.


Instruction:
Here you have a function in Python 3, explain what it does
def addCategory(self,name,nmin=0,n=0,nmax=100):
        assert isinstance(name,basestring) # py2 compat is done at the top
        if name in self.categories:
            raise KeyError("Category with name '%s' already exists"%name)
        self.categories[name]=[nmin,n,nmax]
        self.redraw()

Response: Adds a category with the given name.


Instruction:
Can you tell what is the following Python 3 function doing
def updateCategory(self,name,nmin=None,n=None,nmax=None):
        # smart update, only stuff that was given
        if name not in self.categories:
            raise KeyError("No Category with name '%s'"%name)
        if nmin is not None:
            self.categories[name][0]=nmin
        if n is not None:
            self.categories[name][1]=n
        if nmax is not None:
            self.categories[name][2]=nmax
        self.redraw()
        self.doAction("progresschange")

Response: Update the category with the given name.


Instruction:
Can you implement a function in Python 3 that
deletes the category with the given name.

Response:
def deleteCategory(self,name):
        """
        Deletes the category with the given name.

        If the category does not exist, a :py:exc:`KeyError` will be thrown.
        """
        if name not in self.categories:
            raise KeyError("No Category with name '%s'"%name)
        del self.categories[name]
        self.redraw()


Instruction:
How would you code a function in Python 3 to
return the percentage of the slider.

Response:
def p(self):
        """
        Helper property containing the percentage this slider is "filled".

        This property is read-only.
        """
        return (self.n-self.nmin)/max((self.nmax-self.nmin),1)


Instruction:
Can you create a Python 3 function that
adds a new layer to the stack optionally at the specified z - value.

Response:
def addLayer(self,layer,z=-1):
        """
        Adds a new layer to the stack, optionally at the specified z-value.

        ``layer`` must be an instance of Layer or subclasses.

        ``z`` can be used to override the index of the layer in the stack. Defaults to ``-1`` for appending.
        """
        # Adds a new layer to the stack, optionally at the specified z-value
        # The z-value is the index this layer should be inserted in, or -1 for appending
        if not isinstance(layer,Layer):
            raise TypeError("layer must be an instance of Layer!")
        if z==-1:
            self.layers.append(layer)
        else:
            self.layers.insert(z,layer)


Instruction:
How would you code a function in Python 3 to
map a buffer region using this attribute as an accessor.

Response:
def _get_region(self, buffer, start, count):
        '''Map a buffer region using this attribute as an accessor.

        The returned region can be modified as if the buffer was a contiguous
        array of this attribute (though it may actually be interleaved or
        otherwise non-contiguous).

        The returned region consists of a contiguous array of component
        data elements.  For example, if this attribute uses 3 floats per
        vertex, and the `count` parameter is 4, the number of floats mapped
        will be ``3 * 4 = 12``.

        :Parameters:
            `buffer` : `AbstractMappable`
                The buffer to map.
            `start` : int
                Offset of the first vertex to map.
            `count` : int
                Number of vertices to map

        :rtype: `AbstractBufferRegion`
        '''
        byte_start = self.stride * start
        byte_size = self.stride * count
        array_count = self.count * count
        if self.stride == self.size or not array_count:
            # non-interleaved
            ptr_type = ctypes.POINTER(self.c_type * array_count)
            return buffer.get_region(byte_start, byte_size, ptr_type)
        else:
            # interleaved
            byte_start += self.offset
            byte_size -= self.offset
            elem_stride = self.stride // ctypes.sizeof(self.c_type)
            elem_offset = self.offset // ctypes.sizeof(self.c_type)
            ptr_type = ctypes.POINTER(
                self.c_type * int((count * elem_stride - elem_offset)))
            region = buffer.get_region(byte_start, byte_size, ptr_type)
            return vertexbuffer.IndirectArrayRegion(
                region, array_count, self.count, elem_stride)


Instruction:
How would you code a function in Python 3 to
draw vertices in the domain.

Response:
def _draw(self, mode, vertex_list=None):
        '''Draw vertices in the domain.

        If `vertex_list` is not specified, all vertices in the domain are
        drawn.  This is the most efficient way to render primitives.

        If `vertex_list` specifies a `VertexList`, only primitives in that
        list will be drawn.

        :Parameters:
            `mode` : int
                OpenGL drawing mode, e.g. ``GL_POINTS``, ``GL_LINES``, etc.
            `vertex_list` : `VertexList`
                Vertex list to draw, or ``None`` for all lists in this domain.

        '''
        glPushClientAttrib(GL_CLIENT_VERTEX_ARRAY_BIT)
        for buffer, attributes in self.buffer_attributes:
            buffer.bind()
            for attribute in attributes:
                attribute.enable()
                attribute.set_pointer(attribute.buffer.ptr)
        if vertexbuffer._workaround_vbo_finish:
            glFinish()

        if vertex_list is not None:
            glDrawArrays(mode, vertex_list.start, vertex_list.count)
        else:
            starts, sizes = self.allocator.get_allocated_regions()
            primcount = len(starts)
            if primcount == 0:
                pass
            elif primcount == 1:
                # Common case
                glDrawArrays(mode, starts[0], int(sizes[0]))
            elif gl_info.have_version(1, 4):
                starts = (GLint * primcount)(*starts)
                sizes = (GLsizei * primcount)(*sizes)
                glMultiDrawArrays(mode, starts, sizes, primcount)
            else:
                for start, size in zip(starts, sizes):
                    glDrawArrays(mode, start, size)

        for buffer, _ in self.buffer_attributes:
            buffer.unbind()
        glPopClientAttrib()


Instruction:
How would you explain what the following Python 3 function does
def patch_float2int():
    pyglet.graphics.vertexattribute.AbstractAttribute.get_region = _get_region
    pyglet.graphics.vertexbuffer.MappableVertexBufferObject.bind = _bind
    pyglet.graphics.vertexbuffer.IndirectArrayRegion.__setitem__ = _iar__setitem__
    pyglet.graphics.vertexdomain.VertexDomain.draw = _draw

Response: Patches the pyglet. graphics. vertexattribute. AbstractAttribute. get_region and pyglet. graphics. vertexbuffer. IndirectArrayRegion. set_item to _iar__setitem__.


Instruction:
Can you generate the documentation for the following Python 3 function
def register_pyglet_handler(peng,func,event,raiseErrors=False):
    peng.addEventListener("pyglet:%s"%event,(lambda data:func(*data["args"])),raiseErrors)

Response: Registers a given pyglet - style event handler for the given peng.


Instruction:
Can you write a function in Python 3 where it
adds a callback to the specified action.

Response:
def addAction(self,action,func,*args,**kwargs):
        """
        Adds a callback to the specified action.

        All other positional and keyword arguments will be stored and passed to the function upon activation.
        """
        if not hasattr(self,"actions"):
            self.actions = {}
        if action not in self.actions:
            self.actions[action] = []
        self.actions[action].append((func,args,kwargs))


Instruction:
Here you have a function in Python 3, explain what it does
def doAction(self,action):
        if not hasattr(self,"actions"):
            return
        for f,args,kwargs in self.actions.get(action,[]):
            f(*args,**kwargs)

Response: Helper method that calls all callbacks registered for the given action.


Instruction:
Explain what the following Python 3 code does
def genNewID(self):
        if self.reuse_ids:
            i = self.start_id
            while True:
                if i not in self._data["reg"]:
                    assert i<=self.max_id
                    return i # no need to change any variables
                i+=1
        else:
            with self.id_lock:
                # new id creation in lock, to avoid issues with multiple threads
                i = self._data["next_id"]
                assert i<=self.max_id
                self._data["next_id"]+=1
            return i

Response: Generates a new ID for the current internal counter.


Instruction:
Here you have a function in Python 3, explain what it does
def register(self,name,force_id=None):
        with self.registry_lock:
            if force_id is None:
                new_id = self.genNewID()
            else:
                new_id = force_id
            self._data["reg"][new_id]=name
            return new_id

Response: Registers a name to the registry.


Instruction:
Write a Python 3 function for
normalizing an ID or string representation of a resource ID.

Response:
def normalizeID(self,in_id):
        """
        Takes in an object and normalizes it to its ID/integer representation.

        Currently, only integers and strings may be passed in, else a :py:exc:`TypeError`
        will be thrown.
        """
        if isinstance(in_id,int):
            assert in_id in self._data["reg"]
            return in_id
        elif isinstance(in_id,str):
            assert in_id in self._data["reg"].inv
            return self._data["reg"].inv[in_id]
        else:
            raise TypeError("Only int and str can be converted to IDs")


Instruction:
Can you generate a brief explanation for the following Python 3 code
def normalizeName(self,in_name):
        if isinstance(in_name,str):
            assert in_name in self._data["reg"].inv
            return in_name
        elif isinstance(in_name,int):
            assert in_name in self._data["reg"]
            return self._data["reg"][in_name]
        else:
            raise TypeError("Only int and str can be converted to names")

Response: Normalizes a name to its name or string representation.


Instruction:
Write a Python 3 function for
setting the view used to the specified name.

Response:
def setView(self,name):
        """
        Sets the view used to the specified ``name``\ .

        The name must be known to the world or else a :py:exc:`ValueError` is raised.
        """
        if name not in self.world.views:
            raise ValueError("Invalid viewname for world!")
        self.viewname = viewname
        self.view = self.world.getView(self.viewname)


Instruction:
Can you write a function in Python 3 where it
sets up the attributes used by Layer3D and calls Layer3D. predraw

Response:
def predraw(self):
        """
        Sets up the attributes used by :py:class:`Layer3D()` and calls :py:meth:`Layer3D.predraw()`\ .
        """
        self.cam = self.view.cam
        super(LayerWorld,self).predraw()


Instruction:
Can you generate a brief explanation for the following Python 3 code
def addLayer(self,layer,z_index=None):
        if z_index is None:
            z_index = layer.z_index
        i = 0
        for l,z in self.layers:
            if z>z_index:
                break
            i+=1
        self._layers[layer.name]=layer
        self.layers.insert(i,[layer,z_index])

Response: Adds the given layer to the internal list of the related resources.


Instruction:
Create a Python 3 function for
redrawing the given layer.

Response:
def redraw_layer(self,name):
        """
        Redraws the given layer.

        :raises ValueError: If there is no Layer with the given name.
        """
        if name not in self._layers:
            raise ValueError("Layer %s not part of widget, cannot redraw")
        self._layers[name].on_redraw()


Instruction:
Create a Python 3 function for
drawing all layers of this LayeredWidget.

Response:
def draw(self):
        """
        Draws all layers of this LayeredWidget.

        This should normally be unneccessary, since it is recommended that layers use Vertex Lists instead of OpenGL Immediate Mode.
        """
        super(LayeredWidget,self).draw()
        for layer,_ in self.layers:
            layer._draw()


Instruction:
How would you implement a function in Python 3 that
deletes all layers within this LayeredWidget before deleting itself.

Response:
def delete(self):
        """
        Deletes all layers within this LayeredWidget before deleting itself.

        Recommended to call if you are removing the widget, but not yet exiting the interpreter.
        """
        for layer,_ in self.layers:
            layer.delete()
        self.layers = []
        self._layers = {}
        super(LayeredWidget,self).delete()


Instruction:
Here you have a function in Python 3, explain what it does
def on_redraw(self):
        super(WidgetLayer,self).on_redraw()
        if not self._initialized:
            self.initialize()
            self._initialized = True

Response: Called when the Layer should be redrawn.


Instruction:
Here you have a function in Python 3, explain what it does
def offset(self):
        if callable(self._offset):
            return util.WatchingList(self._offset(*(self.widget.pos+self.widget.size)),self._wlredraw_offset)
        else:
            return util.WatchingList(self._offset,self._wlredraw_offset)

Response: Returns the offset of the current object.


Instruction:
Explain what the following Python 3 code does
def getPos(self):
        # Returns sx,sy,ex,ey
        # sx,sy are bottom-left/lowest
        # ex,ey are top-right/highest
        sx,sy = self.widget.pos[0]+self.border[0]+self.offset[0],                       self.widget.pos[1]+self.border[1]+self.offset[1]
        ex,ey = self.widget.pos[0]+self.widget.size[0]-self.border[0]+self.offset[0],   self.widget.pos[1]+self.widget.size[1]-self.border[1]+self.offset[1]
        return sx,sy,ex,ey

Response: Returns the absolute position and size of the layer.


Instruction:
Can you tell what is the following Python 3 function doing
def getSize(self):
        return self.widget.size[0]-self.border[0]*2,self.widget.size[1]-self.border[1]*2

Response: Returns the size of the layer with the border size subtracted.


Instruction:
How would you explain what the following Python 3 function does
def addImage(self,name,rsrc):
        self.imgs[name]=self.widget.peng.resourceMgr.getTex(*rsrc)

Response: Adds an image to the internal registry.


Instruction:
Here you have a function in Python 3, explain what it does
def switchImage(self,name):
        if name not in self.imgs:
            raise ValueError("No image of name '%s'"%name)
        elif self.cur_img==name:
            return

        self.cur_img = name
        self.on_redraw()

Response: Switches the active image to the given name.


Instruction:
Write a Python 3 function for
redrawing the label by calculating its position.

Response:
def redraw_label(self):
        """
        Re-draws the text by calculating its position.

        Currently, the text will always be centered on the position of the layer.
        """
        # Convenience variables
        x,y,_,_ = self.getPos()
        sx,sy = self.getSize()

        self._label.x = x+sx/2.
        self._label.y = y+sy/2.
        self._label.width = sx
        # Height is not set, would look weird otherwise
        #self._label.height = sx
        self._label._update()


Instruction:
Given the following Python 3 function, write the documentation
def redraw_label(self):
        # Convenience variables
        x,y,_,_ = self.getPos()
        sx,sy = self.getSize()

        if self.font_name is not None:
            self._label.font_name = self.font_name
        if self.font_size is not None:
            self._label.font_size = self.font_size
        if self.font_color is not None:
            self._label.color = self.font_color

        self._label.x = x+sx/2.
        self._label.y = y+sy/2.
        self._label.width = sx
        # Height is not set, would look weird otherwise
        #self._label.height = sx
        self._label._update()

Response: Redraws the label by calculating its position.


Instruction:
How would you code a function in Python 3 to
generate the vertices used by this layer.

Response:
def genVertices(self):
        """
        Called to generate the vertices used by this layer.

        The length of the output of this method should be three times the :py:attr:`n_vertices` attribute.

        See the source code of this method for more information about the order of the vertices.
        """
        sx,sy,ex,ey = self.getPos()
        b = self.bborder

        # Vertex Naming
        # Y
        # |1  2  3  4
        # |5  6  7  8
        # |9  10 11 12
        # |13 14 15 16
        # +------> X
        # Border order
        # 4 2-tuples
        # Each marks x,y offset from the respective corner
        # tuples are in order topleft,topright,bottomleft,bottomright
        # indices:
        # 0,1:topleft; 2,3:topright; 4,5:bottomleft; 6,7:bottomright
        # For a simple border that is even, just repeat the first tuple three more times

        v1 = sx,            ey
        v2 = sx+b[0],       ey
        v3 = ex-b[2],       ey
        v4 = ex,            ey

        v5 = sx,            ey-b[1]
        v6 = sx+b[0],       ey-b[1]
        v7 = ex-b[2],       ey-b[3]
        v8 = ex,            ey-b[3]

        v9 = sx,            sy+b[5]
        v10= sx+b[4],       sy+b[5]
        v11= ex-b[6],       sy+b[7]
        v12= ex,            sy+b[7]

        v13= sx,            sy
        v14= sx+b[4],       sy
        v15= ex-b[6],       sy
        v16= ex,            sy

        # Layer is separated into 9 sections, naming:
        # 1 2 3
        # 4 5 6
        # 7 8 9
        # Within each section, vertices are given counter-clockwise, starting with the bottom-left
        # 4 3
        # 1 2
        # This is important when assigning colors

        q1 = v5 +v6 +v2 +v1
        q2 = v6 +v7 +v3 +v2
        q3 = v7 +v8 +v4 +v3
        q4 = v9 +v10+v6 +v5
        q5 = v10+v11+v7 +v6
        q6 = v11+v12+v8 +v7
        q7 = v13+v14+v10+v9
        q8 = v14+v15+v11+v10
        q9 = v15+v16+v12+v11

        return q1+q2+q3+q4+q5+q6+q7+q8+q9


Instruction:
Can you generate the documentation for the following Python 3 function
def read_h5(hdfstore, group = ""):
  m = Mesh()
  m.elements.data = hdf["elements/connectivity"]
  m.nodes.data    = hdf["nodes/xyz"]
  for key in hdf.keys():
    if key.startswith("/nodes/sets"):
      k = key.replace("/nodes/sets/", "")
      m.nodes.sets[k] = set(hdf[key])
    if key.startswith("/elements/sets"):
      k = key.replace("/elements/sets/", "")
      m.elements.sets[k] = set(hdf[key])
    if key.startswith("/elements/surfaces"):
      k = key.replace("/elements/surfaces/", "")
      m.elements.surfaces[k] = hdf[key]
    if key.startswith("/fields/"):
      if key.endswith("/metadata"):
        tag = key.split("/")[2]
        f = Field()
        f.metadata = hdf["fields/{0}/metadata".format(tag)]
        f.metadata = hdf["fields/{0}/data".format(tag)]
        f.master = m
        m.add_field(tag, f)
  hdf.close()
  return m

Response: Reads a mesh from an HDF5 file.


Instruction:
Implement a function in Python 3 to
read a GMSH MSH file and returns a Mesh instance.

Response:
def read_msh(path):
  """
  Reads a GMSH MSH file and returns a :class:`Mesh` instance.

  :arg path: path to MSH file.
  :type path: str

  """
  elementMap = { 15:"point1",
                 1:"line2",
                 2:"tri3",
                 3:"quad4",
                 4:"tetra4",
                 5:"hexa8",
                 6:"prism6",
                 7:"pyra4",

               }
  lines = np.array(open(path, "r").readlines())
  locs = {}
  nl = len(lines)
  for i in range(nl):
    line = lines[i].lower().strip()
    if line.startswith("$"):
      if line.startswith("$end"):
        locs[env].append(i)
      else:
        env = line[1:]
        locs[env] = [i]
  nodes = pd.read_csv(
          io.StringIO("\n".join(
                   lines[locs["nodes"][0]+2:locs["nodes"][1]])),
                   sep = " ",
                   names = ["labels", "x", "y", "z"])

  elements = {"labels":[], "etype":[], "conn": [], "tags":[], "sets":{}}

  for line in lines[locs["elements"][0]+2:locs["elements"][1]]:
    d = np.array([int(w) for w in line.split()])
    elements["labels"].append( d[0] )
    elements["etype"].append(elementMap[d[1]] )
    elements["tags"].append( d[3: 3+d[2]] )
    elements["conn"].append(d[3+d[2]:])
  elements["labels"] = np.array(elements["labels"])
  physicalNames = {}
  for line in lines[locs["physicalnames"][0]+2:locs["physicalnames"][1]]:
    w = line.split()
    physicalNames[int(w[1])] = w[2].replace('"', '')
  sets = {}
  tags = np.array([t[0] for t in elements["tags"]])
  for k in physicalNames.keys():
    sets[physicalNames[k]] = np.array([t == k for t in tags])
  """
  for tag, values in sets.items():
    elements["sets"][tag] = elements["labels"][values]
  """
  elements["sets"] = sets
  return Mesh(nlabels = nodes["labels"],
              coords = np.array(nodes[["x", "y", "z"]]),
              elabels = elements["labels"],
              conn = elements["conn"],
              types = elements["etype"],
              esets = elements["sets"])


Instruction:
How would you code a function in Python 3 to
read Abaqus inp file

Response:
def read_inp(path):
  """
  Reads Abaqus inp file
  """

  def lineInfo(line):
    out =  {"type": "data"}
    if line[0] == "*":
      if line[1] == "*":
        out["type"] = "comment"
        out["text"] = line[2:]
      else:
        out["type"] = "command"
        words = line[1:].split(",")
        out["value"] = words[0].strip()
        out["options"] = {}
        for word in words[1:]:
          key, value =  [s.strip() for s in word.split("=")]
          out["options"][key] = value
    return out

  def elementMapper(inpeltype):
    if inpeltype == "t3d2": return "Line2"
    if inpeltype[:3] in ["cps", "cpe", "cax"]:
      if inpeltype[3] == "3": return "tri3"
      if inpeltype[3] == "4": return "quad4"
    if inpeltype[:3] in ["c3d"]:
      if inpeltype[3] == "4": return "tetra4"
      if inpeltype[3] == "5": return "pyra5"
      if inpeltype[3] == "6": return "prism6"
      if inpeltype[3] == "8": return "hexa8"

  nlabels      = []
  coords       = []
  nsets        = {}
  elabels      = []
  etypes       = []
  connectivity = []
  esets        = {}
  surfaces     = {}

  # File preprocessing
  lines = np.array([l.strip().lower() for l in open(path).readlines()])
  lines = [line for line in  lines if len(line) != 0]
  # Data processing
  env, setlabel = None, None
  for line in lines:
    d = lineInfo(line)
    if d["type"] == "command":
      env = d["value"]
      # Nodes
      if env == "node":
        opt = d["options"]
        currentset = None
        if "nset" in opt.keys():
          currentset = opt["nset"]
          nsets[currentset] = []

      # Elements
      if env == "element":
        opt = d["options"]
        eltype = elementMapper(opt["type"])
        currentset = None
        if "elset" in opt.keys():
          currentset = opt["elset"]
          esets[currentset] = []

      # Nsets
      if env == "nset":
        opt = d["options"]
        currentset = opt["nset"]
        nsets[currentset] = []

      # Elsets
      if env == "elset":
        opt = d["options"]
        currentset = opt["elset"]
        esets[currentset] = []

      # Surfaces
      if env == "surface":
        opt = d["options"]
        currentsurface = opt["name"]
        if opt["type"] == "element":
          surfaces[currentsurface] = []

    if d["type"] == "data":
      words = line.strip().split(",")
      if env == "node":
        label = int(words[0])
        nlabels.append(label)
        coords.append(
            np.array([np.float64(w) for w in words[1:4]])
                     )
        if currentset != None: nsets[currentset].append(label)

      if env == "element":
        label  = int(words[0])
        elabels.append(label)
        connectivity.append(
            np.array( [np.int32(w) for w in words[1:] if len(w) != 0 ])
                           )
        etypes.append(eltype)
        if currentset != None: esets[currentset].append(label)

      if env == "nset":
        nsets[currentset] += [int(w) for w in words if len(w) != 0]

      if env == "elset":
        esets[currentset] += [int(w) for w in words if len(w) != 0]

      if env == "surface":
        if opt["type"] == "element":
          surfaces[currentsurface].append([w.strip() for w in words])

  surfaces2 = {}
  for tag, surface in surfaces.items():
    surfaces2[tag] = []
    for sdata in surface:
      labels = esets[sdata[0]]
      face = int(sdata[1].split("s")[1].strip())-1
      for label in labels:
        surfaces2[tag].append((label, face))

  return Mesh(nlabels = nlabels,
              coords  = coords,
              nsets   = nsets,
              elabels = elabels,
              etypes  = etypes,
              connectivity = connectivity,
              esets = esets,)


Instruction:
Create a Python 3 function for
writing the mesh to an XDMF file.

Response:
def write_xdmf(mesh, path, dataformat = "XML"):
  """
  Dumps the mesh to XDMF format.
  """
  pattern = Template(open(MODPATH + "/templates/mesh/xdmf.xdmf").read())
  attribute_pattern = Template(open(MODPATH + "/templates/mesh/xdmf_attribute.xdmf").read())
  # MAPPINGS
  cell_map = {
      "tri3":   4,
      "quad4":  5,
      "tetra4": 6,
      "pyra5":  7,
      "prism6": 8,
      "hexa8":  9}
  # REFERENCES
  nodes, elements = mesh.nodes.data, mesh.elements.data
  fields = mesh.fields
  # NUMBERS
  Ne, Nn = len(elements), len(nodes)
  # NODES
  nodes_map = np.arange(nodes.index.max()+1)
  nodes_map[nodes.index] = np.arange(len(nodes.index))
  nodes_map[0] = -1
  # ELEMENTS
  cols = ["n{0}".format(i) for i in range(elements.shape[1]-1)]
  connectivities  = mesh.elements.data[cols].as_matrix()
  connectivities[np.isnan(connectivities)] = 0
  connectivities = connectivities.astype(np.int32)
  connectivities = nodes_map[connectivities]
  labels          = np.array(elements.index)
  etypes          = np.array([cell_map[t] for t in elements.etype])
  lconn           = Ne + (connectivities != -1).sum()
  # FIELDS
  fields_string = ""
  field_data = {}
  for tag, field in fields.items():
      field_data[tag] = {}
      field.data.sort_index(inplace = True)
      fshape = field.data.shape[1]
      if   fshape  == 1: ftype = "Scalar"
      elif fshape  == 3: ftype = "Vector"
      elif fshape  == 2:
        ftype = "Vector"
        # UGLY HACK...
        field = copy.copy(field)
        field.data["v3"] = np.zeros_like(field.data.index)
        fields[tag] = field
        # BACK TO NORMAL
      elif fshape  == 6: ftype = "Tensor6"
      elif fshape  == 4:
        ftype = "Tensor6"
        # UGLY HACK...
        field = copy.copy(field)
        field.data["v13"] = np.zeros_like(field.data.index)
        field.data["v23"] = np.zeros_like(field.data.index)
        fields[tag] = field
        # BACK TO NORMAL
      if field.metadata.position == "Nodal":
        position = "Node"
      if field.metadata.position == "Element":
        position = "Cell"
      field_data[tag]["TAG"]           = tag
      field_data[tag]["ATTRIBUTETYPE"] = ftype
      field_data[tag]["FORMAT"]        = dataformat
      field_data[tag]["FIELD_DIMENSION"] = " ".join([str(l) for l in field.data.shape])
      field_data[tag]["POSITION"]      = position
  if dataformat == "XML":
    #NODES
    nodes_string = "\n".join([11*" " + "{0} {1} {2}".format(
                              n.x,
                              n.y,
                              n.z)
                        for i, n in nodes.iterrows()])
    # ELEMENTS
    elements_string = ""
    for i in range(Ne):
      elements_string += 11*" " + str(etypes[i]) + " "
      c = connectivities[i]
      c = c[np.where(c != -1)]
      elements_string += " ".join([str(i) for i in c]) + "\n"
    elements_strings = elements_string[:-1]
    # FIELDS
    for tag, field in fields.items():
      fdata = field.data.to_csv(sep = " ",
                                index = False,
                                header = False).split("\n")
      fdata = [11 * " " + l for l in fdata]
      fdata = "\n".join(fdata)
      field_data[tag]["DATA"] = fdata
      fields_string += attribute_pattern.substitute(**field_data[tag])
  elif dataformat == "HDF":
    hdf = pd.HDFStore(path + ".h5")
    hdf.put("COORDS", mesh.nodes.data[list("xyz")])
    flatconn = np.zeros(lconn, dtype = np.int32)
    pos = 0
    for i in range(Ne):
      c = connectivities[i]
      c = c[np.where(c != -1)]
      lc = len(c)
      flatconn[pos] = etypes[i]
      flatconn[pos + 1 + np.arange(lc)] = c
      pos += 1 + lc
    hdf.put("CONNECTIVITY", pd.DataFrame(flatconn))
    nodes_string = 11*" " + "{0}.h5:/COORDS/block0_values".format(path)
    elements_string = 11*" " + "{0}.h5:/CONNECTIVITY/block0_values".format(path)
    for tag, field in fields.items():
      fstrings[tag] = fstrings[tag].replace("#DATA",
         11*" " + "{0}.h5:/FIELDS/{1}/block0_values".format(path, tag))
      fields_string += fstrings[tag]
      hdf.put("FIELDS/{0}".format(tag), fields.data)
    hdf.close()
  """
  pattern = pattern.replace("#ELEMENT_NUMBER", str(Ne))
  pattern = pattern.replace("#CONN_DIMENSION", str(lconn))
  pattern = pattern.replace("#CONN_PATH", elements_string)
  pattern = pattern.replace("#NODE_NUMBER", str(Nn))
  pattern = pattern.replace("#NODE_PATH", nodes_string)
  pattern = pattern.replace("#DATAFORMAT", dataformat)
  pattern = pattern.replace("#ATTRIBUTES", fields_string)
  """
  fields_string = "\n".join([attribute_pattern.substitute(**value) for key, value in field_data.items()])
  pattern = pattern.substitute(
     ELEMENT_NUMBER = str(Ne),
     CONN_DIMENSION = str(lconn),
     CONN_PATH      = elements_string,
     NODE_NUMBER    = str(Nn),
     NODE_PATH      = nodes_string,
     DATAFORMAT     = dataformat,
     ATTRIBUTES     = fields_string)
  open(path + ".xdmf", "wb").write(pattern)


Instruction:
Given the following Python 3 function, write the documentation
def write_inp(mesh, path = None, maxwidth = 40, sections = "solid"):
  def set_to_inp(sets, keyword):
    ss = ""
    for sk in sets.keys():
      labels = sets[sk].loc[sets[sk]].index.values
      labels = list(labels)
      labels.sort()
      if len(labels)!= 0:
        ss += "*{0}, {0}={1}\n".format(keyword, sk)
        ss += argiope.utils.list_to_string(labels) + "\n"
    return ss.strip()

  # DATA
  mesh = mesh.copy()

  # NODES
  nodes_output = (mesh.nodes.coords.to_csv(header = False).split())
  nodes_output = ("\n".join(["  " + s.replace(",", ", ") for s in nodes_output]))

  # NODE SETS
  if "sets" in mesh.nodes.columns.levels[0]:
    nsets = set_to_inp(mesh.nodes.sets, "NSET")
  else:
    nsets = "**"

  # SURFACES
  surf_output = []
  if "surfaces" in mesh.elements.keys():
    sk = mesh.elements.surfaces.keys()
    for sindex in  np.unique(sk.labels[0]):
      slabel = sk.levels[0][sindex]
      surface = mesh.elements.surfaces[slabel]
      if surface.values.sum() != 0:
        mesh.surface_to_element_sets(slabel)
        surf_output.append( "*SURFACE, TYPE=ELEMENT, NAME={0}".format(slabel))
        for findex in surface.keys():
          if surface[findex].sum() != 0:
            surf_output.append("  _SURF_{0}_FACE{1}, S{1}".format(slabel,
                                                                  findex[1:]))
  else:
    surf_output.append("**")

  # ELEMENTS
  elements_output = ""
  for etype, group in mesh.elements.groupby((("type", "solver", ""),)):
    els = group.conn.replace(0, np.nan).to_csv(header = False,
                                               float_format='%.0f').split()
    elements_output += "*ELEMENT, TYPE={0}\n".format(etype)
    elements_output += ("\n".join(["  " + s.strip().strip(",").
                             replace(",", ", ") for s in els]))
    elements_output += "\n"
  elements_output = elements_output.strip()
  el_sets = {}

  # MATERIALS
  section_output = ""
  for material, group in mesh.elements.groupby("materials"):
    slabel = "_MAT_{0}".format(material)
    section_output += "*ELSET, ELSET=_MAT_{0}\n{1}\n".format(
                 material,
                 argiope.utils.list_to_string(group.index.values))

    #mesh.elements[("sets", slabel, "")] = False
    #mesh.elements.loc[group.index, ("sets", slabel, "")] = True
    if sections == "solid":
      section_output += "*SOLID SECTION, ELSET=_MAT_{0}, MATERIAL={0}\n".format(
       material)

  # ELEMENTS SETS
  if "sets" in mesh.elements.columns.levels[0]:
    esets = set_to_inp(mesh.elements.sets.swaplevel(1,0, axis = 1)[""],"ELSET")
  else:
    esets = "**"
  """
  ek = mesh.elements.sets.keys()
  for esindex in  np.unique(ek.labels[0]):
    eslabel = ek.levels[0][esindex]
    eset = mesh.elements.sets[slabel]
  """

  # PATTERN
  pattern = Template(open(MODPATH + "/templates/mesh/inp.inp").read())
  pattern = pattern.substitute(
    NODES     = nodes_output,
    NODE_SETS = nsets,
    ELEMENTS  = elements_output,
    ELEMENT_SETS = esets,
    ELEMENT_SURFACES = "\n".join(surf_output),
    SECTIONS = section_output.strip())
  pattern = pattern.strip()
  if path == None:
    return pattern
  else:
    open(path, "w").write(pattern)

Response: Writes the INP file for the given mesh.


Instruction:
Write a Python 3 script to
create a Connectivity array for a given shape.

Response:
def _make_conn(shape):
    """
    Connectivity builder using Numba for speed boost.
    """
    shape = np.array(shape)
    Ne = shape.prod()
    if len(shape) == 2:
        nx, ny = np.array(shape) +1
        conn = np.zeros((Ne, 4), dtype = np.int32)
        counter = 0
        pattern = np.array([0,1,1+nx,nx])
        for j in range(shape[1]):
            for i in range(shape[0]):
                conn[counter] = pattern + 1 + i + j*nx
                counter += 1

    if len(shape) == 3:
        nx, ny, nz  = np.array(shape) +1
        conn = np.zeros((Ne, 8), dtype = np.int32)
        counter = 0
        pattern = np.array([0,1,1+nx,nx,nx*ny,1+nx*ny,1+(nx+1)*ny,(nx+1)*ny])
        for k in range(shape[2]):
            for j in range(shape[1]):
                for i in range(shape[0]):
                    conn[counter] = pattern + 1 + i + j*nx+ k*nx*ny
                    counter += 1
    return conn


Instruction:
Write a Python 3 script for
returning a structured mesh.

Response:
def structured_mesh(shape = (2,2,2), dim = (1.,1.,1.)):
    """
    Returns a structured mesh.

    :arg shape: 2 or 3 integers (eg: shape = (10, 10, 10)).
    :type shape: tuple
    :arg dim: 2 or 3 floats (eg: dim = (4., 2., 1.))
    :type dim: tuple

    .. note::

       This function does not use GMSH for mesh generation.

    >>> import argiope as ag
    >>> mesh = ag.mesh.structured_mesh(shape =(10,10,10), dim=(1.,1.,1.)))
    """
    # PREPROCESSING
    shape = np.array(shape)
    dim   = np.array(dim)
    Ne = shape.prod()
    Nn = (shape + 1).prod()
    # LABELS
    nindex = np.arange(Nn) + 1
    eindex = np.arange(Ne) + 1
    # COORDINATES
    coords = [ np.linspace(0., dim[i], shape[i] + 1) for i in range(len(shape))]
    coords = np.array(np.meshgrid(*coords))
    coords = np.array([c.swapaxes(0,1).flatten("F") for c in coords]).T
    if len(shape) == 2:
        c = coords
        coords = np.zeros((Nn, 3))
        coords[:, :2] = c
    # CONNECTIVITY
    conn = _make_conn(shape)
    # MESH INSTANCE
    mesh = Mesh(nlabels = nindex,
                coords  = coords,
                elabels = eindex,
                conn = conn,)
    if len(shape) == 2: mesh.elements[("type", "argiope")] = "quad4"
    if len(shape) == 3: mesh.elements[("type", "argiope")] = "hexa8"
    return mesh


Instruction:
Implement a function in Python 3 to
set the elements of a single element group.

Response:
def set_elements(self, elabels = None,
                         types = None,
                         stypes = "",
                         conn = None,
                         esets = {},
                         surfaces = {},
                         materials = "",
                         **kwargs):
    """
    Sets the element data.

    :arg elabels: element labels. Items be strictly positive and int typed
                  in 1D array-like with shape :math:`(N_e)`.
    :type elabels: 1D uint typed array-like
    :arg types: element types chosen among argiope specific element types.
    :type types: str typed array-like
    :arg stypes: element types chosen in solver (depends on the chosen solver) specific element types.
    :type stypes: str typed array-like
    :arg conn: connectivity table. In order to deal with non rectangular tables, :math:`0` can be used to fill missing data.
    :type conn: uint typed array-like
    :arg esets: element sets. Contains boolean array-like of shape :math:`(N_e)`.
    :type esets: dict
    :arg surfaces: surfaces. Contains boolean array-like of shape :math:`(N_e, N_s )` with :math:`N_s` being the maximum number of faces on a single element.
    :type surfaces: dict
    :arg materials: material keys. Any number a of materials can be used.
    :type materials: str typed array-like

    """
    # COLUMNS BUILDING
    if elabels is None:
       warnings.warn(
       "Since no element labels where provided, no elements where created",
       Warning)
       self.elements = None
    else:
      columns = pd.MultiIndex.from_tuples([("type", "argiope", "")])
      self.elements = pd.DataFrame(data = types,
                                   columns = columns,
                                   index = elabels)
      self.elements.index.name = "element"
      self.elements.loc[:, ("type", "solver", "")] = stypes
      # Connectivity
      c = pd.DataFrame(conn, index = elabels)
      c.fillna(0, inplace = True)
      c[:] = c.values.astype(np.int32)
      c.columns = pd.MultiIndex.from_product([["conn"],
                                              ["n{0}".format(n) for
                                               n in np.arange(c.shape[1])],
                                              [""]])
      self.elements = self.elements.join(c)
      # Sets
      for k, v in esets.items(): self.elements[("sets", k, "")] = v
      self.elements["sets", "all", ""] = True
      # Surfaces
      for k, v in surfaces.items():
        for fk, vv in v.items():
          self.elements[("surfaces", k, "s{0}".format(fk))] = vv
      # Materials
      self.elements[("materials", "", "") ] = materials
      self.elements.sort_index(axis = 1, inplace = True)


Instruction:
Write a Python 3 script to
add the fields to the list of fields.

Response:
def add_fields(self, fields = None, **kwargs):
    """
    Add the fields into the list of fields.
    """
    if fields != None:
      for field in fields:
        self.fields.append(field)


Instruction:
Write a Python 3 function that can
return the dimension of the embedded space of each element.

Response:
def space(self):
    """
    Returns the dimension of the embedded space of each element.
    """
    return self.elements.type.argiope.map(
           lambda t: ELEMENTS[t].space)


Instruction:
How would you implement a function in Python 3 that
returns the number of vertices of the eache element according to its type

Response:
def nvert(self):
    """
    Returns the number of vertices of eache element according to its type/
    """
    return self.elements.type.argiope.map(
           lambda t: ELEMENTS[t].nvert)


Instruction:
How would you code a function in Python 3 to
split the tree into a list of nodes.

Response:
def split(self, into = "edges", loc = None,
            at = "labels", sort_index = True):
    """
    Returns the decomposition of the elements.

    Inputs:
    * into: must be in ['edges', 'faces', 'simplices', 'angles']
    * loc: None or labels of the chosen elements.
    * at: must be in ['labels', 'coords']
    """
    if type(loc) == type(None):
      elements = self.elements
    else:
      elements = self.elements.loc[loc]
    out = []
    for etype, group in elements.groupby([("type", "argiope", "")]):
      try:
        output_maps = getattr(ELEMENTS[etype], into)
        for om in range(len(output_maps)):
          oshape = len(output_maps[om])
          conn = group.conn
          columns = pd.MultiIndex.from_product([(om,), np.arange(oshape)],
                                                names = [into, "vertex"])
          data = (conn.values[:, output_maps[om]].reshape(len(conn), oshape))
          df = pd.DataFrame(data = data,
                            columns = columns,
                            index = conn.index).stack((0,1))
          out.append(df)
      except:
        print("Can not extract '{0}' from '{1}'".format(into, etype))
    if len(out) != 0:
      out = pd.concat(out)
      out.sort_index(inplace = True)
      if at == "coords":
        data = self.nodes.coords.loc[out.values].values
        out = pd.DataFrame(index = out.index, data = data,
                           columns = ["x", "y", "z"])
      return out


Instruction:
Make a summary of the following Python 3 code
def centroids_and_volumes(self, sort_index = True):
    elements = self.elements
    out = []
    for etype, group in self.elements.groupby([("type", "argiope", "")]):
      etype_info = ELEMENTS[etype]
      simplices_info = etype_info.simplices
      index = group.index
      simplices_data = self.split(into = "simplices",
                             loc = index,
                             at = "coords")
      simplices = simplices_data.values.reshape(
                  index.size,
                  simplices_info.shape[0],
                  simplices_info.shape[1],
                  3)
      edges = simplices[:,:,1:] - simplices[:,:,:1]
      simplices_centroids = simplices.mean(axis = 2)
      if etype_info.space == 2:
        simplices_volumes = np.linalg.norm(
                  np.cross(edges[:,:,0],
                           edges[:,:,1],
                           axis = 2),
                  axis = 2)/2.
      elif etype_info.space == 3:
        simplices_volumes =  (np.cross(edges[:,:,0],
                                       edges[:,:,1], axis = 2)
                             * edges[:,:, 2]).sum(axis = 2) / 6.
      elements_volumes = simplices_volumes.sum(axis = 1)
      elements_centroids = ((simplices_volumes.reshape(*simplices_volumes.shape, 1)
                          * simplices_centroids).sum(axis = 1)
                          / elements_volumes.reshape(*elements_volumes.shape,1))
      volumes_df = pd.DataFrame(index = index,
                                data = elements_volumes,
                                columns = pd.MultiIndex.from_product(
                                [["volume"], [""]]))
      centroids_df = pd.DataFrame(index = index,
                                data = elements_centroids,
                                columns = pd.MultiIndex.from_product(
                                [["centroid"], ["x", "y", "z"]]))
      out.append(pd.concat([volumes_df, centroids_df], axis = 1))
    out = pd.concat(out)
    if sort_index: out.sort_index(inplace = True)
    return out.sort_index(axis= 1)

Response: Returns a dataframe containing the volume and centroids of all the elements.


Instruction:
Write a Python 3 script for
returning the internal angles of all elements and the associated statistics

Response:
def angles(self, zfill = 3):
    """
    Returns the internal angles of all elements and the associated statistics
    """
    elements = self.elements.sort_index(axis = 1)
    etypes = elements[("type", "argiope")].unique()
    out = []
    for etype in etypes:
      etype_info = ELEMENTS[etype]
      angles_info = etype_info.angles
      loc = elements[("type", "argiope", "")] == etype
      index = elements.loc[loc].index
      angles_data = self.split(into = "angles",
                             loc = loc,
                             at = "coords")
      data = angles_data.values.reshape(index.size,
                                        angles_info.shape[0],
                                        angles_info.shape[1],
                                        3)
      edges = data[:,:,[0,2],:] - data[:,:,1:2,:]
      edges /= np.linalg.norm(edges, axis = 3).reshape(
               index.size, angles_info.shape[0], 2, 1)
      angles = np.degrees(np.arccos((
               edges[:,:,0] * edges[:,:,1]).sum(axis = 2)))

      deviation = angles - etype_info.optimal_angles
      angles_df = pd.DataFrame(index = index,
                               data = angles,
                               columns = pd.MultiIndex.from_product(
      [["angles"], ["a" + "{0}".format(s).zfill(zfill)
              for s in range(angles_info.shape[0])]]))
      deviation_df = pd.DataFrame(index = index,
                               data = deviation,
                               columns = pd.MultiIndex.from_product(
      [["deviation"], ["d" + "{0}".format(s).zfill(zfill)
              for s in range(angles_info.shape[0])]]))

      df = pd.concat([angles_df, deviation_df], axis = 1).sort_index(axis = 1)
      df["stats", "max_angle"] = df.angles.max(axis = 1)
      df["stats", "min_angle"] = df.angles.min(axis = 1)
      df["stats", "max_angular_deviation"] = df.deviation.max(axis = 1)
      df["stats", "min_angular_deviation"] = df.deviation.min(axis = 1)
      df["stats", "max_abs_angular_deviation"] = abs(df.deviation).max(axis = 1)
      df = df.sort_index(axis = 1)
      out.append(df)

    out = pd.concat(out).sort_index(axis = 1)
    return out


Instruction:
Can you implement a function in Python 3 that
returns the aspect ratio of all elements in the sequence.

Response:
def edges(self, zfill = 3):
    """
    Returns the aspect ratio of all elements.
    """
    edges = self.split("edges", at = "coords").unstack()
    edges["lx"] = edges.x[1]-edges.x[0]
    edges["ly"] = edges.y[1]-edges.y[0]
    edges["lz"] = edges.z[1]-edges.z[0]
    edges["l"] = np.linalg.norm(edges[["lx", "ly", "lz"]], axis = 1)
    edges = (edges.l).unstack()
    edges.columns = pd.MultiIndex.from_product([["length"],
                    ["e" + "{0}".format(s).zfill(zfill)
                    for s in np.arange(edges.shape[1])]])
    edges[("stats", "lmax")] = edges.length.max(axis = 1)
    edges[("stats", "lmin")] = edges.length.min(axis = 1)
    edges[("stats", "aspect_ratio")] = edges.stats.lmax / edges.stats.lmin
    return edges.sort_index(axis = 1)


Instruction:
Make a summary of the following Python 3 code
def stats(self):
    cv = self.centroids_and_volumes()
    angles  = self.angles()
    edges = self.edges()
    return pd.concat([cv , angles[["stats"]], edges[["stats"]] ],
                     axis = 1).sort_index(axis = 1)

Response: Returns a DataFrame with the stats of the current node.


Instruction:
Can you create a Python 3 function that
makes a node set from an element set.

Response:
def element_set_to_node_set(self, tag):
    """
    Makes a node set from an element set.
    """
    nodes, elements = self.nodes, self.elements
    loc = (elements.conn[elements[("sets", tag, "")]]
           .stack().stack().unique())
    loc = loc[loc != 0]
    nodes[("sets", tag)] = False
    nodes.loc[loc, ("sets", tag) ] = True


Instruction:
Can you generate the documentation for the following Python 3 function
def node_set_to_surface(self, tag):
    # Create a dummy node with label 0
    nodes = self.nodes.copy()
    dummy = nodes.iloc[0].copy()
    dummy["coords"] *= np.nan
    dummy["sets"] = True
    nodes.loc[0] = dummy
    # Getting element surfaces
    element_surfaces= self.split("surfaces").unstack()
    # killer hack !
    surf = pd.DataFrame(
             nodes.sets[tag].loc[element_surfaces.values.flatten()]
                   .values.reshape(element_surfaces.shape)
                   .prod(axis = 1)
                   .astype(np.bool),
             index = element_surfaces.index).unstack().fillna(False)
    for k in surf.keys():
      self.elements["surfaces", tag, "f{0}".format(k[1]+1) ] = surf.loc[:, k]

Response: Converts a node set to surface.


Instruction:
Create a Python 3 function for
creating elements sets corresponding to a surface.

Response:
def surface_to_element_sets(self, tag):
    """
    Creates elements sets corresponding to a surface.
    """
    surface = self.elements.surfaces[tag]
    for findex in surface.keys():
      if surface[findex].sum() != 0:
        self.elements[("sets", "_SURF_{0}_FACE{1}"
                     .format(tag, findex[1:]), "")] = surface[findex]


Instruction:
Make a summary of the following Python 3 code
def to_polycollection(self, *args, **kwargs):
    from matplotlib import collections
    nodes, elements = self.nodes, self.elements.reset_index()
    verts = []
    index = []
    for etype, group in elements.groupby([("type", "argiope", "")]):
      index += list(group.index)
      nvert = ELEMENTS[etype].nvert
      conn = group.conn.values[:, :nvert].flatten()
      coords = nodes.coords[["x", "y"]].loc[conn].values.reshape(
                                            len(group), nvert, 2)
      verts += list(coords)
    verts = np.array(verts)
    verts= verts[np.argsort(index)]
    return collections.PolyCollection(verts, *args,**kwargs )

Response: Returns the mesh as matplotlib polygon collection.


Instruction:
How would you explain what the following Python 3 function does
def to_triangulation(self):
    from matplotlib.tri import Triangulation
    conn = self.split("simplices").unstack()
    coords = self.nodes.coords.copy()
    node_map  = pd.Series(data = np.arange(len(coords)), index = coords.index)
    conn = node_map.loc[conn.values.flatten()].values.reshape(*conn.shape)
    return Triangulation(coords.x.values, coords.y.values, conn)

Response: Returns the mesh as a matplotlib. tri. Triangulation instance.


Instruction:
Write a Python 3 script for
returning the fields metadata as a dataframe.

Response:
def fields_metadata(self):
    """
    Returns fields metadata as a dataframe.
    """
    return (pd.concat([f.metadata() for f in self.fields], axis = 1)
            .transpose()
            .sort_values(["step_num", "frame", "label", "position"]))


Instruction:
Can you create a Python 3 function that
returns a dataframe containing the metadata of the current node.

Response:
def metadata(self):
    """
    Returns metadata as a dataframe.
    """
    return pd.Series({
           "part": self.part,
           "step_num": self.step_num,
           "step_label": self.step_label,
           "frame": self.frame,
           "frame_value": self.frame_value,
           "label": self.label,
           "position": self.position,
    })


Instruction:
Can you tell what is the following Python 3 function doing
def make_directories(self):
    if os.path.isdir(self.workdir) == False: os.mkdir(self.workdir)

Response: Checks if required directories exist and creates them if needed.


Instruction:
Here you have a function in Python 3, explain what it does
def run_postproc(self):
    t0 = time.time()
    if self.verbose:
      print('#### POST-PROCESSING "{0}" USING POST-PROCESSOR "{1}"'.format(self.label,
                                               self.solver.upper()))
    if self.solver == "abaqus":
      command = '{0} viewer noGUI={1}_abqpp.py'.format(self.solver_path, self.label)
      process = subprocess.Popen(
                command,
                cwd    = self.workdir,
                shell  = True,
                stdout = subprocess.PIPE,
                stderr = subprocess.STDOUT)

      for line in iter(process.stdout.readline, b''):
         line = line.rstrip().decode('utf8')
         print("    ", line)
    t1 = time.time()
    if self.verbose:
      print('  => POST-PROCESSED {0}: DURATION = {1:.2f}s >'.format(self.label,
                                                                  t1 - t0))

Response: Runs the post - proc script.


Instruction:
Explain what the following Python 3 code does
def run_gmsh(self):
    argiope.utils.run_gmsh(gmsh_path = self.gmsh_path,
                           gmsh_space = self.gmsh_space,
                           gmsh_options = self.gmsh_options,
                           name = self.file_name + ".geo",
                           workdir = self.workdir)
    self.mesh = argiope.mesh.read_msh(self.workdir + self.file_name + ".msh")

Response: Takes the gmsh file and runs it


Instruction:
Implement a Python 3 function for
reading a history output report.

Response:
def read_history_report(path, steps, x_name = None):
  """
  Reads an history output report.
  """
  data = pd.read_csv(path, delim_whitespace = True)
  if x_name != None:
    data[x_name] = data.X
    del data["X"]

  data["step"] = 0
  t = 0.
  for i in range(len(steps)):
    dt = steps[i].duration
    loc = data[data.t == t].index
    if len(loc) == 2:
      data.loc[loc[1]:, "step"] = i
    t += dt
  return data


Instruction:
Make a summary of the following Python 3 code
def read_field_report(path, data_flag = "*DATA", meta_data_flag = "*METADATA"):
  text = open(path).read()
  mdpos = text.find(meta_data_flag)
  dpos = text.find(data_flag)
  mdata = io.StringIO( "\n".join(text[mdpos:dpos].split("\n")[1:]))
  data = io.StringIO( "\n".join(text[dpos:].split("\n")[1:]))
  data = pd.read_csv(data, index_col = 0)
  data = data.groupby(data.index).mean()
  mdata = pd.read_csv(mdata, sep = "=", header = None, index_col = 0)[1]
  mdata = mdata.to_dict()
  out = {}
  out["step_num"] = int(mdata["step_num"])
  out["step_label"] = mdata["step_label"]
  out["frame"] = int(mdata["frame"])
  out["frame_value"] = float(mdata["frame_value"])
  out["part"] = mdata["instance"]
  position_map = {"NODAL": "node",
                  "ELEMENT_CENTROID": "element",
                  "WHOLE_ELEMENT": "element"}
  out["position"] = position_map[mdata["position"]]
  out["label"] = mdata["label"]
  out["data"] = data
  field_class = getattr(argiope.mesh, mdata["argiope_class"])
  return field_class(**out)

Response: Reads a field output report.


Instruction:
Write a Python 3 script to
convert a list - like to string with given line width.

Response:
def list_to_string(l = range(200), width = 40, indent = "  "):
    """
    Converts a list-like to string with given line width.
    """
    l = [str(v) + "," for v in l]
    counter = 0
    out = "" + indent
    for w in l:
        s = len(w)
        if counter + s > width:
            out += "\n" + indent
            counter = 0
        out += w
        counter += s
    return out.strip(",")


Instruction:
Given the following Python 3 function, write the documentation
def _equation(nodes = (1, 2), dofs = (1, 1), coefficients = (1., 1.),
              comment = None):
    """
    Returns an Abaqus INP formated string for a given linear equation.
    """
    N = len(nodes)
    if comment == None:
      out = ""
    else:
      out = "**EQUATION: {0}\n".format(comment)
    out+= "*EQUATION\n  {0}\n  ".format(N)
    out += "\n  ".join([ ",".join([ str(nodes[i]),
                                  str(int(dofs[i])),
                                  str(coefficients[i]) ]) for i in range(N)])
    return out

Response: Returns an Abaqus INP formated string for a given linear equation.


Instruction:
Implement a function in Python 3 to
return a set as inp string with unsorted option.

Response:
def _unsorted_set(df, label, **kwargs):
    """
    Returns a set as inp string with unsorted option.
    """
    out = "*NSET, NSET={0}, UNSORTED\n".format(label)
    labels = df.index.values
    return out + argiope.utils.list_to_string(labels, **kwargs)


Instruction:
How would you explain what the following Python 3 function does
def parse_response(self, response):
        payload = None
        try:
            if isinstance(response.json, collections.Callable):
                payload = response.json()
            else:
                # json isn't callable in old versions of requests
                payload = response.json
        except ValueError:
            # response does not have JSON content
            payload = response.content

        if not self._raise_errors:
            return payload
        else:
            if response.status_code == 401:
                raise AuthenticationError(payload['message'])
            elif response.status_code == 500:
                raise ServerError(payload['message'])
            elif isinstance(payload, dict) and not payload['success']:
                raise APIError(payload['message'])
            else:
                return payload

Response:
Parses the API response and raises appropriate
AttributeNames errors if raise_errors was set to True


Instruction:
Write a Python 3 function for
building the url for the specified method and arguments and returns the response as a dictionary.

Response:
def _get(self, method, **kwargs):
        """Builds the url for the specified method and arguments and returns
        the response as a dictionary.
        """

        payload = kwargs.copy()
        payload['api_key'] = self.api_key
        payload['api_secret'] = self.api_secret

        to = payload.pop('to', None)
        if to:
            if isinstance(to, basestring):
                payload['to'] = to
            else:
                # Presumably it's a list or tuple
                for num_i, fax_num in enumerate(to):
                    payload['to[%d]' % num_i] = fax_num

        files = payload.pop('files', [])
        if not isinstance(files, (list, tuple)): files = (files,)

        req_files = {}
        for file_i, f in enumerate(files):
            if isinstance(f, basestring):
                req_files['filename[%d]' % file_i] = open(f, 'rb')
            else:
                f.seek(0)
                req_files['filename[%d]' % file_i] = f

        url = '%s/v%d/%s' % (self.BASE_URL, self.VERSION, method)

        r = requests.post(url, data=payload, files=req_files)

        return self.parse_response(r)


Instruction:
Create a Python 3 function for
returning the material definition as a string in Abaqus INP format.

Response:
def write_inp(self):
     """
     Returns the material definition as a string in Abaqus INP format.
     """
     template = self.get_template()
     return template.substitute({"class": self.__class__.__name__,
                                 "label": self.label}).strip()


Instruction:
How would you explain what the following Python 3 function does
def write_inp(self):
    template = self.get_template()
    plastic_table = self.get_plastic_table()
    return template.substitute({
        "class": self.__class__.__name__,
        "label": self.label,
        "young_modulus": self.young_modulus,
        "poisson_ratio": self.poisson_ratio,
        "plastic_table": (self.get_plastic_table()[["stress", "plastic_strain"]]
                          .to_csv(header = False,
                                  index = False,
                                  sep = ",").strip())}).strip()

Response: Returns the material definition as a string in Abaqus INP format.


Instruction:
Explain what the following Python 3 code does
def get_plastic_table(self):
     E = self.young_modulus
     sy = self.yield_stress
     n = self.hardening_exponent
     eps_max = self.max_strain
     Np = self.strain_data_points
     ey = sy/E
     s = 10.**np.linspace(0., np.log10(eps_max/ey), Np)
     strain = ey * s
     stress = sy * s**n
     plastic_strain = strain - stress / E
     return pd.DataFrame({"strain": strain,
                          "stress": stress,
                          "plastic_strain": plastic_strain})

Response: Calculates the plastic data for a given set of data points.


Instruction:
Can you implement a function in Python 3 that
calculates the plastic data for a given set of data points.

Response:
def get_plastic_table(self):
     """
     Calculates the plastic data
     """
     K = self.consistency
     sy = self.yield_stress
     n = self.hardening_exponent
     eps_max = self.max_strain
     Np = self.strain_data_points
     plastic_strain = np.linspace(0., eps_max, Np)
     stress = sy + K * plastic_strain**n
     return pd.DataFrame({"stress": stress,
                          "plastic_strain": plastic_strain})