Author Topic: [Python] Looking For Some Good Examples  (Read 20067 times)

Hashiba

  • Newcomer
  • Posts: 6
  • Karma: +0/-0
    • View Profile
    • Email
[Python] Looking For Some Good Examples
« on: February 05, 2016, 07:34:57 PM »
I'm yet another new Programmer+Pythoner+roguedev that has been lurking around these forums for the last week or so after finishing the (in)famous python + libtcod tutorial.  I've been giving some thought about where to go next, and there is some temptation to just keep adding onto that existing code.  However, a fairly large part of me feels that in order to really know how to code, I have to code, not just follow along a tutorial or merely extend some existing logic.  When I combine this thought with the fact I really don't like the way the tutorial code is written (all one file, globals all over the place, etc.) I decided the next stage for me is to essentially start from scratch with my own code, purposefully implementing things differently to both make certain I understand the logic as well as become more comfortable with Python.

I set out to attempt to follow a lot of these "best practices" I keep reading about and try a strict OOP style.  So far I've recreated the basic "@" walking around the "." tech demo while borrowing as little from the tutorial as possible (there is some common code due to libtcod, but I tried to build all of mine from the docs rather than the tut).  I'm getting ready to move to the next steps, but this also means I'll be adding more classes and possibly files and I'm purposely committing the grave sin of trying to factor before I code.  I mean, I know I can code the next features, but I'm wanting to learn better ways to be coding them.  Inheritance with relatively dumb base classes? Composition on top of relatively smart base classes?  How big should my core "Engine" class be allowed to get? I have a lot of questions that I'm sure don't have really concrete answers, but I'm sure that there *are* answers.

I keep reading the advice here and elsewhere to "look at *good* code" and follow examples of how to structure things, and how to pass information between the classes, but there is a hitch.  I am a new programmer, so despite the fact I can find 100s of python games on Github, I have no idea if I'm looking at *good* code to learn by example.  A lot of the projects I have looked at, especially 7DRLs look like they have some terrible practices in use.  I found some with circular imports and others with dozens of global variables (not counting CONSTANTS), and this just doesn't help me learn to be able to manage a large project.

What I'm hoping for is this:  Will any of you be so kind as to point me to a (couple of) roguelike project(s) that are written in Python, use OOP and are written in what would reasonably be considered *good* code?  Obviously there are many styles of coding, but there must be a few projects that most reasonably agree on are well written.

Avagart

  • 7DRL Reviewer
  • Rogueliker
  • *
  • Posts: 567
  • Karma: +0/-0
    • View Profile
Re: [Python] Looking For Some Good Examples
« Reply #1 on: February 05, 2016, 09:53:45 PM »
First - roguelikes rarely has a good code. I checked large amount of code of open source python roguelikes and I didn's see any with really good code. Even my own ;) Btw, I started little project now to learn code things in proper way... Second - you know, there is Zen of python, there is 'do it in more pythonic way', PEP-8 etc. But for sure there is not *only one good way to code*. OOP? Tere is old flame war about classes in python. Some people write strict OOP code, everything in classes (check source of PyRo) but some people claims that classes in python are... 'too commonly used'.

I don't want to start this discussion over, but you would check this reddit threat with video included.

Omnivore

  • Rogueliker
  • ***
  • Posts: 154
  • Karma: +0/-0
    • View Profile
Re: [Python] Looking For Some Good Examples
« Reply #2 on: February 15, 2016, 05:50:12 AM »
The Python standard library, especially the later stuff, is a good place to look.  (Some of the earlier stuff is.. non-standard these days).  It really doesn't matter what the problem domain of the code is, more that it is well written, well structured, documented, clear, concise, has good test coverage with tests that are non-trivial.

Don't get hung up on methodologies.  OOP is just another methodology, works great some places, not so well in others.  Same with Functional, or any other methodology.  You can apply the same thing to Agile practices, though some are pretty much "must do's" such as unit testing and refactoring.

