How to double a number of binary digits in an integer? For example, if bin(x)="1001" then bin(y) must be "11000011". Is there any smart and fast algorithm ?
Write a function, called constrainedMatchPair which takes three arguments: a tuple representing starting points for the first substring, a tuple representing starting points for the second substring, and the length of the first substring. The function should return a tuple of all members (call it n) of the first tuple for which there is an element in the second tuple (call it k) such that n+m+1 = k, where m is the length of the first substring. Complete the definition
def constrainedMatchPair(firstMatch,secondMatch,length):
Many of my view functions do similar things. For the most part, they reverse to a different views upon clicking a button / a text link.
So I wrote a helper function render_reverse
def render_reverse(f, args): # args are all string type
return eval('reverse(' + f + ', ' + args + ')' )
eval is a bad practice, and is pretty slow. It takes 3 seconds to start redirecting, whereas calling reverse directly takes less than 1 second to start redirecting.
What alternative do I have? By the way, the function above doesn't work properly. I was modelling after this line (which works)
eval('reverse("homepage", args=["abcdefg"])')
Thanks.
I would like to control the location of matplotlib clabels on a contour plot, but without utilizing the manual=True flag in clabel. For example, I would like to specify an x-coordinate, and have labels created at the points that pass through this line. I see that you can get the location of the individual labels using get_position(), but I am stuck at that. Any help would be greatly appreciated. Thanks!
EDIT:
The above image is an example plot that I would like to apply this method to. The default label positions are inconvenient -- the flat areas between Day 2 and Day 4 would be more visually appealing.
I want to save the results of my function binomal_aux to a tuple but I don't have an idea how to, here is my code I have right now.
def binomal (n):
i=0
for i in range(n):
binomal_aux(n,i) #want this to be in a tuple so, binomal (2) = (1,2,1)
return
def binomal_aux (n,k):
if (k==0):
return 1
elif (n==k):
return 1
else:
return (binomal_aux(n-1,k) + binomal_aux(n-1,k-1))
I Have a model like this
foo=models.char
bar=models.dateime
In wich several foos arrives in one day in different time. I need to list all the foos in a specific date, no matter the time they arrive.
I can't change the model, so splitting the bar in two fields(one for date and one for time) is out of reach right now :(
I joined two tables together and what I like to do is concatenate multi vaule in one records without duplicated value.
TAXLOT_ZONE
TID ZONE
1 A
1 A
1 B
1 C
2 D
2 D
2 E
3 A
3 B
4 C
5 D
Desirable Final table looks like;
TID ZONE
1 A, B, C
2 D, E
3 A, B
4 C
5 D
This is code:
http://www.dpaste.de/Ij0S/
1,if there is a erorr (networking erorr,or Unhandled error in Deferred), the code stop.
I need this code running until all urls finish request. (May be the parallel function not work? )
2,when I write image to local filesystem, if meet erorr, there images may not complete.
Thanks!
Is there a good way in Django to convert an entire model to a dictionary? I mean, like this:
class DictModel(models.Model):
key = models.CharField(20)
value = models.CharField(200)
DictModel.objects.all().to_dict()
... with the result being a dictionary with the key/value pairs made up of records in the Model? Has anyone else seen this as being useful for them?
Thanks.
Update
I just wanted to add is that my ultimate goal is to be able to do a simple variable lookup inside a Template. Something like:
{{ DictModel.exampleKey }}
With a result of DictModel.objects.get(key__exact=exampleKey).value
Overall, though, you guys have really surprised me with how helpful allof your responses are, and how different the ways to approach it can be. Thanks a lot.
take = raw_input('Please enter the string of numbers that compose code\n\n\t')
y = str(take)
l = []
for i in xrange(0, len(y), 3):
l.append(str(y[i:i+3]))
b = len(l)
a = 0
while(a!=b):
c = l[a].replace('444', ' ')
c = l[a].replace('111', 'a')
c = l[a].replace('112', 'b')
c = l[a].replace('113', 'c')
c = l[a].replace('114', 'd')
c = l[a].replace('115', 'e')
etc...
a = a + 1
filename = 'decmes.txt'
file = open(filename, 'w')
file.write(c)
file.close()
I can enter anything, just 111 for example and it gives me back the same thing I put in. Maybe it's something dumb, but I can't figure it out.
I am trying to get a list of all existing model fields and properties for a given object. Is there a clean way to instrospect an object so that I can get a dict of fields and properties.
class MyModel(Model)
url = models.TextField()
def _get_location(self):
return "%s/jobs/%d"%(url, self.id)
location = property(_get_location)
What I want is something that returns a dict that looks like this:
{
'id' : 1,
'url':'http://foo',
'location' : 'http://foo/jobs/1'
}
I can use model._meta.fields to get the model fields, but this doesn't give me things that are properties but not real DB fields.
"8,5,,1,4,7,,,,7,,1,9,3,6,,,8,6,3,9,,2,5,4,,,,,3,2,,,7,4,1,1,,4,,6,9,,5,,,,5,,,1,,6,3,,,6,5,,,,7,4,,1,7,6,,,,8,,5,,,7,1,,3,9,"
I'm doing a programming challenge where i need to parse this sequence into my sudoku script.
Need to get the above sequence into 8,5,0,1,4,7,0,0,0,7,0,1,9,3,6,0,0,8.........
I tried re but without success, help is appreciated, thanks.
i have things that requires processing and rarely changes except with certain events to take advantage of memcached. can i store a serial version of an object in a data field quickly?
Hi folks,
is it possible to give users the permission to view, but not to change or delete.
currently in the only permissions I see are "add", "change" and "delete"... but there is no "read/view" in there.
I really need this as some users will only be able to consult the admin panel, in order to see what has been added in.
Help would be amazing!
I'd like everything to function correctly, except when it's mobile, the entire site will used a set of specific templates.
Also, I'd like to autodetect if it's mobile. If so, then use that set of templates throughout the entire site.
| random_code | varchar(200) | YES | UNI | NULL | |
MyTable.objects.filter(random_code = None)
Is this correct? Will this SELECT where there is no random code set? Above is my table.
Hello, I'm developing a web page in Django (using apache server) that needs to call a shell command to enable/dissable some daemons. I'm try to do it with
os.system(service httpd restart 1>$HOME/out 2>$HOME/error)
and this command doesn't return anything. Any idea how can i fix this?
In a class method, I can add attributes using the built-in function:
setattr(self, "var_name", value).
If I want to do the same thing within a module, I can do something like:
globals()["var_name"] = value
Is this the best way to do this, or is there a more pythonic solution?
I'm trying to figure out if there's an elegant and concise way to have a class accessing one of its own properties when "used" as a dictionary, basically redirecting all the methods that'd be implemented in an ordered dictionary to one of its properties.
Currently I'm inheriting from IterableUserDict and explicitly setting its data to another property, and it seems to be working, but I know that UserDict is considered sort of old, and I'm concerned I might be overlooking something.
What I have:
class ConnectionInterface(IterableUserDict):
def __init__(self, hostObject):
self._hostObject= hostObject
self.ports= odict.OrderedDict()
self.inputPorts= odict.OrderedDict()
self.outputPorts= odict.OrderedDict()
self.data= self.ports
This way I expect the object to behave and respond (and be used) the way I mean it to, except I want to get a freebie ordered dictionary behaviour on its property "ports" when it's iterated, items are gotten by key, something is looked up ala if this in myObject, and so on.
Any advice welcome, the above seems to be working fine, but I have an odd itch that I might be missing something.
Thanks in advance.
class a(object):
c=b()# how to call the b method
d=4
def __init__(self):
print self.c
def b(self):
return self.d+1
a()
how to call the 'b' method not in the __init__
thanks
Starting from an Html input like this:
<p>
<a href="http://www.foo.com">this if foo</a>
<a href="http://www.bar.com">this if bar</a>
</p>
using BeautifulSoup, i would like to change this Html in:
<p>
<a href="http://www.foo.com">this if foo</a><b>OK</b>
<a href="http://www.bar.com">this if bar</a><b>OK</b>
</p>
Is it possible to do this using BeautifulSoup?