Author Topic: Loops And Such  (Read 7439 times)

bam

  • Hero Member
  • *****
  • Posts: 1213
    • View Profile
    • http://thegrinder.ws
Loops And Such
« on: August 19, 2006, 04:13:40 am »
I have this so far:
Code: [Select]
for item in dupes[]:
   mydupeitem=item
   for item in filter[]:
      if mydupeitem[2:] = item:
         pass
      else
         write......etc.


now is there anything wrong with this? it wipes out my entire bash history, so it never gets to the write statement. filter[] is just a list of filters(strings) and dupes[] is just a list of strings as well, basically i an 'cleaning' my bash history at logoff, I have a remove duplicates script running and it sorts but I would like to remove any "cd blahblah" and rm's mv's etc. any ideas, I am new to python, so any help would be great.
SL-C3100 current: Stock/Tetsu 18h
Socket BT CF Card
Linksys WCF-12 802.11b/Cheapie USB Ethernet

The Grinder

zmiq2

  • Sr. Member
  • ****
  • Posts: 383
    • View Profile
    • http://
Loops And Such
« Reply #1 on: August 19, 2006, 05:45:19 pm »
Instead of

Code: [Select]
for item in dupes[]:
  mydupeitem=item

I would use something like

Code: [Select]
for mydupeitem in dupes[]:
  

I centainly think that using the same variable name, item, in both nested loops doesn't seem a good idea and could cause an undesired side effect.

HTH
sl-c750, archos av580, socket cf [bt, wifi, modem], noname cf lan, audiovox rtm800 gsm-gprs cf, rom: sharp -> oz3.5.3 -> cacko -> oz3.5.4.1

bam

  • Hero Member
  • *****
  • Posts: 1213
    • View Profile
    • http://thegrinder.ws
Loops And Such
« Reply #2 on: August 19, 2006, 09:32:21 pm »
I agree, I thought "item" was one of the collections constants, such as in vb with "for each vbtextbox in form" i know the wrong syntax but you get the idea.
SL-C3100 current: Stock/Tetsu 18h
Socket BT CF Card
Linksys WCF-12 802.11b/Cheapie USB Ethernet

The Grinder

kopsis

  • Sr. Member
  • ****
  • Posts: 329
    • View Profile
    • http://kopsisengineering.com
Loops And Such
« Reply #3 on: August 19, 2006, 10:14:53 pm »
Quote
I have this so far:
Code: [Select]
for item in dupes[]:
   mydupeitem=item
   for item in filter[]:
      if mydupeitem[2:] = item:
         pass
      else
         write......etc.
