Best Python code snippet using avocado_python
test_nbconvert_handlers.py
Source: test_nbconvert_handlers.py
...12 new_notebook, new_markdown_cell, new_code_cell, new_output,13)14from ipython_genutils.testing.decorators import onlyif_cmds_exist15from base64 import encodebytes16def cmd_exists(cmd):17 """Check is a command exists."""18 if shutil.which(cmd) is None:19 return False20 return True21class NbconvertAPI(object):22 """Wrapper for nbconvert API calls."""23 def __init__(self, request):24 self.request = request25 def _req(self, verb, path, body=None, params=None):26 response = self.request(verb,27 url_path_join('nbconvert', path),28 data=body, params=params,29 )30 response.raise_for_status()31 return response32 def from_file(self, format, path, name, download=False):33 return self._req('GET', url_path_join(format, path, name),34 params={'download':download})35 def from_post(self, format, nbmodel):36 body = json.dumps(nbmodel)37 return self._req('POST', format, body)38 def list_formats(self):39 return self._req('GET', '')40png_green_pixel = encodebytes(b'\x89PNG\r\n\x1a\n\x00\x00\x00\rIHDR\x00'41b'\x00\x00\x01\x00\x00x00\x01\x08\x02\x00\x00\x00\x90wS\xde\x00\x00\x00\x0cIDAT'42b'\x08\xd7c\x90\xfb\xcf\x00\x00\x02\\\x01\x1e.~d\x87\x00\x00\x00\x00IEND\xaeB`\x82'43).decode('ascii')44class APITest(NotebookTestBase):45 def setUp(self):46 nbdir = self.notebook_dir47 if not os.path.isdir(pjoin(nbdir, 'foo')):48 subdir = pjoin(nbdir, 'foo')49 os.mkdir(subdir)50 # Make sure that we clean this up when we're done.51 # By using addCleanup this will happen correctly even if we fail52 # later in setUp.53 @self.addCleanup54 def cleanup_dir():55 shutil.rmtree(subdir, ignore_errors=True)56 nb = new_notebook()57 nb.cells.append(new_markdown_cell(u'Created by test ³'))58 cc1 = new_code_cell(source=u'print(2*6)')59 cc1.outputs.append(new_output(output_type="stream", text=u'12'))60 cc1.outputs.append(new_output(output_type="execute_result",61 data={'image/png' : png_green_pixel},62 execution_count=1,63 ))64 nb.cells.append(cc1)65 with io.open(pjoin(nbdir, 'foo', 'testnb.ipynb'), 'w',66 encoding='utf-8') as f:67 write(nb, f, version=4)68 self.nbconvert_api = NbconvertAPI(self.request)69 @pytest.mark.skipif(70 not cmd_exists('pandoc'),71 reason="Pandoc wasn't found. Skipping this test."72 )73 def test_from_file(self):74 r = self.nbconvert_api.from_file('html', 'foo', 'testnb.ipynb')75 self.assertEqual(r.status_code, 200)76 self.assertIn(u'text/html', r.headers['Content-Type'])77 self.assertIn(u'Created by test', r.text)78 self.assertIn(u'print', r.text)79 r = self.nbconvert_api.from_file('python', 'foo', 'testnb.ipynb')80 self.assertIn(u'text/x-python', r.headers['Content-Type'])81 self.assertIn(u'print(2*6)', r.text)82 @pytest.mark.skipif(83 not cmd_exists('pandoc'),84 reason="Pandoc wasn't found. Skipping this test."85 )86 def test_from_file_404(self):87 with assert_http_error(404):88 self.nbconvert_api.from_file('html', 'foo', 'thisdoesntexist.ipynb')89 @pytest.mark.skipif(90 not cmd_exists('pandoc'),91 reason="Pandoc wasn't found. Skipping this test."92 )93 def test_from_file_download(self):94 r = self.nbconvert_api.from_file('python', 'foo', 'testnb.ipynb', download=True)95 content_disposition = r.headers['Content-Disposition']96 self.assertIn('attachment', content_disposition)97 self.assertIn('testnb.py', content_disposition)98 @pytest.mark.skipif(99 not cmd_exists('pandoc'),100 reason="Pandoc wasn't found. Skipping this test."101 )102 def test_from_file_zip(self):103 r = self.nbconvert_api.from_file('latex', 'foo', 'testnb.ipynb', download=True)104 self.assertIn(u'application/zip', r.headers['Content-Type'])105 self.assertIn(u'.zip', r.headers['Content-Disposition'])106 @pytest.mark.skipif(107 not cmd_exists('pandoc'),108 reason="Pandoc wasn't found. Skipping this test."109 )110 def test_from_post(self):111 nbmodel = self.request('GET', 'api/contents/foo/testnb.ipynb').json()112 r = self.nbconvert_api.from_post(format='html', nbmodel=nbmodel)113 self.assertEqual(r.status_code, 200)114 self.assertIn(u'text/html', r.headers['Content-Type'])115 self.assertIn(u'Created by test', r.text)116 self.assertIn(u'print', r.text)117 r = self.nbconvert_api.from_post(format='python', nbmodel=nbmodel)118 self.assertIn(u'text/x-python', r.headers['Content-Type'])119 self.assertIn(u'print(2*6)', r.text)120 @pytest.mark.skipif(121 not cmd_exists('pandoc'),122 reason="Pandoc wasn't found. Skipping this test."123 )124 def test_from_post_zip(self):125 nbmodel = self.request('GET', 'api/contents/foo/testnb.ipynb').json()126 r = self.nbconvert_api.from_post(format='latex', nbmodel=nbmodel)127 self.assertIn(u'application/zip', r.headers['Content-Type'])...
generate-pixmaps.py
Source: generate-pixmaps.py
...5import sys6import subprocess7# note: to be launched from where the svg files are8# this script requires inkscape, imagemagick and makeicns9def cmd_exists(cmd):10 return subprocess.call("type " + cmd, shell=True, 11 stdout=subprocess.PIPE, stderr=subprocess.PIPE) == 012if __name__ == '__main__':13 if not cmd_exists('inkscape'):14 print 'Install Inkscape!'15 sys.exit(1)16 if not cmd_exists('convert'):17 print 'convert not found, Windows icons generation disabled'18 if not cmd_exists('makeicns'):19 print 'makeicns not found, OS X icons generation disabled'20 21 for file in glob.glob('*.svg'):22 23 def convert(target_dir, target_size, add=''):24 source_file = os.path.join(os.getcwd(), file)25 target_file = os.path.join(os.getcwd(), target_dir, os.path.splitext(file)[0] + add + '.png')26 print source_file27 print target_file28 cmd = 'inkscape --export-png={0} --export-width={1} --export-height={1} {2}'29 os.system(cmd.format(target_file, target_size, source_file))30 31 base_source_file = os.path.splitext(file)[0]32 print 'Processing ' + base_source_file33 34 print 'Generating freedesktop files'35 convert('../freedesktop/48x48', '48')36 37 if cmd_exists('convert'):38 print 'Generating Windows ico'39 convert('../windows/', '256', '-256')40 convert('../windows/', '48', '-48')41 os.system('convert ../windows/{0}-48.png ../windows/{0}-256.png ../windows/{0}.ico'.format(base_source_file))42 os.system('rm ../windows/{0}-48.png ../windows/{0}-256.png'.format(base_source_file))43 44 if cmd_exists('makeicns'):45 print 'Generating OS X icns'46 convert('../osx/', '512', '-512')47 convert('../osx/', '256', '-256')48 convert('../osx/', '128', '-128')49 convert('../osx/', '32', '-32')50 convert('../osx/', '16', '-16')51 os.system('makeicns -512 ../osx/{0}-512.png -256 ../osx/{0}-256.png -128 ../osx/{0}-128.png -32 ../osx/{0}-32.png -16 ../osx/{0}-16.png -out ../osx/{0}.icns'.format(base_source_file))...
Check out the latest blogs from LambdaTest on this topic:
Hola Testers! Hope you all had a great Thanksgiving weekend! To make this time more memorable, we at LambdaTest have something to offer you as a token of appreciation.
In addition to the four values, the Agile Manifesto contains twelve principles that are used as guides for all methodologies included under the Agile movement, such as XP, Scrum, and Kanban.
JavaScript is one of the most widely used programming languages. This popularity invites a lot of JavaScript development and testing frameworks to ease the process of working with it. As a result, numerous JavaScript testing frameworks can be used to perform unit testing.
When I started writing tests with Cypress, I was always going to use the user interface to interact and change the application’s state when running tests.
The rapid shift in the use of technology has impacted testing and quality assurance significantly, especially around the cloud adoption of agile development methodologies. With this, the increasing importance of quality and automation testing has risen enough to deliver quality work.
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!!