Welcome, Guest

Author Topic: Coding  (Read 179936 times)

vh

  • formerly mudkipz
  • *****
  • Posts: 1138
  • "giving heat meaning"
Re: Python
« Reply #270 on: September 22, 2013, 12:41:02 PM »
that indenting is screwy, use code

and....it'd help if you also typed out the error message

Darvince

  • *****
  • Posts: 1835
  • 差不多
Re: Python
« Reply #271 on: September 25, 2013, 10:47:35 PM »
if usforum had karma i would give you about 100 karma.

vh

  • formerly mudkipz
  • *****
  • Posts: 1138
  • "giving heat meaning"
Re: Python
« Reply #272 on: September 29, 2013, 10:29:50 AM »
PYPY is fast

« Last Edit: September 29, 2013, 01:06:37 PM by vh »

tuto99

  • *****
  • Posts: 533
  • Baba Booey
Re: Python
« Reply #273 on: September 29, 2013, 02:58:53 PM »
Uhhhhhhhhhhhhhh.......................................... what

Darvince

  • *****
  • Posts: 1835
  • 差不多
Re: Python
« Reply #274 on: September 29, 2013, 03:00:32 PM »
wtf is pypy

blotz

  • Formerly 'bong'
  • *****
  • Posts: 813
  • op pls
Re: Python
« Reply #275 on: September 29, 2013, 03:10:40 PM »
it speeds up pygmc

vh

  • formerly mudkipz
  • *****
  • Posts: 1138
  • "giving heat meaning"
Re: Python
« Reply #276 on: September 29, 2013, 03:20:47 PM »
wtf is pypy

pypy is like an implementation of python but faster. there's a complicated explanation but all you need to know is that it's faster for most things

blotz

  • Formerly 'bong'
  • *****
  • Posts: 813
  • op pls
Re: Python
« Reply #277 on: September 29, 2013, 03:26:43 PM »
i want complicated explanation

vh

  • formerly mudkipz
  • *****
  • Posts: 1138
  • "giving heat meaning"
Re: Python
« Reply #278 on: October 09, 2013, 05:55:49 PM »
so i've got this idea and if atomic or someone thinks its promising i'll code it

basically you have two 'swarms' of instances which 'battle' with each other until one side wins

each instance, or bot, in the swarm has
memory
processing power
scanners
energy
communications
energy transfer
energy weapons

each player will give a set of instructions (code) to the bots to do stuff like accelerate in a direction, transfer energy, communicate information, compute, scan for enemies, store information, etc. of course, there are several limitations that the player must follow

each bot will have an attribute memory, which is a list. so for example, in your code, you can't say "v = 40", instead, you have to store it as bot.memory[0] = 40. if the memory exceeds the requirements, it'll be overwritten/wiped.

another restriction is processing power. i haven't figured out how to regulate this yet, but basically each bot has a limited amount of power so it can't run expensive algorithms and shit.

there are also communications, for sending from 1-10kb of information. very helpful if you can't process the data fast enough, you can have a bot scan and send the information to other bots for processing.

the rest of the things are pretty much self explanatory. you use energy to attack, accelerate, maybe some other things. there's also damage, which goes from 0 to 1 and at 1 the bot is destroyed. ability of the bot to function is determined by (1-d)^0.2 where d is damage.

to add more interesting things, i thought bots might be able to specialize. for example, there could be group of computation bots and a group of scanning bots which work together better than bots which do both scanning and computations. so you can allocate the abilities of each bot towards a particular function.

distance affects communications, energy transfer, and weapon damage

so, basically, competitors of players will create a short bit of code (probably limited to 100 or 200 lines) in order to compete with the enemy swarm. they'll be given a list of functions to use like scan(), accelerate(direction), senddata(id), and etc to use.

atomic7732

  • Global Moderator
  • *****
  • Posts: 3828
  • caught in the river turning blue
    • Paladin of Storms
Re: Python
« Reply #279 on: October 09, 2013, 08:41:17 PM »
this sounds like it'd be a good flash game thing

