2

My program requires the usage of a hash table, and an sql database; currently I would like to put the hash table part of my program (add,get,delete etc) and database part of my program(same thing) into separate python files so that I can find and edit specific parts of my code easier, my hashing part relying on my database part as they work together. However my pycharm suggests that (when I import both python files separately) that my second database program will not be used. For example

mainProgram

import hashProgam
import databaseProgram
main()
  do blah blah blah blah
  call function foo_bar()
run main()

hashProgram

foo_bar()
  do blah blah
  call function boo_lah()

databaseProgram

boo_lah()
  do blah blah

the problem occurs (I assume) when my hashProgram wants to call my database program, though I dont know why that would be, because I have imported them both?

Sorry if this doesnt make sense, but I dont really know how else to word it, thank you for any help :-)

James Green
  • 125
  • 12
  • Your pseudocode style is a bit agressive (you can write in python conventionally), but one problem can be is circular reference (hard to judge). Can you post sctual error message. Is this a warning or really an error when you run the ptogram? – Evgeny Aug 31 '18 at 01:32
  • A warning, I never tested it because it was late at night, and just wanted to get on with programming. I also thought that maybe someone would be able to provide a tip on how to split up my program into smaller more manageable python files – James Green Aug 31 '18 at 13:47
  • It seems that any variables that are defined inside my main program are "not defined" in my imported programs...? For example, im using 3 variables for hashing : hashString = hashId % hashKey hashId being defined in the separate program, and hashKey being defined in the main program. However when the main program is executed, (importing all of my side programs) hashKey is not "defined" – James Green Aug 31 '18 at 13:56

1 Answers1

0

... tip on how to split up my program into smaller more manageable python files

This is a fairly basic technique, just go over a simple tutorial for that like one in official documentation.

... when the main program is executed, (importing all of my side programs) hashKey is not "defined"

Something like this setup should work:

foo.py

hash_key = 1

main.py

import foo
assert foo.hash_key == 1 
print(foo.hash_key) 

... my hashing part relying on my database part as they work together.

On a rare occasion when your modules import each other you can end up with a circular reference and need a smarter split of modules. I'm not sure it is your case, perhaps you just not using proper import syntax yet.

Evgeny
  • 4,173
  • 2
  • 19
  • 39
  • Technically, yes, you will be able to use everything from a module after `from import * `, but this is considered a bad pattern. The reason you put somehting in a module is that there is something there that need to be importable, and something that does not. in a larger project you almost certainly do `import foo` and `foo.x` so that you keep track of where `x` came from. `from foo import *` is least desirable. – Evgeny Aug 31 '18 at 23:26
  • okay, well say I just want to split up my program into more digestible pieces, what would I have to do? – James Green Sep 03 '18 at 10:50
  • a) make sure you can import things from a module in python, 2) think of a program structure that is modular (few entry points, testable interface, good separation, etc), 3) implement. it comes with practice, but a good start may also be a well-written program in one file, then you may want to split it to modules. – Evgeny Sep 03 '18 at 13:32