Tycoon Talk
Become a Big fish!
The number 1 forum for online business!
Post topics, ask questions, share your knowledge.
Tycoon Talk is part of Freelancer.com - find skilled workers online at a fraction of the cost.

PHP Forum


You are currently viewing our PHP Forum as a guest. Please register to participate.
Login



Freelance Jobs

Reply
Translation of python to php for a app
Old 08-09-2011, 10:02 AM Translation of python to php for a app
Junior Talker

Posts: 1
Trades: 0
i have here a python script..
what it does is check for a specific parts of a text file

well, if you know python, i bet you could get what it does

so here is the .py code

Code:
#!/usr/bin/env python

# counters.py script for TR
# usage: counters.py filename [att/def/both]
# filename = file containing copy of guild battle log
# 'att/def/both' = sort counters by attacker or defender
# 'both' shows both, apparently. default is 'att'
# written by Laanders, feel free to share and improve
# Akima - added html outputting


import string
import sys
import os

# input

if len(sys.argv) < 2:
    print "Not enough arguments, quitting."
    quit()

if len(sys.argv) > 3:
    print "Too many arguments, quitting."
    quit()

print "Reading from input file: %s \n" % sys.argv[1]

file=open(sys.argv[1],'r')

try:
    outarg=sys.argv[2]
except IndexError:
    outarg='att'

if outarg == 'def': outtype=1
if outarg == 'att': outtype=2
if outarg == 'both': outtype=3

#try:
#    listarg=sys.argv[2]
#    file=open(sys.argv[2],'r')
#except IndexError:
#    listarg=False

# reading in

# enemy checking
check_enemies=True
show_type=2

if os.path.isfile('enemies.lst'):
    check_enemies=True
    enemies=[]
    efile=open('enemies.lst','r')
    for line in efile:
        if '(#' in line:
            lspl=line.split()
            ename=lspl[1]
            eid=lspl[2]
            emage=ename+' '+eid
            enemies.append(emage)
            

cnt={}
cnt2={}
lastline=' '

for line in file:
    if 'sieged' in lastline or 'attacked' in lastline or ') pillaged' in lastline:
        lspl=lastline.split()

        name=lspl[3]
        mid=lspl[4].split('\'')[0]
        defender=name+' '+mid

    name=lspl[0]
        mid=lspl[1]
    attacker=name+' '+mid
        
    if ') pillaged' in lastline:
      if len(line) < 5:        # break loop if line=blank (block, nothing killed)
        lastline=line
        continue
      nspl=line.split()
      if nspl[2]=='killed':        # break loop if blocked
        lastline=line
        continue

        cnt.setdefault(defender,[]).append(attacker)
        cnt2.setdefault(attacker,[]).append(defender)
        
    lastline=line


# condensing data

# sort by attacker

if outtype==2 or outtype==3:

    print "<p><br>-----------------------------------<br>"
    print 'Active counters sorted by ATTACKER:<br>'
    print "-----------------------------------<br><br>"

    for i in cnt2.iterkeys():
        cond={}
        for j in cnt2[i]:
            if cond.has_key(j):
                cond[j]=cond[j]+1
            else: cond.setdefault(j,1)
        print '<br><strong>\n</strong>','&lt;b&gt;', i ,'&lt;/b&gt;','has given counters to:<br>'
        for k,v in cond.iteritems():
            vstr=str(v)
            print k+' ('+vstr+'x)<br>'

    print 

# sort by defender

if outtype==1 or outtype==3:

    print "</p><p><br>-----------------------------------<br>"
    print 'Active counters sorted by DEFENDER:<br>'
    print "-----------------------------------<br><br>"

    for i in cnt.iterkeys():
        cond={}
        for j in cnt[i]:
            if cond.has_key(j):
                cond[j]=cond[j]+1
            else: cond.setdefault(j,1)
        print '<br><strong>\n</strong>',i,'has counters on:<br>'
        for k,v in cond.iteritems():
            vstr=str(v)
            print k+' ('+vstr+'x)<br>'
    print

print "-----------------------------------<br><br>"


#for k,v in cnt.iteritems():
#    print '\n',k,'has counters on:'
#    for j in v:
#        print j
filename is counters.py
to run it, you use counters.py br.txt > counters.html

it creates an html doc

what i want is, instead of importing a external file
there will be a text area where the contents of the br.txt
will be pasted

also, the enemy.lst file
instead of importing it, the list will be pasted to a 2nd text area..

love you guys..i hope you can work it out
iamspix is offline
Reply With Quote
View Public Profile
 
 
Register now for full access!
Reply     « Reply to Translation of python to php for a app
 

Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are Off
Pingbacks are Off
Refbacks are Off





   
RSS Feed  Feeds: RSS   JS   XML
RSS Feed  Feeds for this forum: RSS   JS   XML



Page generated in 0.08269 seconds with 12 queries