How to use dispatch method in ladle

Best JavaScript code snippet using ladle

generate-dispatch-table.py

Source:generate-dispatch-table.py Github

copy

Full Screen

1#!/usr/bin/env python32#3# Copyright (C) 2016 Google, Inc.4#5# Licensed under the Apache License, Version 2.0 (the "License");6# you may not use this file except in compliance with the License.7# You may obtain a copy of the License at8#9# http://www.apache.org/licenses/LICENSE-2.010#11# Unless required by applicable law or agreed to in writing, software12# distributed under the License is distributed on an "AS IS" BASIS,13# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.14# See the License for the specific language governing permissions and15# limitations under the License.16"""Generate Vulkan dispatch table.17"""18import os19import sys20class Command(object):21 PLATFORM = 022 LOADER = 123 INSTANCE = 224 DEVICE = 325 def __init__(self, name, dispatch):26 self.name = name27 self.dispatch = dispatch28 self.ty = self._get_type()29 @staticmethod30 def valid_c_typedef(c):31 return (c.startswith("typedef") and32 c.endswith(");") and33 "*PFN_vkVoidFunction" not in c)34 @classmethod35 def from_c_typedef(cls, c):36 name_begin = c.find("*PFN_vk") + 737 name_end = c.find(")(", name_begin)38 name = c[name_begin:name_end]39 dispatch_begin = name_end + 240 dispatch_end = c.find(" ", dispatch_begin)41 dispatch = c[dispatch_begin:dispatch_end]42 if not dispatch.startswith("Vk"):43 dispatch = None44 return cls(name, dispatch)45 def _get_type(self):46 if self.dispatch:47 if self.dispatch in ["VkDevice", "VkQueue", "VkCommandBuffer"]:48 return self.DEVICE49 else:50 return self.INSTANCE51 else:52 if self.name in ["GetInstanceProcAddr"]:53 return self.PLATFORM54 else:55 return self.LOADER56 def __repr__(self):57 return "Command(name=%s, dispatch=%s)" % \58 (repr(self.name), repr(self.dispatch))59class Extension(object):60 def __init__(self, name, version, guard=None, commands=[]):61 self.name = name62 self.version = version63 self.guard = guard64 self.commands = commands[:]65 def add_command(self, cmd):66 self.commands.append(cmd)67 def __repr__(self):68 lines = []69 lines.append("Extension(name=%s, version=%s, guard=%s, commands=[" %70 (repr(self.name), repr(self.version), repr(self.guard)))71 for cmd in self.commands:72 lines.append(" %s," % repr(cmd))73 lines.append("])")74 return "\n".join(lines)75# generated by "generate-dispatch-table.py parse vulkan.h"76vk_core = Extension(name='VK_core', version=0, guard=None, commands=[77 Command(name='CreateInstance', dispatch=None),78 Command(name='DestroyInstance', dispatch='VkInstance'),79 Command(name='EnumeratePhysicalDevices', dispatch='VkInstance'),80 Command(name='GetPhysicalDeviceFeatures', dispatch='VkPhysicalDevice'),81 Command(name='GetPhysicalDeviceFormatProperties', dispatch='VkPhysicalDevice'),82 Command(name='GetPhysicalDeviceImageFormatProperties', dispatch='VkPhysicalDevice'),83 Command(name='GetPhysicalDeviceProperties', dispatch='VkPhysicalDevice'),84 Command(name='GetPhysicalDeviceQueueFamilyProperties', dispatch='VkPhysicalDevice'),85 Command(name='GetPhysicalDeviceMemoryProperties', dispatch='VkPhysicalDevice'),86 Command(name='GetInstanceProcAddr', dispatch='VkInstance'),87 Command(name='GetDeviceProcAddr', dispatch='VkDevice'),88 Command(name='CreateDevice', dispatch='VkPhysicalDevice'),89 Command(name='DestroyDevice', dispatch='VkDevice'),90 Command(name='EnumerateInstanceExtensionProperties', dispatch=None),91 Command(name='EnumerateDeviceExtensionProperties', dispatch='VkPhysicalDevice'),92 Command(name='EnumerateInstanceLayerProperties', dispatch=None),93 Command(name='GetDeviceQueue', dispatch='VkDevice'),94 Command(name='QueueSubmit', dispatch='VkQueue'),95 Command(name='QueueWaitIdle', dispatch='VkQueue'),96 Command(name='DeviceWaitIdle', dispatch='VkDevice'),97 Command(name='AllocateMemory', dispatch='VkDevice'),98 Command(name='FreeMemory', dispatch='VkDevice'),99 Command(name='MapMemory', dispatch='VkDevice'),100 Command(name='UnmapMemory', dispatch='VkDevice'),101 Command(name='FlushMappedMemoryRanges', dispatch='VkDevice'),102 Command(name='InvalidateMappedMemoryRanges', dispatch='VkDevice'),103 Command(name='GetDeviceMemoryCommitment', dispatch='VkDevice'),104 Command(name='BindBufferMemory', dispatch='VkDevice'),105 Command(name='BindImageMemory', dispatch='VkDevice'),106 Command(name='GetBufferMemoryRequirements', dispatch='VkDevice'),107 Command(name='GetImageMemoryRequirements', dispatch='VkDevice'),108 Command(name='GetImageSparseMemoryRequirements', dispatch='VkDevice'),109 Command(name='GetPhysicalDeviceSparseImageFormatProperties', dispatch='VkPhysicalDevice'),110 Command(name='QueueBindSparse', dispatch='VkQueue'),111 Command(name='CreateFence', dispatch='VkDevice'),112 Command(name='DestroyFence', dispatch='VkDevice'),113 Command(name='ResetFences', dispatch='VkDevice'),114 Command(name='GetFenceStatus', dispatch='VkDevice'),115 Command(name='WaitForFences', dispatch='VkDevice'),116 Command(name='CreateSemaphore', dispatch='VkDevice'),117 Command(name='DestroySemaphore', dispatch='VkDevice'),118 Command(name='CreateEvent', dispatch='VkDevice'),119 Command(name='DestroyEvent', dispatch='VkDevice'),120 Command(name='GetEventStatus', dispatch='VkDevice'),121 Command(name='SetEvent', dispatch='VkDevice'),122 Command(name='ResetEvent', dispatch='VkDevice'),123 Command(name='CreateQueryPool', dispatch='VkDevice'),124 Command(name='DestroyQueryPool', dispatch='VkDevice'),125 Command(name='GetQueryPoolResults', dispatch='VkDevice'),126 Command(name='CreateBuffer', dispatch='VkDevice'),127 Command(name='DestroyBuffer', dispatch='VkDevice'),128 Command(name='CreateBufferView', dispatch='VkDevice'),129 Command(name='DestroyBufferView', dispatch='VkDevice'),130 Command(name='CreateImage', dispatch='VkDevice'),131 Command(name='DestroyImage', dispatch='VkDevice'),132 Command(name='GetImageSubresourceLayout', dispatch='VkDevice'),133 Command(name='CreateImageView', dispatch='VkDevice'),134 Command(name='DestroyImageView', dispatch='VkDevice'),135 Command(name='CreateShaderModule', dispatch='VkDevice'),136 Command(name='DestroyShaderModule', dispatch='VkDevice'),137 Command(name='CreatePipelineCache', dispatch='VkDevice'),138 Command(name='DestroyPipelineCache', dispatch='VkDevice'),139 Command(name='GetPipelineCacheData', dispatch='VkDevice'),140 Command(name='MergePipelineCaches', dispatch='VkDevice'),141 Command(name='CreateGraphicsPipelines', dispatch='VkDevice'),142 Command(name='CreateComputePipelines', dispatch='VkDevice'),143 Command(name='DestroyPipeline', dispatch='VkDevice'),144 Command(name='CreatePipelineLayout', dispatch='VkDevice'),145 Command(name='DestroyPipelineLayout', dispatch='VkDevice'),146 Command(name='CreateSampler', dispatch='VkDevice'),147 Command(name='DestroySampler', dispatch='VkDevice'),148 Command(name='CreateDescriptorSetLayout', dispatch='VkDevice'),149 Command(name='DestroyDescriptorSetLayout', dispatch='VkDevice'),150 Command(name='CreateDescriptorPool', dispatch='VkDevice'),151 Command(name='DestroyDescriptorPool', dispatch='VkDevice'),152 Command(name='ResetDescriptorPool', dispatch='VkDevice'),153 Command(name='AllocateDescriptorSets', dispatch='VkDevice'),154 Command(name='FreeDescriptorSets', dispatch='VkDevice'),155 Command(name='UpdateDescriptorSets', dispatch='VkDevice'),156 Command(name='CreateFramebuffer', dispatch='VkDevice'),157 Command(name='DestroyFramebuffer', dispatch='VkDevice'),158 Command(name='CreateRenderPass', dispatch='VkDevice'),159 Command(name='DestroyRenderPass', dispatch='VkDevice'),160 Command(name='GetRenderAreaGranularity', dispatch='VkDevice'),161 Command(name='CreateCommandPool', dispatch='VkDevice'),162 Command(name='DestroyCommandPool', dispatch='VkDevice'),163 Command(name='ResetCommandPool', dispatch='VkDevice'),164 Command(name='AllocateCommandBuffers', dispatch='VkDevice'),165 Command(name='FreeCommandBuffers', dispatch='VkDevice'),166 Command(name='BeginCommandBuffer', dispatch='VkCommandBuffer'),167 Command(name='EndCommandBuffer', dispatch='VkCommandBuffer'),168 Command(name='ResetCommandBuffer', dispatch='VkCommandBuffer'),169 Command(name='CmdBindPipeline', dispatch='VkCommandBuffer'),170 Command(name='CmdSetViewport', dispatch='VkCommandBuffer'),171 Command(name='CmdSetScissor', dispatch='VkCommandBuffer'),172 Command(name='CmdSetLineWidth', dispatch='VkCommandBuffer'),173 Command(name='CmdSetDepthBias', dispatch='VkCommandBuffer'),174 Command(name='CmdSetBlendConstants', dispatch='VkCommandBuffer'),175 Command(name='CmdSetDepthBounds', dispatch='VkCommandBuffer'),176 Command(name='CmdSetStencilCompareMask', dispatch='VkCommandBuffer'),177 Command(name='CmdSetStencilWriteMask', dispatch='VkCommandBuffer'),178 Command(name='CmdSetStencilReference', dispatch='VkCommandBuffer'),179 Command(name='CmdBindDescriptorSets', dispatch='VkCommandBuffer'),180 Command(name='CmdBindIndexBuffer', dispatch='VkCommandBuffer'),181 Command(name='CmdBindVertexBuffers', dispatch='VkCommandBuffer'),182 Command(name='CmdDraw', dispatch='VkCommandBuffer'),183 Command(name='CmdDrawIndexed', dispatch='VkCommandBuffer'),184 Command(name='CmdDrawIndirect', dispatch='VkCommandBuffer'),185 Command(name='CmdDrawIndexedIndirect', dispatch='VkCommandBuffer'),186 Command(name='CmdDispatch', dispatch='VkCommandBuffer'),187 Command(name='CmdDispatchIndirect', dispatch='VkCommandBuffer'),188 Command(name='CmdCopyBuffer', dispatch='VkCommandBuffer'),189 Command(name='CmdCopyImage', dispatch='VkCommandBuffer'),190 Command(name='CmdBlitImage', dispatch='VkCommandBuffer'),191 Command(name='CmdCopyBufferToImage', dispatch='VkCommandBuffer'),192 Command(name='CmdCopyImageToBuffer', dispatch='VkCommandBuffer'),193 Command(name='CmdUpdateBuffer', dispatch='VkCommandBuffer'),194 Command(name='CmdFillBuffer', dispatch='VkCommandBuffer'),195 Command(name='CmdClearColorImage', dispatch='VkCommandBuffer'),196 Command(name='CmdClearDepthStencilImage', dispatch='VkCommandBuffer'),197 Command(name='CmdClearAttachments', dispatch='VkCommandBuffer'),198 Command(name='CmdResolveImage', dispatch='VkCommandBuffer'),199 Command(name='CmdSetEvent', dispatch='VkCommandBuffer'),200 Command(name='CmdResetEvent', dispatch='VkCommandBuffer'),201 Command(name='CmdWaitEvents', dispatch='VkCommandBuffer'),202 Command(name='CmdPipelineBarrier', dispatch='VkCommandBuffer'),203 Command(name='CmdBeginQuery', dispatch='VkCommandBuffer'),204 Command(name='CmdEndQuery', dispatch='VkCommandBuffer'),205 Command(name='CmdResetQueryPool', dispatch='VkCommandBuffer'),206 Command(name='CmdWriteTimestamp', dispatch='VkCommandBuffer'),207 Command(name='CmdCopyQueryPoolResults', dispatch='VkCommandBuffer'),208 Command(name='CmdPushConstants', dispatch='VkCommandBuffer'),209 Command(name='CmdBeginRenderPass', dispatch='VkCommandBuffer'),210 Command(name='CmdNextSubpass', dispatch='VkCommandBuffer'),211 Command(name='CmdEndRenderPass', dispatch='VkCommandBuffer'),212 Command(name='CmdExecuteCommands', dispatch='VkCommandBuffer'),213])214vk_khr_surface = Extension(name='VK_KHR_surface', version=25, guard=None, commands=[215 Command(name='DestroySurfaceKHR', dispatch='VkInstance'),216 Command(name='GetPhysicalDeviceSurfaceSupportKHR', dispatch='VkPhysicalDevice'),217 Command(name='GetPhysicalDeviceSurfaceCapabilitiesKHR', dispatch='VkPhysicalDevice'),218 Command(name='GetPhysicalDeviceSurfaceFormatsKHR', dispatch='VkPhysicalDevice'),219 Command(name='GetPhysicalDeviceSurfacePresentModesKHR', dispatch='VkPhysicalDevice'),220])221vk_khr_swapchain = Extension(name='VK_KHR_swapchain', version=67, guard=None, commands=[222 Command(name='CreateSwapchainKHR', dispatch='VkDevice'),223 Command(name='DestroySwapchainKHR', dispatch='VkDevice'),224 Command(name='GetSwapchainImagesKHR', dispatch='VkDevice'),225 Command(name='AcquireNextImageKHR', dispatch='VkDevice'),226 Command(name='QueuePresentKHR', dispatch='VkQueue'),227])228vk_khr_display = Extension(name='VK_KHR_display', version=21, guard=None, commands=[229 Command(name='GetPhysicalDeviceDisplayPropertiesKHR', dispatch='VkPhysicalDevice'),230 Command(name='GetPhysicalDeviceDisplayPlanePropertiesKHR', dispatch='VkPhysicalDevice'),231 Command(name='GetDisplayPlaneSupportedDisplaysKHR', dispatch='VkPhysicalDevice'),232 Command(name='GetDisplayModePropertiesKHR', dispatch='VkPhysicalDevice'),233 Command(name='CreateDisplayModeKHR', dispatch='VkPhysicalDevice'),234 Command(name='GetDisplayPlaneCapabilitiesKHR', dispatch='VkPhysicalDevice'),235 Command(name='CreateDisplayPlaneSurfaceKHR', dispatch='VkInstance'),236])237vk_khr_display_swapchain = Extension(name='VK_KHR_display_swapchain', version=9, guard=None, commands=[238 Command(name='CreateSharedSwapchainsKHR', dispatch='VkDevice'),239])240vk_khr_xlib_surface = Extension(name='VK_KHR_xlib_surface', version=6, guard='VK_USE_PLATFORM_XLIB_KHR', commands=[241 Command(name='CreateXlibSurfaceKHR', dispatch='VkInstance'),242 Command(name='GetPhysicalDeviceXlibPresentationSupportKHR', dispatch='VkPhysicalDevice'),243])244vk_khr_xcb_surface = Extension(name='VK_KHR_xcb_surface', version=6, guard='VK_USE_PLATFORM_XCB_KHR', commands=[245 Command(name='CreateXcbSurfaceKHR', dispatch='VkInstance'),246 Command(name='GetPhysicalDeviceXcbPresentationSupportKHR', dispatch='VkPhysicalDevice'),247])248vk_khr_wayland_surface = Extension(name='VK_KHR_wayland_surface', version=5, guard='VK_USE_PLATFORM_WAYLAND_KHR', commands=[249 Command(name='CreateWaylandSurfaceKHR', dispatch='VkInstance'),250 Command(name='GetPhysicalDeviceWaylandPresentationSupportKHR', dispatch='VkPhysicalDevice'),251])252vk_khr_mir_surface = Extension(name='VK_KHR_mir_surface', version=4, guard='VK_USE_PLATFORM_MIR_KHR', commands=[253 Command(name='CreateMirSurfaceKHR', dispatch='VkInstance'),254 Command(name='GetPhysicalDeviceMirPresentationSupportKHR', dispatch='VkPhysicalDevice'),255])256vk_khr_android_surface = Extension(name='VK_KHR_android_surface', version=6, guard='VK_USE_PLATFORM_ANDROID_KHR', commands=[257 Command(name='CreateAndroidSurfaceKHR', dispatch='VkInstance'),258])259vk_khr_win32_surface = Extension(name='VK_KHR_win32_surface', version=5, guard='VK_USE_PLATFORM_WIN32_KHR', commands=[260 Command(name='CreateWin32SurfaceKHR', dispatch='VkInstance'),261 Command(name='GetPhysicalDeviceWin32PresentationSupportKHR', dispatch='VkPhysicalDevice'),262])263vk_ext_debug_report = Extension(name='VK_EXT_debug_report', version=1, guard=None, commands=[264 Command(name='CreateDebugReportCallbackEXT', dispatch='VkInstance'),265 Command(name='DestroyDebugReportCallbackEXT', dispatch='VkInstance'),266 Command(name='DebugReportMessageEXT', dispatch='VkInstance'),267])268extensions = [269 vk_core,270 vk_khr_surface,271 vk_khr_swapchain,272 vk_khr_display,273 vk_khr_display_swapchain,274 vk_khr_xlib_surface,275 vk_khr_xcb_surface,276 vk_khr_wayland_surface,277 vk_khr_mir_surface,278 vk_khr_android_surface,279 vk_khr_win32_surface,280 vk_ext_debug_report,281]282def generate_header(guard):283 lines = []284 lines.append("// This file is generated.")285 lines.append("#ifndef %s" % guard)286 lines.append("#define %s" % guard)287 lines.append("")288 lines.append("#include <vulkan/vulkan.h>")289 lines.append("")290 lines.append("namespace vk {")291 lines.append("")292 for ext in extensions:293 if ext.guard:294 lines.append("#ifdef %s" % ext.guard)295 lines.append("// %s" % ext.name)296 for cmd in ext.commands:297 lines.append("extern PFN_vk%s %s;" % (cmd.name, cmd.name))298 if ext.guard:299 lines.append("#endif")300 lines.append("")301 lines.append("void init_dispatch_table_top(PFN_vkGetInstanceProcAddr get_instance_proc_addr);")302 lines.append("void init_dispatch_table_middle(VkInstance instance, bool include_bottom);")303 lines.append("void init_dispatch_table_bottom(VkInstance instance, VkDevice dev);")304 lines.append("")305 lines.append("} // namespace vk")306 lines.append("")307 lines.append("#endif // %s" % guard)308 return "\n".join(lines)309def get_proc_addr(dispatchable, cmd, guard=None):310 if dispatchable == "dev":311 func = "GetDeviceProcAddr"312 else:313 func = "GetInstanceProcAddr"314 c = " %s = reinterpret_cast<PFN_vk%s>(%s(%s, \"vk%s\"));" % \315 (cmd.name, cmd.name, func, dispatchable, cmd.name)316 if guard:317 c = ("#ifdef %s\n" % guard) + c + "\n#endif"318 return c319def generate_source(header):320 lines = []321 lines.append("// This file is generated.")322 lines.append("#include \"%s\"" % header)323 lines.append("")324 lines.append("namespace vk {")325 lines.append("")326 commands_by_types = {}327 get_instance_proc_addr = None328 get_device_proc_addr = None329 for ext in extensions:330 if ext.guard:331 lines.append("#ifdef %s" % ext.guard)332 for cmd in ext.commands:333 lines.append("PFN_vk%s %s;" % (cmd.name, cmd.name))334 if cmd.ty not in commands_by_types:335 commands_by_types[cmd.ty] = []336 commands_by_types[cmd.ty].append([cmd, ext.guard])337 if cmd.name == "GetInstanceProcAddr":338 get_instance_proc_addr = cmd339 elif cmd.name == "GetDeviceProcAddr":340 get_device_proc_addr = cmd341 if ext.guard:342 lines.append("#endif")343 lines.append("")344 lines.append("void init_dispatch_table_top(PFN_vkGetInstanceProcAddr get_instance_proc_addr)")345 lines.append("{")346 lines.append(" GetInstanceProcAddr = get_instance_proc_addr;")347 lines.append("")348 for cmd, guard in commands_by_types[Command.LOADER]:349 lines.append(get_proc_addr("VK_NULL_HANDLE", cmd, guard))350 lines.append("}")351 lines.append("")352 lines.append("void init_dispatch_table_middle(VkInstance instance, bool include_bottom)")353 lines.append("{")354 lines.append(get_proc_addr("instance", get_instance_proc_addr))355 lines.append("")356 for cmd, guard in commands_by_types[Command.INSTANCE]:357 if cmd == get_instance_proc_addr:358 continue359 lines.append(get_proc_addr("instance", cmd, guard))360 lines.append("")361 lines.append(" if (!include_bottom)")362 lines.append(" return;")363 lines.append("")364 for cmd, guard in commands_by_types[Command.DEVICE]:365 lines.append(get_proc_addr("instance", cmd, guard))366 lines.append("}")367 lines.append("")368 lines.append("void init_dispatch_table_bottom(VkInstance instance, VkDevice dev)")369 lines.append("{")370 lines.append(get_proc_addr("instance", get_device_proc_addr))371 lines.append(get_proc_addr("dev", get_device_proc_addr))372 lines.append("")373 for cmd, guard in commands_by_types[Command.DEVICE]:374 if cmd == get_device_proc_addr:375 continue376 lines.append(get_proc_addr("dev", cmd, guard))377 lines.append("}")378 lines.append("")379 lines.append("} // namespace vk")380 return "\n".join(lines)381def parse_vulkan_h(filename):382 extensions = []383 with open(filename, "r") as f:384 current_ext = None385 ext_guard = None386 spec_version = None387 for line in f:388 line = line.strip();389 if line.startswith("#define VK_API_VERSION"):390 minor_end = line.rfind(",")391 minor_begin = line.rfind(",", 0, minor_end) + 1392 spec_version = int(line[minor_begin:minor_end])393 # add core394 current_ext = Extension("VK_core", spec_version)395 extensions.append(current_ext)396 elif Command.valid_c_typedef(line):397 current_ext.add_command(Command.from_c_typedef(line))398 elif line.startswith("#ifdef VK_USE_PLATFORM"):399 guard_begin = line.find(" ") + 1400 ext_guard = line[guard_begin:]401 elif line.startswith("#define") and "SPEC_VERSION " in line:402 version_begin = line.rfind(" ") + 1403 spec_version = int(line[version_begin:])404 elif line.startswith("#define") and "EXTENSION_NAME " in line:405 name_end = line.rfind("\"")406 name_begin = line.rfind("\"", 0, name_end) + 1407 name = line[name_begin:name_end]408 # add extension409 current_ext = Extension(name, spec_version, ext_guard)410 extensions.append(current_ext)411 elif ext_guard and line.startswith("#endif") and ext_guard in line:412 ext_guard = None413 for ext in extensions:414 print("%s = %s" % (ext.name.lower(), repr(ext)))415 print("")416 print("extensions = [")417 for ext in extensions:418 print(" %s," % ext.name.lower())419 print("]")420if __name__ == "__main__":421 if sys.argv[1] == "parse":422 parse_vulkan_h(sys.argv[2])423 else:424 filename = sys.argv[1]425 base = os.path.basename(filename)426 contents = []427 if base.endswith(".h"):428 contents = generate_header(base.replace(".", "_").upper())429 elif base.endswith(".cpp"):430 contents = generate_source(base.replace(".cpp", ".h"))431 with open(filename, "w") as f:...

