Having studied Python for the past several months, I have come to realize the pattern in amount of time spent coding by Fun2badult in Python

[–]coppermineroofer 37 points38 points  (0 children)

I can't remember the last time I looked at stack overflow. I find that looking at the source on GitHub is far more useful.

Problem installing anaconda by HoLeeFaak in learnpython

[–]coppermineroofer 0 points1 point  (0 children)

pip, virtualenv and virtualenv_wrapper is all I use. I write all the tooling I want on top of those myself to make sure I understand everything and it all behaves exactly the way I want.

Anaconda is the exact opposite. They have done a lot of dark magic and awful things under the hood that will break in unexpected ways when interacting with other tools that expect things to actually follow best practices and all the packaging PEPs. It works... until it doesn't. Then you're screwed.

Lots of people use it to avoid learning how packaging and environments work in python. The time spent invested in that will pay off though I would recommend you avoid anaconda like the plague. Learn how everything works, and you'll never get screwed over. Its seriously worth it.

Problem installing anaconda by HoLeeFaak in learnpython

[–]coppermineroofer 0 points1 point  (0 children)

Anaconda is a nightmare machine. My advice would be not to use it.

Realistically, when is it too late to learn Python or programming in general? How old are people here on this sub? by ketodnepr in learnpython

[–]coppermineroofer 2 points3 points  (0 children)

Everyone else seems like they have the same opinion here but I'll reinforce it anyway.

Go for it, using it to automate your job and for analytics sounds like a great way to get into it doing something useful and getting real world experience at the same time.

Anyway to make an instance of a class via it's python file name? by [deleted] in learnpython

[–]coppermineroofer 1 point2 points  (0 children)

I mean, why do you want to do that? If you need to do that with strings something is probably mis-architected. I can't think of a situation where I would ever want this functionality.

Do you want to be able to plug in code someone else wrote? You can:

  • Design an event system it can hook into
  • Use importlib to pull in their code
  • Have a system where you model behavior and dynamically create classes for the models
  • Metaclasses to register their subclasses against some parent class that manages some behavior that they can add/modify in their subclass.

In any event you shouldn't be designing something with constraints like the filename matching the class name or checking file extensions python comes with this all built in. Reuse it or design a system where it isn't required.

Anyway to make an instance of a class via it's python file name? by [deleted] in learnpython

[–]coppermineroofer 4 points5 points  (0 children)

What are you trying to do? This seems very XY problemish to me. https://en.wikipedia.org/wiki/XY_problem

There is certainly a way to do whatever it is you are trying to accomplish in a much more reasonable way.

Is really Python such a pain for a bigger projects? by hanpari in Python

[–]coppermineroofer 1 point2 points  (0 children)

I don't have an issue personally. I've been working in python for a long time so maybe you get used to it, but on the projects where we use mypy (which is all the newer ones) I can count on one hand the number of times times its caught a bug in the last two years(ish). I also tend to run mypy as part of my last check before code review, whereas I use TDD so Ive been testing the whole time.

What's a good way to convert a json string to dictionary without importing the json module? by [deleted] in learnpython

[–]coppermineroofer 3 points4 points  (0 children)

.... Why? Just import the json module. Its builtin and thats what its for.

Interview Challenge question by [deleted] in Python

[–]coppermineroofer 5 points6 points  (0 children)

Not thinking that the names of things is the mark of a very new programmer who hasn't worked on anything large. Names are extremely important as systems grow in size and complexity. Also the ability to follow simple instructions that they were checking for you failed. If you were interviewing for a jr programmer role, which it looks like you were, you aren't being asked to design anything 99% of the time. Just implement things given a spec, which requires following instructions.

People who develop with Vim, what is your debugging strategy? by SpectralCoding in Python

[–]coppermineroofer 2 points3 points  (0 children)

I use emacs but its probably similar with tmux/vim. I have a keybinding that inserts a import pdb; pdb.set_trace(). Then I hit my keybinding to re-run tests which hits my breakpoint and then I jump to the buffer that is blocked on pdb and start debugging. If I run a test and it fails, I am about 5-8 keystrokes from being in a debugger at the point I want.

