Understanding PyMMO

Started by
17 comments, last by Sky Warden 10 years, 11 months ago

Yes, pretty much. You need to figure out a clean way to pack 1) the instruction name and 2) the arguments on the client side, and a way to unpack them properly on the server side. A simple way to do this would be to encode this in JSON; that may not be terribly efficient, but it's a simple place to start. Python has standard libs for encoding and decoding JSON: http://docs.python.org/2/library/json.html.

Thank you. This kind of information is what I'm always looking for. It will be very nice if someone can give me a list of "things or libraries you should learn for your programming." :lol:

I will learn it right away. Maybe tomorrow. It's late in the night here.

Two words: "code injection". http://en.wikipedia.org/wiki/Code_injection

Regardless of the security risks, there are almost always better alternatives to `eval`. In your case, using an explicitly defined map of instructions is one of those alternatives.

Ah yes. Code injection. I didn't realize that. ohmy.png

Wait. Maybe I get it wrong, but you said alternative. Does that mean I can avoid 'eval'? I thought the instruction map is just a way to make sure that the sent instruction is available and valid. The client sends the instruction in string, and if the instruction is in the map, it uses 'eval' to execute it. Do I get it wrong or I just misunderstand the word 'alternative'?

Advertisement

Wait. Maybe I get it wrong, but you said alternative. Does that mean I can avoid 'eval'? I thought the instruction map is just a way to make sure that the sent instruction is available and valid. The client sends the instruction in string, and if the instruction is in the map, it uses 'eval' to execute it. Do I get it wrong or I just misunderstand the word 'alternative'?

You don't need `eval` to execute it.


def move_character(char_id, vector):
    # TODO:
    pass

def kill_character(char_id):
    # TODO:
    pass

command_map = {
    'move_character': move_character,
    'kill_character': kill_character,
}

# sent from client; some string
command_name = 'move_character'
# also sent from client; a tuple or list, for example, which can be empty
# if the command doesn't take any arguments
command_args = (15, <whatever vector is>)

command_func = command_map.get(command_name)
if command_func is not None:
    command_func(*command_args)
else:
    # Do something else if the command is invalid

To understand the * sytanx, read http://docs.python.org/2/tutorial/controlflow.html#more-on-defining-functions, specifically http://docs.python.org/2/tutorial/controlflow.html#arbitrary-argument-lists and http://docs.python.org/2/tutorial/controlflow.html#unpacking-argument-lists.

Wait. Maybe I get it wrong, but you said alternative. Does that mean I can avoid 'eval'? I thought the instruction map is just a way to make sure that the sent instruction is available and valid. The client sends the instruction in string, and if the instruction is in the map, it uses 'eval' to execute it. Do I get it wrong or I just misunderstand the word 'alternative'?

You don't need `eval` to execute it.


def move_character(char_id, vector):
    # TODO:
    pass

def kill_character(char_id):
    # TODO:
    pass

command_map = {
    'move_character': move_character,
    'kill_character': kill_character,
}

# sent from client; some string
command_name = 'move_character'
# also sent from client; a tuple or list, for example, which can be empty
# if the command doesn't take any arguments
command_args = (15, <whatever vector is>)

command_func = command_map.get(command_name)
if command_func is not None:
    command_func(*command_args)
else:
    # Do something else if the command is invalid

To understand the * sytanx, read http://docs.python.org/2/tutorial/controlflow.html#more-on-defining-functions, specifically http://docs.python.org/2/tutorial/controlflow.html#arbitrary-argument-lists and http://docs.python.org/2/tutorial/controlflow.html#unpacking-argument-lists.

That's sweet. Thanks.

I'm going to write my own version of game server. I really want to test my knowledge. I must confess that actually I haven't analyzed that pymmo (I just analyzed one file). I spent my time to study something else. laugh.png

For the database I will use MySQL and SQLAlchemy, and Twisted for the network. If you know any interesting library which can help me doing this thing, tell me please. That is, if you don't mind it.