Full Screen

Full Screen

unparse.py

Source:unparse.py Github

copy

Full Screen

...27 """Unparser(tree, file=sys.stdout) -> None.28 Print the source for tree to file."""29 self.f = file30 self._indent = 031 self.dispatch(tree)32 print("", file=self.f)33 self.f.flush()34 def fill(self, text = ""):35 "Indent a piece of text, according to the current indentation level"36 self.f.write("\n"+" "*self._indent + text)37 def write(self, text):38 "Append a piece of text to the current line."39 self.f.write(text)40 def enter(self):41 "Print ':', and increase the indentation."42 self.write(":")43 self._indent += 144 def leave(self):45 "Decrease the indentation level."46 self._indent -= 147 def dispatch(self, tree):48 "Dispatcher function, dispatching tree type T to method _T."49 if isinstance(tree, list):50 for t in tree:51 self.dispatch(t)52 return53 meth = getattr(self, "_"+tree.__class__.__name__)54 meth(tree)55 ############### Unparsing methods ######################56 # There should be one method per concrete grammar type #57 # Constructors should be grouped by sum type. Ideally, #58 # this would follow the order in the grammar, but #59 # currently doesn't. #60 ########################################################61 def _Module(self, tree):62 for stmt in tree.body:63 self.dispatch(stmt)64 # stmt65 def _Expr(self, tree):66 self.fill()67 self.dispatch(tree.value)68 def _Import(self, t):69 self.fill("import ")70 interleave(lambda: self.write(", "), self.dispatch, t.names)71 def _ImportFrom(self, t):72 self.fill("from ")73 self.write("." * t.level)74 if t.module:75 self.write(t.module)76 self.write(" import ")77 interleave(lambda: self.write(", "), self.dispatch, t.names)78 def _Assign(self, t):79 self.fill()80 for target in t.targets:81 self.dispatch(target)82 self.write(" = ")83 self.dispatch(t.value)84 def _AugAssign(self, t):85 self.fill()86 self.dispatch(t.target)87 self.write(" "+self.binop[t.op.__class__.__name__]+"= ")88 self.dispatch(t.value)89 def _Return(self, t):90 self.fill("return")91 if t.value:92 self.write(" ")93 self.dispatch(t.value)94 def _Pass(self, t):95 self.fill("pass")96 def _Break(self, t):97 self.fill("break")98 def _Continue(self, t):99 self.fill("continue")100 def _Delete(self, t):101 self.fill("del ")102 interleave(lambda: self.write(", "), self.dispatch, t.targets)103 def _Assert(self, t):104 self.fill("assert ")105 self.dispatch(t.test)106 if t.msg:107 self.write(", ")108 self.dispatch(t.msg)109 def _Global(self, t):110 self.fill("global ")111 interleave(lambda: self.write(", "), self.write, t.names)112 def _Nonlocal(self, t):113 self.fill("nonlocal ")114 interleave(lambda: self.write(", "), self.write, t.names)115 def _Await(self, t):116 self.write("(")117 self.write("await")118 if t.value:119 self.write(" ")120 self.dispatch(t.value)121 self.write(")")122 def _Yield(self, t):123 self.write("(")124 self.write("yield")125 if t.value:126 self.write(" ")127 self.dispatch(t.value)128 self.write(")")129 def _YieldFrom(self, t):130 self.write("(")131 self.write("yield from")132 if t.value:133 self.write(" ")134 self.dispatch(t.value)135 self.write(")")136 def _Raise(self, t):137 self.fill("raise")138 if not t.exc:139 assert not t.cause140 return141 self.write(" ")142 self.dispatch(t.exc)143 if t.cause:144 self.write(" from ")145 self.dispatch(t.cause)146 def _Try(self, t):147 self.fill("try")148 self.enter()149 self.dispatch(t.body)150 self.leave()151 for ex in t.handlers:152 self.dispatch(ex)153 if t.orelse:154 self.fill("else")155 self.enter()156 self.dispatch(t.orelse)157 self.leave()158 if t.finalbody:159 self.fill("finally")160 self.enter()161 self.dispatch(t.finalbody)162 self.leave()163 def _ExceptHandler(self, t):164 self.fill("except")165 if t.type:166 self.write(" ")167 self.dispatch(t.type)168 if t.name:169 self.write(" as ")170 self.write(t.name)171 self.enter()172 self.dispatch(t.body)173 self.leave()174 def _ClassDef(self, t):175 self.write("\n")176 for deco in t.decorator_list:177 self.fill("@")178 self.dispatch(deco)179 self.fill("class "+t.name)180 self.write("(")181 comma = False182 for e in t.bases:183 if comma: self.write(", ")184 else: comma = True185 self.dispatch(e)186 for e in t.keywords:187 if comma: self.write(", ")188 else: comma = True189 self.dispatch(e)190 self.write(")")191 self.enter()192 self.dispatch(t.body)193 self.leave()194 def _FunctionDef(self, t):195 self.__FunctionDef_helper(t, "def")196 def _AsyncFunctionDef(self, t):197 self.__FunctionDef_helper(t, "async def")198 def __FunctionDef_helper(self, t, fill_suffix):199 self.write("\n")200 for deco in t.decorator_list:201 self.fill("@")202 self.dispatch(deco)203 def_str = fill_suffix+" "+t.name + "("204 self.fill(def_str)205 self.dispatch(t.args)206 self.write(")")207 if t.returns:208 self.write(" -> ")209 self.dispatch(t.returns)210 self.enter()211 self.dispatch(t.body)212 self.leave()213 def _For(self, t):214 self.__For_helper("for ", t)215 def _AsyncFor(self, t):216 self.__For_helper("async for ", t)217 def __For_helper(self, fill, t):218 self.fill(fill)219 self.dispatch(t.target)220 self.write(" in ")221 self.dispatch(t.iter)222 self.enter()223 self.dispatch(t.body)224 self.leave()225 if t.orelse:226 self.fill("else")227 self.enter()228 self.dispatch(t.orelse)229 self.leave()230 def _If(self, t):231 self.fill("if ")232 self.dispatch(t.test)233 self.enter()234 self.dispatch(t.body)235 self.leave()236 # collapse nested ifs into equivalent elifs.237 while (t.orelse and len(t.orelse) == 1 and238 isinstance(t.orelse[0], ast.If)):239 t = t.orelse[0]240 self.fill("elif ")241 self.dispatch(t.test)242 self.enter()243 self.dispatch(t.body)244 self.leave()245 # final else246 if t.orelse:247 self.fill("else")248 self.enter()249 self.dispatch(t.orelse)250 self.leave()251 def _While(self, t):252 self.fill("while ")253 self.dispatch(t.test)254 self.enter()255 self.dispatch(t.body)256 self.leave()257 if t.orelse:258 self.fill("else")259 self.enter()260 self.dispatch(t.orelse)261 self.leave()262 def _With(self, t):263 self.fill("with ")264 interleave(lambda: self.write(", "), self.dispatch, t.items)265 self.enter()266 self.dispatch(t.body)267 self.leave()268 def _AsyncWith(self, t):269 self.fill("async with ")270 interleave(lambda: self.write(", "), self.dispatch, t.items)271 self.enter()272 self.dispatch(t.body)273 self.leave()274 # expr275 def _Bytes(self, t):276 self.write(repr(t.s))277 def _Str(self, tree):278 self.write(repr(tree.s))279 def _Name(self, t):280 self.write(t.id)281 def _NameConstant(self, t):282 self.write(repr(t.value))283 def _Num(self, t):284 # Substitute overflowing decimal literal for AST infinities.285 self.write(repr(t.n).replace("inf", INFSTR))286 def _List(self, t):287 self.write("[")288 interleave(lambda: self.write(", "), self.dispatch, t.elts)289 self.write("]")290 def _ListComp(self, t):291 self.write("[")292 self.dispatch(t.elt)293 for gen in t.generators:294 self.dispatch(gen)295 self.write("]")296 def _GeneratorExp(self, t):297 self.write("(")298 self.dispatch(t.elt)299 for gen in t.generators:300 self.dispatch(gen)301 self.write(")")302 def _SetComp(self, t):303 self.write("{")304 self.dispatch(t.elt)305 for gen in t.generators:306 self.dispatch(gen)307 self.write("}")308 def _DictComp(self, t):309 self.write("{")310 self.dispatch(t.key)311 self.write(": ")312 self.dispatch(t.value)313 for gen in t.generators:314 self.dispatch(gen)315 self.write("}")316 def _comprehension(self, t):317 self.write(" for ")318 self.dispatch(t.target)319 self.write(" in ")320 self.dispatch(t.iter)321 for if_clause in t.ifs:322 self.write(" if ")323 self.dispatch(if_clause)324 def _IfExp(self, t):325 self.write("(")326 self.dispatch(t.body)327 self.write(" if ")328 self.dispatch(t.test)329 self.write(" else ")330 self.dispatch(t.orelse)331 self.write(")")332 def _Set(self, t):333 assert(t.elts) # should be at least one element334 self.write("{")335 interleave(lambda: self.write(", "), self.dispatch, t.elts)336 self.write("}")337 def _Dict(self, t):338 self.write("{")339 def write_key_value_pair(k, v):340 self.dispatch(k)341 self.write(": ")342 self.dispatch(v)343 def write_item(item):344 k, v = item345 if k is None:346 # for dictionary unpacking operator in dicts {**{'y': 2}}347 # see PEP 448 for details348 self.write("**")349 self.dispatch(v)350 else:351 write_key_value_pair(k, v)352 interleave(lambda: self.write(", "), write_item, zip(t.keys, t.values))353 self.write("}")354 def _Tuple(self, t):355 self.write("(")356 if len(t.elts) == 1:357 (elt,) = t.elts358 self.dispatch(elt)359 self.write(",")360 else:361 interleave(lambda: self.write(", "), self.dispatch, t.elts)362 self.write(")")363 unop = {"Invert":"~", "Not": "not", "UAdd":"+", "USub":"-"}364 def _UnaryOp(self, t):365 self.write("(")366 self.write(self.unop[t.op.__class__.__name__])367 self.write(" ")368 self.dispatch(t.operand)369 self.write(")")370 binop = { "Add":"+", "Sub":"-", "Mult":"*", "MatMult":"@", "Div":"/", "Mod":"%",371 "LShift":"<<", "RShift":">>", "BitOr":"|", "BitXor":"^", "BitAnd":"&",372 "FloorDiv":"//", "Pow": "**"}373 def _BinOp(self, t):374 self.write("(")375 self.dispatch(t.left)376 self.write(" " + self.binop[t.op.__class__.__name__] + " ")377 self.dispatch(t.right)378 self.write(")")379 cmpops = {"Eq":"==", "NotEq":"!=", "Lt":"<", "LtE":"<=", "Gt":">", "GtE":">=",380 "Is":"is", "IsNot":"is not", "In":"in", "NotIn":"not in"}381 def _Compare(self, t):382 self.write("(")383 self.dispatch(t.left)384 for o, e in zip(t.ops, t.comparators):385 self.write(" " + self.cmpops[o.__class__.__name__] + " ")386 self.dispatch(e)387 self.write(")")388 boolops = {ast.And: 'and', ast.Or: 'or'}389 def _BoolOp(self, t):390 self.write("(")391 s = " %s " % self.boolops[t.op.__class__]392 interleave(lambda: self.write(s), self.dispatch, t.values)393 self.write(")")394 def _Attribute(self,t):395 self.dispatch(t.value)396 # Special case: 3.__abs__() is a syntax error, so if t.value397 # is an integer literal then we need to either parenthesize398 # it or add an extra space to get 3 .__abs__().399 if isinstance(t.value, ast.Num) and isinstance(t.value.n, int):400 self.write(" ")401 self.write(".")402 self.write(t.attr)403 def _Call(self, t):404 self.dispatch(t.func)405 self.write("(")406 comma = False407 for e in t.args:408 if comma: self.write(", ")409 else: comma = True410 self.dispatch(e)411 for e in t.keywords:412 if comma: self.write(", ")413 else: comma = True414 self.dispatch(e)415 self.write(")")416 def _Subscript(self, t):417 self.dispatch(t.value)418 self.write("[")419 self.dispatch(t.slice)420 self.write("]")421 def _Starred(self, t):422 self.write("*")423 self.dispatch(t.value)424 # slice425 def _Ellipsis(self, t):426 self.write("...")427 def _Index(self, t):428 self.dispatch(t.value)429 def _Slice(self, t):430 if t.lower:431 self.dispatch(t.lower)432 self.write(":")433 if t.upper:434 self.dispatch(t.upper)435 if t.step:436 self.write(":")437 self.dispatch(t.step)438 def _ExtSlice(self, t):439 interleave(lambda: self.write(', '), self.dispatch, t.dims)440 # argument441 def _arg(self, t):442 self.write(t.arg)443 if t.annotation:444 self.write(": ")445 self.dispatch(t.annotation)446 # others447 def _arguments(self, t):448 first = True449 # normal arguments450 defaults = [None] * (len(t.args) - len(t.defaults)) + t.defaults451 for a, d in zip(t.args, defaults):452 if first:first = False453 else: self.write(", ")454 self.dispatch(a)455 if d:456 self.write("=")457 self.dispatch(d)458 # varargs, or bare '*' if no varargs but keyword-only arguments present459 if t.vararg or t.kwonlyargs:460 if first:first = False461 else: self.write(", ")462 self.write("*")463 if t.vararg:464 self.write(t.vararg.arg)465 if t.vararg.annotation:466 self.write(": ")467 self.dispatch(t.vararg.annotation)468 # keyword-only arguments469 if t.kwonlyargs:470 for a, d in zip(t.kwonlyargs, t.kw_defaults):471 if first:first = False472 else: self.write(", ")473 self.dispatch(a),474 if d:475 self.write("=")476 self.dispatch(d)477 # kwargs478 if t.kwarg:479 if first:first = False480 else: self.write(", ")481 self.write("**"+t.kwarg.arg)482 if t.kwarg.annotation:483 self.write(": ")484 self.dispatch(t.kwarg.annotation)485 def _keyword(self, t):486 if t.arg is None:487 self.write("**")488 else:489 self.write(t.arg)490 self.write("=")491 self.dispatch(t.value)492 def _Lambda(self, t):493 self.write("(")494 self.write("lambda ")495 self.dispatch(t.args)496 self.write(": ")497 self.dispatch(t.body)498 self.write(")")499 def _alias(self, t):500 self.write(t.name)501 if t.asname:502 self.write(" as "+t.asname)503 def _withitem(self, t):504 self.dispatch(t.context_expr)505 if t.optional_vars:506 self.write(" as ")507 self.dispatch(t.optional_vars)508def roundtrip(filename, output=sys.stdout):509 with open(filename, "rb") as pyfile:510 encoding = tokenize.detect_encoding(pyfile.readline)[0]511 with open(filename, "r", encoding=encoding) as pyfile:512 source = pyfile.read()513 tree = compile(source, filename, "exec", ast.PyCF_ONLY_AST)514 Unparser(tree, output)515def testdir(a):516 try:517 names = [n for n in os.listdir(a) if n.endswith('.py')]518 except OSError:519 print("Directory not readable: %s" % a, file=sys.stderr)520 else:521 for n in names:...

