• Welcome to The Cave of Dragonflies forums, where the smallest bugs live alongside the strongest dragons.

    Guests are not able to post messages or even read certain areas of the forums. Now, that's boring, don't you think? Registration, on the other hand, is simple, completely free of charge, and does not require you to give out any personal information at all. As soon as you register, you can take part in some of the happy fun things at the forums such as posting messages, voting in polls, sending private messages to people and being told that this is where we drink tea and eat cod.

    Of course I'm not forcing you to do anything if you don't want to, but seriously, what have you got to lose? Five seconds of your life?

mewtini
Reaction score
3,701

Profile posts Latest activity Postings About

  • I think I did fine.

    ..and now it's summer! And my rubber band ball is the size of a baseball, and I almost emptied my house's supply of rubber bands. I may have to buy more.
    ...and suddenly it's exam week. Fantabulous.

    ...Completely unrelatedly, I just beat Fire Emblem Awakening on Lunatic difficulty. Lunatic+ comes next, I guess!
    I am alive! Haha. Thought I'd stop by since I'm not doing anything this summer but working.
    Actually mostly my allergies are in March and April, so idk (no problem at all)

    I'm actually feeling okayish about finals, just math I'm not sure about.
    nothing much. I invited some friends over and we played Risk. And then we player Super Smash Bros. Brawl.
    started studying

    >not making any progress
    >
    That's alright; I just was wondering because I was pretty sure I sent it but wasn't positive if you got it.

    Anyway, how's life
    Yep that's msg.nick. I got so confused by that code for a minute because I thought player *was* msg.nick reading it just now. "Why would msg.nick ever not be msg.nick?!?!"

    data = [(data[i-1].lower().capitalize() + ", " + data).split(", ") for i in range(0, len(data), 3)][1:]

    That's. some code. that does a thing. Basically it generates lists like ["Bug", "Bug Bite", "Struggle Bug", "Megahorn"].

    A piece of text that looks like "Bug Bite, Struggle Bug, Megahorn" is data!

    data[i-1] is "BUG". I don't want "BUG" in all caps, so first I do "BUG".lower() to get "bug"! Then I capitalize() it again to get Bug.

    "for i in range(0, len(data), 3)" this is a thing that counts by threes! I'll show you.

    >>> for i in range(0, 20, 3):
    ... print i
    ...
    0
    3
    6
    9
    12
    15
    18


    See how it counts by threes up to twenty! You can count by twos or fours or anything else, too.

    data is all the lines in a text file! http://paste.kde.org/744758/86617741/ This exact text file. So data[0] is "= ATTACK CARDS =", data[1] is an empty line, data[2] is "BUG" and so on.

    That [1:] at the end of the code? That copies the whole list from item 1 to the last item. So item zero gets thrown out! It's called a slice when you do that.


    >>> list = [0, 1, 2, 3, 4, 5, 6]
    >>> print list[1:]
    [1, 2, 3, 4, 5, 6]
    >>> print list [2:5]
    [2, 3, 4]
    >>> print list[:4]
    [0, 1, 2, 3]
    >>>


    The reason we're throwing out item zero is that item zero is "= ATTACK CARDS =" and obviously we have no need for that since it's just a title.

    So now we can jump on multiples of three (0, 3, 6...) and all of them will be a line like "Struggle Bug, Bug Bite, Megahorn"! So then we can have data. Remember, i in there is a multiple of three.

    And we know that the type name (BUG, FIRE, WATER) will always be right above the attacks list! So we can do data[i -1] to get to BUG from "Struggle Bug, Bug Bite, Megahorn". And then we do that capitalizing and lowering thing we talked about already to get "Bug".

    And since we have (data[i-1] + ", " + data), we now have... "Bug, Bug Bite, Struggle Bug, Megahorn"!!! (pretend I'm keeping these examples in the same order all the time) Now we can just perform .split(", ") on that.

    And ta-dah! We now have ["Bug", "Bug Bite", "Struggle Bug", "Megahorn"].

    Why we want that is so I can use csv.writer() to make a csv with the line Bug,Bug Bite,Struggle Bug,Megahorn later. Why we want a csv like that is because it's really easy to read csvs so then I can read it in my TypeFort plugin later.

    Here's the whole code:


    import csv

    with open("attacks.txt") as f:
    data = [line.replace("\n", "") for line in f.readlines()]

    data = [(data[i-1].lower().capitalize() + ", " + data).split(", ")
    for i in range(0, len(data), 3)][1:]

    with open("attacks.csv", 'wa') as f:
    csv.writer(f).writerows(data)


    Since it takes that many paragraphs to explain it (and I messed up on what the .split was doing and had to go back and edit, whoops), it's not a good code. Also, it's a problem if I want to edit the code. More typos can happen, I might forget what a part does and be really confused when my edit doesn't work, etc. It'd be a lot more readable as like...


    data = data[1:] #goodbye, "= ATTACK CARDS ="

    all_attacks = []

    for i in range(0, len(data), 3): #every three
    type = data.lower().capitalize() #BUG to Bug
    attacks = data[i - 1] #Struggle Bug, Bug Bite, Megahorn

    combined = (type + ", " + attacks) #Bug, Struggle Bug, Bug Bite, Megahorn

    list = combined.split(", ") #["Bug", "Struggle Bug", "Bug Bite"]

    all_attacks.append(list)


    That's a little too expanded because we have a bunch of completely pointless variables like combined and list. You need a balance between super-expanded and super-squished. But! It's more readable.

    (also you might notice, when you expand things, you can actually leave comments on the lines. There's no space for comments on my squished version.)

    Also I just figured out you can do this instead:

    >>> "BUG".title()
    'Bug'


    So that would help the code out a bit.

    data = [(data[i-1].lower().capitalize() + ", " + data).split(", ") for i in range(0, len(data), 3)][1:]
    vs
    data = [(data[i-1].title() + ", " + data).split(", ") for i in range(0, len(data), 3)][1:]

    I only get away with writing that super-condensed code because it's a throw-a-way program that I only need for the csv. I have the csv! So I never have to read it again.

    I could write it more expanded but I think it's fun to see how condensed I can get code to be. So I practice that when I have baby throw-a-way programs.

    ------------

    But, more importantly, why are you trapped in a list?!?!?!
  • Loading…
  • Loading…
  • Loading…
Back
Top Bottom