Best Python code snippet using ATX
svg.py
Source:svg.py  
1#!/usr/local/bin/python2# -*- coding: utf-8 -*-3# file: svg.py4from . main import helper5from math import pi, sin, cos6def svg(*args, **kw):7    """8    Use in this way:9    10    ´from remarkuple import helper as h, svg´11    12    """13    class svg(type(helper.svg())):14        15        def __init__(self, *args, **kw):16            super(self.__class__, self).__init__(*args, **kw)17            # cartesian grid on/off18            self.__dict__['grid'] = False19            # axes on/off20            self.__dict__['axes'] = False21            # show origin22            self.__dict__['origin'] = False23            # grid aspects24            self.__dict__['grid_aspects'] = (4, 8)25            # x26            self.__dict__['x'] = 20027            # y28            self.__dict__['y'] = 20029            # elements30            self.__dict__['elements'] = []31            # size of the svg canvas, width and height32            self.__dict__['size'] = (100, 100)33            # grid id34            self.__dict__['grid_id'] = "gid%s" % id(self)35            # grid item id36            self.__dict__['grid_item_id'] = "gitem%s" % id(self)37            # grid width38            width = kw['width'] if kw.has_key('width') else self.__dict__['x']*239            # grid height40            height = kw['height'] if kw.has_key('height') else self.__dict__['y']*241            self.set_size(width, height)42        43        def set_grid(self, grid = True):44            self.__dict__['grid'] = grid45            return self46        47        def set_axes(self, axes = True):48            self.__dict__['axes'] = axes49            return self50        51        def set_origin(self, origin = True):52            self.__dict__['origin'] = origin53            return self54        55        def set_size(self, width, height):56            self.__dict__['x'] = width/257            self.__dict__['y'] = height/258            self.__dict__['size'] = (width, height)59            self.width = width60            self.height = height61            self.viewbox = "0 0 %s %s" % (width+1, height+1)62            return self63        64        def set_text(self, *args, **kw):65            kw['x'] = self.__dict__['x']+kw['x'] if kw.has_key('x') else self.__dict__['x'] 66            kw['y'] = self.__dict__['y']-kw['y'] if kw.has_key('y') else self.__dict__['y']67            self.__dict__['elements'].append(helper.text(*args, **kw))68            return self69        70        def set_rectangle(self, *args, **kw):71            kw['x'] = self.__dict__['x']+kw['x'] if kw.has_key('x') else self.__dict__['x']72            kw['y'] = self.__dict__['y']-kw['y'] if kw.has_key('y') else self.__dict__['y']73            self.__dict__['elements'].append(helper.rect(*args, **kw))74            return self75        76        def set_group(self, *args, **kw):77            self.__dict__['elements'].append(helper.g(*args, **kw))78            return self79        80        def set_defs(self, *args, **kw):81            self.__dict__['elements'].append(helper.defs(*args, **kw))82            return self83        84        def set_line(self, *args, **kw):85            kw['x1'] = self.__dict__['x']+kw['x1'] if kw.has_key('x1') else self.__dict__['x']86            kw['y1'] = self.__dict__['y']-kw['y1'] if kw.has_key('y1') else self.__dict__['y']87            kw['x2'] = self.__dict__['x']+kw['x2'] if kw.has_key('x2') else self.__dict__['x'] + 1088            kw['y2'] = self.__dict__['y']-kw['y2'] if kw.has_key('y2') else self.__dict__['y'] + 1089            self.__dict__['elements'].append(helper.line(*args, **kw))90            return self91        92        def set_circle(self, *args, **kw):93            kw['cx'] = self.__dict__['x']+kw['cx'] if kw.has_key('cx') else self.__dict__['x']94            kw['cy'] = self.__dict__['y']-kw['cy'] if kw.has_key('cy') else self.__dict__['y']95            self.__dict__['elements'].append(helper.circle(*args, **kw))96            return self97        98        def set_triangle(self, *args, **kw):99            kw['sides'] = 3100            return self.set_regular_polygon(*args, **kw)101        102        def set_square(self, *args, **kw):103            kw['sides'] = 4104            return self.set_regular_polygon(*args, **kw)105        106        def set_pentagon(self, *args, **kw):107            kw['sides'] = 5108            return self.set_regular_polygon(*args, **kw)109        110        def set_hexagon(self, *args, **kw):111            kw['sides'] = 6112            return self.set_regular_polygon(*args, **kw)113        114        def set_regular_polygon(self, *args, **kw):115            # set default properties for vertex116            kw['cx'] = kw['cx']+self.__dict__['x'] if kw.has_key('cx') else self.__dict__['x']117            kw['cy'] = kw['cy']-self.__dict__['y'] if kw.has_key('cy') else self.__dict__['y']118            kw['radius'] = kw['radius'] if kw.has_key('radius') else 1119            kw['sides'] = kw['sides'] if kw.has_key('sides') else 4120            kw['degrees'] = kw['degrees'] if kw.has_key('degrees') else 0121            # set generate polygon points122            points = self.polygon_points(self.vertex(cx=kw['cx'],123                                                     cy=kw['cy'],124                                                     radius=kw['radius'],125                                                     sides=kw['sides'],126                                                     degrees=kw['degrees']))127            # delete default properties128            del kw['cx']129            del kw['cy']130            del kw['radius']131            del kw['sides']132            del kw['degrees']133            # create polygon with given points and keywords134            self.__dict__['elements'].append(helper.polygon(points=points, *args, **kw))135            return self136        137        def polygon_points(self, vertex):138            return ' '.join(['%s,%s' % (point[0], point[1]) for point in vertex])139        140        def vertex(self, cx = 0, cy = 0, sides = 4, radius = 1, degrees = 0):141            centerAng = 2*pi/sides142            startAng = pi*degrees/180143            points = []144            for i in range(0, sides):145                ang = startAng + (i*centerAng);146                x = round(cx + radius*cos(ang), 2)147                y = round(cy - radius*sin(ang), 2)148                points.append([x,y])149            return points150        151        def _set_grid(self):152            """ Set up background grid for drawing canvas """153            grid_sizex = self.__dict__['size'][0]/self.__dict__['grid_aspects'][0]154            grid_sizey = self.__dict__['size'][1]/self.__dict__['grid_aspects'][0]155            156            sizex = grid_sizex*(1.0/self.__dict__['grid_aspects'][1])157            sizey = grid_sizey*(1.0/self.__dict__['grid_aspects'][1])158            159            defs = helper.defs()160            defs += helper.pattern(helper.path(**{'d': 'M %s 0 L 0 0 0 %s' % (sizex, sizey), 'fill': 'none', 'stroke': 'gray', 'stroke-width': 0.5}),161                                   **{'id': self.__dict__['grid_item_id'], 'width': sizex, 'height': sizey, 'patternUnits': 'userSpaceOnUse'})162            defs += helper.pattern(helper.path(**{'d': 'M %s 0 L 0 0 0 %s' % (grid_sizex, grid_sizey), 'fill': 'none', 'stroke': 'gray', 'stroke-width': 1}),163                                   helper.rect(width=grid_sizex, height=grid_sizey, fill="url(#%s)" % self.__dict__['grid_item_id']),164                                   **{'id': self.__dict__['grid_id'], 'width': grid_sizex, 'height': grid_sizey, 'patternUnits': 'userSpaceOnUse'})165            self += defs166            self += helper.rect(fill="white", height=self.__dict__['size'][0]+1, width=self.__dict__['size'][1]+1)167            self += helper.rect(fill="url(#%s)" % self.__dict__['grid_id'], height=self.__dict__['size'][0]+1, width=self.__dict__['size'][1]+1)168            169        def _set_axes(self):170            """ Set up x and y axis for drawing canvas """171            self += helper.line(stroke="black", x1=self.__dict__['x'], x2=self.__dict__['x'], y1=0, y2=self.__dict__['y']*2)172            self += helper.line(stroke="black", x1=0, x2=self.__dict__['x']*2, y1=self.__dict__['y'], y2=self.__dict__['y'])173        174        def _set_origin(self):175            """ Set up origin dot and x/y coordinates for drawing canvas """176            self += helper.circle(cx=self.__dict__['x'], cy=self.__dict__['y'], r=2, fill="black", stroke="black", style="fill-opacity: 50%")177            self += helper.text("(0,0)", x=self.__dict__['x']+5, y=self.__dict__['y']-5, style="fill-opacity: 50%")178        179        def __str__(self):180            if self.__dict__['grid']:181                self._set_grid()182            if self.__dict__['axes']:183                self._set_axes()184            if self.__dict__['origin']:185                self._set_origin()186            if self.__dict__['elements']:187                for element in self.__dict__['elements']:188                    self += element189            return super(self.__class__, self).__str__()190    ...test_slots_jy.py
Source:test_slots_jy.py  
1"""Slot tests2Made for Jython.3"""4from test import test_support5import unittest6# The strict tests fail on PyPy (but work on CPython and Jython).7# They're questionable8strict = True9class SlottedTestCase(unittest.TestCase):10    def test_slotted(self):11        class Foo(object):12            __slots__ = 'bar'13        self.assert_('__dict__' not in Foo.__dict__)14        foo = Foo()15        self.assert_(not hasattr(foo, '__dict__'))16        foo.bar = 'hello bar'17        self.assertEqual(foo.bar, 'hello bar')18        self.assertRaises(AttributeError, setattr, foo, 'foo', 'hello foo')19        class Baz(object):20            __slots__ = ['python', 'jython']21        self.assert_('__dict__' not in Baz.__dict__)22        baz = Baz()23        self.assert_(not hasattr(baz, '__dict__'))24        baz.python = 'hello python'25        baz.jython = 'hello jython'26        self.assertEqual(baz.python, 'hello python')27        self.assertEqual(baz.jython, 'hello jython')28        self.assertRaises(AttributeError, setattr, baz, 'foo', 'hello')29class SlottedWithDictTestCase(unittest.TestCase):30    def test_subclass(self):31        class Base(object):32            pass33        class Foo(Base):34            __slots__ = 'bar'35        self.assert_('__dict__' not in Foo.__dict__)36        foo = Foo()37        self.assert_(hasattr(foo, '__dict__'))38        foo.bar = 'hello bar'39        foo.foo = 'hello foo'40        self.assertEqual(foo.bar, 'hello bar')41        self.assertEqual(foo.__dict__, {'foo': 'hello foo'})42    def test_subclass_mro(self):43        class Base(object):44            pass45        class Slotted(object):46            __slots__ = 'baz'47        class Foo(Slotted, Base):48            __slots__ = 'bar'49        if strict:50            self.assert_('__dict__' in Foo.__dict__)51            self.assertEqual(Foo.__dict__['__dict__'].__objclass__, Foo)52        foo = Foo()53        self.assert_(hasattr(foo, '__dict__'))54        foo.bar = 'hello bar'55        foo.baz = 'hello baz'56        foo.foo = 'hello foo'57        self.assertEqual(foo.bar, 'hello bar')58        self.assertEqual(foo.baz, 'hello baz')59        self.assertEqual(foo.__dict__, {'foo': 'hello foo'})60        class Bar(Slotted, Base):61            pass62        if strict:63            self.assert_('__dict__' in Bar.__dict__)64            self.assertEqual(Bar.__dict__['__dict__'].__objclass__, Bar)65        bar = Bar()66        self.assert_(hasattr(bar, '__dict__'))67        bar.bar = 'hello bar'68        bar.baz = 'hello baz'69        bar.foo = 'hello foo'70        self.assertEqual(bar.bar, 'hello bar')71        self.assertEqual(bar.baz, 'hello baz')72        self.assertEqual(bar.__dict__, {'foo': 'hello foo', 'bar': 'hello bar'})73    def test_subclass_oldstyle(self):74        class OldBase:75            pass76        class Foo(OldBase, object):77            __slots__ = 'bar'78        if strict:79            self.assert_('__dict__' in Foo.__dict__)80            self.assertEqual(Foo.__dict__['__dict__'].__objclass__, Foo)81        foo = Foo()82        self.assert_(hasattr(foo, '__dict__'))83        foo.bar = 'hello bar'84        foo.foo = 'hello foo'85        self.assertEqual(foo.bar, 'hello bar')86        self.assertEqual(foo.__dict__, {'foo': 'hello foo'})87        class Bar(OldBase, object):88            __slots__ = '__dict__'89        self.assert_('__dict__' in Bar.__dict__)90        self.assertEqual(Bar.__dict__['__dict__'].__objclass__, Bar)91        bar = Bar()92        self.assert_(hasattr(bar, '__dict__'))93        bar.bar = 'hello bar'94        bar.foo = 'hello foo'95        self.assertEqual(bar.bar, 'hello bar')96        self.assertEqual(bar.__dict__, {'foo': 'hello foo', 'bar': 'hello bar'})97    def test_mixin_oldstyle(self):98        class OldBase:99            pass100        class NewBase(object):101            pass102        class Baz(NewBase, OldBase):103            __slots__ = 'baz'104        self.assert_('__dict__' not in Baz.__dict__)105        baz = Baz()106        self.assert_(hasattr(baz, '__dict__'))107        baz.baz = 'hello baz'108        baz.bar = 'hello bar'109        self.assertEqual(baz.baz, 'hello baz')110        self.assertEqual(baz.bar, 'hello bar')111        self.assertEqual(baz.__dict__, {'bar': 'hello bar'})112class SlottedWithWeakrefTestCase(unittest.TestCase):113    def test_subclass_oldstyle(self):114        class OldBase:115            pass116        class Foo(OldBase, object):117            __slots__ = '__dict__'118        self.assert_(hasattr(Foo, '__weakref__'))119def test_main():120    test_support.run_unittest(SlottedTestCase,121                              SlottedWithDictTestCase,122                              SlottedWithWeakrefTestCase)123if __name__ == '__main__':...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.
You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.
Get 100 minutes of automation test minutes FREE!!
