How to use new_chain method in localstack

Best Python code snippet using localstack_python

test_firewall.py

Source:test_firewall.py Github

copy

Full Screen

1# Copyright 2015 NEC Corporation2# All Rights Reserved.3#4# Licensed under the Apache License, Version 2.0 (the "License"); you may5# not use this file except in compliance with the License. You may obtain6# a copy of the License at7#8# http://www.apache.org/licenses/LICENSE-2.09#10# Unless required by applicable law or agreed to in writing, software11# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT12# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the13# License for the specific language governing permissions and limitations14# under the License.15import mock16from oslo_config import cfg17from ironic_inspector.common import ironic as ir_utils18from ironic_inspector import firewall19from ironic_inspector import introspection_state as istate20from ironic_inspector import node_cache21from ironic_inspector.test import base as test_base22CONF = cfg.CONF23IB_DATA = """24EMAC=02:00:02:97:00:01 IMAC=97:fe:80:00:00:00:00:00:00:7c:fe:90:03:00:29:26:5225EMAC=02:00:00:61:00:02 IMAC=61:fe:80:00:00:00:00:00:00:7c:fe:90:03:00:29:24:4f26"""27@mock.patch.object(firewall, '_iptables')28@mock.patch.object(ir_utils, 'get_client')29@mock.patch.object(firewall.subprocess, 'check_call')30class TestFirewall(test_base.NodeTest):31 CLIENT_ID = 'ff:00:00:00:00:00:02:00:00:02:c9:00:7c:fe:90:03:00:29:24:4f'32 def test_update_filters_without_manage_firewall(self, mock_call,33 mock_get_client,34 mock_iptables):35 CONF.set_override('manage_firewall', False, 'firewall')36 firewall.update_filters()37 self.assertEqual(0, mock_iptables.call_count)38 def test_init_args(self, mock_call, mock_get_client, mock_iptables):39 rootwrap_path = '/some/fake/path'40 CONF.set_override('rootwrap_config', rootwrap_path)41 firewall.init()42 init_expected_args = [43 ('-D', 'INPUT', '-i', 'br-ctlplane', '-p', 'udp', '--dport', '67',44 '-j', CONF.firewall.firewall_chain),45 ('-F', CONF.firewall.firewall_chain),46 ('-X', CONF.firewall.firewall_chain),47 ('-N', CONF.firewall.firewall_chain)]48 call_args_list = mock_iptables.call_args_list49 for (args, call) in zip(init_expected_args, call_args_list):50 self.assertEqual(args, call[0])51 expected = ('sudo', 'ironic-inspector-rootwrap', rootwrap_path,52 'iptables', '-w')53 self.assertEqual(expected, firewall.BASE_COMMAND)54 def test_init_args_old_iptables(self, mock_call, mock_get_client,55 mock_iptables):56 rootwrap_path = '/some/fake/path'57 CONF.set_override('rootwrap_config', rootwrap_path)58 mock_call.side_effect = firewall.subprocess.CalledProcessError(2, '')59 firewall.init()60 init_expected_args = [61 ('-D', 'INPUT', '-i', 'br-ctlplane', '-p', 'udp', '--dport', '67',62 '-j', CONF.firewall.firewall_chain),63 ('-F', CONF.firewall.firewall_chain),64 ('-X', CONF.firewall.firewall_chain),65 ('-N', CONF.firewall.firewall_chain)]66 call_args_list = mock_iptables.call_args_list67 for (args, call) in zip(init_expected_args, call_args_list):68 self.assertEqual(args, call[0])69 expected = ('sudo', 'ironic-inspector-rootwrap', rootwrap_path,70 'iptables',)71 self.assertEqual(expected, firewall.BASE_COMMAND)72 def test_init_kwargs(self, mock_call, mock_get_client, mock_iptables):73 firewall.init()74 init_expected_kwargs = [75 {'ignore': True},76 {'ignore': True},77 {'ignore': True}]78 call_args_list = mock_iptables.call_args_list79 for (kwargs, call) in zip(init_expected_kwargs, call_args_list):80 self.assertEqual(kwargs, call[1])81 def test_update_filters_args(self, mock_call, mock_get_client,82 mock_iptables):83 # Pretend that we have nodes on introspection84 node_cache.add_node(self.node.uuid, state=istate.States.waiting,85 bmc_address='1.2.3.4')86 firewall.init()87 update_filters_expected_args = [88 ('-D', 'INPUT', '-i', 'br-ctlplane', '-p', 'udp', '--dport',89 '67', '-j', CONF.firewall.firewall_chain),90 ('-F', CONF.firewall.firewall_chain),91 ('-X', CONF.firewall.firewall_chain),92 ('-N', CONF.firewall.firewall_chain),93 ('-D', 'INPUT', '-i', 'br-ctlplane', '-p', 'udp', '--dport',94 '67', '-j', firewall.NEW_CHAIN),95 ('-F', firewall.NEW_CHAIN),96 ('-X', firewall.NEW_CHAIN),97 ('-N', firewall.NEW_CHAIN),98 ('-A', firewall.NEW_CHAIN, '-j', 'ACCEPT'),99 ('-I', 'INPUT', '-i', 'br-ctlplane', '-p', 'udp', '--dport',100 '67', '-j', firewall.NEW_CHAIN),101 ('-D', 'INPUT', '-i', 'br-ctlplane', '-p', 'udp', '--dport',102 '67', '-j', CONF.firewall.firewall_chain),103 ('-F', CONF.firewall.firewall_chain),104 ('-X', CONF.firewall.firewall_chain),105 ('-E', firewall.NEW_CHAIN, CONF.firewall.firewall_chain)106 ]107 firewall.update_filters()108 call_args_list = mock_iptables.call_args_list109 for (args, call) in zip(update_filters_expected_args,110 call_args_list):111 self.assertEqual(args, call[0])112 def test_update_filters_kwargs(self, mock_call, mock_get_client,113 mock_iptables):114 firewall.init()115 update_filters_expected_kwargs = [116 {'ignore': True},117 {'ignore': True},118 {'ignore': True},119 {},120 {'ignore': True},121 {'ignore': True},122 {'ignore': True},123 {},124 {},125 {},126 {'ignore': True},127 {'ignore': True},128 {'ignore': True}129 ]130 firewall.update_filters()131 call_args_list = mock_iptables.call_args_list132 for (kwargs, call) in zip(update_filters_expected_kwargs,133 call_args_list):134 self.assertEqual(kwargs, call[1])135 def test_update_filters_with_blacklist(self, mock_call, mock_get_client,136 mock_iptables):137 active_macs = ['11:22:33:44:55:66', '66:55:44:33:22:11']138 inactive_mac = ['AA:BB:CC:DD:EE:FF']139 self.macs = active_macs + inactive_mac140 self.ports = [mock.Mock(address=m) for m in self.macs]141 mock_get_client.port.list.return_value = self.ports142 node_cache.add_node(self.node.uuid, mac=active_macs,143 state=istate.States.finished,144 bmc_address='1.2.3.4', foo=None)145 firewall.init()146 update_filters_expected_args = [147 ('-D', 'INPUT', '-i', 'br-ctlplane', '-p', 'udp', '--dport',148 '67', '-j', CONF.firewall.firewall_chain),149 ('-F', CONF.firewall.firewall_chain),150 ('-X', CONF.firewall.firewall_chain),151 ('-N', CONF.firewall.firewall_chain),152 ('-D', 'INPUT', '-i', 'br-ctlplane', '-p', 'udp', '--dport',153 '67', '-j', firewall.NEW_CHAIN),154 ('-F', firewall.NEW_CHAIN),155 ('-X', firewall.NEW_CHAIN),156 ('-N', firewall.NEW_CHAIN),157 # Blacklist158 ('-A', firewall.NEW_CHAIN, '-m', 'mac', '--mac-source',159 inactive_mac[0], '-j', 'DROP'),160 ('-A', firewall.NEW_CHAIN, '-j', 'ACCEPT'),161 ('-I', 'INPUT', '-i', 'br-ctlplane', '-p', 'udp', '--dport',162 '67', '-j', firewall.NEW_CHAIN),163 ('-D', 'INPUT', '-i', 'br-ctlplane', '-p', 'udp', '--dport',164 '67', '-j', CONF.firewall.firewall_chain),165 ('-F', CONF.firewall.firewall_chain),166 ('-X', CONF.firewall.firewall_chain),167 ('-E', firewall.NEW_CHAIN, CONF.firewall.firewall_chain)168 ]169 firewall.update_filters(mock_get_client)170 call_args_list = mock_iptables.call_args_list171 for (args, call) in zip(update_filters_expected_args,172 call_args_list):173 self.assertEqual(args, call[0])174 # check caching175 mock_iptables.reset_mock()176 firewall.update_filters(mock_get_client)177 self.assertFalse(mock_iptables.called)178 def test_update_filters_clean_cache_on_error(self, mock_call,179 mock_get_client,180 mock_iptables):181 active_macs = ['11:22:33:44:55:66', '66:55:44:33:22:11']182 inactive_mac = ['AA:BB:CC:DD:EE:FF']183 self.macs = active_macs + inactive_mac184 self.ports = [mock.Mock(address=m) for m in self.macs]185 mock_get_client.port.list.return_value = self.ports186 node_cache.add_node(self.node.uuid, mac=active_macs,187 state=istate.States.finished,188 bmc_address='1.2.3.4', foo=None)189 firewall.init()190 update_filters_expected_args = [191 ('-D', 'INPUT', '-i', 'br-ctlplane', '-p', 'udp', '--dport',192 '67', '-j', firewall.NEW_CHAIN),193 ('-F', firewall.NEW_CHAIN),194 ('-X', firewall.NEW_CHAIN),195 ('-N', firewall.NEW_CHAIN),196 # Blacklist197 ('-A', firewall.NEW_CHAIN, '-m', 'mac', '--mac-source',198 inactive_mac[0], '-j', 'DROP'),199 ('-A', firewall.NEW_CHAIN, '-j', 'ACCEPT'),200 ('-I', 'INPUT', '-i', 'br-ctlplane', '-p', 'udp', '--dport',201 '67', '-j', firewall.NEW_CHAIN),202 ('-D', 'INPUT', '-i', 'br-ctlplane', '-p', 'udp', '--dport',203 '67', '-j', CONF.firewall.firewall_chain),204 ('-F', CONF.firewall.firewall_chain),205 ('-X', CONF.firewall.firewall_chain),206 ('-E', firewall.NEW_CHAIN, CONF.firewall.firewall_chain)207 ]208 mock_iptables.side_effect = [None, None, RuntimeError()]209 self.assertRaises(RuntimeError, firewall.update_filters,210 mock_get_client)211 # check caching212 mock_iptables.reset_mock()213 mock_iptables.side_effect = None214 firewall.update_filters(mock_get_client)215 call_args_list = mock_iptables.call_args_list216 for (args, call) in zip(update_filters_expected_args,217 call_args_list):218 self.assertEqual(args, call[0])219 def test_update_filters_args_node_not_found_hook(self, mock_call,220 mock_get_client,221 mock_iptables):222 # DHCP should be always opened if node_not_found hook is set223 CONF.set_override('node_not_found_hook', 'enroll', 'processing')224 firewall.init()225 update_filters_expected_args = [226 ('-D', 'INPUT', '-i', 'br-ctlplane', '-p', 'udp', '--dport',227 '67', '-j', CONF.firewall.firewall_chain),228 ('-F', CONF.firewall.firewall_chain),229 ('-X', CONF.firewall.firewall_chain),230 ('-N', CONF.firewall.firewall_chain),231 ('-D', 'INPUT', '-i', 'br-ctlplane', '-p', 'udp', '--dport',232 '67', '-j', firewall.NEW_CHAIN),233 ('-F', firewall.NEW_CHAIN),234 ('-X', firewall.NEW_CHAIN),235 ('-N', firewall.NEW_CHAIN),236 ('-A', firewall.NEW_CHAIN, '-j', 'ACCEPT'),237 ('-I', 'INPUT', '-i', 'br-ctlplane', '-p', 'udp', '--dport',238 '67', '-j', firewall.NEW_CHAIN),239 ('-D', 'INPUT', '-i', 'br-ctlplane', '-p', 'udp', '--dport',240 '67', '-j', CONF.firewall.firewall_chain),241 ('-F', CONF.firewall.firewall_chain),242 ('-X', CONF.firewall.firewall_chain),243 ('-E', firewall.NEW_CHAIN, CONF.firewall.firewall_chain)244 ]245 firewall.update_filters()246 call_args_list = mock_iptables.call_args_list247 for (args, call) in zip(update_filters_expected_args,248 call_args_list):249 self.assertEqual(args, call[0])250 def test_update_filters_args_no_introspection(self, mock_call,251 mock_get_client,252 mock_iptables):253 firewall.init()254 firewall.BLACKLIST_CACHE = ['foo']255 mock_get_client.return_value.port.list.return_value = [256 mock.Mock(address='foobar')]257 update_filters_expected_args = [258 ('-D', 'INPUT', '-i', 'br-ctlplane', '-p', 'udp', '--dport',259 '67', '-j', CONF.firewall.firewall_chain),260 ('-F', CONF.firewall.firewall_chain),261 ('-X', CONF.firewall.firewall_chain),262 ('-N', CONF.firewall.firewall_chain),263 ('-D', 'INPUT', '-i', 'br-ctlplane', '-p', 'udp', '--dport',264 '67', '-j', firewall.NEW_CHAIN),265 ('-F', firewall.NEW_CHAIN),266 ('-X', firewall.NEW_CHAIN),267 ('-N', firewall.NEW_CHAIN),268 ('-A', firewall.NEW_CHAIN, '-j', 'REJECT'),269 ('-I', 'INPUT', '-i', 'br-ctlplane', '-p', 'udp', '--dport',270 '67', '-j', firewall.NEW_CHAIN),271 ('-D', 'INPUT', '-i', 'br-ctlplane', '-p', 'udp', '--dport',272 '67', '-j', CONF.firewall.firewall_chain),273 ('-F', CONF.firewall.firewall_chain),274 ('-X', CONF.firewall.firewall_chain),275 ('-E', firewall.NEW_CHAIN, CONF.firewall.firewall_chain)276 ]277 firewall.update_filters()278 call_args_list = mock_iptables.call_args_list279 for (args, call) in zip(update_filters_expected_args,280 call_args_list):281 self.assertEqual(args, call[0])282 self.assertIsNone(firewall.BLACKLIST_CACHE)283 # Check caching enabled flag284 mock_iptables.reset_mock()285 firewall.update_filters()286 self.assertFalse(mock_iptables.called)287 # Adding a node changes it back288 node_cache.add_node(self.node.uuid, state=istate.States.starting,289 bmc_address='1.2.3.4')290 mock_iptables.reset_mock()291 firewall.update_filters()292 mock_iptables.assert_any_call('-A', firewall.NEW_CHAIN, '-j', 'ACCEPT')293 self.assertEqual({'foobar'}, firewall.BLACKLIST_CACHE)294 def test_update_filters_infiniband(295 self, mock_call, mock_get_client, mock_iptables):296 CONF.set_override('ethoib_interfaces', ['eth0'], 'firewall')297 active_macs = ['11:22:33:44:55:66', '66:55:44:33:22:11']298 expected_rmac = '02:00:00:61:00:02'299 ports = [mock.Mock(address=m) for m in active_macs]300 ports.append(mock.Mock(address='7c:fe:90:29:24:4f',301 extra={'client-id': self.CLIENT_ID},302 spec=['address', 'extra']))303 mock_get_client.port.list.return_value = ports304 node_cache.add_node(self.node.uuid, mac=active_macs,305 state=istate.States.finished,306 bmc_address='1.2.3.4', foo=None)307 firewall.init()308 update_filters_expected_args = [309 ('-D', 'INPUT', '-i', 'br-ctlplane', '-p', 'udp', '--dport',310 '67', '-j', CONF.firewall.firewall_chain),311 ('-F', CONF.firewall.firewall_chain),312 ('-X', CONF.firewall.firewall_chain),313 ('-N', CONF.firewall.firewall_chain),314 ('-D', 'INPUT', '-i', 'br-ctlplane', '-p', 'udp', '--dport',315 '67', '-j', firewall.NEW_CHAIN),316 ('-F', firewall.NEW_CHAIN),317 ('-X', firewall.NEW_CHAIN),318 ('-N', firewall.NEW_CHAIN),319 # Blacklist320 ('-A', firewall.NEW_CHAIN, '-m', 'mac', '--mac-source',321 expected_rmac, '-j', 'DROP'),322 ('-A', firewall.NEW_CHAIN, '-j', 'ACCEPT'),323 ('-I', 'INPUT', '-i', 'br-ctlplane', '-p', 'udp', '--dport',324 '67', '-j', firewall.NEW_CHAIN),325 ('-D', 'INPUT', '-i', 'br-ctlplane', '-p', 'udp', '--dport',326 '67', '-j', CONF.firewall.firewall_chain),327 ('-F', CONF.firewall.firewall_chain),328 ('-X', CONF.firewall.firewall_chain),329 ('-E', firewall.NEW_CHAIN, CONF.firewall.firewall_chain)330 ]331 fileobj = mock.mock_open(read_data=IB_DATA)332 with mock.patch('six.moves.builtins.open', fileobj, create=True):333 firewall.update_filters(mock_get_client)334 call_args_list = mock_iptables.call_args_list335 for (args, call) in zip(update_filters_expected_args,336 call_args_list):337 self.assertEqual(args, call[0])338 def test_update_filters_infiniband_no_such_file(339 self, mock_call, mock_get_client, mock_iptables):340 CONF.set_override('ethoib_interfaces', ['eth0'], 'firewall')341 active_macs = ['11:22:33:44:55:66', '66:55:44:33:22:11']342 ports = [mock.Mock(address=m) for m in active_macs]343 ports.append(mock.Mock(address='7c:fe:90:29:24:4f',344 extra={'client-id': self.CLIENT_ID},345 spec=['address', 'extra']))346 mock_get_client.port.list.return_value = ports347 node_cache.add_node(self.node.uuid, mac=active_macs,348 state=istate.States.finished,349 bmc_address='1.2.3.4', foo=None)350 firewall.init()351 update_filters_expected_args = [352 ('-D', 'INPUT', '-i', 'br-ctlplane', '-p', 'udp', '--dport',353 '67', '-j', CONF.firewall.firewall_chain),354 ('-F', CONF.firewall.firewall_chain),355 ('-X', CONF.firewall.firewall_chain),356 ('-N', CONF.firewall.firewall_chain),357 ('-D', 'INPUT', '-i', 'br-ctlplane', '-p', 'udp', '--dport',358 '67', '-j', firewall.NEW_CHAIN),359 ('-F', firewall.NEW_CHAIN),360 ('-X', firewall.NEW_CHAIN),361 ('-N', firewall.NEW_CHAIN),362 # Blacklist363 ('-A', firewall.NEW_CHAIN, '-m', 'mac', '--mac-source',364 '7c:fe:90:29:24:4f', '-j', 'DROP'),365 ('-A', firewall.NEW_CHAIN, '-j', 'ACCEPT'),366 ('-I', 'INPUT', '-i', 'br-ctlplane', '-p', 'udp', '--dport',367 '67', '-j', firewall.NEW_CHAIN),368 ('-D', 'INPUT', '-i', 'br-ctlplane', '-p', 'udp', '--dport',369 '67', '-j', CONF.firewall.firewall_chain),370 ('-F', CONF.firewall.firewall_chain),371 ('-X', CONF.firewall.firewall_chain),372 ('-E', firewall.NEW_CHAIN, CONF.firewall.firewall_chain)373 ]374 with mock.patch('six.moves.builtins.open', side_effect=IOError()):375 firewall.update_filters(mock_get_client)376 call_args_list = mock_iptables.call_args_list377 for (args, call) in zip(update_filters_expected_args,378 call_args_list):...

