Python Programming
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me
Go Back   Dev Shed ForumsProgramming LanguagesPython Programming

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
  #1  
Old October 15th, 2004, 07:44 AM
Grim Archon's Avatar
Grim Archon Grim Archon is offline
Mini me.
Dev Shed Novice (500 - 999 posts)
 
Join Date: Nov 2003
Location: Cambridge, UK
Posts: 783 Grim Archon User rank is Corporal (100 - 500 Reputation Level)Grim Archon User rank is Corporal (100 - 500 Reputation Level)Grim Archon User rank is Corporal (100 - 500 Reputation Level)Grim Archon User rank is Corporal (100 - 500 Reputation Level)  Folding Points: 1488 Folding Title: Novice Folder
Time spent in forums: 3 Days 2 h 15 m 57 sec
Reputation Power: 8
Send a message via MSN to Grim Archon
Markup CGI - please test

In quiet moments I've been working on a python CGI script to do code markup. If you are interested enough to test it, here it is:
Markup CGI
Key features are:
1. User control over format and colour
2. Line numbering.
3. Cookies to remember settings.

Here's a sample:
Code:
[001] #Replace this code with your own
[002] 
[003] def Hello(name="World"):
[004]     """A simple function"""
[005]     print "Hello "+name #Classic
[006]     a =1+1
[007]     b=(6+7)-2
[008]     if a < b :
[009]         print "a=%s, b=%s"%(a,b)
[010] 
[011] Hello("you")
[012] Hello()
[013] Hello("The Universe")
[014] 
[015] 


You don't have to like my colours

grim
__________________
*** Experimental Python Markup CGI V2 ***

Last edited by Grim Archon : October 17th, 2004 at 09:22 AM. Reason: numbering changed

Reply With Quote
  #2  
Old October 15th, 2004, 01:29 PM
jimmy2k1 jimmy2k1 is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Mar 2002
Posts: 89 jimmy2k1 User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 8 h 48 m 50 sec
Reputation Power: 7
that's really nice but I did get this error:

Code:
KeyError	Python 2.2.3: /usr/bin/python
Fri Oct 15 10:27:32 2004

A problem occurred in a Python script. Here is the sequence of function calls leading up to the error, in the order they occurred.

 /home/pharsc2/public_html/cgi-bin/syntax.py
  622                 oblock= cgi.escape(makeBlock(data),True)
  623                 #oblock=oblock%items
  624                 print html1%items,
  625                 fname = str(time.time()+random.random())
  626                 tf = file(os.sep+"tmp"+os.sep+fname,"w")
html1 = '\n <head>\n <meta http-equiv="Conten...tle>\n </head>\n <body>\n <hr>\n ', items = {'com': 'Red', 'entity': '1', 'key': 'BLACK', 'markup': 'vbmarkup', 'num': 'BLACK', 'op': 'BLACK', 'reformat': '1', 'str': 'Green'}

KeyError: res
      __doc__ = 'Mapping key not found.'
      __getitem__ = <bound method KeyError.__getitem__ of <exceptions.KeyError instance>>
      __init__ = <bound method KeyError.__init__ of <exceptions.KeyError instance>>
      __module__ = 'exceptions'
      __str__ = <bound method KeyError.__str__ of <exceptions.KeyError instance>>
      args = ('res',)

Reply With Quote
  #3  
Old October 15th, 2004, 01:30 PM
jimmy2k1 jimmy2k1 is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Mar 2002
Posts: 89 jimmy2k1 User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 8 h 48 m 50 sec
Reputation Power: 7
line 624 ^^^

Reply With Quote
  #4  
Old October 15th, 2004, 02:11 PM
Grim Archon's Avatar
Grim Archon Grim Archon is offline
Mini me.
Dev Shed Novice (500 - 999 posts)
 