[div align=\"right\"][a href=\"index.php?act=findpost&pid=139096\"][{POST_SNAPBACK}][/a][/div]

A few observations:

* Your "if mydupeitem..." line is using an assignment (=) instead of a compare (==).
* You're reusing your loop variable (item) in the inner loop. I'm not sure Python cares (I'm in the habit of not trying such things), but in other languages like C that would cause much breakage. Note: "item" is just a variable that gets set on each loop iteration. You can use any variable name there that you wish.
* I don't understand the empty [] brackets on the "for item ..." lines. They should cause syntax errors.

A more elegant solution (assuming you're using a recent version of Python would be something like:
Code: [Select]
def f(x):
    for item in filter:
        if x[2:] = item:
            return false
    return true

filtered_list = filter(f, dupes)

That will run f(x) on every element in dupes and set filtered_list equal to the list of those for which f(x) returned true (i.e. they did not match any items in the filter list).

bam

  • Hero Member
  • *****
  • Posts: 1213
    • View Profile
    • http://thegrinder.ws
Loops And Such
« Reply #4 on: August 20, 2006, 12:35:49 am »
morning,
yea there was some issues, got it all solved, though not quite as nice as yours. I am new to c-style programming, mostly I diddle with visual basic(for my company work). I saw that python had a nice gui to play with pyQt so this "seemed" closest to vb, so I thought the learning curve would be a little more flat, but guess not. I will however need to start writing "functions" and "subs" in order to keep stuff organized and not spagetti code, not only that but reuseability will be nicer.


actually here is what I did:
Code: [Select]
filterlist = ["df","cd","mv","cp"]

(removed duplicates before filter)

for ditem in noDupes:
   for fitem in filterlist:
      if ditem[2:] ==fitem:
         filterfound == bool(True)
         break
      if filterfound == False:
         fileToOutput.write(ditem)
      filterfound == bool(False)



I use this script to clean up my .bash_history file of duplicates and unwanted commands. It executes on exiting console(.bash_logout)
« Last Edit: August 20, 2006, 12:43:25 am by bam »
SL-C3100 current: Stock/Tetsu 18h
Socket BT CF Card
Linksys WCF-12 802.11b/Cheapie USB Ethernet

The Grinder

kopsis

  • Sr. Member
  • ****
  • Posts: 329
    • View Profile
    • http://kopsisengineering.com
Loops And Such
« Reply #5 on: August 20, 2006, 08:10:36 am »
Quote
yea there was some issues, got it all solved, though not quite as nice as yours. I am new to c-style programming, mostly I diddle with visual basic(for my company work). I saw that python had a nice gui to play with pyQt so this "seemed" closest to vb, so I thought the learning curve would be a little more flat, but guess not. I will however need to start writing "functions" and "subs" in order to keep stuff organized and not spagetti code, not only that but reuseability will be nicer.
[div align=\"right\"][{POST_SNAPBACK}][/a][/div]

Congrats on getting your program working  Going from VB to any modern language is going to have bit of a learning curve, but if you stick with it you'll make yourself a much better programmer. However, I have to disagree with your statement that you're new to C style programming. What you call C style is technically referred to a "imperative" programming. Both VB and C (at least in conventional usage) are actuall very similar in abstract programming language terms.

What causes a lot of confusion for new Python programmers is that it offers a number of features borrowed from the world of "functional programming". Things like lists, first-class functions, list comprehensions and built-ins like map() and filter() let you break out of the imperative style when writing Python code.

Why would you want to do that? Well, research shows that for many types of problems functional programming techniques result in much smaller programs with far fewer bugs. It makes it possible to work with programs as mathematical expressions rather than cookbook recipes. Ask yourself "where are most of the bugs in my programs?" The answer is usually "loops and state". Note how my example contained neither  The function I defined wasn't there for traditional structured programming purity, it was there so I could pass it as a variable to the filter() built-in  The whole idea of passing functions as variables may sound a bit strange, but that's one of the keys to FP.

Now, just as one can write FORTRAN style code in C++, it's possible to write VB style code in Python. But if you really want to improve yourself as a programmer, focus on learning to use the FP features in Python. In the long run, you'll be glad you did  If you want to learn more [a href=\"http://www.defmacro.org/ramblings/fp.html]Functional Programming For The Rest Of Us[/url] is a good place to start.

bam

  • Hero Member
  • *****
  • Posts: 1213
    • View Profile
    • http://thegrinder.ws
Loops And Such
« Reply #6 on: August 20, 2006, 12:09:11 pm »
FP is just, phenominal, actually I like it(about half way through the article now. This dramatiacally simplifies coding.
SL-C3100 current: Stock/Tetsu 18h
Socket BT CF Card
Linksys WCF-12 802.11b/Cheapie USB Ethernet

The Grinder

bam

  • Hero Member
  • *****
  • Posts: 1213
    • View Profile
    • http://thegrinder.ws
Loops And Such
« Reply #7 on: September 01, 2006, 10:20:35 pm »
well here is the end result, I think I am getting ahold of it

#!/usr/bin/env python

import os
import fileinput

fileToSearch="/home/zaurus/.bash_history"

#filterlist for non-saves, just add as needed
filterlist=[ "cd","mv","cp","df","exit"]
filteredlist=[]
mylist=[]
noDupes=[]

[mylist.append(line) for line in fileinput.input( fileToSearch )]
fileinput.close()

[noDupes.append(j) for j in mylist if not noDupes.count(j)]
noDupes.sort

filteredlist=noDupes
os.remove(fileToSearch)
fileToOutput = open( fileToSearch, 'w' )

[filteredlist.remove(y) for x in filterlist for y in noDupes if x == y[0:len(x)] ]
[fileToOutput.write(item) for item in filteredlist]
fileToOutput.close()

basically run this under .bash_logout to clean up my bash_history file of duplicates and unwanted commands, eventually I will have it check against "valid" bash commands/executables as well.
SL-C3100 current: Stock/Tetsu 18h
Socket BT CF Card
Linksys WCF-12 802.11b/Cheapie USB Ethernet

The Grinder