Code editors and style for Python 3

Need help with C, C++, perl, python, etc?

Re: Code editors and style for Python 3

Postby neuraleskimo » 2020-01-03 21:36

pylkko wrote:Sorry, I didn't mean to come off all elitist and shit.

No problem. I don't listen to people who use IDE's. Just joking. ;-)

I don't IDE's them because I rarely need the features. I primarily write C or C++ code, but rarely use the compile button and then the run button. Instead, I would rather control-tab to my terminal and hit the up arrow + return. I haven't used a debugger in decades. Instead, I write lots of tests, fully instrument my code, and use liberal logging. Besides running a debugger is hard or impossible in lots of scenarios. Firefox is the defacto documentation viewer. On and on... I will say that auto-completion is pretty nice SOMETIMES: when it is correct and when there aren't 50 variations in the popup.

My experience anyway. I won't think less of you for requiring training-wheels on your development tools! Just joking. ;-) Seriously, there are some nice IDE's and I am probably just a curmudgeon.
User avatar
neuraleskimo
 
Posts: 175
Joined: 2019-03-12 23:26
Location: Bloomington, Indiana, USA

Re: Code editors and style for Python 3

Postby arzgi » 2020-01-04 14:46

Wheelerof4te wrote:I don't really know how to use it and how to write (better said access) it. I know that I can define a base class and use it to create other sub-classed classes (urgh..). Then, the new class shares the attributes and methods of a base class. And that is all I know.

Honestly, it is very messy for me. I see classes only as a collection of associated attributes and functions (methods). Basically as structs. I can't wrap my finger around the concept of OOP yet.


There are many books of OOP programming, it's a wide subject. For my own projects, rarely have needed any classes (outside school projects so many years ago...), so I most program python in functional style. Blame M$ extended basic, which was the first I learned :mrgreen: Then if I write a program to some less technically oriented, some light GUI-frontend using tkinter (python3-tk package).

Again, use what you need, there is no single solution that works everywhere, and often there are more ways to achieve same result.
arzgi
 
Posts: 636
Joined: 2008-02-21 17:03
Location: Finland

Re: Code editors and style for Python 3

Postby Wheelerof4te » 2020-01-04 15:01

arzgi wrote:Again, use what you need, there is no single solution that works everywhere, and often there are more ways to achieve same result.


I fuly agree!
Ok, it's time to share my completed game with you all. Some of you remember how I started a thread about it a few months back. Well, now it is complete, but I deleted my github account. Nevertheless, here is a copy from my Google drive:
https://drive.google.com/open?id=1gilOe ... nvQDy6WSL4

If you are on Buster, make sure you have python3-libtcod installed in order to run the game. Otherwise, install python-tcod and change the import statement to read
import tcod as tcod

Controls are in the README file. Read the TIPS too, the game isn't easy :)

Enjoy!
Last edited by Wheelerof4te on 2020-01-13 11:48, edited 1 time in total.
Wheelerof4te
 
Posts: 1454
Joined: 2015-08-30 20:14

Re: Code editors and style for Python 3

Postby arzgi » 2020-01-04 15:06

If you are interested in game programming, pygame might be worth checking (python3-pygame package).
arzgi
 
Posts: 636
Joined: 2008-02-21 17:03
Location: Finland

Re: Code editors and style for Python 3

Postby Wheelerof4te » 2020-01-04 15:30

^To be honest, no, I'm not so interested in game development. At least not with Python. I have written this game ever since I started learning Python and it's very dear project to me. It shows all my rookie mistakes (only one file, heavy usage of globals, etc.). Ever since, I have grown out of my game devel dream and started using Python for what it's good at.

Pygame is also against what I have written before in this thread: I don't want to learn some 3rd party library because it feels like learning a new language. Especially if it's just a wrapper library for some C/C++ code.
Wheelerof4te
 
Posts: 1454
Joined: 2015-08-30 20:14

Re: Code editors and style for Python 3

Postby pylkko » 2020-01-05 10:36

I get the "i don't want to get too far a way from "pure" syntax" thing. But I don't know if it is good to worry about it too much. After all, every part of the language is defined and created somewhere. For example, in pure python, without importing external libraries you can use dictionaries to store data when it suites that format. But the source code of that is also out there:
https://github.com/python/cpython/blob/ ... ctobject.c

