« first day (895 days earlier)      last day (4044 days later) » 

Zac
6:03 PM
how do I check if a string is in a list/dictionary
if username in adminList:
doesn't return true despite there being a username string equal to one of the entires in adminList
 
ooh i know this one
 
Zac
do tell
 
if "mystring" in ["mystring", "is", "maybe", "here"]
 
here's a example i believe
def checkWord(answer):
    myFile = open("words.txt","r")
    for eachLine in myFile:
        if answer == eachLine.strip():
            return True
    return False
 
and dict is just a question of if you want to check in the keys or values
if "mystring" in dict.keys:
 
6:05 PM
keys is redundant, and also wrong.
 
Zac
my admin list is loaded from a text file into the adminlist variable I can't work out if it is a dictionary or a list
 
In that you meant keys(), which you don't need because in does that anyway.
@Zac How are you loading it? That's insane.
 
yeah, you're right ... "mystring" in dictis enough
 
Zac
def loadAdminFile():
	global adminUsernamePath
	global adminList
	with open(adminUsernamePath) as file:
		adminList = file.readlines()
 
well, readlines never returns a dict, so it's a list, but the lines are \n-terminated.
 
6:07 PM
There's your problem. readlines() returns a list of lines, with the newlines still attached.
 
but to check whether a string is in the values of a dictionary you have to "mystring" in dict.values()validate
 
Zac
this is my fucntion that isn't working
 
also, here's a rule of thumb for using global: if you have the keyword global in your code, you've done something horribly wrong.
 
Zac
    def removeAdmin(removeUserName):
	#xcomPrint("In removeAdmin")
	global adminList
	xcomPrint("RemoveUser : " + str(removeUserName))
	if removeUserName in adminList:
		del adminList[userName]
		xcomPrint(userName + " removed from winamp admin database")
		writeAdminFile()
	else:
		xcomPrint("Username not in database")
 
so there really isn't a BobSmith value in your list. But there is a BobSmith\n entry.
(supposing that the user name is "BobSmith")
You could do , adminList = file.read().split("\n") instead of adminList = file.readlines(). That would get rid of the newlines.
 
Zac
6:10 PM
but I write new lines when I add a new user
def addAdmin(newUserName):
	#xcomPrint("In addAdmin")
	global adminList
	adminList.append(newUserName + "\n")
	xcomPrint("Added " + str(newUserName))
	writeAdminFile()
 
file.read().splitlines() is a better choice
drops also all kinds of line breaks
 
when you append to adminList, don't add "\n" to it. Then, in writeAdminFile, add newlines during the writing of the file.
 
Is there a place when i can paste some python and ask how i could improve it?
 
For example, file.write("\n".join(adminList))
 
6:12 PM
That's where you post working code that you want to see critiqued.
 
Zac
ok that'll work
There is a further issue the adminlist is a list because I've used .append on it right? so if not a dictionary how do I remove a name from it ?

    del adminList[userName]

doesn't appear to be removing entires
 
If adminList is a list you'd need to remove things by index.
 
never use del to remove elements!!!
 
or use .pop() or .remove(userName).
@ColinO'Coal: why not, actually?
Usually, not in a loop, because you need to be aware of the iterator interactions with a changed list.
but generally speaking, there is nothing wrong with a del somelist[someindex] call.
 
because it's no gurantee that the implementation of your interpreter will remove/delete the object/referece/etc. definitively
 
6:19 PM
Eh?
 
Zac
so I have a list of usernames, called adminlist ['bob', 'fred', 'terry'] how do I remove fred ?
 
Values that are no longer referenced are removed.
 
okay, it works for lists
no not ever - more or less in responsibility of the implementation of the GV
 
@ColinO'Coal: Not sure where you got that idea from..
 
6:20 PM
del adminlist[adminlist.index("fred")] works. A tad ugly however.
 
In CPython reference counting is used, and a ref count dropping to 0 means it'll be cleared immediately.
 
adminlist = filter(lambda name: name != "fred", adminlist), perhaps?
 