Full Screen

Full Screen

base.py

Source:base.py Github

copy

Full Screen

...137 dispatch_cls = type(138 "%sDispatch" % classname, (dispatch_base,), {"__slots__": event_names}139 )140 dispatch_cls._event_names = event_names141 dispatch_inst = cls._set_dispatch(cls, dispatch_cls)142 for k in dispatch_cls._event_names:143 setattr(dispatch_inst, k, _ClsLevelDispatch(cls, dict_[k]))144 _registrars[k].append(cls)145 for super_ in dispatch_cls.__bases__:146 if issubclass(super_, _Dispatch) and super_ is not _Dispatch:147 for ls in super_._events.dispatch._event_descriptors:148 setattr(dispatch_inst, ls.name, ls)149 dispatch_cls._event_names.append(ls.name)150 if getattr(cls, "_dispatch_target", None):151 cls._dispatch_target.dispatch = dispatcher(cls)152def _remove_dispatcher(cls):153 for k in cls.dispatch._event_names:154 _registrars[k].remove(cls)155 if not _registrars[k]:156 del _registrars[k]157class Events(util.with_metaclass(_EventMeta, object)):158 """Define event listening functions for a particular target type."""159 @staticmethod160 def _set_dispatch(cls, dispatch_cls):161 # this allows an Events subclass to define additional utility162 # methods made available to the target via163 # "self.dispatch._events.<utilitymethod>"164 # @staticemethod to allow easy "super" calls while in a metaclass165 # constructor.166 cls.dispatch = dispatch_cls(None)167 dispatch_cls._events = cls168 return cls.dispatch169 @classmethod170 def _accept_with(cls, target):171 # Mapper, ClassManager, Session override this to172 # also accept classes, scoped_sessions, sessionmakers, etc.173 if hasattr(target, "dispatch") and (174 isinstance(target.dispatch, cls.dispatch.__class__)...

