Best Python code snippet using autotest_python
http_directory.py
Source: http_directory.py
...3import urllib24import os5import re6re_url = re.compile(r'^(([a-zA-Z_-]+)://([^/]+))(/.*)?$')7def resolve_link(link, url):8 m = re_url.match(link)9 if m is not None:10 if not m.group(4):11 # http://domain -> http://domain/12 return link + '/'13 else:14 return link15 elif link[0] == '/':16 # /some/path17 murl = re_url.match(url)18 return murl.group(1) + link19 else:20 # relative/path21 if url[-1] == '/':22 return url + link23 else:24 return url + '/' + link25class ListingParser(HTMLParser):26 """Parses an HTML file and build a list of links.27 Links are stored into the 'links' set. They are resolved into absolute28 links.29 """30 def __init__(self, url):31 HTMLParser.__init__(self)32 if url[-1] != '/':33 url += '/'34 self.__url = url35 self.links = set()36 def handle_starttag(self, tag, attrs):37 if tag == 'a':38 for key, value in attrs:39 if key == 'href':40 if not value:41 continue42 value = resolve_link(value, self.__url)43 self.links.add(value)44 break45def download_directory(url, target):46 def mkdir():47 if not mkdir.done:48 try:49 os.mkdir(target)50 except OSError:51 pass52 mkdir.done = True53 mkdir.done = False54 response = urllib2.urlopen(url)55 if response.info().type == 'text/html':56 contents = response.read()57 parser = ListingParser(url)58 parser.feed(contents)59 for link in parser.links:60 link = resolve_link(link, url)61 if link[-1] == '/':62 link = link[:-1]63 if not link.startswith(url):64 continue65 name = link.rsplit('/', 1)[1]66 if '?' in name:67 continue68 mkdir()69 download_directory(link, os.path.join(target, name))70 if not mkdir.done:71 # We didn't find anything to write inside this directory72 # Maybe it's a HTML file?73 if url[-1] != '/':74 end = target[-5:].lower()75 if not (end.endswith('.htm') or end.endswith('.html')):76 target = target + '.html'77 with open(target, 'wb') as fp:78 fp.write(contents)79 else:80 buffer_size = 409681 with open(target, 'wb') as fp:82 chunk = response.read(buffer_size)83 while chunk:84 fp.write(chunk)85 chunk = response.read(buffer_size)86###############################################################################87import unittest88class TestLinkResolution(unittest.TestCase):89 def test_absolute_link(self):90 self.assertEqual(91 resolve_link('http://website.org/p/test.txt',92 'http://some/other/url'),93 'http://website.org/p/test.txt')94 self.assertEqual(95 resolve_link('http://website.org',96 'http://some/other/url'),97 'http://website.org/')98 def test_absolute_path(self):99 self.assertEqual(100 resolve_link('/p/test.txt', 'http://some/url'),101 'http://some/p/test.txt')102 self.assertEqual(103 resolve_link('/p/test.txt', 'http://some/url/'),104 'http://some/p/test.txt')105 self.assertEqual(106 resolve_link('/p/test.txt', 'http://site'),107 'http://site/p/test.txt')108 self.assertEqual(109 resolve_link('/p/test.txt', 'http://site/'),110 'http://site/p/test.txt')111 def test_relative_path(self):112 self.assertEqual(113 resolve_link('some/file', 'http://site/folder'),114 'http://site/folder/some/file')115 self.assertEqual(116 resolve_link('some/file', 'http://site/folder/'),117 'http://site/folder/some/file')118 self.assertEqual(119 resolve_link('some/dir/', 'http://site/folder'),120 'http://site/folder/some/dir/')121class TestParser(unittest.TestCase):122 def test_parse(self):123 parser = ListingParser('http://a.remram.fr/test')124 parser.feed("""125<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 3.2 Final//EN"><html><head><title>126Index of /test</title></head><body><h1>Index of /test</h1><table><tr><th>127<img src="/icons/blank.gif" alt="[ICO]"></th><th><a href="?C=N;O=D">Name</a>128</th><th><a href="?C=M;O=A">Last modified</a></th><th><a href="?C=S;O=A">Size129</a></th><th><a href="?C=D;O=A">Description</a></th></tr><tr><th colspan="5">130<hr></th></tr><tr><td valign="top"><img src="/icons/back.gif" alt="[DIR]"></td>131<td><a href="/">Parent Directory</a></td><td> </td><td align="right"> - 132</td><td> </td></tr><tr><td valign="top">133<img src="/icons/unknown.gif" alt="[ ]"></td><td><a href="a">a</a></td>...
symbol_link.py
Source: symbol_link.py
1class SymbolLink:2 def __init__(self):3 self.linked_vars = {}4 def resolve_link(self, var):5 if var != self.linked_vars[var]:6 self.linked_vars[var] = self.resolve_link(self.linked_vars[var])7 return self.linked_vars[var]8 def link_var2(self, var1, var2):9 if var1 in self.linked_vars:10 if var2 in self.linked_vars:11 self.linked_vars[self.resolve_link(var1)] = self.resolve_link(var2)12 else:13 self.linked_vars[var2] = self.resolve_link(var1)14 else:15 if var2 in self.linked_vars:16 self.linked_vars[var1] = self.resolve_link(var2)17 else:18 self.linked_vars[var1] = self.linked_vars[var2] = var119 def link_varn(self, *vars):20 for i in range(len(vars) - 1):21 self.link_var2(vars[i], vars[i + 1])22 def vars_linked(self, var1, var2):23 if var1 == var2:24 return True25 if var1 not in self.linked_vars:26 return False27 if var2 not in self.linked_vars:28 return False...
tests.py
Source: tests.py
2from cms.models.fields import resolve_link, LinkResolutionError3class TestLinkField(TestCase):4 5 def testResolveLink(self):6 self.assertEqual(resolve_link("http://www.example.com/foo/"), "http://www.example.com/foo/")7 self.assertEqual(resolve_link("www.example.com/foo/"), "http://www.example.com/foo/")8 self.assertEqual(resolve_link("www.example.com"), "http://www.example.com/")9 self.assertEqual(resolve_link("/foo/"), "/foo/")...
Check out the latest blogs from LambdaTest on this topic:
Before we discuss Scala testing, let us understand the fundamentals of Scala and how this programming language is a preferred choice for your development requirements.The popularity and usage of Scala are rapidly rising, evident by the ever-increasing open positions for Scala developers.
So, now that the first installment of this two fold article has been published (hence you might have an idea of what Agile Testing is not in my opinion), I’ve started feeling the pressure to explain what Agile Testing actually means to me.
Did you know that according to Statista, the number of smartphone users will reach 18.22 billion by 2025? Let’s face it, digital transformation is skyrocketing and will continue to do so. This swamps the mobile app development market with various options and gives rise to the need for the best mobile app testing tools
As a developer, checking the cross browser compatibility of your CSS properties is of utmost importance when building your website. I have often found myself excited to use a CSS feature only to discover that it’s still not supported on all browsers. Even if it is supported, the feature might be experimental and not work consistently across all browsers. Ask any front-end developer about using a CSS feature whose support is still in the experimental phase in most prominent web browsers. ????
The count of mobile users is on a steep rise. According to the research, by 2025, it is expected to reach 7.49 billion users worldwide. 70% of all US digital media time comes from mobile apps, and to your surprise, the average smartphone owner uses ten apps per day and 30 apps each month.
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!!