• 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,710

Profile posts Latest activity Postings About

  • 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?!?!?!
    (the code isn't long, it's just in hide tags because it looks really bad in the middle of normal text. Aaand pretend indenting exists.)

    Okay about the join. So here's the line we used that wasn't working:


    enemy_fort = [fort for fort in self.forts if fort != team][0]


    You at least sort of remember that, right!

    The part in brackets is a list comprehension! A list comprehension means instead of making a list and then writing code, like:


    my_cats = []

    for cat in all_cats_ever:
    if cat.owner == me:
    my_cats.append(cat)


    You stick the code into the brackets!


    my_cats = [cat for cat in all_cats_ever if cat.owner == me]


    List comprehension syntax is a little bit confusing to learn, but it has some benefits, like: shorter (one line versus four), can be a lot easier to read once you're used to it, and also doesn't pointlessly start an empty list just to append to it. Shorter isn't the goal and you should totally use long code whenever it's easier for you to understand, but sometimes shorter is inherently easier to understand.

    Here's a sample of when a list comprehension looks like a mess, too:


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


    You can tell what the [cat for cats] thing is saying pretty easily, especially when you get used to list comprehensions! But that one up there is confusing and you have to think about it and maybe read more code for context of what's going on before you can edit it. I know what it means because I wrote it recently! (because I was having too much fun experimenting with squishing it all into a list comprehension) But if I forget later it'd take me some energy to work it out. And then once you get what it's doing it's unclear *why* it's doing it. What's the deal with only doing all that data splitting and lowering every three numbers! (that's what that for i in range does) What's so special about every third thing and second thing (I look at third number minus one, too) in the data. Why am I splitting things up! What's data[i-1]. Why do I use [1:] which throws out some arbitrary items from the finished list. Who knows. (It's because I'm parsing a text file for Pokemon attacks and the first couple of lines are a title and an empty, so those are garbage to me. And the other part is because every three lines are attack names! And the line i-1 is above the attack names and is the type like Bug. But who would know that.) Also it's harder to edit because there's a loooot of room for hard-to-notice typos in all those parentheses and brackets. Actually when I was working on that code I introduced a bunch of typos that took way longer to fix than they would have if I hadn't had all that squished into one line.

    Anyway, you don't actually need to know that stuff. What you do need to know is that we're doing this with the non-working code:


    for player in self.forts:
    if player != person_who_typed_the_attack_command:
    enemy = player


    Since there's only two forts in a game, whichever fort isn't the attacker's fort must be the enemy's fort! Except we stuck it in a list comprehension. So instead of: enemy = "Mewtini" We actually have: enemy = ["Mewtini"]

    You're trapped inside a list!!! So ["Mewtini"][0] takes the zeroeth item (and only item) out of the list so that we *do* have enemy = "Mewtini".

    But for some reason that caused a problem. So instead of that we have "".join["Mewtini"] which joins all the list items together except there's only one so we just end up with the string "Mewtini". Just. like. it should've. been. already.

    *And* we printed out the value before the join statement and it was a right value. It just didn't work before??? I'm sure there's a good explanation and we're just missing some detail but I have no idea what it is. So I gave up and left the join part! I added it in the first place just to be sure we were creating an appropriate string and then it worked.

    Also changing the output should be easy! :o We just have to do enemy_fort["HP"] -= damage. Right now we just have it print that without actually ever changing it.

    Well it'll be easy when you're able to get online, anyway!!! Was it on Tuesday that he's leaving?
    Uh... finals are in two weeks asdfjkl I haven't even started studying and aaaaaaaaaaaa
    enemy_fort = self.forts["".join([fort for fort in self.forts if fort != team])]

    Don't ask why you need the join because I really. don't. know.

    [19:34] <Kiruwing> ~use Giant Wheel
    [19:34] <kittybot> Kiruwing: 37

    ~reload PillowFort
    ~regteam Meowing Lights
    ~jointeam Meowing Lights
    ~regwpn Giant Wheel
    ~wpnwk Giant Wheel wood, alive
    ~wpnstrg Giant Wheel metal, stone
    ~startfort magic


    ~regteam Fire
    ~jointeam Fire
    ~startfort alive

    Closing your plugin now, just figured out that one mean problem!!
  • Loading…
  • Loading…
  • Loading…
Back
Top Bottom