So, if you use a library which "hides" the technical detail from the user/coder... using such a thing is not "cheating" and being dumb, if you take the time to figure out what it is doing under the hood. The same thing also happens with the "pure" elements of your language. Many times looking up how the "basic elements" actually do stuff can also help you design your own software better. But there is a lot of variance here, some libraries do it in a less distracting way than others, and it can also be annoying if you return to that code after a long time and no longer remember how that one weird library did things.

I notice that you complain much about object-orientation and inheritance. I really understand that. Especially when you just want to write scripts to automate things in your own life as a linux user, it is mostly useless. But it is also very convenient for some purposes... for example saving money in a coding company when you just stick together new code from the pieces. I think that if you asked why people use these paradigms in software design, they would cite a lot of reasons that have never been "empirically tested" to be true/not. For example, it is claimed that this kind of code is more safe (because things like encapsulation help design safe code) or that it is "more productive" (because it is intuitive of a way for people to think and maximizes code reuse). Anyone could really easily contest any of these claims. I have always wondered a lot about how much superstition there appears to be amongst code developers. At first glance it appears to be a bit of a contradiction, because you would tend to think that coders are "scientific", after all it is a STEM subject. But there are large trends that come and go in software design, where suddenly everyone starts to feel that "we need to do this and that" without any easy-to-follow logic behind it. And I guess most software developers are not very "scientific" at heart and consider coding to be a kind of "craft".