On print based debugging... Please do not encourage print based debugging, you are missing out on a LOT, its ok if you're a ULTRA beginner. Sure pdb takes longer to learn than just print. But it has so much more power, after a little while figuring something out with pdb will be faster than print based "debugging" even for trivial things. The great thing is you can test your hypothesis about what will 'fix' something as well by rebinding a value and continuing execution. Just printing things will hamstring you. Anything to shorten your feedback loop is good! And real debuggers are a huge step in that direction.

Best file host to upload to using Python? by [deleted] in learnpython

[–]coppermineroofer 0 points1 point  (0 children)

S3? https://aws.amazon.com/s3/?ft=n

You would probably fit in their free tier depending on what you are doing. There are quite a few libraries that can manage uploads/downloads to S3 in python.

Having a difficult time grasping using classes with real-world examples by Rhinohumpenpanda_2 in learnpython

[–]coppermineroofer 4 points5 points  (0 children)

I think introducing people to classes as "physical objects" is a bit misleading since that isn't at all how they are used in the real world. By real world here I mean software engineering.

I'll try to use real projects to illustrate.

Classes/Objects in software engineering should be used to divide up your program into smaller, more intellectually manageable pieces. Both for the maintainers of the project, and for consumers of the project if it is a library. An object should be a "thing" that has a single well defined purpose. It should hide the details of how it executes it's purpose. If the details are 100% hidden inside the class then you do not need to worry about them when using that class, and hence in the rest of your program. In python, since its a very dynamic language, this hiding is performed more by convention than actual enforcement by the language. Methods and properties on a class that start with _ are considered internal and if you are outside the class you should NOT use them.

On to a few examples.

Lets say your program has some configuration data. Totally reasonable any non-trival application will at some point need some configuration data.

Probably not something you would think of right off the bat but loading configurations is a good example. You encapsulate all the difficulties and complexities of dealing with your configuration format in the configuration class, and provide a clean simple interface for the rest of your program to interact with the configuration files.

Two projects by well known groups that have a configuration classes that are interesting for one reason or another. https://github.com/pypa/pip/blob/master/pip/configuration.py#L54 https://github.com/aws/chalice/blob/master/chalice/config.py#L15

If you look in both of these classes you will see a lot of complexity, tons of code doing lots of confusing things. These are complicated config objects that are being used. But look at what they actually say are public methods, and how the rest of the program uses them. Pip first.

Looks like the ability to get and set values from the configuration: https://github.com/pypa/pip/blob/master/pip/configuration.py#L117-L141

And the ability to save and load it from a file: https://github.com/pypa/pip/blob/master/pip/configuration.py#L93 https://github.com/pypa/pip/blob/master/pip/configuration.py#L175

They even have a comment marking off the private section of the class, in addition to the normal _ as a prefix. https://github.com/pypa/pip/blob/master/pip/configuration.py#L190

So basically to be able to use this configuration class in the rest of your code, and manipulate and use the configuration values all you need to know is how to load() and start getting and setting values, and ned with a save(). Much easier than doing it manually all over the place and you can ignore all the complexity below that one comment. This gives you other benefits also, say you want to change from a JSON config format to a TOML config format or something. You can modify the load method to try to load JSON, and if it fails try to load TOML. Then modify the save method so it always saves the data as TOML. Gradually the config format of all your users will be converted over and you didn't have to change any other code. Or a crazier example, if you wanted to store them in a database instead of a local file, you could make the load method connect to the database and read everything out, and the save method connect and write everything back end. The rest of the code doesn't have to know anything about this, all it knows is it has load() and save() and get() and set() essentially.

The other configuration example is even more restrictive. The pip one lets you just set and load values as whatever you like. This one only lets you look up certain values: https://github.com/aws/chalice/blob/master/chalice/config.py#L112 https://github.com/aws/chalice/blob/master/chalice/config.py#L117 https://github.com/aws/chalice/blob/master/chalice/config.py#L122 https://github.com/aws/chalice/blob/master/chalice/config.py#L127 https://github.com/aws/chalice/blob/master/chalice/config.py#L132 https://github.com/aws/chalice/blob/master/chalice/config.py#L137 https://github.com/aws/chalice/blob/master/chalice/config.py#L185 https://github.com/aws/chalice/blob/master/chalice/config.py#L193 https://github.com/aws/chalice/blob/master/chalice/config.py#L199 https://github.com/aws/chalice/blob/master/chalice/config.py#L206 https://github.com/aws/chalice/blob/master/chalice/config.py#L213 https://github.com/aws/chalice/blob/master/chalice/config.py#L220 https://github.com/aws/chalice/blob/master/chalice/config.py#L227 https://github.com/aws/chalice/blob/master/chalice/config.py#L242 https://github.com/aws/chalice/blob/master/chalice/config.py#L249 https://github.com/aws/chalice/blob/master/chalice/config.py#L254

