Python Forum

Full Version: how to implement the .mymethod() notation of Python
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
I only learned that I could use my own Python modules in Python a few days ago.

I would like to know how this kind of thing works:

A simple example:

import os
photos = os.listdir(pathToPhotos) 
Now photos in a list of all the photos in my path. I can do things with it.

I don't know, but I think 'listdir()' is a sub-function in the module 'os'.

Can I do this kind of thing with my own modules? Can I put lots of sub-functions in a module, then call them when I need them like this:

import my_makeHTML
myhtml = makeHTML.checkboxes()
How is this implemented? I am grateful for any tips, links or info.
Read about modules and packages

There is also 3-part tutorial in the Tutorials section of the forum. Here is link to part1
A full example of making a package.
From Python 3.3+ supports Implicit Namespace Packages that allows to create a package without an __init__.py file.
This however only applies to empty __init__.py files.

So i always have one __init__.py top level,where bind all together and lift sub-modules.
This can be very import to make it easier for user of your package.

Folder structure:
my_makehtml\
|-- __init__.py
  checkboxes\
  |-- example.py
  |-- singel_line
    doc\    
    |-- web_doc.py
Files:
__init__.py
from .checkboxes.example import checkbox_example
from .checkboxes.singe_line import singel_line_checkbox
from .checkboxes.doc.web_doc import website_checkboxexample
example.py
def checkbox_example():
    return '''
     <!DOCTYPE html>
    <html>
    <body>
      <h1>Show checkboxes:</h1>
        <form action="/action_page">
        <input type="checkbox" name="vehicle1" value="Bike"> I have a bike<br>
        <input type="checkbox" name="vehicle2" value="Car"> I have a car<br>
        <input type="checkbox" name="vehicle3" value="Boat" checked> I have a boat<br><br>
        <input type="submit" value="Submit">
      </form>
      </body>
    </html>'''
sing_line.py
def singel_line_checkbox(name, value):
    return f'<input type="checkbox" name={name} value={value}> <br>'
web_doc.py
import webbrowser

def website_checkboxexample():
    webbrowser.open_new_tab('https://www.w3schools.com/tags/att_input_checked.asp')

if __name__ == '__main__':
    to_website()

Package usage example:
λ ptpython
>>> import my_makehtml

>>> my_makehtml.singel_line_checkbox('car', 9)
'<input type="checkbox" name=car value=9> <br>'

>>> print(my_makehtml.checkbox_example())

     <!DOCTYPE html>
    <html>
    <body>
      <h1>Show checkboxes:</h1>
        <form action="/action_page">
        <input type="checkbox" name="vehicle1" value="Bike"> I have a bike<br>
        <input type="checkbox" name="vehicle2" value="Car"> I have a car<br>
        <input type="checkbox" name="vehicle3" value="Boat" checked> I have a boat<br><br>
        <input type="submit" value="Submit">
      </form>
      </body>
    </html>

# This open a webpage in browser with checkbox example
>>> my_makehtml.website_checkboxexample()

>>> exit()
Using it with from in import:
λ ptpython
>>> from my_makehtml import singel_line_checkbox

>>> singel_line_checkbox('python', 'forum')
'<input type="checkbox" name=python value=forum> <br>'
More here Packaging/Modules--Wheel--pip--setup.py--Freeze
It's a topic with many layer,so it can be a little confusing Confused
So the road can be your_code --> make_package --> setup-py --> distribution on PyPi
As a example if want to share code with others.
Wow! Thanks both of you! I don't think I will ever be good enough to distribute code!

If I can make things work for me, I'm happy. I see I've got a lot of reading to do!
Pedroski55 Wrote:I don't think I will ever be good enough to distribute code!
A simple .py file online is a way to start distributing python code. Many good modules have been distributed this way at the beginning of python.