Full Screen

Full Screen

dispatchinfo.py

Source:dispatchinfo.py Github

copy

Full Screen

1#!/usr/bin/env python2#3# Copyright 2007 Google Inc.4#5# Licensed under the Apache License, Version 2.0 (the "License");6# you may not use this file except in compliance with the License.7# You may obtain a copy of the License at8#9# http://www.apache.org/licenses/LICENSE-2.010#11# Unless required by applicable law or agreed to in writing, software12# distributed under the License is distributed on an "AS IS" BASIS,13# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.14# See the License for the specific language governing permissions and15# limitations under the License.16#17"""Dispatch configuration tools.18Library for parsing dispatch.yaml files and working with these in memory.19"""20import os21import re22if os.environ.get('APPENGINE_RUNTIME') == 'python27':23 from google.appengine.api import appinfo24 from google.appengine.api import validation25 from google.appengine.api import yaml_builder26 from google.appengine.api import yaml_listener27 from google.appengine.api import yaml_object28else:29 from google.appengine.api import appinfo30 from google.appengine.api import validation31 from google.appengine.api import yaml_builder32 from google.appengine.api import yaml_listener33 from google.appengine.api import yaml_object34_URL_SPLITTER_RE = re.compile(r'^([^/]+)(/.*)$')35_URL_HOST_EXACT_PATTERN_RE = re.compile(r"""36# 0 or more . terminated hostname segments (may not start or end in -).37^([a-z0-9]([a-z0-9\-]*[a-z0-9])*\.)*38# followed by a host name segment.39([a-z0-9]([a-z0-9\-]*[a-z0-9])*)$""", re.VERBOSE)40_URL_IP_V4_ADDR_RE = re.compile(r"""41#4 1-3 digit numbers separated by .42^([0-9]{1,3})\.([0-9]{1,3})\.([0-9]{1,3})\.([0-9]{1,3})$""", re.VERBOSE)43_URL_HOST_SUFFIX_PATTERN_RE = re.compile(r"""44# Single star or45^([*]|46# Host prefix with no ., Ex '*-a' or47[*][a-z0-9\-]*[a-z0-9]|48# Host prefix with ., Ex '*-a.b-c.d'49[*](\.|[a-z0-9\-]*[a-z0-9]\.)([a-z0-9]([a-z0-9\-]*[a-z0-9])*\.)*50([a-z0-9]([a-z0-9\-]*[a-z0-9])*))$51""", re.VERBOSE)52APPLICATION = 'application'53DISPATCH = 'dispatch'54URL = 'url'55MODULE = 'module'56SERVICE = 'service'57class Error(Exception):58 """Base class for errors in this module."""59class MalformedDispatchConfigurationError(Error):60 """Configuration file for dispatch is malformed."""61class DispatchEntryURLValidator(validation.Validator):62 """Validater for URL patterns."""63 def Validate(self, value, unused_key=None):64 """Validates an URL pattern."""65 if value is None:66 raise validation.MissingAttribute('url must be specified')67 if not isinstance(value, basestring):68 raise validation.ValidationError('url must be a string, not \'%r\'' %69 type(value))70 url_holder = ParsedURL(value)71 if url_holder.host_exact:72 _ValidateMatch(_URL_HOST_EXACT_PATTERN_RE, url_holder.host,73 'invalid host_pattern \'%s\'' % url_holder.host)74 _ValidateNotIpV4Address(url_holder.host)75 else:76 _ValidateMatch(_URL_HOST_SUFFIX_PATTERN_RE, url_holder.host_pattern,77 'invalid host_pattern \'%s\'' % url_holder.host_pattern)78 return value79class ParsedURL(object):80 """Dispath Entry URL holder class.81 Attributes:82 host_pattern: The host pattern component of the URL pattern.83 host_exact: True iff the host pattern does not start with a *.84 host: host_pattern with any leading * removed.85 path_pattern: The path pattern component of the URL pattern.86 path_exact: True iff path_pattern does not end with a *.87 path: path_pattern with any trailing * removed.88 """89 def __init__(self, url_pattern):90 """Initializes this ParsedURL with an URL pattern value.91 Args:92 url_pattern: An URL pattern that conforms to the regular expression93 '^([^/]+)(/.*)$'.94 Raises:95 validation.ValidationError: When url_pattern does not match the required96 regular expression.97 """98 split_matcher = _ValidateMatch(_URL_SPLITTER_RE, url_pattern,99 'invalid url \'%s\'' % url_pattern)100 self.host_pattern, self.path_pattern = split_matcher.groups()101 if self.host_pattern.startswith('*'):102 self.host_exact = False103 self.host = self.host_pattern[1:]104 else:105 self.host_exact = True106 self.host = self.host_pattern107 if self.path_pattern.endswith('*'):108 self.path_exact = False109 self.path = self.path_pattern[:-1]110 else:111 self.path_exact = True112 self.path = self.path_pattern113def _ValidateMatch(regex, value, message):114 """Validate value matches regex."""115 matcher = regex.match(value)116 if not matcher:117 raise validation.ValidationError(message)118 return matcher119def _ValidateNotIpV4Address(host):120 """Validate host is not an IPV4 address."""121 matcher = _URL_IP_V4_ADDR_RE.match(host)122 if matcher and sum(1 for x in matcher.groups() if int(x) <= 255) == 4:123 raise validation.ValidationError('Host may not match an ipv4 address \'%s\''124 % host)125 return matcher126class DispatchEntry(validation.Validated):127 """A Dispatch entry describes a mapping from a URL pattern to a module."""128 ATTRIBUTES = {129 URL: DispatchEntryURLValidator(),130 MODULE: validation.Optional(appinfo.MODULE_ID_RE_STRING),131 SERVICE: validation.Optional(appinfo.MODULE_ID_RE_STRING)132 }133class DispatchInfoExternal(validation.Validated):134 """Describes the format of a dispatch.yaml file."""135 ATTRIBUTES = {136 APPLICATION: validation.Optional(appinfo.APPLICATION_RE_STRING),137 DISPATCH: validation.Optional(validation.Repeated(DispatchEntry)),138 }139def LoadSingleDispatch(dispatch_info, open_fn=None):140 """Load a dispatch.yaml file or string and return a DispatchInfoExternal.141 Args:142 dispatch_info: The contents of a dispatch.yaml file as a string, or an open143 file object.144 open_fn: Function for opening files. Unused here, needed to provide145 a polymorphic API used by appcfg.py yaml parsing.146 Returns:147 A DispatchInfoExternal instance which represents the contents of the parsed148 yaml file.149 Raises:150 MalformedDispatchConfigurationError: The yaml file contains multiple151 dispatch sections or is missing a required value.152 yaml_errors.EventError: An error occured while parsing the yaml file.153 """154 builder = yaml_object.ObjectBuilder(DispatchInfoExternal)155 handler = yaml_builder.BuilderHandler(builder)156 listener = yaml_listener.EventListener(handler)157 listener.Parse(dispatch_info)158 parsed_yaml = handler.GetResults()159 if not parsed_yaml:160 return DispatchInfoExternal()161 if len(parsed_yaml) > 1:162 raise MalformedDispatchConfigurationError('Multiple dispatch: sections '163 'in configuration.')164 dispatch_info_external = parsed_yaml[0]165 for dispatch in getattr(dispatch_info_external, DISPATCH) or []:166 if dispatch.module and dispatch.service:167 raise MalformedDispatchConfigurationError(168 'Both module: and service: in dispatch entry. Please use only one.')169 if not (dispatch.module or dispatch.service):170 raise MalformedDispatchConfigurationError(171 "Missing required value 'service'.")172 dispatch.module = dispatch.module or dispatch.service173 dispatch.service = None...

