Forum Archive

ForumCodeBlock

[deleted]

Here is my ForumCodeBlock class. It returns either the first python script in a Pythonista Forum thread, or the first plist.

Python scripts are identified as code blocks beginning with a triple quoted string or else import.

Plists are identified as code blocks beginning with the XML signature.


image (c) @tony .... (for more classes in this family see... Classes )


ForumCodeBlock.py

import urllib2, bs4

class ForumCodeBlock(object):
    def __init__(self):
        self.__make_self()

    def __make_self(self):
        self.ForumCodeBlock_version = '3.1'
        self.ForumCodeBlock_source_code = 'Original by @tony.'
        self.ForumCodeBlock_permissions = 'Permission to use/subclass/redistribute, but NOT to modify code.'
        self.forum_url = None
        self.block_type = None

    def text(self):
        for fcb in bs4.BeautifulSoup(urllib2.urlopen(self.forum_url).read()).find_all('code'):
            sT = fcb.getText()
            if self.block_type == 'python':
                if sT[:3] == "'''" or sT[:6] == 'import':
                    return sT
            elif self.block_type == 'plist':
                if sT[:5] == '<?xml':
                    return sT
        return None

if __name__ == "__main__":
    fcb = ForumCodeBlock()
    fcb.forum_url = 'http://omz-forums.appspot.com/pythonista/post/5885488242098176'
    fcb.block_type = 'python'
    print fcb.text()
    fcb.block_type = 'plist'
    print fcb.text()

Version 3.1: tidy public interface

Larry_Mondello

@tony, How about getting onto GitHub like the rest of the world. Publishing all this code on The forum is just making the code hard to find and is cluttering up the forum. GitHub is the way that the world shares code.

[deleted]

@Larry_Mondello If I've understood you I would make a post here for each script I want share from GitHub and that would be the same number of posts. Plus I'd have to monitor two places then... here and GitHub.

I'm new to Pythonista/python (and don't know GitHub) and I've seen posts here that put me off GitHub, the latest about cluttering up Pythosista's folder structure and slowing it right down.

Good on those who know and like GitHub, but personally, for me the forum is fine.

JonB

GitHub is the best way to manage large projects, or projects that have multiple contibutors. The only thing this slows down is the pythonista move file dialog (which, if you don't use folders anyway you'll never have to use).

Gist on pythonista works well for single files.

Using gistcheck allows you to create a few action menu items to create a new gist, update the current file from the version in gist, and commit changes you are working on back to gist. Revision history is maintained automatically, and you can go back anytime to a previous rev.

Then you'd be able to just point a link / screen shot once, rather than having it bump your thread each time you add code. I too have been confused by seeing a dozen posts in Share Code where no code is actually shared....

If you've never used any kind of source/version control system before, I think you'll like it, and find it solves many of the types of problems you are likely finding you need to reinvent (I imagine that you've found the need to update versions, so probably are working on a modification of ForumCodeBlock that edits your post to add the new version)

Of course, github and gisthub make it easy to share your code, but also for others to modify and improve your code (in their own forks) which you can then easily merge back in. I don't think I've ever seen a license such as what you seem to be trying which allows unlimited use of the code, but not modification of it. there may be limitations on redistribution of modified code, like attribution, etc, but no modification at all? what does that even mean? Why share your code at all?

dgelessus

The way I'm doing it is putting all my scripts into a single repo. This is not how GitHub is really meant to be used and I'm fully aware of that, but since almost all of my projects are single scripts it would be a waste to create a repo for each. Copying over single text files is not harder than doing so from the forum and it doesn't clutter up the folder structure.

Main advantages of GitHub are:

  1. You can always link to the latest version (like this)
  2. Thus, shorter posts
  3. You can place READMEs and documentation next to your scripts without enlarging the main download
  4. Others can fork your repo and send pull requests to suggest code improvements
  5. You can set others as collaborators on your repo and they will be able to edit it freely
  6. Free web hosting on http://github.io/ if you want to

FYI about the folder structure issue - that only slows down the move menu, not the entire app. It also only becomes really noticeable once you install many (and large) external packages.

[deleted]

Wow... that touched a nerve!

On a light note @JonB ... I've used version control most of my life. You wouldn't have known that and it's kind to have offered the help. I started computing as a kid on a plastic computer with 3 flip-flops and a manual clock that my brother bought me out of a Scientific American ad!

I'm coding now for fun... it's just my craziness I'm sure, but I can't get excited about version control, it's great to be able to forget what a repo is beyond it being a cheap used car! :)

ccc

Wow! I have never hear of the plastic computer. With today's 3d printers, you could probably print one off. Cool!!

[deleted]

@ccc :)