Is there a fast way to check if one set entirely contains another?
Something like:
>>>[1, 2, 3].containsAll([2, 1])
True
>>>[1, 2, 3].containsAll([3, 5, 9])
False
I'm parsing sentences like:
"CS 2310 or equivalent experience"
The desired output:
[[("CS", 2310)], ["equivalent experience"]]
YACC tokenizer symbols:
tokens = [
'DEPT_CODE',
'COURSE_NUMBER',
'OR_CONJ',
'MISC_TEXT',
]
t_DEPT_CODE = r'[A-Z]{2,}'
t_COURSE_NUMBER = r'[0-9]{4}'
t_OR_CONJ = r'or'
t_ignore = ' \t'
terms = {'DEPT_CODE': t_DEPT_CODE,
'COURSE_NUMBER': t_COURSE_NUMBER,
'OR_CONJ': t_OR_CONJ}
for name, regex in terms.items():
terms[name] = "^%s$" % regex
def t_MISC_TEXT(t):
r'\S+'
for name, regex in terms.items():
# print "trying to match %s with regex %s" % (t.value, regex)
if re.match(regex, t.value):
t.type = name
return t
return t
(MISC_TEXT is meant to match anything not caught by the other terms.)
Some relevant rules from the parser:
precedence = (
('left', 'MISC_TEXT'),
)
def p_statement_course_data(p):
'statement : course_data'
p[0] = p[1]
def p_course_data(p):
'course_data : course'
p[0] = p[1]
def p_course(p):
'course : DEPT_CODE COURSE_NUMBER'
p[0] = make_course(p[1], int(p[2]))
def p_or_phrase(p):
'or_phrase : statement OR_CONJ statement'
p[0] = [[p[1]], [p[3]]]
def p_misc_text(p):
'''text_aggregate : MISC_TEXT MISC_TEXT
| MISC_TEXT text_aggregate
| text_aggregate MISC_TEXT '''
p[0] = "%s %s" % (p[0], [1])
def p_text_aggregate_statement(p):
'statement : text_aggregate'
p[0] = p[1]
Unfortunately, this fails:
# works as it should
>>> token_list("CS 2110 or equivalent experience")
[LexToken(DEPT_CODE,'CS',1,0), LexToken(COURSE_NUMBER,'2110',1,3), LexToken(OR_CONJ,'or',1,8), LexToken(MISC_TEXT,'equivalent',1,11), LexToken(MISC_TEXT,'experience',1,22)]
# fails. bummer.
>>> parser.parse("CS 2110 or equivalent experience")
Syntax error in input: LexToken(MISC_TEXT,'equivalent',1,11)
What am I doing wrong? I don't fully understand how to set precedence rules.
Also, this is my error function:
def p_error(p):
print "Syntax error in input: %s" % p
Is there a way to see which rule the parser was trying when it failed? Or some other way to make the parser print which rules its trying?
Given an array 'a' I would like to sort the array by columns "a.sort(axis=0)" do some stuff to the array and then undo the sort. By that I don't mean re sort but basically reversing how each element was moved. I assume argsort() is what I need but it is not clear to me how to sort an array with the results of argsort() or more importantly apply the reverse/inverse of argsort()
Here is a little more detail
I have an array a, shape(a) = rXc I need to sort each column
aargsort = a.argsort(axis=0) # May use this later
aSort = a.sort(axis=0)
now average each row
aSortRM = asort.mean(axis=1)
now replace each col in a row with the row mean.
is there a better way than this
aWithMeans = ones_like(a)
for ind in range(r) # r = number of rows
aWithMeans[ind]* aSortRM[ind]
Now I need to undo the sort I did in the first step. ????
Is there any possibility to copy variable by reference no matter if its int or class instance?
My goal is to have two lists of the same objects and when one changes, change is visible in second.
In other words i need pointers:/
I have a simple GAE app that includes a login/logout link. This app is running on the dev server at the moment.
The base page handler gets the current user, and creates a login/logout url appropriately. It then puts this information into a _template_data dictionary, for convenience of subclasses.
class BasePage(webapp.RequestHandler):
_user = users.get_current_user()
_login_logout_link = None
if _user:
_login_logout_link = users.create_logout_url('/')
else:
_login_logout_link = users.create_login_url('/')
_template_data = {}
_template_data['login_logout_link'] = _login_logout_link
_template_data['user'] = _user
def render(self, templateName, templateData):
path = os.path.join(os.path.dirname(__file__), 'Static/Templates/%s.html' % templateName)
self.response.out.write(template.render(path, templateData))
Here is one such subclass:
class MainPage(BasePage):
def get(self):
self.render('start', self._template_data)
The login/logout link is displayed fine, and going to the correct devserver login/logout page. However, it seems to have no effect - the server still seems to think the user is logged out. What am I doing wrong here?
I open a website with urlopen. I then put the website sourcecode into a variable like so
source = website.read()
When I just print the source it comes out formatted correctly, however when I try to iterate through each line each character is it's own line.
for example
when I just print it looks like this
<HTML> title</html>
When I do this
for line in source:
print line
it looks like this
<
H
T
M
L
... etc
I need to find a string that starts with "var" and then print that entire line.
I'm currently using the walk method in a uni assignment. It's all working fine, but I was hoping that someone could explain something to me.
in the example below, what is the a parameter used for on the myvisit method?
>>> from os.path import walk
>>> def myvisit(a, dir, files):
... print dir,": %d files"%len(files)
>>> walk('/etc', myvisit, None)
/etc : 193 files
/etc/default : 12 files
/etc/cron.d : 6 files
/etc/rc.d : 6 files
/etc/rc.d/rc0.d : 18 files
/etc/rc.d/rc1.d : 27 files
/etc/rc.d/rc2.d : 42 files
/etc/rc.d/rc3.d : 17 files
/etc/rc.d/rcS.d : 13 files
some case i write code like these:
a,temp,b = s.partition('-')
i just need to pick the first and 3rd element. temp would never be used. is there a better way to do this?
the common case is ,a better way to pick separted element to make a new list?
for example
i want to make a new list use old list 0,1,3,7 element
code would be this:
newlist = [oldlist[0],oldlist[1],oldlist[3],oldlist[7]]
it's pretty ugly,isn't it?
I'm building a program that will sum digits in a given list in a recursive way. Say, if the source list has 10 elements, the second list will have 9, the third 8 and so on until the last list that will have only one element. This is done by adding the first element to the second, then the second to the third and so on. I'm stuck without feedback from the shell. It halts without throwing any errors, then in a couple of seconds the fan is spinning like crazy.
I've read quite a few posts here and changed my approach, but I'm not sure that what have so far can produce the results I'm looking for. Thanks in advance:
#---------------------------------------------------
#functions
#---------------------------------------------------
#sum up pairs in a list
def reduce(inputList):
i = 0
while (i < len(inputList)):
#ref to current and next item
j = i + 1
#don't go for the last item
if j != len(inputList):
#new number eq current + next number
newNumber = inputList[i] + inputList[j]
if newNumber >= 10:
#reduce newNumber to single digit
newNumber = sum(map(int, str(newNumber)))
#collect into temp list
outputList.append(newNumber)
i = i + 1
return outputList;
#---------------------------------------------------
#program starts here
#---------------------------------------------------
outputList = []
sourceList = [7, 3, 1, 2, 1, 4, 6]
counter = len(sourceList)
dict = {}
dict[0] = sourceList
print '-------------'
print 'Level 0:', dict[0]
for i in range(counter):
j = i + 1
if j != counter:
baseList = dict.get(i)
#check function to understand what it does
newList = reduce(baseList)
#new key and value from previous/transformed value
dict[j] = newList
print 'Level %d: %s' % (j, dict[j])
Hello.
For my project I would be using the argparse library. My question is, how do I distribute it with my project. I am asking this because of the technicalities and legalities involved.
Do I just:
Put the argparse.py file along with
my project. That is, in the tar file for my project.
Create a package for it for my
distro?
Tell the user to install it himself?
Sorry for being such a noob, but I new to all this.
I have a list of sublists, and I want to see if any of the integer values from the first sublist plus one are contained in the second sublist. For all such values, I want to see if that value plus one is contained in the third sublist, and so on, proceeding in this fashion across all sublists. If there is a way of proceeding in this fashion from the first sublist to the last sublist, I wish to return True; otherwise I wish to return False. In other words, for each value in sublist one, for each "step" in a "walk" across all sublists read left to right, if that value + n (where n = number of steps taken) is contained in the current sublist, the function should return True; otherwise it should return False. (Sorry for the clumsy phrasing--I'm not sure how to clean up my language without using many more words.)
Here's what I wrote.
a = [ [1,3],[2,4],[3,5],[6],[7] ]
def find_list_traversing_walk(l):
for i in l[0]:
index_position = 0
first_pass = 1
walking_current_path = 1
while walking_current_path == 1:
if first_pass == 1:
first_pass = 0
walking_value = i
if walking_value+1 in l[index_position + 1]:
index_position += 1
walking_value += 1
if index_position+1 == len(l):
print "There is a walk across the sublists for initial value ", walking_value - index_position
return True
else:
walking_current_path = 0
return False
print find_list_traversing_walk(a)
My question is: Have I overlooked something simple here, or will this function return True for all true positives and False for all true negatives? Are there easier ways to accomplish the intended task? I would be grateful for any feedback others can offer!
is there a way that if the following class is created; I can grab a list of attributes that exist. (this class is just an bland example, it is not my task at hand)
class new_class():
def __init__(self, number):
self.multi = int(number) * 2
self.str = str(number)
a = new_class(2)
print(', '.join(a.SOMETHING))
* the attempt is that "multi, str" will print. the point here is that if a class object has attributes added at different parts of a script that I can grab a quick listing of the attributes which are defined.
Is there a way to get the timezone of the connecting user using Pylons, and to adjust the content before rendering accordingly? Or can this only be done by JS?
Thanks.
suppose I have a dictionary whose keys are strings. How can I efficiently make a new dictionary from that which contains only the keys present in some list?
for example:
# a dictionary mapping strings to stuff
mydict = {'quux': ...,
'bar': ...,
'foo': ...}
# list of keys to be selected from mydict
keys_to_select = ['foo', 'bar', ...]
The way I came up with is:
filtered_mydict = [mydict[k] for k in mydict.keys() \
if k in keys_to_select]
but I think this is highly inefficient because: (1) it requires enumerating the keys with keys(), (2) it requires looking up k in keys_to_select each time. at least one of these can be avoided, I would think. any ideas? I can use scipy/numpy too if needed.
Say I have the folowing code:
class Class1(object):
def __init__(self):
self.my_attr = 1
self.my_other_attr = 2
class Class2(Class1):
def __init__(self):
super(Class1,self).__init__()
Why does Class2 not inherit the attributes of Class1?
This doesn't need to be a real time solution, but are there some log files or system messages that could be read to identify periods of time where someone was connected via RDP to a Windows 7 machine?
I'm building a watchdog script for a computer which will be deployed in a remote place and would like to add this metric to a daily status update.
I don't know why I am having so much trouble creating a 3 dimensional list.
I need the program to create an empty n by n list. So for n = 4:
x = [[[],[],[],[]],[[],[],[],[]],[[],[],[],[]],[[],[],[],[]]]
I've tried using:
y = [n*[n*[]]]
y = [[[]]* n for i in range(n)]
Which both appear to be creating copies of a reference.
I've also tried naieve application of the list builder with little success:
y = [[[]* n for i in range(n)]* n for i in range(n)]
y = [[[]* n for i in range(1)]* n for i in range(n)]
I've also tried building up the array iteratively using loops, with no success. In my rapid flurry of attempts to not post something stupidly easy to SO, I came upon a solution:
y = []
for i in range(0,n):
y.append([[]*n for i in range(n)])
Is there an easier/ more intuitive way of doing this?
Hi folks,
I'm looking for a way to read in c++ a text file containing numpy arrays and put the data into vector , can anyone help me out please ?
Thanks a lot.
Archy
I am hoping to write a script that will allow for the detection of video on a url and provide a download link to a *flv for google chrome.
Anyone have any suggestions were to start and get a footing?
I want to implement a code that loops inside an array that its size is set by the user that means that the size isn't constant.
for example:
A=[1,2,3,4,5]
then I want the output to be like this:
[1],[2],[3],[4],[5]
[1,2],[1,3],[1,4],[1,5]
[2,3],[2,4],[2,5]
[3,4],[3,5]
[4,5]
[1,2,3],[1,2,4],[1,2,5]
[1,3,4],[1,3,5]
and so on
[1,2,3,4],[1,2,3,5]
[2,3,4,5]
[1,2,3,4,5]
Can you help me implement this code?
I need to delete some unicode symbols from the string '?????? ??????? ???????????? ??????????'
I know they exist here for sure. I try:
re.sub('([\u064B-\u0652\u06D4\u0670\u0674\u06D5-\u06ED]+)', '', '?????? ??????? ???????????? ??????????')
but it doesn't work. String stays the same. ant suggestion what i do wrong?