Full Screen

Full Screen

test_dispatch.py

Source:test_dispatch.py Github

copy

Full Screen

...85 ModelCaseUUID, ModelCaseFloat, ModelCaseInt, ModelCaseDecimal86):87 ...88@pytest.fixture(scope="function")89def dispatch():90 @Dispatch91 def dispatch(m, *args, **kwargs):92 raise TypeError(f"Oh, No! {m.type_}, {m.outer_type_}")93 @dispatch.dispatcher94 def _(m: ModelField, *args, dispatch: Dispatch = None, **kwargs):95 if m.outer_type_ in dispatch._funcs:96 func = dispatch._funcs.get(m.outer_type_)97 return func(m, *args, **kwargs)98 meta_func = dispatch._funcs.get(m.outer_type_.__class__)99 if meta_func is None:100 return dispatch._base(m)101 else:102 func = meta_func(m, dispatch=dispatch)103 return func(m, *args, **kwargs)104 @dispatch.register(ConstrainedNumberMeta, JsonMeta, type)105 def _(m: ModelField, *args, dispatch: Dispatch = None, **kwargs):106 type_ = m.outer_type_107 if type_.__mro__[1] in dispatch._funcs:108 return dispatch._funcs[type_.__mro__[1]]109 yield dispatch110@pytest.mark.parametrize(111 "field",112 ModelCaseUUID.__fields__.values(),113 ids=ModelCaseUUID.__fields__.keys(),114)115def test_dispatch_uuid(field, dispatch):116 m = field117 @dispatch.register(uuid.UUID, UUID1, UUID3, UUID4, UUID5)118 def _(m):119 return "uuid"120 assert dispatch(m) == "uuid"121@pytest.mark.parametrize(122 "field",123 ModelCaseInt.__fields__.values(),124 ids=ModelCaseInt.__fields__.keys(),125)126def test_dispatch_int(field, dispatch):127 m = field128 @dispatch.register(129 int,130 PositiveInt,131 NegativeInt,132 NonPositiveInt,133 NonNegativeInt,134 StrictInt,135 ConstrainedInt,136 )137 def _(m):138 return "int"139 assert dispatch(m) == "int"140@pytest.mark.parametrize(141 "field",142 ModelCaseFloat.__fields__.values(),143 ids=ModelCaseFloat.__fields__.keys(),144)145def test_dispatch_float(field, dispatch):146 m = field147 @dispatch.register(148 float,149 ConstrainedFloat,150 PositiveFloat,151 NegativeFloat,152 NonPositiveFloat,153 NonNegativeFloat,154 StrictFloat,155 )156 def _(m):157 return "float"158 assert dispatch(m) == "float"159@pytest.mark.parametrize(160 "field",161 ModelCaseDecimal.__fields__.values(),162 ids=ModelCaseDecimal.__fields__.keys(),163)164def test_dispatch_decimal(field, dispatch):165 m = field166 @dispatch.register(167 Decimal,168 ConstrainedDecimal,169 )170 def _(m):171 return "decimal"172 assert dispatch(m) == "decimal"173@pytest.mark.parametrize(174 ("key", "field"),175 ModelCaseTypes.__fields__.items(),176 ids=ModelCaseTypes.__fields__.keys(),177)178def test_dispatch_types(field, key, dispatch):179 m = field180 @dispatch.register(uuid.UUID, UUID1, UUID3, UUID4, UUID5)181 def _(m):182 return "uuid"183 @dispatch.register(184 float,185 ConstrainedFloat,186 PositiveFloat,187 NegativeFloat,188 NonPositiveFloat,189 NonNegativeFloat,190 StrictFloat,191 )192 def _(m):193 return "float"194 @dispatch.register(195 int,196 PositiveInt,197 NegativeInt,198 NonPositiveInt,199 NonNegativeInt,200 StrictInt,201 ConstrainedInt,202 )203 def _(m):204 return "int"205 @dispatch.register(206 Decimal,207 ConstrainedDecimal,208 )209 def _(m):210 return "decimal"211 assert dispatch(m) in key212def test_wrong_type():213 class BaseWrong(BaseModel):214 test: Dict215 with pytest.raises(TypeError):...

