Welcome, Guest

Author Topic: Coding  (Read 279520 times)

atomic7732

  • Global Moderator
  • *****
  • Posts: 3849
  • caught in the river turning blue
    • Paladin of Storms
Re: Python
« Reply #90 on: November 14, 2012, 06:07:04 PM »
Code: [Select]
import math
#http://www.onlineconversion.com/julian_date.htm

curtime = raw_input("Current Julian Date:")
curtime = float(curtime.strip("\n"))
we1399 = 2232023.896169

year = (curtime-we1399)/365.256363004
day = (year-math.floor(year))*365 #actual days in the year, 616 being the year with 366
grade1 = (day-math.floor(day))*100
grade2 = (grade1-math.floor(grade1))*1000

print "%d.%d %d %d" % (math.floor(year), math.floor(day), math.floor(grade1), math.floor(grade2)) #example 613.328 85 417

Soleani calendar date and time calculator

at the time of this post it was 613.320 27 520
« Last Edit: November 14, 2012, 06:11:08 PM by atomic7732 »

vh

  • formerly mudkipz
  • *****
  • Posts: 1140
  • "giving heat meaning"
Re: Python
« Reply #91 on: November 14, 2012, 07:35:24 PM »
ok now make it take take time.time() to find the current soleani date

atomic7732

  • Global Moderator
  • *****
  • Posts: 3849
  • caught in the river turning blue
    • Paladin of Storms
Re: Python
« Reply #92 on: November 14, 2012, 07:59:51 PM »
I prefer to use Julian Date... because it's consistent... It didn't jump 11 days like the Gregorian.

unless time.time() is a JD or Unix time?

vh

  • formerly mudkipz
  • *****
  • Posts: 1140
  • "giving heat meaning"
Re: Python
« Reply #93 on: November 14, 2012, 08:01:13 PM »
yeah unix time.

vh

  • formerly mudkipz
  • *****
  • Posts: 1140
  • "giving heat meaning"
Re: Python
« Reply #94 on: November 14, 2012, 08:01:39 PM »
oh unix time doesn't count leap seconds this makes things to much simpler

atomic7732

  • Global Moderator
  • *****
  • Posts: 3849
  • caught in the river turning blue
    • Paladin of Storms
Re: Python
« Reply #95 on: November 14, 2012, 08:04:08 PM »
well then if you want that I'll have to do a calculation of JD at the UNIX epoch

and then add a certain amount of Soleani years and that's kind of lame actually.

vh

  • formerly mudkipz
  • *****
  • Posts: 1140
  • "giving heat meaning"
Re: Python
« Reply #96 on: November 14, 2012, 08:05:42 PM »
well i do want it and if you won't do it i will when i have time in the far future :P

vh

  • formerly mudkipz
  • *****
  • Posts: 1140
  • "giving heat meaning"
Re: Python
« Reply #97 on: November 18, 2012, 05:20:59 PM »
this will generate a white png file, given the size you want it to be. the goal is to make a completely text based image editor
(jk)

oh you need the pypng module

import png
n = int(input('columns?'))
y = int(input('rows?'))
pixels = [[] for x in xrange(0,n)]
for x in xrange(0,y):
    for list in pixels:
        list.append(255)
png.from_array(pixels, 'L').save('image.png')

vh

  • formerly mudkipz
  • *****
  • Posts: 1140
  • "giving heat meaning"
Re: Python
« Reply #98 on: November 23, 2012, 12:12:04 PM »
i've written something that will generate cryptographically strong prime numbers, generated from random noise and entropy from your computer

(if anyone wants me to make this print the primes to a file i'll do that too because theres's no copy in the python terminal)

a note about accuracy: an input of n will yield a false positive of a prime once in n^4. for example a value of 10 will yield once mistake per 10,000 trials. an input of 1000 will yield a mistake in 1 trillion trials.

average times to generate and print primes with an accuracy of 10 on a laptop: (times in miliseconds)
length 10: 2.5
length 50: 70
length 100: 500
length 200: 5400


YOU don't need any additional modules just python 2.7 to run this

vh

  • formerly mudkipz
  • *****
  • Posts: 1140
  • "giving heat meaning"