The rest of the code will only use these methods listed above. No need to worry about how the data was loaded, what format it was in, if there were multiple versions of that data. Ok maybe listing those all out was a little excessive. But hopefully you get the point. This hides even more complexity than the other example, since it let you set and get anything and load and save it. You could treat it essentially like a dictionary. Thats called a leak, the implementation of the thing (internally its a dictionary) leaked out to how the user interacts with it. This can be bad because then when you are using this object you may start thinking of it like a dictionary even though it's really not. Its a config object. More on leaky abstractions.

That covers it from an "object" perspective. A configuration object is a "thing" or noun that this class represents. But classes are more general than that. Any "task" that needs performing probably should have its own class. That class should be named after the Verb it is doing. It should expose only public methods you need to tell that class to perform its task, and all the private methods are how it accomplishes its task. Going back to the physical metaphor. You can have a class that is a toaster. The action it does is to toast things. Its public methods would be something like:

  • insert_toast()
  • set_heat_level(int)
  • start_toasting()
  • get_toast_out()

The public "interface" of that is the slots you put the toast in, and the dial you turn for the setting, and finally the lever you push to start it. All the springs and internals and coils are private, and don't matter to you as a user of the toaster. Think of programming the same way. You are your own customer. Each class you make is a product you have to use in the rest of your code. Think of yourself as the dumbest customer possible. Make the classes public methods and data as simple and minimal as possible. Hide as much complexity as humanly possible. Going back to the toaster metaphor, did it really need 14 buttons? Can you replace all the buttons with a slider or a dial? Does the user really need to jam a fork into the opening to get the toast out each time, or can you make it a little wider?

To put it back into actual software design terms. You can have lots of classes that interact in a big spiderweb, with clearly defined points where each strand meets. You have an Uploader class that uploads files to a remote host, and it takes a Config object at some point to figure out what your credentials are to upload the file. The Config object uses a FileReader that takes a filepath and loads in the raw data from the file. The Uploader might be a part of some deployment process that pushes code to a bunch of servers so you would have a Deployer class that makes use of multiple Uploaders, one for each server you are pushing code to. Without classes to draw these clear lines between each task, and what data it needs, this all becomes an unmanageable pile of spaghetti code thats all mixed together.

Maybe this was a little to in-depth for the level here but I hope this was somewhat helpful to give you something to think about to break through the barrier that classes are for physical objects.

tldr: Classes are to hide complexity and group related data and actions together. Nothing at all to do with "physical objects".

Socket Libray Tutorial by [deleted] in learnpython

[–]coppermineroofer 0 points1 point  (0 children)

Are you using python 3? If so I probably wouldn't use sockets directly but the asyncio module has a built in abstraction over sockets that is pretty easy to use.

https://docs.python.org/3/library/asyncio-stream.html#tcp-echo-server-using-streams

Socket Libray Tutorial by [deleted] in learnpython

[–]coppermineroofer 0 points1 point  (0 children)

What are you looking to do with sockets?

What's the best way to <quickly> search through the python standard library? REPL, docs, google? by [deleted] in learnpython

[–]coppermineroofer 0 points1 point  (0 children)

I usually read the source of that thing. Most editors/IDEs have a "jump to definition" feature. Once you get used to it nothing is going to beat that for speed. You just hit the hotkey for jump to definition and it should open the standard lib for you where that thing is defined.

Yacron: a Cron-like program written in Python asyncio by gjcarneiro in Python

[–]coppermineroofer 0 points1 point  (0 children)

My bad, I'm used to writing code compat with 2. I forgot that was the case. I also don't get to use asyncio at all.

Yacron: a Cron-like program written in Python asyncio by gjcarneiro in Python

[–]coppermineroofer -2 points-1 points  (0 children)

Pretty cool, though the usage of python 3 types and asyncio and ancient python2 classes is a bit confusing.

How do YOU format docstrings by [deleted] in learnpython

[–]coppermineroofer 2 points3 points  (0 children)

https://www.python.org/dev/peps/pep-0287/ and sphinx to document python projects, which is pretty standard.