Full Screen

Full Screen

policy_iteration.py

Source:policy_iteration.py Github

copy

Full Screen

1"""2MSIA 490 HW13Policy iteration algorithm4Sungsoo Lim - 10/19/205Problem6- One state is assumed that represents the number of remaining customers at the station7- Two actions are assumed that either a bus is dispatched or it is not at the given time and state8- Reward is assumed to be negative, as there are only costs involved9- Assume that a bus can be dispatched below its capacity10"""11import numpy as np12import matplotlib.pyplot as plt 13NO_DISPATCH = 0 #not dispatching a bus is set as 014DISPATCH = 1 #dispatching a bus is set as 115def policy_iteration(policy, theta, discount_factor):16 nA = 2 #number of actions - dispatch a bus or not17 K = 15 #capacity of a shuttle if dispatched18 c_f = 100 #cost of dispatching a shuttle19 c_h = 2 #cost of waiting per customer20 P = {} #transition probability and reward tuple for each action21 P = {a: [] for a in range(nA)}22 23 s = np.random.randint(1,6) #initial number of customers24 def expected_value(policy,P,V):25 """26 Calculate the expected value function for all actions in the given policy27 Assume equal action probability28 """29 v = 030 for a, action_prob in enumerate(policy):31 for prob, reward in P[a]:32 v += action_prob*prob*(reward + discount_factor * V)33 return v34 #Initialize vectors for function output35 V = [0,0] #value36 pol = [0] #policy37 custm =[0] #total number of customers at each time point38 i = 1 #iterative index39 while True:40 #stopping condition for iteration41 delta = 0 42 cust = np.random.randint(1,6) #uniform random increasing customers43 #next state for dispatching a shuttle44 if K > s: #can dispatch a shuttle not at maximum capacity in the beginning45 next_cust_dispatch = cust46 else:47 next_cust_dispatch = (s-K+cust)48 next_cust_no_dispatch = s+cust #next state for not dispatching a shuttle49 reward_no_dispatch = -next_cust_no_dispatch*c_h #reward for not dispatching50 reward_dispatch = -next_cust_dispatch*c_h - c_f #reward for dispatching51 52 #prob, reward for not/dispatching53 P[NO_DISPATCH] = [(1.0, reward_no_dispatch)]54 P[DISPATCH] = [(1.0, reward_dispatch)]55 #the capacity capped at 200 customers56 if next_cust_no_dispatch >= 200: #always dispatch57 P[NO_DISPATCH] = P[DISPATCH]58 #First calculate the expected value function based on the next state59 v = expected_value(policy,P,V[i-1])60 delta = max(delta, np.abs(v - V[i]))61 #random uniformly update the state (number of customers)62 if s >= 200: #always dispatch63 s = next_cust_dispatch64 best_action = 165 if np.random.uniform(0,1.0) <=0.50:66 s = next_cust_no_dispatch67 best_action = 068 else:69 s = next_cust_dispatch70 best_action = 171 i = i + 172 V.append(v)73 pol.append(best_action)74 custm.append(s)75 if delta < theta:76 break77 return custm,pol,V78def policy_improvement(discount_factor, theta, policy_iteration_fn=policy_iteration):79 """80 Greedily improve the random policy81 """82 def plus_one(nA,P,V):83 """84 Calculate the value function for all action in a given state85 """86 A = np.zeros(nA) #either dispatch or not dispatch87 for a in range(nA):88 for prob, reward in P[a]:89 A[a] += prob*(reward + discount_factor * V)90 return A91 policy = [0.5, 0.5] #random policy92 nA = 2 #two actions - dispatch or not dispatch a bus93 P = {} #transition probability and reward tuple for each action94 P = {a: [] for a in range(nA)}95 c_f = 100 #cost of dispatching a shuttle96 c_h = 2 #cost of waiting per customer97 K = 15 #capacity98 while True:99 100 custm, pol, V = policy_iteration_fn(policy,theta,discount_factor)101 102 del V[0] #delete the first unnecessary element from V103 policy_stable = True104 for i in range(len(V)-1,-1,-1): #for each state105 106 chosen_action = pol[i]107 108 reward_no_dispatch = -custm[i]*c_h #reward for not dispatching109 reward_dispatch = -(custm[i]-K)*c_h - c_f #reward for dispatching110 111 #prob, reward for not/dispatching112 P[NO_DISPATCH] = [(1.0, reward_no_dispatch)]113 P[DISPATCH] = [(1.0, reward_dispatch)] 114 A = plus_one(nA,P,V[i-1])115 best_action = np.argmax(A)116 if chosen_action != best_action:117 policy_stable = False118 policy = np.eye(nA)[best_action].tolist()119 if policy_stable:120 return custm, pol, V121custm, pol, V = policy_improvement(0.95,0.0001)122plt.scatter(custm,pol)123plt.title("Policy iteration")124plt.xlabel('Number of people')125plt.ylabel('Optimal Policy')...