As a roguelike dev, you'll mostly be working on your own.  That does not mean you can skimp on documentation or unit tests, to the contrary, unless you have perfect photographic memory - you're going to need those doc string comments three months from now when you try to use or modify that great nifty map generator you wrote last week.  Python is exceptionally sensitive to the needs of both documentation and unit tests because it is not only a dynamic typed language but also a very open one. 

Don't be afraid to use abstract classes as interfaces, or to write Mocks, or stub functions and classes.  Those things are tools that help keep you focused on the task at hand which enormously helps the look and feel of your codebase.  I mean, how can you write code that doesn't look like spaghetti, if you write it in a scattered fashion?  Make notes, in code.  // TODO: FIX THIS (and explain what this is that needs fixing)

It also helps a ton to have some sort of requirements doc.  Even just a page or two that informally describes what you're trying to achieve, what you want the end product to be able to do.  Refer back to it often, update it, flesh it out as you go.  Make a habit of reading it at the beginning of every session.

Try not to jump on bandwagons or trends unless they prove their value to what you are doing.  Even then, in moderation.  I could probably write pages on the evils of pattern-first design.  Frankly you don't even want to look at design-patterns until the second or third refactoring.  By then you might be able to see patterns in your own code that *might* benefit from seeing how other people have solved similar patterns.

Keep things simple.  Short.  To the point.  Above all, YAGNI - You Ain't Gonna Need It.  Don't stick code in until you need it.  Never write code thinking that well, I might want to do xyz someday so this will.. no.. just plain no.

I'll keep my eyes open for larger projects on the net in Python that might be worth a look and maybe post a followup later.

Hope this helps :)
Brian aka Omnivore


Krice

  • (Banned)
  • Rogueliker
  • ***
  • Posts: 2316
  • Karma: +0/-2
    • View Profile
    • Email
Re: [Python] Looking For Some Good Examples
« Reply #3 on: February 16, 2016, 07:47:24 AM »
I think python is not the best language for OOP. I thought it was supposed to be a functional language? Anyway, it was one of those languages the developer made because he wanted "a better C++". It's never a good starting point to create a language, because it's based on a false idea of making some feature of C++ a "problem". The real problem is that you don't understand the language at all.

Python suffered exactly the same fate than Java did earlier. Java was supposed to be the better C++, but it became a scripting language, which python is also good at in conjunction with real programming languages like C.

Omnivore

  • Rogueliker
  • ***
  • Posts: 154
  • Karma: +0/-0
    • View Profile
Re: [Python] Looking For Some Good Examples
« Reply #4 on: February 16, 2016, 09:58:57 PM »
I think python is not the best language for OOP. I thought it was supposed to be a functional language? Anyway, it was one of those languages the developer made because he wanted "a better C++". It's never a good starting point to create a language, because it's based on a false idea of making some feature of C++ a "problem". The real problem is that you don't understand the language at all.

Python suffered exactly the same fate than Java did earlier. Java was supposed to be the better C++, but it became a scripting language, which python is also good at in conjunction with real programming languages like C.

Where do you get this garbage?  Do you dream it up?  Spin it wholesale out of rags? 

Try http://www.python-course.eu/python3_history_and_philosophy.php for the real story. 

From reading your posts over the years, one could easily come to the conclusion that you don't have a clue about either C++ or OOP, or for that matter programming in general.  I'm not at all certain that such a conclusion would be erroneous.

Brian aka Omnivore

Hashiba

  • Newcomer
  • Posts: 6
  • Karma: +0/-0
    • View Profile
    • Email
Re: [Python] Looking For Some Good Examples
« Reply #5 on: February 16, 2016, 10:30:46 PM »
My years of on-and-off again lurking have well prepared me to properly digest posts by Krice.  ::)    Seriously man, you [Krice] drop the odd bit of intelligible commentary here and there, but mostly it's just hugely negative word vomit that just isn't accurate.  Python bad at OOP, OK, opinion filed, thank you.

@Avagart  --  I read your reply some time ago, but didn't get around to acknowledging it.  Your video link opened a door to a whole section of the Python world I was until then unaware of.  I've been watching a lot of Raymond Hettinger's talks and I spent a lot of time reading more code, including Pyro.  I'm learning and therefor my idea of what "good" code looks like is evolving, at least within Python.