Re: Python
« Reply #99 on: November 27, 2012, 07:27:52 PM »
with the attached file, figure out to add the text in all the population tags IF and only if the nation is in a certain region

notes:
derp = open('derp.xml', 'r')
derp = etree.parse(derp)
derp = derp.getroot()
print derp[n][n].text #where n is index to extract text from given location

vh

  • formerly mudkipz
  • *****
  • Posts: 1140
  • "giving heat meaning"
Re: Python
« Reply #100 on: November 27, 2012, 07:52:17 PM »
Code: [Select]
haven't tested this code yet; if it works i will be elated

import os
from lxml import etree
derp = (etree.parse(open('derp.xml ','r'))).getroot()
pop1, pop2, pop3, pop4, pop5, pop6, pop7, pop8, pop9 = 0,0,0,0,0,0,0,0,0
for x in xrange(0,len(derp)):
    if derp[x][7].text == 'The Pacific':
        pop1 = pop1 + derp[x][8]
    if derp[x][7].text == 'The North Pacific':
        pop2 = pop2 + derp[x][8]
    if derp[x][7].text == 'The South Pacific':
        pop3 = pop3 + derp[x][8]
    if derp[x][7].text == 'The East Pacific':
        pop4 = pop4 + derp[x][8]
    if derp[x][7].text == 'The West Pacific':
        pop5 = pop5 + derp[x][8]
    if derp[x][7].text == 'The Rejected Realms':
        pop6 = pop6 + derp[x][8]
    if derp[x][7].text == 'Balder':
        pop7 = pop7 + derp[x][8]
    if derp[x][7].text == 'Osiris':
        pop8 = pop8 + derp[x][8]
    if derp[x][7].text == 'Lazarus':
        pop9 = pop9 + derp[x][8]
print pop1, 'The Pacific'
print pop2, 'The North Pacific'
print pop3, 'The South Pacific'
print pop4, 'The East Pacific'
print pop5, 'The West Pacific'
print pop6, 'The Rejected Realms'
print pop7, 'Balder'
print pop8, 'Osiris'
print pop9, 'Lazarus'
print pop1+pop2+pop3+pop4+pop5+pop6+pop7+pop8+pop9, 'Total'
os.system('pause')
os.system('pause')
os.system('pause')

vh

  • formerly mudkipz
  • *****
  • Posts: 1140
  • "giving heat meaning"
Re: Python
« Reply #101 on: November 27, 2012, 07:55:05 PM »
OMG THIS IS THE FIRST significant (if you call 30 lines significant) piece of code that has ever run perfectly on the first try with no issues i am so happy. now going to run it on the 5.5 million line daily data dump and hope it works equally well

vh

  • formerly mudkipz
  • *****
  • Posts: 1140
  • "giving heat meaning"
Re: Python
« Reply #102 on: November 27, 2012, 07:55:46 PM »
this python topic: me talking to myself. if anyone else reads this reply please reply to it so i know this topic hasn't just become a notepad for myself

vh

  • formerly mudkipz
  • *****
  • Posts: 1140
  • "giving heat meaning"
Re: Python
« Reply #103 on: November 27, 2012, 08:08:39 PM »
two erros in the above program, which were forgetting the .text and to int() the entire thing

vh

  • formerly mudkipz
  • *****
  • Posts: 1140
  • "giving heat meaning"
Re: Python
« Reply #104 on: November 27, 2012, 08:08:50 PM »
fixed version

Code: [Select]
import os
from lxml import etree
derp = (etree.parse(open('nations.xml ','r'))).getroot()
pop1, pop2, pop3, pop4, pop5, pop6, pop7, pop8, pop9 = 0,0,0,0,0,0,0,0,0
for x in xrange(0,len(derp)):
    if derp[x][7].text == 'The Pacific':
        pop1 = pop1 + int(derp[x][8].text)
    if derp[x][7].text == 'The North Pacific':
        pop2 = pop2 + int(derp[x][8].text)
    if derp[x][7].text == 'The South Pacific':
        pop3 = pop3 + int(derp[x][8].text)
    if derp[x][7].text == 'The East Pacific':
        pop4 = pop4 + int(derp[x][8].text)
    if derp[x][7].text == 'The West Pacific':
        pop5 = pop5 + int(derp[x][8].text)
    if derp[x][7].text == 'The Rejected Realms':
        pop6 = pop6 + int(derp[x][8].text)
    if derp[x][7].text == 'Balder':
        pop7 = pop7 + int(derp[x][8].text)
    if derp[x][7].text == 'Osiris':
        pop8 = pop8 + int(derp[x][8].text)
    if derp[x][7].text == 'Lazarus':
        pop9 = pop9 + int(derp[x][8].text)
