Best Python code snippet using behave
test_spec.py
Source:test_spec.py
...53notification:54 email: messense@icloud.com55 slack_webhook: https://1"""56 f = io.StringIO(s)57 spec = Specification.parse_file(f)58 assert spec.dockerfile == 'MyDockerfile'59 assert spec.services == ['redis-server']60 assert spec.scripts == ['ls']61 assert spec.after_success == ['pwd']62 assert spec.after_failure == ['exit']63 assert spec.notification.email.recipients == ['messense@icloud.com']64 assert spec.notification.email.on_success == 'never'65 assert spec.notification.email.on_failure == 'always'66 assert spec.notification.slack_webhook.webhooks == ['https://1']67 assert spec.notification.slack_webhook.on_success == 'always'68 assert spec.notification.slack_webhook.on_failure == 'always'69def test_parse_file_single_list(app):70 s = """script:71 - ls72dockerfile: MyDockerfile73service:74 - redis-server75after_success:76 - pwd77after_failure:78 - exit79notification:80 email:81 - messense@icloud.com82 slack_webhook:83 - https://1"""84 f = io.StringIO(s)85 spec = Specification.parse_file(f)86 assert spec.dockerfile == 'MyDockerfile'87 assert spec.services == ['redis-server']88 assert spec.scripts == ['ls']89 assert spec.after_success == ['pwd']90 assert spec.after_failure == ['exit']91 assert spec.notification.email.recipients == ['messense@icloud.com']92 assert spec.notification.email.on_success == 'never'93 assert spec.notification.email.on_failure == 'always'94 assert spec.notification.slack_webhook.webhooks == ['https://1']95 assert spec.notification.slack_webhook.on_success == 'always'96 assert spec.notification.slack_webhook.on_failure == 'always'97def test_parse_file_multi_list(app):98 s = """script:99 - ls100 - ps101dockerfile: MyDockerfile102service:103 - redis-server104 - postgresql105after_success:106 - pwd107 - rm108after_failure:109 - echo110 - exit111notification:112 email:113 - tech@bosondata.com.cn114 - messense@icloud.com115 slack_webhook:116 - https://1117 - https://2"""118 f = io.StringIO(s)119 spec = Specification.parse_file(f)120 assert spec.dockerfile == 'MyDockerfile'121 assert spec.services == ['redis-server', 'postgresql']122 assert spec.scripts == ['ls', 'ps']123 assert spec.after_success == ['pwd', 'rm']124 assert spec.after_failure == ['echo', 'exit']125 assert spec.notification.email.recipients == ['tech@bosondata.com.cn', 'messense@icloud.com']126 assert spec.notification.email.on_success == 'never'127 assert spec.notification.email.on_failure == 'always'128 assert len(spec.notification.slack_webhook.webhooks) == 2129 assert spec.notification.slack_webhook.on_success == 'always'130 assert spec.notification.slack_webhook.on_failure == 'always'131def test_parse_notification_object_no_option(app):132 s = """script:133 - ls134notification:135 email:136 recipients:137 - tech@bosondata.com.cn138 - messense@icloud.com139 slack_webhook:140 webhooks:141 - https://1142 - https://2"""143 f = io.StringIO(s)144 spec = Specification.parse_file(f)145 assert spec.notification.email.recipients == ['tech@bosondata.com.cn', 'messense@icloud.com']146 assert spec.notification.email.on_success == 'never'147 assert spec.notification.email.on_failure == 'always'148 assert len(spec.notification.slack_webhook.webhooks) == 2149 assert spec.notification.slack_webhook.on_success == 'always'150 assert spec.notification.slack_webhook.on_failure == 'always'151def test_parse_notification_object_with_option(app):152 s = """script:153 - ls154notification:155 email:156 recipients:157 - tech@bosondata.com.cn158 - messense@icloud.com159 on_success: always160 slack_webhook:161 webhooks:162 - https://1163 - https://2164 on_success: never"""165 f = io.StringIO(s)166 spec = Specification.parse_file(f)167 assert spec.notification.email.recipients == ['tech@bosondata.com.cn', 'messense@icloud.com']168 assert spec.notification.email.on_success == 'always'169 assert spec.notification.email.on_failure == 'always'170 assert len(spec.notification.slack_webhook.webhooks) == 2171 assert spec.notification.slack_webhook.on_success == 'never'172 assert spec.notification.slack_webhook.on_failure == 'always'173def test_parse_env_single_string(app):174 s = "env: X=1 Y=2 Z=3\n"175 f = io.StringIO(s)176 spec = Specification.parse_file(f)177 assert len(spec.environments) == 1178 env0 = spec.environments[0]179 assert env0['X'] == '1'180 assert env0['Y'] == '2'181 assert env0['Z'] == '3'182def test_parse_env_single_list(app):183 s = """env:184 - X=1 Y=2 Z=3"""185 f = io.StringIO(s)186 spec = Specification.parse_file(f)187 assert len(spec.environments) == 1188 env0 = spec.environments[0]189 assert env0['X'] == '1'190 assert env0['Y'] == '2'191 assert env0['Z'] == '3'192def test_parse_secure_env(app):193 s = """env:194 - secure: {}""".format(to_text(SecureToken.encrypt('X=1 Y=2 Z=3')))195 f = io.StringIO(s)196 spec = Specification.parse_file(f)197 assert len(spec.environments) == 1198 env0 = spec.environments[0]199 assert env0['X'] == '1'200 assert env0['Y'] == '2'201 assert env0['Z'] == '3'202def test_parse_env_multi_list(app):203 s = """env:204 - X=1 Y=2 Z=3205 - X=3 Y=2 Z=1"""206 f = io.StringIO(s)207 spec = Specification.parse_file(f)208 assert len(spec.environments) == 2209 env0 = spec.environments[0]210 assert env0['X'] == '1'211 assert env0['Y'] == '2'212 assert env0['Z'] == '3'213 env1 = spec.environments[1]214 assert env1['X'] == '3'215 assert env1['Y'] == '2'216 assert env1['Z'] == '1'217def test_parse_simple_linter(app):218 s = """linter: flake8"""219 f = io.StringIO(s)220 spec = Specification.parse_file(f)221 assert len(spec.linters) == 1222 linter0 = spec.linters[0]223 assert linter0.name == 'flake8'224 assert linter0.pattern is None225def test_parse_linter_with_pattern(app):226 s = """linter:227 - {name: "flake8", pattern: "*.py", whatever: 123}228 - {name: "jsonlint", pattern: "*.mapping"}229 - yamllint"""230 f = io.StringIO(s)231 spec = Specification.parse_file(f)232 assert len(spec.linters) == 3233 linter0 = spec.linters[0]234 assert linter0.name == 'flake8'235 assert linter0.pattern == '*.py'236 assert linter0.whatever == 123237def test_parse_multi_linters_with_pattern(app):238 s = """linter:239 - {name: "flake8", pattern: "*.py"}240 - eslint"""241 f = io.StringIO(s)242 spec = Specification.parse_file(f)243 assert len(spec.linters) == 2244 linter0 = spec.linters[0]245 assert linter0.name == 'flake8'246 assert linter0.pattern == '*.py'247 linter1 = spec.linters[1]248 assert linter1.name == 'eslint'249 assert linter1.pattern is None250def test_parse_linter_with_regex_pattern(app):251 s = """linter: {name: "flake8", pattern: '.*\.(sls|yml|yaml)$'}"""252 f = io.StringIO(s)253 spec = Specification.parse_file(f)254 assert len(spec.linters) == 1255 linter0 = spec.linters[0]256 assert linter0.name == 'flake8'257 assert linter0.pattern == '.*\.(sls|yml|yaml)$'258def test_parse_privileged(app):259 s = """privileged: True"""260 f = io.StringIO(s)261 spec = Specification.parse_file(f)262 assert spec.privileged263 s = """privileged: no"""264 f = io.StringIO(s)265 spec = Specification.parse_file(f)266 assert not spec.privileged267 s = """linter: flake8"""268 f = io.StringIO(s)269 spec = Specification.parse_file(f)270 assert not spec.privileged271def test_parse_docker(app):272 s = """docker: True"""273 f = io.StringIO(s)274 spec = Specification.parse_file(f)275 assert spec.docker276 s = """docker: no"""277 f = io.StringIO(s)278 spec = Specification.parse_file(f)279 assert not spec.docker280 s = """linter: flake8"""281 f = io.StringIO(s)282 spec = Specification.parse_file(f)283 assert not spec.docker284def test_parse_image(app):285 s = """script: ls"""286 f = io.StringIO(s)287 spec = Specification.parse_file(f)288 assert not spec.image289 s = """image: python"""290 f = io.StringIO(s)291 spec = Specification.parse_file(f)292 assert spec.image == 'python:latest'293 s = """image: python:2.7"""294 f = io.StringIO(s)295 spec = Specification.parse_file(f)296 assert spec.image == 'python:2.7'297def test_generate_script_full_feature(app):298 s = """script:299 - ls300service:301 - redis-server302after_success:303 - pwd304after_failure:305 - exit"""306 f = io.StringIO(s)307 spec = Specification.parse_file(f)308 script = spec.shell_script309 assert 'echo + pwd\npwd' in script310 assert 'echo + exit\nexit' in script311def test_generate_script_after_success(app):312 s = """script:313 - ls314service:315 - redis-server316after_success:317 - pwd"""318 f = io.StringIO(s)319 spec = Specification.parse_file(f)320 script = spec.shell_script321 assert 'echo + pwd\npwd' in script322 assert 'if [ $SCRIPT_EXIT_CODE -eq 0 ]; then' in script323 assert 'if [ $SCRIPT_EXIT_CODE -ne 0 ]; then' not in script324def test_generate_script_after_failure(app):325 s = """script:326 - ls327service:328 - redis-server329after_failure:330 - exit"""331 f = io.StringIO(s)332 spec = Specification.parse_file(f)333 script = spec.shell_script334 assert 'echo + exit\nexit' in script335 assert 'if [ $SCRIPT_EXIT_CODE -eq 0 ]; then' not in script336 assert 'if [ $SCRIPT_EXIT_CODE -ne 0 ]; then' in script337def test_secure_field(app):338 class SecureSchema(Schema):339 token = SecureField()340 schema = SecureSchema()341 # case 1: plaintext342 data = {'token': 'abc'}343 result = schema.load(data)344 assert result['token'] == 'abc'345 # case 2: valid secure token346 data = {'token': {'secure': SecureToken.encrypt('def')}}347 result = schema.load(data)348 assert result['token'] == 'def'349 # case 3: invalid secure token350 data = {'token': {'secure': 'gAAAAABYmoldCp-EQGUKCppiqmVOu2jLrAKUz6E2e4aOMMD8Vu0VKswmJexHX6vUEoxVYKFUlSonPb91QKXZBEZdBezHzJMCHg=='}} # NOQA351 result = schema.load(data)352 assert result['token'] == ''353def test_list_field(app):354 class ListSchema(Schema):355 services = ListField(fields.String())356 schema = ListSchema()357 # case 1: scalar as list358 data = {'services': 'redis-server'}359 result = schema.load(data)360 assert result['services'] == ['redis-server']361 # case 2: list362 data = {'services': ['redis-server']}363 result = schema.load(data)364 assert result['services'] == ['redis-server']365def test_deploy_single_provider_object(app):366 s = """deploy:367 provider: script368 script: echo test369 tag: true"""370 f = io.StringIO(s)371 spec = Specification.parse_file(f)372 assert len(spec.deploy) == 1373 deploy = spec.deploy[0]374 assert deploy.tag375 assert not deploy.branch376 assert deploy.provider == 'script'377 assert deploy.script == ['echo test']378def test_deploy_single_provider_list(app):379 s = """deploy:380 - provider: script381 script: echo test"""382 f = io.StringIO(s)383 spec = Specification.parse_file(f)384 assert len(spec.deploy) == 1385 deploy = spec.deploy[0]386 assert not deploy.tag387 assert not deploy.branch388 assert deploy.provider == 'script'389 assert deploy.script == ['echo test']390def test_deploy_multiple_provider(app):391 s = """deploy:392 - provider: script393 script: echo test394 - provider: pypi395 username: test396 password: test397 tag: true"""398 f = io.StringIO(s)399 spec = Specification.parse_file(f)400 assert len(spec.deploy) == 2401 script = spec.deploy[0]402 assert not script.tag403 assert not script.branch404 assert script.provider == 'script'405 assert script.script == ['echo test']406 pypi = spec.deploy[1]407 assert pypi.tag408 assert not pypi.branch409 assert pypi.provider == 'pypi'410 assert pypi.username == 'test'411 assert pypi.password == 'test'412def test_deploy_pypi_provider_no_auth(app):413 s = """deploy:414 - provider: pypi415 tag: true"""416 f = io.StringIO(s)417 spec = Specification.parse_file(f)418 assert len(spec.deploy) == 1419 pypi = spec.deploy[0]420 assert pypi.tag421 assert not pypi.branch422 assert pypi.provider == 'pypi'423 assert not pypi.username424 assert not pypi.password425 assert pypi.repository426def test_deploy_single_provider_object_should_fail(app):427 from badwolf.exceptions import InvalidSpecification428 s = """deploy:429 script: echo test430 tag: true"""431 f = io.StringIO(s)432 with pytest.raises(InvalidSpecification):433 Specification.parse_file(f)434def test_parse_simple_artifacts(app):435 s = 'artifacts: true'436 f = io.StringIO(s)437 spec = Specification.parse_file(f)438 assert spec.artifacts.paths == ['$(git ls-files -o | tr "\\n" ":")']439 assert spec.artifacts.excludes == []440def test_parse_artifacts_paths(app):441 s = '''artifacts:442 paths:443 - dist444 excludes: __pycache__445'''446 f = io.StringIO(s)447 spec = Specification.parse_file(f)448 assert spec.artifacts.paths == ['dist']449 assert spec.artifacts.excludes == ['__pycache__']450def test_parse_vault_no_env(app):451 s = '''vault:452 url: http://localhost:8200453 token: abc123454'''455 f = io.StringIO(s)456 spec = Specification.parse_file(f)457 assert spec.vault.url == 'http://localhost:8200'458 assert spec.vault.token == 'abc123'459 assert not spec.vault.env460def test_parse_vault_with_single_env(app):461 s = '''vault:462 url: http://localhost:8200463 token: abc123464 env: API_TOKEN secret/api:token465'''466 f = io.StringIO(s)467 spec = Specification.parse_file(f)468 assert spec.vault.url == 'http://localhost:8200'469 assert spec.vault.token == 'abc123'470 env = spec.vault.env['API_TOKEN']471 assert env == ('secret/api', 'token')472def test_parse_vault_with_multi_env(app):473 s = '''vault:474 url: http://localhost:8200475 token: abc123476 env:477 - API_TOKEN secret/api:token478 - API_KEY secret/api:key479'''480 f = io.StringIO(s)481 spec = Specification.parse_file(f)482 assert spec.vault.url == 'http://localhost:8200'483 assert spec.vault.token == 'abc123'484 env = spec.vault.env['API_TOKEN']485 assert env == ('secret/api', 'token')486 env = spec.vault.env['API_KEY']487 assert env == ('secret/api', 'key')488def test_parse_vault_with_env_error(app):489 s = '''vault:490 url: http://localhost:8200491 token: abc123492 env: API_TOKEN secret/api token493'''494 f = io.StringIO(s)495 with pytest.raises(InvalidSpecification):496 Specification.parse_file(f)497def test_parse_vault_with_secretfile(app):498 s = '''vault:499 url: http://localhost:8200500 token: abc123501 env: API_TOKEN secret/api:token502'''503 f = io.StringIO(s)504 spec = Specification.parse_file(f)505 assert spec.vault.url == 'http://localhost:8200'506 assert spec.vault.token == 'abc123'507 env = spec.vault.env['API_TOKEN']508 assert env == ('secret/api', 'token')509 s = '''API_TOKEN secret/API:token510# comment511API_KEY secret/API:key512'''513 f = io.StringIO(s)514 spec.parse_secretfile(f)515 env = spec.vault.env['API_TOKEN']516 assert env == ('secret/API', 'token')517 env = spec.vault.env['API_KEY']518 assert env == ('secret/API', 'key')
test_examples.py
Source:test_examples.py
...7EXAMPLES = "../examples/"8e = lambda filename: os.path.join(EXAMPLES, filename + u'.tap')9class TestExamples(unittest.TestCase):10 def test000(self):11 doc = taptaptap.parse_file(e('000'))12 self.assertTrue(doc[1].field)13 self.assertEquals(doc[1].description, u'This is fine')14 self.assertEquals(len(doc), 1)15 self.assertTrue(doc.valid())16 def test001(self):17 doc = taptaptap.parse_file(e('001'))18 self.assertTrue(doc[1].field)19 self.assertEquals(doc[1].description, u"This one's fine")20 self.assertEquals(doc.range(), (1, 1))21 self.assertEquals(doc.plan(), u'1..1')22 self.assertFalse(doc.bailed())23 def test002(self):24 doc = taptaptap.parse_file(e('002'))25 self.assertEquals(doc.version, 13)26 self.assertTrue(doc[1].field)27 self.assertEquals(doc[1].description, u"This is fine")28 self.assertFalse(doc[1].todo)29 self.assertEquals(doc.range(), (1, 1))30 self.assertEquals(doc.plan(), u'1..1')31 self.assertFalse(doc.bailed())32 def test003(self):33 doc = taptaptap.parse_file(e('003'))34 self.assertFalse(doc.skip)35 self.assertEquals(doc.plan(), u'1..4')36 self.assertEquals(doc.range(), (1, 4))37 self.assertEquals(doc.actual_plan(), u'1..4')38 self.assertEquals(doc.actual_range(), (1, 4))39 self.assertEquals(len(doc), 4)40 self.assertEquals(doc.actual_length(), 4)41 self.assertEquals(doc[1].number, 1)42 def test004(self):43 doc = taptaptap.parse_file(e('004'))44 self.assertFalse(doc[1].field)45 self.assertEquals(doc.count_not_ok(), 1)46 self.assertEquals(doc.count_todo(), 0)47 self.assertEquals(doc.count_skip(), 0)48 def test005(self):49 doc = taptaptap.parse_file(e('005'))50 self.assertTrue(doc[1].field)51 self.assertFalse(doc[2].field)52 self.assertFalse(doc[3].todo)53 self.assertTrue(doc[4].todo)54 def test006(self):55 doc = taptaptap.parse_file(e('006'))56 self.assertEquals(len(doc), 48)57 self.assertEquals(doc.actual_length(), 3)58 self.assertEquals(doc.range(), (1, 48))59 self.assertEquals(doc.actual_range(), (1, 48))60 self.assertEquals(doc[1].description, u'Description # Directive')61 self.assertIn(u'...', doc[1].data[0])62 self.assertEquals(doc[48].description, u'Description')63 self.assertIn(u'more tests...', doc[48].data[0])64 def test007(self):65 doc = taptaptap.parse_file(e('007'))66 self.assertIn(u'Create a new', unicode(doc))67 def test008(self):68 doc = taptaptap.parse_file(e('008'))69 self.assertFalse(doc.bailed())70 self.assertFalse(doc.valid())71 self.assertEquals(len(doc), 7)72 def test009(self):73 doc = taptaptap.parse_file(e('009'))74 self.assertFalse(doc.bailed())75 self.assertTrue(doc.valid())76 self.assertEquals(doc.plan(), u'1..5')77 self.assertEquals(doc.actual_range(), (1, 5))78 def test010(self):79 doc = taptaptap.parse_file(e('010'))80 self.assertFalse(doc.bailed())81 self.assertTrue(doc.valid())82 self.assertTrue(doc.skip)83 self.assertEquals(len(doc), 0)84 def test011(self):85 doc = taptaptap.parse_file(e('011'))86 self.assertFalse(doc.bailed())87 self.assertTrue(doc.valid())88 self.assertTrue(doc.skip)89 self.assertEquals(len(doc), 0)90 self.assertEquals(doc.actual_length(), 6)91 self.assertEquals(doc.count_not_ok(), 1)92 self.assertEquals(doc.count_todo(), 0)93 def test012(self):94 doc = taptaptap.parse_file(e('012'))95 self.assertTrue(doc[3].todo)96 def test013(self):97 doc = taptaptap.parse_file(e('013'))98 self.assertTrue(len(doc), 9)99 self.assertTrue(doc.valid())100 def test014(self):101 doc = taptaptap.parse_file(e('014'))102 self.assertTrue(len(doc), 6)103 self.assertTrue(doc.valid())104 self.assertEquals(doc[6].description, u'Board size is 1')105 def test015(self):106 doc = taptaptap.parse_file(e('015'))107 self.assertEquals(doc.version, 13)108 self.assertEquals(doc.plan(), u'1..6')109 def test016(self):110 doc = taptaptap.parse_file(e('016'))111 self.assertFalse(doc[2].field)112 self.assertEquals(doc[2].data[0],113 {'message': 'First line invalid', 'severity': 'fail', 'data':114 {'got': 'Flirble', 'expect': 'Fnible'}})115 self.assertFalse(doc[4].field)116 self.assertTrue(doc[4].todo)117 self.assertEquals(doc[4].data[0],118 {'message': "Can't make summary yet", 'severity': 'todo'})119 def test017(self):120 doc = taptaptap.parse_file(e('017'))121 self.assertEquals(doc.plan(), u'1..2')122 self.assertEquals(doc[2].data[0], u' Text1\n')123 self.assertEquals(doc[2].data[1], {124 'message': 'First line invalid',125 'severity': 'fail',126 'data': {'got': 'Flirble', 'expect': 'Fnible'}127 })128 self.assertEquals(doc[2].data[2], ' not ok Text2\n')129 self.assertEquals(doc[2].data[3], {'key': 'value'})130 self.assertTrue(doc.valid())131 def test018(self):132 doc = taptaptap.parse_file(e('018'))133 self.assertRaises(taptaptap.exc.TapInvalidNumbering, lambda: doc.valid())134 def test019(self):135 doc = taptaptap.parse_file(e('019'))136 self.assertEquals(doc.version, 13)137 self.assertTrue(doc[7].field)138 self.assertEquals(doc[7].description, u"The object isa Board")139 self.assertFalse(doc[2].todo)140 self.assertEquals(doc.range(), (1, 12))141 self.assertEquals(doc.plan(), u'1..12')142 self.assertFalse(doc.bailed())143 self.assertTrue(doc.valid())144 def test020(self):145 doc = taptaptap.parse_file(e('020'))146 self.assertEquals(len(doc), 0)147 def test021(self):148 doc = taptaptap.parse_file(e('021'))149 self.assertEquals(len(doc), 573)150 self.assertEquals(doc.actual_length(), 1)151 self.assertTrue(doc.bailed())152 self.assertFalse(doc[1].field)153 self.assertIn(u"Couldn't connect to database.", doc.bailout_message())154 def iterate():155 for tc in doc:156 pass157 self.assertRaises(taptaptap.exc.TapBailout, iterate)158 def test022(self):159 doc = taptaptap.parse_file(e('022'))160 self.assertEquals(len(doc), 2)161 self.assertEquals(doc.actual_length(), 2)162 self.assertTrue(doc.bailed())163 self.assertFalse(doc.valid())164 # require first bailout message165 self.assertEquals(doc.bailout_message(), u"Couldn't connect to database.")166 def test023(self):167 doc = taptaptap.parse_file(e('023'))168 self.assertTrue(doc.valid())169 def test024(self):170 # The ultimate Pile of Poo test171 # http://intertwingly.net/blog/2013/10/22/The-Pile-of-Poo-Test172 doc = taptaptap.parse_file(e('024'))173 self.assertTrue(doc[1].description, u'ð©')174 self.assertTrue(doc.valid())175 def test025(self):176 doc = taptaptap.parse_file(e('025'))177 self.assertTrue(doc[1].field)178 self.assertTrue(doc[2].field)179 self.assertFalse(doc[3].field)180 self.assertFalse(doc[4].field)181 self.assertTrue(doc.bailed())182 self.assertFalse(doc.valid())183 self.assertEquals(doc.bailout_message(), u'Stopped iteration')184 def test026(self):185 doc = taptaptap.parse_file(e('026'))186 self.assertFalse(doc.valid())187if __name__ == '__main__':...
plot_clone_clade_verification.py
Source:plot_clone_clade_verification.py
1###############################2#3# Rest of script begins here4#5################################6import pylab7import numpy8import sys9from math import log1010import matplotlib.colors as colors11import matplotlib.cm as cmx12import matplotlib.pyplot as plt13import matplotlib.gridspec as gridspec14import matplotlib as mpl15from numpy.random import binomial16import bz217import parse_file18import matplotlib19import matplotlib.pyplot as plt20import timecourse_utils21import figure_utils22# dictionary between population and dictionary between clone and list of classification of mutations that it "has" at that timepoint. do not add anything if it is deleted. 23# clone_clades[population][clone] 24# then figure is bar charts (sorted by timepoints) 25# separate line for each population26focal_population = 'm6'27#focal_population = 'p5'28#focal_population = 'm4'29#focal_population = 'm5'30#focal_population = 'm1'31all_populations = parse_file.complete_nonmutator_lines + parse_file.mutator_lines32all_colors = parse_file.nonmutator_line_colors+parse_file.mutator_line_colors33remaining_populations = []34remaining_colors = []35for population,color in zip(all_populations, all_colors):36 if population!=focal_population:37 remaining_populations.append(population)38 remaining_colors.append(color)39remaining_populations = all_populations40remaining_colors = all_colors41metapopulations = [parse_file.complete_nonmutator_lines, parse_file.mutator_lines]42##############################43#44# First set up the figure45#46##############################47mpl.rcParams['font.size'] = 5.048mpl.rcParams['lines.linewidth'] = 0.549mpl.rcParams['legend.frameon'] = False50mpl.rcParams['legend.fontsize'] = 'small'51pylab.figure(1,figsize=(7,6.5))52fig = pylab.gcf()53# make three panels panels54outer_grid = gridspec.GridSpec(1, 2, wspace=0.25)55inner_grid_1 = gridspec.GridSpecFromSubplotSpec(6, 1, height_ratios=([1]*6),56 subplot_spec=outer_grid[0], hspace=0.2) #, hspace=0.08)57inner_grid_2 = gridspec.GridSpecFromSubplotSpec(6, 1, height_ratios=([1]*6),58 subplot_spec=outer_grid[1], hspace=0.2) #, hspace=0.08)59inner_grids = [inner_grid_1, inner_grid_2]60axes = []61for metapopulation_idx in xrange(0,2):62 axes.append([])63 for population_idx in xrange(0,6):64 65 population = metapopulations[metapopulation_idx][population_idx]66 67 axis = plt.Subplot(fig, inner_grids[metapopulation_idx][population_idx])68 fig.add_subplot(axis)69 axes[metapopulation_idx].append(axis)70 axis.set_title( parse_file.get_pretty_name(population),fontsize=5,y=0.96,loc='left')71 72 axis.spines['top'].set_visible(False)73 axis.spines['right'].set_visible(False)74 axis.get_yaxis().tick_left()75 axis.get_yaxis().set_tick_params(direction='out',length=2)76 axis.set_xticks([])77 axis.set_ylabel('Fixed mutations') 78 79 axis.set_xlim([-1,22])80 81 if population_idx==5:82 axis.set_xlabel('Clones')83 84 85########################################86#87# Now do the plotting (focal first, then rest)88#89########################################90theory_times = numpy.arange(0,121)*50091gene_names, start_positions, end_positions, promoter_start_positions, promoter_end_positions, gene_sequences, strands = parse_file.parse_gene_list()92gene_name_position_map = {gene_names[i]: (start_positions[i], end_positions[i]) for i in xrange(0,len(gene_names))}93state_color_map = {parse_file.clade_hmm_states['FB']:'0.7', parse_file.clade_hmm_states['FM']:'#7a0177', parse_file.clade_hmm_states['Fm']: '#f768a1'}94for metapopulation_idx in xrange(0,2):95 for population_idx in xrange(0,6):96 population = metapopulations[metapopulation_idx][population_idx] 97 axis = axes[metapopulation_idx][population_idx]98 sys.stderr.write('Processing %s...\n' % population)99 # load mutations100 mutations, depth_tuple = parse_file.parse_annotated_timecourse(population)101 102 population_avg_depth_times, population_avg_depths, clone_avg_depth_times, clone_avg_depths = depth_tuple103 104 dummy_times,fmajors,fminors,haplotype_trajectories = parse_file.parse_haplotype_timecourse(population)105 106 clone_mutation_Ls = []107 for clone_idx in xrange(0,len(clone_avg_depth_times)):108 clone_mutation_Ls.append( {state: 0 for state in state_color_map} )109 110 for mutation_idx in xrange(0,len(mutations)):111 112 location, gene_name, allele, var_type, test_statistic, pvalue, cutoff_idx, depth_fold_change, depth_change_pvalue, times, alts, depths, clone_times, clone_alts, clone_depths = mutations[mutation_idx] 113 114 Ls = haplotype_trajectories[mutation_idx]115 116 good_idxs, filtered_alts, filtered_depths = timecourse_utils.mask_timepoints(times, alts, depths, var_type, cutoff_idx, depth_fold_change, depth_change_pvalue)117 118 119 clone_freqs = clone_alts*1.0/(clone_depths+(clone_depths==0))120 clone_depth_fold_changes = timecourse_utils.estimate_depth_fold_changes(clone_avg_depths, clone_depths)121 for clone_idx in xrange(0,len(clone_times)):122 123 L = Ls[numpy.fabs(times-clone_times[clone_idx]).argmin()]124 125 if L not in state_color_map:126 continue127 128 # state is in allowed states129 # let's see whether the clone "has" it130 if clone_depths[clone_idx] > parse_file.default_min_depth:131 if clone_depth_fold_changes[clone_idx] > -4:132 if clone_freqs[clone_idx] > 0.5:133 clone_mutation_Ls[clone_idx][L] += 1134 #time to plot!135 for clone_idx in xrange(0,len(clone_mutation_Ls)):136 137 x = clone_idx138 width = 0.6139 140 y0 = 0141 y1 = clone_mutation_Ls[clone_idx][parse_file.clade_hmm_states['FB']]142 color = state_color_map[parse_file.clade_hmm_states['FB']]143 144 if y1-y0 > 0.5:145 axis.fill_between([x,x+0.7],[y0,y0],[y1,y1],color=color)146 147 y0 += (y1-y0)148 y1 += clone_mutation_Ls[clone_idx][parse_file.clade_hmm_states['FM']]149 color = state_color_map[parse_file.clade_hmm_states['FM']]150 151 if y1-y0 > 0.5:152 axis.fill_between([x,x+0.7],[y0,y0],[y1,y1],color=color)153 154 y0 += (y1-y0)155 y1 += clone_mutation_Ls[clone_idx][parse_file.clade_hmm_states['Fm']]156 color = state_color_map[parse_file.clade_hmm_states['Fm']]157 158 if y1-y0 > 0.5:159 axis.fill_between([x,x+0.7],[y0,y0],[y1,y1],color=color)160 161 if metapopulation_idx==0 and population_idx==0:162 163 color = state_color_map[parse_file.clade_hmm_states['FB']]164 axis.bar([-1],[1], facecolor=color,edgecolor='none',label='$F_B$')165 166 color = state_color_map[parse_file.clade_hmm_states['FM']]167 axis.bar([-1],[1], facecolor=color,edgecolor='none',label='$F_M$')168 169 color = state_color_map[parse_file.clade_hmm_states['Fm']]170 axis.bar([-1],[1], facecolor=color,edgecolor='none',label='$F_m$')171 172 axis.legend(loc='upper left',frameon=False,fontsize=7)173 174 175sys.stderr.write("Saving final image...\t")176fig.savefig(parse_file.figure_directory+'supplemental_clone_clade_verification.pdf',bbox_inches='tight',transparent=True,dpi=300)...
parser.py
Source:parser.py
...6Copyright (c) 2018 - The Alan Turing Institute7License: See the LICENSE file.8Date: 2018-10-229"""10def parse_file(11 S, dialect=None, delimiter=None, quotechar=None, escapechar=None12):13 """14 Parse a CSV file given as a string by ``S`` into a list of lists.15 This function automatically takes double quotes into account, uses 16 universal newlines, and can deal with quotes that start *inside* a cell. 17 Quotes are only stripped from cells if they occur at the start and the end 18 of the cell.19 Tests20 -----21 Testing splitting on delimiter with or without quotes22 >>> parse_file('A,B,C,D,E', delimiter=',', quotechar='"')23 [['A', 'B', 'C', 'D', 'E']]24 >>> parse_file('A,B,C,D,E', delimiter=',', quotechar='')25 [['A', 'B', 'C', 'D', 'E']]26 >>> parse_file('A,B,C,D,E')27 [['A,B,C,D,E']]28 >>> parse_file('A,"B",C,D,E', delimiter=',', quotechar='"')29 [['A', 'B', 'C', 'D', 'E']]30 >>> parse_file('A,"B,C",D,E', delimiter=',', quotechar='"')31 [['A', 'B,C', 'D', 'E']]32 >>> parse_file('A,"B,C",D,E', delimiter=',', quotechar='')33 [['A', '"B', 'C"', 'D', 'E']]34 >>> parse_file('"A","B","C",,,,', delimiter=',', quotechar='')35 [['"A"', '"B"', '"C"', '', '', '', '']]36 Testing splitting on rows only:37 >>> parse_file('A"B"C\\rA"B""C""D"', quotechar='')38 [['A"B"C'], ['A"B""C""D"']]39 >>> parse_file('A"B"C\\nA"B""C""D"', quotechar='')40 [['A"B"C'], ['A"B""C""D"']]41 >>> parse_file('A"B"C\\r\\nA"B""C""D"', quotechar='')42 [['A"B"C'], ['A"B""C""D"']]43 >>> parse_file('A"B\\r\\nB"C\\r\\nD"E"F\\r\\nG', quotechar='"')44 [['A"B\\r\\nB"C'], ['D"E"F'], ['G']]45 >>> parse_file('A"B\\nB"C\\nD"E"F\\nG', quotechar='"')46 [['A"B\\nB"C'], ['D"E"F'], ['G']]47 >>> parse_file('A"B\\nB\\rB"C\\nD"E"F\\nG', quotechar='"')48 [['A"B\\nB\\rB"C'], ['D"E"F'], ['G']]49 Tests from Python's builtin CSV module:50 >>> parse_file('')51 []52 >>> parse_file('a,b\\r', delimiter=',')53 [['a', 'b']]54 >>> parse_file('a,b\\n', delimiter=',')55 [['a', 'b']]56 >>> parse_file('a,b\\r\\n', delimiter=',')57 [['a', 'b']]58 >>> parse_file('a,"', delimiter=',', quotechar='"')59 [['a', '']]60 >>> parse_file('"a', delimiter=',', quotechar='"')61 [['a']]62 >>> parse_file('a,|b,c', delimiter=',', quotechar='"', escapechar='|') # differs from Python (1)63 [['a', '|b', 'c']]64 >>> parse_file('a,b|,c', delimiter=',', quotechar='"', escapechar='|')65 [['a', 'b,c']]66 >>> parse_file('a,"b,|c"', delimiter=',', quotechar='"', escapechar='|') # differs from Python (1)67 [['a', 'b,|c']]68 >>> parse_file('a,"b,c|""', delimiter=',', quotechar='"', escapechar='|')69 [['a', 'b,c"']]70 >>> parse_file('a,"b,c"|', delimiter=',', quotechar='"', escapechar='|') # differs from Python (2)71 [['a', 'b,c']]72 >>> parse_file('1,",3,",5', delimiter=',', quotechar='"')73 [['1', ',3,', '5']]74 >>> parse_file('1,",3,",5', delimiter=',', quotechar='')75 [['1', '"', '3', '"', '5']]76 >>> parse_file(',3,"5",7.3, 9', delimiter=',', quotechar='"')77 [['', '3', '5', '7.3', ' 9']]78 >>> parse_file('"a\\nb", 7', delimiter=',', quotechar='"')79 [['a\\nb', ' 7']]80 Double quotes:81 >>> parse_file('a,"a""b""c"', delimiter=',', quotechar='"')82 [['a', 'a"b"c']]83 Mix double and escapechar:84 >>> parse_file('a,"bc""d"",|"f|""', delimiter=',', quotechar='"', escapechar='|')85 [['a', 'bc"d","f"']]86 Other tests:87 >>> parse_file('a,b "c" d,e', delimiter=',', quotechar='')88 [['a', 'b "c" d', 'e']]89 >>> parse_file('a,b "c" d,e', delimiter=',', quotechar='"')90 [['a', 'b "c" d', 'e']]91 >>> parse_file('a,\\rb,c', delimiter=',')92 [['a', ''], ['b', 'c']]93 >>> parse_file('a,b\\r\\n\\r\\nc,d\\r\\n', delimiter=',')94 [['a', 'b'], ['c', 'd']]95 >>> parse_file('\\r\\na,b\\rc,d\\n\\re,f\\r\\n', delimiter=',')96 [['a', 'b'], ['c', 'd'], ['e', 'f']]97 Further escape char tests:98 >>> parse_file('a,b,c||d', delimiter=',', quotechar='', escapechar='|')99 [['a', 'b', 'c|d']]100 >>> parse_file('a,b,c||d,e|,d', delimiter=',', quotechar='', escapechar='|')101 [['a', 'b', 'c|d', 'e,d']]102 Quote mismatch until EOF:103 >>> parse_file('a,b,c"d,e\\n', delimiter=',', quotechar='"')104 [['a', 'b', 'c"d,e\\n']]105 >>> parse_file('a,b,c"d,e\\n', delimiter=',', quotechar='')106 [['a', 'b', 'c"d', 'e']]107 >>> parse_file('a,b,"c,d', delimiter=',', quotechar='"')108 [['a', 'b', 'c,d']]109 >>> parse_file('a,b,"c,d\\n', delimiter=',', quotechar='"')110 [['a', 'b', 'c,d\\n']]111 Single column:112 >>> parse_file('a\\rb\\rc\\n')113 [['a'], ['b'], ['c']]114 These tests illustrate a difference with the Python parser, which in this 115 case would return ``[['a', 'abc', 'd']]``.116 >>> parse_file('a,"ab"c,d', delimiter=',', quotechar='')117 [['a', '"ab"c', 'd']]118 >>> parse_file('a,"ab"c,d', delimiter=',', quotechar='"')119 [['a', '"ab"c', 'd']]120 Notes121 -----122 (1) We only interpret the escape character if it precedes the provided 123 delimiter, quotechar, or itself. Otherwise, the escape character does not 124 serve any purpose, and should not be dropped automatically.125 (2) For some reason the Python test suite places this escape character 126 *inside* the preceding quoted block. This seems counterintuitive and 127 incorrect and thus this behavior has not been duplicated.128 """129 if not dialect is None:130 delimiter = dialect.delimiter if delimiter is None else delimiter131 quotechar = dialect.quotechar if quotechar is None else quotechar132 escapechar = dialect.escapechar if escapechar is None else escapechar...
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!!