0
  1. If I have function like this

    def get_time_format(value, time):
        for item in value:
            time.append(datetime.strptime(str(item),'%H').strftime('%I%P').lstrip('0').upper())
        return time
    

    For example, above function I am using across different modules, Instead of code repeation, can I put that in separate file and call them in the module whichever requires them.

  2. Second part is related to the first one, so if I have few lines of code which is common. Can I create a file which is having all the common code lines?

    hierarchy:-
    hello.py
    hello.py/ds/a.py
    hello.py/ds/b.py
    hello.py/ds/c.py
    hello.py/ds/d.py
    

    Above hello.py is my main file and a,b,c and d are modules.

Can some tell me where I can to create a file to share common codes within modules as asked in 1 and 2. I am new to python and using the modules for the first time.

atline
  • 28,355
  • 16
  • 77
  • 113
Terry
  • 19
  • 1
  • 8
  • 3
    yes you can create a different file and put all the code it that file. Create that file in same directory so you can import function from that file easily – vipul prajapati Dec 24 '18 at 08:55
  • 1
    check this question - you may find it helpful on how to organise your project - https://stackoverflow.com/questions/7948494/whats-the-difference-between-a-python-module-and-a-python-package If the number of modules increase, you may want to organise them as a package – buran Dec 24 '18 at 09:42

3 Answers3

0

save the function as a example.py file in any directory. Example home/ubuntu/Desktop Now use the following snippet in any code to use the function

import sys
sys.path.insert(0, 'home/ubuntu/Desktop')
import example as eg

eg.get_time_format(value, time)

You can define multiple functions like get_time_format(value,time) foo(x,y) bar(x,y) in example.py like To use them, just type eg.foo(x,y) eg.bar(x,y)

0

I would recommend using a package for this. See buran's comment

Simply create a directory to put your code in and then include an __init__.py file. The __init__.py is a special file that tells python to mark the directory as a python package and therefore make it importable. see What is __init__.py for?.

Note: as __init__.py is a marker file, it is usually just an empty file containing no code.

Then you can do the following:

# structure
my_directory
-/__init__.py
-foo.py

# foo.py 
def bar(boo):
    print(boo)

Subsequently import the function in other modules or an interactive shell as follows:

from foo import bar 
bar('ble')
>>>> ble

This has the added advantage allowing imports when working outside of the directory of the package:

from my_directory.foo import bar 
bar('boo')
>>>>boo

or a full import of foo:

from my_directory import foo
foo.bar('boo')
>>>>boo
mdmjsh
  • 915
  • 9
  • 20
0

create an init.py in the subfolder ds, and then import all the functions from a, b, c, d in this init file. then it is possible to import ds in hello.py and use all the functions defined in a~d.

张振羽
  • 24
  • 4