print pop1, 'The Pacific'
print pop2, 'The North Pacific'
print pop3, 'The South Pacific'
print pop4, 'The East Pacific'
print pop5, 'The West Pacific'
print pop6, 'The Rejected Realms'
print pop7, 'Balder'
print pop8, 'Osiris'
print pop9, 'Lazarus'
print pop1+pop2+pop3+pop4+pop5+pop6+pop7+pop8+pop9, 'Total'
os.system('pause')
os.system('pause')
os.system('pause')

atomic7732

  • Global Moderator
  • *****
  • Posts: 3849
  • caught in the river turning blue
    • Paladin of Storms
Re: Python
« Reply #105 on: November 27, 2012, 08:42:34 PM »
this python topic: me talking to myself. if anyone else reads this reply please reply to it so i know this topic hasn't just become a notepad for myself
I read it. So the program above finds out the population of a region?

vh

  • formerly mudkipz
  • *****
  • Posts: 1140
  • "giving heat meaning"
Re: Python
« Reply #106 on: November 27, 2012, 08:47:24 PM »
yeah, based on the nations.xml daily dump.

blotz

  • Formerly 'bong'
  • *****
  • Posts: 813
  • op pls
Re: Python
« Reply #107 on: November 28, 2012, 05:08:09 AM »
i have reeded it

vh

  • formerly mudkipz
  • *****
  • Posts: 1140
  • "giving heat meaning"
Re: Python
« Reply #108 on: November 28, 2012, 02:02:43 PM »
well it turns out that the code can't load the entire 5.5 million line xml file at once so i manually split the file into 6 million line chunks. the code runs through each one. I also optimized the code and reduced the amount of time needed by looking through the region.xml file for that day which i also downloaded. since nations are grouped by regions, i can search put region if statements in the order that they appear in the xml file to save time. also i put a counter in there that causes the program to ignore the if statement if all the nations in that region have already been counted.

it took about 3 hours to go through 1.1 million lines with my previous script, lets see about this one.

Code: [Select]
import os
import time
start = time.time()
from lxml import etree
p1,p2,p3,p4,p5,p6,p7,p8,p9 = 0,0,0,0,0,0,0,0,0
pop1, pop2, pop3, pop4, pop5, pop6, pop7, pop8, pop9 = 0,0,0,0,0,0,0,0,0
def count():
    global p1,p2,p3,p4,p5,p6,p7,p8,p9,pop1,pop2,pop3,pop4,pop5,pop6,pop7,pop8,pop9,derp
    for x in xrange(0,len(derp)):
        if p2 < 5590 and derp[x][7].text == 'The North Pacific':
            pop2 = pop2 + int(derp[x][8].text)
            p2 = p2 + 2
        elif p4 < 5100 and derp[x][7].text == 'The East Pacific':
            pop4 = pop4 + int(derp[x][8].text)
            p4 = p4 + 4
        elif p5 < 4929 and derp[x][7].text == 'The West Pacific':
            pop5 = pop5 + int(derp[x][8].text)
            p5 = p5 + 5
        elif p9 < 3299 and derp[x][7].text == 'Lazarus':
            pop9 = pop9 + int(derp[x][8].text)
            p9 = p9 + 9
        elif p1 < 4928 and derp[x][7].text == 'The Pacific':
            pop1 = pop1 + int(derp[x][8].text)
            p1 = p1 + 1
        elif p8 < 2774 and derp[x][7].text == 'Osiris':
            pop8 = pop8 + int(derp[x][8].text)
            p8 = p8 + 8
        elif p3 < 4963 and derp[x][7].text == 'The South Pacific':
            pop3 = pop3 + int(derp[x][8].text)
            p3 = p3 + 3
        elif p6 < 1424 and derp[x][7].text == 'The Rejected Realms':
            pop6 = pop6 + int(derp[x][8].text)
            p6 = p6 + 6
        elif p7 < 2779 and derp[x][7].text == 'Balder':
            pop7 = pop7 + int(derp[x][8].text)
            p7 = p7 + 7
        print pop2/1000,pop4/1000,pop5/1000,pop9/1000,pop1/1000,pop8/1000,pop3/1000,pop6/1000,pop7/1000
