I'm trying to work with opts but cannot get it to work in other PC because arguments it's always empty. Below is my code.
import getopt
import sys
try:
print getopt.getopt(sys.argv[1:], "f::c::")
opts, args = getopt.getopt(sys.argv[1:], "f::c::")
except getopt.GetoptError, err:
# print help information and exit:
print str(err) # will print something like "option -a not recognized"
sys.exit(2)
print opts
print args
funcion = None
tipo = None
for opt, arg in opts:
if opt in ('-f'):
funcion = arg
if opt in ('-c'):
tipo = arg
print funcion
print tipo
usage test:
python test.py –f import_dbs –c 1
PC A result:
([('-f', 'imports'), ('-c', '1')], [])
[('-f', 'imports'), ('-c', '1')]
[]
imports
1
PC B result:
([], ['\x96f', 'import_dbs', '\x96c', '1'])
[]
['\x96f', 'import_dbs', '\x96c', '1']
None
None
the problem is with your cli command, not your code. you have dashes (or some sort of unicode) instead of hyphens
$ python test.py –f import_dbs –c 1
None
None
$ python test.py -f import_dbs –c 1
import_dbs
None
$ python test.py -f import_dbs -c 1
import_dbs
1
$ echo "python test.py –f import_dbs –c 1" | od -c
0000000 p y t h o n t e s t . p y –
0000020 ** ** f i m p o r t _ d b s –
0000040 ** ** c 1 \n
0000046
$ echo "python test.py -f import_dbs -c 1" | od -c
0000000 p y t h o n t e s t . p y -
0000020 f i m p o r t _ d b s - c
0000040 1 \n
0000042
probably caused by cutting and pasting or weird keyboard mapping.
Related
I have a simple program for traslating morse code back and forth as a school project and part of it is also writting unit tests. I ve tried for multiple hours but was not succesful at all
dictionary={'A':'.-', 'B':'-...',
'C':'-.-.', 'D':'-..', 'E':'.','F':'..-.', 'G':'--.', 'H':'....',
'I':'..', 'J':'.---', 'K':'-.-','L':'.-..', 'M':'--', 'N':'-.',
'O':'---', 'P':'.--.', 'Q':'--.-','R':'.-.', 'S':'...', 'T':'-',
'U':'..-', 'V':'...-', 'W':'.--','X':'-..-', 'Y':'-.--', 'Z':'--..'}
dictionary2={'.-':'A','-...':'B','-.-.':'C','-..':'D', '.':'E','..-.':'F','--.':'G','....':'H',
'..':'I','.---':'J', '-.-':'K','.-..':'L', '--':'M', '-.':'N',
'---':'O', '.--.':'P', '--.-':'Q','.-.':'R', '...':'S', '-':'T',
'..-':'U', '...-':'V', '.--':'W','-..-':'X', '-.--':'Y', '--..':'Z', '':' '}
def coding(s):
void=""
for i in s: #indexing
if i != ' ':
void+=dictionary[i]+' '
else:
void +=' '
print(void)
def decoding(s):
void = ""
splitstring=s.split(" ")
splitstring = s.split(" ")#splitting by spaces
#splitstring=splitstring.remove('')
for i in splitstring: #indexing
void += dictionary2[i]
print(void)
def selection(f):
f=int(input("1. Z ČEŠTINY DO MORSEOVKY || 2. Z MORESOVKY DO ČEŠTINY ")) # menu
return f
c = 1
d = 0
while(c!="0"):
d =selection(d)
a=input("ZADEJ TEXT NA ŠIFROVÁNÍ: ")
a=a.upper()
startUp="" # not being used but program cant be ran without it for some reason
if d==1: # translating to morse code
coding(a)
else: # translating from morse code
decoding(a)
c = input(("POKUD CHCTE PROGRAM UKONČIT, ZADEJTE 0 || PRO POKRAČOVÁNÍ ZADEJTE LIBOVOLNÝ ZNAK : "))
this is what i ve tried:
import pytest
from example import coding
from example import decoding
def test_coding():
assert coding('HELLO') == '.... . .-.. .-.. --- '
assert coding('WORLD') == '.-- .-. .. -. ..-. '
assert coding('HELLO WORLD') == '.... . .-.. .-.. --- .-- .-. .. -. ..-. '
test_coding()
#To write unit tests for the decoding() function, we can test that it decodes strings correctly by comparing the output of the function with the expected result. For example:
def test_decoding():
assert decoding('.... . .-.. .-.. --- ') == 'HELLO'
assert decoding('.-- .-. .. -. ..-. ') == 'WORLD'
assert decoding('.... . .-.. .-.. --- .-- .-. .. -. ..-. ') == 'HELLO WORLD'
test_decoding()
My teacher wants those tests to be ran with this command:
py.exe -m pytest --cov=. --cov-fail-under=66
in command block, it has to reach at least 66% success rate.
thus far i ve always ended up with this result:
=========================================================================================================== test session starts ============================================================================================================
platform win32 -- Python 3.9.6, pytest-7.2.0, pluggy-1.0.0
rootdir: C:\Users\ash31\OneDrive\Plocha\AP1VS-final-project
plugins: cov-4.0.0
collected 0 items / 1 error
================================================================================================================== ERRORS ==================================================================================================================
_____________________________________________________________________________________________________ ERROR collecting example_test.py _____________________________________________________________________________________________________
example_test.py:1: in <module>
from example import coding
example.py:51: in <module>
d =selection(d)
example.py:43: in selection
f=int(input("1. Z ČEŠTINY DO MORSEOVKY || 2. Z MORESOVKY DO ČEŠTINY ")) # menu
..\..\..\AppData\Local\Programs\Python\Python39\lib\site-packages\_pytest\capture.py:192: in read
raise OSError(
E OSError: pytest: reading from stdin while output is captured! Consider using `-s`.
------------------------------------------------------------------------------------------------------------- Captured stdout --------------------------------------------------------------------------------------------------------------
1. Z ČEŠTINY DO MORSEOVKY || 2. Z MORESOVKY DO ČEŠTINY
----------- coverage: platform win32, python 3.9.6-final-0 -----------
Name Stmts Miss Cover
-------------------------------------
example.py 32 22 31%
example_test.py 24 23 4%
-------------------------------------
TOTAL 56 45 20%
FAIL Required test coverage of 66% not reached. Total coverage: 19.64%
========================================================================================================= short test summary info ==========================================================================================================
ERROR example_test.py - OSError: pytest: reading from stdin while output is captured! Consider using `-s`.
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! Interrupted: 1 error during collection !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
============================================================================================================= 1 error in 0.46s =============================================================================================================
Im translating a model done on weka to python-weka-wrapper3 and i dont know how to an evaluator and search options on attributeselectedclassifier.
This is the model on weka:
weka.classifiers.meta.AttributeSelectedClassifier -E "weka.attributeSelection.CfsSubsetEval -P 1 -E 1" -S "weka.attributeSelection.GreedyStepwise -B -T -1.7976931348623157E308 -N -1 -num-slots 1" -W weka.classifiers.meta.MultiSearch -- -E FM -search "weka.core.setupgenerator.MathParameter -property classifier.classifier.classifier.numOfBoostingIterations -min 5.0 -max 50.0 -step 1.0 -base 10.0 -expression I" -class-label 1 -algorithm "weka.classifiers.meta.multisearch.DefaultSearch -sample-size 100.0 -initial-folds 2 -subsequent-folds 10 -initial-test-set . -subsequent-test-set . -num-slots 1" -log-file /Applications/weka-3-8-3 -S 1 -W weka.classifiers.meta.Bagging -- -P 100 -S 1 -num-slots 1 -I 100 -W weka.classifiers.meta.FilteredClassifier -- -F "weka.filters.supervised.instance.SMOTE -C 0 -K 3 -P 250.0 -S 1" -S 1 -W weka.classifiers.meta.CostSensitiveClassifier -- -cost-matrix "[0.0 1.0; 1.0 0.0]" -S 1 -W weka.classifiers.trees.ADTree -- -B 10 -E -3 -S 1
and I have this right now:
base = Classifier(classname="weka.classifiers.trees.ADTree", options=["-B", "10", "-E", "-3", "-S", "1"])
cls = SingleClassifierEnhancer(classname="weka.classifiers.meta.CostSensitiveClassifier",
options =["-cost-matrix", "[0.0 1.0; 1.0 0.0]", "-S", "1"])
cls.classifier = base
smote = Filter(classname="weka.filters.supervised.instance.SMOTE", options=["-C", "0", "-K", "3", "-P", "250.0", "-S", "1"])
fc = FilteredClassifier()
fc.filter = smote
fc.classifier = cls
bagging_cls = Classifier(classname="weka.classifiers.meta.Bagging",
options=["-P", "100", "-S", "1", "-num-slots", "1", "-I", "100"])
bagging_cls.classifier = fc
multisearch_cls = MultiSearch(
options = ["-S", "1"])
multisearch_cls.evaluation = "FM"
multisearch_cls.log_file = "/home/pablo/Escritorio/TFG/OUTPUT.txt"
multisearch_cls.search = ["-sample-size", "100", "-initial-folds", "2", "-subsequent-folds", "10",
"-initial-test-set", ".", "-subsequent-test-set", ".", "-num-slots", "1"]
mparam = MathParameter()
mparam.prop = "numOfBoostingIterations"
mparam.minimum = 5.0
mparam.maximum = 50.0
mparam.step = 1.0
mparam.base = 10.0
mparam.expression = "I"
multisearch_cls.parameters = [mparam]
multisearch_cls.classifier = bagging_cls
AttS_cls = AttributeSelectedClassifier()
AttS_cls.evaluator = "weka.attributeSelection.CfsSubsetEval -P 1 -E 1"
AttS_cls.search = "weka.attributeSelection.GreedyStepwise -B -T -1.7976931348623157E308 -N -1 -num-slots 1"
AttS_cls.classifier = multisearch_cls
train, test = data_modelos_1_2.train_test_split(70.0, Random(1))
AttS_cls.build_classifier(train)
evl = Evaluation(train)
evl.crossvalidate_model(AttS_cls, test, 10, Random(1))
print(AttS_cls)
#graph.plot_dot_graph(AttS_cls.graph)
print("")
print("=== Setup ===")
print("Classifier: " + AttS_cls.to_commandline())
print("Dataset: ")
print(test.relationname)
print("")
print(evl.summary("=== " + str(10) + " -fold Cross-Validation ==="))
print(evl.class_details())
plcls.plot_roc(evl, class_index=[0, 1], wait=True)
but when I do
AttS_cls.evaluator = "weka.attributeSelection.CfsSubsetEval -P 1 -E 1"
AttS_cls.search = "weka.attributeSelection.GreedyStepwise -B -T -1.7976931348623157E308 -N -1 -num-slots 1"
it reach me this error:
---------------------------------------------------------------------------
AttributeError Traceback (most recent call last)
/tmp/ipykernel_40724/2750622902.py in <module>
30
31 AttS_cls = AttributeSelectedClassifier()
---> 32 AttS_cls.search = "weka.attributeSelection.GreedyStepwise"
33 AttS_cls.classifier = multisearch_cls
34
/usr/local/lib/python3.8/dist-packages/weka/classifiers.py in search(self, search)
435 :type search: ASSearch
436 """
--> 437 javabridge.call(self.jobject, "setSearch", "(Lweka/attributeSelection/ASSearch;)V", search.jobject)
438
439
AttributeError: 'str' object has no attribute 'jobject'
I understand that I have to set them as objects because it raise this error because i try to set them as strings but I dont know how.
You need to instantiate ASSearch and ASEvaluation objects. If you have command-lines, you can use the from_commandline helper method like this:
from weka.core.classes import from_commandline, get_classname
from weka.attribute_selection import ASSearch
from weka.attribute_selection import ASEvaluation
search = from_commandline('weka.attributeSelection.GreedyStepwise -B -T -1.7976931348623157E308 -N -1 -num-slots 1', classname=get_classname(ASSearch))
evaluation = from_commandline('weka.attributeSelection.CfsSubsetEval -P 1 -E 1', classname=get_classname(ASEvaluation))
The second argument of the from_commandline method is the classname of the wrapper that you want to use instead of OptionHandler. For simplicity, I import the correct wrappers and then use the get_classname method to return the dot notation of the wrapper's class. That way I can avoid accidental typos in the classname strings.
Also, by using single quotes, you won't have to worry about Weka's quotes in the command-lines and you can just use the Weka command-line string verbatim.
You can also use the same approach for instantiating the AttributeSelectedClassifier wrapper itself, without having to go through instantiating search and evaluation separately:
from weka.core.classes import from_commandline, get_classname
from weka.classifiers import AttributeSelectedClassifier
cls = from_commandline('weka.classifiers.meta.AttributeSelectedClassifier -E "weka.attributeSelection.CfsSubsetEval -P 1 -E 1" -S "weka.attributeSelection.GreedyStepwise -B -T -1.7976931348623157E308 -N -1 -num-slots 1" -W weka.classifiers.meta.MultiSearch -- -E FM -search "weka.core.setupgenerator.MathParameter -property classifier.classifier.classifier.numOfBoostingIterations -min 5.0 -max 50.0 -step 1.0 -base 10.0 -expression I" -class-label 1 -algorithm "weka.classifiers.meta.multisearch.DefaultSearch -sample-size 100.0 -initial-folds 2 -subsequent-folds 10 -initial-test-set . -subsequent-test-set . -num-slots 1" -log-file /Applications/weka-3-8-3 -S 1 -W weka.classifiers.meta.Bagging -- -P 100 -S 1 -num-slots 1 -I 100 -W weka.classifiers.meta.FilteredClassifier -- -F "weka.filters.supervised.instance.SMOTE -C 0 -K 3 -P 250.0 -S 1" -S 1 -W weka.classifiers.meta.CostSensitiveClassifier -- -cost-matrix "[0.0 1.0; 1.0 0.0]" -S 1 -W weka.classifiers.trees.ADTree -- -B 10 -E -3 -S 1', get_classname(AttributeSelectedClassifier))
i'm working on some file consisting of interval (begin, end) and associated pattern.
exemple :
grep "HTT" dijen*.omim* | cut -f 2-4
begin end pattern
28557332 28557333 AAGAGGAGG
28557445 28557446 AAG
28557250 28557251 AAG
3076319 3076320 AGC
28542936 28542937 AGGGGGGGGGGGGGGG
3052465 3052466 AC
3085084 3085085 AC
3051274 3051275 AC
3053094 3053095 AC
28556760 28556761 AAG
3051279 3051280 AC
3085521 3085522 AC
3084920 3084962 AC
3076453 3076454 AGC
3084794 3084795 AC
3076272 3076273 ACGCCCCCCGCATCGCC
3085257 3085258 AC
3140188 3140189 AAGG
3052498 3052499 AC
3053110 3053111 AC
3084759 3084760 AC
3053008 3053009 AC
3076570 3076571 AGGGGGGGCCGCGCCGGCGG
28557240 28557241 AAG
3052366 3052367 AC
3076334 3076335 AGC
3052487 3052944 AC
3084931 3084932 AC
3075962 3075963 AGGCGGGGCCGCGCCGGCGG
28557451 28557452 AAG
3052859 3052860 AC
3052446 3052447 AC
3076498 3076499 AGC
3139943 3139944 AACGAAGGAAGGAAGG
3052450 3052758 AC
3084985 3084986 AC
28556880 28556881 AAG
3052146 3052147 AC
3052923 3052924 AC
3084967 3084968 AC
3076275 3076281 AGC
3051420 3051421 AC
3076455 3076757 AGC
grep "HTT" dijen*.omim* | cut -f 2-4 | sort -u | wc -l
336
do you think there is a way ,with bash, to increment with +1 the begin and the end of each line at every step of the loop from 0 to 500 and count how many lines are still uniq and same by begin end pattern (or 80% of homology for begin and end)
something like :
#! /usr/bin/env python3
# -*- coding: utf-8 -*-
import sys
import os
import ipdb
gene_file = [line.strip() for line in open(sys.argv[1], "r")]
finale_liste = list()
def test_intervalle(liste):
#test if 80% commun sequence between 2 intervals - if yes - merge the 2 intervals
for i in liste:
(begin_i, end_i, motif_i) = i
for j in liste:
(begin_j, end_j, motif_j) = j
if (end_i-begin_j)/(end_i-begin_i) > 0.8 and motif_i == motif_j:
liste.remove(i)
liste.remove(j)
liste.append([begin_i, end_j, motif_i])
return(liste)
for i in range(1,501):
output_name = "HTT_"+i+".txt"
liste = list()
for line in gene_file:
(begin, end, motif) = line.split("\t")
begin = int(begin)+i
end = int(end)+i
liste.append([begin, end, motif])
new_liste = test_intervalle(liste)
with open(output_name, 'w') as f:
w = csv.writer(f,delimiter="\t")
for i in new_liste:
w.writerow(i+"\n")
command_line = "sort -u "+ output_name+ "| wc -l"
stream = os.popen(command_line)
output = stream.read()
finale_liste.append((i, output))
with open("finale_liste.txt", "w") as finale:
for i in finale_liste:
finale.write(i"\n")
something like that but in bash, or awk or better python ?
from itertools import permutations
from itertools import combinations
import itertools
import random
import os
def split(arr, size):
arrs = []
while len(arr) > size:
pice = arr[:size]
arrs.append(pice)
arr = arr[size:]
arrs.append(arr)
return arrs
print("Enter the GCAP utility command:")
N=raw_input()
A= [N]
A1=[]
S7=[]
S6=[]
X=[]
X1=[]
m1=[]
G=0
A1=0
G1=[]
g1=[]
u=[]
def HELP():
print('SYNTAX : ABC')
def comp():
for x11, y11 in zip(Y, m1[G-1]):
if x11 == y11:
X1 = sorted(m1[G-1])
A1= X1
return A1
def my_containsAll(str1, Y):
for c in Y:
if c not in str1: return 0;
return 1;
def mysplit(s):
head = s.rstrip('0123456789')
tail = s[len(head):]
return head, tail
def imcon(u,U3):
U1= ['imconfig','-i']
U2=U1+u+U3
return U2
if N == 'GCOA.01': ## for first GCOA.01 command
print("Enter the command in this format ")
S=raw_input()
print ("Enter the user interface for example : ")
U= raw_input()
for s in ['GCOA.01']:
g = mysplit(s)
g1.append('-c')
g1.append(g[1])
u.append(U)
#print(g1)
if S == "GCOA.01": ## if no other parameter typed
U4= imcon(u,g1)
ss= ' '.join(U4)
#print(ss)
else:
S=S.split()
S1 =[i.split('\t', 1)[0] for i in S] ## spliting the command into list
#print(S1)
G1= S1[0]
#print(G1)
for s in ['GCOA.01']:
g = mysplit(s)
#print(g[1])
S11=S1[0]
S5=[S11]
del S1[0]
for x in S1: ## separate the parameters from the whole command
print(x)
X.append(x[:2])
Y=list(X)
S2=['-c','-a','-h']
S12 =sorted(S2)
str1 = ''.join(S12)
#print(str1)
A2= my_containsAll(str1, Y)
if A2 == 1:
#print(S2)
S3=[]
for i in range(1, len(S2)+1): ## creating the permutation and combinations of the parameters
S3.extend(itertools.combinations(S2, i))
if i >= 1:
for k in S3:
for c in permutations(k):
S6.append(c)
for l in S6:
if l not in S7:
S7.append(l)
# print(S7)
for m in S7: ## creating the list of the different possible combination of P&C
m=list(m)
m1.append(m)
#print (m1)
for i1 in m1: ## for finding the index of the parameters given by the user from the P&C list of parameters
G=G+1
if Y == i1:
#print (G)
break;
g1=g1+S1
#print(g1)
if Y == m1[0]:
R=imcon(u,g1)
ss=' '.join(R)
print(ss) ## this print are basically command which is to direct on terminal window
elif Y == m1[1]:
R=imcon(u,g1)
ss=' '.join(R)
print(ss)
else:
HELP()
Output :
imconfig -i th0 -c 01 -h -a
example: (in command prompt)
c:/python34> python ssm9.py
imconfig -i th0 -c 01 - h -a
c:/python34> imconfig -i th0 -c 01 -h -a
/// i want something like that means the above printed value redirect here. but in linux terminal window
I wrote a sample code like this
vivek#vivekUbuntu:~$ cat sample.py
#! /usr/bin/python
import socket
hostname = socket.gethostname()
def getPingCommand(host):
return "ping %s" %host
print getPingCommand(hostname)
And then executed like this:
vivek#vivekUbuntu:~$ `python sample.py`
PING vivekUbuntu (127.0.1.1) 56(84) bytes of data.
64 bytes from vivekUbuntu (127.0.1.1): icmp_seq=1 ttl=64 time=0.020 ms
64 bytes from vivekUbuntu (127.0.1.1): icmp_seq=2 ttl=64 time=0.027 ms
64 bytes from vivekUbuntu (127.0.1.1): icmp_seq=3 ttl=64 time=0.050 ms
64 bytes from vivekUbuntu (127.0.1.1): icmp_seq=4 ttl=64 time=0.050 ms
64 bytes from vivekUbuntu (127.0.1.1): icmp_seq=5 ttl=64 time=0.049 ms
^C
--- vivekUbuntu ping statistics ---
5 packets transmitted, 5 received, 0% packet loss, time 4007ms
rtt min/avg/max/mdev = 0.020/0.039/0.050/0.013 ms
vivek#vivekUbuntu:~$
Even the option mentioned by S0III0s is also correct. It works for me.
`python ssm9.py`
or
$(python ssm9.py)
or
eval `python ssm9.py`
or
eval $(python ssm9.py)
Should all work.
(You can try it like this:)
> $(echo "echo test")
test
I wrote what I thought was a straightforward Python script to traverse a given directory and tabulate all the file suffixes it finds. The output looks like this:
OTUS-ASIO:face fish$ sufs
>>> /Users/fish/Dropbox/ost2/face (total 194)
=== 1 1 -
=== css 16 -----
=== gif 14 -----
=== html 12 ----
=== icc 87 --------------------------
=== jpg 3 -
=== js 46 --------------
=== png 3 -
=== zip 2 -
... which would be great, if those values were correct. They are not. Here's what happens when I run it in a subdirectory of the directory I listed above:
OTUS-ASIO:face fish$ cd images/
OTUS-ASIO:images fish$ sufs
>>> /Users/fish/Dropbox/ost2/face/images (total 1016)
=== JPG 3 -
=== gif 17 -
=== ico 1 -
=== jpeg 1 -
=== jpg 901 --------------------------
=== png 87 ---
... It only seems to go one directory level down. Running the script one level up didn't pick up on the 'jpeg' suffix at all, and seemed to miss a good 898 jpg files.
The script in question is here:
#!/usr/bin/env python
# encoding: utf-8
"""
getfilesuffixes.py
Created by FI$H 2000 on 2010-10-15.
Copyright (c) 2010 OST, LLC. All rights reserved.
"""
import sys, os, getopt
help_message = '''
Prints a list of all the file suffixes found in each DIR, with counts.
Defaults to the current directory wth no args.
$ %s DIR [DIR DIR etc ...]
''' % os.path.basename(__file__)
dirs = dict()
skips = ('DS_Store','hgignore')
class Usage(Exception):
def __init__(self, msg):
self.msg = msg
def getmesomesuffixes(rootdir, thisdir=None):
if not thisdir:
thisdir = rootdir
for thing in [os.path.abspath(h) for h in os.listdir(thisdir)]:
if os.path.isdir(thing):
getmesomesuffixes(rootdir), thing)
else:
if thing.rfind('.') > -1:
suf = thing.rsplit('.').pop()
dirs[rootdir][suf] = dirs[rootdir].get(suf, 0) + 1
return
def main(argv=None):
if argv is None:
argv = sys.argv
try:
try:
opts, args = getopt.getopt(argv[1:], "h", ["help",])
except getopt.error, msg:
raise Usage(msg)
for option, value in opts:
if option == "-v":
verbose = True
if option in ("-h", "--help"):
raise Usage(help_message)
if len(args) == 0:
args.append(os.getcwd())
for durr in [os.path.abspath(arg) for arg in args]:
if os.path.isdir(durr):
dirs[durr] = dict()
for k, v in dirs.items():
getmesomesuffixes(k)
print ""
for k, v in dirs.items():
sufs = v.items()
sufs.sort()
maxcount = reduce(lambda fs, ns: fs > ns and fs or ns, map(lambda t: t[1], sufs), 1)
mincount = reduce(lambda fs, ns: fs < ns and fs or ns, map(lambda t: t[1], sufs), 1)
total = reduce(lambda fs, ns: fs + ns, map(lambda t: t[1], sufs), 0)
print ">>>\t\t\t%s (total %s)" % (k, total)
for suf, sufcount in sufs:
try:
skips.index(suf)
except ValueError:
print "===\t\t\t%12s\t %3s\t %s" % (suf, sufcount, "-" * (int(float(float(sufcount) / float(maxcount)) * 25) + 1))
print ""
except Usage, err:
print >> sys.stderr, sys.argv[0].split("/")[-1] + ": " + str(err.msg)
print >> sys.stderr, "\t for help use --help"
return 2
if __name__ == "__main__":
sys.exit(main())
It seems that getmesomesuffixes() is subtly not doing what I want it to. I hate to ask such an annoying question, but if anyone can spot whatever amateur-hour error I am making with a quick once-over, it would save me some serious frustration.
Yeah, Won't you be better off if you used os.walk
for root, dirs, files in os.walk(basedir):
... do you stuff ..
See the example at
http://docs.python.org/library/os.html
Also look at os.path.splitext(path), a finer way to find the type of your file.
>>> os.path.splitext('/d/c/as.jpeg')
('/d/c/as', '.jpeg')
>>>
Both of these together should simplify your code.
import os
import os.path
from collections import defaultdict
def foo(dir='.'):
d = defaultdict(int)
for _, _, files in os.walk(dir):
for f in files:
d[os.path.splitext(f)[1]] += 1
return d
if __name__ == '__main__':
d = foo()
for k, v in sorted(d.items()):
print k, v