[Tutor] Data persistence problem

Jim Mooney cybervigilante at gmail.com
Sat Jun 22 01:00:43 CEST 2013


On 21 June 2013 14:59, ALAN GAULD <alan.gauld at btinternet.com> wrote:

>
> Give us a clue, show us your code!

I was hoping you wouldn't say that since it's another of my insane
Lazy Typer programs to avoid typing, which are no doubt considered
frivolous. Although I'm learning a lot doing them ;')

Okay, I have a snippet that posts the below text in automatically. I
can use it to make any number of dicts, lists, or sets, from just
tokens in a single string, and they are printed out as the different
types, named and and numbered so they have different program names.
That way I don't have to type in quotes, commas, colons, and so forth.
Once the interpreter prints them out, I just select all and paste them
into my program, replacing what's below. Kind of a manual
meta-programming ;')

============= pasted in snippet ================

## Output sequence will be all strings. The first type in the 2nd
parameter list will be the
## default due to 'or" short circuiting, so you can just delete to get
what you want.
## You MUST have an even number of tokens for a dict or you'll get an exception.

from maker import makeseq
makeseq("Replace me, even number of tokens for dict", dict or list or set)

============= end snippet ====================

>From this I can create any number of numbered dicts, lists or sets
(The 'or' short circuit means I get dicts as default, and only have to
delete from the front to get the others - dicts are default since
they're the most annoying to type - now That's lazy.)

For instance if I type this in after the import above:

makeseq("this better be an even number of tokens", dict or list or set)
makeseq("bacon pigs lies politicians foreclosures bankers
cheeseburgers good", dict or list or set)
makeseq("this is a list and it is not a very big list", list or set)
makeseq("Yet another list to show the different types increment
seperately", list or set)
makeseq("and finally some sets", set)
makeseq("sets can be be be be be very very useful to eliminate
duplicates duplicates", set)
makeseq("But this time I'm just counting set incidence up to three", set)

I get this in the interpreter:

D1 = {'Replace': 'me,', 'for': 'dict', 'of': 'tokens', 'even': 'number'}
D2 = {'even': 'number', 'of': 'tokens', 'be': 'an', 'this': 'better'}
D3 = {'cheeseburgers': 'good', 'bacon': 'pigs', 'lies': 'politicians',
'foreclosures': 'bankers'}
L1 = ['this', 'is', 'a', 'list', 'and', 'it', 'is', 'not', 'a',
'very', 'big', 'list']
L2 = ['Yet', 'another', 'list', 'to', 'show', 'the', 'different',
'types', 'increment', 'seperately']
S1 = {'sets', 'some', 'finally', 'and'}
S2 = {'eliminate', 'to', 'very', 'can', 'be', 'sets', 'duplicates', 'useful'}
S3 = {'just', 'to', 'time', 'incidence', 'set', 'this', "I'm", 'But',
'up', 'counting', 'three'}

Come to think of it I should rename makeseq since only the list is a sequence.

Then I just paste all that in to replace the original routine.  Since
I'm learning, I just like to have objects I can practice on without a
lot of typing difficult characters.

Below is the module, maker.py, that I import. As you can see I'm using
globals to increment the numbering for dicts, lists, and sets,
separately. This only creates strings at present, but I realized I can
get around that and "unstring" once I learn regular expressions. Since
I'm going "outside" the program to a higher level, and copying back
into it, I can break some informatic rules. It's the way you could
write a Java program with python, then run it, doing unPythonic
things. That's my tentative theory, anyway. (Although using Python to
write Java is like using a Faberge egg to pound nails, IMHO ;')

================== begin maker.py module ========================

'''Accept a space-separated string of tokens that are each contiguous
characters.
The second parameter will turn them into a dict, list, or set. Dicts
must have an
even number of tokens. More than one of each type will be sequentially numbered
so they can be differentiated, such as D1, D2, L1, L2, L3. S1, S2,
etc. All sequences
will be printed out to be copied from the interpreter for programmatic use.'''

dictnumfarkadoodle = listnumfarkadoodle = setnumfarkadoodle = 0
# Since these are global I'm using words not likely to be duplicated
until I figure a different way and
# replace 'farkadoodle' with '' ;')

def makeseq(instring, typein):
    global dictnumfarkadoodle, listnumfarkadoodle, setnumfarkadoodle
    if isinstance(dict(), typein):
        newdict = {}
        dl = instring.split()
        if len(dl) % 2 != 0: raise Exception ("list entries must be
even") # so they match
        for idx in range(0,len(dl),2):
            newdict[dl[idx]] = dl[idx+1]
        dictnumfarkadoodle += 1
        print('D' + str(dictnumfarkadoodle) + ' =', newdict)
    elif isinstance(list(), typein):
        newlist = []
        dl = instring.split()
        for word in dl:
            newlist.append(word)
        listnumfarkadoodle += 1
        print('L' + str(listnumfarkadoodle) + ' =', newlist)
    elif isinstance(set(), typein):
        newset = set()
        dl = instring.split()
        for word in dl:
            newset.add(word)
        setnumfarkadoodle += 1
        print('S' + str(setnumfarkadoodle) + ' =', newset)
    else: print('Second parameter must be list, set, or dict')

# oops, I error out on a non-type 2nd parameter. Fix this later

============== end module ====================

A pride of lions, a gaggle of geese, a pack of wolves, a sewer of bankers.


More information about the Tutor mailing list