for x in xrange(1,7):
    filename = 'nations'+str(x)+'.xml'
    herp = open(filename, 'r')
    derp = (etree.parse(herp)).getroot()
    herp.close()
    count()
print pop1, 'The Pacific'
print pop2, 'The North Pacific'
print pop3, 'The South Pacific'
print pop4, 'The East Pacific'
print pop5, 'The West Pacific'
print pop6, 'The Rejected Realms'
print pop7, 'Balder'
print pop8, 'Osiris'
print pop9, 'Lazarus'
print pop1+pop2+pop3+pop4+pop5+pop6+pop7+pop8+pop9, 'Total'
print 'time', time.time()-start
derp = None
os.system('pause')
os.system('pause')
os.system('pause')
* mudkipz congratulates self on nifty for loop that runs through each file automatically.
« Last Edit: November 28, 2012, 02:19:41 PM by mudkipz »

vh

  • formerly mudkipz
  • *****
  • Posts: 1140
  • "giving heat meaning"
Re: Python
« Reply #109 on: November 28, 2012, 03:02:36 PM »
revised version:

Code: [Select]
import os
import time
start = time.time()
from lxml import etree
p1,p2,p3,p4,p5,p6,p7,p8,p9 = 0,0,0,0,0,0,0,0,0
pop1, pop2, pop3, pop4, pop5, pop6, pop7, pop8, pop9 = 0,0,0,0,0,0,0,0,0
def count(p):
    global p1,p2,p3,p4,p5,p6,p7,p8,p9,pop1,pop2,pop3,pop4,pop5,pop6,pop7,pop8,pop9,derp
    for x in xrange(0,len(derp)):
        if p2 < 5590 and derp[x][7].text == 'The North Pacific':
            pop2 = pop2 + int(derp[x][8].text)
            p2 = p2 + 2
        elif p4 < 5100 and derp[x][7].text == 'The East Pacific':
            pop4 = pop4 + int(derp[x][8].text)
            p4 = p4 + 4
        elif p5 < 4929 and derp[x][7].text == 'The West Pacific':
            pop5 = pop5 + int(derp[x][8].text)
            p5 = p5 + 5
        elif p6 < 1424 and derp[x][7].text == 'The Rejected Realms':
            pop6 = pop6 + int(derp[x][8].text)
            p6 = p6 + 6
        elif p9 < 3299 and derp[x][7].text == 'Lazarus':
            pop9 = pop9 + int(derp[x][8].text)
            p9 = p9 + 9
        elif p1 < 4928 and derp[x][7].text == 'The Pacific':
            pop1 = pop1 + int(derp[x][8].text)
            p1 = p1 + 1
        elif p8 < 2774 and derp[x][7].text == 'Osiris':
            pop8 = pop8 + int(derp[x][8].text)
            p8 = p8 + 8
        elif p3 < 4963 and derp[x][7].text == 'The South Pacific':
            pop3 = pop3 + int(derp[x][8].text)
            p3 = p3 + 3
        elif p7 < 2779 and derp[x][7].text == 'Balder':
            pop7 = pop7 + int(derp[x][8].text)
            p7 = p7 + 7
        print pop2/1000,pop4/1000,pop5/1000,pop6/1000,pop9/1000,pop1/1000,pop8/1000,pop3/1000,pop7/1000, p
for x in xrange(1,7):
    filename = 'nations'+str(x)+'.xml'
    herp = open(filename, 'r')
    derp = (etree.parse(herp)).getroot()
    herp.close()
    p = x
    count(p)