Full Screen

Full Screen

dispatch_xml_parser.py

Source:dispatch_xml_parser.py Github

copy

Full Screen

1#!/usr/bin/env python2#3# Copyright 2007 Google Inc.4#5# Licensed under the Apache License, Version 2.0 (the "License");6# you may not use this file except in compliance with the License.7# You may obtain a copy of the License at8#9# http://www.apache.org/licenses/LICENSE-2.010#11# Unless required by applicable law or agreed to in writing, software12# distributed under the License is distributed on an "AS IS" BASIS,13# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.14# See the License for the specific language governing permissions and15# limitations under the License.16#17"""Directly processes text of dispatch.xml.18DispatchXmlParser is called with an XML string to produce a list of19DispatchEntry objects containing the data from the XML.20"""21from xml.etree import ElementTree22from google.appengine.tools import xml_parser_utils23from google.appengine.tools.app_engine_config_exception import AppEngineConfigException24MISSING_URL = '<dispatch> node must contain a <url>'25MISSING_MODULE = '<dispatch> node must contain a <module>'26def GetDispatchYaml(application, dispatch_xml_str):27 return _MakeDispatchListIntoYaml(28 application, DispatchXmlParser().ProcessXml(dispatch_xml_str))29def _MakeDispatchListIntoYaml(application, dispatch_list):30 """Converts list of DispatchEntry objects into a YAML string."""31 statements = [32 'application: %s' % application,33 'dispatch:',34 ]35 for entry in dispatch_list:36 statements += entry.ToYaml()37 return '\n'.join(statements) + '\n'38class DispatchXmlParser(object):39 """Provides logic for walking down XML tree and pulling data."""40 def ProcessXml(self, xml_str):41 """Parses XML string and returns object representation of relevant info.42 Args:43 xml_str: The XML string.44 Returns:45 A list of DispatchEntry objects defining how URLs are dispatched to46 modules.47 Raises:48 AppEngineConfigException: In case of malformed XML or illegal inputs.49 """50 try:51 self.dispatch_entries = []52 self.errors = []53 xml_root = ElementTree.fromstring(xml_str)54 if xml_root.tag != 'dispatch-entries':55 raise AppEngineConfigException('Root tag must be <dispatch-entries>')56 for child in xml_root.getchildren():57 self.ProcessDispatchNode(child)58 if self.errors:59 raise AppEngineConfigException('\n'.join(self.errors))60 return self.dispatch_entries61 except ElementTree.ParseError:62 raise AppEngineConfigException('Bad input -- not valid XML')63 def ProcessDispatchNode(self, node):64 """Processes XML <dispatch> nodes into DispatchEntry objects.65 The following information is parsed out:66 url: The URL or URL pattern to route.67 module: The module to route it to.68 If there are no errors, the data is loaded into a DispatchEntry object69 and added to a list. Upon error, a description of the error is added to70 a list and the method terminates.71 Args:72 node: <dispatch> XML node in dos.xml.73 """74 tag = xml_parser_utils.GetTag(node)75 if tag != 'dispatch':76 self.errors.append('Unrecognized node: <%s>' % tag)77 return78 entry = DispatchEntry()79 entry.url = xml_parser_utils.GetChildNodeText(node, 'url')80 entry.module = xml_parser_utils.GetChildNodeText(node, 'module')81 validation = self._ValidateEntry(entry)82 if validation:83 self.errors.append(validation)84 return85 self.dispatch_entries.append(entry)86 def _ValidateEntry(self, entry):87 if not entry.url:88 return MISSING_URL89 if not entry.module:90 return MISSING_MODULE91class DispatchEntry(object):92 """Instances contain information about individual dispatch entries."""93 def ToYaml(self):94 return [95 "- url: '%s'" % self._SanitizeForYaml(self.url),96 ' module: %s' % self.module,97 ]98 def _SanitizeForYaml(self, dirty_str):...

Full Screen

Full Screen

setup.py

Source:setup.py Github

copy

Full Screen

1#!/usr/bin/env python2# coding: utf83from setuptools import setup4setup(5 name="sacsv",6 version="1.5",7 description=u"Swiss Army csv: command-line tools to manipulate csv-formatted data",8 author=u"Gabor Nyeki",9 url="http://www.gabornyeki.com/",10 packages=["sacsv"],11 install_requires=["argh"],12 python_requires=">=3.5",13 provides=["sacsv (1.5)"],14 entry_points={15 "console_scripts": [16 "csv2jsonl = sacsv.csv2jsonl:dispatch",17 "csvaddrandom = sacsv.csvaddrandom:dispatch",18 "csvadduniqueid = sacsv.csvadduniqueid:dispatch",19 "csvaggregate = sacsv.csvaggregate:dispatch",20 "csvappend = sacsv.csvappend:dispatch",21 "csvdropdups = sacsv.csvdropdups:dispatch",22 "csvfindsortkey = sacsv.csvfindsortkey:dispatch",23 "csvkeepmax = sacsv.csvkeepmax:dispatch",24 "csvleftjoin = sacsv.csvleftjoin:dispatch",25 "csvop = sacsv.csvop:dispatch",26 "csvparallel = sacsv.csvparallel:dispatch",27 "csvrename = sacsv.csvrename:dispatch",28 "csvreorder = sacsv.csvreorder:dispatch",29 "csvsed = sacsv.csvsed:dispatch",30 "csvsort = sacsv.csvsort:dispatch",31 "csvtranspose = sacsv.csvtranspose:dispatch",32 "fw2csv = sacsv.fw2csv:dispatch",33 "longcsv2wide = sacsv.longcsv2wide:dispatch",34 "widecsv2long = sacsv.widecsv2long:dispatch",35 ],36 }...

Full Screen

Full Screen

Using AI Code Generation

copy

Full Screen

