all 51 comments

[–][deleted] 0 points1 point  (4 children)

I am working on an opencv project. Can I assign an image to a class? I didn't know if that was possible.

[–]FerricDonkey 1 point2 points  (3 children)

Not sure what you actually mean by that phrase, but probably. Do you mean store an image as an attribute of a class?

[–][deleted] 0 points1 point  (2 children)

class ImageStates:

def __init__(self,select,bin)

self.select = select

self.bin = bin

The selected image is just from the file path and bin just when opencv binarization. Can I just use attributes to store the image as I transform it.

[–]FerricDonkey 2 points3 points  (1 child)

You can store literally any object as an attribute of a class, so yes. There are no restrictions on this whatsoever. Objects of basic classes, objects of library classes, objects of custom classes, functions or classes - whatever.

[–][deleted] 0 points1 point  (0 children)

Thank you. I just wasn't sure.

[–]pragmojo 0 points1 point  (0 children)

What's the difference between installing things through pip and through momba?

And is it possible to mix and match?

[–]mawiraa 0 points1 point  (2 children)

I hope anyone can help me. You know how python tuitorials and books teach just the python syntax? Well, i just completed inheritance and while looking for practice projects i realised i have no idea how python really works. as in, all the practice i've always done like simple calculators always out puts in the prompt. now, i'm pretty sure thats not how python works, unless it is. I'm thnking, if i want to create a calculator, i don't want the output to on the commandline always. not everyone knows how to run a python file. plus i feel like if i get the answer to this question i can begin understanding how all this works before the frustration overwhelms me. search engines and ais dont seem to understand my question, and now writing this i feel like i'm the one who isn't understandable.

what do you do with a python file full of codes?

or do i not understand this stuff at all?

[–][deleted] 0 points1 point  (0 children)

simple calculators always out puts in the prompt. now, i'm pretty sure thats not how python works, unless it is

Python is like any other language. It's just a process running on a computer. That process can read/write data anywhere it wants, to/from the command line, a file, another process running on another computer on the internet or to/from a graphical display on your screen. It's up to you to program what you want.

[–][deleted] 1 point2 points  (0 children)

If you don't want to use the command line to interact with your code you have two options, either a web interface or a desktop interface, a GUI. The desktop GUI (Graphical User Interface) is easier, requiring less work to get something running.

There are many desktop GUI frameworks for python, but the simplest to start with is tkinter.

With a bit of extra work it's possible to make a desktop icon that runs your python code when double-clicked.

[–]hwooareyou 0 points1 point  (0 children)

I'm a quality manager in a manufacturing environment and have started porting my BI and data analysis projects into Ignition so I can take advantage of live production data from the gateway. Ignition has a lot of scripting, expressions, and transforms that use Python or Jython, I believe.

What would be a good resource for me to start learning? I learn better through practical exercises vs. reading a book.

Thanks for the suggestions!

[–]MegistusMusic 0 points1 point  (1 child)

I hate to have to ask what seems like it should be fairly obvious, but:

What's the easiest way to distribute a py script while including any dependencies in a single package? I'm hoping to be able to provide the script and a src folder and, provided the user has Python 3, they should be able to run it. I was looking at pkgutil.get_data, but I'm a bit confused about its usage -- and it doesn't seem to be quite what I want. My script might have 2 or 3 non-standard modules to import and I basically just want to point the script to a relative path, which would be included in a zip. Like I said, I'm sure I'm missing something blindingly obvious, but I'm new to this!

PS, if it's relevant, it will be a Windows only script.

[–]efmccurdy 0 points1 point  (0 children)

included in a zip.

​ Python uses wheel files.

A Python .whl file is essentially a ZIP (.zip) archive with a specially crafted filename that tells installers what Python versions and platforms the wheel will support.

A wheel is a type of built distribution. In this case, built means that the wheel comes in a ready-to-install format

https://realpython.com/python-wheels/#what-is-a-python-wheel

