Python Forum
how to implement the .mymethod() notation of Python
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
how to implement the .mymethod() notation of Python
#1
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.
Reply
#2
Read about modules and packages

There is also 3-part tutorial in the Tutorials section of the forum. Here is link to part1
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply
#3
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.
Reply
#4
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!
Reply
#5
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.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  How do I properly implement restarting a multithreaded python application? MrFentazis 1 572 Jul-17-2023, 09:10 PM
Last Post: JamesSmith
  ''.join and start:stop:step notation for lists ringgeest11 2 2,354 Jun-24-2023, 06:09 AM
Last Post: ferdnyc
  issue with converting a scientific notation to standard notation thomaswfirth 4 1,266 Jun-06-2023, 06:06 PM
Last Post: rajeshgk
  notation MCL169 8 1,379 Apr-14-2023, 12:06 PM
Last Post: MCL169
  Issue in writing sql data into csv for decimal value to scientific notation mg24 8 2,818 Dec-06-2022, 11:09 AM
Last Post: mg24
  Right way to implement interfaces yossiy123 1 1,218 May-12-2022, 10:31 AM
Last Post: Gribouillis
  Graphics Formatting - X-axis Notation and Annotations - Matplotlib silviover_junior 0 1,750 Mar-17-2021, 01:19 PM
Last Post: silviover_junior
Question best way to implement algorithm hamidze 3 2,159 Feb-27-2021, 07:10 PM
Last Post: hamidze
  How to understand the byte notation in python3 blackknite 3 2,815 Feb-23-2021, 04:45 PM
Last Post: bowlofred
  Simple question concerning python dot notation. miner_tom 1 1,858 Mar-24-2020, 05:20 PM
Last Post: buran

Forum Jump:

User Panel Messages

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