idk how you're planning to do it but if it was like programming 10 python things at once that sounds like it could be overwhelming

vh

  • formerly mudkipz
  • *****
  • Posts: 1138
  • "giving heat meaning"
Re: Python
« Reply #280 on: October 10, 2013, 11:45:23 AM »
class BOT

like that.

vh

  • formerly mudkipz
  • *****
  • Posts: 1138
  • "giving heat meaning"
Re: Python
« Reply #281 on: October 12, 2013, 04:01:05 AM »
short script, the hardest part was getting the memory to clear itself, which i solved by defining the thingy as a function

basically you download a few of these files from http://nsarchive.net/, don't unzip them.
then put the processor in the same directory as the files and run. it should take a bit over a minute per file and happily eat 2gbs of ram. when it's done you'll have several 100 MB files sitting in your folder that contains the attributes of every single nation in ns for that time period

so i can load each file and say

Code: [Select]
mainstats = []
for nation in thingy:
    mainstats.append(sum(nation.freedomscores))
count = 0
for 300 in mainstats:
    count += 1
print count
to find out how many nations have 100 civil rights, 100 economy, and 100 political freedom. no screwing around with xml files, parsing, and stuff because i've already done that

 
« Last Edit: October 12, 2013, 04:39:35 AM by vh »

Bla

  • Global Moderator
  • *****
  • Posts: 1013
  • The stars died so you can live.
Re: Python
« Reply #282 on: October 12, 2013, 04:04:48 AM »
Nice, do you know if that archive also stores all the detailed statistics like income equality and stuff?

vh

  • formerly mudkipz
  • *****
  • Posts: 1138
  • "giving heat meaning"
Re: Python
« Reply #283 on: October 12, 2013, 04:12:14 AM »
well it stores the social equality budget for government. but no, it doesn't have the 69 census measures, you can only get those by the api and they're not archived. actually the daily dumps aren't archived by default either, this is just a player-made database

vh

  • formerly mudkipz
  • *****
  • Posts: 1138
  • "giving heat meaning"
Re: Python
« Reply #284 on: October 13, 2013, 11:37:47 PM »
for dar

Darvince

  • *****
  • Posts: 1835
  • 差不多
Re: Python
« Reply #285 on: October 13, 2013, 11:40:07 PM »
i thank

vh

  • formerly mudkipz
  • *****
  • Posts: 1138
  • "giving heat meaning"
Re: Python
« Reply #286 on: October 13, 2013, 11:59:23 PM »
adsf

vh

  • formerly mudkipz
  • *****
  • Posts: 1138
  • "giving heat meaning"
Re: Python
« Reply #287 on: October 17, 2013, 04:03:40 PM »
rough pseudocode that will attempt to weave together chatlogs

Code: [Select]
import os
import glob

filelist = ['kipulog.txt', 'atoulog.txt', 'darulog.txt']
combcount = 0
comblist = []

#gets number of each file
def getmax(f):
    m = [[],[],[]]
    for x in range(0,3):
        for file in glob.glob('*'+f[x]):
            print file
            if str.isdigit(file[:2]):
                m[x].append(int(file[:2]))
            else:
                m[x].append(int(file[0]))
        m[x] = max(m[x]) #get all file lengths
        return m

class SplitFile
    filemax = getmax(filelist)
    def __init__(self, name):
        self.name = name
        self.index = 0
    def open(self):
        #f = open(self.index + self.filename, 'r+')
        #open self.index
    def close(self, name):
        #write f into open(self.index + name, 'w')
        #f.close()
    def readline(self):
        #f.readline()
    def getnext(self):
        #m = readline
        #if m == ''
        #close(name)
        #self.index += 1
        #if self.index != max:
            #open()
            #m = readline()
            #return m
            #readline, else close, increment, open, read.
        #else
            #return 0 if end of file
    def within(self, time, length):
        #open current file, before, and after
        #build a list with all cases
        #find first, and find last that fit
        #close the files
        #return them all inbetween
        #return all entries within length of time