Join Date: Nov 2003
Location: Cambridge, UK
Posts: 783 Grim Archon User rank is Corporal (100 - 500 Reputation Level)Grim Archon User rank is Corporal (100 - 500 Reputation Level)Grim Archon User rank is Corporal (100 - 500 Reputation Level)Grim Archon User rank is Corporal (100 - 500 Reputation Level)  Folding Points: 1488 Folding Title: Novice Folder
Time spent in forums: 3 Days 2 h 15 m 57 sec
Reputation Power: 8
Send a message via MSN to Grim Archon
Weird, learning something about cookies - should have realised it is stateless and you can't guarantee that if one is received then they are all received.

Hopefully, now fixed with an update to a default dictionary.

grim

Reply With Quote
  #5  
Old October 16th, 2004, 12:59 PM
netytan's Avatar
netytan netytan is offline
Hello World :)
Dev Shed Frequenter (2500 - 2999 posts)
 
Join Date: Mar 2003
Location: Hull, UK
Posts: 2,536 netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level)netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level)netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level)netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level)netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level)netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level)netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level) 
Time spent in forums: 1 Week 2 Days 18 h 11 m 13 sec
Reputation Power: 63
Send a message via ICQ to netytan Send a message via AIM to netytan Send a message via MSN to netytan Send a message via Yahoo to netytan
Tested it with normal Python code and code copied from the shell and it works perfectly . If you havn't posted a link to this in the tips and tricks thread do it already .

Keep it up Paul,

Mark.
__________________
programming language development: www.netytan.com Hula


Reply With Quote
  #6  
Old October 17th, 2004, 09:19 AM
Grim Archon's Avatar
Grim Archon Grim Archon is offline
Mini me.
Dev Shed Novice (500 - 999 posts)
 
Join Date: Nov 2003
Location: Cambridge, UK
Posts: 783 Grim Archon User rank is Corporal (100 - 500 Reputation Level)Grim Archon User rank is Corporal (100 - 500 Reputation Level)Grim Archon User rank is Corporal (100 - 500 Reputation Level)Grim Archon User rank is Corporal (100 - 500 Reputation Level)  Folding Points: 1488 Folding Title: Novice Folder
Time spent in forums: 3 Days 2 h 15 m 57 sec
Reputation Power: 8
Send a message via MSN to Grim Archon
Mark,
Thanks for that

I think I'll allow a little more testing before suggesting it for everyday use tho - I just changed it so line numbers start from a more logical 1.

I haven't used cookies directly before so the bug that jimmy2k1 reported is a bit of a funny to me. I patched it by having defaults that get updated by whatever cookies are returned but I'm not happy with that. Anyone have any ideas?

I might change it so that one cookie has all settings which is probably tidier anyway.

grim

Reply With Quote
  #7  
Old October 17th, 2004, 02:46 PM
Scorpions4ever's Avatar
Scorpions4ever Scorpions4ever is offline
Banned ;)
Click here for more information.
 
Join Date: Nov 2001
Location: Glendale, Los Angeles County, California, USA
Posts: 7,717 Scorpions4ever User rank is General 3rd Grade (Above 100000 Reputation Level)Scorpions4ever User rank is General 3rd Grade (Above 100000 Reputation Level)Scorpions4ever User rank is General 3rd Grade (Above 100000 Reputation Level)Scorpions4ever User rank is General 3rd Grade (Above 100000 Reputation Level)Scorpions4ever User rank is General 3rd Grade (Above 100000 Reputation Level)Scorpions4ever User rank is General 3rd Grade (Above 100000 Reputation Level)Scorpions4ever User rank is General 3rd Grade (Above 100000 Reputation Level)Scorpions4ever User rank is General 3rd Grade (Above 100000 Reputation Level)Scorpions4ever User rank is General 3rd Grade (Above 100000 Reputation Level)Scorpions4ever User rank is General 3rd Grade (Above 100000 Reputation Level)Scorpions4ever User rank is General 3rd Grade (Above 100000 Reputation Level)Scorpions4ever User rank is General 3rd Grade (Above 100000 Reputation Level)Scorpions4ever User rank is General 3rd Grade (Above 100000 Reputation Level)Scorpions4ever User rank is General 3rd Grade (Above 100000 Reputation Level)Scorpions4ever User rank is General 3rd Grade (Above 100000 Reputation Level)Scorpions4ever User rank is General 3rd Grade (Above 100000 Reputation Level) 
Time spent in forums: 1 Month 3 Days 12 h 29 m 35 sec
Reputation Power: 1179
Nice stuff . One error I noticed was, when I enter something like this:
Code:
#!/usr/bin/env python
x = "foo" + "bar" + "baz"


