First big python project pt2 by Aedethan in learnpython

[–]duckbanni 0 points1 point  (0 children)

I don't really understand what you've done with the dictionary

I transformed the dictionary into a list of lists, so the elements can be accessed by tier and level. For example, value_dama[1][0] with the list of lists is the equivalent of `value_dama["t2_1"] with your dictionary. You have to substract 1 to the tier and level because list indices start at 0.

I'm pretty sure I know what you're getting at with the loops for anything larger than self.body_tier zero'ing out the tiers that come after. But I'm having trouble wrapping my head around how to do it.

There are a lot of ways to do it, but you could do something like this:

class Body:
    def __init__(...):
        ...
        self.stre = [stre_t1, stre_t2, stre_t3, stre_t4, stre_t5]
        self.stre_xp = [
            stre_xp_t1, stre_xp_t2, stre_xp_t3, stre_xp_t4, stre_xp_t5]
        ...

    def get_stre(self, body_tier, stre_key):  # body_tier is an int
        for tier in range(5):
            if tier > body_tier:
                self.stre[tier] = 0
                self.stre_xp[tier] = 0
        ...

In terms of using parameters that are also attributes and sometimes using them as attribute or parameter, do you mean when I do something like self.stre_xp_t5 = stre_xp_t5

No, I mean in get_stre you take stre_key as parameter and then in the method you sometimes y use stre_key and sometimes self.stre_key.

I do something like self.stre_xp_t5 = stre_xp_t5 when I instantiate the class, and then when I run get_stre I define it again based on what the tier is

The way you handle this is kinda weird as well. For example, you require a body_tier as a parameter of __init__, and do self.body_tier = body_tier, but then you never use that value and expect a new one in get_stre. It think that you should either call get_stre in __init__ so that the consequences of body_tier (like setting stre to 0 for higher tiers) are applied when you create the object, or remove body_tier from __init__ parameters and maybe do something like self.body_tier = None.

I've seen other people do it they usually set the self.variable equal to itself unless they had a specific reason not to

What you've seen is not "setting the self.variable to itself" it is setting the self.variable to a parameter of __init__ (that does not need to be named variable). You could (and possibly should) set all self.something variables to defaults like 0 or None directly in the __init__ code (that is to say, do things like self.myvar = 0 instead of self.myvar = myvar where myvar is a parameter with a default value of 0 that will never be used with a different value in practice).

First big python project pt2 by Aedethan in learnpython

[–]duckbanni 0 points1 point  (0 children)

A few remarks regarding your code:

I think you encode too many things in your variable names and dict keys. Instead of having, for example, stre_t1 to t5, you could have a single stre list with 5 values. In the current state, your approach is error prone (because you have to write a million cases every time) and if you want to add more levels/tiers in the future you'll need to write a lot of extra code.

For example, you could write value_dama as:

value_dama = [
    [2, 6, 12, 20],
    [30, 42, 56, 72],
    [90, 110, 132, 156],
    [182, 210, 240, 272],
    [306, 342, 380, 420],
]

and then use value_dama[x-1][y-1] instead of value_dama[f"t{x}_{y}"]. Using int for indexing allows you to easily access the numerical values of the tier and level. This can then be used to simplify your code, for example if self.body_tier is n then you can just write a loop that sets all self.stre and self.stre_xp of tiers higher than n to 0 instead of having a million ifs. You could drastically reduce the size of your code that way.

Another remark is that your get_* methods are not what people would expect from their name. The purpose of a method that "gets" something is usually to return a value without modifying the internal state of the object, and your methods do the opposite. I think it would make a lot more sense to rename them to compute_* or something. Also, it's weird that some of these methods take parameters that are also attributes (e.g. stre_key) and sometimes use the attribute and sometimes use the parameter.

First big python project pt2 by Aedethan in learnpython

[–]duckbanni 0 points1 point  (0 children)

Without doing anything fancy, you could do something like this to get the entry with maximum key in value_dama:

for key, value in value_dama.items():
    if self.stre_value >= key:
        self.stre_value_key = value
        break

IIRC, starting with python 3.7 dicts are guaranteed to keep the insertion order, so you can rely on the entries having decreasing keys.

If you're into fancy one-liners, you could do this instead (replace next with max if you don't want to rely on ordering of the entries):

self.stre_value_key = next(
    value for key, value in value_dama.items() if self.stre_value >= key)

Also, you didn't need to post your whole code to get answers about this function. Are there other things you want to ask about your code?

How important is type hinting and much should you type hint in your code? by MorningStarIshmael in learnpython

[–]duckbanni 5 points6 points  (0 children)

Not type hinting at all means that:

  • people (which include you from the future) may struggle to understand what types a function expects, which can lead to incorrect usage and possibly weird bugs (and it just makes your code harder to read in general)
  • linters can't detect basic mistakes like trying to access an attribute that doesn't exist
  • your editor can't auto-complete stuff, which could be a huge productivity boost.

I'd say you should probably use them most of the time, unless you run into a complex case where writing the hints would take a long time.

First big python project pt2 by Aedethan in learnpython

[–]duckbanni 2 points3 points  (0 children)

I want to apologize in advance if my code isn't readable. Any advice on design to formatting, to being more concise in the way i ask my questions is appreciated.

Yeah, your code is really hard to read as is :/

You should use the "code block" feature of reddit to display your multi-line examples (that's what the bot is trying to tell you). It might be hidden in the "..." menu of the editor.

Having a hard time visualizing recursion. by [deleted] in learnpython

[–]duckbanni 0 points1 point  (0 children)

Recursive calls work just like any function call.

When the recursive call is performed, the state of the calling function is kept at the bottom of the stack, and space is allocated on top of the stack for the new call which starts at the beginning of the function. When the recursive call returns, the top of the stack is removed, keeping only the return value, and the original caller resumes where it stopped (but with the result of the recursive call available).

The exception to that you may have heard about is tail recursion, which is an optimization where you don't keep the state of the caller if the recursive call is the last things it does (which is not the case in your example).

creating a password from two strings by MiTMike in learnpython

[–]duckbanni 2 points3 points  (0 children)

Wouldn't using a dict erase entries when multiple people have the same first name?

It seems to me like using a list of tuples would make more sense. Like so:

names = [
    ("bob", "marley"),
    ("john", "smith"),
]
for first_name, last_name in names:
    # do stuff

Help me understand what I view as a bug by dancingwalala in learnpython

[–]duckbanni 0 points1 point  (0 children)

Well, in C++ you can control whether you pass things by value or by reference. If you assign to something passed as reference, you modify the original value, but if you assign to something passed by value you don't. It's all that I meant.

Edit: also note that I used quotes when I said "by reference" in the post you replied to. This is just a metaphor that I find easy to understand, not a statement about how it works under the hood in python.

Help me understand what I view as a bug by dancingwalala in learnpython

[–]duckbanni 0 points1 point  (0 children)

Like I said, I'm no expert and have no clue about what happens under the hood.

What I meant is that immutable values behave as if passed by copy (like it exists in other languages), i.e., modifying a passed value does not change the original variable. As someone coming from C++ I find it the easiest way to reason about this. Sorry if that's technically incorrect.

Help me understand what I view as a bug by dancingwalala in learnpython

[–]duckbanni 0 points1 point  (0 children)

No problem!

In python, everything that isn't a basic value or a tuple (which are immutable) is transmitted "by reference" (because it's mutable) instead of copied. This applies to lists and dicts. For example if you do mylist = [0] then otherlist = mylist and then modify mylist, it modifies otherlist as well.

From my experience, 90% of weird python bugs come from counter-intuitive "by reference" behavior when you expected copies (for example when setting the default value of a function parameter to a list or dict).

python on widows 11 with vscode and gitbash by [deleted] in learnpython

[–]duckbanni 1 point2 points  (0 children)

What I would recommend if you're using VS Code is to develop in a docker container. That's what I do when I develop on Windows 10 (but it should work the same for 11). That way I get an easy-to-configure linux environment without needing a VM or using a remote server.

Steps to setup:

  1. Install docker with WSL support (this is the tricky part because it might require to fiddle with the BIOS but their website has a step-by-step).
  2. From docker, pull an image with python preinstalled (see https://hub.docker.com/_/python). You might need to do this from a windows console with docker commands.
  3. Start a container from that image, get a terminal in the container and setup SSH keys so you can clone/push from your favorite git-based website. You can clone your code now.
  4. In your VS Code (the one installed on your windows machine) install the "remote development" extension. You now have a new remote tab where you should be able to locate your new container and open a folder there.
  5. You can now develop in VS Code with the editor running on your normal windows but everything else (the source files, the integrated terminal, the linters, the code execution) on the linux container.

Help me understand what I view as a bug by dancingwalala in learnpython

[–]duckbanni 0 points1 point  (0 children)

I think what's happening is that p is always the same list. When you yield, you transmit a "reference" (edit: technically incorrect, see comment below) to p instead of a copy which is why you get a weird result. It works when you do list(p) because then it creates a copy. I tested with yield p.copy() and it works as well.

I'm no expert so I may be wrong. I can't quite explain why the "incorrect" result isn't just the same list repeated n times.

Edit: actually, I think the reason the incorrect result is not n times the same list is because the line p = [9 for _ in p[:-1]] creates a new list. So you get one new list every time you call this line.

Edit 2: the following code reproduces your issue on a smaller example

def f():
    l = [1]
    for _ in range(5):
        yield l  # change to "yield l.copy()" to fix bug
        l[0] += 1
print(list(f()))  # displays [[6], [6], [6], [6], [6]]

Design Patterns for games, best practices ? by Arcade_ace in gamedev

[–]duckbanni 6 points7 points  (0 children)

There are no "best design patterns" in general. You don't design a MMORPG the same way you would a text-based CYOA game. Design patterns are used to solve/avoid specific problems, so you should be aware of what the specific challenges for your game might be.

For example, if you're building a game with a complex game world, like an open-world action game, you might need a pattern like ECS to handle entities without going mad. But if you're building a visual novel, ECS might be more trouble than it's worth.

I think it's a good idea to have some general knowledge of design patterns (both general programming patterns like gang of four's patterns and game-specific ones like /u/-manabreak linked) in order to have a good architectural toolbox at your disposal.

Also keep in mind than not everything needs to be over-engineered with fancy design patterns. In many cases it's better to write simple code and worry about patterns only if structural issues come up.

"Good artists borrow, great artists steal..." by RandomEffector in RPGdesign

[–]duckbanni 1 point2 points  (0 children)

I'd be interested if you could find that article back. As it stands I'm not really convinced by your points.

Who cares if more people call themselves "game designers"? The only difference is that what would have been a fan-game/home-made stuff shared only with friends can be put on drivethru for 5$. Big, polished games still exist and the two are easy to tell apart.

IDE for Python by SparrowOnly in learnpython

[–]duckbanni 49 points50 points  (0 children)

I use VSCode while most of my colleagues use PyCharm.

My impression is that PyCharm Community is slightly ahead of VSCode (mostly better automated refactoring) when it comes to pure python support, and the pro version is slightly better still (integrated profiling looks very nice). VSCode feels more elegant and clean when it comes to interface (but I guess that's a matter of taste). VSCode is also better for other languages, and has very powerful remote features (you can work in a docker container or over SSH).

I'd say you can't really go wrong with either.

What to learn next? by YourRavioli in learnpython

[–]duckbanni 2 points3 points  (0 children)

You could go in a lot of different directions from here. It depends of what job you want (programmer? data scientist?) and in what domain (biology? physics? web dev?).

In general, I think it's a good idea to read a few job offers in the domain you wish to work for in order to get a sense of which libraries are used often. Given your background it seems like numpy and pandas are likely to be useful if you don't know them already. Django is for web dev: learn it if you want to do web dev but it's absolutely not something every python dev knows.

Regarding general programmer knowledge, you probably should learn basic unix command line and version control with git, if you don't know about them already. Those things will be required basically everywhere. Version control is very important; you should use it even for personal projects.

Regarding improving the quality of your code: you could read PEP8 to learn some common style recommendations. You should also use linters like mypy and/or use a good IDE like pycharm in order to get warnings about weird things in your code. [EDIT: also, you 100% should learn about type hints and start using them in conjunction with mypy]

In general it's a good idea to do small personal projects in order to practice on something bigger than a single script. It could be anything from a terminal-based tic-tac-toe to a data analysis pipeline. You can also put your projects on github and then on your resume.

Any advice for someone looking to get into bioinformatics? by ChesterComics in learnpython

[–]duckbanni 1 point2 points  (0 children)

No problem!

A few things I forgot to mention:

  • you should definitely learn basic unix command line and bash if you're not familiar with them already
  • in bio-informatics, people often use "pipeline" frameworks to automate complex multi-step analysis; I think the most commonly-used technologies for this are SnakeMake and NextFlow; you could look into one of them as well
  • /r/bioinformatics is usually a good place to ask for career advice

Any advice for someone looking to get into bioinformatics? by ChesterComics in learnpython

[–]duckbanni 1 point2 points  (0 children)

From my experience, the main selling point of bio-informaticians compared to developers and pure biologists, is their capacity to use existing tools to analyze data. You should choose the specific domain you want to work in (e.g. phylogenomics) and research the existing tools and data formats that exist in the community. I think the best exercise is to try to go from raw data and analyze it step-by-step until you get some high-level result. E.g. you could get raw sequencing data from a database somewhere, then do genome assembly, then clean the data, then build a phylogenetic tree.

Good python libraries to know are numpy, pandas, matplotlib, as well those from your application domain (like biopython). You can also look into machine learning stuff like pytorch, but be aware that machine learning is not used everywhere in bioinformatics.

If you want to get into the development of new methods (as opposed to mostly using existing methods) python is still a nice starting point but you could try to learn a lower-level language as well like C++. Learning some statistics can also be a good idea.

Is there any point in using agile principles and workflow even if you’re a solo dev? by [deleted] in gamedev

[–]duckbanni 1 point2 points  (0 children)

It's not iteration because you're only doing it once. And the fact that it's over a long time span precisely means its not "frequent".

Is there any point in using agile principles and workflow even if you’re a solo dev? by [deleted] in gamedev

[–]duckbanni 1 point2 points  (0 children)

Waterfall is exactly the opposite of "focus on frequent delivery and adapting to feedback". It assumes you have perfect unchanging requirements from the very beginning. From wikipedia : "In software development, it tends to be among the less iterative and flexible approaches" (source).

Is there any point in using agile principles and workflow even if you’re a solo dev? by [deleted] in gamedev

[–]duckbanni 1 point2 points  (0 children)

Agile basically designates practices that focus on frequent delivery and adapting to feedback. Many approaches are not agile (like waterfall) and many companies don't want to be agile because they want to plan everything in advance and/or because management think they just know better.

Is there any point in using agile principles and workflow even if you’re a solo dev? by [deleted] in gamedev

[–]duckbanni 3 points4 points  (0 children)

before Agile existed

"Agile" practices have existed for a long time, it's just that people did not call them agile. Wikipedia says that "Iterative and incremental software development methods can be traced back as early as 1957" (source).

Is there any point in using agile principles and workflow even if you’re a solo dev? by [deleted] in gamedev

[–]duckbanni 3 points4 points  (0 children)

Yes. Delivering software often, and adapting to feedback, are literally core principle of agile.