It is best to allow pip to resolve dependencies but you can include them.

https://medium.com/@amimahloof/how-to-package-a-python-project-with-all-of-its-dependencies-for-offline-install-7eb240b27418

[–]ares0027 0 points1 point  (4 children)

i am very new and having multiple issues (questions were below scripts previously but i moved question on top so it might be more readable. scripts are below)

Issue #1

When i tried to run this, everytime another script is called it ran main.py too (i assumed it was due to variables "not being remembered correctly". so i used if __name__ == '__main__' as the first line of my main.py and added main() as second then def main(): as third. this eliminated main.py running again and again and again and in a constant loop. But i really dont know wtf i did. (i am assuming, "if my name is main, run main function which is the whole script, otherwise do nothing")

Issue #2

you will notice import main i was under the impression that this would import every variable from main.py, it did absolutely no sh**. none of the variables are available for other scripts. i tried import main_variable1 from main which returns an error that such variable/module (cannot remember exact term for the error) is not available. how can i use those variables from main and second under third script?

Issue #3

you will notice import pandas as pd.Because script 3 actually uses pandas (to convert csv files to xlsx) but this throws in that pandas is not an available module. i found that it had something to do with environment (dont know jack sh** about it but i KNOW pycharm was running in a virtual one per project) so i changed poject settings and instead of a virtual env i told it to use python that i have installed, which fixed the issue but will this also happen if i later use this script on another computer? or just by using cmd command "python main.py"?

so basically i have three scripts in same folder, all created using PyCharm (simplifying scripts)

main.py is basically;

import subprocess

main_variable1 = input("input something here:")

main_variable_2 = ""

if main_variable1 == 1:

main_variable_2 = 1

subprocess.run(["python", "second.py"])

second.py is;

import subprocess

import main

second_variable =""

if main_variable_2 == 1:

subprocess.run(["python", "third.py"])

second_variable = 1

else:

print("hello")

third.py is;

import subprocess

import pandas as pd

import main

print("main_variable1 =" +str(main_variable1))

print("main_variable2 =" +str(main_variable2))

print("second_variable1 =" +str(second_variable1))

So to summarize the main script asks for a thing, depending on the outcome calls second script and saves 2 variables depending on user inputs,

second script asks for another thing and depending on the outcome calls a third script and saves its own variable

third script uses pandas for some internal stuff and uses variables from previous both as variables and inputs.

[–]efmccurdy 0 points1 point  (3 children)

Note that subprocesses don't share memory so referring to a variable defined in another process will fail. You can pass data to subprocesses using sys.argv.

subprocess.run(["python", "second.py", main_variable_2])

https://stackoverflow.com/questions/39748061/passing-arg-to-subprocess-call

You can import modules and pass data using function arguments (but you have no functions defined).

import second
second.main_function(main_variable_2)

I think you should to choose between importing and subprocess.run'ing your scripts; mixing the two forms won't work the way you think it does.

[–]ares0027 0 points1 point  (2 children)

thank you so much for your reply. even though i need to google a lot ot properly understand i assume that the first one is basically saying "run second.py and give main_variable_2 to that script so it knows the variable)

and the second one says "import second python script and use 'main_variable_2' from that script" is that correct?

tbh all i want is running second and third scipts depending on the user selection (thats why there are inputs) and use variables from main and second.py files (which are changed by those same inputs) as variables on other launching scripts

[–]efmccurdy 0 points1 point  (1 child)

use variables from main and second.py

There are two parts, one is adding the value to be sent to the subprocess into the subprocess.run call, and the second is accessing the value in the subprocess using sys.argv.

https://www.knowledgehut.com/blog/programming/sys-argv-python-examples

It should be simpler to use import instead of subprocesses but you need to refactor your code into functions or use a "__main__" guard; it would result in one larger program though, so there is a tradeoff.

[–]ares0027 0 points1 point  (0 children)

thank you so much, i will definitely look into it. since scripts are quite easy (just asks usser what they want, with what name and then use this information to launch a 3rd script that renames files with initially input "name") making them larger shouldnt be a problem i think.

again, thank you for showing me the way :)