Full Screen

Full Screen

encrypt_playfair.py

Source:encrypt_playfair.py Github

copy

Full Screen

1# Κρυπτογράφηση2def encryption(key_table):3 #---------------------------------------------------------------------------- 4 #Ζητάει από τον χρήστη να εισάγει το κείμενο που θέλει να κρυπτογραφίσει 5 message = input("Enter the message you want to encrypt in UPPERCASE. !REMEMBER! : Use only ONE space character to seperate words ")6 #Αφού κάνει τις απαιτούμενες αλλαγές : 7 #1) Αφαιρεί τους κενούς χαρακτήρες του κειμένου και μετατρέπει τους χαρακτήρες σε κεφαλαία8 message = ''.join(message.split()) 9 message = message.upper()10 #2) Αντικαθιστεί τον χαρακτήρα J με τον χαρακτήρα I11 message = message.replace("J" , "I")12 #3) Προσθέτει τον χαρακτήρα Q ανάμεσα σε διπλανούς όμοιους χαρακτήρες13 cnumber = len(message)14 for x in range(cnumber-1): 15 if message[x] == message[x+1]:16 message = message[:x+1] + 'Q' +message[x+1:]17 cnumber += 118 chain = list(message)19 #4) Εισάγει τον χαρακτήρα Q στο τέλος του νήματος αν ο αριθμός των χαρακτήρων είναι μονός20 if cnumber % 2 == 1:21 chain.append('Q') 22 cnumber +=123 # Τα εισάγει σε μία δισδιάστατη λίστα χωρίζοντάς το κείμενο σε ζευγάρια χαρακτήρων24 new_chain = [chain[y:y+2] for y in range(0,cnumber,2)][:cnumber]25 #1) Αναζητά τις θέσεις των ζευγαριών της λίστας στον πίνακα με τη λέξη κλειδί26 for r in range(0,cnumber//2,1):27 i1 = -128 i2 = -129 j1 = -130 j2 = -131 key1 = new_chain[r][0]32 key2 = new_chain[r][1]33 found1 = False34 found2 = False35 for i in range(8) : 36 for j in range(8) :37 if key1 == key_table[i][j] :38 i1 = i39 j1 = j40 found1 = True41 if key2 == key_table[i][j] :42 i2 = i43 j2 = j44 found2 = True45 if found1 and found2 :46 break47 if found1 and found2 :48 break49 if i1 == i2 :50 if j1 == 7 :51 new_chain[r][0] = key_table[i1][0]52 new_chain[r][1] = key_table[i2][j2+1]53 elif j2 == 7 :54 new_chain[r][0] = key_table[i1][j1+1]55 new_chain[r][1] = key_table[i2][0] 56 else :57 new_chain[r][0] = key_table[i1][j1+1]58 new_chain[r][1] = key_table[i2][j2+1]59 elif j1 == j2 :60 if i1 == 7 :61 new_chain[r][0] = key_table[0][j1]62 new_chain[r][1] = key_table[i2+1][j2]63 elif i2 == 7 :64 new_chain[r][0] = key_table[i1+1][j1]65 new_chain[r][1] = key_table[0][j2] 66 else : 67 new_chain[r][0] = key_table[i1+1][j1]68 new_chain[r][1] = key_table[i2+1][j2]69 else :70 new_chain[r][0] = key_table[i1][j2]71 new_chain[r][1] = key_table[i2][j1]72 print("This is the final message : ")73 for i in range(cnumber//2):74 for j in range(2):75 print(new_chain[i][j], end="")76 print("\n")77 78# _____ 79# / ___ \80# / / _ \ \81# /( /( /(_)\ )\ )\82# ( \ \ ___ / / )83# ( \ _____ / )84# /( )\85# | \ / |86# | \ _______ / |87# \ / \ / \ /88# \/ | | \/89# | | 90# | |91# | |...

