Archive for category Python

I think I just figured out how to cure cancer

class CancerBot extends NanoBot:
	def onCellIdentified(self, cell):
		if cell.is_cancer():
			self.destroy(cell)

bot = CancerBot()
bot.run()

All I need is some help with implementation details.

Leave a Comment

Disabling images in Python Markdown

Here’s a quick snippet that shows how to disable images (or any extension) in Python Markdown:

import markdown

class MyMarkdownExtension(markdown.Extension):

	def extendMarkdown(self, md, md_globals):
		del md.inlinePatterns['image_link']
		del md.inlinePatterns['image_reference']

my_markdown_extension = MyMarkdownExtension()

raw_text = 'This is a test image: ![Google Logo](http://www.google.com/images/logos/ps_logo2a_cp.png)'

print markdown.markdown(raw_text, [my_markdown_extension], safe_mode = 'escape')

Edit:
Some processing is disabled through md.parser.blockprocessors instead.

Here are the options for each:
md.inlinePatterns:

'backtick'
'escape'
'reference'
'link'
'image_link'
'image_reference'
'short_reference'
'autolink'
'automail'
'linebreak2'
'linebreak'
'html'
'entity'
'not_strong'
'strong_em'
'strong'
'emphasis'
'emphasis2'

md.parser.blockprocessors:

'indent'
'code'
'hashheader'
'setextheader'
'hr'
'olist'
'ulist'
'quote'
'paragraph'

5 Comments

typeof in python

I could not find a good way to get the name of a class from an instance with Python on google. It’s simple (but not obvious):

str(type(var))

1 Comment

Follow

Get every new post delivered to your Inbox.