def search(object, maxtime, loe): #loe is list of entries
    #a more efficient search than the one presented can probably be accomplished
    #for item in loe:
        #if object.alias = item.alias and object.text = item.text and abs(object.time-item.time) < maxtime:
            #return index? or item? or just modify item?
   

def offsetscan(tlist):
    #initiate instances of splitfile for all three threads in tlist
    #for each thread:
        #while 1:
            #l = get next until length > LONG:
            #l2 = search(l, MAXTIME, log)
            #find l.time- l2.time
            #assign that value to offset
            #assign offset to l.offset
            #break at eof (0).
    #modifies tlist

def poffset(thread, ref):
    #for item in thread:
        #item.time += item.offset[ref]
    #clears offset parameter and makes it zero, shifting the time in the process

def poff(tlist)
    cthread = 'kipulog.txt' #official time thread
    #kipulog already synchronized
    #poffset(atoulog, kipulog)
    #poffset(darulog, kipulog)
    #use poffset to synchronize all times with cthread

def getfirst(threadlist):
    #initialize t as [0, 0, 0]
    #for each thread:
        #t[x] = thread[x].time
    #i = t.find(max(t))
    #return i

def combadd(item,comblist):
    #if comblist is short:
        #comblist.append(item)
    #if comblist is long:
        #write to file
        #increment filename by one
        #comblist = []
   
def weave(tlist): #tlist must be poffset
    '''
    combines all threads in tlist into a single thread
    '''
    f = getfirst(tlist) #returns index of first thread
    #while 1:
        #n = f.getnext()
        #for two other logs:
            #j = search(log.within(n), SHORT, log)
            #m = log.getnext()
            #if j:
                #j.incorp = 1
                #n.incorp = 1
            #if j != m:
                #f = log
                #break
            #else:
                #j.incorp = 1
            #combadd(j)
        #if eof:
            #break
    #end
       
   

vh

  • formerly mudkipz
  • *****
  • Posts: 1138
  • "giving heat meaning"
Re: Python
« Reply #288 on: October 20, 2013, 06:45:56 PM »
model for ubv where ubv.py and engine.py are run simultaneously off two command prompts

simultaneously:
ubv.py reads in text from next.txt and sends it to the channel
ubv.py reads in text from freenode and writes it in rec.txt

and

engine.py reads in text from rec.txt as a list, processes the first element, and deleted it
engine.py takes the result of the processing and writes it back into next.txt

this way, even if processing takes a minute or two, ubv.py will continue pinging and just check next.txt to see if engine.py and come up with an answer. it also neatly encapsulates the processing into a single function

vh

  • formerly mudkipz
  • *****
  • Posts: 1138
  • "giving heat meaning"
Re: Python
« Reply #289 on: October 22, 2013, 07:24:34 PM »
Code: [Select]
import Image
import numpy as np
import cPickle
from operator import itemgetter


hdist = 60 #distance between each day

aliasdict = {}#connects aliases to numbers
colordict = {
    1:np.array([255,0,255]),#
    2:np.array([255,128,128]),#
    3:np.array([128,255,128]),#
    4:np.array([255,255,0]),#
    5:np.array([128,128,255]),#
    6:np.array([0,255,255]),#
    7:np.array([255,128,128]),#
    9:np.array([255,64,192]),#
    10:np.array([255,192,64]),#
    11:np.array([170,170,170])#
    }
   