@Omnivore  --  Thanks, since I first made my post, I can't help but feel the anti-OOP crowd has some excellent points about following the laws of OOP for the mere sake of it, rather than to make the code better.  If something doesn't make the code better, then why are you doing it?  I looked at my meager code only a week after I wrote it and already had some issues understanding why I had done certain things certain ways.  I took that moment to decide to go through it and fully comment every class, method and function save for the painfully obvious ones.  As for not coding things until you need them, I also ripped out a bunch of    def this_menu() ---> pass    nonsense I had scattered around, since I don't have any menus yet.  Being new, I went for the low-hanging fruit instead of tackling the next step and added those useless and empty things because, hey, I'll need them later right?  People like you keep writing things that make me realize that kind of thing is just stupid.  I'll figure out my menu classes/functions when I need to implement a menu from now on.

I don't have a design document per se, but I do have a half-dozen sheets of paper scrawled full of pretty unorganized notes.  I have this nebulous mass of ideas drifting around my brain that are revolving around a common theme that I could hammer out into an actual game concept, I've just been putting it off  8).   Actually, several years ago I first learned a little bit of Python, and even felt the urge to make a roguelike.  I see a post of mine from 3 years ago here asking a pretty stupid question as I was jumping in too deep trying to use Pygame and not use the libtcod tutorial.   I made it to some Cellular Automata caves I could never figure out how to ensure were joined up before I quit.  I bring this up because at that time, I had this generic desire to make a game, but I had no game in mind to actually make.  It seems an obvious flaw now, but somehow it didn't deter me at the time and I feel a lot of the roguelike and gamedev newbies that show up and then disappear again fall into this same trap.  If you don't have an actual game you want to make, how can you expect to actually make it?

You're advice about a "requirements" doc is well heeded, and hopefully you keep giving it to other newbies.  In fact, now that you've forced me to organize my thoughts on the subject, I'm going to put off fiddling with BSPs and drunken walks and actually write that design doc.

Krice

  • (Banned)
  • Rogueliker
  • ***
  • Posts: 2316
  • Karma: +0/-2
    • View Profile
    • Email
Re: [Python] Looking For Some Good Examples
« Reply #6 on: February 17, 2016, 07:53:36 AM »
From reading your posts over the years, one could easily come to the conclusion that you don't have a clue about either C++ or OOP

It seems like you are determined to hold that opinion. All I'm saying is that people who run into trouble with OOP most likely don't get it. It's ok, they can try something else just fine.

Krice

  • (Banned)
  • Rogueliker
  • ***
  • Posts: 2316
  • Karma: +0/-2
    • View Profile
    • Email
Re: [Python] Looking For Some Good Examples
« Reply #7 on: February 17, 2016, 09:11:31 AM »
Python bad at OOP, OK, opinion filed, thank you.

Maybe you are too young and inexperienced to remember what they said about Java when it first was released. Those kind of people believe that there is a "perfect" language in which they then believe and also it's important to point out flaws in other languages. What they all fail to understand is that the programming language is much less important than the programmer. It's of course hard to admit to yourself that you might be wrong or bad at programming.

TheCreator

  • Rogueliker
  • ***
  • Posts: 370
  • Karma: +0/-0
    • View Profile
    • Fame
    • Email
Re: [Python] Looking For Some Good Examples
« Reply #8 on: February 17, 2016, 11:55:38 AM »
I don't have a design document per se, but I do have a half-dozen sheets of paper scrawled full of pretty unorganized notes.  I have this nebulous mass of ideas drifting around my brain that are revolving around a common theme that I could hammer out into an actual game concept, I've just been putting it off  8).   Actually, several years ago I first learned a little bit of Python, and even felt the urge to make a roguelike.  I see a post of mine from 3 years ago here asking a pretty stupid question as I was jumping in too deep trying to use Pygame and not use the libtcod tutorial.   I made it to some Cellular Automata caves I could never figure out how to ensure were joined up before I quit.  I bring this up because at that time, I had this generic desire to make a game, but I had no game in mind to actually make.  It seems an obvious flaw now, but somehow it didn't deter me at the time and I feel a lot of the roguelike and gamedev newbies that show up and then disappear again fall into this same trap.  If you don't have an actual game you want to make, how can you expect to actually make it?

