Notifications
Clear all

adding code in a post loses the indents needed for python

7 Posts
3 Users
0 Likes
699 Views
jfabernathy
(@jfabernathy)
Estimable Member
Joined: 2 years ago
Posts: 148
Topic starter  

When I post python code into a post as suggested in one of the how to links https://forum.dronebotworkshop.com/add-code/

I see that my code is missing all the indents that are needed to not only make it readable, but work correctly.  Python uses indents instead of punctuation to show when an if statement ends, etc.

I use editors that convert my tab to 4 spaces which is pretty much standard these days.  but the cut and paste into this forum's posts seem to strip those spaces away.

Any ideas??

 

If your code won't compile, have another glass of bourbon. Eventual the problem will be solved.


   
Quote
robotBuilder
(@robotbuilder)
Noble Member
Joined: 3 years ago
Posts: 1554
 

@jfabernathy

Does your editor have a copy as HTML option like the Arduino IDE?  That seems to work with the Arduino source code in retaining indents and colour coding of text.  I copied some Python source code to the Arduino IDE which sort of worked except the TAB character was replaced with two space characters as seen below.

 

"""
Prints the scan code of all currently pressed keys.
Updates on every keyboard event.
"""
import sys
sys.path.append('..')
import keyboard

def print_pressed_keys(e):
  line = ', '.join(str(code) for code in keyboard._pressed_events)
  # '\r' and end='' overwrites the previous line.
  # ' '*40 prints 40 spaces at the end to ensure the previous line is cleared.
  print('\r' + line + ' '*40, end='')
  
keyboard.hook(print_pressed_keys)
keyboard.wait()

 

 

 

 

 


   
ReplyQuote
jfabernathy
(@jfabernathy)
Estimable Member
Joined: 2 years ago
Posts: 148
Topic starter  

@robotbuilder I have not found an editor that supports the copy HTML. Also my source code contains no tabs because the tab is replaced with 4 spaces when you type it.

 

If your code won't compile, have another glass of bourbon. Eventual the problem will be solved.


   
ReplyQuote
jfabernathy
(@jfabernathy)
Estimable Member
Joined: 2 years ago
Posts: 148
Topic starter  

@robotbuilder I can paste my code into Arduino IDE and then copy it as HTML, but when I paste it into the forum, It's html. How do you get it to look like code again?

If your code won't compile, have another glass of bourbon. Eventual the problem will be solved.


   
ReplyQuote
jfabernathy
(@jfabernathy)
Estimable Member
Joined: 2 years ago
Posts: 148
Topic starter  

It seems that this experiment worked. I started with a blank reply and then clicked on the {;} button and pasted the HTML into the window
Then I closed that window and selected the text and used <>

months = ["January", "February", "March", "April", "May", "June", "July",
          "August", "September", "October", "November", "December"]

USE_24HR_TIME = False

def time_transform(val):
#   val is in format '2021-03-25T20:07:39.402Z'

    dstStart = {2021: 14, 2022: 13, 2023: 12, 2024: 10, 2025: 9, 2026: 8, 2027: 14, 2028: 12, 2029: 11, 2030: 10, 2031: 9}

    dstStop = {2021: 7, 2022: 6, 2023: 5, 2024: 3, 2025: 2, 2026: 1, 2027: 7, 2028: 5, 2029: 4, 2030: 3, 2031: 2}

    if val == None:
        val = "When: Unavailable"
    year = int(val[0:4])
    month = int(val[5:7])
    day = int(val[8:10])
    hour = int(val[11:13]) # hour is in UTC
    if month in range(3, 12):
        if month == 3 and day >= int(dstStart[year])+1 or month == 3 and day == int(dstStart[year]) and hour >= 7:
            timezoneOffset = 4
            
        elif month == 11 and day <= int(dstStop[year]):
            if day <= int(dstStop[year])-1 or day == int(dstStop[year]) and hour <= 6:
                timezoneOffset = 4
            else:
                timezoneOffset = 5

        elif month in range(4,11):
            timezoneOffset = 4
        else:
            timezoneOffset = 5
        
    else:
        timezoneOffset = 5
        
    hour = hour - timezoneOffset
    if hour < 0:
        hour = hour + 24
        day = day -1
    min = int(val[14:16])

    if USE_24HR_TIME:
        timestring = "%d:%02d" % (hour, min)
    elif hour > 12:
        timestring = "%d:%02d pm" % (hour-12, min)
    else:
        timestring = "%d:%02d am" % (hour, min)

    return "%s %d, %d, at %s" % (months[month-1], day, year, timestring)
testval = '2021-11-07T08:00:39.402Z'
print(testval)
print(time_transform(testval))


If your code won't compile, have another glass of bourbon. Eventual the problem will be solved.


   
ReplyQuote
b
 b
(@b)
Prominent Member
Joined: 3 years ago
Posts: 912
 
Posted by: @jfabernathy

I started with a blank reply and then clicked on the {;} button and pasted the HTML into the window
Then I closed that window and selected the text and used <>

Oh well done, when I post code sometimes I succeed but other times I manage to screw it up.  Now I just have to copy these words of wisdom somewhere where I remember to read them before a code post!   I'll probably forget where I saved it admittedly. ☹️ 


   
ReplyQuote
robotBuilder
(@robotbuilder)
Noble Member
Joined: 3 years ago
Posts: 1554
 

My main screw up used to be not clicking between paragraph symbols before insertion resulting in text and programs being combined.  Now I do a number of crlf,  [ENTER] presses, between items to make sure I can fit things before or after other stuff in the post.

<p> </p>
<p> </p>
<p> </p>
<p> </p>
<p> </p>

 


   
ReplyQuote