biglist = cPickle.load(#whatever)

biglist.sort(key=operator.itemgetter(0)) #sort by time

#get alias most used
aliaslist = {}
for line in biglist:
    b = line[1]
    if b not in aliaslist.keys():
        aliaslist[b] = 1
    else:
        aliaslist[b] += 1
#sort by value

aliaslist = list(reversed(sorted(aliaslist.iteritems(), key = operator.itemgetter(1))))#now a list

for x in xrange(0,10):
    aliasdict[aliaslist[x]] = x+1

print aliasdict

def additivepaste(canvas, article, c, r):
    for x in xrange(0,len(article)):
        for y in xrange(0,len(article[0])):
            canvas[x+c][y+r] += article[x][y]

ls = 25

def j2u(j):
    return (j-2440587.5)*86400.0+ls

def u2j(u):
    return ((u-ls)/86400.0)+2440587.5

class Day:
    mlen = 460
    mtime = 10
    duration = 6
    i = Day.mlen/2
    aliascount = 13
    def __init__(self, julian, start, lines=[])
        self.julian = julian
        self.start = start
        self.lines = lines
        self.total = 0
        for line in lines:
            self.total += len(line[2])
        self.map = numpy.zeroes([Day.mlen+1,90000/Day.mtime,Day.aliascount])
        self.article = numpy.zeroes([Day.mlen+1,90000/Day.mtime], dtype=(float,3))
    def addline(self, line):
        c = aliasdict[line[1]]
        #assume odd num
        l = len(line[2])
        for x in xrange(i-l/2, i+l/2):
            t = (line[0]-start)/10
            for y in xrange(t, t+Day.duration):
                self.map[x][y][c] += 1.0/(y-t+1.0) #fades as it ascends
    def makearticle(self):
        for x in xrange(0,len(self.article)):
            for y in xrange(0,len(self.article[0])):
                for z in xrange(0,Day.aliascount):
                    self.article[x][y] += colordict[self.map[x][y][z]]
    def contrain(self):
        for x in xrange(0,len(self.article)):
            for y in xrange(0,len(self.article[0])):
                self.article[x][y] = list(reversed(sorted(self.article[x][y])))
                overflow = 0
                for c in xrange(0,3):
                    d = self.article[c]-255
                    if d > 255:
                        overflow += d
                    elif overflow > 0:
                        if overflow > -d:
                            self.article[c] = 255
                            overflow -= d
                        else:
                            self.article[c] += overflow
                            overflow = 0
                    else:
                        break

daylist = []
#add days
for line in biglist:
    j = int(u2j(line[0]))
    if daylist[-1].julian == j:
        daylist[-1].lines.append(line)
    else:
        daylist.apppend(Day(j, j2u(j), [line])

       
days = int((llist[-1][0]-llist[0][0])/86400)+2

offset = 1000
voffset = 500
canvas = np.zeroes([days*hdist+1000, 90000/Day.mtime], dtype = (float,3))

for day in daylist:
    additivepaste(canvas, day, day.julian*hdist+offset, voffset)

#contrain to 255
for x in xrange(0,len(canvas)):
    for y in xrange(0,len(canvas[0])):
        for c in xrange(0,3):
            if canvas[x][y][c] > 255:
                canvas[x][y][c] = 255

#convert to image
img = Image.fromarray(canvas, 'RGB')
img.save('irc.png')

code to convert irc logs into an visualization. the only parts thats uncompleted is the part where i load in the data. that'll have to wait until i complete the code from two post above about merging the logs

blotz

  • Formerly 'bong'
  • *****
  • Posts: 813
  • op pls
Re: Python
« Reply #290 on: October 23, 2013, 11:52:08 AM »
what's a visualization? like how do you see the text in a log other than opening a txt file?

vh

  • formerly mudkipz
  • *****
  • Posts: 1138
  • "giving heat meaning"
Re: Python
« Reply #291 on: November 01, 2013, 11:53:08 PM »
code to download the zday data from ns api.

Code: [Select]
import urllib2
import time

def getevent(str):
    str = str[19:str.find("\n</HAPP")]
    limstart = str.rfind('EVENT id="')+10
    limend = str[limstart:].find('"')+limstart
    return str, int(str[limstart:limend])
zurl = 'http://www.nationstates.net/cgi-bin/api.cgi?q=happenings;view=world;filter=zombie;beforeid='
lim = 10000000
#header
zxml = []
zxml.append('<WORLD><HAPPENINGS>')
while 1:
    print lim, len(zxml)
    c,lim = getevent(urllib2.urlopen(zurl+str(lim)).read())
    if c.find('EVENT') != -1:
        zxml.append(c)
    else:
        break
    time.sleep(0.65)
   
zxml.append('\n</HAPPENINGS></WORLD>\n')
#add tail

t = ''.join(zxml)
#save file
with open('nsxml', 'w') as output:
    output.write(t)

i'm just posting this here as reference if anyone wants to do the same with similar things

vh

  • formerly mudkipz
  • *****
  • Posts: 1138
  • "giving heat meaning"
Re: Python
« Reply #292 on: November 06, 2013, 10:48:50 PM »
this takes a graphml file and moves the distant points closer to the center.

it's quite impressive what can be done in 12 lines

Code: [Select]
import networkx as nx, random
N = nx.read_graphml('zdaybmig.graphml')
xoff, yoff = 1000.0, 500.0
rparam, maxdist2 = 3.0,16000.0**2
K = N.nodes()
for x in xrange(0,len(K)):
    n = N.node[K[x]]
    while float(n['x']+xoff)**2+float(n['y']+yoff)**2 > maxdist2:
        j = random.random()*(rparam-1)+1
        n['x'] /= j
        n['y'] /= j
nx.write_graphml(N,'zdaymig.graphml')

Bla

  • Global Moderator
  • *****
  • Posts: 1013
  • The stars died so you can live.
Re: Python
« Reply #293 on: November 24, 2013, 04:49:28 AM »
When running it, I get this error:

Code: [Select]
Traceback (most recent call last):
  File "C:\Users\Mikkel\Desktop\guitest.py", line 6, in <module>
    import matplotlib
ImportError: No module named matplotlib

vh

  • formerly mudkipz
  • *****
  • Posts: 1138
  • "giving heat meaning"
Re: Python
« Reply #294 on: November 24, 2013, 05:56:19 AM »
right. you'll need matplotlib


Bla

  • Global Moderator
  • *****
  • Posts: 1013
  • The stars died so you can live.
Re: Python
« Reply #295 on: November 24, 2013, 07:29:19 AM »
I installed it to the default subfolder
Lib\site-packages
but it still gives the exact same error. :l

vh

  • formerly mudkipz
  • *****
  • Posts: 1138
  • "giving heat meaning"
Re: Python
« Reply #296 on: November 24, 2013, 09:32:07 AM »
have you tried the binary installers?

Bla

  • Global Moderator
  • *****
  • Posts: 1013
  • The stars died so you can live.
Re: Python
« Reply #297 on: November 24, 2013, 09:44:45 AM »
I ran the
matplotlib-1.3.1.win-amd64-py2.7.exe
I also tried downloading the .rar and running setup.py inside it, still not working. I don't really have patience to read and follow long texts on several different ways to install it and remove it where most of them probably won't work.

If the binary file is the .dmg file, no, Idk how to use that.

vh

  • formerly mudkipz
  • *****
  • Posts: 1138
  • "giving heat meaning"
Re: Python
« Reply #298 on: November 24, 2013, 10:44:30 AM »
i don't know how familiar you are with setup.py, but you ran it with a command like
python setup.py install
?

also i too have problems installing with modules and sometimes it works when i install it in /Lib instead of /Lib/site-packages. maybe you can copy/paste the thingies and try again

if you have pip or easy_install installed, you might try something like
easy_install matplotlib
(on command line)

vh

  • formerly mudkipz
  • *****
  • Posts: 1138
  • "giving heat meaning"
Re: Python
« Reply #299 on: November 24, 2013, 07:49:55 PM »
i've got an interesting problem i thought we could have an impromptu coding contest on. java, python, or whatever language. (too bad there's no general coding topic.)

anyways it was inspired by darvince:
<Darvince>tell him the amount of correct numbers he got
<Darvince>don't tell him in what order though

basically, a number will be generated, and the algorithm must guess the number. the test() function will return True if the correct answer is guessed and an integer containing the number of digits correct will be returned if it was the wrong number.

some numbers will be of specified lengths and some others will have unspecified lengths (<1000).

fastest algorithm wins

if anyone's interested, post so that i know i'm not the only person working on this