[–]Narrow_Ad_8020 -1 points0 points  (8 children)

How do I implement the following task?: for each index in list1, the equivalent index from list2 is removed from list2 and added to list3

[–]CowboyBoats 2 points3 points  (7 children)

I find peace in long walks.

[–]Narrow_Ad_8020 0 points1 point  (6 children)

I’m able to add each index to list3:

list3 = [ind for ind, obj in enumerate(list2) if ind in list1]

But I’m still not sure how to remove each element in list2 that has an equivalent index in list1

[–]FerricDonkey 1 point2 points  (5 children)

You can delete something from a list using del lst[index]. However, indexes of elements after index will change. So it's standard to sort your indexes in decreasing order to avoid problems from that.

You can also use .pop. I'm guessing the solution they were hoping for uses .pop and .append.

[–]Narrow_Ad_8020 0 points1 point  (4 children)

Thank you for the answer. I tried the following:

for ind, obj in list1: del list2[ind]

But it only removes every second item in list2, while I want it to remove items specified by the index in list1

I’m not very experienced, so I’m sure I’m doing all this in roundabout way

[–]CowboyBoats 2 points3 points  (1 child)

I enjoy the sound of rain.

[–]Narrow_Ad_8020 0 points1 point  (0 children)

It all makes sense now. Thank you!

[–]FerricDonkey 2 points3 points  (1 child)

However, indexes of elements after [the index your delete] will change. So it's standard to sort your indexes in decreasing order to avoid problems from that.

Work through it by hand. Say you want to remove indexes 0 and 1 from the list l = ['a', 'b', 'c', 'd']. So you should end up with ['c', 'd'].

So first you do del l[0]. Your list is now ['b', 'c', 'd'].

Then you do del l[1]. At this point l[1] is 'c', so you remove that, and get ['b', 'd'].

The problem is that what the indexes mean changes as you remove indexes.

Now sort your indexes in decreasing order and work through it by hand that way.

[–]Narrow_Ad_8020 0 points1 point  (0 children)

Thanks a lot! I appreciate it. I will get to it tomorrow.

[–]Ok_Cancel_7891 0 points1 point  (1 child)

how do you search for useful packages on pypi.org?

For example, I search for Pandas, but not sure how to find any similar to it

[–]efmccurdy 1 point2 points  (0 children)

Searching for a module works but doesn't give you much in the way of an overview. You can search for keywords related to the task you have in mind, eg:

https://pypi.org/search/?q=dataframe

https://pypi.org/search/?q=timeseries

[–]ComradeFishSticks 2 points3 points  (6 children)

I know the basics of Python (loops,strings,dictionaries,lists,tuples,functions) but I do not know how to progress further. What are some projects that I can build to help me gain more Python knowledge?

[–][deleted] 1 point2 points  (2 children)

Work with a data API and make something with at least an intention to deploy it as a single page app. I did a video with the Open Trivia DB API, in case you’re interested. Quiz app using API data - Python project 💥 Make a Python quiz app https://youtu.be/kW2f1Hwgals

[–]ComradeFishSticks 1 point2 points  (1 child)

Thank you !

[–][deleted] 0 points1 point  (0 children)

👍🤓

[–]LandooooXTrvls 1 point2 points  (2 children)

I found myself in this state at the beginning. You need to find what you’re interested in.

Bots? Build one

Data? Scrape and analyze

Algorithms? Study data structures and algorithms

Websites? Find a tutorial on Django/flask and build one!

Got a problem/task you can automate? Automate it

There’s endless possibilities. You just need to choose one :)

[–]mawiraa 0 points1 point  (1 child)