I personally use julia a lot nowadays, although I still also use Python. This is for scientific data-analysis and scripting/automation at work. For personal stuff I use c in linux. I find that using c is not that much more annoying that python for simpler things like reading and writing files, making user interface and so on (the ncurses library is really quick to learn and nice to use). Usually if it requires some kind of network related stuff (like starting a server) then I use a python library (although I have seen people do this in c also). I also have a lot of self made objects with micro-controllers in them and there I have to use c anyway. Well, you don't have to anymore.. but it is still quite common because of the low resources that mcu's have (you don't need to have the Python interpreter on the device and c is faster, which is relevant on devices with like 50 MHz ARM cores or at times even worse). Although, a really pragmatic advantage of Python in these kind of situations is that once you have the binary constructed and sent your device, you need to keep the source code somewhere and document which devices has which version of the code, whereas with Python (or similar) you could simply read it of the device itself.

In many respects julia is like python, but it has way better performance, at times better than c and fortran code. However, since it reached 1.0 last year, the ecosystem is not as developed as in Python, though I am really expecting it to grow. It is not object-oriented, although it does have some similar properties, like multiple-dispatch (that you can have multiple versions of the same function with identical name, so that when you call that function it does different things with different data or whatever). It is also not interpreted but just-in-time compiled, although you do have a shell like in Python for interactive use. Almost all of the source code is in julia itself, which is makes it a lot easier to modify on all levels.
User avatar
pylkko
 
Posts: 1738
Joined: 2014-11-06 19:02

Re: Code editors and style for Python 3

Postby Wheelerof4te » 2020-01-05 11:50

pylkko wrote:For example, in pure python, without importing external libraries you can use dictionaries to store data when it suites that format.

Yes, it is much more efficient to use dictionaries. Using classes for storing data, however, is more flexible. Even more so with the recently added "data classes". As I have written above, I use classes the same way structs are used in c. What I don't use them for, is "Object Oriented Programming".

pylkko wrote:So, if you use a library which "hides" the technical detail from the user/coder... using such a thing is not "cheating" and being dumb, if you take the time to figure out what it is doing under the hood. The same thing also happens with the "pure" elements of your language. Many times looking up how the "basic elements" actually do stuff can also help you design your own software better.

I very much agree that using 3rd party libs is not cheating in any way. Python is designed to be modular, after all. Developers are even discussing stripping some modules from the stdlib, since almost everyone uses pip these days to install their favorite "do it all" library.
What I object to is the learning curve that is neaded to learn to be efficient with those 3rd party libs. In contrast, here are all of the standard library modules, together with the documentation:
https://docs.python.org/3/py-modindex.html
And this, too:
https://docs.python.org/3/library/

pylkko wrote: I think that if you asked why people use these paradigms in software design, they would cite a lot of reasons that have never been "empirically tested" to be true/not. For example, it is claimed that this kind of code is more safe (because things like encapsulation help design safe code) or that it is "more productive" (because it is intuitive of a way for people to think and maximizes code reuse).

Functions can also be reused. That's the major reason we write functions. Classes are intuitive because they represent real-life objects in a way. However, the functionality of those objects is often not applicable to software world, thus you throw that intuition in the air and confuse everyone. In Python, calling methods is slower than functions, which can ironically impact large-scale projects that classes are designed for.

pylkko wrote: I have always wondered a lot about how much superstition there appears to be amongst code developers.

A LOT. I am no expert in STEM or a scientist or anything such. I'm just a normal guy who liked software and computers ever since I was a kid.

pylkko wrote:In many respects julia is like python, but it has way better performance, at times better than c and fortran code. However, since it reached 1.0 last year, the ecosystem is not as developed as in Python, though I am really expecting it to grow. It is not object-oriented, although it does have some similar properties, like multiple-dispatch (that you can have multiple versions of the same function with identical name, so that when you call that function it does different things with different data or whatever). It is also not interpreted but just-in-time compiled, although you do have a shell like in Python for interactive use. Almost all of the source code is in julia itself, which is makes it a lot easier to modify on all levels.

I hope that it takes off, it sounds like julia is improved Python in every way. I guess it is now popular to write would be replacements to popular languages. We have Rust and Go for c++/c, now I hear about julia for Python. New ideas for the new generations of software developers.
Wheelerof4te
 
Posts: 1454
Joined: 2015-08-30 20:14

Re: Code editors and style for Python 3

Postby neuraleskimo » 2020-01-06 14:25

Sorry for the delay, I finally found some time to look at your code...

Wheelerof4te wrote:To be honest, no, I'm not so interested in game development. At least not with Python. I have written this game ever since I started learning Python and it's very dear project to me. It shows all my rookie mistakes (only one file, heavy usage of globals, etc.). Ever since, I have grown out of my game devel dream and started using Python for what it's good at.

Very good! You should be proud and rookie mistakes are no problem.

Wheelerof4te wrote:I don't really know how to use it and how to write (better said access) it. I know that I can define a base class and use it to create other sub-classed classes (urgh..). Then, the new class shares the attributes and methods of a base class. And that is all I know.

Honestly, it is very messy for me. I see classes only as a collection of associated attributes and functions (methods). Basically as structs. I can't wrap my finger around the concept of OOP yet.

As @arzgi said, there are many books on OOP and it would be good to read one. However, I am assuming: 1) you have read the Python docs and 2) you don't have formal training in programming.

Here is a two sentence summary... Inheritance allows programs to capture/model the hierarchical relationships among real-world objects. In static- (and strong-) typed languages (e.g., C++ or Java), inheritance (actually polymorphism) solves a signficiant technical problem. This second sentence is not really an issue for Python.

Looking at your code, you have a lot of players that all have one function defined: take_turn(). When you write code like that, inheritance is always an option. HOWEVER, in your instance, you really didn't need it. In Python, you can store instances of each class in a list with no problems. In C++ or Java, you would need inheritance. Now, had you stored some state in each character (e.g., location or health), that would be a good reason to use inheritance. Also, not a technical argument, but using inheritance in your game (while not technically needed) would allow someone reading and maintaining your code to see that the player class are related.

If you are up for a challenge, let's try a programming challenge. You seem to be interested in writing a personal finance app. Let's assume that you have two account types: saving and checking. Each account needs to track a balance and maintain a list of transactions. However, each account has different rules. Let's assume that the savings account can never have a balance less than $5 and will only accept deposits and withdrawals from a teller or ATM. Let's assume a checking account can have a negative balance, will accept deposits from anyone (e.g., a paycheck), and withdrawals from a teller, ATM, or check. How would you approach this problem? Avoid thinking about code as much as possible. Just describe the data, functions, and relationships in human-language.
User avatar
neuraleskimo
 
Posts: 175
Joined: 2019-03-12 23:26
Location: Bloomington, Indiana, USA

