Simplify re.sub commands

This is the place for queries that don't fit in any of the other categories.

Simplify re.sub commands

Postby DevynCJohnson » Sat Jun 01, 2013 12:36 pm

How can I simplify and optimize this ugly mess? I am using Python3.3.

Warning:
You before seeing code: :D
You after seeing my mess: :o

I have the code here as an attachment because the server seems to have problems with my code, even in the code tags.
Attachments
python_help.txt
(4.36 KiB) Downloaded 37 times
Ubuntu 13.04 | Python3.3
User avatar
DevynCJohnson
 
Posts: 21
Joined: Sat May 25, 2013 1:53 pm

Re: Simplify re.sub commands

Postby Mekire » Sat Jun 01, 2013 1:50 pm

Do you not understand what a for loop is? Why do you have the same line over and over with only a single character different?
And why is all your code on one line separated by semi-colons >.>

Code: Select all
wtf = "all your random characters"

for char in wtf:
    DATA = re.sub('<prn>(.*){}(.*)</prn>'.format(char), r'<prn>\1*\2</prn>', DATA, flags=re.I)

And that is assuming your code actually does what you want in the first place.

-Mek
User avatar
Mekire
 
Posts: 814
Joined: Thu Feb 07, 2013 11:33 pm
Location: Amakusa, Japan

Re: Simplify re.sub commands

Postby DevynCJohnson » Sat Jun 01, 2013 2:13 pm

Thank you Mekire. I never thought of using a for construct with a find a replace. I like how you name variables. With my coding, I like to keep similar tasks on the same line, so everything that deals with these star characters will be on one line. I know, I am weird. Thank you again.
Ubuntu 13.04 | Python3.3
User avatar
DevynCJohnson
 
Posts: 21
Joined: Sat May 25, 2013 1:53 pm

Re: Simplify re.sub commands

Postby Mekire » Sat Jun 01, 2013 2:20 pm

Ok. Ignoring advice on star imports is one thing. But if you really think it is ok to have a 4300 character line there is either something truly wrong, or you are a troll. No one is going to help you if you keep this up.

Trolling (apologies but trolling restricted to python 2.7):
Code: Select all
while 1:from pygame import*;import random as s,os,sys;u,v,d,h,k=display,event,globals,range,enumerate;(z,J,S,B,p,w)=(init(),20,u.set_mode((200,400)),Surface((240,500)),0,[0]*3)if'q'not in d()else(z,J,S,B,p,w);g,f=S.blit,B.fill;(z,z,z,z,K,X)=(B.fill(0xFFFFFF),B.fill(0,(20,0,200,480)),S.blit(B,(-20,-80)),time.set_timer(24,200),(0xFF0000,0x3200FF,0xFF00,0xFFFF00,0xFFFF,0x9B9B9B,0xC800FF),[[bin(int(Q,36))[2:].zfill(64)[i*16:i*16+16:]for i in h(4)]for Q in'16b82uhz95u 7up5qtbapuu 3jjam8ic84i 8p7d0riv4g4 8bdcy9qax5u f8sajq6gpd0 304x2ozzgpu'.split()])if'q'not in d()else(z,z,z,z,K,X);N={273:(0,1),276:(-1,0),275:(1,0)};q=s.randint(0,6)if not w[2]else q;(b,c,V,r)=([[(D%4,D//4)for D,F in k(Q)if F=='1']for Q in X[q]],K[q],B.copy(),0)if not w[2]else(b,c,V,r);w=[4,0,1]if not w[2]else w;m=w[:];o=b[r];n=list(v.get());n.append(v.Event(25))if not n else 0;u.flip();a=[([w[0],w[1]+1,1]if e.type==24 and all(V.get_at(((i+w[0])*J,(j+w[1]+1)*J))==(0,0,0,255)for i,j in b[r])else[w[0],w[1],0]if e.type==24 and any(V.get_at(((i+w[0])*J,(j+w[1]+1)*J))!=(0,0,0,255)for i,j in b[r])else(quit(),sys.exit())if e.type==12 or(e.type==2 and e.key==27)else w,(w[0]+N.get(e.key,[0,0])[0],(r+N.get(e.key,[0,0])[1])%4)if e.type==2 and all(V.get_at(((i+w[0]+N.get(e.key,[0,0])[0])*J,(j+w[1])*J))==(0,0,0,255)for i,j in b[(r+N.get(e.key,[0,0])[1])%4])else(w[0],r))for t,e in k(n)];w,w[0],r=a[-1][0],a[-1][1][0],a[-1][1][1];[(f(0,(((m[0]+i)*J,(m[1]+j)*J),(J,J))),f(c,(((w[0]+x)*J,(w[1]+y)*J),(J,J))),g(B,(-20,-80)))for(i,j),(x,y)in zip(o,b[r])if t==len(n)-1];[(B.blit(S,(20,100),(0,0,200,H*J)),f(0,(20,0,200,100)),f(0xFFFFFF,(0,480,240,20)),g(B,(-20,-80)))for H in h(0,20)if not w[2]and t==len(n)-1 and all(S.get_at((W*J,H*J))!=(0,0,0,255)for W in h(0,10))]


-Mek
User avatar
Mekire
 
Posts: 814
Joined: Thu Feb 07, 2013 11:33 pm
Location: Amakusa, Japan

Re: Simplify re.sub commands

Postby micseydel » Sat Jun 01, 2013 10:02 pm

From what source did you learn Python?
Join the #python-forum IRC channel on irc.freenode.net!
User avatar
micseydel
 
Posts: 923
Joined: Tue Feb 12, 2013 2:18 am
Location: Mountain View, CA

Re: Simplify re.sub commands

Postby DevynCJohnson » Sun Jun 02, 2013 1:53 pm

Why do you ask? Are my programming skills that bad? :lol:
Ubuntu 13.04 | Python3.3
User avatar
DevynCJohnson
 
Posts: 21
Joined: Sat May 25, 2013 1:53 pm


Return to General Coding Help

Who is online

Users browsing this forum: Cereal and 5 guests