Full Screen

Full Screen

pdb_rechain.py

Source:pdb_rechain.py Github

copy

Full Screen

1#!/usr/bin/env python2"""3Replaces a particular chain ID in a PDB file.4usage: python pdb_rechain.py -<chain> -<chain> <pdb file>5example: python pdb_rechain.py -A -B 1CTF.pdb6This program is part of the PDB tools distributed with HADDOCK7or with the HADDOCK tutorial. The utilities in this package8can be used to quickly manipulate PDB files, with the benefit9of 'piping' several different commands. This is a rewrite of old10FORTRAN77 code that was taking too much effort to compile. RIP.11"""12import os13import re14import sys15__author__ = "Joao Rodrigues"16__email__ = "j.p.g.l.m.rodrigues@gmail.com"17#USAGE = "usage: " + sys.argv[0] + " -<chain> -<chain> <pdb file>\n"18USAGE = __doc__.format(__author__, __email__)19def check_input(args):20 """Checks whether to read from stdin/file and validates user input/options."""21 if not len(args):22 sys.stderr.write(USAGE)23 sys.exit(1)24 elif len(args) == 2:25 # Pipe?26 if not sys.stdin.isatty():27 pdbfh = sys.stdin28 else:29 sys.stderr.write(USAGE)30 sys.exit(1)31 ori_chain = args[0]32 new_chain = args[1]33 assert re.match('\-[A-Za-z0-9 ]', ori_chain), 'Invalid chain ID: ' + ori_chain34 assert re.match('\-[A-Za-z0-9 ]', ori_chain), 'Invalid chain ID: ' + new_chain35 elif len(args) == 3:36 if not os.path.exists(args[2]):37 sys.stderr.write('File not found: ' + args[0] + '\n')38 sys.stderr.write(USAGE)39 sys.exit(1)40 pdbfh = open(args[2], 'r')41 ori_chain = args[0]42 new_chain = args[1]43 assert re.match('\-[A-Za-z0-9 ]', ori_chain), 'Invalid chain ID: ' + ori_chain44 assert re.match('\-[A-Za-z0-9 ]', ori_chain), 'Invalid chain ID: ' + new_chain45 else:46 sys.stderr.write(USAGE)47 sys.exit(1)48 return (pdbfh, ori_chain, new_chain)49def _alter_chain(fhandle, ori_chain, new_chain):50 """Enclosing logic in a function to speed up a bit"""51 coord_re = re.compile('^(ATOM|HETATM)')52 fhandle = fhandle53 ori_chain = ori_chain[1:]54 new_chain = new_chain[1:]55 if not ori_chain:56 ori_chain = ' '57 if not new_chain:58 new_chain = ' '59 for line in fhandle:60 if coord_re.match(line) and line[21] == ori_chain:61 yield line[:21] + new_chain + line[22:]62 else:63 yield line64if __name__ == '__main__':65 # Check Input66 pdbfh, ori_chain, new_chain = check_input(sys.argv[1:])67 # Do the job68 new_pdb = _alter_chain(pdbfh, ori_chain, new_chain)69 try:70 sys.stdout.write(''.join(new_pdb))71 sys.stdout.flush()72 except IOError:73 # This is here to catch Broken Pipes74 # for example to use 'head' or 'tail' without75 # the error message showing up76 pass77 # last line of the script78 # We can close it even if it is sys.stdin79 pdbfh.close()...

Full Screen

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 localstack 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