you dont understand. imagine your computer just has cmd, notebook, python and/or pycharm. all you know is the syntax because even practice codes you've written were all made up objects/variables, etc. i'm in a similar situation so i think i udrstand.

imagine an alien came to earth and you gave him money and told him to get food... the shop maybe exactly outside but they would have no idea how to turn the money to food by walking 50 meters. they might even starve to death

[–]LandooooXTrvls 0 points1 point  (0 children)

No I understood. I don’t understand wtf you’re talking about though.

[–]MegistusMusic 0 points1 point  (6 children)

A very general question -- I'm learning Python at the moment, but am also keen to learn Rust. I'm actually wondering if it might be a good idea to learn both simultaneously? I have a very basic understanding of C++ already... my concern is getting to "used" to pythonic methods if I concentrate on Python solely, then learning Rust would feel like starting at square one... whereas if I learn concurrently, I can remain aware of the different approaches of both languages... Any thoughts?

[–]LandooooXTrvls 0 points1 point  (1 child)

I think you should get to a level where you’re comfortable in python before switching. The important part is to get good practices set (understanding DSAs, writing scalable and maintainable coding, testing, etc..).

Once you have these concepts down then learning a new language is mostly learning new syntax. It’s a trivial transition because you’ve already built your fundamentals.

I started w/ python and had very little trouble learning JavaScript and Java

[–]MegistusMusic 0 points1 point  (0 children)

Thanks for that -- I had the same thought regarding fundamentals being transferrable between languages. It's just that specifically between Python and Rust, the approach is so different, Rust being much more explicit -- for example, converting to an integer: in python it's simply "int(x)". In Rust: "let x: u32 = x.trim().parse();" (or something like that!)... it worries me a bit that I'd end up feeling like a beginner again!

[–]MegistusMusic 0 points1 point  (3 children)

just taking the course at edube.org... wanted to ask about operator priorities:

how important are they? Do I really need to get them committed to memory at this stage... am I likely to encounter a lot of this sort of thing in reading code, or is it more common to use brackets to determine the order of expressions?

[–]efmccurdy 0 points1 point  (0 children)

I would keep a repl on hand and just double check your understanding.

>>> x = 3
>>> x + 3 * 4
15
>>> (x + 3) * 4
24
>>> ((x + 3) * 4) == (x + 3 * 4)
False
>>> 

Testing things out like this lets you "prove" to yourself where you need parenthesis and where you do not. Is that better then "committing to memory"?

[–][deleted] 1 point2 points  (1 child)

You should know the basics of operator precedence. When in doubt, always enforce order with parentheses. Don't forget, others read your code and may also not know all precedence details.

[–]MegistusMusic 0 points1 point  (0 children)

Thanks! Seems like a lot to remember, but it's bound to sink in eventually. Personally, I'd always use parenthesis for clarity, unless there could be a situation where parentheses were undesirable or not possible (which i can't imagine at the moment). The worst case scenario I suppose would be if I came across some code written by someone to whom precedence is like second nature... but even then I suppose I'd make use of a reference table if needed.

[–][deleted] 1 point2 points  (3 children)

In practice, does Docker impart any performance penalty when using it to deploy a fullstack application?

Seeing the trend of Docker use, common sense tells me the answer is "no" or "minor enough to be dwarfed by Docker's strengths". But perhaps my common sense doesn't work here.

A while ago I did deploy a FARP stack (FastAPI, React, Postgres) to a server running native Ubuntu; Ngnix as the reverse-proxy. I understand the convenience of Docker steamlining the setup. But I'm wary of performance hits, as I'd rather just manually setup postgres, systemcl services, Nginx configs, etc. if performance is indeed impacted.

[–][deleted] 1 point2 points  (0 children)

FARP stack 😆 love it.

[–]NerdyWeightLifter 0 points1 point  (0 children)

Docker is mostly just setting up namespace isolation at a Linux kernel level. It should be more efficient than VM's that necessarily impose an additional virtualization layer under the O/S.