the GC only has to break circular dependencies.
You only need to worry about this if you are dealing with things that need clearing up.
Such as files, that need to be closed.
 
adminlist = [a for a in adminlist if a != "terry"]
 
6:22 PM
In Jython or IronPython, you want to close explicitly rather than depend no the auto-close on delete.
 
Files and sockets are a special case.
 
Generally a good idea to use them as a context manager to make sure they get closed.
 
Oh! Here's a good way. adminlist.remove("terry").
Short and sweet.
 
But to state that del somelist[index] is not a good idea is nonsense.
 
Generally I agree with you, but look: `>>> l = [1,2,3]
>>> del l[0]
>>> l
[2, 3]`
 
6:27 PM
@ColinO'Coal: Better link to the canonical documentation rather than the (possibly outdated) printed copy copy.
 
Looks good so far.
 
And how is that a problem, Colin?
 
@mar
 
Better avoid using l as a variable.
 
@MartijnPieters: next time, I'll do my very best to rember
 
6:28 PM
it is too easily confused with other letters.
 
It's not a problem. First I've seen Zacs sample and Kevins agreement, my toes folded up, because I assumed that using delnever would be a good idea to remove elements from collections or objects.
But presumably it's a feasible way.
ugly
 
Zac
thanks kevin I'll use .remove(username)
 
Personally I tend to have difficulty with deleting elements from a collection, since I'm usually iterating through them at the time.
So I usually just compile a stuffToDelete collection, and filter out the offending elements with myCollection = myCollection - stuffToDelete
 
Iterating and removing on parallel time without special constructs like in Java the "iterator" with special methods for deletion.
 
Or similar. You get the idea.
 
Zac
6:34 PM
damn still not working and I don't know why :

    def removeAdmin(removeUserName):
	global adminList
	if removeUserName in adminList:
		adminList.remove(removeUserName)
		xcomPrint(removeUserName + " removed from winamp admin database")
		writeAdminFile()
		return
	else:
		xcomPrint("Username not in database")
 
how are you verifying that it is "not working"?
 
perhaps it's case sensitive. Maybe the entry in the admin list is "BobSmith" and you're trying to remove "bobsmith".
 
Zac
I am entering bob as a username
and I can see the text file
it has bob in it
and the if statement is falling through to the else
 
Newlines again?
 
if you print adminList, what does it show?
 
Zac
6:37 PM
it prints "bob" "fred"
on new lines
 
That's extremely strange. When I print a list, it's always on one line.
Even if the strings that make up the list contain newlines.
 
That is because the contents of a list are always shown using repr().
 
Zac
humm, might be that I'm loading the new lines from the text file into the adminlist
 
and repr('string with \n newlines \n') uses escape sequences for newlines.
 
Zac
so this
def loadAdminFile():
	global adminUsernamePath
	global adminList
	with open(adminUsernamePath) as file:
		adminList = file.readlines()
 
6:39 PM
@Kevin, turn it into a string with ''.join(yourlist) when printing and it'll print the newlines.
@Zac: That preserves newlines.
 
Zac
puts the text including new line into it
 
adminList = file.read().splitlines() will remove the newlines.
 
So i'm trying to run a word through a large txt of words, with this
def test(letters):
    myFile = open("words.txt","r")
    for line in myFile:
        if (Counter(letters) - Counter(line)) == 0:
            print(line)


test("dog")
With the idea of using counters to find words with the exact ssame spelling
then returnin the word
so this would return dog obviously, although it could be test("ogd")
and return dog
 
To find whether two words contain the exact same characters, you can do Counter(a) == Counter(b)
 
Zac
wow at last it is working
 
6:43 PM
cool i'll try that Kevin
 
Zac
thx @MartijnPieters
 
better way for open your file -> use the with statement
 
@ColinO'Coal Is that at me or Zac?
 
sorry, mean you @jack
 
I dislike with myself. To me, it unnecessarily introduces another level of indentation. YMMV of course.
 