P.S. I love this community. biggrin.png

For the database I will use MySQL and SQLAlchemy, and Twisted for the network. If you know any interesting library which can help me doing this thing, tell me please. That is, if you don't mind it.

The philosophy of Python's standard libraries is "batteries included", so pretty much everything you'll need exists in the standard lib (apart from things like Twisted and SQLAlchemy). So it depends on what you want to do. Google is your friend in this case. If you want to do some particular thing in your code, just search "python <particular thing>". This will tell you pretty quickly if there is a package in the standard lib to help you with this.

It would be pointless to suggest particular libraries with knowing exactly what you're building. However, being rather passionate about Python, I will suggest a few other subjects for further reading:

  1. Python style guide (PEP8) - http://www.python.org/dev/peps/pep-0008/ <--- read this twice
  2. pep8 tool (for automatically checking if your code meets style guidelines) - https://pypi.python.org/pypi/pep8
  3. Learn about unit testing and write tests for your code - http://docs.python.org/2/library/unittest.html
  4. From a command prompt, type this: python -c "import this". Read.

For the database I will use MySQL and SQLAlchemy, and Twisted for the network. If you know any interesting library which can help me doing this thing, tell me please. That is, if you don't mind it.

The philosophy of Python's standard libraries is "batteries included", so pretty much everything you'll need exists in the standard lib (apart from things like Twisted and SQLAlchemy). So it depends on what you want to do. Google is your friend in this case. If you want to do some particular thing in your code, just search "python <particular thing>". This will tell you pretty quickly if there is a package in the standard lib to help you with this.

It would be pointless to suggest particular libraries with knowing exactly what you're building. However, being rather passionate about Python, I will suggest a few other subjects for further reading:

  1. Python style guide (PEP8) - http://www.python.org/dev/peps/pep-0008/ <--- read this twice
  2. pep8 tool (for automatically checking if your code meets style guidelines) - https://pypi.python.org/pypi/pep8
  3. Learn about unit testing and write tests for your code - http://docs.python.org/2/library/unittest.html
  4. From a command prompt, type this: python -c "import this". Read.

I've read some of the style guide before, but haven't finished yet. I think I will finish it now, but don't worry, my code is pretty neat at the moment. Also, that tool will be useful.