1var ladle = require('ladle');2var dispatch = ladle.dispatch;3var get = ladle.get;4var post = ladle.post;5var put = ladle.put;6var del = ladle.del;7var patch = ladle.patch;8var app = ladle();9app.use(function(req, res, next) {10 res.setHeader("Access-Control-Allow-Origin", "*");11 res.setHeader("Access-Control-Allow-Headers", "Origin, X-Requested-With, Content-Type, Accept");12 next();13});14app.get('/getdata', function(req, res) {15 var data = {16 };17 res.json(data);18});19app.post('/postdata', function(req, res) {20 var data = {21 };22 res.json(data);23});24app.put('/putdata', function(req, res) {25 var data = {26 };27 res.json(data);28});29app.delete('/deletedata', function(req, res) {30 var data = {31 };32 res.json(data);33});34app.patch('/patchdata', function(req, res) {35 var data = {36 };37 res.json(data);38});39app.listen(3000);40console.log("Server is running at port 3000");

Full Screen

Using AI Code Generation

copy

Full Screen

1var ladle = require('ladle');2var ladleDispatch = ladle.dispatch;3var ladleDispatch = ladle.dispatch;4var ladleDispatch = ladle.dispatch;5var ladle = require('ladle');6var ladleDispatch = ladle.dispatch;7var ladle = require('ladle');8var ladleDispatch = ladle.dispatch;9var ladleDispatch = ladle.dispatch;10var ladleDispatch = ladle.dispatch;11var ladle = require('ladle');12var ladleDispatch = ladle.dispatch;13var ladle = require('ladle');14var ladleDispatch = ladle.dispatch;15var ladleDispatch = ladle.dispatch;16var ladleDispatch = ladle.dispatch;17var ladle = require('ladle');18var ladleDispatch = ladle.dispatch;19var ladle = require('ladle');20var ladleDispatch = ladle.dispatch;21var ladleDispatch = ladle.dispatch;22var ladleDispatch = ladle.dispatch;23var ladle = require('ladle');24var ladleDispatch = ladle.dispatch;25var ladle = require('ladle');26var ladleDispatch = ladle.dispatch;27var ladleDispatch = ladle.dispatch;28var ladleDispatch = ladle.dispatch;29var ladle = require('ladle');30var ladleDispatch = ladle.dispatch;31var ladle = require('ladle');32var ladleDispatch = ladle.dispatch;33var ladleDispatch = ladle.dispatch;34var ladleDispatch = ladle.dispatch;35var ladle = require('ladle');36var ladleDispatch = ladle.dispatch;37var ladle = require('ladle');38var ladleDispatch = ladle.dispatch;39var ladleDispatch = ladle.dispatch;40var ladleDispatch = ladle.dispatch;41var ladle = require('ladle

Full Screen

Using AI Code Generation

copy

Full Screen

1var ladle = require('ladle');2var dispatch = ladle.dispatch;3var app = ladle.app;4var server = ladle.server;5var route = ladle.route;6var response = ladle.response;7var request = ladle.request;8var controller = ladle.controller;9var myController = controller.create({10 index: function (req, res) {11 res.send('Hello World!');12 }13});14app.use(server.create('/', myController.index));15dispatch(app);16var ladle = require('ladle');17var dispatch = ladle.dispatch;18var app = ladle.app;19var server = ladle.server;20var route = ladle.route;21var response = ladle.response;22var request = ladle.request;23var controller = ladle.controller;24var myController = controller.create({25 index: function (req, res) {26 res.send('Hello World!');27 }28});29app.use(server.create('/', myController.index));30dispatch(app);31var ladle = require('ladle');32var dispatch = ladle.dispatch;33var app = ladle.app;34var server = ladle.server;35var route = ladle.route;36var response = ladle.response;37var request = ladle.request;38var controller = ladle.controller;39var myController = controller.create({40 index: function (req, res) {41 res.send('Hello World!');42 }43});44app.use(server.create('/', myController.index));45dispatch(app);46var ladle = require('ladle');47var dispatch = ladle.dispatch;48var app = ladle.app;49var server = ladle.server;50var route = ladle.route;51var response = ladle.response;52var request = ladle.request;53var controller = ladle.controller;54var myController = controller.create({55 index: function (req, res) {56 res.send('Hello World!');57 }58});59app.use(server.create('/', myController.index));60dispatch(app);61var ladle = require('ladle');62var dispatch = ladle.dispatch;63var app = ladle.app;64var server = ladle.server;65var route = ladle.route;66var response = ladle.response;

Full Screen

Using AI Code Generation

copy

Full Screen

1var ladle = require('ladle');2var ladleDispatch = ladle.dispatch;3ladleDispatch('test', 'test', {test: 'test'});4var ladle = require('ladle');5ladle.dispatch('test', 'test', {test: 'test'});6var ladle = require('ladle');7var ladleDispatch = ladle.dispatch;8ladleDispatch('test', 'test', {test: 'test'});9var ladle = require('ladle');10ladle.dispatch('test', 'test', {test: 'test'});11var ladle = require('ladle');12var ladleDispatch = ladle.dispatch;13ladleDispatch('test', 'test', {test: 'test'});14var ladle = require('ladle');15ladle.dispatch('test', 'test', {test: 'test'});16var ladle = require('ladle');17var ladleDispatch = ladle.dispatch;18ladleDispatch('test', 'test', {test: 'test'});19var ladle = require('ladle');20ladle.dispatch('test', 'test', {test: 'test'});21var ladle = require('ladle');22var ladleDispatch = ladle.dispatch;23ladleDispatch('test', 'test', {test: 'test'});24var ladle = require('ladle');25ladle.dispatch('test', 'test', {test: 'test'});26var ladle = require('ladle');27var ladleDispatch = ladle.dispatch;28ladleDispatch('test', 'test', {test: 'test'});

Full Screen

Using AI Code Generation

copy

Full Screen

1var ladle = require('ladle');2var ladleDispatch = ladle.dispatch;3ladleDispatch('someEvent', 'someData');4ladleDispatch('someEvent', 'someData', function(err, data) {5 if(err) {6 } else {7 }8});9ladleDispatch('someEvent', 'someData', function(err, data) {10 if(err) {11 } else {12 }13}, this);14ladleDispatch('someEvent', 'someData', this);15ladleDispatch('someEvent', 'someData', this, function(err, data) {16 if(err) {17 } else {18 }19});20ladleDispatch('someEvent', 'someData', this, function(err, data) {21 if(err) {22 } else {23 }24});25ladleDispatch('someEvent', 'someData', this, function(err, data) {26 if(err) {27 } else {28 }29}, this);30ladleDispatch('someEvent', 'someData', this, function(err, data) {31 if(err) {32 } else {33 }34}, this);35ladleDispatch('someEvent', 'someData', this, function(err, data) {36 if(err) {37 } else {38 }39}, this);40ladleDispatch('someEvent', 'someData', this, function(err, data) {41 if(err) {42 } else {43 }44}, this);

Full Screen

Using AI Code Generation

copy

Full Screen

1var ladle = require('ladle');2var dispatch = ladle.dispatch;3function add(a, b) {4 return a + b;5}6dispatch(add, 3, 4);7function multiply(a, b) {8 return a * b;9}10dispatch(multiply, 2, 3);11function subtract(a, b) {12 return a - b;13}14dispatch(subtract, 8, 7);15function divide(a, b) {16 return a / b;17}18dispatch(divide, 10, 5);19function hello() {20 return 'hello';21}22dispatch(hello);23function bye() {24 return 'bye';25}26dispatch(bye);27function hi() {28 return 'hi';29}30dispatch(hi);31function howdy() {32 return 'howdy';33}34dispatch(howdy);35function hey() {36 return 'hey';37}38dispatch(hey);39function helloWorld() {40 return 'hello world';41}42dispatch(helloWorld);43function helloUniverse() {44 return 'hello universe';45}46dispatch(helloUniverse);47function helloPlanet() {48 return 'hello planet';49}50dispatch(helloPlanet);51function helloMars() {52 return 'hello mars';53}54dispatch(helloMars);55function helloJupiter() {56 return 'hello jupiter';57}58dispatch(helloJ

Full Screen

Using AI Code Generation

copy

Full Screen

1var ladle = require('ladle');2var dispatch = ladle.dispatch;3var action = {4 args: {5 }6};7dispatch(action);8var ladle = require('ladle');9var dispatch = ladle.dispatch;10var action = {11 args: {12 }13};14dispatch(action);15var Q = require('q');16var test = function (action) {17 var deferred = Q.defer();18 deferred.resolve({19 });20 return deferred.promise;21};22module.exports = test;23var Q = require('q');24var test = function (data) {25 var deferred = Q.defer();26 deferred.resolve({27 });28 return deferred.promise;29};30module.exports = test;31var Q = require('q');32var test = function (data) {33 var deferred = Q.defer();34 deferred.resolve({35 });36 return deferred.promise;37};

Full Screen

Automation Testing Tutorials

Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.

LambdaTest Learning Hubs:

YouTube

You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.

Run ladle automation tests on LambdaTest cloud grid

Perform automation testing on 3000+ real desktop and mobile devices online.

Try LambdaTest Now !!

Get 100 minutes of automation test minutes FREE!!

Next-Gen App & Browser Testing Cloud

Was this article helpful?

Helpful

NotHelpful