Editing a .txt file - Algorithm not working - python

I want to automatially edit .txt files with code. Everything containing victory_poins shall be removed and entered in another form after the "history={" statement. But in the end, it adds an additional history={. Why?
Code:
def überschreiben(filename,vp, capital):
data_out=open(filename,"r")
data_in=open(filename+"_output.txt","w")
vpsegment=False
for line in data_out:
if "\thistory" in line:
data_in.write(line+'\n\t\tvictory_points = { '+str(capital)+' '+str(vp)+' }\n')
if "\t\tvictory_points" in line:
vppivot=line
vpsegment=True
if vpsegment==True:
if "}" in line:
data_in.write("")
vpsegment=False
else:
data_in.write("")
else:
data_in.write(line)
data_in.close()
data_out.close()
Input:
state={
id=1
name="STATE_1" # Corsica
manpower = 322900
state_category = town
history={
owner = FRA
victory_points = { 3838 1 }
buildings = {
infrastructure = 4
industrial_complex = 1
air_base = 1
3838 = {
naval_base = 3
}
}
add_core_of = FRA
}
provinces={
3838 9851 11804
}
}
Output:
[...]
state_category = town
history={
victory_points = { 00001 8 }
history={
owner = FRA
buildings = {
infrastructure = 4
industrial_complex = 1
air_base = 1
3838 = {
naval_base = 3
}
}
add_core_of = FRA
}
provinces={
3838 9851 11804
}
}
Where does the second history={ come from?

Let's look at what happens when you read the line history{ :
if "\thistory" in line:
data_in.write(line+'\n\t\tvictory_points = { '+str(capital)+' '+str(vp)+' }\n')
The line contains "\thistory" so it writes the lines (it writes the first "history{") and other things
if "\t\tvictory_points" in line:
vppivot=line
vpsegment=True
Nothing happens because the line does not contain "\t\tvictory_points"
if vpsegment==True:
if "}" in line:
data_in.write("")
vpsegment=False
else:
data_in.write("")
else:
data_in.write(line)
vpsegment==False so it goes to the else statement and write the line which is "\thistory{"

Related

Stopping the pipette with user input (opentrons)

Working on a biotech research project where a robot is doing the dilution and I was having trouble configuring the code so that the pipette stops a specific column. Ideally we want the code to ask the user for which column the pipette to stop at.
More info on the API: https://protocols.opentrons.com/protocol/customizable_serial_dilution_ot2
def get_values(*names):
import json
_all_values = json.loads("""{"pipette_type":"p300_single_gen2","mount_side":"right","tip_type":"standard","trough_type":"nest_12_reservoir_15ml","plate_type":"nest_96_wellplate_200ul_flat","dilution_factor":3,"num_of_dilutions":10,"total_mixing_volume":150,"blank_on":true,"tip_use_strategy":"never","air_gap_volume":10}""")
return [_all_values[n] for n in names]
"""DETAILS."""
metadata = {
'protocolName': 'Customizable Serial Dilution',
'author': 'Opentrons <protocols#opentrons.com>',
'source': 'Protocol Library',
'apiLevel': '2.11'
}
def run(protocol_context):
"""PROTOCOL BODY."""
[pipette_type, mount_side, tip_type, trough_type, plate_type,
dilution_factor, num_of_dilutions, total_mixing_volume,
blank_on, tip_use_strategy, air_gap_volume] = get_values( # noqa: F821
'pipette_type', 'mount_side', 'tip_type', 'trough_type',
'plate_type', 'dilution_factor', 'num_of_dilutions',
'total_mixing_volume', 'blank_on',
'tip_use_strategy', 'air_gap_volume'
)
# check for bad setup here
if not 1 <= num_of_dilutions <= 11:
raise Exception('Enter a number of dilutions between 1 and 11')
if num_of_dilutions == 11 and blank_on == 1:
raise Exception(
'No room for blank with 11 dilutions')
pip_range = pipette_type.split('_')[0].lower()
tiprack_map = {
'p10': {
'standard': 'opentrons_96_tiprack_10ul',
'filter': 'opentrons_96_filtertiprack_20ul'
},
'p20': {
'standard': 'opentrons_96_tiprack_20ul',
'filter': 'opentrons_96_filtertiprack_20ul'
},
'p50': {
'standard': 'opentrons_96_tiprack_300ul',
'filter': 'opentrons_96_filtertiprack_200ul'
},
'p300': {
'standard': 'opentrons_96_tiprack_300ul',
'filter': 'opentrons_96_filtertiprack_200ul'
},
'p1000': {
'standard': 'opentrons_96_tiprack_1000ul',
'filter': 'opentrons_96_filtertiprack_1000ul'
}
}
# labware
trough = protocol_context.load_labware(
trough_type, '2')
plate = protocol_context.load_labware(
plate_type, '3')
tip_name = tiprack_map[pip_range][tip_type]
tipracks = [
protocol_context.load_labware(tip_name, slot)
for slot in ['1', '4']
]
print(mount_side)
# pipette
pipette = protocol_context.load_instrument(
pipette_type, mount_side, tipracks)
# reagents
diluent = trough.wells()[0]
transfer_volume = total_mixing_volume/dilution_factor
diluent_volume = total_mixing_volume - transfer_volume
if 'multi' in pipette_type:
dilution_destination_sets = [
[row] for row in plate.rows()[0][1:num_of_dilutions]]
dilution_source_sets = [
[row] for row in plate.rows()[0][:num_of_dilutions-1]]
blank_set = [plate.rows()[0][num_of_dilutions+1]]
else:
dilution_destination_sets = plate.columns()[1:num_of_dilutions]
dilution_source_sets = plate.columns()[:num_of_dilutions-1]
blank_set = plate.columns()[num_of_dilutions+1]
all_diluent_destinations = [
well for set in dilution_destination_sets for well in set]
pipette.pick_up_tip()
for dest in all_diluent_destinations:
# Distribute diluent across the plate to the the number of samples
# And add diluent to one column after the number of samples for a blank
pipette.transfer(
diluent_volume,
diluent,
dest,
air_gap=air_gap_volume,
new_tip='never')
pipette.drop_tip()
# Dilution of samples across the 96-well flat bottom plate
if tip_use_strategy == 'never':
pipette.pick_up_tip()
for source_set, dest_set in zip(dilution_source_sets,
dilution_destination_sets):
for s, d in zip(source_set, dest_set):
pipette.transfer(
transfer_volume,
s,
d,
air_gap=air_gap_volume,
mix_after=(5, total_mixing_volume/2),
new_tip=tip_use_strategy)
if tip_use_strategy == 'never':
pipette.drop_tip()
if blank_on:
pipette.pick_up_tip()
for blank_well in blank_set:
pipette.transfer(
diluent_volume,
diluent,
blank_well,
air_gap=air_gap_volume,
new_tip='never')
pipette.drop_tip()
Any help is very much appreciated. Thank you!
Currently the robot just goes through all the columns but we want to find a way to have it stop as a specific column.

Change Python string

I have a long string that looks like below. This is a small part of it, but the pattern repeat.
The raw data that I get when I read the API with Requests:
...
}
#VER B 160 20201020 "Test Bolag AB (4117)" 20201223
{
#TRANS 3001 {6 "1000050"} -180000 "" "" 0
#TRANS 2611 {6 "1000050"} -45000 "" "" 0
#TRANS 1510 {6 "1000050"} 225000 "" "" 0
}
#VER A 2 20200212 "Test Bolag AB1" 20201223
{
#TRANS 1930 {} -7549 "" "" 0
#TRANS 2641 {} 1209.75 "" "" 0
#TRANS 7990 {} 6339.25 "" "" 0
}
...
The code I've written now:
lst = r.text.split('}\r\n')
for i in range(len(lst)):
tmpstr1 = str(lst[i])
tmpstr2 = tmpstr1.replace(" ",";")
tmpstr3 = tmpstr2.replace("\\r","")
tmpstr4 = tmpstr3.replace("\\n","")
tmpstr5 = re.sub(r'[^A-Za-z0-9;,#-.]', '', tmpstr4)
tmpstr6 = tmpstr5.replace("#VER;","")
tmplst1 = tmpstr6.split('#TRANS;')
tmpstr7 = str(tmplst1)
tmpstr8 = str(tmplst1[0])
tmpstr9 = tmpstr7.replace(";0",tmpstr8)
tmpstr10 = re.sub(r'[^A-Za-z0-9;,-]', '', tmpstr9)
tmpstr11 = tmpstr10.strip()
tmplst2 = tmpstr11.split(',')
tmplst2.pop(0)
lst[i] = str(tmplst2)
print(lst[200])
This is what I get now:
['3001;6;1000050;-180000;;B;160;20201020;Kundfaktura;Test;Bolag;AB;4117;20201223', '2611;6;1000050;-45000;;B;160;20201020;Kundfaktura;Test;Bolag;AB;4117;20201223', '1510;6;1000050;225000;;B;160;20201020;Kundfaktura;Test;Bolag;AB;4117;20201223']
This is what I want to get:
3001;6;1000050;-180000;;B;160;20201020;Kundfaktura;Test Bolag AB;4117;20201223
2611;6;1000050;-45000;;B;160;20201020;Kundfaktura;Test Bolag AB;4117;20201223
1510;6;1000050;225000;;B;160;20201020;Kundfaktura;Test Bolag AB;4117;20201223
Thanks in advance!
Just save them in arrays of an array.
So you would get every entry in an array, which will make it more dynamically for future use.
For this define an array and append after every for loop.

Bitcoin Transaction Mapping Throws KeyError

I have the following piece of code, which seems to run until line 36 recipientlist.append(target["addr"]) and then throws the error KeyError: 'addr'
However 'addr' seems to be in the data so not sure what the issue is
Can someone please help?
import json
import requests
z = 0
i = 0
firstpart = "https://blockchain.info/rawaddr/"
initialinput = '3PaGEcGDjPsNQHAQ4pTmjQuLXWoEwvnr11'
initialreq = firstpart + initialinput
firstjson = (requests.get(initialreq)).json()
graphvizlines = []
addresslist = []
usedaddresslist = []
addresslist.append(initialinput)
usedaddresslist.append(initialinput)
while i < 6:
if z is 1:
initialreq = firstpart + addresslist[i]
firstjson = (requests.get(initialreq)).json()
for transaction in firstjson["txs"]:
payerlist = []
recipientlist = []
print("\n" + transaction["hash"])
for item in transaction["inputs"]:
payerlist.append(item["prev_out"]["addr"])
if item["prev_out"]["addr"] not in addresslist:
addresslist.append(item["prev_out"]["addr"])
for target in transaction["out"]:
recipientlist.append(target["addr"])
if target["addr"] not in addresslist:
addresslist.append(target["addr"])
for payer in payerlist:
for recipient in recipientlist:
a = '"' + payer + '"' + " -> " + '"' + recipient + '"' + ";"
if a not in graphvizlines:
graphvizlines.append(a)
i = i + 1
z = 1
for t in graphvizlines:
print(t)
While addr is in your data, it's not in every inputs element. Check the very last element in txs, you'll see that inputs is:
"inputs": [
{
"sequence": 0,
"witness": "304402203f872bfd7093fcdad6a3735cbd76f276279890b0304e6f23f54c51388cc2a84402203731d7a7f71265f072f6792c8f4d2e805ff8f86bbfbd0b48a187d573c051593001",
"prev_out": {
"spent": true,
"spending_outpoints": [
{
"tx_index": 0,
"n": 0
}
],
"tx_index": 0,
"type": 0,
"value": 1880609,
"n": 1,
"script": "0014292738ed3f9466f8eedd8c49e5bb013088a7052b"
},
"script": ""
}
],
This element lacks the presence of prev_out.addr.
You will need to first check if the addr element exists or wrap your loop in a try/except.
for transaction in firstjson['txs']:
...
for item in transaction['inputs']:
address = item.get('prev_out').get('addr')
if(address == None):
continue
payerlist.append(address)
...
The above would still fail if prev_out didn't exist, so you should confirm what will be in the result and what might be.

python script to download youtube video

On giving youtube video url, I first download video page and extract javascript object between
<script>var ytplayer = ytplayer ..... </script>
I got
{
"args": {
"is_listed": "1",
"account_playback_token": "QUFFLUhqbWdXR1NfQjRiRmNzWVhRVTM0ajlNcnM1alVUd3xBQ3Jtc0tsVi01WFp5VmV2MTU3RnpkYUVkRzVqR1ZTNUI4T2JaQzk1ckxPejdVNkYzUk5zOTdjZnNmb1BYZHNLQ05nblZZbFk2ZWJXNHRPNVFoNVVNc2RjTE1YekdKSGY4dlVhSnlCU1ctNFZJdXBKbWhIRG1TZw==",
"ptk": "RajshriEntertainment",
"focEnabled": "1",
"tag_for_child_directed": false,
"adaptive_fmts": ......,
"probe_url": .....,
"rmktEnabled": "1",
"allow_ratings": "1",
"dbp": "ChoKFk5RNTV5UGs5bDZmSk5wSjQ4a3RiSHcQARABGAI",
"cc3_module": "1",
"no_get_video_log": "1",
"fmt_list": ......,
"title":..........,
"invideo": true,
"sffb": true,
"iurlmq_webp": ,
"cosver": "10_8_4",
"url_encoded_fmt_stream_map": .................,
"max_dynamic_allocation_ad_tag_length": "2040",
"innertube_api_key": "AIzaSyAO_FJ2SlqU8Q4STEHLGCilw_Y9_11qcW8",
"timestamp": "1446586407",
"cc_asr": "1",
"apiary_host_firstparty": "",
"adsense_video_doc_id": "yt_Vd4iNPuRlx4",
"innertube_context_client_version": "1.20151102",
"mpu": true,
"tmi": "1",
"ldpj": "-19",
"fade_out_duration_milliseconds": "1000",
.........
}
}
i found key adaptive_fmts and url_encoded_fmt_stream_map contain multiple url in percent-encoded form.
i take one url from url_encoded_fmt_stream_map it look like this
https://r1---sn-o3o-qxal.googlevideo.com/videoplayback?
ratebypass=yes&
signature=982E413BBE08CA5801420F9696E0F2ED691B99FA.D666D39D1A0AF066F76F12632A10D3B8076076CE&
lmt=1443906393476832&
expire=1446604919&
fexp=9406983%2C9408710%2C9414764%2C9416126%2C9417707%2C9421410%2C9422596%2C9423663&
itag=22&
dur=128.801&
source=youtube&
upn=pk2CEhVBeFM&
sver=3&
key=yt6&
id=o-AK-OlE5NUsbkp51EZY2yKuz5vsSGofgUvrvTtOrhC72e&
sparams=dur%2Cid%2Cinitcwndbps%2Cip%2Cipbits%2Citag%2Clmt%2Cmime%2Cmm%2Cmn%2Cms%2Cmv%2Cpl%2Cratebypass%2Crequiressl%2Csource%2Cupn%2Cexpire&
mime=video%2Fmp4&
ipbits=0&
pl=21&
ip=x.y.z.a&
initcwndbps=5405000&
requiressl=yes&
mn=sn-o3o-qxal&
mm=31&
ms=au&
mv=m&
mt=1446583222&
itag=22&
type=video/mp4
but when I paste this(above) url in browser nothing happen, I mean not work.
Please help me.
Also
What is difference between adaptive_fmts and url_encoded_fmt_stream_map containing urls?
In python2.7, this works:
import urlparse, urllib2
vid = "vzS1Vkpsi5k"
save_title = "YouTube SpaceX - Booster Number 4 - Thaicom 8 06-06-2016"
url_init = "https://www.youtube.com/get_video_info?video_id=" + vid
resp = urllib2.urlopen(url_init, timeout=10)
data = resp.read()
info = urlparse.parse_qs(data)
title = info['title']
print "length: ", info['length_seconds'][0] + " seconds"
stream_map = info['adaptive_fmts'][0]
vid_info = stream_map.split(",")
mp4_filename = save_title + ".mp4"
for video in vid_info:
item = urlparse.parse_qs(video)
#print 'quality: ', item['quality'][0]
#print 'type: ', item['type'][0]
url_download = item['url'][0]
resp = urllib2.urlopen(url_download)
print resp.headers
length = int(resp.headers['Content-Length'])
my_file = open(mp4_filename, "w+")
done, i = 0, 0
buff = resp.read(1024)
while buff:
my_file.write(buff)
done += 1024
percent = done * 100.0 / length
buff = resp.read(1024)
if not i%1000:
percent = done * 100.0 / length
print str(percent) + "%"
i += 1
break

Extract pattern from sql script

I try to generate output by reading either sql script or shell script in unix box and output file is generated with statement functionality (Create,drop,update,delete,merge,insert) followed by tablename. I try to accomplish this output in a generic way to read any code and generate the output. Can this be achieved using awk programming.
OUTPUT
MERGE|temp_st_rx_wk_str_ip_rpt
SELECT|rx_ov_ord_excep_str_sku
SELECT|ndc
SELECT|fiscal_week
SELECT|store
SELECT|dss_saf_user01.rx_ov_ord_exclu_str
SELECT|rx_osv_invoice_str_ndc
DROP|temp_extract
CREATE|temp_build_extract
SELECT|temp_st_rx_wk_str_ip_rpt
CODE
merge into temp_st_rx_wk_str_ip_rpt s
USING (SELECT b.week_nbr,
b.store_nbr,
SUM (NVL (a.orig_on_ord_qty, 0)) AS mnd_ov_ord_orig_qty,
SUM (NVL (b.inv_qty, 0)) AS mnd_ov_inv_qty
FROM (SELECT /*+ PARALLEL (s,8) */ w.week_nbr, s.store_nbr, s.ndc_nbr,
SUM (s.orig_on_ord_qty) AS orig_on_ord_qty
FROM rx_ov_ord_excep_str_sku s,
ndc n,
fiscal_week w,
store st
WHERE s.ndc_nbr = n.ndc_nbr
AND s.store_nbr = st.store_nbr
AND s.ord_dt BETWEEN w.start_dt AND w.end_dt
AND n.schd_drug_cd NOT IN (''02'', ''07'')
AND n.gen_brand_ind <> ''Y''
AND s.orig_on_ord_qty < 1000 -- Arbitrary value used to exclude bad data
AND w.week_nbr = &P_WEEK_NBR
AND st.area_nbr NOT IN (0, 10, 11)
AND st.pharm_ind = ''Y''
AND s.store_nbr NOT IN
(SELECT store_nbr
FROM dss_saf_user01.rx_ov_ord_exclu_str
WHERE rx_ov_ord_exclu_cd = ''CP'')
GROUP BY w.week_nbr, s.store_nbr, s.ndc_nbr) a,
(SELECT /*+ INDEX (s,RX_OSV_INVOICE_STR_NDC_PK) */
w.week_nbr, s.store_nbr, s.ndc_nbr,
SUM (s.inv_qty) AS inv_qty
FROM rx_osv_invoice_str_ndc s,
ndc n,
store st,
fiscal_week w
WHERE s.ndc_nbr = n.ndc_nbr
AND s.store_nbr = st.store_nbr
AND s.ord_dt BETWEEN w.start_dt AND w.end_dt
AND s.ord_type_cd <> ''F''
AND n.schd_drug_cd NOT IN (''02'', ''07'')
AND n.gen_brand_ind <> ''Y''
AND s.inv_qty > 0
AND w.week_nbr = &P_WEEK_NBR
AND st.area_nbr NOT IN (0, 10, 11)
AND st.pharm_ind = ''Y''
AND s.store_nbr NOT IN
(SELECT store_nbr
FROM dss_saf_user01.rx_ov_ord_exclu_str
WHERE rx_ov_ord_exclu_cd = ''CP'')
GROUP BY w.week_nbr, s.store_nbr, s.ndc_nbr) b
WHERE a.week_nbr (+) = b.week_nbr
AND a.store_nbr (+) = b.store_nbr
AND a.ndc_nbr (+) = b.ndc_nbr
GROUP BY b.week_nbr, b.store_nbr) t
ON (t.week_nbr = s.week_nbr
AND t.store_nbr = s.store_nbr)
WHEN NOT MATCHED
THEN
INSERT (week_nbr, store_nbr, mnd_ov_ord_orig_qty, mnd_ov_inv_qty)
VALUES (t.week_nbr, t.store_nbr, t.mnd_ov_ord_orig_qty, t.mnd_ov_inv_qty)
WHEN MATCHED
THEN
UPDATE SET
s.mnd_ov_ord_orig_qty = t.mnd_ov_ord_orig_qty,
s.mnd_ov_inv_qty = t.mnd_ov_inv_qty';
commit;
drop table temp_extract;
create table temp_build_extract as select * from temp_st_rx_wk_Str_ip_rpt;
You can try:
awk -f e.awk input.txt
where input.txt is your input file (CODE), and e.awk is:
/^merge / {
if (match($0,/merge into ([^[:blank:]]+)/,a)) {
print "MERGE|"a[1]
next
}
}
/FROM [^(]/ {
getFromTabs()
if (match(from,/FROM ([^[:blank:]]+)/,a)) {
printKey(a[1])
do {
ind=index(from,",")
if (ind) {
from=substr(from,ind+1)
match(from,/[[:space:]]*([[:alnum:]]+)/,a)
printKey(a[1])
}
}
while (ind)
}
}
/^drop/ {
if (match($0,/drop table ([^[:blank:]]+)/,a)) {
print "DROP|"a[1]
next
}
}
/^create/ {
if (match($0,/create table ([^[:blank:]]+)/,a)) {
print "CREATE|"a[1]
}
if (match($0,/select.*[[:blank:]]([^[:blank:]]+);/,a)) {
print "SELECT|"a[1]
}
}
function printKey(key) {
if (!(key in T)) {
print "SELECT|"key
T[key]++
}
}
function getFromTabs(p) {
p=0
from=""
do {
from=(p++==0)?$0:(from ORS $0)
getline
}
while (!/WHERE/)
}
For your sample code above this produces output:
MERGE|temp_st_rx_wk_str_ip_rpt
SELECT|rx_ov_ord_excep_str_sku
SELECT|ndc
SELECT|fiscal
SELECT|store
SELECT|dss_saf_user01.rx_ov_ord_exclu_str
SELECT|rx_osv_invoice_str_ndc
DROP|temp_extract;
CREATE|temp_build_extract
SELECT|temp_st_rx_wk_Str_ip_rpt
(Note that I know nothing about SQL, so you must check if this looks ok to you.)

Categories