I will start coding now. Is there a place here to post my code (when it's finished) so I can get feedback or review about it? I think getting some feedbacks will be nice.

I've read some of the style guide before, but haven't finished yet. I think I will finish it now, but don't worry, my code is pretty neat at the moment. Also, that tool will be useful.


I'm not worried. But someone else (including the future you) trying to read your code might. =)

I will start coding now. Is there a place here to post my code (when it's finished) so I can get feedback or review about it? I think getting some feedbacks will be nice.

Yes, there are many places to store your code online and receive feedback, which leads me to another important software development topic: version control. Are you using version control? If not, start using a version control system now.

One of the best places (in my opinion) to post code is GitHub.com. You'll need to learn how to use git, which is a great skill to have. Many people are using git. You can either store your code in your repository, or you can use `gists` which are a lighter-weight method of storing code and text. `gists` are ideal for small bodies of code (scripts, prototypes, examples, etc.), whereas a full repository is ideal for large bodies of code (a project, a library, etc.).

I recommend starting with gists first, because you don't need to learn git commands to use them. Gists are also good because they're tied to your github account, so you can find them easily. (As a software developer, github is a create place to build a portfolio. Many potential employers will ask for code samples; it's great to just be able to point them to your github page.)

Another simpler, quicker, and slightly more crude way to post code is to use something like pastebin.com or pastie.org. Both services can do syntax highlighting for a bunch of major languages (bash, C, XML, Java, Python, etc.).

ABOUT LICENSING:

If you're going to post code, make sure the license of the code is clear. This can either be done with a license header in the file (example: https://gist.github.com/larsbutler/5330230) or with a LICENSE file in the repository (example: https://github.com/larsbutler/geomet/blob/master/LICENSE.txt). If the code belongs to someone else, be sure to note who wrote it and how it is licensed. A lot of people who post code on their blogs do not specify a license for their code, which is a mistake in my opinion. (This leads to people either using code in such a way that the original author didn't intended or doesn't like, or simply not being able to use the code for fear of infringing on a copyright. The pymmo code, for example, did not include a license, as far as I can tell.

For your original code, you can choose an open source license so people can use your code. See http://en.wikipedia.org/wiki/List_of_software_licenses and http://opensource.org/licenses to find out more. If you don't really care that much and want to license your code in an extremely "free" way, the BSD or MIT license is a good choice.

If you want to post your code publicly, but you don't necessarily want to use an open source license, you can simply include a statement in your code that you are the copyright owner and that use of this code is not permitted for commercial/personal/educational (be specific as you want) purposes. Just be aware that if your code derived from open source licensed code, the license of that code may prohibit you from "closing" your code in this way.

I'm not worried. But someone else (including the future you) trying to read your code might. =)

Perhaps. I remember reading a code I typed a year ago, and... I don't want to talk about it. :lol:

ABOUT LICENSING:

Oh good sir. I think I can't thank you enough. I was keeping this question and waiting for the right time to ask, and suddenly, you answered it.

As far as my concern goes, what if I make something that uses other libraries? Just say, I make this game server thingy and it uses Twisted and SQLAlchemy. Do I have to write the credits, ask for permission to the developers, or something like that? I'm not so good at this thing called 'law'.

Also, is a bunch of libraries, modules, etc can be called a framework? Then what makes a framework a good framework? Maybe sounds random, but I would like to know if something that I make is a framework or not. It will be so embarrassing if I call things which isn't a framework "a framework" or vice versa.

For the python standard libs, you shouldn't need any special citation. For other libs, check the project web page or source code for info about the license. When in doubt, ask the authors via email or IRC. Even if you do make a slight faux pas and forget to cite a lib properly, this shouldn't be a big problem unless you are trying to sell a product. But I'm not a lawyer, so do not take this as legal advice. Again, when in doubt, ask the author.

Regarding frameworks and libs, I see that you're trying to understand terminology. Knowing jargon and using it properly is good. :)

A library is simply a collection of functionality which encapsulates the details of a particular set of related tasks. An XML library is a good example (like the standard python 'xml' lib).

A framework may contain library-like code (and indeed, can be imported and used only as a library), but the distinguishing feature is that it suggests/forces you to construct your application a certain way. While a library will help you solve a particular problem in your application by abstracting away complexity, a framework will help you design, structure, and organize your code, in addition to abstracting away complexity.

Those are the best definitions I can come up with, in my own words. Someone else might have a better definition.

For the python standard libs, you shouldn't need any special citation. For other libs, check the project web page or source code for info about the license. When in doubt, ask the authors via email or IRC. Even if you do make a slight faux pas and forget to cite a lib properly, this shouldn't be a big problem unless you are trying to sell a product. But I'm not a lawyer, so do not take this as legal advice. Again, when in doubt, ask the author.

Regarding frameworks and libs, I see that you're trying to understand terminology. Knowing jargon and using it properly is good. smile.png

A library is simply a collection of functionality which encapsulates the details of a particular set of related tasks. An XML library is a good example (like the standard python 'xml' lib).

A framework may contain library-like code (and indeed, can be imported and used only as a library), but the distinguishing feature is that it suggests/forces you to construct your application a certain way. While a library will help you solve a particular problem in your application by abstracting away complexity, a framework will help you design, structure, and organize your code, in addition to abstracting away complexity.

Those are the best definitions I can come up with, in my own words. Someone else might have a better definition.

Thank you for the information. It gives me enough idea about what a framework is. Like Code Igniter right? I think I will make one that ease mmo development (everyone needs a project to improve right?), but sure I still need to learn many things. Well, I will try to make a game server first. I will figure out about the framework later.

Thank you again. :)

This topic is closed to new replies.

Advertisement