Perhaps you don't want to be a programmer at all. If you have a lot of ideas and like to write them down, you may want to become a game designer. Try to organize your notes into a real design document. If you manage to get your ideas presented in a nice, readable form, you'll probably find someone who will gladly help you with the programming side of the project. That would be probably much better than struggling to do something you don't really want to do. I think that some of the most famous roguelike authors are actually not so great at programming, but I doubt that any one of them is a weak designer. Designing skills are kind of more important than programming (this actually applies to all game genres, not only to roguelikes), so let them develop before you grow too old :). Some Python knowledge won't hurt, sure, just don't spend too much time on that until you are absolutely sure that you are hopeless as a designer.
Fame (Untitled) - my game. Everything is a roguelike.

tuturto

  • Rogueliker
  • ***
  • Posts: 259
  • Karma: +0/-0
    • View Profile
    • pyherc
Re: [Python] Looking For Some Good Examples
« Reply #9 on: February 17, 2016, 04:41:05 PM »
Wanting to learn from good code is a good idea. But you can also read any code and spot both good and bad practices and learn from both. Most likely as your skills develop over the time, your views on what is good or bad code will change too. So, I would just read code and try to learn from it and most importantly, learn from what you write, both good and bad. Also, good or bad code is somewhat subjective matter. Ask ten programmers and you get a dozen opinions. Someone might argue that only the most generic and flexible codebase is good, while other argues that everything can be hardcoded if it just solves the problem at hand. I guess it depends on what you want to achieve: write a game, learn to program, learn more about designing and architecture, etc (I chose not to write an engine, but a programming language for writing a roguelike, so it's extra slow progress).

The only thing you really need for writing a game is stubborness anyway. Everything else is secondary and can be substituted with some more stubborness and will to stick with it. And remember to have fun too while you write your game.
« Last Edit: February 17, 2016, 05:09:06 PM by tuturto »
Everyone you will ever meet knows something you don't.
 - Bill Nye

Hashiba

  • Newcomer
  • Posts: 6
  • Karma: +0/-0
    • View Profile
    • Email
Re: [Python] Looking For Some Good Examples
« Reply #10 on: February 17, 2016, 08:27:24 PM »
Maybe you are too young and inexperienced to remember what they said about Java when it first was released. Those kind of people believe that there is a "perfect" language in which they then believe and also it's important to point out flaws in other languages. What they all fail to understand is that the programming language is much less important than the programmer. It's of course hard to admit to yourself that you might be wrong or bad at programming.

This just cracked me up!  You see, it seems like you are trying to make some kind of jab at me for using Python, or for being a new coder, which is inherently pointless since neither of those things anyone would be sensitive about.  Of course I'm bad at programming, it is sort of assumed when someone is new :P.   But then you slip up big time and imply that people like Guido van Rossum and other language creators are bad programmers.  I guess one day they were trying to code something and it was too hard, so they just set about creating a whole language.  If we extend on this logical path, you'll end up making the argument that we just write everything in Fortran, or why not assembly, or hell, hard wire things with primitive logic gates like REAL programmers.  Since no one in this thread was supporting the idea that any language had any inherent advantages over any other, I really just can't see what put you on that train of thought.

Aside from all of that, "[the] programming language is much less important than the programmer."  Sounds good to me.  Therefor, I'll stick with Python since I've already started learning it and there is no reason to switch to another language.


Perhaps you don't want to be a programmer at all. If you have a lot of ideas and like to write them down, you may want to become a game designer. Try to organize your notes into a real design document. If you manage to get your ideas presented in a nice, readable form, you'll probably find someone who will gladly help you with the programming side of the project. That would be probably much better than struggling to do something you don't really want to do. I think that some of the most famous roguelike authors are actually not so great at programming, but I doubt that any one of them is a weak designer. Designing skills are kind of more important than programming (this actually applies to all game genres, not only to roguelikes), so let them develop before you grow too old :). Some Python knowledge won't hurt, sure, just don't spend too much time on that until you are absolutely sure that you are hopeless as a designer.

I'm not really sure what you are suggesting here.  You make it sound like programming and design are mutually exclusive.  So I have to program other people's games if I want to program, or just design games if I want to do that?  I'm sure your intended message isn't as polemic as that, so I'll assume there is a subtlety that is lost on me.  I really do want to be a programmer, because as frustrating as it can be at times, I actually enjoy it.  I have no idea if I'll ever be "great" at programming, but I labour under the delusion that I'll be at least competent one day.  My "game" ideas have been very few and far between, thus giving very little material to try to program.  This current project is the first time I've really had a vision of what I wanted to do, and I've resolved to see it to a playable state regardless of how long it takes.  Perhaps at some point my vision will exceed my programming potential and I'll relegate myself to relying on a team, but I don't foresee it.


The only thing you really need for writing a game is stubborness anyway. Everything else is secondary and can be substituted with some more stubborness and will to stick with it. And remember to have fun too while you write your game.

I'm known for being stubborn, I'm sure it will come in handy.  ;D

Krice

  • (Banned)
  • Rogueliker
  • ***
  • Posts: 2316
  • Karma: +0/-2
    • View Profile
    • Email
Re: [Python] Looking For Some Good Examples
« Reply #11 on: February 17, 2016, 08:57:09 PM »
This just cracked me up!  You see, it seems like you are trying to make some kind of jab at me for using Python, or for being a new coder

It's nothing personal and this is not the time to crack up. It'll come later.

tuturto

  • Rogueliker
  • ***
  • Posts: 259
  • Karma: +0/-0
    • View Profile
    • pyherc
Re: [Python] Looking For Some Good Examples
« Reply #12 on: February 18, 2016, 05:26:36 AM »
First - roguelikes rarely has a good code. I checked large amount of code of open source python roguelikes and I didn's see any with really good code. Even my own ;)

If you aren't super-busy, would you want to give a glance at: https://github.com/tuturto/pyherc (or at least at the parts written in Python, Lisp side might not be relevant in this topic) and put in your two pennies worth about the code? It's my first Python program of any significant size and getting some feedback would be super-cool and might be relevant to Hashiba too. Concrete examples about good or bad code is often better than just an abstract concept. I understand if you don't want to do this or don't have time of course.
Everyone you will ever meet knows something you don't.
 - Bill Nye

Avagart

  • 7DRL Reviewer
  • Rogueliker
  • *
  • Posts: 567
  • Karma: +0/-0
    • View Profile
Re: [Python] Looking For Some Good Examples
« Reply #13 on: February 18, 2016, 08:10:12 PM »
Probably I'm not the best person for judging the code - as I said in my first post, I started new project recently just for learning 'good coding' techniques. I'm not the python guru - these roguelikes which was generally mentioned earlier usually have code that looked very bad at / from first glance. You know, Writhing Mass of Code :) But if you want to I could give a glace at pyherc code, no problem.

Omnivore

  • Rogueliker
  • ***
  • Posts: 154
  • Karma: +0/-0
    • View Profile
Re: [Python] Looking For Some Good Examples
« Reply #14 on: February 18, 2016, 08:37:35 PM »
Not claiming to be the best person for the job nor claiming guru status, when it comes to Python I consider myself a journeyman/retired hobbyist.  However, I dropped by your repo and poked around. 

I liked what I saw.  Good commenting, seems to have good test coverage, the unit tests looked useful, the code base is organized, and it uses good solid Pythonic practices as far as I can see.  If I spent enough time I could probably nitpick something :)  There was nothing that jumped out at me as being "NO! WRONG!".  All in all, looks better than some of my own work.

Hope this helps,
Brian aka Omnivore

PS: couple of suggestions to consider; you might want to include the target version of Python in the file header info, and along the same lines, you might want to note somewhere a new Python programmer might see that the .hy files are for the lisp extension.  Newcomers probably aren't aware of the various add on file extensions that they might run across, pyx files, hy files, etc.
« Last Edit: February 19, 2016, 03:43:54 AM by Omnivore »