the output is colourized, but the spaces between the + symbols are removed in the output.
Code:
#!/usr/bin/env python
x = "foo"+"bar"+"baz"


Also, your VBcode output has a space before the #!/usr/bin/env python line
__________________
Up the Irons
What Would Jimi Do? Smash amps. Burn guitar. Take the groupies home.
"Death Before Dishonour, my Friends!!" - Bruce D ickinson, Iron Maiden Aug 20, 2005 @ OzzFest
Down with Sharon Osbourne

Puzzle of the Month solved by Keath and KevinADC, superior perl programmers of the month

Reply With Quote
  #8  
Old October 17th, 2004, 03:00 PM
netytan's Avatar
netytan netytan is offline
Hello World :)
Dev Shed Frequenter (2500 - 2999 posts)
 
Join Date: Mar 2003
Location: Hull, UK
Posts: 2,536 netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level)netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level)netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level)netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level)netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level)netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level)netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level) 
Time spent in forums: 1 Week 2 Days 18 h 11 m 13 sec
Reputation Power: 63
Send a message via ICQ to netytan Send a message via AIM to netytan Send a message via MSN to netytan Send a message via Yahoo to netytan
You can fix the first example by selecting "No" in the reformat section . Not sure about the second though.

Reply With Quote
  #9  
Old October 17th, 2004, 03:39 PM
Grim Archon's Avatar
Grim Archon Grim Archon is offline
Mini me.
Dev Shed Novice (500 - 999 posts)
 
Join Date: Nov 2003
Location: Cambridge, UK
Posts: 783 Grim Archon User rank is Corporal (100 - 500 Reputation Level)Grim Archon User rank is Corporal (100 - 500 Reputation Level)Grim Archon User rank is Corporal (100 - 500 Reputation Level)Grim Archon User rank is Corporal (100 - 500 Reputation Level)  Folding Points: 1488 Folding Title: Novice Folder
Time spent in forums: 3 Days 2 h 15 m 57 sec
Reputation Power: 8
Send a message via MSN to Grim Archon
Yep Scorp, you got bit by formatting

Reformat: No - means (hopefully) don't make any change to layout.

Reformat: Style 1 Tries to impose some PEP 8 style guidelines to the code. This means ignoring spaces/tabs and putting them back in where guidelines suggest they should go. The rules are fairly simplistic but try to make the code layout consistent. (Looks like I need to modify the comment handling slightly for when it's at the beginning of a line.) I put it in because my code gets a little sloppy after editing and I wanted something to tidy it up. 9/10 times it does okay.

Reformat Style 2 Is an alternative that some may prefer. (suck it an see)

The mark-up decisions are made by the tokenizer module so it should be compatible with IDLE and other packages that use it.
This might lead to funny colouration when marking up none-python code but seems to work for C/C++ okay as a lot of the reserved words are common.

grim

Reply With Quote
  #10  
Old October 18th, 2004, 11:07 AM
Grim Archon's Avatar
Grim Archon Grim Archon is offline
Mini me.
Dev Shed Novice (500 - 999 posts)
 
Join Date: Nov 2003
Location: Cambridge, UK
Posts: 783 Grim Archon User rank is Corporal (100 - 500 Reputation Level)Grim Archon User rank is Corporal (100 - 500 Reputation Level)Grim Archon User rank is Corporal (100 - 500 Reputation Level)Grim Archon User rank is Corporal (100 - 500 Reputation Level)  Folding Points: 1488 Folding Title: Novice Folder
Time spent in forums: 3 Days 2 h 15 m 57 sec
Reputation Power: 8
Send a message via MSN to Grim Archon
Fixed spurious space insertion when comment was in column 0 and Style 1 is used.

Reply With Quote
Reply

Viewing: Dev Shed ForumsProgramming LanguagesPython Programming > Markup CGI - please test


Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump


Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support |