6:45 PM
or everybody ... whatsoever you are doing with files -> use the with statement to close them automatically after leaving the with block
 
I got it
I missed the .strip()
 
if you not use the with statement, then at least block the read`operation into a try: ... finally: and close the open file in the finally sub-block.
 
I have a confession. I basically never close a file object when I'm writing a small script.
 
it's a really bad idea, to leave file (and other streams) opened
 
Usually I just do data = file("input.txt").read().split("\n")
If there's a way to open a file, read all the data, and close it, all in one line, I'll be happy to use it!
 
6:48 PM
And if your scripts hangs and the process crashes an pass down the thread to the system level?
Then the file handle still alives.
And maybe bocked.
And this may results - according to the operation system - into a problem.
 
The only scripts I've ever written that caused the process to crash also caused the whole system to crash. I assume the file handles are all sorted out when I reboot :-)
 
inveterate killer argument ^^
 
Don't emulate me at home, kids. Close your files when you are done with them.
And eat your vegetables.
 
hehehe, great, you made my day
 
@Kevin How could i change
Counter(letters) == Counter(line.strip())
Um how do i explain this
 
7:05 PM
How to get it to return all words that match, not just the first one?
def getAnagrams(letters):
    myFile = open("words.txt')
    words = myFile.read().splitlines()
    myFile.close()
    return [word for word in words if Counter(letters) == Counter(word)]
 
all words but 1 letter
basically trying to find the largest word from a large list .txt
from a jumble of characters
so 'dogx' would return dog
as the largest
 
search for "longest common substring"
 
Out of curiosity? what's the use case for this? Trying to write an app to help you win at Scrabble?
 
Haha almost
I suppose bit like countdown
i have jumble of letters, 'agrsuteod’
then it looks from a list for the biggest word it can make
like 'outrage' for example
This is what i have currently
   from collections import Counter

def test(letters):
    myFile = open("words.txt","r")
    for line in myFile:
        if Counter(letters) == Counter(line.strip()):
            print(line)



test("dog")
So that would return dog and god
Brb food.
Back
 
ok, I suggested Counter(letters) == Counter(line) when I thought that you wanted all words that contain exactly the letters in letters.
If you want all words that can be made from the letters, but not necessarily all of them, then the checkYinX solution Martijn suggested is appropriate.
You might write a function like this:
from collections import Counter

def checkYinX(y,x):
    return not (Counter(y) - Counter(x))

def wordsICanMake(letters):
    file = open("words.txt")
    words = file.read().splitlines()
    file.close()
    return [word for word in words if checkYinX(word, letters)]
words = wordsICanMake("agrsuteod")
words.sort(key = len, reverse = True)
print words[0]
output: outraged
the sort method there sorts the words in descending order of size, and then you print the first element.
 
7:25 PM
Okay cool, But how would i amend that as to only return the longest strings?
Wait
sorry
I didn't add the sort at the end,
 
Hmm, instead of print word[0] you could do print [word for word in words if len(word) == len(words[0])], and that will give all words with the biggest length
 
I was just typing how would i do that!
you read my mind
 
ex. ["outraged", "outrages"]
 
Boom
I love you Kevin
 
7:29 PM
and you of course colin
Be right back
 
instead of words.sort(key=len, reverse=True)[0] you should also can use max(words, key=len)
should not be so expensive than the sort operation
 
that's a neat trick! got to close my browser but i should be back in a bit..
 
the key kwarg is really great - you can also use lambda expressions to create a comparator in a very simple way
 
Zac
7:47 PM
I have an issue with a python module import
If I import it once it works
but have "unloading" the python script if I run it again it throws an error
is there a way to ensure it is out of memory
Its a python script that uses win32 api
for controlling winamp
I get this error : File "C:\Python27\Lib\winamp2.py", line 27, in <module>
import win32api, win32con, win32gui, win32process, pywintypes
File "C:\Python27\lib\site-packages\win32\lib\pywintypes.py", line 124, in <module>
__import_pywin32_system_module__("pywintypes", globals())
File "C:\Python27\lib\site-packages\win32\lib\pywintypes.py", line 114, in __import_pywin32_system_module__
assert sys.modules[modname] is old_mod
TypeError: 'NoneType' object has no attribute '__getitem__'
which I think is due to it loading the library twice in windows
 
Can you give a practical sample how it first works and then not?
You said you guess you have an import problem.
But ... where you are "unloading" what?
 
He's running python scripts from an IRC client that allows users to supply their own python extensions.
 
oh ha
 
When he calls "/unload [whatever]" from the client, it "unloads" the script.
 
okay, got it
ahm, this could be an environment and pythonpath problem
 
7:54 PM
Which we've determined doesn't work very well sometimes. Namely when you have unterminated threads, and now apparently while importing some modules.
 
Zac
I load it on an IRC client and unload it there
yeah thanks kevin
the script that worked at first when I first ran it was this
 
So, first time - you load the script - it works.
 
Zac
__module_name__ = "Xcom Winamp2 Test"
__module_version__ = "1.0.0"
__module_description__ = "Xcom Winamp2 TEST"
__doc__ = "To be implemented"

import winamp2

winamp = winamp2.Winamp2()

winamp.play()



print "\0034",__module_name__, __module_version__,"has been loaded\003"
 
After unloading and loading again - it not works.
 
Zac
but the next time I ran it it didnt' work
yes
 
7:56 PM
@Zac: for pasting code, you can also use internet services like pastebin.com
 
Zac
The reason I'm using winamp2 is because I was testing another library called winamp but it didnt' have any return playlist functions
 
the name of your script is "winamp.py", right?
could you test renaming the variable winamp into wa or something else, which is not equal named like your (or another) script?
 
Zac
winamp2.py
 
...
wa = winamp2.Winamp2()
wa.play()
...
 
Zac
yeah
worked the first time I ran it with no errors but when I run again I get that pasted error abov
I checked that \pywintypes.py file
and it is full of comments saying windows tries to load it many times
and work arounds
in fact the comments are quite funny
 
8:00 PM
mhm
you can try following:
if not pywintypes:
import pywintypes
in your head import block
 
Zac
do I add that to the winamp2.py file ?
 
yeah and no
 
Zac
?
 
remove the import of pywintypes in your script head
and attach this if-guard-clause in the next line
...
import win32api, win32con, win32gui, win32process
if not pywintypes:
import pywintypes
...
 
Zac
didn't work
Traceback (most recent call last):
File "C:\Users\OCZ-a\AppData\Roaming\HexChat\WINAMPTEST2.py", line 6, in <module>
import winamp2
File "C:\Python27\Lib\winamp2.py", line 28, in <module>
if not pywintypes:
NameError: name 'pywintypes' is not defined
 
8:04 PM
okay, as I've expected :/
 
Zac
its working in my IDLE and commandline enviroment
but not in the XChat IRC python client
I'm guessing because XChat IRC doesn't unload it properly or something
 
Evening all
 
Zac
so was wondering if there is a command that can ensure an import is deimported ?
yeah If I close Xchat
and then load the script it works
 
another Idea @Zac
if not 'pywintypes' in sys.modules: import pywintpes
 
Zac
nope :
Traceback (most recent call last):
File "C:\Users\OCZ-a\AppData\Roaming\HexChat\WINAMPTEST2.py", line 6, in <module>
import winamp2
File "C:\Python27\Lib\winamp2.py", line 29, in <module>
if not 'pywintypes' in sys.modules: import pywintpes
NameError: name 'sys' is not defined
 
8:11 PM
sure, you have to import sys before ^^
 
Anyone know where to download cracked datadirect xquery?
 
Zac
what so I add import sys too ?
 
@Zac: put import sys in the line before
yeah
 
@ColinO'Coal It's worth noting that that's exactly what import module does - at already caches the entry in sys.modules....
@user2213396 are you asking that in every room? :)
 
Zac
now I get : Traceback (most recent call last):
File "C:\Users\OCZ-a\AppData\Roaming\HexChat\WINAMPTEST2.py", line 6, in <module>
import winamp2
File "C:\Python27\Lib\winamp2.py", line 29, in <module>
if not 'pywintypes' in sys.modules: import pywintpes
ImportError: No module named pywintpes
 
8:13 PM
@Jon: Zac is using an IRC environment for loading/unloading python scripts ... and it seems that he has a problem with multiple imports
@Zac: sorry, wrote pywintypes wrong - misspelling
correct it simply
 
Zac
back to this :
Traceback (most recent call last):
File "C:\Users\OCZ-a\AppData\Roaming\HexChat\WINAMPTEST2.py", line 6, in <module>
import winamp2
File "C:\Python27\Lib\winamp2.py", line 29, in <module>
if not 'pywintypes' in sys.modules: import pywintypes
File "C:\Python27\lib\site-packages\win32\lib\pywintypes.py", line 124, in <module>
__import_pywin32_system_module__("pywintypes", globals())
File "C:\Python27\lib\site-packages\win32\lib\pywintypes.py", line 114, in __import_pywin32_system_module__
assert sys.modules[modname] is old_mod
same error as before
 
@Zac: like Jon said ... make no sense ... damn
 
Zac
Its clearly something to do with the IRC client not clearing the program from memory properly
because it works in idle and from command line
and even in pythonwin
but not in Xchat after the first run
It does work on the first run though
 
@Zac: you are using X-Chat or X-Chat2?
 
Zac
its actually called HexChat version 2.9.4
xchat is the python library
sorry if that was confusing
but hexchat was recommended to me
and I'm starting to think I should have used mIRC instead
 
8:23 PM
I'm on the project page and github page .. no prob :).
Mhmm .. no issue regarding problems like you have.
 
Zac
could I replace your conditional load statement for the winamp2 import
if not 'winamp2' import pywintypes
?
erm
I meant
if not 'winamp2' import winamp2
would that work as a statement ?
 
Zac
can you check if an import has already been imported in a generic way ?
 
'<module_name' in sys.modules
it seems unbelieveable
but in your case, line 111 in pywintypes seems to kill the sys.modules content
 
Zac
well if you can be bothered you can download hexchat and try it
This is the script pywinamp.googlecode.com/files/winamp.py
pywinamp.googlecode.com/files/winamp.py
hex chat from here hexchat.org
Its so strange that it works when first loaded after hex chat has started up with no errors
 
8:38 PM
yeah ... and I guess, I fear that the problem based on the virtual python environment
/py load <path_to_your_winamp_file>
right?
 
Zac
from within hexchat
yes
you can do that
or you can use the window menu script load button
Window - > plugin and script - > dialogue
 
okay ... give me some minutes to install all that
 
Zac
thanks you're a star you can have some of my points
 
mhm mhm, palim palim....
oh no thanks ^^
I'm not here in SO for gathering points.
@Zac: okay, got the same error
As I've feard: line 111 in pywintpyes kills sys.modules using on twice
 
Zac
any ideas?
 
8:54 PM
hack pywintypes and you won't get that error again ^^
but that's no mainstream solution I guess
You should push this issue to the HexChat developers
so that they can take a look on this problem
 
Zac
yeah thats a good idea
 
from my side, it seems, that it's an evironment problem, based in the magic of the python plugin (hcpython.dll)
 
Zac
I have another winamp.py file that works fine but doesn't have the getplaylist functions
I might try and transplant those into the other library
 
Oh wait
I've another idea
for what you are using pywintypes exactly?
Just for catching a pywintpyes.error ... right?
 
Zac
I don't know
I didn't write the winamp.py file
I'm just importing it as a library
 
8:58 PM
take a look on line 187
except pywintypes.error, e:
thats the only reference, where the pywintypes symbol has been used
 
Zac
yeah
 

« first day (895 days earlier)      last day (4044 days later) »