|
neighbors is a list of elements that either have the value True or False, so that won't work. Also, if you give the function coordinates of a cell on the edge of the grid it will just raise the KeyError instead of giving you the neighbors.
|
# ? Sep 28, 2013 05:10 |
|
|
# ? Jun 12, 2024 07:44 |
|
Dictionaries are mutable; you can add things to and remove things from them without creating a new dictionary. But in neighbors you are working with a list, rather than a dictionary. Lists are also mutable, though! But you can avoid the problem of removing it after the fact by not putting it there in the first place. You can actually put an if into a comprehension: code:
A couple other comments: Why do you assign xcoord and ycoord to x and y instead of writing (for example) "def togglecell(x, y):"? This may be a bit picky, but avoid quit() and use sys.exit() instead. quit() is intended only for use in the interactive interpreter. It is a flaw of Python that it works at all outside of it. If you are interested, a suggested next feature: write a function that returns a range-like list clipped to the size of your grid. It can call range itself, if you want. In other words: code:
|
# ? Sep 28, 2013 05:30 |
|
Dominoes posted:ie: Python's a language for programmers? I disagree; C#, Java, and other languages require you to install a run-time environment of some sort, just like Python. Even Visual Studio C++ programs often require you to install something in order to get them to run on a fresh Windows machine. Windows applications that don't require some sort of installation are the exception, not the norm. Dominoes posted:I doubt anyone reading this post would be able to run any python program I made on their Ubuntu computer without a bit of legwork. Imagine asking a non-programer to do that. Why not? If you've created an RPM or something for them to install then it should be as simple as code:
code:
e: When your installation instructions are "type this command into your terminal" then you can't really accuse your users of needing to know anything. This doesn't mean that you need to build a binary, just a package QuarkJets fucked around with this message at 07:40 on Sep 28, 2013 |
# ? Sep 28, 2013 07:36 |
|
Pollyanna posted:
Note that if your X/Y coordinates here is 0, it will wrap around to the other side, which may not be what you want. It's also a bit complicated compared to how I would write it. (also, stop mixing tabs and spaces)
|
# ? Sep 28, 2013 11:58 |
|
I wrote a script to synchronize two folders, mostly to use for games that don't support steam cloud. It works in conjunction with google drive. I needed to create all sub folders for a full file path. This is what I came up with. Is there a better, less C, more python way to accomplish this?
|
# ? Sep 28, 2013 15:27 |
|
If you wanted it to be cross platform you could make use of os.path.join and similar utils rather than manually concatenating dir strings
|
# ? Sep 28, 2013 15:41 |
|
QuarkJets posted:Why not? If you've created an RPM or something for them to install then it should be as simple as Dominoes fucked around with this message at 15:53 on Sep 28, 2013 |
# ? Sep 28, 2013 15:47 |
|
Dominoes posted:Didn't know if worked that way - I assumed RPMs were for binaries. So for example, it might install Python 3.3, Pyqt5, a customized version of Pypdf that has bugfixes, an unofficial dev build of oathlib, the appropriate scipy modules etc? When uninstalled, would it clean up the packages it left scattered across the hard drive/python install? Or would it all be packaged together in the RPM? I think that's possible, but probably not what you'd want to do. Instead, you can list those as your dependencies, and when someone tries to install your RPM they'll be asked if they want to install those dependencies (which would be downloaded from somewhere else). Have a look at this: http://docs.python.org/2/distutils/builtdist.html
|
# ? Sep 28, 2013 20:11 |
|
Okay, what was I doing again?Python code:
Right, I remember. I want to iterate over the dictionary the_world and return the values of all cells directly next to the queried cell, NOT including the queried cell itself. Unfortunately, subtracting a bool from a list doesn't work (obviously). Maybe I want a way to tell that mess that defines neighbors to skip over the_world[(x, y)]. Forgive me if I've asked this already. Is there a simpler way of doing that [the_world[(i, j)] for i in range(x - 1, x + 2) for j in range(y - 1, y + 2)] poo poo? Nested for loops or something? edit: itertools.product works for this. Python code:
Pollyanna fucked around with this message at 06:44 on Sep 29, 2013 |
# ? Sep 29, 2013 06:11 |
|
Pollyanna posted:Unfortunately, the same thing that makes Python common in that field means that there's not much support for anything that doesn't do what a bioinformatician or computational biologist wants (i.e. anything beyond scripting and automation). Not really sure I'm following you here.
|
# ? Sep 29, 2013 06:38 |
|
Pollyanna posted:Forgive me if I've asked this already. Is there a simpler way of doing that [the_world[(i, j)] for i in range(x - 1, x + 2) for j in range(y - 1, y + 2)] poo poo? Nested for loops or something? Python code:
|
# ? Sep 29, 2013 06:40 |
|
fritz posted:Not really sure I'm following you here. Ignore that post. I talk like I know what I'm talking about. returnh posted:
Sorry, I just updated my code I'm still trying to figure out the whole edge problem right now, though. Python code:
|
# ? Sep 29, 2013 06:45 |
|
I want to tell it to ignore any (i,j) that isn't present in the_world, like (-1, -1). So that means checking to see if there is a the_world[(i, j)]. I'll try moving the try: into the if statement.Python code:
Pollyanna fucked around with this message at 07:01 on Sep 29, 2013 |
# ? Sep 29, 2013 06:58 |
|
You could use dict.get() with a default value for nonexisting cells.Python code:
|
# ? Sep 29, 2013 07:08 |
|
Okay, this should be a (sorta) working version of Conway's Game of Life: http://pastebin.com/7b8Zt6kf It asks for a height and width, and currently doesn't toggle any cells. That comes once I figure out how to give it a GUI.
|
# ? Sep 29, 2013 08:04 |
|
Pollyanna posted:Okay, this should be a (sorta) working version of Conway's Game of Life: Overall the code looks good, but I have suggestions: Instead of doing things like if the_world[x, y] == False: you should do if not the_world[x, y]:. Likewise, when you check for whether something is True, do if the_world[x,y] Are you sure that you want to use & instead of and? They are different and it looks like you mean to use and, but I'm not sure I had never heard of itertools before! What a neat tool. However, appending to a list is still a pretty slow operation compared to generating a list. itertools.product is creating a generator, so you can do this in a single line (broken up for readability): Python code:
|
# ? Sep 29, 2013 08:50 |
|
To each his own but if I wanted to use a list comprehension here (lots of people complain about them looking terse) I would've done this way:Python code:
Python code:
I really don't like using those pesky \ line breakers. I love these 0 < x < 1 math inequation style in python, too bad you don't get those in numpy. If you want some spoilers, tef made a game of life in a few lines some time ago, I thought it was really well written and I learned a lot back then just looking at it: http://forums.somethingawful.com/showthread.php?threadid=3376083&pagenumber=52&perpage=40#post405420314
|
# ? Sep 29, 2013 10:28 |
|
Pollyanna posted:Okay, this should be a (sorta) working version of Conway's Game of Life: This should all probably be packaged up into a class so things like high and wide and the_world become member variables instead of globals, but this is orthogonal to my comments below. Your code doesn't actually implement the correct rules for the game of life as described on the wiki page. I don't know if this is intentional or not. You should name togglecell and checkneighbors as toggle_cell and check_neighbors respectively, to match the rest of your code and normal python convention. neighbors would be clearer if you generate a list of all neighbors and then prune out the ones outside the grid like so (pretty formatting optional): code:
toggle_cell can be simpler (it also isn't used anywhere): code:
check_neighbors is badly named for what it does. It really figures out what the location x,y should look like at the next time step. I think a better structure would be to have a function like advance_time which would move your world one step into the future like this: code:
code:
I know other people in the thread told you to use a dictionary to represent the world, but I think this is a really absurd data structure to represent a dense matrix. A 2d numpy array of booleans or integers is a much more sane choice imo.
|
# ? Sep 29, 2013 10:30 |
|
http://pastebin.com/NDpuYbTG According to ~my tests~ this should work if you like accessing points through list-indices. A view needs to be implemented that will display the state of the Universe instance between each Universe.tick() call.
|
# ? Sep 29, 2013 16:00 |
|
QuarkJets posted:Overall the code looks good, but I have suggestions: Thanks for bringing this up, PyCharm helped me clean up the logic and it looks a lot nicer now Python code:
itertools is pretty great. Means I don't have to write new or complicated code. As for the list-append stuff, I'm still considering changing the data structure to an array. It seems a lot more obvious to me and I can easily change it into a picture if I want. Speaking of arrays... Nippashish posted:This should all probably be packaged up into a class so things like high and wide and the_world become member variables instead of globals, but this is orthogonal to my comments below. Howso? I think I got the logic behind whether a cell lives or dies right - ...wait poo poo, I see it. Python code:
quote:You should name togglecell and checkneighbors as toggle_cell and check_neighbors respectively, to match the rest of your code and normal python convention. Done. quote:neighbors would be clearer if you generate a list of all neighbors and then prune out the ones outside the grid like so (pretty formatting optional): Wow, that's way more elegant. I got the code working and it looks like it runs way faster. quote:You shouldn't call a variable neighbors inside a function called neighbors. Confusing things would start happening if you ever tried to call neighbors recursively. Right, PyCharm has been yelling at me over that. I think I've fixed all instances of it now. quote:toggle_cell can be simpler (it also isn't used anywhere): Yeah, that's an artifact of some other thing that didn't pan out. I removed it. quote:next_round could just be replaced with the_world = updated_world unless you have some plan for why you need two copies of the updated world around. Good point. Fixed. quote:check_neighbors is badly named for what it does. It really figures out what the location x,y should look like at the next time step. I renamed it "tick", so it'll return what the gameboard will look like next round (or "tick"). quote:I think a better structure would be to have a function like advance_time which would move your world one step into the future like this: I think this is what I was trying to do with next_round, but it didn't work very well. quote:I know other people in the thread told you to use a dictionary to represent the world, but I think this is a really absurd data structure to represent a dense matrix. A 2d numpy array of booleans or integers is a much more sane choice imo. Yeah, I kinda want to change this over to an array instead. I'm used to matrices and I want to output this as an image eventually, so a matrix (array) makes the most sense to me. I'll need to figure out how to make this logic work with arrays instead, though, since it requires certain changes. As for the class wrapper, let me get back to that later. Symbolic Butt posted:If you want some spoilers, tef made a game of life in a few lines some time ago, I thought it was really well written and I learned a lot back then just looking at it: http://forums.somethingawful.com/showthread.php?threadid=3376083&pagenumber=52&perpage=40#post405420314 gently caress What I'm doing is different from trying to reinvent the wheel. I'm writing this as programming practice. But reading that definitely shows I have a ways to go. Here's what I got so far: http://pastebin.com/ikbZj2xN edit: oh my god arrays use the opposite x y notation that i've been using the entire time ffffffuckkkkkkkkkkkkkkkkkkkk Well, it shouldn't matter though, right? As long as the range(wide) etc. stuff is the same. Pollyanna fucked around with this message at 17:24 on Sep 29, 2013 |
# ? Sep 29, 2013 17:12 |
|
suffix posted:You could use dict.get() with a default value for nonexisting cells. If you're going to do that, maybe just use a collections.defaultdict instead.
|
# ? Sep 29, 2013 17:22 |
|
Okay, wait, something hosed up. Setting three contiguous cells as alive results in none of them living in the next round, while one of them should. What Forget it, I'm moving onto arrays.
|
# ? Sep 29, 2013 17:52 |
|
Your neighbors function generates a list of valid neighbor xy pairs. You still need to get the True/False cell statuses from the_world before counting how many cells are alive.
|
# ? Sep 29, 2013 18:33 |
|
Okay, here's the array version: http://pastebin.com/ikbZj2xN Right now, it asks you for a height, width, and number of rounds. For testing this out, I input 5, 5, and 2. I have it print out the beforehand world, then set it equal to the after world, then print the beforehand world again. It should end up as: Python code:
Python code:
I tried to use the second array as the beginning, but: Python code:
Python code:
gently caress, I think what's happening is that the drat program is changing the array between checking each cell's neighbors. What the gently caress? Why? How come it works correctly once, but doesn't work the second time!? edit: Oh god. I forgot to reset next_world. I'm retarded. This is the whole "by value" versus "by reference" thing, right? How does that work? Final version: http://pastebin.com/ikbZj2xN Pollyanna fucked around with this message at 20:02 on Sep 29, 2013 |
# ? Sep 29, 2013 19:21 |
|
Has anyone found an easy to way to package qt5 programs without the 17mb localization icudt49.dll ? Bloat. I've read you can compile a custom QT with a -no-icu tag, but has anyone here successfully done so?
Dominoes fucked around with this message at 19:33 on Sep 29, 2013 |
# ? Sep 29, 2013 19:29 |
|
Dominoes posted:Has anyone found an easy to way to package qt5 programs without the 17mb localization icudt49.dll ? Bloat. I've read you can compile a custom QT with a -no-icu tag, but has anyone here successfully done so? Have you tried zipping it after packaging? I'd expect that a localization file would be mostly plaintext and therefore highly compressible.
|
# ? Sep 29, 2013 20:11 |
|
BeefofAges posted:Have you tried zipping it after packaging? I'd expect that a localization file would be mostly plaintext and therefore highly compressible.
|
# ? Sep 29, 2013 20:14 |
|
Pollyanna posted:Also I don't know what the hell is up with tabs vs spaces vs whatever. I use tabs in PyCharm and copying the code changes them to spaces or something. Yeah, I don't think that SA uses tabs. Vim has an option for automatically converting tabs into a number of spaces, PyCharm probably does too; that would be my suggestion. I like it for exactly these types of situations (where copy-pasting code results in an tab vs space mismatch)
|
# ? Sep 29, 2013 20:37 |
|
QuarkJets posted:Yeah, I don't think that SA uses tabs. Vim has an option for automatically converting tabs into a number of spaces, PyCharm probably does too; that would be my suggestion. I like it for exactly these types of situations (where copy-pasting code results in an tab vs space mismatch) I actually think PyCharm automatically handles tabs and spaces and whatnot already, cause tabbing puts in four spaces for me. Easy enough. Also, is there a tutorial for Tkinter out there that doesn't suck?
|
# ? Sep 29, 2013 22:50 |
|
There's an option in PyCharm settings to turn tabs into 4 spaces, pretty useful. Also, turn on punctuation, that way you can see what you've used. If for some ungodly reason you have to use tabs, at least be consistent.
NtotheTC fucked around with this message at 23:06 on Sep 29, 2013 |
# ? Sep 29, 2013 22:50 |
|
Pollyanna posted:I renamed it "tick", so it'll return what the gameboard will look like next round (or "tick"). tick is a good name for that function. Instead of having tick update the new board directly I recommend writing it so you use it like this: code:
In general having functions create the values they return instead of accepting an argument that they mutate (like you do in tick now) will make your code a lot easier to reason about since you never need to worry that a function might change some data you give it.
|
# ? Sep 29, 2013 23:21 |
|
Pollyanna posted:edit: oh my god arrays use the opposite x y notation that i've been using the entire time ffffffuckkkkkkkkkkkkkkkkkkkk Don't be lazy about this. I once wrote an A* pathfinding implementation that had this exact same bug and it worked just fine for all my square grid tests. It wasn't until we tried making non square maps later that there was suddenly a really hard to find bug.
|
# ? Sep 30, 2013 03:27 |
|
edit: type mismatch, whoops!
|
# ? Sep 30, 2013 04:13 |
|
salisbury shake posted:http://pastebin.com/NDpuYbTG I mostly like this but I think you should move _step() and tick() out of your Universe class. That way Universe is a representation of the game board and there would be a clean separation between the model (Universe) and the business logic (_step() and tick()). Also, why is _step() a property?
|
# ? Sep 30, 2013 15:49 |
|
i'm trying to write haskell-style monads in python. doing something like this is straight-forward:code:
code:
|
# ? Sep 30, 2013 15:57 |
|
Messing around with numpy lately and I've been wondering if there's some way to make index arrays work like a defaultdict.Python code:
|
# ? Sep 30, 2013 15:59 |
|
Symbolic Butt posted:Messing around with numpy lately and I've been wondering if there's some way to make index arrays work like a defaultdict. Before you go any further take another look at this problem and see if you can't solve it another way. Does numpy have any kind of sparse array? What about making an array of all 0's that the same size as the one you want and adding both together? I'm not a numpy expert but I think you should try to solve your problem within the numpy API rather than creating a custom extension of numpy that relies on the behavior of their array's private __getitem__ method. You have no guarantee that np.ndarray.__getitem__'s behavior won't change in the next numpy release, breaking your extension. What is almost certainly happening here is that the argument, index, being passed to your overloaded __getitem__ method is the array [3, 3, 1, 5]. Which is then passed to np.ndarray.__getitem__(self, index). This method would normally look up each of the indexes and return an array but it raises an IndexError because it doesn't know about 5. I think what you'll have to do here is unpack the array in your overloaded __getitem__ method and feed it to np.ndarray.__getitem__() one value at a time.
|
# ? Sep 30, 2013 16:21 |
|
Pie Colony posted:i'm trying to write haskell-style monads in python. doing something like this is straight-forward: I don't think you can actually change any methods on the built in FunctionType object, but there's probably some other type of hackery that could get you to a similar place.
|
# ? Sep 30, 2013 16:51 |
|
Pie Colony posted:i'm trying to write haskell-style monads in python. doing something like this is straight-forward: Focus on getting the semantics correct over emulating do-notation (use a python macro library for that if you have to) https://github.com/lihaoyi/macropy is good. All you have to have is a Functor class which defines fmap, a Monad class which extends it (maybe an applicative too if you want) and defines mreturn and join/bind Remember that you must follow the Monad laws and functor laws or there's no point (up to strictness and exception and whatever). Without case classes/algebraic data types it gets kind of awkward though, unfortunately
|
# ? Sep 30, 2013 17:11 |
|
|
# ? Jun 12, 2024 07:44 |
|
Nippashish posted:tick is a good name for that function. Cool, thanks. I updated it: Python code:
|
# ? Sep 30, 2013 17:29 |