Re: Code editors and style for Python 3

Postby Wheelerof4te » 2020-01-06 15:10

^ Thank you for taking the time to read the code. Yes, all those AI classes have only one method, and that's to take their turn. The game is mostly complete for now, and it works. I will try to change it when I have the time.

Now, onto the challenge. I would make the savings account the base class since it shares two methods with the checking account, but lacks the third. Then, I would sub-class the checking account and give it the methods and data needed for negative balance and withrawing with a check or accepting deposits from everyone.
Thanks for the clear example :)

EDIT: I had a BankAccount class already in my fin.accounts module, so I tried to extend it with the example above. I don't think it looks very good, at least it's not very readable to me.

Code: Select all
class BankAccount:
    """
    My bank account data.
    """
    def __init__(self, owner, bank, amount, currency=""):
        self.owner = owner
        self.bank = bank
        self.amount = float(amount)
        self.currency = currency
        self.transactions = []
       
        if self.amount <= 5:
            self.amount = 5

    def deposit(self, amount):
        """
        Deposit an amount to the account.
        """
        self.amount += amount

    def withdraw_from_atm(self, amount):
        """
        Withdraw an amount from the account using ATM.
        """
        self.amount -= amount

    def change_owner_to(self, new_owner):
        """
        Change the account's owner.
        """
        self.owner = new_owner
       
class CheckingAccount(BankAccount):
    def __init__(self, owner, bank, amount, currency=""):
        super.__init__(owner, bank, amount, currency="")
       
    def check_withdrawal(self, amount):
        self.amount -= amount


I also removed the BookInc class from the previous example and replaced the entries with list of tuples. Now I have just this function to make .csv file:

Code: Select all
import csv

def napravi_csv(fakture: list, kol1: str, kol2: str, kol3: str, kol4: str, kol5: str, naziv_fajla="KUF.csv", obrazac="kuf: 02-"):
    with open(naziv_fajla, 'w', newline='') as csvfile: # Serbian
        writer = csv.writer(csvfile, delimiter=' ', quoting=csv.QUOTE_MINIMAL)
        writer.writerow([kol1] + [kol2] + [kol3] + [kol4] + [kol5])
        for fak in fakture:
            writer.writerow([obrazac + str(fak[0]) + "/" + str(fak[1])] + [fak[2]] + [fak[3]] + [fak[4]] + [fak[5]])


The function is in Serbian, but you can guess the gist of it. It takes the entries in list "fakture", writes the header files as the first row and every other row starts with pattern "obrazac", followed by the index of tuples as colums.
Wheelerof4te
 
Posts: 1454
Joined: 2015-08-30 20:14

Re: Code editors and style for Python 3

Postby neuraleskimo » 2020-01-08 14:49

Wheelerof4te wrote:^ Thank you for taking the time to read the code.

No problem. I am happy to help.

Wheelerof4te wrote:Now, onto the challenge. I would make the savings account the base class since it shares two methods with the checking account, but lacks the third. Then, I would sub-class the checking account and give it the methods and data needed for negative balance and withrawing with a check or accepting deposits from everyone.
Thanks for the clear example :)

EDIT: I had a BankAccount class already in my fin.accounts module, so I tried to extend it with the example above. I don't think it looks very good, at least it's not very readable to me.

Looks good. However, I put a subtle trick in the challenge. If you derive CheckingAccont from BankAccount, then CheckingAccount will inherit the minimum balance requirement from BankAccount. Now, let me add an extra requirement. Let's say that I now want the minimum balance to be configurable. Does that change your design?

Hint: Before thinking about code, how are a savings and checking account related in the human world? Is a checking account a special case of a savings account OR are checking and savings accounts both special cases of some generic account? In pseudo-code, class SavingsAccount(GenericAccount) and class CheckingAccount(GenericAccount).

Hint 2: Humans, dogs, and cats are all mammals. That is, humans, dogs, and cats have common properties that establish some relationship. Is there a "mammal" or is mammal just a concept?
User avatar
neuraleskimo
 
Posts: 175
Joined: 2019-03-12 23:26
Location: Bloomington, Indiana, USA

Previous

Return to Programming

Who is online

Users browsing this forum: No registered users and 4 guests

fashionable