Python Forum
"pythonic" way of splitting up my code?
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
"pythonic" way of splitting up my code?
#1
Hello everybody!
Im learning python by myself, so there are unknown "gaps" in my skill set..

Instead of trying to explain my overly complex and confusing project (that i use to teach myself python), i will use a hypothetical project.

I would like to use modular code, each "function/feature" in its own separate python-file(.py).

I have more than one feature that requires network (wlan).

I kind of prefer to have a separate python file for my network management, connecting/disconnecting the wifi from defined functions instead of having all of that code in my "main program.py".

so lets imagine we have "main.py", "wifi.py", "clock.py" and "foo.py".
The main.py program includes the clock and foo features and executes them using function calls.
We have included wifi.py in the clock.py program to implement network time synk.
Now, the foo.py also needs network stuff and would benefit from including the wifi.py projekt.

But im somewhat baffled and confused, how should i go about this, should i include wifi.py from two project.py-files, or should i write a new implementation in the new foo.py project?
Or could i pass the "wifi" object to the function as parameter, implementing wifi only once but in the main program instead?

Whats the "correct" / "pythonic" way of solving such programmatical "issues"?

Thank you for your time,
/liggisten
Reply
#2
(Dec-30-2023, 03:27 PM)liggisten Wrote: The main.py program includes the clock and foo features and executes them using function calls.
What do you mean by "include"? In Python you don't include other files, you "import" libraries, so if main.py needs clock and foo
# file main.py
import clock
from foo import spam
clock needs wifi, so
# file clock.py
import wifi
foo needs wifi too so
# file foo.py
from wifi import connect_me
Where is the problem?
« We can solve any problem by introducing an extra level of indirection »
Reply
#3
Quote:But im somewhat baffled and confused, how should i go about this, should i include wifi.py from two project.py-files, or should i write a new implementation in the new foo.py project?
Or could i pass the "wifi" object to the function as parameter, implementing wifi only once but in the main program instead?
Let me paraphrase:
Quote:I am wondering if I should make a global module object and reference the object by importing a module, or should I create an object in main and pass the object as a function argument.
Did I get it right?

Is the wifi object a singleton (only one and is immutable)? If the object is a singleton I think it is fine to make it a module attribute and reference the object through the module. This is done quite often in Python modules that contain objects that always have the same value (string.ascii_letters for example). If you can have more than one wifi object, or if you can change the wifi object, you should make it a module object.
Reply
#4
(Dec-30-2023, 04:30 PM)Gribouillis Wrote: What do you mean by "include"?
...
Where is the problem?

Im sorry, of course you are right, i meant "import", not "include".. (old arduino habit i guess..)
And yes, your examples shows pretty much exactly how i pictured it should work.

If its not painfully obvious, the problem here is me, or rather my ignorance. :)

I did modify my overly complex project like in your example, but it ended up with some sort of hangup, no crash, but no progress.
The last printed debug-string suggested that this happens very shortly after the second import of my wifi library and since i couldnt find anything else wrong, i figured i must had misunderstood or "guessed wrong", ran out of memory or whatnot..

But i figure that issue must be elsewhere in my code.
Haha.. No surprise there, given its current quite flimsy state.. ;)


(Dec-30-2023, 04:56 PM)deanhystad Wrote: Did I get it right?

Thank you for translating me, i think you nailed it. :)

I am unsure if its a singleton or not, but from your description i would hesitantly guess Yes.
I create the wifi object inside wifi.py like so:
#file wifi.py
import network
wlan=network.WLAN(network.STA_IF)
I defined functions to use the wlan object;
#file wifi.py
...
import wifi.credentials # separate file for wifi password & ssid
def connect(ssid=credentials['ssid'],psk=credentials['passwd']):
     wlan.connect(ssid,psk)
(Dec-30-2023, 04:56 PM)deanhystad Wrote: ...or if you can change the wifi object, you should make it a module object.

I guess you could say i "change the wifi object" by changing its state, from off to connected and so on?
But my gut says you mean changing like pointing to a different network interface and so on, create new variables and not merely change some values or states?

Im guessing that a "module object" would be a object defined in a module, like the previously mentioned wifi.py "program" and shared by
import wifi
line where needed?


And just to make things clear as day for me;
A
import modname
doesnt make a new "copy", but references the same "item" when imported again and again (in terms of ram and so on)?

Thank you folks for your input, and a big thank you for your patience with my n00bishness!
Reply
#5
(Dec-30-2023, 08:10 PM)liggisten Wrote: And just to make things clear as day for me;
A
1 import modname doesnt make a new "copy", but references the same "item" when imported again and again (in terms of ram and so on)?
No second copy is made. The module object is stored in a global dictionary named sys.modules. If another file imports modname, it gets immediately a reference to the module object sys.modules[modname].

Read about modules in the Python tutorial
« We can solve any problem by introducing an extra level of indirection »
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Splitting code into several files TLammert 4 1,526 Jun-26-2022, 02:33 PM
Last Post: TLammert
  which is "better" (or more Pythonic)? Skaperen 2 2,084 Feb-01-2020, 03:10 PM
Last Post: Skaperen
  which is "better" (or more Pythonic)? Skaperen 7 3,267 Feb-01-2020, 03:51 AM
Last Post: Skaperen
  which is "better" (or more Pythonic)? Skaperen 8 3,406 Nov-16-2019, 06:46 PM
Last Post: Skaperen
  which is more Pythonic? Skaperen 5 2,902 Jul-16-2019, 01:00 AM
Last Post: Skaperen
  Make my code more pythonic aug828 5 3,429 Sep-02-2018, 06:39 PM
Last Post: Gribouillis
Question Code golfing: splitting a list Ofnuts 7 6,227 Jan-12-2017, 11:01 PM
Last Post: wavic

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020