print pop1, 'The Pacific'
print pop2, 'The North Pacific'
print pop3, 'The South Pacific'
print pop4, 'The East Pacific'
print pop5, 'The West Pacific'
print pop6, 'The Rejected Realms'
print pop7, 'Balder'
print pop8, 'Osiris'
print pop9, 'Lazarus'
print pop1+pop2+pop3+pop4+pop5+pop6+pop7+pop8+pop9, 'Total'
print 'time', time.time()-start
derp = None
os.system('pause')
os.system('pause')
os.system('pause')

vh

  • formerly mudkipz
  • *****
  • Posts: 1140
  • "giving heat meaning"
Re: Python
« Reply #110 on: November 28, 2012, 03:31:02 PM »
well fail, nsdossier already lists this info. edit: ah-ha, it can't process regions with 2k+ nations i win!

vh

  • formerly mudkipz
  • *****
  • Posts: 1140
  • "giving heat meaning"
Re: Python
« Reply #111 on: November 28, 2012, 03:32:22 PM »
welp, after removing all bugs, i have started the first test run

vh

  • formerly mudkipz
  • *****
  • Posts: 1140
  • "giving heat meaning"
Re: Python
« Reply #112 on: November 28, 2012, 03:39:09 PM »
absolution ns tracker seems to have gone down

vh

  • formerly mudkipz
  • *****
  • Posts: 1140
  • "giving heat meaning"
Re: Python
« Reply #113 on: November 28, 2012, 03:39:17 PM »
now on part 2/6

vh

  • formerly mudkipz
  • *****
  • Posts: 1140
  • "giving heat meaning"
Re: Python
« Reply #114 on: November 28, 2012, 03:58:17 PM »
damnit, i ran it, and it came up with a bunch of wrong answers

vh

  • formerly mudkipz
  • *****
  • Posts: 1140
  • "giving heat meaning"
Re: Python
« Reply #115 on: November 28, 2012, 07:37:12 PM »
well its not working so i've simplified it to test one region at a time and see what was wrong.
« Last Edit: November 28, 2012, 08:09:24 PM by mudkipz »

blotz

  • Formerly 'bong'
  • *****
  • Posts: 813
  • op pls
Re: Python
« Reply #116 on: November 28, 2012, 08:04:25 PM »
how long does it take for 1 nation about? and how many nations total?

vh

  • formerly mudkipz
  • *****
  • Posts: 1140
  • "giving heat meaning"
Re: Python
« Reply #117 on: November 28, 2012, 08:09:13 PM »
>nations?

i'm adding up regions

vh

  • formerly mudkipz
  • *****
  • Posts: 1140
  • "giving heat meaning"
Re: Python
« Reply #118 on: December 01, 2012, 03:28:08 AM »
this is pretty epic

Code: [Select]
import os
import itertools as it
import networkx as nx
import matplotlib.pyplot as plt
derp = list(range(100))
def findsubsets(s,m):
    return list(set(it.combinations(s,m)))
herp = findsubsets(derp,2)
for list in herp:
    list = tuple(list)   
G=nx.Graph()
G.add_nodes_from(derp)
G.add_edges_from(herp)
nx.draw_spring(G)
plt.show()

vh

  • formerly mudkipz
  • *****
  • Posts: 1140
  • "giving heat meaning"
Re: Python
« Reply #119 on: December 01, 2012, 04:27:44 AM »
WIP, this prints out a list of all the embassy connections in nationstates

Code: [Select]
import os
import itertools as it
import networkx as nx
import matplotlib.pyplot as plt
from lxml import etree
herp = open('regions.xml', 'r')
derp = (etree.parse(herp)).getroot()
herp.close()
embassylist = []
for element in derp:
    if len(element[9]) > 0:
        name = element[0].text
        length = len(element[9])
        for x in xrange(0,length):
            emb = element[9][x].text
            perp = [name, emb]
            embassylist.append(tuple(perp))
dictsort = {}
for x in embassylist:
    dictsort[x]=1
embassylist = list(dictsort.keys())
print len(embassylist)
print embassylist